Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
8f18cf13 | 19 | #include <linux/kernel.h> |
065631f6 | 20 | #include <linux/bio.h> |
39279cc3 | 21 | #include <linux/buffer_head.h> |
f2eb0a24 | 22 | #include <linux/file.h> |
39279cc3 CM |
23 | #include <linux/fs.h> |
24 | #include <linux/pagemap.h> | |
25 | #include <linux/highmem.h> | |
26 | #include <linux/time.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/string.h> | |
39279cc3 CM |
29 | #include <linux/backing-dev.h> |
30 | #include <linux/mpage.h> | |
31 | #include <linux/swap.h> | |
32 | #include <linux/writeback.h> | |
33 | #include <linux/statfs.h> | |
34 | #include <linux/compat.h> | |
9ebefb18 | 35 | #include <linux/bit_spinlock.h> |
5103e947 | 36 | #include <linux/xattr.h> |
33268eaf | 37 | #include <linux/posix_acl.h> |
d899e052 | 38 | #include <linux/falloc.h> |
5a0e3ad6 | 39 | #include <linux/slab.h> |
7a36ddec | 40 | #include <linux/ratelimit.h> |
22c44fe6 | 41 | #include <linux/mount.h> |
55e301fd | 42 | #include <linux/btrfs.h> |
53b381b3 | 43 | #include <linux/blkdev.h> |
f23b5a59 | 44 | #include <linux/posix_acl_xattr.h> |
e2e40f2c | 45 | #include <linux/uio.h> |
39279cc3 CM |
46 | #include "ctree.h" |
47 | #include "disk-io.h" | |
48 | #include "transaction.h" | |
49 | #include "btrfs_inode.h" | |
39279cc3 | 50 | #include "print-tree.h" |
e6dcd2dc | 51 | #include "ordered-data.h" |
95819c05 | 52 | #include "xattr.h" |
e02119d5 | 53 | #include "tree-log.h" |
4a54c8c1 | 54 | #include "volumes.h" |
c8b97818 | 55 | #include "compression.h" |
b4ce94de | 56 | #include "locking.h" |
dc89e982 | 57 | #include "free-space-cache.h" |
581bb050 | 58 | #include "inode-map.h" |
38c227d8 | 59 | #include "backref.h" |
f23b5a59 | 60 | #include "hash.h" |
63541927 | 61 | #include "props.h" |
31193213 | 62 | #include "qgroup.h" |
dda3245e | 63 | #include "dedupe.h" |
39279cc3 CM |
64 | |
65 | struct btrfs_iget_args { | |
90d3e592 | 66 | struct btrfs_key *location; |
39279cc3 CM |
67 | struct btrfs_root *root; |
68 | }; | |
69 | ||
f28a4928 FM |
70 | struct btrfs_dio_data { |
71 | u64 outstanding_extents; | |
72 | u64 reserve; | |
73 | u64 unsubmitted_oe_range_start; | |
74 | u64 unsubmitted_oe_range_end; | |
75 | }; | |
76 | ||
6e1d5dcc AD |
77 | static const struct inode_operations btrfs_dir_inode_operations; |
78 | static const struct inode_operations btrfs_symlink_inode_operations; | |
79 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
80 | static const struct inode_operations btrfs_special_inode_operations; | |
81 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b AD |
82 | static const struct address_space_operations btrfs_aops; |
83 | static const struct address_space_operations btrfs_symlink_aops; | |
828c0950 | 84 | static const struct file_operations btrfs_dir_file_operations; |
20e5506b | 85 | static const struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
86 | |
87 | static struct kmem_cache *btrfs_inode_cachep; | |
88 | struct kmem_cache *btrfs_trans_handle_cachep; | |
89 | struct kmem_cache *btrfs_transaction_cachep; | |
39279cc3 | 90 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 91 | struct kmem_cache *btrfs_free_space_cachep; |
39279cc3 CM |
92 | |
93 | #define S_SHIFT 12 | |
4d4ab6d6 | 94 | static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
39279cc3 CM |
95 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
96 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
97 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
98 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
99 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
100 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
101 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
102 | }; | |
103 | ||
3972f260 | 104 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
a41ad394 | 105 | static int btrfs_truncate(struct inode *inode); |
5fd02043 | 106 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
771ed689 CM |
107 | static noinline int cow_file_range(struct inode *inode, |
108 | struct page *locked_page, | |
dda3245e WX |
109 | u64 start, u64 end, u64 delalloc_end, |
110 | int *page_started, unsigned long *nr_written, | |
111 | int unlock, struct btrfs_dedupe_hash *hash); | |
70c8a91c JB |
112 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
113 | u64 len, u64 orig_start, | |
114 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
115 | u64 orig_block_len, u64 ram_bytes, |
116 | int type); | |
7b128766 | 117 | |
48a3b636 | 118 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 119 | |
6a3891c5 JB |
120 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
121 | void btrfs_test_inode_set_ops(struct inode *inode) | |
122 | { | |
123 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
124 | } | |
125 | #endif | |
126 | ||
f34f57a3 | 127 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
128 | struct inode *inode, struct inode *dir, |
129 | const struct qstr *qstr) | |
0279b4cd JO |
130 | { |
131 | int err; | |
132 | ||
f34f57a3 | 133 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 134 | if (!err) |
2a7dba39 | 135 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
136 | return err; |
137 | } | |
138 | ||
c8b97818 CM |
139 | /* |
140 | * this does all the hard work for inserting an inline extent into | |
141 | * the btree. The caller should have done a btrfs_drop_extents so that | |
142 | * no overlapping inline items exist in the btree | |
143 | */ | |
40f76580 | 144 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 145 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
146 | struct btrfs_root *root, struct inode *inode, |
147 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 148 | int compress_type, |
c8b97818 CM |
149 | struct page **compressed_pages) |
150 | { | |
c8b97818 CM |
151 | struct extent_buffer *leaf; |
152 | struct page *page = NULL; | |
153 | char *kaddr; | |
154 | unsigned long ptr; | |
155 | struct btrfs_file_extent_item *ei; | |
156 | int err = 0; | |
157 | int ret; | |
158 | size_t cur_size = size; | |
c8b97818 | 159 | unsigned long offset; |
c8b97818 | 160 | |
fe3f566c | 161 | if (compressed_size && compressed_pages) |
c8b97818 | 162 | cur_size = compressed_size; |
c8b97818 | 163 | |
1acae57b | 164 | inode_add_bytes(inode, size); |
c8b97818 | 165 | |
1acae57b FDBM |
166 | if (!extent_inserted) { |
167 | struct btrfs_key key; | |
168 | size_t datasize; | |
c8b97818 | 169 | |
1acae57b FDBM |
170 | key.objectid = btrfs_ino(inode); |
171 | key.offset = start; | |
962a298f | 172 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 173 | |
1acae57b FDBM |
174 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
175 | path->leave_spinning = 1; | |
176 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
177 | datasize); | |
178 | if (ret) { | |
179 | err = ret; | |
180 | goto fail; | |
181 | } | |
c8b97818 CM |
182 | } |
183 | leaf = path->nodes[0]; | |
184 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
185 | struct btrfs_file_extent_item); | |
186 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
187 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
188 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
189 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
190 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
191 | ptr = btrfs_file_extent_inline_start(ei); | |
192 | ||
261507a0 | 193 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
194 | struct page *cpage; |
195 | int i = 0; | |
d397712b | 196 | while (compressed_size > 0) { |
c8b97818 | 197 | cpage = compressed_pages[i]; |
5b050f04 | 198 | cur_size = min_t(unsigned long, compressed_size, |
09cbfeaf | 199 | PAGE_SIZE); |
c8b97818 | 200 | |
7ac687d9 | 201 | kaddr = kmap_atomic(cpage); |
c8b97818 | 202 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 203 | kunmap_atomic(kaddr); |
c8b97818 CM |
204 | |
205 | i++; | |
206 | ptr += cur_size; | |
207 | compressed_size -= cur_size; | |
208 | } | |
209 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 210 | compress_type); |
c8b97818 CM |
211 | } else { |
212 | page = find_get_page(inode->i_mapping, | |
09cbfeaf | 213 | start >> PAGE_SHIFT); |
c8b97818 | 214 | btrfs_set_file_extent_compression(leaf, ei, 0); |
7ac687d9 | 215 | kaddr = kmap_atomic(page); |
09cbfeaf | 216 | offset = start & (PAGE_SIZE - 1); |
c8b97818 | 217 | write_extent_buffer(leaf, kaddr + offset, ptr, size); |
7ac687d9 | 218 | kunmap_atomic(kaddr); |
09cbfeaf | 219 | put_page(page); |
c8b97818 CM |
220 | } |
221 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 222 | btrfs_release_path(path); |
c8b97818 | 223 | |
c2167754 YZ |
224 | /* |
225 | * we're an inline extent, so nobody can | |
226 | * extend the file past i_size without locking | |
227 | * a page we already have locked. | |
228 | * | |
229 | * We must do any isize and inode updates | |
230 | * before we unlock the pages. Otherwise we | |
231 | * could end up racing with unlink. | |
232 | */ | |
c8b97818 | 233 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 234 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 235 | |
79787eaa | 236 | return ret; |
c8b97818 | 237 | fail: |
c8b97818 CM |
238 | return err; |
239 | } | |
240 | ||
241 | ||
242 | /* | |
243 | * conditionally insert an inline extent into the file. This | |
244 | * does the checks required to make sure the data is small enough | |
245 | * to fit as an inline extent. | |
246 | */ | |
00361589 JB |
247 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
248 | struct inode *inode, u64 start, | |
249 | u64 end, size_t compressed_size, | |
250 | int compress_type, | |
251 | struct page **compressed_pages) | |
c8b97818 | 252 | { |
00361589 | 253 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
254 | u64 isize = i_size_read(inode); |
255 | u64 actual_end = min(end + 1, isize); | |
256 | u64 inline_len = actual_end - start; | |
fda2832f | 257 | u64 aligned_end = ALIGN(end, root->sectorsize); |
c8b97818 CM |
258 | u64 data_len = inline_len; |
259 | int ret; | |
1acae57b FDBM |
260 | struct btrfs_path *path; |
261 | int extent_inserted = 0; | |
262 | u32 extent_item_size; | |
c8b97818 CM |
263 | |
264 | if (compressed_size) | |
265 | data_len = compressed_size; | |
266 | ||
267 | if (start > 0 || | |
0c29ba99 | 268 | actual_end > root->sectorsize || |
354877be | 269 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || |
c8b97818 CM |
270 | (!compressed_size && |
271 | (actual_end & (root->sectorsize - 1)) == 0) || | |
272 | end + 1 < isize || | |
273 | data_len > root->fs_info->max_inline) { | |
274 | return 1; | |
275 | } | |
276 | ||
1acae57b FDBM |
277 | path = btrfs_alloc_path(); |
278 | if (!path) | |
279 | return -ENOMEM; | |
280 | ||
00361589 | 281 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
282 | if (IS_ERR(trans)) { |
283 | btrfs_free_path(path); | |
00361589 | 284 | return PTR_ERR(trans); |
1acae57b | 285 | } |
00361589 JB |
286 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
287 | ||
1acae57b FDBM |
288 | if (compressed_size && compressed_pages) |
289 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
290 | compressed_size); | |
291 | else | |
292 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
293 | inline_len); | |
294 | ||
295 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
296 | start, aligned_end, NULL, | |
297 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 JB |
298 | if (ret) { |
299 | btrfs_abort_transaction(trans, root, ret); | |
300 | goto out; | |
301 | } | |
c8b97818 CM |
302 | |
303 | if (isize > actual_end) | |
304 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
305 | ret = insert_inline_extent(trans, path, extent_inserted, |
306 | root, inode, start, | |
c8b97818 | 307 | inline_len, compressed_size, |
fe3f566c | 308 | compress_type, compressed_pages); |
2adcac1a | 309 | if (ret && ret != -ENOSPC) { |
79787eaa | 310 | btrfs_abort_transaction(trans, root, ret); |
00361589 | 311 | goto out; |
2adcac1a | 312 | } else if (ret == -ENOSPC) { |
00361589 JB |
313 | ret = 1; |
314 | goto out; | |
79787eaa | 315 | } |
2adcac1a | 316 | |
bdc20e67 | 317 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
0ca1f7ce | 318 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
a1ed835e | 319 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
00361589 | 320 | out: |
94ed938a QW |
321 | /* |
322 | * Don't forget to free the reserved space, as for inlined extent | |
323 | * it won't count as data extent, free them directly here. | |
324 | * And at reserve time, it's always aligned to page size, so | |
325 | * just free one page here. | |
326 | */ | |
09cbfeaf | 327 | btrfs_qgroup_free_data(inode, 0, PAGE_SIZE); |
1acae57b | 328 | btrfs_free_path(path); |
00361589 JB |
329 | btrfs_end_transaction(trans, root); |
330 | return ret; | |
c8b97818 CM |
331 | } |
332 | ||
771ed689 CM |
333 | struct async_extent { |
334 | u64 start; | |
335 | u64 ram_size; | |
336 | u64 compressed_size; | |
337 | struct page **pages; | |
338 | unsigned long nr_pages; | |
261507a0 | 339 | int compress_type; |
771ed689 CM |
340 | struct list_head list; |
341 | }; | |
342 | ||
343 | struct async_cow { | |
344 | struct inode *inode; | |
345 | struct btrfs_root *root; | |
346 | struct page *locked_page; | |
347 | u64 start; | |
348 | u64 end; | |
349 | struct list_head extents; | |
350 | struct btrfs_work work; | |
351 | }; | |
352 | ||
353 | static noinline int add_async_extent(struct async_cow *cow, | |
354 | u64 start, u64 ram_size, | |
355 | u64 compressed_size, | |
356 | struct page **pages, | |
261507a0 LZ |
357 | unsigned long nr_pages, |
358 | int compress_type) | |
771ed689 CM |
359 | { |
360 | struct async_extent *async_extent; | |
361 | ||
362 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 363 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
364 | async_extent->start = start; |
365 | async_extent->ram_size = ram_size; | |
366 | async_extent->compressed_size = compressed_size; | |
367 | async_extent->pages = pages; | |
368 | async_extent->nr_pages = nr_pages; | |
261507a0 | 369 | async_extent->compress_type = compress_type; |
771ed689 CM |
370 | list_add_tail(&async_extent->list, &cow->extents); |
371 | return 0; | |
372 | } | |
373 | ||
f79707b0 WS |
374 | static inline int inode_need_compress(struct inode *inode) |
375 | { | |
376 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
377 | ||
378 | /* force compress */ | |
3cdde224 | 379 | if (btrfs_test_opt(root->fs_info, FORCE_COMPRESS)) |
f79707b0 WS |
380 | return 1; |
381 | /* bad compression ratios */ | |
382 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
383 | return 0; | |
3cdde224 | 384 | if (btrfs_test_opt(root->fs_info, COMPRESS) || |
f79707b0 WS |
385 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || |
386 | BTRFS_I(inode)->force_compress) | |
387 | return 1; | |
388 | return 0; | |
389 | } | |
390 | ||
d352ac68 | 391 | /* |
771ed689 CM |
392 | * we create compressed extents in two phases. The first |
393 | * phase compresses a range of pages that have already been | |
394 | * locked (both pages and state bits are locked). | |
c8b97818 | 395 | * |
771ed689 CM |
396 | * This is done inside an ordered work queue, and the compression |
397 | * is spread across many cpus. The actual IO submission is step | |
398 | * two, and the ordered work queue takes care of making sure that | |
399 | * happens in the same order things were put onto the queue by | |
400 | * writepages and friends. | |
c8b97818 | 401 | * |
771ed689 CM |
402 | * If this code finds it can't get good compression, it puts an |
403 | * entry onto the work queue to write the uncompressed bytes. This | |
404 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
405 | * are written in the same order that the flusher thread sent them |
406 | * down. | |
d352ac68 | 407 | */ |
c44f649e | 408 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
409 | struct page *locked_page, |
410 | u64 start, u64 end, | |
411 | struct async_cow *async_cow, | |
412 | int *num_added) | |
b888db2b CM |
413 | { |
414 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
db94535d | 415 | u64 num_bytes; |
db94535d | 416 | u64 blocksize = root->sectorsize; |
c8b97818 | 417 | u64 actual_end; |
42dc7bab | 418 | u64 isize = i_size_read(inode); |
e6dcd2dc | 419 | int ret = 0; |
c8b97818 CM |
420 | struct page **pages = NULL; |
421 | unsigned long nr_pages; | |
422 | unsigned long nr_pages_ret = 0; | |
423 | unsigned long total_compressed = 0; | |
424 | unsigned long total_in = 0; | |
ee22184b BL |
425 | unsigned long max_compressed = SZ_128K; |
426 | unsigned long max_uncompressed = SZ_128K; | |
c8b97818 CM |
427 | int i; |
428 | int will_compress; | |
261507a0 | 429 | int compress_type = root->fs_info->compress_type; |
4adaa611 | 430 | int redirty = 0; |
b888db2b | 431 | |
4cb13e5d | 432 | /* if this is a small write inside eof, kick off a defrag */ |
ee22184b | 433 | if ((end - start + 1) < SZ_16K && |
4cb13e5d | 434 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
4cb5300b CM |
435 | btrfs_add_inode_defrag(NULL, inode); |
436 | ||
42dc7bab | 437 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
438 | again: |
439 | will_compress = 0; | |
09cbfeaf KS |
440 | nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1; |
441 | nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE); | |
be20aa9d | 442 | |
f03d9301 CM |
443 | /* |
444 | * we don't want to send crud past the end of i_size through | |
445 | * compression, that's just a waste of CPU time. So, if the | |
446 | * end of the file is before the start of our current | |
447 | * requested range of bytes, we bail out to the uncompressed | |
448 | * cleanup code that can deal with all of this. | |
449 | * | |
450 | * It isn't really the fastest way to fix things, but this is a | |
451 | * very uncommon corner. | |
452 | */ | |
453 | if (actual_end <= start) | |
454 | goto cleanup_and_bail_uncompressed; | |
455 | ||
c8b97818 CM |
456 | total_compressed = actual_end - start; |
457 | ||
4bcbb332 SW |
458 | /* |
459 | * skip compression for a small file range(<=blocksize) that | |
01327610 | 460 | * isn't an inline extent, since it doesn't save disk space at all. |
4bcbb332 SW |
461 | */ |
462 | if (total_compressed <= blocksize && | |
463 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
464 | goto cleanup_and_bail_uncompressed; | |
465 | ||
c8b97818 CM |
466 | /* we want to make sure that amount of ram required to uncompress |
467 | * an extent is reasonable, so we limit the total size in ram | |
771ed689 CM |
468 | * of a compressed extent to 128k. This is a crucial number |
469 | * because it also controls how easily we can spread reads across | |
470 | * cpus for decompression. | |
471 | * | |
472 | * We also want to make sure the amount of IO required to do | |
473 | * a random read is reasonably small, so we limit the size of | |
474 | * a compressed extent to 128k. | |
c8b97818 CM |
475 | */ |
476 | total_compressed = min(total_compressed, max_uncompressed); | |
fda2832f | 477 | num_bytes = ALIGN(end - start + 1, blocksize); |
be20aa9d | 478 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
479 | total_in = 0; |
480 | ret = 0; | |
db94535d | 481 | |
771ed689 CM |
482 | /* |
483 | * we do compression for mount -o compress and when the | |
484 | * inode has not been flagged as nocompress. This flag can | |
485 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 486 | */ |
f79707b0 | 487 | if (inode_need_compress(inode)) { |
c8b97818 | 488 | WARN_ON(pages); |
31e818fe | 489 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
490 | if (!pages) { |
491 | /* just bail out to the uncompressed code */ | |
492 | goto cont; | |
493 | } | |
c8b97818 | 494 | |
261507a0 LZ |
495 | if (BTRFS_I(inode)->force_compress) |
496 | compress_type = BTRFS_I(inode)->force_compress; | |
497 | ||
4adaa611 CM |
498 | /* |
499 | * we need to call clear_page_dirty_for_io on each | |
500 | * page in the range. Otherwise applications with the file | |
501 | * mmap'd can wander in and change the page contents while | |
502 | * we are compressing them. | |
503 | * | |
504 | * If the compression fails for any reason, we set the pages | |
505 | * dirty again later on. | |
506 | */ | |
507 | extent_range_clear_dirty_for_io(inode, start, end); | |
508 | redirty = 1; | |
261507a0 LZ |
509 | ret = btrfs_compress_pages(compress_type, |
510 | inode->i_mapping, start, | |
511 | total_compressed, pages, | |
512 | nr_pages, &nr_pages_ret, | |
513 | &total_in, | |
514 | &total_compressed, | |
515 | max_compressed); | |
c8b97818 CM |
516 | |
517 | if (!ret) { | |
518 | unsigned long offset = total_compressed & | |
09cbfeaf | 519 | (PAGE_SIZE - 1); |
c8b97818 CM |
520 | struct page *page = pages[nr_pages_ret - 1]; |
521 | char *kaddr; | |
522 | ||
523 | /* zero the tail end of the last page, we might be | |
524 | * sending it down to disk | |
525 | */ | |
526 | if (offset) { | |
7ac687d9 | 527 | kaddr = kmap_atomic(page); |
c8b97818 | 528 | memset(kaddr + offset, 0, |
09cbfeaf | 529 | PAGE_SIZE - offset); |
7ac687d9 | 530 | kunmap_atomic(kaddr); |
c8b97818 CM |
531 | } |
532 | will_compress = 1; | |
533 | } | |
534 | } | |
560f7d75 | 535 | cont: |
c8b97818 CM |
536 | if (start == 0) { |
537 | /* lets try to make an inline extent */ | |
771ed689 | 538 | if (ret || total_in < (actual_end - start)) { |
c8b97818 | 539 | /* we didn't compress the entire range, try |
771ed689 | 540 | * to make an uncompressed inline extent. |
c8b97818 | 541 | */ |
00361589 JB |
542 | ret = cow_file_range_inline(root, inode, start, end, |
543 | 0, 0, NULL); | |
c8b97818 | 544 | } else { |
771ed689 | 545 | /* try making a compressed inline extent */ |
00361589 | 546 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
547 | total_compressed, |
548 | compress_type, pages); | |
c8b97818 | 549 | } |
79787eaa | 550 | if (ret <= 0) { |
151a41bc JB |
551 | unsigned long clear_flags = EXTENT_DELALLOC | |
552 | EXTENT_DEFRAG; | |
e6eb4314 FM |
553 | unsigned long page_error_op; |
554 | ||
151a41bc | 555 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
e6eb4314 | 556 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 557 | |
771ed689 | 558 | /* |
79787eaa JM |
559 | * inline extent creation worked or returned error, |
560 | * we don't need to create any more async work items. | |
561 | * Unlock and free up our temp pages. | |
771ed689 | 562 | */ |
c2790a2e | 563 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
151a41bc | 564 | clear_flags, PAGE_UNLOCK | |
c2790a2e JB |
565 | PAGE_CLEAR_DIRTY | |
566 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 567 | page_error_op | |
c2790a2e | 568 | PAGE_END_WRITEBACK); |
c8b97818 CM |
569 | goto free_pages_out; |
570 | } | |
571 | } | |
572 | ||
573 | if (will_compress) { | |
574 | /* | |
575 | * we aren't doing an inline extent round the compressed size | |
576 | * up to a block size boundary so the allocator does sane | |
577 | * things | |
578 | */ | |
fda2832f | 579 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
580 | |
581 | /* | |
582 | * one last check to make sure the compression is really a | |
583 | * win, compare the page count read with the blocks on disk | |
584 | */ | |
09cbfeaf | 585 | total_in = ALIGN(total_in, PAGE_SIZE); |
c8b97818 CM |
586 | if (total_compressed >= total_in) { |
587 | will_compress = 0; | |
588 | } else { | |
c8b97818 | 589 | num_bytes = total_in; |
c8bb0c8b AS |
590 | *num_added += 1; |
591 | ||
592 | /* | |
593 | * The async work queues will take care of doing actual | |
594 | * allocation on disk for these compressed pages, and | |
595 | * will submit them to the elevator. | |
596 | */ | |
597 | add_async_extent(async_cow, start, num_bytes, | |
598 | total_compressed, pages, nr_pages_ret, | |
599 | compress_type); | |
600 | ||
601 | if (start + num_bytes < end) { | |
602 | start += num_bytes; | |
603 | pages = NULL; | |
604 | cond_resched(); | |
605 | goto again; | |
606 | } | |
607 | return; | |
c8b97818 CM |
608 | } |
609 | } | |
c8bb0c8b | 610 | if (pages) { |
c8b97818 CM |
611 | /* |
612 | * the compression code ran but failed to make things smaller, | |
613 | * free any pages it allocated and our page pointer array | |
614 | */ | |
615 | for (i = 0; i < nr_pages_ret; i++) { | |
70b99e69 | 616 | WARN_ON(pages[i]->mapping); |
09cbfeaf | 617 | put_page(pages[i]); |
c8b97818 CM |
618 | } |
619 | kfree(pages); | |
620 | pages = NULL; | |
621 | total_compressed = 0; | |
622 | nr_pages_ret = 0; | |
623 | ||
624 | /* flag the file so we don't compress in the future */ | |
3cdde224 | 625 | if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) && |
1e701a32 | 626 | !(BTRFS_I(inode)->force_compress)) { |
a555f810 | 627 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 628 | } |
c8b97818 | 629 | } |
f03d9301 | 630 | cleanup_and_bail_uncompressed: |
c8bb0c8b AS |
631 | /* |
632 | * No compression, but we still need to write the pages in the file | |
633 | * we've been given so far. redirty the locked page if it corresponds | |
634 | * to our extent and set things up for the async work queue to run | |
635 | * cow_file_range to do the normal delalloc dance. | |
636 | */ | |
637 | if (page_offset(locked_page) >= start && | |
638 | page_offset(locked_page) <= end) | |
639 | __set_page_dirty_nobuffers(locked_page); | |
640 | /* unlocked later on in the async handlers */ | |
641 | ||
642 | if (redirty) | |
643 | extent_range_redirty_for_io(inode, start, end); | |
644 | add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0, | |
645 | BTRFS_COMPRESS_NONE); | |
646 | *num_added += 1; | |
3b951516 | 647 | |
c44f649e | 648 | return; |
771ed689 CM |
649 | |
650 | free_pages_out: | |
651 | for (i = 0; i < nr_pages_ret; i++) { | |
652 | WARN_ON(pages[i]->mapping); | |
09cbfeaf | 653 | put_page(pages[i]); |
771ed689 | 654 | } |
d397712b | 655 | kfree(pages); |
771ed689 | 656 | } |
771ed689 | 657 | |
40ae837b FM |
658 | static void free_async_extent_pages(struct async_extent *async_extent) |
659 | { | |
660 | int i; | |
661 | ||
662 | if (!async_extent->pages) | |
663 | return; | |
664 | ||
665 | for (i = 0; i < async_extent->nr_pages; i++) { | |
666 | WARN_ON(async_extent->pages[i]->mapping); | |
09cbfeaf | 667 | put_page(async_extent->pages[i]); |
40ae837b FM |
668 | } |
669 | kfree(async_extent->pages); | |
670 | async_extent->nr_pages = 0; | |
671 | async_extent->pages = NULL; | |
771ed689 CM |
672 | } |
673 | ||
674 | /* | |
675 | * phase two of compressed writeback. This is the ordered portion | |
676 | * of the code, which only gets called in the order the work was | |
677 | * queued. We walk all the async extents created by compress_file_range | |
678 | * and send them down to the disk. | |
679 | */ | |
dec8f175 | 680 | static noinline void submit_compressed_extents(struct inode *inode, |
771ed689 CM |
681 | struct async_cow *async_cow) |
682 | { | |
683 | struct async_extent *async_extent; | |
684 | u64 alloc_hint = 0; | |
771ed689 CM |
685 | struct btrfs_key ins; |
686 | struct extent_map *em; | |
687 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
688 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
689 | struct extent_io_tree *io_tree; | |
f5a84ee3 | 690 | int ret = 0; |
771ed689 | 691 | |
3e04e7f1 | 692 | again: |
d397712b | 693 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
694 | async_extent = list_entry(async_cow->extents.next, |
695 | struct async_extent, list); | |
696 | list_del(&async_extent->list); | |
c8b97818 | 697 | |
771ed689 CM |
698 | io_tree = &BTRFS_I(inode)->io_tree; |
699 | ||
f5a84ee3 | 700 | retry: |
771ed689 CM |
701 | /* did the compression code fall back to uncompressed IO? */ |
702 | if (!async_extent->pages) { | |
703 | int page_started = 0; | |
704 | unsigned long nr_written = 0; | |
705 | ||
706 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 707 | async_extent->start + |
d0082371 | 708 | async_extent->ram_size - 1); |
771ed689 CM |
709 | |
710 | /* allocate blocks */ | |
f5a84ee3 JB |
711 | ret = cow_file_range(inode, async_cow->locked_page, |
712 | async_extent->start, | |
713 | async_extent->start + | |
714 | async_extent->ram_size - 1, | |
dda3245e WX |
715 | async_extent->start + |
716 | async_extent->ram_size - 1, | |
717 | &page_started, &nr_written, 0, | |
718 | NULL); | |
771ed689 | 719 | |
79787eaa JM |
720 | /* JDM XXX */ |
721 | ||
771ed689 CM |
722 | /* |
723 | * if page_started, cow_file_range inserted an | |
724 | * inline extent and took care of all the unlocking | |
725 | * and IO for us. Otherwise, we need to submit | |
726 | * all those pages down to the drive. | |
727 | */ | |
f5a84ee3 | 728 | if (!page_started && !ret) |
771ed689 CM |
729 | extent_write_locked_range(io_tree, |
730 | inode, async_extent->start, | |
d397712b | 731 | async_extent->start + |
771ed689 CM |
732 | async_extent->ram_size - 1, |
733 | btrfs_get_extent, | |
734 | WB_SYNC_ALL); | |
3e04e7f1 JB |
735 | else if (ret) |
736 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
737 | kfree(async_extent); |
738 | cond_resched(); | |
739 | continue; | |
740 | } | |
741 | ||
742 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 743 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 744 | |
00361589 | 745 | ret = btrfs_reserve_extent(root, |
771ed689 CM |
746 | async_extent->compressed_size, |
747 | async_extent->compressed_size, | |
e570fd27 | 748 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 749 | if (ret) { |
40ae837b | 750 | free_async_extent_pages(async_extent); |
3e04e7f1 | 751 | |
fdf8e2ea JB |
752 | if (ret == -ENOSPC) { |
753 | unlock_extent(io_tree, async_extent->start, | |
754 | async_extent->start + | |
755 | async_extent->ram_size - 1); | |
ce62003f LB |
756 | |
757 | /* | |
758 | * we need to redirty the pages if we decide to | |
759 | * fallback to uncompressed IO, otherwise we | |
760 | * will not submit these pages down to lower | |
761 | * layers. | |
762 | */ | |
763 | extent_range_redirty_for_io(inode, | |
764 | async_extent->start, | |
765 | async_extent->start + | |
766 | async_extent->ram_size - 1); | |
767 | ||
79787eaa | 768 | goto retry; |
fdf8e2ea | 769 | } |
3e04e7f1 | 770 | goto out_free; |
f5a84ee3 | 771 | } |
c2167754 YZ |
772 | /* |
773 | * here we're doing allocation and writeback of the | |
774 | * compressed pages | |
775 | */ | |
776 | btrfs_drop_extent_cache(inode, async_extent->start, | |
777 | async_extent->start + | |
778 | async_extent->ram_size - 1, 0); | |
779 | ||
172ddd60 | 780 | em = alloc_extent_map(); |
b9aa55be LB |
781 | if (!em) { |
782 | ret = -ENOMEM; | |
3e04e7f1 | 783 | goto out_free_reserve; |
b9aa55be | 784 | } |
771ed689 CM |
785 | em->start = async_extent->start; |
786 | em->len = async_extent->ram_size; | |
445a6944 | 787 | em->orig_start = em->start; |
2ab28f32 JB |
788 | em->mod_start = em->start; |
789 | em->mod_len = em->len; | |
c8b97818 | 790 | |
771ed689 CM |
791 | em->block_start = ins.objectid; |
792 | em->block_len = ins.offset; | |
b4939680 | 793 | em->orig_block_len = ins.offset; |
cc95bef6 | 794 | em->ram_bytes = async_extent->ram_size; |
771ed689 | 795 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
261507a0 | 796 | em->compress_type = async_extent->compress_type; |
771ed689 CM |
797 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
798 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
70c8a91c | 799 | em->generation = -1; |
771ed689 | 800 | |
d397712b | 801 | while (1) { |
890871be | 802 | write_lock(&em_tree->lock); |
09a2a8f9 | 803 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 804 | write_unlock(&em_tree->lock); |
771ed689 CM |
805 | if (ret != -EEXIST) { |
806 | free_extent_map(em); | |
807 | break; | |
808 | } | |
809 | btrfs_drop_extent_cache(inode, async_extent->start, | |
810 | async_extent->start + | |
811 | async_extent->ram_size - 1, 0); | |
812 | } | |
813 | ||
3e04e7f1 JB |
814 | if (ret) |
815 | goto out_free_reserve; | |
816 | ||
261507a0 LZ |
817 | ret = btrfs_add_ordered_extent_compress(inode, |
818 | async_extent->start, | |
819 | ins.objectid, | |
820 | async_extent->ram_size, | |
821 | ins.offset, | |
822 | BTRFS_ORDERED_COMPRESSED, | |
823 | async_extent->compress_type); | |
d9f85963 FM |
824 | if (ret) { |
825 | btrfs_drop_extent_cache(inode, async_extent->start, | |
826 | async_extent->start + | |
827 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 828 | goto out_free_reserve; |
d9f85963 | 829 | } |
9cfa3e34 | 830 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
771ed689 | 831 | |
771ed689 CM |
832 | /* |
833 | * clear dirty, set writeback and unlock the pages. | |
834 | */ | |
c2790a2e | 835 | extent_clear_unlock_delalloc(inode, async_extent->start, |
a791e35e CM |
836 | async_extent->start + |
837 | async_extent->ram_size - 1, | |
151a41bc JB |
838 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
839 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 840 | PAGE_SET_WRITEBACK); |
771ed689 | 841 | ret = btrfs_submit_compressed_write(inode, |
d397712b CM |
842 | async_extent->start, |
843 | async_extent->ram_size, | |
844 | ins.objectid, | |
845 | ins.offset, async_extent->pages, | |
846 | async_extent->nr_pages); | |
fce2a4e6 FM |
847 | if (ret) { |
848 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
849 | struct page *p = async_extent->pages[0]; | |
850 | const u64 start = async_extent->start; | |
851 | const u64 end = start + async_extent->ram_size - 1; | |
852 | ||
853 | p->mapping = inode->i_mapping; | |
854 | tree->ops->writepage_end_io_hook(p, start, end, | |
855 | NULL, 0); | |
856 | p->mapping = NULL; | |
857 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, | |
858 | PAGE_END_WRITEBACK | | |
859 | PAGE_SET_ERROR); | |
40ae837b | 860 | free_async_extent_pages(async_extent); |
fce2a4e6 | 861 | } |
771ed689 CM |
862 | alloc_hint = ins.objectid + ins.offset; |
863 | kfree(async_extent); | |
864 | cond_resched(); | |
865 | } | |
dec8f175 | 866 | return; |
3e04e7f1 | 867 | out_free_reserve: |
9cfa3e34 | 868 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
e570fd27 | 869 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 870 | out_free: |
c2790a2e | 871 | extent_clear_unlock_delalloc(inode, async_extent->start, |
3e04e7f1 JB |
872 | async_extent->start + |
873 | async_extent->ram_size - 1, | |
c2790a2e | 874 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
875 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
876 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
877 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
878 | PAGE_SET_ERROR); | |
40ae837b | 879 | free_async_extent_pages(async_extent); |
79787eaa | 880 | kfree(async_extent); |
3e04e7f1 | 881 | goto again; |
771ed689 CM |
882 | } |
883 | ||
4b46fce2 JB |
884 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
885 | u64 num_bytes) | |
886 | { | |
887 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
888 | struct extent_map *em; | |
889 | u64 alloc_hint = 0; | |
890 | ||
891 | read_lock(&em_tree->lock); | |
892 | em = search_extent_mapping(em_tree, start, num_bytes); | |
893 | if (em) { | |
894 | /* | |
895 | * if block start isn't an actual block number then find the | |
896 | * first block in this inode and use that as a hint. If that | |
897 | * block is also bogus then just don't worry about it. | |
898 | */ | |
899 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
900 | free_extent_map(em); | |
901 | em = search_extent_mapping(em_tree, 0, 0); | |
902 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
903 | alloc_hint = em->block_start; | |
904 | if (em) | |
905 | free_extent_map(em); | |
906 | } else { | |
907 | alloc_hint = em->block_start; | |
908 | free_extent_map(em); | |
909 | } | |
910 | } | |
911 | read_unlock(&em_tree->lock); | |
912 | ||
913 | return alloc_hint; | |
914 | } | |
915 | ||
771ed689 CM |
916 | /* |
917 | * when extent_io.c finds a delayed allocation range in the file, | |
918 | * the call backs end up in this code. The basic idea is to | |
919 | * allocate extents on disk for the range, and create ordered data structs | |
920 | * in ram to track those extents. | |
921 | * | |
922 | * locked_page is the page that writepage had locked already. We use | |
923 | * it to make sure we don't do extra locks or unlocks. | |
924 | * | |
925 | * *page_started is set to one if we unlock locked_page and do everything | |
926 | * required to start IO on it. It may be clean and already done with | |
927 | * IO when we return. | |
928 | */ | |
00361589 JB |
929 | static noinline int cow_file_range(struct inode *inode, |
930 | struct page *locked_page, | |
dda3245e WX |
931 | u64 start, u64 end, u64 delalloc_end, |
932 | int *page_started, unsigned long *nr_written, | |
933 | int unlock, struct btrfs_dedupe_hash *hash) | |
771ed689 | 934 | { |
00361589 | 935 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
936 | u64 alloc_hint = 0; |
937 | u64 num_bytes; | |
938 | unsigned long ram_size; | |
939 | u64 disk_num_bytes; | |
940 | u64 cur_alloc_size; | |
941 | u64 blocksize = root->sectorsize; | |
771ed689 CM |
942 | struct btrfs_key ins; |
943 | struct extent_map *em; | |
944 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
945 | int ret = 0; | |
946 | ||
02ecd2c2 JB |
947 | if (btrfs_is_free_space_inode(inode)) { |
948 | WARN_ON_ONCE(1); | |
29bce2f3 JB |
949 | ret = -EINVAL; |
950 | goto out_unlock; | |
02ecd2c2 | 951 | } |
771ed689 | 952 | |
fda2832f | 953 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
954 | num_bytes = max(blocksize, num_bytes); |
955 | disk_num_bytes = num_bytes; | |
771ed689 | 956 | |
4cb5300b | 957 | /* if this is a small write inside eof, kick off defrag */ |
ee22184b | 958 | if (num_bytes < SZ_64K && |
4cb13e5d | 959 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) |
00361589 | 960 | btrfs_add_inode_defrag(NULL, inode); |
4cb5300b | 961 | |
771ed689 CM |
962 | if (start == 0) { |
963 | /* lets try to make an inline extent */ | |
00361589 JB |
964 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
965 | NULL); | |
771ed689 | 966 | if (ret == 0) { |
c2790a2e JB |
967 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
968 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc | 969 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
970 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
971 | PAGE_END_WRITEBACK); | |
c2167754 | 972 | |
771ed689 | 973 | *nr_written = *nr_written + |
09cbfeaf | 974 | (end - start + PAGE_SIZE) / PAGE_SIZE; |
771ed689 | 975 | *page_started = 1; |
771ed689 | 976 | goto out; |
79787eaa | 977 | } else if (ret < 0) { |
79787eaa | 978 | goto out_unlock; |
771ed689 CM |
979 | } |
980 | } | |
981 | ||
982 | BUG_ON(disk_num_bytes > | |
6c41761f | 983 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
771ed689 | 984 | |
4b46fce2 | 985 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 CM |
986 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
987 | ||
d397712b | 988 | while (disk_num_bytes > 0) { |
a791e35e CM |
989 | unsigned long op; |
990 | ||
287a0ab9 | 991 | cur_alloc_size = disk_num_bytes; |
00361589 | 992 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
771ed689 | 993 | root->sectorsize, 0, alloc_hint, |
e570fd27 | 994 | &ins, 1, 1); |
00361589 | 995 | if (ret < 0) |
79787eaa | 996 | goto out_unlock; |
d397712b | 997 | |
172ddd60 | 998 | em = alloc_extent_map(); |
b9aa55be LB |
999 | if (!em) { |
1000 | ret = -ENOMEM; | |
ace68bac | 1001 | goto out_reserve; |
b9aa55be | 1002 | } |
e6dcd2dc | 1003 | em->start = start; |
445a6944 | 1004 | em->orig_start = em->start; |
771ed689 CM |
1005 | ram_size = ins.offset; |
1006 | em->len = ins.offset; | |
2ab28f32 JB |
1007 | em->mod_start = em->start; |
1008 | em->mod_len = em->len; | |
c8b97818 | 1009 | |
e6dcd2dc | 1010 | em->block_start = ins.objectid; |
c8b97818 | 1011 | em->block_len = ins.offset; |
b4939680 | 1012 | em->orig_block_len = ins.offset; |
cc95bef6 | 1013 | em->ram_bytes = ram_size; |
e6dcd2dc | 1014 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
7f3c74fb | 1015 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
70c8a91c | 1016 | em->generation = -1; |
c8b97818 | 1017 | |
d397712b | 1018 | while (1) { |
890871be | 1019 | write_lock(&em_tree->lock); |
09a2a8f9 | 1020 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1021 | write_unlock(&em_tree->lock); |
e6dcd2dc CM |
1022 | if (ret != -EEXIST) { |
1023 | free_extent_map(em); | |
1024 | break; | |
1025 | } | |
1026 | btrfs_drop_extent_cache(inode, start, | |
c8b97818 | 1027 | start + ram_size - 1, 0); |
e6dcd2dc | 1028 | } |
ace68bac LB |
1029 | if (ret) |
1030 | goto out_reserve; | |
e6dcd2dc | 1031 | |
98d20f67 | 1032 | cur_alloc_size = ins.offset; |
e6dcd2dc | 1033 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1034 | ram_size, cur_alloc_size, 0); |
ace68bac | 1035 | if (ret) |
d9f85963 | 1036 | goto out_drop_extent_cache; |
c8b97818 | 1037 | |
17d217fe YZ |
1038 | if (root->root_key.objectid == |
1039 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1040 | ret = btrfs_reloc_clone_csums(inode, start, | |
1041 | cur_alloc_size); | |
00361589 | 1042 | if (ret) |
d9f85963 | 1043 | goto out_drop_extent_cache; |
17d217fe YZ |
1044 | } |
1045 | ||
9cfa3e34 FM |
1046 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
1047 | ||
d397712b | 1048 | if (disk_num_bytes < cur_alloc_size) |
3b951516 | 1049 | break; |
d397712b | 1050 | |
c8b97818 CM |
1051 | /* we're not doing compressed IO, don't unlock the first |
1052 | * page (which the caller expects to stay locked), don't | |
1053 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1054 | * |
1055 | * Do set the Private2 bit so we know this page was properly | |
1056 | * setup for writepage | |
c8b97818 | 1057 | */ |
c2790a2e JB |
1058 | op = unlock ? PAGE_UNLOCK : 0; |
1059 | op |= PAGE_SET_PRIVATE2; | |
a791e35e | 1060 | |
c2790a2e JB |
1061 | extent_clear_unlock_delalloc(inode, start, |
1062 | start + ram_size - 1, locked_page, | |
1063 | EXTENT_LOCKED | EXTENT_DELALLOC, | |
1064 | op); | |
c8b97818 | 1065 | disk_num_bytes -= cur_alloc_size; |
c59f8951 CM |
1066 | num_bytes -= cur_alloc_size; |
1067 | alloc_hint = ins.objectid + ins.offset; | |
1068 | start += cur_alloc_size; | |
b888db2b | 1069 | } |
79787eaa | 1070 | out: |
be20aa9d | 1071 | return ret; |
b7d5b0a8 | 1072 | |
d9f85963 FM |
1073 | out_drop_extent_cache: |
1074 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); | |
ace68bac | 1075 | out_reserve: |
9cfa3e34 | 1076 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
e570fd27 | 1077 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 1078 | out_unlock: |
c2790a2e | 1079 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
151a41bc JB |
1080 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
1081 | EXTENT_DELALLOC | EXTENT_DEFRAG, | |
1082 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1083 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); | |
79787eaa | 1084 | goto out; |
771ed689 | 1085 | } |
c8b97818 | 1086 | |
771ed689 CM |
1087 | /* |
1088 | * work queue call back to started compression on a file and pages | |
1089 | */ | |
1090 | static noinline void async_cow_start(struct btrfs_work *work) | |
1091 | { | |
1092 | struct async_cow *async_cow; | |
1093 | int num_added = 0; | |
1094 | async_cow = container_of(work, struct async_cow, work); | |
1095 | ||
1096 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1097 | async_cow->start, async_cow->end, async_cow, | |
1098 | &num_added); | |
8180ef88 | 1099 | if (num_added == 0) { |
cb77fcd8 | 1100 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1101 | async_cow->inode = NULL; |
8180ef88 | 1102 | } |
771ed689 CM |
1103 | } |
1104 | ||
1105 | /* | |
1106 | * work queue call back to submit previously compressed pages | |
1107 | */ | |
1108 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1109 | { | |
1110 | struct async_cow *async_cow; | |
1111 | struct btrfs_root *root; | |
1112 | unsigned long nr_pages; | |
1113 | ||
1114 | async_cow = container_of(work, struct async_cow, work); | |
1115 | ||
1116 | root = async_cow->root; | |
09cbfeaf KS |
1117 | nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> |
1118 | PAGE_SHIFT; | |
771ed689 | 1119 | |
ee863954 DS |
1120 | /* |
1121 | * atomic_sub_return implies a barrier for waitqueue_active | |
1122 | */ | |
66657b31 | 1123 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
ee22184b | 1124 | 5 * SZ_1M && |
771ed689 CM |
1125 | waitqueue_active(&root->fs_info->async_submit_wait)) |
1126 | wake_up(&root->fs_info->async_submit_wait); | |
1127 | ||
d397712b | 1128 | if (async_cow->inode) |
771ed689 | 1129 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1130 | } |
c8b97818 | 1131 | |
771ed689 CM |
1132 | static noinline void async_cow_free(struct btrfs_work *work) |
1133 | { | |
1134 | struct async_cow *async_cow; | |
1135 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1136 | if (async_cow->inode) |
cb77fcd8 | 1137 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1138 | kfree(async_cow); |
1139 | } | |
1140 | ||
1141 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1142 | u64 start, u64 end, int *page_started, | |
1143 | unsigned long *nr_written) | |
1144 | { | |
1145 | struct async_cow *async_cow; | |
1146 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1147 | unsigned long nr_pages; | |
1148 | u64 cur_end; | |
ee22184b | 1149 | int limit = 10 * SZ_1M; |
771ed689 | 1150 | |
a3429ab7 CM |
1151 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1152 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1153 | while (start < end) { |
771ed689 | 1154 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1155 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1156 | async_cow->inode = igrab(inode); |
771ed689 CM |
1157 | async_cow->root = root; |
1158 | async_cow->locked_page = locked_page; | |
1159 | async_cow->start = start; | |
1160 | ||
f79707b0 | 1161 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
3cdde224 | 1162 | !btrfs_test_opt(root->fs_info, FORCE_COMPRESS)) |
771ed689 CM |
1163 | cur_end = end; |
1164 | else | |
ee22184b | 1165 | cur_end = min(end, start + SZ_512K - 1); |
771ed689 CM |
1166 | |
1167 | async_cow->end = cur_end; | |
1168 | INIT_LIST_HEAD(&async_cow->extents); | |
1169 | ||
9e0af237 LB |
1170 | btrfs_init_work(&async_cow->work, |
1171 | btrfs_delalloc_helper, | |
1172 | async_cow_start, async_cow_submit, | |
1173 | async_cow_free); | |
771ed689 | 1174 | |
09cbfeaf KS |
1175 | nr_pages = (cur_end - start + PAGE_SIZE) >> |
1176 | PAGE_SHIFT; | |
771ed689 CM |
1177 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); |
1178 | ||
afe3d242 QW |
1179 | btrfs_queue_work(root->fs_info->delalloc_workers, |
1180 | &async_cow->work); | |
771ed689 CM |
1181 | |
1182 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { | |
1183 | wait_event(root->fs_info->async_submit_wait, | |
1184 | (atomic_read(&root->fs_info->async_delalloc_pages) < | |
1185 | limit)); | |
1186 | } | |
1187 | ||
d397712b | 1188 | while (atomic_read(&root->fs_info->async_submit_draining) && |
771ed689 CM |
1189 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
1190 | wait_event(root->fs_info->async_submit_wait, | |
1191 | (atomic_read(&root->fs_info->async_delalloc_pages) == | |
1192 | 0)); | |
1193 | } | |
1194 | ||
1195 | *nr_written += nr_pages; | |
1196 | start = cur_end + 1; | |
1197 | } | |
1198 | *page_started = 1; | |
1199 | return 0; | |
be20aa9d CM |
1200 | } |
1201 | ||
d397712b | 1202 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
17d217fe YZ |
1203 | u64 bytenr, u64 num_bytes) |
1204 | { | |
1205 | int ret; | |
1206 | struct btrfs_ordered_sum *sums; | |
1207 | LIST_HEAD(list); | |
1208 | ||
07d400a6 | 1209 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
a2de733c | 1210 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1211 | if (ret == 0 && list_empty(&list)) |
1212 | return 0; | |
1213 | ||
1214 | while (!list_empty(&list)) { | |
1215 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1216 | list_del(&sums->list); | |
1217 | kfree(sums); | |
1218 | } | |
1219 | return 1; | |
1220 | } | |
1221 | ||
d352ac68 CM |
1222 | /* |
1223 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1224 | * of the extents that exist in the file, and COWs the file as required. | |
1225 | * | |
1226 | * If no cow copies or snapshots exist, we write directly to the existing | |
1227 | * blocks on disk | |
1228 | */ | |
7f366cfe CM |
1229 | static noinline int run_delalloc_nocow(struct inode *inode, |
1230 | struct page *locked_page, | |
771ed689 CM |
1231 | u64 start, u64 end, int *page_started, int force, |
1232 | unsigned long *nr_written) | |
be20aa9d | 1233 | { |
be20aa9d | 1234 | struct btrfs_root *root = BTRFS_I(inode)->root; |
7ea394f1 | 1235 | struct btrfs_trans_handle *trans; |
be20aa9d | 1236 | struct extent_buffer *leaf; |
be20aa9d | 1237 | struct btrfs_path *path; |
80ff3856 | 1238 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1239 | struct btrfs_key found_key; |
80ff3856 YZ |
1240 | u64 cow_start; |
1241 | u64 cur_offset; | |
1242 | u64 extent_end; | |
5d4f98a2 | 1243 | u64 extent_offset; |
80ff3856 YZ |
1244 | u64 disk_bytenr; |
1245 | u64 num_bytes; | |
b4939680 | 1246 | u64 disk_num_bytes; |
cc95bef6 | 1247 | u64 ram_bytes; |
80ff3856 | 1248 | int extent_type; |
79787eaa | 1249 | int ret, err; |
d899e052 | 1250 | int type; |
80ff3856 YZ |
1251 | int nocow; |
1252 | int check_prev = 1; | |
82d5902d | 1253 | bool nolock; |
33345d01 | 1254 | u64 ino = btrfs_ino(inode); |
be20aa9d CM |
1255 | |
1256 | path = btrfs_alloc_path(); | |
17ca04af | 1257 | if (!path) { |
c2790a2e JB |
1258 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1259 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1260 | EXTENT_DO_ACCOUNTING | |
1261 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1262 | PAGE_CLEAR_DIRTY | |
1263 | PAGE_SET_WRITEBACK | | |
1264 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1265 | return -ENOMEM; |
17ca04af | 1266 | } |
82d5902d | 1267 | |
83eea1f1 | 1268 | nolock = btrfs_is_free_space_inode(inode); |
82d5902d LZ |
1269 | |
1270 | if (nolock) | |
7a7eaa40 | 1271 | trans = btrfs_join_transaction_nolock(root); |
82d5902d | 1272 | else |
7a7eaa40 | 1273 | trans = btrfs_join_transaction(root); |
ff5714cc | 1274 | |
79787eaa | 1275 | if (IS_ERR(trans)) { |
c2790a2e JB |
1276 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1277 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1278 | EXTENT_DO_ACCOUNTING | |
1279 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1280 | PAGE_CLEAR_DIRTY | |
1281 | PAGE_SET_WRITEBACK | | |
1282 | PAGE_END_WRITEBACK); | |
79787eaa JM |
1283 | btrfs_free_path(path); |
1284 | return PTR_ERR(trans); | |
1285 | } | |
1286 | ||
74b21075 | 1287 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
be20aa9d | 1288 | |
80ff3856 YZ |
1289 | cow_start = (u64)-1; |
1290 | cur_offset = start; | |
1291 | while (1) { | |
33345d01 | 1292 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
80ff3856 | 1293 | cur_offset, 0); |
d788a349 | 1294 | if (ret < 0) |
79787eaa | 1295 | goto error; |
80ff3856 YZ |
1296 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1297 | leaf = path->nodes[0]; | |
1298 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1299 | path->slots[0] - 1); | |
33345d01 | 1300 | if (found_key.objectid == ino && |
80ff3856 YZ |
1301 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1302 | path->slots[0]--; | |
1303 | } | |
1304 | check_prev = 0; | |
1305 | next_slot: | |
1306 | leaf = path->nodes[0]; | |
1307 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1308 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1309 | if (ret < 0) |
79787eaa | 1310 | goto error; |
80ff3856 YZ |
1311 | if (ret > 0) |
1312 | break; | |
1313 | leaf = path->nodes[0]; | |
1314 | } | |
be20aa9d | 1315 | |
80ff3856 YZ |
1316 | nocow = 0; |
1317 | disk_bytenr = 0; | |
17d217fe | 1318 | num_bytes = 0; |
80ff3856 YZ |
1319 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1320 | ||
1d512cb7 FM |
1321 | if (found_key.objectid > ino) |
1322 | break; | |
1323 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1324 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1325 | path->slots[0]++; | |
1326 | goto next_slot; | |
1327 | } | |
1328 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
80ff3856 YZ |
1329 | found_key.offset > end) |
1330 | break; | |
1331 | ||
1332 | if (found_key.offset > cur_offset) { | |
1333 | extent_end = found_key.offset; | |
e9061e21 | 1334 | extent_type = 0; |
80ff3856 YZ |
1335 | goto out_check; |
1336 | } | |
1337 | ||
1338 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1339 | struct btrfs_file_extent_item); | |
1340 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1341 | ||
cc95bef6 | 1342 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1343 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1344 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1345 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1346 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1347 | extent_end = found_key.offset + |
1348 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1349 | disk_num_bytes = |
1350 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1351 | if (extent_end <= start) { |
1352 | path->slots[0]++; | |
1353 | goto next_slot; | |
1354 | } | |
17d217fe YZ |
1355 | if (disk_bytenr == 0) |
1356 | goto out_check; | |
80ff3856 YZ |
1357 | if (btrfs_file_extent_compression(leaf, fi) || |
1358 | btrfs_file_extent_encryption(leaf, fi) || | |
1359 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1360 | goto out_check; | |
d899e052 YZ |
1361 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1362 | goto out_check; | |
d2fb3437 | 1363 | if (btrfs_extent_readonly(root, disk_bytenr)) |
80ff3856 | 1364 | goto out_check; |
33345d01 | 1365 | if (btrfs_cross_ref_exist(trans, root, ino, |
5d4f98a2 YZ |
1366 | found_key.offset - |
1367 | extent_offset, disk_bytenr)) | |
17d217fe | 1368 | goto out_check; |
5d4f98a2 | 1369 | disk_bytenr += extent_offset; |
17d217fe YZ |
1370 | disk_bytenr += cur_offset - found_key.offset; |
1371 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1372 | /* |
1373 | * if there are pending snapshots for this root, | |
1374 | * we fall into common COW way. | |
1375 | */ | |
1376 | if (!nolock) { | |
9ea24bbe | 1377 | err = btrfs_start_write_no_snapshoting(root); |
e9894fd3 WS |
1378 | if (!err) |
1379 | goto out_check; | |
1380 | } | |
17d217fe YZ |
1381 | /* |
1382 | * force cow if csum exists in the range. | |
1383 | * this ensure that csum for a given extent are | |
1384 | * either valid or do not exist. | |
1385 | */ | |
1386 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) | |
1387 | goto out_check; | |
f78c436c FM |
1388 | if (!btrfs_inc_nocow_writers(root->fs_info, |
1389 | disk_bytenr)) | |
1390 | goto out_check; | |
80ff3856 YZ |
1391 | nocow = 1; |
1392 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1393 | extent_end = found_key.offset + | |
514ac8ad CM |
1394 | btrfs_file_extent_inline_len(leaf, |
1395 | path->slots[0], fi); | |
80ff3856 YZ |
1396 | extent_end = ALIGN(extent_end, root->sectorsize); |
1397 | } else { | |
1398 | BUG_ON(1); | |
1399 | } | |
1400 | out_check: | |
1401 | if (extent_end <= start) { | |
1402 | path->slots[0]++; | |
e9894fd3 | 1403 | if (!nolock && nocow) |
9ea24bbe | 1404 | btrfs_end_write_no_snapshoting(root); |
f78c436c FM |
1405 | if (nocow) |
1406 | btrfs_dec_nocow_writers(root->fs_info, | |
1407 | disk_bytenr); | |
80ff3856 YZ |
1408 | goto next_slot; |
1409 | } | |
1410 | if (!nocow) { | |
1411 | if (cow_start == (u64)-1) | |
1412 | cow_start = cur_offset; | |
1413 | cur_offset = extent_end; | |
1414 | if (cur_offset > end) | |
1415 | break; | |
1416 | path->slots[0]++; | |
1417 | goto next_slot; | |
7ea394f1 YZ |
1418 | } |
1419 | ||
b3b4aa74 | 1420 | btrfs_release_path(path); |
80ff3856 | 1421 | if (cow_start != (u64)-1) { |
00361589 JB |
1422 | ret = cow_file_range(inode, locked_page, |
1423 | cow_start, found_key.offset - 1, | |
dda3245e WX |
1424 | end, page_started, nr_written, 1, |
1425 | NULL); | |
e9894fd3 WS |
1426 | if (ret) { |
1427 | if (!nolock && nocow) | |
9ea24bbe | 1428 | btrfs_end_write_no_snapshoting(root); |
f78c436c FM |
1429 | if (nocow) |
1430 | btrfs_dec_nocow_writers(root->fs_info, | |
1431 | disk_bytenr); | |
79787eaa | 1432 | goto error; |
e9894fd3 | 1433 | } |
80ff3856 | 1434 | cow_start = (u64)-1; |
7ea394f1 | 1435 | } |
80ff3856 | 1436 | |
d899e052 YZ |
1437 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
1438 | struct extent_map *em; | |
1439 | struct extent_map_tree *em_tree; | |
1440 | em_tree = &BTRFS_I(inode)->extent_tree; | |
172ddd60 | 1441 | em = alloc_extent_map(); |
79787eaa | 1442 | BUG_ON(!em); /* -ENOMEM */ |
d899e052 | 1443 | em->start = cur_offset; |
70c8a91c | 1444 | em->orig_start = found_key.offset - extent_offset; |
d899e052 YZ |
1445 | em->len = num_bytes; |
1446 | em->block_len = num_bytes; | |
1447 | em->block_start = disk_bytenr; | |
b4939680 | 1448 | em->orig_block_len = disk_num_bytes; |
cc95bef6 | 1449 | em->ram_bytes = ram_bytes; |
d899e052 | 1450 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
2ab28f32 JB |
1451 | em->mod_start = em->start; |
1452 | em->mod_len = em->len; | |
d899e052 | 1453 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
b11e234d | 1454 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
70c8a91c | 1455 | em->generation = -1; |
d899e052 | 1456 | while (1) { |
890871be | 1457 | write_lock(&em_tree->lock); |
09a2a8f9 | 1458 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1459 | write_unlock(&em_tree->lock); |
d899e052 YZ |
1460 | if (ret != -EEXIST) { |
1461 | free_extent_map(em); | |
1462 | break; | |
1463 | } | |
1464 | btrfs_drop_extent_cache(inode, em->start, | |
1465 | em->start + em->len - 1, 0); | |
1466 | } | |
1467 | type = BTRFS_ORDERED_PREALLOC; | |
1468 | } else { | |
1469 | type = BTRFS_ORDERED_NOCOW; | |
1470 | } | |
80ff3856 YZ |
1471 | |
1472 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1473 | num_bytes, num_bytes, type); |
f78c436c FM |
1474 | if (nocow) |
1475 | btrfs_dec_nocow_writers(root->fs_info, disk_bytenr); | |
79787eaa | 1476 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1477 | |
efa56464 YZ |
1478 | if (root->root_key.objectid == |
1479 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1480 | ret = btrfs_reloc_clone_csums(inode, cur_offset, | |
1481 | num_bytes); | |
e9894fd3 WS |
1482 | if (ret) { |
1483 | if (!nolock && nocow) | |
9ea24bbe | 1484 | btrfs_end_write_no_snapshoting(root); |
79787eaa | 1485 | goto error; |
e9894fd3 | 1486 | } |
efa56464 YZ |
1487 | } |
1488 | ||
c2790a2e JB |
1489 | extent_clear_unlock_delalloc(inode, cur_offset, |
1490 | cur_offset + num_bytes - 1, | |
1491 | locked_page, EXTENT_LOCKED | | |
1492 | EXTENT_DELALLOC, PAGE_UNLOCK | | |
1493 | PAGE_SET_PRIVATE2); | |
e9894fd3 | 1494 | if (!nolock && nocow) |
9ea24bbe | 1495 | btrfs_end_write_no_snapshoting(root); |
80ff3856 YZ |
1496 | cur_offset = extent_end; |
1497 | if (cur_offset > end) | |
1498 | break; | |
be20aa9d | 1499 | } |
b3b4aa74 | 1500 | btrfs_release_path(path); |
80ff3856 | 1501 | |
17ca04af | 1502 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1503 | cow_start = cur_offset; |
17ca04af JB |
1504 | cur_offset = end; |
1505 | } | |
1506 | ||
80ff3856 | 1507 | if (cow_start != (u64)-1) { |
dda3245e WX |
1508 | ret = cow_file_range(inode, locked_page, cow_start, end, end, |
1509 | page_started, nr_written, 1, NULL); | |
d788a349 | 1510 | if (ret) |
79787eaa | 1511 | goto error; |
80ff3856 YZ |
1512 | } |
1513 | ||
79787eaa | 1514 | error: |
a698d075 | 1515 | err = btrfs_end_transaction(trans, root); |
79787eaa JM |
1516 | if (!ret) |
1517 | ret = err; | |
1518 | ||
17ca04af | 1519 | if (ret && cur_offset < end) |
c2790a2e JB |
1520 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
1521 | locked_page, EXTENT_LOCKED | | |
151a41bc JB |
1522 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1523 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1524 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1525 | PAGE_SET_WRITEBACK | |
1526 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1527 | btrfs_free_path(path); |
79787eaa | 1528 | return ret; |
be20aa9d CM |
1529 | } |
1530 | ||
47059d93 WS |
1531 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1532 | { | |
1533 | ||
1534 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1535 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1536 | return 0; | |
1537 | ||
1538 | /* | |
1539 | * @defrag_bytes is a hint value, no spinlock held here, | |
1540 | * if is not zero, it means the file is defragging. | |
1541 | * Force cow if given extent needs to be defragged. | |
1542 | */ | |
1543 | if (BTRFS_I(inode)->defrag_bytes && | |
1544 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1545 | EXTENT_DEFRAG, 0, NULL)) | |
1546 | return 1; | |
1547 | ||
1548 | return 0; | |
1549 | } | |
1550 | ||
d352ac68 CM |
1551 | /* |
1552 | * extent_io.c call back to do delayed allocation processing | |
1553 | */ | |
c8b97818 | 1554 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
771ed689 CM |
1555 | u64 start, u64 end, int *page_started, |
1556 | unsigned long *nr_written) | |
be20aa9d | 1557 | { |
be20aa9d | 1558 | int ret; |
47059d93 | 1559 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1560 | |
47059d93 | 1561 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1562 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1563 | page_started, 1, nr_written); |
47059d93 | 1564 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1565 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1566 | page_started, 0, nr_written); |
7816030e | 1567 | } else if (!inode_need_compress(inode)) { |
dda3245e WX |
1568 | ret = cow_file_range(inode, locked_page, start, end, end, |
1569 | page_started, nr_written, 1, NULL); | |
7ddf5a42 JB |
1570 | } else { |
1571 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1572 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1573 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1574 | page_started, nr_written); |
7ddf5a42 | 1575 | } |
b888db2b CM |
1576 | return ret; |
1577 | } | |
1578 | ||
1bf85046 JM |
1579 | static void btrfs_split_extent_hook(struct inode *inode, |
1580 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1581 | { |
dcab6a3b JB |
1582 | u64 size; |
1583 | ||
0ca1f7ce | 1584 | /* not delalloc, ignore it */ |
9ed74f2d | 1585 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1586 | return; |
9ed74f2d | 1587 | |
dcab6a3b JB |
1588 | size = orig->end - orig->start + 1; |
1589 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
1590 | u64 num_extents; | |
1591 | u64 new_size; | |
1592 | ||
1593 | /* | |
ba117213 JB |
1594 | * See the explanation in btrfs_merge_extent_hook, the same |
1595 | * applies here, just in reverse. | |
dcab6a3b JB |
1596 | */ |
1597 | new_size = orig->end - split + 1; | |
ba117213 | 1598 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
dcab6a3b | 1599 | BTRFS_MAX_EXTENT_SIZE); |
ba117213 JB |
1600 | new_size = split - orig->start; |
1601 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1602 | BTRFS_MAX_EXTENT_SIZE); | |
1603 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1604 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) | |
dcab6a3b JB |
1605 | return; |
1606 | } | |
1607 | ||
9e0baf60 JB |
1608 | spin_lock(&BTRFS_I(inode)->lock); |
1609 | BTRFS_I(inode)->outstanding_extents++; | |
1610 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1611 | } |
1612 | ||
1613 | /* | |
1614 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1615 | * extents so we can keep track of new extents that are just merged onto old | |
1616 | * extents, such as when we are doing sequential writes, so we can properly | |
1617 | * account for the metadata space we'll need. | |
1618 | */ | |
1bf85046 JM |
1619 | static void btrfs_merge_extent_hook(struct inode *inode, |
1620 | struct extent_state *new, | |
1621 | struct extent_state *other) | |
9ed74f2d | 1622 | { |
dcab6a3b JB |
1623 | u64 new_size, old_size; |
1624 | u64 num_extents; | |
1625 | ||
9ed74f2d JB |
1626 | /* not delalloc, ignore it */ |
1627 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1628 | return; |
9ed74f2d | 1629 | |
8461a3de JB |
1630 | if (new->start > other->start) |
1631 | new_size = new->end - other->start + 1; | |
1632 | else | |
1633 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1634 | |
1635 | /* we're not bigger than the max, unreserve the space and go */ | |
1636 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1637 | spin_lock(&BTRFS_I(inode)->lock); | |
1638 | BTRFS_I(inode)->outstanding_extents--; | |
1639 | spin_unlock(&BTRFS_I(inode)->lock); | |
1640 | return; | |
1641 | } | |
1642 | ||
1643 | /* | |
ba117213 JB |
1644 | * We have to add up either side to figure out how many extents were |
1645 | * accounted for before we merged into one big extent. If the number of | |
1646 | * extents we accounted for is <= the amount we need for the new range | |
1647 | * then we can return, otherwise drop. Think of it like this | |
1648 | * | |
1649 | * [ 4k][MAX_SIZE] | |
1650 | * | |
1651 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1652 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1653 | * we have 1 so they are == and we can return. But in this case | |
1654 | * | |
1655 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1656 | * | |
1657 | * Each range on their own accounts for 2 extents, but merged together | |
1658 | * they are only 3 extents worth of accounting, so we need to drop in | |
1659 | * this case. | |
dcab6a3b | 1660 | */ |
ba117213 | 1661 | old_size = other->end - other->start + 1; |
dcab6a3b JB |
1662 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
1663 | BTRFS_MAX_EXTENT_SIZE); | |
ba117213 JB |
1664 | old_size = new->end - new->start + 1; |
1665 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1666 | BTRFS_MAX_EXTENT_SIZE); | |
1667 | ||
dcab6a3b | 1668 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
ba117213 | 1669 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
dcab6a3b JB |
1670 | return; |
1671 | ||
9e0baf60 JB |
1672 | spin_lock(&BTRFS_I(inode)->lock); |
1673 | BTRFS_I(inode)->outstanding_extents--; | |
1674 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1675 | } |
1676 | ||
eb73c1b7 MX |
1677 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1678 | struct inode *inode) | |
1679 | { | |
1680 | spin_lock(&root->delalloc_lock); | |
1681 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1682 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1683 | &root->delalloc_inodes); | |
1684 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1685 | &BTRFS_I(inode)->runtime_flags); | |
1686 | root->nr_delalloc_inodes++; | |
1687 | if (root->nr_delalloc_inodes == 1) { | |
1688 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1689 | BUG_ON(!list_empty(&root->delalloc_root)); | |
1690 | list_add_tail(&root->delalloc_root, | |
1691 | &root->fs_info->delalloc_roots); | |
1692 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1693 | } | |
1694 | } | |
1695 | spin_unlock(&root->delalloc_lock); | |
1696 | } | |
1697 | ||
1698 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1699 | struct inode *inode) | |
1700 | { | |
1701 | spin_lock(&root->delalloc_lock); | |
1702 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1703 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); | |
1704 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1705 | &BTRFS_I(inode)->runtime_flags); | |
1706 | root->nr_delalloc_inodes--; | |
1707 | if (!root->nr_delalloc_inodes) { | |
1708 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1709 | BUG_ON(list_empty(&root->delalloc_root)); | |
1710 | list_del_init(&root->delalloc_root); | |
1711 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1712 | } | |
1713 | } | |
1714 | spin_unlock(&root->delalloc_lock); | |
1715 | } | |
1716 | ||
d352ac68 CM |
1717 | /* |
1718 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1719 | * bytes in this file, and to maintain the list of inodes that | |
1720 | * have pending delalloc work to be done. | |
1721 | */ | |
1bf85046 | 1722 | static void btrfs_set_bit_hook(struct inode *inode, |
9ee49a04 | 1723 | struct extent_state *state, unsigned *bits) |
291d673e | 1724 | { |
9ed74f2d | 1725 | |
47059d93 WS |
1726 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1727 | WARN_ON(1); | |
75eff68e CM |
1728 | /* |
1729 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1730 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1731 | * bit, which is only set or cleared with irqs on |
1732 | */ | |
0ca1f7ce | 1733 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1734 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1735 | u64 len = state->end + 1 - state->start; |
83eea1f1 | 1736 | bool do_list = !btrfs_is_free_space_inode(inode); |
9ed74f2d | 1737 | |
9e0baf60 | 1738 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1739 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1740 | } else { |
1741 | spin_lock(&BTRFS_I(inode)->lock); | |
1742 | BTRFS_I(inode)->outstanding_extents++; | |
1743 | spin_unlock(&BTRFS_I(inode)->lock); | |
1744 | } | |
287a0ab9 | 1745 | |
6a3891c5 | 1746 | /* For sanity tests */ |
f5ee5c9a | 1747 | if (btrfs_is_testing(root->fs_info)) |
6a3891c5 JB |
1748 | return; |
1749 | ||
963d678b MX |
1750 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
1751 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1752 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1753 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1754 | if (*bits & EXTENT_DEFRAG) |
1755 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1756 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1757 | &BTRFS_I(inode)->runtime_flags)) |
1758 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1759 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1760 | } |
291d673e CM |
1761 | } |
1762 | ||
d352ac68 CM |
1763 | /* |
1764 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1765 | */ | |
1bf85046 | 1766 | static void btrfs_clear_bit_hook(struct inode *inode, |
41074888 | 1767 | struct extent_state *state, |
9ee49a04 | 1768 | unsigned *bits) |
291d673e | 1769 | { |
47059d93 | 1770 | u64 len = state->end + 1 - state->start; |
dcab6a3b JB |
1771 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
1772 | BTRFS_MAX_EXTENT_SIZE); | |
47059d93 WS |
1773 | |
1774 | spin_lock(&BTRFS_I(inode)->lock); | |
1775 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) | |
1776 | BTRFS_I(inode)->defrag_bytes -= len; | |
1777 | spin_unlock(&BTRFS_I(inode)->lock); | |
1778 | ||
75eff68e CM |
1779 | /* |
1780 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1781 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1782 | * bit, which is only set or cleared with irqs on |
1783 | */ | |
0ca1f7ce | 1784 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1785 | struct btrfs_root *root = BTRFS_I(inode)->root; |
83eea1f1 | 1786 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1787 | |
9e0baf60 | 1788 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1789 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1790 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
1791 | spin_lock(&BTRFS_I(inode)->lock); | |
dcab6a3b | 1792 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
9e0baf60 JB |
1793 | spin_unlock(&BTRFS_I(inode)->lock); |
1794 | } | |
0ca1f7ce | 1795 | |
b6d08f06 JB |
1796 | /* |
1797 | * We don't reserve metadata space for space cache inodes so we | |
1798 | * don't need to call dellalloc_release_metadata if there is an | |
1799 | * error. | |
1800 | */ | |
1801 | if (*bits & EXTENT_DO_ACCOUNTING && | |
1802 | root != root->fs_info->tree_root) | |
0ca1f7ce YZ |
1803 | btrfs_delalloc_release_metadata(inode, len); |
1804 | ||
6a3891c5 | 1805 | /* For sanity tests. */ |
f5ee5c9a | 1806 | if (btrfs_is_testing(root->fs_info)) |
6a3891c5 JB |
1807 | return; |
1808 | ||
0cb59c99 | 1809 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
7ee9e440 | 1810 | && do_list && !(state->state & EXTENT_NORESERVE)) |
51773bec QW |
1811 | btrfs_free_reserved_data_space_noquota(inode, |
1812 | state->start, len); | |
9ed74f2d | 1813 | |
963d678b MX |
1814 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
1815 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1816 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1817 | BTRFS_I(inode)->delalloc_bytes -= len; |
0cb59c99 | 1818 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
df0af1a5 | 1819 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1820 | &BTRFS_I(inode)->runtime_flags)) |
1821 | btrfs_del_delalloc_inode(root, inode); | |
df0af1a5 | 1822 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1823 | } |
291d673e CM |
1824 | } |
1825 | ||
d352ac68 CM |
1826 | /* |
1827 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1828 | * we don't create bios that span stripes or chunks | |
6f034ece LB |
1829 | * |
1830 | * return 1 if page cannot be merged to bio | |
1831 | * return 0 if page can be merged to bio | |
1832 | * return error otherwise | |
d352ac68 | 1833 | */ |
64a16701 | 1834 | int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset, |
c8b97818 CM |
1835 | size_t size, struct bio *bio, |
1836 | unsigned long bio_flags) | |
239b14b3 CM |
1837 | { |
1838 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
4f024f37 | 1839 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1840 | u64 length = 0; |
1841 | u64 map_length; | |
239b14b3 CM |
1842 | int ret; |
1843 | ||
771ed689 CM |
1844 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1845 | return 0; | |
1846 | ||
4f024f37 | 1847 | length = bio->bi_iter.bi_size; |
239b14b3 | 1848 | map_length = length; |
64a16701 | 1849 | ret = btrfs_map_block(root->fs_info, rw, logical, |
f188591e | 1850 | &map_length, NULL, 0); |
6f034ece LB |
1851 | if (ret < 0) |
1852 | return ret; | |
d397712b | 1853 | if (map_length < length + size) |
239b14b3 | 1854 | return 1; |
3444a972 | 1855 | return 0; |
239b14b3 CM |
1856 | } |
1857 | ||
d352ac68 CM |
1858 | /* |
1859 | * in order to insert checksums into the metadata in large chunks, | |
1860 | * we wait until bio submission time. All the pages in the bio are | |
1861 | * checksummed and sums are attached onto the ordered extent record. | |
1862 | * | |
1863 | * At IO completion time the cums attached on the ordered extent record | |
1864 | * are inserted into the btree | |
1865 | */ | |
d397712b CM |
1866 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
1867 | struct bio *bio, int mirror_num, | |
eaf25d93 CM |
1868 | unsigned long bio_flags, |
1869 | u64 bio_offset) | |
065631f6 | 1870 | { |
065631f6 | 1871 | struct btrfs_root *root = BTRFS_I(inode)->root; |
065631f6 | 1872 | int ret = 0; |
e015640f | 1873 | |
d20f7043 | 1874 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
79787eaa | 1875 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1876 | return 0; |
1877 | } | |
e015640f | 1878 | |
4a69a410 CM |
1879 | /* |
1880 | * in order to insert checksums into the metadata in large chunks, | |
1881 | * we wait until bio submission time. All the pages in the bio are | |
1882 | * checksummed and sums are attached onto the ordered extent record. | |
1883 | * | |
1884 | * At IO completion time the cums attached on the ordered extent record | |
1885 | * are inserted into the btree | |
1886 | */ | |
b2950863 | 1887 | static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1888 | int mirror_num, unsigned long bio_flags, |
1889 | u64 bio_offset) | |
4a69a410 CM |
1890 | { |
1891 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
61891923 SB |
1892 | int ret; |
1893 | ||
1894 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); | |
4246a0b6 CH |
1895 | if (ret) { |
1896 | bio->bi_error = ret; | |
1897 | bio_endio(bio); | |
1898 | } | |
61891923 | 1899 | return ret; |
44b8bd7e CM |
1900 | } |
1901 | ||
d352ac68 | 1902 | /* |
cad321ad CM |
1903 | * extent_io.c submission hook. This does the right thing for csum calculation |
1904 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1905 | */ |
b2950863 | 1906 | static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1907 | int mirror_num, unsigned long bio_flags, |
1908 | u64 bio_offset) | |
44b8bd7e CM |
1909 | { |
1910 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
0d51e28a | 1911 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
44b8bd7e | 1912 | int ret = 0; |
19b9bdb0 | 1913 | int skip_sum; |
b812ce28 | 1914 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1915 | |
6cbff00f | 1916 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1917 | |
83eea1f1 | 1918 | if (btrfs_is_free_space_inode(inode)) |
0d51e28a | 1919 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 1920 | |
7b6d91da | 1921 | if (!(rw & REQ_WRITE)) { |
5fd02043 JB |
1922 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
1923 | if (ret) | |
61891923 | 1924 | goto out; |
5fd02043 | 1925 | |
d20f7043 | 1926 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1927 | ret = btrfs_submit_compressed_read(inode, bio, |
1928 | mirror_num, | |
1929 | bio_flags); | |
1930 | goto out; | |
c2db1073 TI |
1931 | } else if (!skip_sum) { |
1932 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); | |
1933 | if (ret) | |
61891923 | 1934 | goto out; |
c2db1073 | 1935 | } |
4d1b5fb4 | 1936 | goto mapit; |
b812ce28 | 1937 | } else if (async && !skip_sum) { |
17d217fe YZ |
1938 | /* csum items have already been cloned */ |
1939 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1940 | goto mapit; | |
19b9bdb0 | 1941 | /* we're doing a write, do the async checksumming */ |
61891923 | 1942 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
44b8bd7e | 1943 | inode, rw, bio, mirror_num, |
eaf25d93 CM |
1944 | bio_flags, bio_offset, |
1945 | __btrfs_submit_bio_start, | |
4a69a410 | 1946 | __btrfs_submit_bio_done); |
61891923 | 1947 | goto out; |
b812ce28 JB |
1948 | } else if (!skip_sum) { |
1949 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); | |
1950 | if (ret) | |
1951 | goto out; | |
19b9bdb0 CM |
1952 | } |
1953 | ||
0b86a832 | 1954 | mapit: |
61891923 SB |
1955 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
1956 | ||
1957 | out: | |
4246a0b6 CH |
1958 | if (ret < 0) { |
1959 | bio->bi_error = ret; | |
1960 | bio_endio(bio); | |
1961 | } | |
61891923 | 1962 | return ret; |
065631f6 | 1963 | } |
6885f308 | 1964 | |
d352ac68 CM |
1965 | /* |
1966 | * given a list of ordered sums record them in the inode. This happens | |
1967 | * at IO completion time based on sums calculated at bio submission time. | |
1968 | */ | |
ba1da2f4 | 1969 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
1970 | struct inode *inode, u64 file_offset, |
1971 | struct list_head *list) | |
1972 | { | |
e6dcd2dc CM |
1973 | struct btrfs_ordered_sum *sum; |
1974 | ||
c6e30871 | 1975 | list_for_each_entry(sum, list, list) { |
39847c4d | 1976 | trans->adding_csums = 1; |
d20f7043 CM |
1977 | btrfs_csum_file_blocks(trans, |
1978 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 1979 | trans->adding_csums = 0; |
e6dcd2dc CM |
1980 | } |
1981 | return 0; | |
1982 | } | |
1983 | ||
2ac55d41 JB |
1984 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
1985 | struct extent_state **cached_state) | |
ea8c2819 | 1986 | { |
09cbfeaf | 1987 | WARN_ON((end & (PAGE_SIZE - 1)) == 0); |
ea8c2819 | 1988 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
7cd8c752 | 1989 | cached_state); |
ea8c2819 CM |
1990 | } |
1991 | ||
d352ac68 | 1992 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
1993 | struct btrfs_writepage_fixup { |
1994 | struct page *page; | |
1995 | struct btrfs_work work; | |
1996 | }; | |
1997 | ||
b2950863 | 1998 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
1999 | { |
2000 | struct btrfs_writepage_fixup *fixup; | |
2001 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 2002 | struct extent_state *cached_state = NULL; |
247e743c CM |
2003 | struct page *page; |
2004 | struct inode *inode; | |
2005 | u64 page_start; | |
2006 | u64 page_end; | |
87826df0 | 2007 | int ret; |
247e743c CM |
2008 | |
2009 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
2010 | page = fixup->page; | |
4a096752 | 2011 | again: |
247e743c CM |
2012 | lock_page(page); |
2013 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
2014 | ClearPageChecked(page); | |
2015 | goto out_page; | |
2016 | } | |
2017 | ||
2018 | inode = page->mapping->host; | |
2019 | page_start = page_offset(page); | |
09cbfeaf | 2020 | page_end = page_offset(page) + PAGE_SIZE - 1; |
247e743c | 2021 | |
ff13db41 | 2022 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, |
d0082371 | 2023 | &cached_state); |
4a096752 CM |
2024 | |
2025 | /* already ordered? We're done */ | |
8b62b72b | 2026 | if (PagePrivate2(page)) |
247e743c | 2027 | goto out; |
4a096752 | 2028 | |
dbfdb6d1 | 2029 | ordered = btrfs_lookup_ordered_range(inode, page_start, |
09cbfeaf | 2030 | PAGE_SIZE); |
4a096752 | 2031 | if (ordered) { |
2ac55d41 JB |
2032 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
2033 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
2034 | unlock_page(page); |
2035 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2036 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2037 | goto again; |
2038 | } | |
247e743c | 2039 | |
7cf5b976 | 2040 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
09cbfeaf | 2041 | PAGE_SIZE); |
87826df0 JM |
2042 | if (ret) { |
2043 | mapping_set_error(page->mapping, ret); | |
2044 | end_extent_writepage(page, ret, page_start, page_end); | |
2045 | ClearPageChecked(page); | |
2046 | goto out; | |
2047 | } | |
2048 | ||
2ac55d41 | 2049 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
247e743c | 2050 | ClearPageChecked(page); |
87826df0 | 2051 | set_page_dirty(page); |
247e743c | 2052 | out: |
2ac55d41 JB |
2053 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2054 | &cached_state, GFP_NOFS); | |
247e743c CM |
2055 | out_page: |
2056 | unlock_page(page); | |
09cbfeaf | 2057 | put_page(page); |
b897abec | 2058 | kfree(fixup); |
247e743c CM |
2059 | } |
2060 | ||
2061 | /* | |
2062 | * There are a few paths in the higher layers of the kernel that directly | |
2063 | * set the page dirty bit without asking the filesystem if it is a | |
2064 | * good idea. This causes problems because we want to make sure COW | |
2065 | * properly happens and the data=ordered rules are followed. | |
2066 | * | |
c8b97818 | 2067 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2068 | * hasn't been properly setup for IO. We kick off an async process |
2069 | * to fix it up. The async helper will wait for ordered extents, set | |
2070 | * the delalloc bit and make it safe to write the page. | |
2071 | */ | |
b2950863 | 2072 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2073 | { |
2074 | struct inode *inode = page->mapping->host; | |
2075 | struct btrfs_writepage_fixup *fixup; | |
2076 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
247e743c | 2077 | |
8b62b72b CM |
2078 | /* this page is properly in the ordered list */ |
2079 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2080 | return 0; |
2081 | ||
2082 | if (PageChecked(page)) | |
2083 | return -EAGAIN; | |
2084 | ||
2085 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2086 | if (!fixup) | |
2087 | return -EAGAIN; | |
f421950f | 2088 | |
247e743c | 2089 | SetPageChecked(page); |
09cbfeaf | 2090 | get_page(page); |
9e0af237 LB |
2091 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2092 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2093 | fixup->page = page; |
dc6e3209 | 2094 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
87826df0 | 2095 | return -EBUSY; |
247e743c CM |
2096 | } |
2097 | ||
d899e052 YZ |
2098 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2099 | struct inode *inode, u64 file_pos, | |
2100 | u64 disk_bytenr, u64 disk_num_bytes, | |
2101 | u64 num_bytes, u64 ram_bytes, | |
2102 | u8 compression, u8 encryption, | |
2103 | u16 other_encoding, int extent_type) | |
2104 | { | |
2105 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2106 | struct btrfs_file_extent_item *fi; | |
2107 | struct btrfs_path *path; | |
2108 | struct extent_buffer *leaf; | |
2109 | struct btrfs_key ins; | |
1acae57b | 2110 | int extent_inserted = 0; |
d899e052 YZ |
2111 | int ret; |
2112 | ||
2113 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2114 | if (!path) |
2115 | return -ENOMEM; | |
d899e052 | 2116 | |
a1ed835e CM |
2117 | /* |
2118 | * we may be replacing one extent in the tree with another. | |
2119 | * The new extent is pinned in the extent map, and we don't want | |
2120 | * to drop it from the cache until it is completely in the btree. | |
2121 | * | |
2122 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2123 | * the caller is expected to unpin it and allow it to be merged | |
2124 | * with the others. | |
2125 | */ | |
1acae57b FDBM |
2126 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2127 | file_pos + num_bytes, NULL, 0, | |
2128 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2129 | if (ret) |
2130 | goto out; | |
d899e052 | 2131 | |
1acae57b FDBM |
2132 | if (!extent_inserted) { |
2133 | ins.objectid = btrfs_ino(inode); | |
2134 | ins.offset = file_pos; | |
2135 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2136 | ||
2137 | path->leave_spinning = 1; | |
2138 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2139 | sizeof(*fi)); | |
2140 | if (ret) | |
2141 | goto out; | |
2142 | } | |
d899e052 YZ |
2143 | leaf = path->nodes[0]; |
2144 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2145 | struct btrfs_file_extent_item); | |
2146 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2147 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2148 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2149 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2150 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2151 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2152 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2153 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2154 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2155 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2156 | |
d899e052 | 2157 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2158 | btrfs_release_path(path); |
d899e052 YZ |
2159 | |
2160 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2161 | |
2162 | ins.objectid = disk_bytenr; | |
2163 | ins.offset = disk_num_bytes; | |
2164 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 YZ |
2165 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2166 | root->root_key.objectid, | |
5846a3c2 QW |
2167 | btrfs_ino(inode), file_pos, |
2168 | ram_bytes, &ins); | |
297d750b | 2169 | /* |
5846a3c2 QW |
2170 | * Release the reserved range from inode dirty range map, as it is |
2171 | * already moved into delayed_ref_head | |
297d750b QW |
2172 | */ |
2173 | btrfs_qgroup_release_data(inode, file_pos, ram_bytes); | |
79787eaa | 2174 | out: |
d899e052 | 2175 | btrfs_free_path(path); |
b9473439 | 2176 | |
79787eaa | 2177 | return ret; |
d899e052 YZ |
2178 | } |
2179 | ||
38c227d8 LB |
2180 | /* snapshot-aware defrag */ |
2181 | struct sa_defrag_extent_backref { | |
2182 | struct rb_node node; | |
2183 | struct old_sa_defrag_extent *old; | |
2184 | u64 root_id; | |
2185 | u64 inum; | |
2186 | u64 file_pos; | |
2187 | u64 extent_offset; | |
2188 | u64 num_bytes; | |
2189 | u64 generation; | |
2190 | }; | |
2191 | ||
2192 | struct old_sa_defrag_extent { | |
2193 | struct list_head list; | |
2194 | struct new_sa_defrag_extent *new; | |
2195 | ||
2196 | u64 extent_offset; | |
2197 | u64 bytenr; | |
2198 | u64 offset; | |
2199 | u64 len; | |
2200 | int count; | |
2201 | }; | |
2202 | ||
2203 | struct new_sa_defrag_extent { | |
2204 | struct rb_root root; | |
2205 | struct list_head head; | |
2206 | struct btrfs_path *path; | |
2207 | struct inode *inode; | |
2208 | u64 file_pos; | |
2209 | u64 len; | |
2210 | u64 bytenr; | |
2211 | u64 disk_len; | |
2212 | u8 compress_type; | |
2213 | }; | |
2214 | ||
2215 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2216 | struct sa_defrag_extent_backref *b2) | |
2217 | { | |
2218 | if (b1->root_id < b2->root_id) | |
2219 | return -1; | |
2220 | else if (b1->root_id > b2->root_id) | |
2221 | return 1; | |
2222 | ||
2223 | if (b1->inum < b2->inum) | |
2224 | return -1; | |
2225 | else if (b1->inum > b2->inum) | |
2226 | return 1; | |
2227 | ||
2228 | if (b1->file_pos < b2->file_pos) | |
2229 | return -1; | |
2230 | else if (b1->file_pos > b2->file_pos) | |
2231 | return 1; | |
2232 | ||
2233 | /* | |
2234 | * [------------------------------] ===> (a range of space) | |
2235 | * |<--->| |<---->| =============> (fs/file tree A) | |
2236 | * |<---------------------------->| ===> (fs/file tree B) | |
2237 | * | |
2238 | * A range of space can refer to two file extents in one tree while | |
2239 | * refer to only one file extent in another tree. | |
2240 | * | |
2241 | * So we may process a disk offset more than one time(two extents in A) | |
2242 | * and locate at the same extent(one extent in B), then insert two same | |
2243 | * backrefs(both refer to the extent in B). | |
2244 | */ | |
2245 | return 0; | |
2246 | } | |
2247 | ||
2248 | static void backref_insert(struct rb_root *root, | |
2249 | struct sa_defrag_extent_backref *backref) | |
2250 | { | |
2251 | struct rb_node **p = &root->rb_node; | |
2252 | struct rb_node *parent = NULL; | |
2253 | struct sa_defrag_extent_backref *entry; | |
2254 | int ret; | |
2255 | ||
2256 | while (*p) { | |
2257 | parent = *p; | |
2258 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2259 | ||
2260 | ret = backref_comp(backref, entry); | |
2261 | if (ret < 0) | |
2262 | p = &(*p)->rb_left; | |
2263 | else | |
2264 | p = &(*p)->rb_right; | |
2265 | } | |
2266 | ||
2267 | rb_link_node(&backref->node, parent, p); | |
2268 | rb_insert_color(&backref->node, root); | |
2269 | } | |
2270 | ||
2271 | /* | |
2272 | * Note the backref might has changed, and in this case we just return 0. | |
2273 | */ | |
2274 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2275 | void *ctx) | |
2276 | { | |
2277 | struct btrfs_file_extent_item *extent; | |
2278 | struct btrfs_fs_info *fs_info; | |
2279 | struct old_sa_defrag_extent *old = ctx; | |
2280 | struct new_sa_defrag_extent *new = old->new; | |
2281 | struct btrfs_path *path = new->path; | |
2282 | struct btrfs_key key; | |
2283 | struct btrfs_root *root; | |
2284 | struct sa_defrag_extent_backref *backref; | |
2285 | struct extent_buffer *leaf; | |
2286 | struct inode *inode = new->inode; | |
2287 | int slot; | |
2288 | int ret; | |
2289 | u64 extent_offset; | |
2290 | u64 num_bytes; | |
2291 | ||
2292 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
2293 | inum == btrfs_ino(inode)) | |
2294 | return 0; | |
2295 | ||
2296 | key.objectid = root_id; | |
2297 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2298 | key.offset = (u64)-1; | |
2299 | ||
2300 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2301 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2302 | if (IS_ERR(root)) { | |
2303 | if (PTR_ERR(root) == -ENOENT) | |
2304 | return 0; | |
2305 | WARN_ON(1); | |
2306 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", | |
2307 | inum, offset, root_id); | |
2308 | return PTR_ERR(root); | |
2309 | } | |
2310 | ||
2311 | key.objectid = inum; | |
2312 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2313 | if (offset > (u64)-1 << 32) | |
2314 | key.offset = 0; | |
2315 | else | |
2316 | key.offset = offset; | |
2317 | ||
2318 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2319 | if (WARN_ON(ret < 0)) |
38c227d8 | 2320 | return ret; |
50f1319c | 2321 | ret = 0; |
38c227d8 LB |
2322 | |
2323 | while (1) { | |
2324 | cond_resched(); | |
2325 | ||
2326 | leaf = path->nodes[0]; | |
2327 | slot = path->slots[0]; | |
2328 | ||
2329 | if (slot >= btrfs_header_nritems(leaf)) { | |
2330 | ret = btrfs_next_leaf(root, path); | |
2331 | if (ret < 0) { | |
2332 | goto out; | |
2333 | } else if (ret > 0) { | |
2334 | ret = 0; | |
2335 | goto out; | |
2336 | } | |
2337 | continue; | |
2338 | } | |
2339 | ||
2340 | path->slots[0]++; | |
2341 | ||
2342 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2343 | ||
2344 | if (key.objectid > inum) | |
2345 | goto out; | |
2346 | ||
2347 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2348 | continue; | |
2349 | ||
2350 | extent = btrfs_item_ptr(leaf, slot, | |
2351 | struct btrfs_file_extent_item); | |
2352 | ||
2353 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2354 | continue; | |
2355 | ||
e68afa49 LB |
2356 | /* |
2357 | * 'offset' refers to the exact key.offset, | |
2358 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2359 | * (key.offset - extent_offset). | |
2360 | */ | |
2361 | if (key.offset != offset) | |
38c227d8 LB |
2362 | continue; |
2363 | ||
e68afa49 | 2364 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2365 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2366 | |
38c227d8 LB |
2367 | if (extent_offset >= old->extent_offset + old->offset + |
2368 | old->len || extent_offset + num_bytes <= | |
2369 | old->extent_offset + old->offset) | |
2370 | continue; | |
38c227d8 LB |
2371 | break; |
2372 | } | |
2373 | ||
2374 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2375 | if (!backref) { | |
2376 | ret = -ENOENT; | |
2377 | goto out; | |
2378 | } | |
2379 | ||
2380 | backref->root_id = root_id; | |
2381 | backref->inum = inum; | |
e68afa49 | 2382 | backref->file_pos = offset; |
38c227d8 LB |
2383 | backref->num_bytes = num_bytes; |
2384 | backref->extent_offset = extent_offset; | |
2385 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2386 | backref->old = old; | |
2387 | backref_insert(&new->root, backref); | |
2388 | old->count++; | |
2389 | out: | |
2390 | btrfs_release_path(path); | |
2391 | WARN_ON(ret); | |
2392 | return ret; | |
2393 | } | |
2394 | ||
2395 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2396 | struct new_sa_defrag_extent *new) | |
2397 | { | |
2398 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; | |
2399 | struct old_sa_defrag_extent *old, *tmp; | |
2400 | int ret; | |
2401 | ||
2402 | new->path = path; | |
2403 | ||
2404 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2405 | ret = iterate_inodes_from_logical(old->bytenr + |
2406 | old->extent_offset, fs_info, | |
38c227d8 LB |
2407 | path, record_one_backref, |
2408 | old); | |
4724b106 JB |
2409 | if (ret < 0 && ret != -ENOENT) |
2410 | return false; | |
38c227d8 LB |
2411 | |
2412 | /* no backref to be processed for this extent */ | |
2413 | if (!old->count) { | |
2414 | list_del(&old->list); | |
2415 | kfree(old); | |
2416 | } | |
2417 | } | |
2418 | ||
2419 | if (list_empty(&new->head)) | |
2420 | return false; | |
2421 | ||
2422 | return true; | |
2423 | } | |
2424 | ||
2425 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2426 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2427 | struct new_sa_defrag_extent *new) |
38c227d8 | 2428 | { |
116e0024 | 2429 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2430 | return 0; |
2431 | ||
2432 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2433 | return 0; | |
2434 | ||
116e0024 LB |
2435 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2436 | return 0; | |
2437 | ||
2438 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2439 | btrfs_file_extent_other_encoding(leaf, fi)) |
2440 | return 0; | |
2441 | ||
2442 | return 1; | |
2443 | } | |
2444 | ||
2445 | /* | |
2446 | * Note the backref might has changed, and in this case we just return 0. | |
2447 | */ | |
2448 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2449 | struct sa_defrag_extent_backref *prev, | |
2450 | struct sa_defrag_extent_backref *backref) | |
2451 | { | |
2452 | struct btrfs_file_extent_item *extent; | |
2453 | struct btrfs_file_extent_item *item; | |
2454 | struct btrfs_ordered_extent *ordered; | |
2455 | struct btrfs_trans_handle *trans; | |
2456 | struct btrfs_fs_info *fs_info; | |
2457 | struct btrfs_root *root; | |
2458 | struct btrfs_key key; | |
2459 | struct extent_buffer *leaf; | |
2460 | struct old_sa_defrag_extent *old = backref->old; | |
2461 | struct new_sa_defrag_extent *new = old->new; | |
2462 | struct inode *src_inode = new->inode; | |
2463 | struct inode *inode; | |
2464 | struct extent_state *cached = NULL; | |
2465 | int ret = 0; | |
2466 | u64 start; | |
2467 | u64 len; | |
2468 | u64 lock_start; | |
2469 | u64 lock_end; | |
2470 | bool merge = false; | |
2471 | int index; | |
2472 | ||
2473 | if (prev && prev->root_id == backref->root_id && | |
2474 | prev->inum == backref->inum && | |
2475 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2476 | merge = true; | |
2477 | ||
2478 | /* step 1: get root */ | |
2479 | key.objectid = backref->root_id; | |
2480 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2481 | key.offset = (u64)-1; | |
2482 | ||
2483 | fs_info = BTRFS_I(src_inode)->root->fs_info; | |
2484 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
2485 | ||
2486 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2487 | if (IS_ERR(root)) { | |
2488 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2489 | if (PTR_ERR(root) == -ENOENT) | |
2490 | return 0; | |
2491 | return PTR_ERR(root); | |
2492 | } | |
38c227d8 | 2493 | |
bcbba5e6 WS |
2494 | if (btrfs_root_readonly(root)) { |
2495 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2496 | return 0; | |
2497 | } | |
2498 | ||
38c227d8 LB |
2499 | /* step 2: get inode */ |
2500 | key.objectid = backref->inum; | |
2501 | key.type = BTRFS_INODE_ITEM_KEY; | |
2502 | key.offset = 0; | |
2503 | ||
2504 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2505 | if (IS_ERR(inode)) { | |
2506 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2507 | return 0; | |
2508 | } | |
2509 | ||
2510 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2511 | ||
2512 | /* step 3: relink backref */ | |
2513 | lock_start = backref->file_pos; | |
2514 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2515 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
ff13db41 | 2516 | &cached); |
38c227d8 LB |
2517 | |
2518 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2519 | if (ordered) { | |
2520 | btrfs_put_ordered_extent(ordered); | |
2521 | goto out_unlock; | |
2522 | } | |
2523 | ||
2524 | trans = btrfs_join_transaction(root); | |
2525 | if (IS_ERR(trans)) { | |
2526 | ret = PTR_ERR(trans); | |
2527 | goto out_unlock; | |
2528 | } | |
2529 | ||
2530 | key.objectid = backref->inum; | |
2531 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2532 | key.offset = backref->file_pos; | |
2533 | ||
2534 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2535 | if (ret < 0) { | |
2536 | goto out_free_path; | |
2537 | } else if (ret > 0) { | |
2538 | ret = 0; | |
2539 | goto out_free_path; | |
2540 | } | |
2541 | ||
2542 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2543 | struct btrfs_file_extent_item); | |
2544 | ||
2545 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2546 | backref->generation) | |
2547 | goto out_free_path; | |
2548 | ||
2549 | btrfs_release_path(path); | |
2550 | ||
2551 | start = backref->file_pos; | |
2552 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2553 | start += old->extent_offset + old->offset - | |
2554 | backref->extent_offset; | |
2555 | ||
2556 | len = min(backref->extent_offset + backref->num_bytes, | |
2557 | old->extent_offset + old->offset + old->len); | |
2558 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2559 | ||
2560 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2561 | start + len, 1); | |
2562 | if (ret) | |
2563 | goto out_free_path; | |
2564 | again: | |
2565 | key.objectid = btrfs_ino(inode); | |
2566 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2567 | key.offset = start; | |
2568 | ||
a09a0a70 | 2569 | path->leave_spinning = 1; |
38c227d8 LB |
2570 | if (merge) { |
2571 | struct btrfs_file_extent_item *fi; | |
2572 | u64 extent_len; | |
2573 | struct btrfs_key found_key; | |
2574 | ||
3c9665df | 2575 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2576 | if (ret < 0) |
2577 | goto out_free_path; | |
2578 | ||
2579 | path->slots[0]--; | |
2580 | leaf = path->nodes[0]; | |
2581 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2582 | ||
2583 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2584 | struct btrfs_file_extent_item); | |
2585 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2586 | ||
116e0024 LB |
2587 | if (extent_len + found_key.offset == start && |
2588 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2589 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2590 | extent_len + len); | |
2591 | btrfs_mark_buffer_dirty(leaf); | |
2592 | inode_add_bytes(inode, len); | |
2593 | ||
2594 | ret = 1; | |
2595 | goto out_free_path; | |
2596 | } else { | |
2597 | merge = false; | |
2598 | btrfs_release_path(path); | |
2599 | goto again; | |
2600 | } | |
2601 | } | |
2602 | ||
2603 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2604 | sizeof(*extent)); | |
2605 | if (ret) { | |
2606 | btrfs_abort_transaction(trans, root, ret); | |
2607 | goto out_free_path; | |
2608 | } | |
2609 | ||
2610 | leaf = path->nodes[0]; | |
2611 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2612 | struct btrfs_file_extent_item); | |
2613 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2614 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2615 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2616 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2617 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2618 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2619 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2620 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2621 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2622 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2623 | ||
2624 | btrfs_mark_buffer_dirty(leaf); | |
2625 | inode_add_bytes(inode, len); | |
a09a0a70 | 2626 | btrfs_release_path(path); |
38c227d8 LB |
2627 | |
2628 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, | |
2629 | new->disk_len, 0, | |
2630 | backref->root_id, backref->inum, | |
b06c4bf5 | 2631 | new->file_pos); /* start - extent_offset */ |
38c227d8 LB |
2632 | if (ret) { |
2633 | btrfs_abort_transaction(trans, root, ret); | |
2634 | goto out_free_path; | |
2635 | } | |
2636 | ||
2637 | ret = 1; | |
2638 | out_free_path: | |
2639 | btrfs_release_path(path); | |
a09a0a70 | 2640 | path->leave_spinning = 0; |
38c227d8 LB |
2641 | btrfs_end_transaction(trans, root); |
2642 | out_unlock: | |
2643 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2644 | &cached, GFP_NOFS); | |
2645 | iput(inode); | |
2646 | return ret; | |
2647 | } | |
2648 | ||
6f519564 LB |
2649 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2650 | { | |
2651 | struct old_sa_defrag_extent *old, *tmp; | |
2652 | ||
2653 | if (!new) | |
2654 | return; | |
2655 | ||
2656 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2657 | kfree(old); |
2658 | } | |
2659 | kfree(new); | |
2660 | } | |
2661 | ||
38c227d8 LB |
2662 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2663 | { | |
2664 | struct btrfs_path *path; | |
38c227d8 LB |
2665 | struct sa_defrag_extent_backref *backref; |
2666 | struct sa_defrag_extent_backref *prev = NULL; | |
2667 | struct inode *inode; | |
2668 | struct btrfs_root *root; | |
2669 | struct rb_node *node; | |
2670 | int ret; | |
2671 | ||
2672 | inode = new->inode; | |
2673 | root = BTRFS_I(inode)->root; | |
2674 | ||
2675 | path = btrfs_alloc_path(); | |
2676 | if (!path) | |
2677 | return; | |
2678 | ||
2679 | if (!record_extent_backrefs(path, new)) { | |
2680 | btrfs_free_path(path); | |
2681 | goto out; | |
2682 | } | |
2683 | btrfs_release_path(path); | |
2684 | ||
2685 | while (1) { | |
2686 | node = rb_first(&new->root); | |
2687 | if (!node) | |
2688 | break; | |
2689 | rb_erase(node, &new->root); | |
2690 | ||
2691 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2692 | ||
2693 | ret = relink_extent_backref(path, prev, backref); | |
2694 | WARN_ON(ret < 0); | |
2695 | ||
2696 | kfree(prev); | |
2697 | ||
2698 | if (ret == 1) | |
2699 | prev = backref; | |
2700 | else | |
2701 | prev = NULL; | |
2702 | cond_resched(); | |
2703 | } | |
2704 | kfree(prev); | |
2705 | ||
2706 | btrfs_free_path(path); | |
38c227d8 | 2707 | out: |
6f519564 LB |
2708 | free_sa_defrag_extent(new); |
2709 | ||
38c227d8 LB |
2710 | atomic_dec(&root->fs_info->defrag_running); |
2711 | wake_up(&root->fs_info->transaction_wait); | |
38c227d8 LB |
2712 | } |
2713 | ||
2714 | static struct new_sa_defrag_extent * | |
2715 | record_old_file_extents(struct inode *inode, | |
2716 | struct btrfs_ordered_extent *ordered) | |
2717 | { | |
2718 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2719 | struct btrfs_path *path; | |
2720 | struct btrfs_key key; | |
6f519564 | 2721 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2722 | struct new_sa_defrag_extent *new; |
2723 | int ret; | |
2724 | ||
2725 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2726 | if (!new) | |
2727 | return NULL; | |
2728 | ||
2729 | new->inode = inode; | |
2730 | new->file_pos = ordered->file_offset; | |
2731 | new->len = ordered->len; | |
2732 | new->bytenr = ordered->start; | |
2733 | new->disk_len = ordered->disk_len; | |
2734 | new->compress_type = ordered->compress_type; | |
2735 | new->root = RB_ROOT; | |
2736 | INIT_LIST_HEAD(&new->head); | |
2737 | ||
2738 | path = btrfs_alloc_path(); | |
2739 | if (!path) | |
2740 | goto out_kfree; | |
2741 | ||
2742 | key.objectid = btrfs_ino(inode); | |
2743 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2744 | key.offset = new->file_pos; | |
2745 | ||
2746 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2747 | if (ret < 0) | |
2748 | goto out_free_path; | |
2749 | if (ret > 0 && path->slots[0] > 0) | |
2750 | path->slots[0]--; | |
2751 | ||
2752 | /* find out all the old extents for the file range */ | |
2753 | while (1) { | |
2754 | struct btrfs_file_extent_item *extent; | |
2755 | struct extent_buffer *l; | |
2756 | int slot; | |
2757 | u64 num_bytes; | |
2758 | u64 offset; | |
2759 | u64 end; | |
2760 | u64 disk_bytenr; | |
2761 | u64 extent_offset; | |
2762 | ||
2763 | l = path->nodes[0]; | |
2764 | slot = path->slots[0]; | |
2765 | ||
2766 | if (slot >= btrfs_header_nritems(l)) { | |
2767 | ret = btrfs_next_leaf(root, path); | |
2768 | if (ret < 0) | |
6f519564 | 2769 | goto out_free_path; |
38c227d8 LB |
2770 | else if (ret > 0) |
2771 | break; | |
2772 | continue; | |
2773 | } | |
2774 | ||
2775 | btrfs_item_key_to_cpu(l, &key, slot); | |
2776 | ||
2777 | if (key.objectid != btrfs_ino(inode)) | |
2778 | break; | |
2779 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2780 | break; | |
2781 | if (key.offset >= new->file_pos + new->len) | |
2782 | break; | |
2783 | ||
2784 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2785 | ||
2786 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2787 | if (key.offset + num_bytes < new->file_pos) | |
2788 | goto next; | |
2789 | ||
2790 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2791 | if (!disk_bytenr) | |
2792 | goto next; | |
2793 | ||
2794 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2795 | ||
2796 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2797 | if (!old) | |
6f519564 | 2798 | goto out_free_path; |
38c227d8 LB |
2799 | |
2800 | offset = max(new->file_pos, key.offset); | |
2801 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2802 | ||
2803 | old->bytenr = disk_bytenr; | |
2804 | old->extent_offset = extent_offset; | |
2805 | old->offset = offset - key.offset; | |
2806 | old->len = end - offset; | |
2807 | old->new = new; | |
2808 | old->count = 0; | |
2809 | list_add_tail(&old->list, &new->head); | |
2810 | next: | |
2811 | path->slots[0]++; | |
2812 | cond_resched(); | |
2813 | } | |
2814 | ||
2815 | btrfs_free_path(path); | |
2816 | atomic_inc(&root->fs_info->defrag_running); | |
2817 | ||
2818 | return new; | |
2819 | ||
38c227d8 LB |
2820 | out_free_path: |
2821 | btrfs_free_path(path); | |
2822 | out_kfree: | |
6f519564 | 2823 | free_sa_defrag_extent(new); |
38c227d8 LB |
2824 | return NULL; |
2825 | } | |
2826 | ||
e570fd27 MX |
2827 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
2828 | u64 start, u64 len) | |
2829 | { | |
2830 | struct btrfs_block_group_cache *cache; | |
2831 | ||
2832 | cache = btrfs_lookup_block_group(root->fs_info, start); | |
2833 | ASSERT(cache); | |
2834 | ||
2835 | spin_lock(&cache->lock); | |
2836 | cache->delalloc_bytes -= len; | |
2837 | spin_unlock(&cache->lock); | |
2838 | ||
2839 | btrfs_put_block_group(cache); | |
2840 | } | |
2841 | ||
d352ac68 CM |
2842 | /* as ordered data IO finishes, this gets called so we can finish |
2843 | * an ordered extent if the range of bytes in the file it covers are | |
2844 | * fully written. | |
2845 | */ | |
5fd02043 | 2846 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2847 | { |
5fd02043 | 2848 | struct inode *inode = ordered_extent->inode; |
e6dcd2dc | 2849 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2850 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2851 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2852 | struct extent_state *cached_state = NULL; |
38c227d8 | 2853 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2854 | int compress_type = 0; |
77cef2ec JB |
2855 | int ret = 0; |
2856 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2857 | bool nolock; |
77cef2ec | 2858 | bool truncated = false; |
e6dcd2dc | 2859 | |
83eea1f1 | 2860 | nolock = btrfs_is_free_space_inode(inode); |
0cb59c99 | 2861 | |
5fd02043 JB |
2862 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2863 | ret = -EIO; | |
2864 | goto out; | |
2865 | } | |
2866 | ||
f612496b MX |
2867 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
2868 | ordered_extent->file_offset + | |
2869 | ordered_extent->len - 1); | |
2870 | ||
77cef2ec JB |
2871 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2872 | truncated = true; | |
2873 | logical_len = ordered_extent->truncated_len; | |
2874 | /* Truncated the entire extent, don't bother adding */ | |
2875 | if (!logical_len) | |
2876 | goto out; | |
2877 | } | |
2878 | ||
c2167754 | 2879 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2880 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a QW |
2881 | |
2882 | /* | |
2883 | * For mwrite(mmap + memset to write) case, we still reserve | |
2884 | * space for NOCOW range. | |
2885 | * As NOCOW won't cause a new delayed ref, just free the space | |
2886 | */ | |
2887 | btrfs_qgroup_free_data(inode, ordered_extent->file_offset, | |
2888 | ordered_extent->len); | |
6c760c07 JB |
2889 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2890 | if (nolock) | |
2891 | trans = btrfs_join_transaction_nolock(root); | |
2892 | else | |
2893 | trans = btrfs_join_transaction(root); | |
2894 | if (IS_ERR(trans)) { | |
2895 | ret = PTR_ERR(trans); | |
2896 | trans = NULL; | |
2897 | goto out; | |
c2167754 | 2898 | } |
6c760c07 JB |
2899 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
2900 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2901 | if (ret) /* -ENOMEM or corruption */ | |
2902 | btrfs_abort_transaction(trans, root, ret); | |
c2167754 YZ |
2903 | goto out; |
2904 | } | |
e6dcd2dc | 2905 | |
2ac55d41 JB |
2906 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2907 | ordered_extent->file_offset + ordered_extent->len - 1, | |
ff13db41 | 2908 | &cached_state); |
e6dcd2dc | 2909 | |
38c227d8 LB |
2910 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2911 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2912 | EXTENT_DEFRAG, 1, cached_state); | |
2913 | if (ret) { | |
2914 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2915 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2916 | /* the inode is shared */ |
2917 | new = record_old_file_extents(inode, ordered_extent); | |
2918 | ||
2919 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2920 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2921 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2922 | } | |
2923 | ||
0cb59c99 | 2924 | if (nolock) |
7a7eaa40 | 2925 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2926 | else |
7a7eaa40 | 2927 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2928 | if (IS_ERR(trans)) { |
2929 | ret = PTR_ERR(trans); | |
2930 | trans = NULL; | |
2931 | goto out_unlock; | |
2932 | } | |
a79b7d4b | 2933 | |
0ca1f7ce | 2934 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
c2167754 | 2935 | |
c8b97818 | 2936 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2937 | compress_type = ordered_extent->compress_type; |
d899e052 | 2938 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2939 | BUG_ON(compress_type); |
920bbbfb | 2940 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
2941 | ordered_extent->file_offset, |
2942 | ordered_extent->file_offset + | |
77cef2ec | 2943 | logical_len); |
d899e052 | 2944 | } else { |
0af3d00b | 2945 | BUG_ON(root == root->fs_info->tree_root); |
d899e052 YZ |
2946 | ret = insert_reserved_file_extent(trans, inode, |
2947 | ordered_extent->file_offset, | |
2948 | ordered_extent->start, | |
2949 | ordered_extent->disk_len, | |
77cef2ec | 2950 | logical_len, logical_len, |
261507a0 | 2951 | compress_type, 0, 0, |
d899e052 | 2952 | BTRFS_FILE_EXTENT_REG); |
e570fd27 MX |
2953 | if (!ret) |
2954 | btrfs_release_delalloc_bytes(root, | |
2955 | ordered_extent->start, | |
2956 | ordered_extent->disk_len); | |
d899e052 | 2957 | } |
5dc562c5 JB |
2958 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
2959 | ordered_extent->file_offset, ordered_extent->len, | |
2960 | trans->transid); | |
79787eaa JM |
2961 | if (ret < 0) { |
2962 | btrfs_abort_transaction(trans, root, ret); | |
5fd02043 | 2963 | goto out_unlock; |
79787eaa | 2964 | } |
2ac55d41 | 2965 | |
e6dcd2dc CM |
2966 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
2967 | &ordered_extent->list); | |
2968 | ||
6c760c07 JB |
2969 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2970 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2971 | if (ret) { /* -ENOMEM or corruption */ | |
2972 | btrfs_abort_transaction(trans, root, ret); | |
2973 | goto out_unlock; | |
1ef30be1 JB |
2974 | } |
2975 | ret = 0; | |
5fd02043 JB |
2976 | out_unlock: |
2977 | unlock_extent_cached(io_tree, ordered_extent->file_offset, | |
2978 | ordered_extent->file_offset + | |
2979 | ordered_extent->len - 1, &cached_state, GFP_NOFS); | |
c2167754 | 2980 | out: |
5b0e95bf | 2981 | if (root != root->fs_info->tree_root) |
0cb59c99 | 2982 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
a698d075 MX |
2983 | if (trans) |
2984 | btrfs_end_transaction(trans, root); | |
0cb59c99 | 2985 | |
77cef2ec JB |
2986 | if (ret || truncated) { |
2987 | u64 start, end; | |
2988 | ||
2989 | if (truncated) | |
2990 | start = ordered_extent->file_offset + logical_len; | |
2991 | else | |
2992 | start = ordered_extent->file_offset; | |
2993 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
2994 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
2995 | ||
2996 | /* Drop the cache for the part of the extent we didn't write. */ | |
2997 | btrfs_drop_extent_cache(inode, start, end, 0); | |
5fd02043 | 2998 | |
0bec9ef5 JB |
2999 | /* |
3000 | * If the ordered extent had an IOERR or something else went | |
3001 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
3002 | * back to the allocator. We only free the extent in the |
3003 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 3004 | */ |
77cef2ec JB |
3005 | if ((ret || !logical_len) && |
3006 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 JB |
3007 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
3008 | btrfs_free_reserved_extent(root, ordered_extent->start, | |
e570fd27 | 3009 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
3010 | } |
3011 | ||
3012 | ||
5fd02043 | 3013 | /* |
8bad3c02 LB |
3014 | * This needs to be done to make sure anybody waiting knows we are done |
3015 | * updating everything for this ordered extent. | |
5fd02043 JB |
3016 | */ |
3017 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
3018 | ||
38c227d8 | 3019 | /* for snapshot-aware defrag */ |
6f519564 LB |
3020 | if (new) { |
3021 | if (ret) { | |
3022 | free_sa_defrag_extent(new); | |
3023 | atomic_dec(&root->fs_info->defrag_running); | |
3024 | } else { | |
3025 | relink_file_extents(new); | |
3026 | } | |
3027 | } | |
38c227d8 | 3028 | |
e6dcd2dc CM |
3029 | /* once for us */ |
3030 | btrfs_put_ordered_extent(ordered_extent); | |
3031 | /* once for the tree */ | |
3032 | btrfs_put_ordered_extent(ordered_extent); | |
3033 | ||
5fd02043 JB |
3034 | return ret; |
3035 | } | |
3036 | ||
3037 | static void finish_ordered_fn(struct btrfs_work *work) | |
3038 | { | |
3039 | struct btrfs_ordered_extent *ordered_extent; | |
3040 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3041 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3042 | } |
3043 | ||
b2950863 | 3044 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
3045 | struct extent_state *state, int uptodate) |
3046 | { | |
5fd02043 JB |
3047 | struct inode *inode = page->mapping->host; |
3048 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3049 | struct btrfs_ordered_extent *ordered_extent = NULL; | |
9e0af237 LB |
3050 | struct btrfs_workqueue *wq; |
3051 | btrfs_work_func_t func; | |
5fd02043 | 3052 | |
1abe9b8a | 3053 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3054 | ||
8b62b72b | 3055 | ClearPagePrivate2(page); |
5fd02043 JB |
3056 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3057 | end - start + 1, uptodate)) | |
3058 | return 0; | |
3059 | ||
9e0af237 LB |
3060 | if (btrfs_is_free_space_inode(inode)) { |
3061 | wq = root->fs_info->endio_freespace_worker; | |
3062 | func = btrfs_freespace_write_helper; | |
3063 | } else { | |
3064 | wq = root->fs_info->endio_write_workers; | |
3065 | func = btrfs_endio_write_helper; | |
3066 | } | |
5fd02043 | 3067 | |
9e0af237 LB |
3068 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3069 | NULL); | |
3070 | btrfs_queue_work(wq, &ordered_extent->work); | |
5fd02043 JB |
3071 | |
3072 | return 0; | |
211f90e6 CM |
3073 | } |
3074 | ||
dc380aea MX |
3075 | static int __readpage_endio_check(struct inode *inode, |
3076 | struct btrfs_io_bio *io_bio, | |
3077 | int icsum, struct page *page, | |
3078 | int pgoff, u64 start, size_t len) | |
3079 | { | |
3080 | char *kaddr; | |
3081 | u32 csum_expected; | |
3082 | u32 csum = ~(u32)0; | |
dc380aea MX |
3083 | |
3084 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3085 | ||
3086 | kaddr = kmap_atomic(page); | |
3087 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
3088 | btrfs_csum_final(csum, (char *)&csum); | |
3089 | if (csum != csum_expected) | |
3090 | goto zeroit; | |
3091 | ||
3092 | kunmap_atomic(kaddr); | |
3093 | return 0; | |
3094 | zeroit: | |
94647322 DS |
3095 | btrfs_warn_rl(BTRFS_I(inode)->root->fs_info, |
3096 | "csum failed ino %llu off %llu csum %u expected csum %u", | |
dc380aea MX |
3097 | btrfs_ino(inode), start, csum, csum_expected); |
3098 | memset(kaddr + pgoff, 1, len); | |
3099 | flush_dcache_page(page); | |
3100 | kunmap_atomic(kaddr); | |
3101 | if (csum_expected == 0) | |
3102 | return 0; | |
3103 | return -EIO; | |
3104 | } | |
3105 | ||
d352ac68 CM |
3106 | /* |
3107 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3108 | * if there's a match, we allow the bio to finish. If not, the code in |
3109 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3110 | */ |
facc8a22 MX |
3111 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3112 | u64 phy_offset, struct page *page, | |
3113 | u64 start, u64 end, int mirror) | |
07157aac | 3114 | { |
4eee4fa4 | 3115 | size_t offset = start - page_offset(page); |
07157aac | 3116 | struct inode *inode = page->mapping->host; |
d1310b2e | 3117 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3118 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3119 | |
d20f7043 CM |
3120 | if (PageChecked(page)) { |
3121 | ClearPageChecked(page); | |
dc380aea | 3122 | return 0; |
d20f7043 | 3123 | } |
6cbff00f CH |
3124 | |
3125 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3126 | return 0; |
17d217fe YZ |
3127 | |
3128 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3129 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
91166212 | 3130 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM); |
b6cda9bc | 3131 | return 0; |
17d217fe | 3132 | } |
d20f7043 | 3133 | |
facc8a22 | 3134 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3135 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3136 | start, (size_t)(end - start + 1)); | |
07157aac | 3137 | } |
b888db2b | 3138 | |
24bbcf04 YZ |
3139 | void btrfs_add_delayed_iput(struct inode *inode) |
3140 | { | |
3141 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
8089fe62 | 3142 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3143 | |
3144 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3145 | return; | |
3146 | ||
24bbcf04 | 3147 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3148 | if (binode->delayed_iput_count == 0) { |
3149 | ASSERT(list_empty(&binode->delayed_iput)); | |
3150 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
3151 | } else { | |
3152 | binode->delayed_iput_count++; | |
3153 | } | |
24bbcf04 YZ |
3154 | spin_unlock(&fs_info->delayed_iput_lock); |
3155 | } | |
3156 | ||
3157 | void btrfs_run_delayed_iputs(struct btrfs_root *root) | |
3158 | { | |
24bbcf04 | 3159 | struct btrfs_fs_info *fs_info = root->fs_info; |
24bbcf04 | 3160 | |
24bbcf04 | 3161 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3162 | while (!list_empty(&fs_info->delayed_iputs)) { |
3163 | struct btrfs_inode *inode; | |
3164 | ||
3165 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3166 | struct btrfs_inode, delayed_iput); | |
3167 | if (inode->delayed_iput_count) { | |
3168 | inode->delayed_iput_count--; | |
3169 | list_move_tail(&inode->delayed_iput, | |
3170 | &fs_info->delayed_iputs); | |
3171 | } else { | |
3172 | list_del_init(&inode->delayed_iput); | |
3173 | } | |
3174 | spin_unlock(&fs_info->delayed_iput_lock); | |
3175 | iput(&inode->vfs_inode); | |
3176 | spin_lock(&fs_info->delayed_iput_lock); | |
24bbcf04 | 3177 | } |
8089fe62 | 3178 | spin_unlock(&fs_info->delayed_iput_lock); |
24bbcf04 YZ |
3179 | } |
3180 | ||
d68fc57b | 3181 | /* |
42b2aa86 | 3182 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3183 | * files in the subvolume, it removes orphan item and frees block_rsv |
3184 | * structure. | |
3185 | */ | |
3186 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3187 | struct btrfs_root *root) | |
3188 | { | |
90290e19 | 3189 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3190 | int ret; |
3191 | ||
8a35d95f | 3192 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3193 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3194 | return; | |
3195 | ||
90290e19 | 3196 | spin_lock(&root->orphan_lock); |
8a35d95f | 3197 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3198 | spin_unlock(&root->orphan_lock); |
3199 | return; | |
3200 | } | |
3201 | ||
3202 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3203 | spin_unlock(&root->orphan_lock); | |
3204 | return; | |
3205 | } | |
3206 | ||
3207 | block_rsv = root->orphan_block_rsv; | |
3208 | root->orphan_block_rsv = NULL; | |
3209 | spin_unlock(&root->orphan_lock); | |
3210 | ||
27cdeb70 | 3211 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b YZ |
3212 | btrfs_root_refs(&root->root_item) > 0) { |
3213 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, | |
3214 | root->root_key.objectid); | |
4ef31a45 JB |
3215 | if (ret) |
3216 | btrfs_abort_transaction(trans, root, ret); | |
3217 | else | |
27cdeb70 MX |
3218 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3219 | &root->state); | |
d68fc57b YZ |
3220 | } |
3221 | ||
90290e19 JB |
3222 | if (block_rsv) { |
3223 | WARN_ON(block_rsv->size > 0); | |
3224 | btrfs_free_block_rsv(root, block_rsv); | |
d68fc57b YZ |
3225 | } |
3226 | } | |
3227 | ||
7b128766 JB |
3228 | /* |
3229 | * This creates an orphan entry for the given inode in case something goes | |
3230 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3231 | * |
3232 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3233 | * this function. | |
7b128766 JB |
3234 | */ |
3235 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
3236 | { | |
3237 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3238 | struct btrfs_block_rsv *block_rsv = NULL; |
3239 | int reserve = 0; | |
3240 | int insert = 0; | |
3241 | int ret; | |
7b128766 | 3242 | |
d68fc57b | 3243 | if (!root->orphan_block_rsv) { |
66d8f3dd | 3244 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
b532402e TI |
3245 | if (!block_rsv) |
3246 | return -ENOMEM; | |
d68fc57b | 3247 | } |
7b128766 | 3248 | |
d68fc57b YZ |
3249 | spin_lock(&root->orphan_lock); |
3250 | if (!root->orphan_block_rsv) { | |
3251 | root->orphan_block_rsv = block_rsv; | |
3252 | } else if (block_rsv) { | |
3253 | btrfs_free_block_rsv(root, block_rsv); | |
3254 | block_rsv = NULL; | |
7b128766 | 3255 | } |
7b128766 | 3256 | |
8a35d95f JB |
3257 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3258 | &BTRFS_I(inode)->runtime_flags)) { | |
d68fc57b YZ |
3259 | #if 0 |
3260 | /* | |
3261 | * For proper ENOSPC handling, we should do orphan | |
3262 | * cleanup when mounting. But this introduces backward | |
3263 | * compatibility issue. | |
3264 | */ | |
3265 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3266 | insert = 2; | |
3267 | else | |
3268 | insert = 1; | |
3269 | #endif | |
3270 | insert = 1; | |
321f0e70 | 3271 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3272 | } |
3273 | ||
72ac3c0d JB |
3274 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3275 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3276 | reserve = 1; |
d68fc57b | 3277 | spin_unlock(&root->orphan_lock); |
7b128766 | 3278 | |
d68fc57b YZ |
3279 | /* grab metadata reservation from transaction handle */ |
3280 | if (reserve) { | |
3281 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
3b6571c1 JB |
3282 | ASSERT(!ret); |
3283 | if (ret) { | |
3284 | atomic_dec(&root->orphan_inodes); | |
3285 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3286 | &BTRFS_I(inode)->runtime_flags); | |
3287 | if (insert) | |
3288 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, | |
3289 | &BTRFS_I(inode)->runtime_flags); | |
3290 | return ret; | |
3291 | } | |
d68fc57b | 3292 | } |
7b128766 | 3293 | |
d68fc57b YZ |
3294 | /* insert an orphan item to track this unlinked/truncated file */ |
3295 | if (insert >= 1) { | |
33345d01 | 3296 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3297 | if (ret) { |
703c88e0 | 3298 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3299 | if (reserve) { |
3300 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3301 | &BTRFS_I(inode)->runtime_flags); | |
3302 | btrfs_orphan_release_metadata(inode); | |
3303 | } | |
3304 | if (ret != -EEXIST) { | |
e8e7cff6 JB |
3305 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3306 | &BTRFS_I(inode)->runtime_flags); | |
4ef31a45 JB |
3307 | btrfs_abort_transaction(trans, root, ret); |
3308 | return ret; | |
3309 | } | |
79787eaa JM |
3310 | } |
3311 | ret = 0; | |
d68fc57b YZ |
3312 | } |
3313 | ||
3314 | /* insert an orphan item to track subvolume contains orphan files */ | |
3315 | if (insert >= 2) { | |
3316 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, | |
3317 | root->root_key.objectid); | |
79787eaa JM |
3318 | if (ret && ret != -EEXIST) { |
3319 | btrfs_abort_transaction(trans, root, ret); | |
3320 | return ret; | |
3321 | } | |
d68fc57b YZ |
3322 | } |
3323 | return 0; | |
7b128766 JB |
3324 | } |
3325 | ||
3326 | /* | |
3327 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3328 | * item for this particular inode. | |
3329 | */ | |
48a3b636 ES |
3330 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3331 | struct inode *inode) | |
7b128766 JB |
3332 | { |
3333 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3334 | int delete_item = 0; |
3335 | int release_rsv = 0; | |
7b128766 JB |
3336 | int ret = 0; |
3337 | ||
d68fc57b | 3338 | spin_lock(&root->orphan_lock); |
8a35d95f JB |
3339 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3340 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3341 | delete_item = 1; |
7b128766 | 3342 | |
72ac3c0d JB |
3343 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3344 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3345 | release_rsv = 1; |
d68fc57b | 3346 | spin_unlock(&root->orphan_lock); |
7b128766 | 3347 | |
703c88e0 | 3348 | if (delete_item) { |
8a35d95f | 3349 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3350 | if (trans) |
3351 | ret = btrfs_del_orphan_item(trans, root, | |
3352 | btrfs_ino(inode)); | |
8a35d95f | 3353 | } |
7b128766 | 3354 | |
703c88e0 FDBM |
3355 | if (release_rsv) |
3356 | btrfs_orphan_release_metadata(inode); | |
3357 | ||
4ef31a45 | 3358 | return ret; |
7b128766 JB |
3359 | } |
3360 | ||
3361 | /* | |
3362 | * this cleans up any orphans that may be left on the list from the last use | |
3363 | * of this root. | |
3364 | */ | |
66b4ffd1 | 3365 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 JB |
3366 | { |
3367 | struct btrfs_path *path; | |
3368 | struct extent_buffer *leaf; | |
7b128766 JB |
3369 | struct btrfs_key key, found_key; |
3370 | struct btrfs_trans_handle *trans; | |
3371 | struct inode *inode; | |
8f6d7f4f | 3372 | u64 last_objectid = 0; |
7b128766 JB |
3373 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3374 | ||
d68fc57b | 3375 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3376 | return 0; |
c71bf099 YZ |
3377 | |
3378 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3379 | if (!path) { |
3380 | ret = -ENOMEM; | |
3381 | goto out; | |
3382 | } | |
e4058b54 | 3383 | path->reada = READA_BACK; |
7b128766 JB |
3384 | |
3385 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3386 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3387 | key.offset = (u64)-1; |
3388 | ||
7b128766 JB |
3389 | while (1) { |
3390 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3391 | if (ret < 0) |
3392 | goto out; | |
7b128766 JB |
3393 | |
3394 | /* | |
3395 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3396 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3397 | * find the key and see if we have stuff that matches |
3398 | */ | |
3399 | if (ret > 0) { | |
66b4ffd1 | 3400 | ret = 0; |
7b128766 JB |
3401 | if (path->slots[0] == 0) |
3402 | break; | |
3403 | path->slots[0]--; | |
3404 | } | |
3405 | ||
3406 | /* pull out the item */ | |
3407 | leaf = path->nodes[0]; | |
7b128766 JB |
3408 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3409 | ||
3410 | /* make sure the item matches what we want */ | |
3411 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3412 | break; | |
962a298f | 3413 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3414 | break; |
3415 | ||
3416 | /* release the path since we're done with it */ | |
b3b4aa74 | 3417 | btrfs_release_path(path); |
7b128766 JB |
3418 | |
3419 | /* | |
3420 | * this is where we are basically btrfs_lookup, without the | |
3421 | * crossing root thing. we store the inode number in the | |
3422 | * offset of the orphan item. | |
3423 | */ | |
8f6d7f4f JB |
3424 | |
3425 | if (found_key.offset == last_objectid) { | |
c2cf52eb SK |
3426 | btrfs_err(root->fs_info, |
3427 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3428 | ret = -EINVAL; |
3429 | goto out; | |
3430 | } | |
3431 | ||
3432 | last_objectid = found_key.offset; | |
3433 | ||
5d4f98a2 YZ |
3434 | found_key.objectid = found_key.offset; |
3435 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3436 | found_key.offset = 0; | |
73f73415 | 3437 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3438 | ret = PTR_ERR_OR_ZERO(inode); |
a8c9e576 | 3439 | if (ret && ret != -ESTALE) |
66b4ffd1 | 3440 | goto out; |
7b128766 | 3441 | |
f8e9e0b0 AJ |
3442 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
3443 | struct btrfs_root *dead_root; | |
3444 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3445 | int is_dead_root = 0; | |
3446 | ||
3447 | /* | |
3448 | * this is an orphan in the tree root. Currently these | |
3449 | * could come from 2 sources: | |
3450 | * a) a snapshot deletion in progress | |
3451 | * b) a free space cache inode | |
3452 | * We need to distinguish those two, as the snapshot | |
3453 | * orphan must not get deleted. | |
3454 | * find_dead_roots already ran before us, so if this | |
3455 | * is a snapshot deletion, we should find the root | |
3456 | * in the dead_roots list | |
3457 | */ | |
3458 | spin_lock(&fs_info->trans_lock); | |
3459 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3460 | root_list) { | |
3461 | if (dead_root->root_key.objectid == | |
3462 | found_key.objectid) { | |
3463 | is_dead_root = 1; | |
3464 | break; | |
3465 | } | |
3466 | } | |
3467 | spin_unlock(&fs_info->trans_lock); | |
3468 | if (is_dead_root) { | |
3469 | /* prevent this orphan from being found again */ | |
3470 | key.offset = found_key.objectid - 1; | |
3471 | continue; | |
3472 | } | |
3473 | } | |
7b128766 | 3474 | /* |
a8c9e576 JB |
3475 | * Inode is already gone but the orphan item is still there, |
3476 | * kill the orphan item. | |
7b128766 | 3477 | */ |
a8c9e576 JB |
3478 | if (ret == -ESTALE) { |
3479 | trans = btrfs_start_transaction(root, 1); | |
66b4ffd1 JB |
3480 | if (IS_ERR(trans)) { |
3481 | ret = PTR_ERR(trans); | |
3482 | goto out; | |
3483 | } | |
c2cf52eb SK |
3484 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
3485 | found_key.objectid); | |
a8c9e576 JB |
3486 | ret = btrfs_del_orphan_item(trans, root, |
3487 | found_key.objectid); | |
5b21f2ed | 3488 | btrfs_end_transaction(trans, root); |
4ef31a45 JB |
3489 | if (ret) |
3490 | goto out; | |
7b128766 JB |
3491 | continue; |
3492 | } | |
3493 | ||
a8c9e576 JB |
3494 | /* |
3495 | * add this inode to the orphan list so btrfs_orphan_del does | |
3496 | * the proper thing when we hit it | |
3497 | */ | |
8a35d95f JB |
3498 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3499 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3500 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3501 | |
7b128766 JB |
3502 | /* if we have links, this was a truncate, lets do that */ |
3503 | if (inode->i_nlink) { | |
fae7f21c | 3504 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3505 | iput(inode); |
3506 | continue; | |
3507 | } | |
7b128766 | 3508 | nr_truncate++; |
f3fe820c JB |
3509 | |
3510 | /* 1 for the orphan item deletion. */ | |
3511 | trans = btrfs_start_transaction(root, 1); | |
3512 | if (IS_ERR(trans)) { | |
c69b26b0 | 3513 | iput(inode); |
f3fe820c JB |
3514 | ret = PTR_ERR(trans); |
3515 | goto out; | |
3516 | } | |
3517 | ret = btrfs_orphan_add(trans, inode); | |
3518 | btrfs_end_transaction(trans, root); | |
c69b26b0 JB |
3519 | if (ret) { |
3520 | iput(inode); | |
f3fe820c | 3521 | goto out; |
c69b26b0 | 3522 | } |
f3fe820c | 3523 | |
66b4ffd1 | 3524 | ret = btrfs_truncate(inode); |
4a7d0f68 JB |
3525 | if (ret) |
3526 | btrfs_orphan_del(NULL, inode); | |
7b128766 JB |
3527 | } else { |
3528 | nr_unlink++; | |
3529 | } | |
3530 | ||
3531 | /* this will do delete_inode and everything for us */ | |
3532 | iput(inode); | |
66b4ffd1 JB |
3533 | if (ret) |
3534 | goto out; | |
7b128766 | 3535 | } |
3254c876 MX |
3536 | /* release the path since we're done with it */ |
3537 | btrfs_release_path(path); | |
3538 | ||
d68fc57b YZ |
3539 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3540 | ||
3541 | if (root->orphan_block_rsv) | |
3542 | btrfs_block_rsv_release(root, root->orphan_block_rsv, | |
3543 | (u64)-1); | |
3544 | ||
27cdeb70 MX |
3545 | if (root->orphan_block_rsv || |
3546 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3547 | trans = btrfs_join_transaction(root); |
66b4ffd1 JB |
3548 | if (!IS_ERR(trans)) |
3549 | btrfs_end_transaction(trans, root); | |
d68fc57b | 3550 | } |
7b128766 JB |
3551 | |
3552 | if (nr_unlink) | |
4884b476 | 3553 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3554 | if (nr_truncate) |
4884b476 | 3555 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3556 | |
3557 | out: | |
3558 | if (ret) | |
68b663d1 | 3559 | btrfs_err(root->fs_info, |
c2cf52eb | 3560 | "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3561 | btrfs_free_path(path); |
3562 | return ret; | |
7b128766 JB |
3563 | } |
3564 | ||
46a53cca CM |
3565 | /* |
3566 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3567 | * don't find any xattrs, we know there can't be any acls. | |
3568 | * | |
3569 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3570 | */ | |
3571 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3572 | int slot, u64 objectid, |
3573 | int *first_xattr_slot) | |
46a53cca CM |
3574 | { |
3575 | u32 nritems = btrfs_header_nritems(leaf); | |
3576 | struct btrfs_key found_key; | |
f23b5a59 JB |
3577 | static u64 xattr_access = 0; |
3578 | static u64 xattr_default = 0; | |
46a53cca CM |
3579 | int scanned = 0; |
3580 | ||
f23b5a59 | 3581 | if (!xattr_access) { |
97d79299 AG |
3582 | xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS, |
3583 | strlen(XATTR_NAME_POSIX_ACL_ACCESS)); | |
3584 | xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT, | |
3585 | strlen(XATTR_NAME_POSIX_ACL_DEFAULT)); | |
f23b5a59 JB |
3586 | } |
3587 | ||
46a53cca | 3588 | slot++; |
63541927 | 3589 | *first_xattr_slot = -1; |
46a53cca CM |
3590 | while (slot < nritems) { |
3591 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3592 | ||
3593 | /* we found a different objectid, there must not be acls */ | |
3594 | if (found_key.objectid != objectid) | |
3595 | return 0; | |
3596 | ||
3597 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3598 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3599 | if (*first_xattr_slot == -1) |
3600 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3601 | if (found_key.offset == xattr_access || |
3602 | found_key.offset == xattr_default) | |
3603 | return 1; | |
3604 | } | |
46a53cca CM |
3605 | |
3606 | /* | |
3607 | * we found a key greater than an xattr key, there can't | |
3608 | * be any acls later on | |
3609 | */ | |
3610 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3611 | return 0; | |
3612 | ||
3613 | slot++; | |
3614 | scanned++; | |
3615 | ||
3616 | /* | |
3617 | * it goes inode, inode backrefs, xattrs, extents, | |
3618 | * so if there are a ton of hard links to an inode there can | |
3619 | * be a lot of backrefs. Don't waste time searching too hard, | |
3620 | * this is just an optimization | |
3621 | */ | |
3622 | if (scanned >= 8) | |
3623 | break; | |
3624 | } | |
3625 | /* we hit the end of the leaf before we found an xattr or | |
3626 | * something larger than an xattr. We have to assume the inode | |
3627 | * has acls | |
3628 | */ | |
63541927 FDBM |
3629 | if (*first_xattr_slot == -1) |
3630 | *first_xattr_slot = slot; | |
46a53cca CM |
3631 | return 1; |
3632 | } | |
3633 | ||
d352ac68 CM |
3634 | /* |
3635 | * read an inode from the btree into the in-memory inode | |
3636 | */ | |
5d4f98a2 | 3637 | static void btrfs_read_locked_inode(struct inode *inode) |
39279cc3 CM |
3638 | { |
3639 | struct btrfs_path *path; | |
5f39d397 | 3640 | struct extent_buffer *leaf; |
39279cc3 CM |
3641 | struct btrfs_inode_item *inode_item; |
3642 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3643 | struct btrfs_key location; | |
67de1176 | 3644 | unsigned long ptr; |
46a53cca | 3645 | int maybe_acls; |
618e21d5 | 3646 | u32 rdev; |
39279cc3 | 3647 | int ret; |
2f7e33d4 | 3648 | bool filled = false; |
63541927 | 3649 | int first_xattr_slot; |
2f7e33d4 MX |
3650 | |
3651 | ret = btrfs_fill_inode(inode, &rdev); | |
3652 | if (!ret) | |
3653 | filled = true; | |
39279cc3 CM |
3654 | |
3655 | path = btrfs_alloc_path(); | |
1748f843 MF |
3656 | if (!path) |
3657 | goto make_bad; | |
3658 | ||
39279cc3 | 3659 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3660 | |
39279cc3 | 3661 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
5f39d397 | 3662 | if (ret) |
39279cc3 | 3663 | goto make_bad; |
39279cc3 | 3664 | |
5f39d397 | 3665 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3666 | |
3667 | if (filled) | |
67de1176 | 3668 | goto cache_index; |
2f7e33d4 | 3669 | |
5f39d397 CM |
3670 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3671 | struct btrfs_inode_item); | |
5f39d397 | 3672 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3673 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3674 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3675 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
dbe674a9 | 3676 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3677 | |
a937b979 DS |
3678 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3679 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3680 | |
a937b979 DS |
3681 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3682 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3683 | |
a937b979 DS |
3684 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3685 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3686 | |
9cc97d64 | 3687 | BTRFS_I(inode)->i_otime.tv_sec = |
3688 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3689 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3690 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3691 | |
a76a3cd4 | 3692 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3693 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3694 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3695 | ||
6e17d30b YD |
3696 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3697 | inode->i_generation = BTRFS_I(inode)->generation; | |
3698 | inode->i_rdev = 0; | |
3699 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3700 | ||
3701 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3702 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3703 | ||
3704 | cache_index: | |
5dc562c5 JB |
3705 | /* |
3706 | * If we were modified in the current generation and evicted from memory | |
3707 | * and then re-read we need to do a full sync since we don't have any | |
3708 | * idea about which extents were modified before we were evicted from | |
3709 | * cache. | |
6e17d30b YD |
3710 | * |
3711 | * This is required for both inode re-read from disk and delayed inode | |
3712 | * in delayed_nodes_tree. | |
5dc562c5 JB |
3713 | */ |
3714 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) | |
3715 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
3716 | &BTRFS_I(inode)->runtime_flags); | |
3717 | ||
bde6c242 FM |
3718 | /* |
3719 | * We don't persist the id of the transaction where an unlink operation | |
3720 | * against the inode was last made. So here we assume the inode might | |
3721 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3722 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3723 | * between the inode and its parent if the inode is fsync'ed and the log | |
3724 | * replayed. For example, in the scenario: | |
3725 | * | |
3726 | * touch mydir/foo | |
3727 | * ln mydir/foo mydir/bar | |
3728 | * sync | |
3729 | * unlink mydir/bar | |
3730 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3731 | * xfs_io -c fsync mydir/foo | |
3732 | * <power failure> | |
3733 | * mount fs, triggers fsync log replay | |
3734 | * | |
3735 | * We must make sure that when we fsync our inode foo we also log its | |
3736 | * parent inode, otherwise after log replay the parent still has the | |
3737 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3738 | * and doesn't have an inode ref with the name "bar" anymore. | |
3739 | * | |
3740 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
01327610 | 3741 | * but it guarantees correctness at the expense of occasional full |
bde6c242 FM |
3742 | * transaction commits on fsync if our inode is a directory, or if our |
3743 | * inode is not a directory, logging its parent unnecessarily. | |
3744 | */ | |
3745 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3746 | ||
67de1176 MX |
3747 | path->slots[0]++; |
3748 | if (inode->i_nlink != 1 || | |
3749 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3750 | goto cache_acl; | |
3751 | ||
3752 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
3753 | if (location.objectid != btrfs_ino(inode)) | |
3754 | goto cache_acl; | |
3755 | ||
3756 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3757 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3758 | struct btrfs_inode_ref *ref; | |
3759 | ||
3760 | ref = (struct btrfs_inode_ref *)ptr; | |
3761 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3762 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3763 | struct btrfs_inode_extref *extref; | |
3764 | ||
3765 | extref = (struct btrfs_inode_extref *)ptr; | |
3766 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3767 | extref); | |
3768 | } | |
2f7e33d4 | 3769 | cache_acl: |
46a53cca CM |
3770 | /* |
3771 | * try to precache a NULL acl entry for files that don't have | |
3772 | * any xattrs or acls | |
3773 | */ | |
33345d01 | 3774 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
63541927 FDBM |
3775 | btrfs_ino(inode), &first_xattr_slot); |
3776 | if (first_xattr_slot != -1) { | |
3777 | path->slots[0] = first_xattr_slot; | |
3778 | ret = btrfs_load_inode_props(inode, path); | |
3779 | if (ret) | |
3780 | btrfs_err(root->fs_info, | |
351fd353 | 3781 | "error loading props for ino %llu (root %llu): %d", |
63541927 FDBM |
3782 | btrfs_ino(inode), |
3783 | root->root_key.objectid, ret); | |
3784 | } | |
3785 | btrfs_free_path(path); | |
3786 | ||
72c04902 AV |
3787 | if (!maybe_acls) |
3788 | cache_no_acl(inode); | |
46a53cca | 3789 | |
39279cc3 | 3790 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3791 | case S_IFREG: |
3792 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3793 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3794 | inode->i_fop = &btrfs_file_operations; |
3795 | inode->i_op = &btrfs_file_inode_operations; | |
3796 | break; | |
3797 | case S_IFDIR: | |
3798 | inode->i_fop = &btrfs_dir_file_operations; | |
3799 | if (root == root->fs_info->tree_root) | |
3800 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
3801 | else | |
3802 | inode->i_op = &btrfs_dir_inode_operations; | |
3803 | break; | |
3804 | case S_IFLNK: | |
3805 | inode->i_op = &btrfs_symlink_inode_operations; | |
21fc61c7 | 3806 | inode_nohighmem(inode); |
39279cc3 CM |
3807 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
3808 | break; | |
618e21d5 | 3809 | default: |
0279b4cd | 3810 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3811 | init_special_inode(inode, inode->i_mode, rdev); |
3812 | break; | |
39279cc3 | 3813 | } |
6cbff00f CH |
3814 | |
3815 | btrfs_update_iflags(inode); | |
39279cc3 CM |
3816 | return; |
3817 | ||
3818 | make_bad: | |
39279cc3 | 3819 | btrfs_free_path(path); |
39279cc3 CM |
3820 | make_bad_inode(inode); |
3821 | } | |
3822 | ||
d352ac68 CM |
3823 | /* |
3824 | * given a leaf and an inode, copy the inode fields into the leaf | |
3825 | */ | |
e02119d5 CM |
3826 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3827 | struct extent_buffer *leaf, | |
5f39d397 | 3828 | struct btrfs_inode_item *item, |
39279cc3 CM |
3829 | struct inode *inode) |
3830 | { | |
51fab693 LB |
3831 | struct btrfs_map_token token; |
3832 | ||
3833 | btrfs_init_map_token(&token); | |
5f39d397 | 3834 | |
51fab693 LB |
3835 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3836 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3837 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3838 | &token); | |
3839 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3840 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3841 | |
a937b979 | 3842 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3843 | inode->i_atime.tv_sec, &token); |
a937b979 | 3844 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3845 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3846 | |
a937b979 | 3847 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3848 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3849 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3850 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3851 | |
a937b979 | 3852 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3853 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3854 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3855 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3856 | |
9cc97d64 | 3857 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3858 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3859 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3860 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3861 | ||
51fab693 LB |
3862 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3863 | &token); | |
3864 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3865 | &token); | |
3866 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3867 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3868 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3869 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3870 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3871 | } |
3872 | ||
d352ac68 CM |
3873 | /* |
3874 | * copy everything in the in-memory inode into the btree. | |
3875 | */ | |
2115133f | 3876 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3877 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3878 | { |
3879 | struct btrfs_inode_item *inode_item; | |
3880 | struct btrfs_path *path; | |
5f39d397 | 3881 | struct extent_buffer *leaf; |
39279cc3 CM |
3882 | int ret; |
3883 | ||
3884 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3885 | if (!path) |
3886 | return -ENOMEM; | |
3887 | ||
b9473439 | 3888 | path->leave_spinning = 1; |
16cdcec7 MX |
3889 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3890 | 1); | |
39279cc3 CM |
3891 | if (ret) { |
3892 | if (ret > 0) | |
3893 | ret = -ENOENT; | |
3894 | goto failed; | |
3895 | } | |
3896 | ||
5f39d397 CM |
3897 | leaf = path->nodes[0]; |
3898 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3899 | struct btrfs_inode_item); |
39279cc3 | 3900 | |
e02119d5 | 3901 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3902 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3903 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3904 | ret = 0; |
3905 | failed: | |
39279cc3 CM |
3906 | btrfs_free_path(path); |
3907 | return ret; | |
3908 | } | |
3909 | ||
2115133f CM |
3910 | /* |
3911 | * copy everything in the in-memory inode into the btree. | |
3912 | */ | |
3913 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3914 | struct btrfs_root *root, struct inode *inode) | |
3915 | { | |
3916 | int ret; | |
3917 | ||
3918 | /* | |
3919 | * If the inode is a free space inode, we can deadlock during commit | |
3920 | * if we put it into the delayed code. | |
3921 | * | |
3922 | * The data relocation inode should also be directly updated | |
3923 | * without delay | |
3924 | */ | |
83eea1f1 | 3925 | if (!btrfs_is_free_space_inode(inode) |
1d52c78a JB |
3926 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
3927 | && !root->fs_info->log_root_recovering) { | |
8ea05e3a AB |
3928 | btrfs_update_root_times(trans, root); |
3929 | ||
2115133f CM |
3930 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3931 | if (!ret) | |
3932 | btrfs_set_inode_last_trans(trans, inode); | |
3933 | return ret; | |
3934 | } | |
3935 | ||
3936 | return btrfs_update_inode_item(trans, root, inode); | |
3937 | } | |
3938 | ||
be6aef60 JB |
3939 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3940 | struct btrfs_root *root, | |
3941 | struct inode *inode) | |
2115133f CM |
3942 | { |
3943 | int ret; | |
3944 | ||
3945 | ret = btrfs_update_inode(trans, root, inode); | |
3946 | if (ret == -ENOSPC) | |
3947 | return btrfs_update_inode_item(trans, root, inode); | |
3948 | return ret; | |
3949 | } | |
3950 | ||
d352ac68 CM |
3951 | /* |
3952 | * unlink helper that gets used here in inode.c and in the tree logging | |
3953 | * recovery code. It remove a link in a directory with a given name, and | |
3954 | * also drops the back refs in the inode to the directory | |
3955 | */ | |
92986796 AV |
3956 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3957 | struct btrfs_root *root, | |
3958 | struct inode *dir, struct inode *inode, | |
3959 | const char *name, int name_len) | |
39279cc3 CM |
3960 | { |
3961 | struct btrfs_path *path; | |
39279cc3 | 3962 | int ret = 0; |
5f39d397 | 3963 | struct extent_buffer *leaf; |
39279cc3 | 3964 | struct btrfs_dir_item *di; |
5f39d397 | 3965 | struct btrfs_key key; |
aec7477b | 3966 | u64 index; |
33345d01 LZ |
3967 | u64 ino = btrfs_ino(inode); |
3968 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3969 | |
3970 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3971 | if (!path) { |
3972 | ret = -ENOMEM; | |
554233a6 | 3973 | goto out; |
54aa1f4d CM |
3974 | } |
3975 | ||
b9473439 | 3976 | path->leave_spinning = 1; |
33345d01 | 3977 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
3978 | name, name_len, -1); |
3979 | if (IS_ERR(di)) { | |
3980 | ret = PTR_ERR(di); | |
3981 | goto err; | |
3982 | } | |
3983 | if (!di) { | |
3984 | ret = -ENOENT; | |
3985 | goto err; | |
3986 | } | |
5f39d397 CM |
3987 | leaf = path->nodes[0]; |
3988 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 3989 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3990 | if (ret) |
3991 | goto err; | |
b3b4aa74 | 3992 | btrfs_release_path(path); |
39279cc3 | 3993 | |
67de1176 MX |
3994 | /* |
3995 | * If we don't have dir index, we have to get it by looking up | |
3996 | * the inode ref, since we get the inode ref, remove it directly, | |
3997 | * it is unnecessary to do delayed deletion. | |
3998 | * | |
3999 | * But if we have dir index, needn't search inode ref to get it. | |
4000 | * Since the inode ref is close to the inode item, it is better | |
4001 | * that we delay to delete it, and just do this deletion when | |
4002 | * we update the inode item. | |
4003 | */ | |
4004 | if (BTRFS_I(inode)->dir_index) { | |
4005 | ret = btrfs_delayed_delete_inode_ref(inode); | |
4006 | if (!ret) { | |
4007 | index = BTRFS_I(inode)->dir_index; | |
4008 | goto skip_backref; | |
4009 | } | |
4010 | } | |
4011 | ||
33345d01 LZ |
4012 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
4013 | dir_ino, &index); | |
aec7477b | 4014 | if (ret) { |
c2cf52eb SK |
4015 | btrfs_info(root->fs_info, |
4016 | "failed to delete reference to %.*s, inode %llu parent %llu", | |
c1c9ff7c | 4017 | name_len, name, ino, dir_ino); |
79787eaa | 4018 | btrfs_abort_transaction(trans, root, ret); |
aec7477b JB |
4019 | goto err; |
4020 | } | |
67de1176 | 4021 | skip_backref: |
16cdcec7 | 4022 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
4023 | if (ret) { |
4024 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 4025 | goto err; |
79787eaa | 4026 | } |
39279cc3 | 4027 | |
e02119d5 | 4028 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
33345d01 | 4029 | inode, dir_ino); |
79787eaa JM |
4030 | if (ret != 0 && ret != -ENOENT) { |
4031 | btrfs_abort_transaction(trans, root, ret); | |
4032 | goto err; | |
4033 | } | |
e02119d5 CM |
4034 | |
4035 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | |
4036 | dir, index); | |
6418c961 CM |
4037 | if (ret == -ENOENT) |
4038 | ret = 0; | |
d4e3991b ZB |
4039 | else if (ret) |
4040 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 CM |
4041 | err: |
4042 | btrfs_free_path(path); | |
e02119d5 CM |
4043 | if (ret) |
4044 | goto out; | |
4045 | ||
4046 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 JB |
4047 | inode_inc_iversion(inode); |
4048 | inode_inc_iversion(dir); | |
04b285f3 DD |
4049 | inode->i_ctime = dir->i_mtime = |
4050 | dir->i_ctime = current_fs_time(inode->i_sb); | |
b9959295 | 4051 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 4052 | out: |
39279cc3 CM |
4053 | return ret; |
4054 | } | |
4055 | ||
92986796 AV |
4056 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4057 | struct btrfs_root *root, | |
4058 | struct inode *dir, struct inode *inode, | |
4059 | const char *name, int name_len) | |
4060 | { | |
4061 | int ret; | |
4062 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4063 | if (!ret) { | |
8b558c5f | 4064 | drop_nlink(inode); |
92986796 AV |
4065 | ret = btrfs_update_inode(trans, root, inode); |
4066 | } | |
4067 | return ret; | |
4068 | } | |
39279cc3 | 4069 | |
a22285a6 YZ |
4070 | /* |
4071 | * helper to start transaction for unlink and rmdir. | |
4072 | * | |
d52be818 JB |
4073 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4074 | * if we cannot make our reservations the normal way try and see if there is | |
4075 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4076 | * allow the unlink to occur. | |
a22285a6 | 4077 | */ |
d52be818 | 4078 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4079 | { |
a22285a6 | 4080 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4081 | |
e70bea5f JB |
4082 | /* |
4083 | * 1 for the possible orphan item | |
4084 | * 1 for the dir item | |
4085 | * 1 for the dir index | |
4086 | * 1 for the inode ref | |
e70bea5f JB |
4087 | * 1 for the inode |
4088 | */ | |
8eab77ff | 4089 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4090 | } |
4091 | ||
4092 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4093 | { | |
4094 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4095 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4096 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4097 | int ret; |
a22285a6 | 4098 | |
d52be818 | 4099 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4100 | if (IS_ERR(trans)) |
4101 | return PTR_ERR(trans); | |
5f39d397 | 4102 | |
2b0143b5 | 4103 | btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0); |
12fcfd22 | 4104 | |
2b0143b5 | 4105 | ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4106 | dentry->d_name.name, dentry->d_name.len); |
b532402e TI |
4107 | if (ret) |
4108 | goto out; | |
7b128766 | 4109 | |
a22285a6 | 4110 | if (inode->i_nlink == 0) { |
7b128766 | 4111 | ret = btrfs_orphan_add(trans, inode); |
b532402e TI |
4112 | if (ret) |
4113 | goto out; | |
a22285a6 | 4114 | } |
7b128766 | 4115 | |
b532402e | 4116 | out: |
d52be818 | 4117 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4118 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
4119 | return ret; |
4120 | } | |
4121 | ||
4df27c4d YZ |
4122 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4123 | struct btrfs_root *root, | |
4124 | struct inode *dir, u64 objectid, | |
4125 | const char *name, int name_len) | |
4126 | { | |
4127 | struct btrfs_path *path; | |
4128 | struct extent_buffer *leaf; | |
4129 | struct btrfs_dir_item *di; | |
4130 | struct btrfs_key key; | |
4131 | u64 index; | |
4132 | int ret; | |
33345d01 | 4133 | u64 dir_ino = btrfs_ino(dir); |
4df27c4d YZ |
4134 | |
4135 | path = btrfs_alloc_path(); | |
4136 | if (!path) | |
4137 | return -ENOMEM; | |
4138 | ||
33345d01 | 4139 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4140 | name, name_len, -1); |
79787eaa JM |
4141 | if (IS_ERR_OR_NULL(di)) { |
4142 | if (!di) | |
4143 | ret = -ENOENT; | |
4144 | else | |
4145 | ret = PTR_ERR(di); | |
4146 | goto out; | |
4147 | } | |
4df27c4d YZ |
4148 | |
4149 | leaf = path->nodes[0]; | |
4150 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4151 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4152 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa JM |
4153 | if (ret) { |
4154 | btrfs_abort_transaction(trans, root, ret); | |
4155 | goto out; | |
4156 | } | |
b3b4aa74 | 4157 | btrfs_release_path(path); |
4df27c4d YZ |
4158 | |
4159 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
4160 | objectid, root->root_key.objectid, | |
33345d01 | 4161 | dir_ino, &index, name, name_len); |
4df27c4d | 4162 | if (ret < 0) { |
79787eaa JM |
4163 | if (ret != -ENOENT) { |
4164 | btrfs_abort_transaction(trans, root, ret); | |
4165 | goto out; | |
4166 | } | |
33345d01 | 4167 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4168 | name, name_len); |
79787eaa JM |
4169 | if (IS_ERR_OR_NULL(di)) { |
4170 | if (!di) | |
4171 | ret = -ENOENT; | |
4172 | else | |
4173 | ret = PTR_ERR(di); | |
4174 | btrfs_abort_transaction(trans, root, ret); | |
4175 | goto out; | |
4176 | } | |
4df27c4d YZ |
4177 | |
4178 | leaf = path->nodes[0]; | |
4179 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4180 | btrfs_release_path(path); |
4df27c4d YZ |
4181 | index = key.offset; |
4182 | } | |
945d8962 | 4183 | btrfs_release_path(path); |
4df27c4d | 4184 | |
16cdcec7 | 4185 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
4186 | if (ret) { |
4187 | btrfs_abort_transaction(trans, root, ret); | |
4188 | goto out; | |
4189 | } | |
4df27c4d YZ |
4190 | |
4191 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 | 4192 | inode_inc_iversion(dir); |
04b285f3 | 4193 | dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb); |
5a24e84c | 4194 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa JM |
4195 | if (ret) |
4196 | btrfs_abort_transaction(trans, root, ret); | |
4197 | out: | |
71d7aed0 | 4198 | btrfs_free_path(path); |
79787eaa | 4199 | return ret; |
4df27c4d YZ |
4200 | } |
4201 | ||
39279cc3 CM |
4202 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4203 | { | |
2b0143b5 | 4204 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4205 | int err = 0; |
39279cc3 | 4206 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4207 | struct btrfs_trans_handle *trans; |
39279cc3 | 4208 | |
b3ae244e | 4209 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4210 | return -ENOTEMPTY; |
b3ae244e DS |
4211 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
4212 | return -EPERM; | |
134d4512 | 4213 | |
d52be818 | 4214 | trans = __unlink_start_trans(dir); |
a22285a6 | 4215 | if (IS_ERR(trans)) |
5df6a9f6 | 4216 | return PTR_ERR(trans); |
5df6a9f6 | 4217 | |
33345d01 | 4218 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4219 | err = btrfs_unlink_subvol(trans, root, dir, |
4220 | BTRFS_I(inode)->location.objectid, | |
4221 | dentry->d_name.name, | |
4222 | dentry->d_name.len); | |
4223 | goto out; | |
4224 | } | |
4225 | ||
7b128766 JB |
4226 | err = btrfs_orphan_add(trans, inode); |
4227 | if (err) | |
4df27c4d | 4228 | goto out; |
7b128766 | 4229 | |
39279cc3 | 4230 | /* now the directory is empty */ |
2b0143b5 | 4231 | err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4232 | dentry->d_name.name, dentry->d_name.len); |
d397712b | 4233 | if (!err) |
dbe674a9 | 4234 | btrfs_i_size_write(inode, 0); |
4df27c4d | 4235 | out: |
d52be818 | 4236 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4237 | btrfs_btree_balance_dirty(root); |
3954401f | 4238 | |
39279cc3 CM |
4239 | return err; |
4240 | } | |
4241 | ||
28f75a0e CM |
4242 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4243 | struct btrfs_root *root, | |
4244 | u64 bytes_deleted) | |
4245 | { | |
4246 | int ret; | |
4247 | ||
dc95f7bf JB |
4248 | /* |
4249 | * This is only used to apply pressure to the enospc system, we don't | |
4250 | * intend to use this reservation at all. | |
4251 | */ | |
28f75a0e | 4252 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); |
dc95f7bf | 4253 | bytes_deleted *= root->nodesize; |
28f75a0e CM |
4254 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, |
4255 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); | |
dc95f7bf JB |
4256 | if (!ret) { |
4257 | trace_btrfs_space_reservation(root->fs_info, "transaction", | |
4258 | trans->transid, | |
4259 | bytes_deleted, 1); | |
28f75a0e | 4260 | trans->bytes_reserved += bytes_deleted; |
dc95f7bf | 4261 | } |
28f75a0e CM |
4262 | return ret; |
4263 | ||
4264 | } | |
4265 | ||
0305cd5f FM |
4266 | static int truncate_inline_extent(struct inode *inode, |
4267 | struct btrfs_path *path, | |
4268 | struct btrfs_key *found_key, | |
4269 | const u64 item_end, | |
4270 | const u64 new_size) | |
4271 | { | |
4272 | struct extent_buffer *leaf = path->nodes[0]; | |
4273 | int slot = path->slots[0]; | |
4274 | struct btrfs_file_extent_item *fi; | |
4275 | u32 size = (u32)(new_size - found_key->offset); | |
4276 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4277 | ||
4278 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4279 | ||
4280 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { | |
4281 | loff_t offset = new_size; | |
09cbfeaf | 4282 | loff_t page_end = ALIGN(offset, PAGE_SIZE); |
0305cd5f FM |
4283 | |
4284 | /* | |
4285 | * Zero out the remaining of the last page of our inline extent, | |
4286 | * instead of directly truncating our inline extent here - that | |
4287 | * would be much more complex (decompressing all the data, then | |
4288 | * compressing the truncated data, which might be bigger than | |
4289 | * the size of the inline extent, resize the extent, etc). | |
4290 | * We release the path because to get the page we might need to | |
4291 | * read the extent item from disk (data not in the page cache). | |
4292 | */ | |
4293 | btrfs_release_path(path); | |
9703fefe CR |
4294 | return btrfs_truncate_block(inode, offset, page_end - offset, |
4295 | 0); | |
0305cd5f FM |
4296 | } |
4297 | ||
4298 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4299 | size = btrfs_file_extent_calc_inline_size(size); | |
4300 | btrfs_truncate_item(root, path, size, 1); | |
4301 | ||
4302 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4303 | inode_sub_bytes(inode, item_end + 1 - new_size); | |
4304 | ||
4305 | return 0; | |
4306 | } | |
4307 | ||
39279cc3 CM |
4308 | /* |
4309 | * this can truncate away extent items, csum items and directory items. | |
4310 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4311 | * any higher than new_size |
39279cc3 CM |
4312 | * |
4313 | * csum items that cross the new i_size are truncated to the new size | |
4314 | * as well. | |
7b128766 JB |
4315 | * |
4316 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4317 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4318 | */ |
8082510e YZ |
4319 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4320 | struct btrfs_root *root, | |
4321 | struct inode *inode, | |
4322 | u64 new_size, u32 min_type) | |
39279cc3 | 4323 | { |
39279cc3 | 4324 | struct btrfs_path *path; |
5f39d397 | 4325 | struct extent_buffer *leaf; |
39279cc3 | 4326 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4327 | struct btrfs_key key; |
4328 | struct btrfs_key found_key; | |
39279cc3 | 4329 | u64 extent_start = 0; |
db94535d | 4330 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4331 | u64 extent_offset = 0; |
39279cc3 | 4332 | u64 item_end = 0; |
c1aa4575 | 4333 | u64 last_size = new_size; |
8082510e | 4334 | u32 found_type = (u8)-1; |
39279cc3 CM |
4335 | int found_extent; |
4336 | int del_item; | |
85e21bac CM |
4337 | int pending_del_nr = 0; |
4338 | int pending_del_slot = 0; | |
179e29e4 | 4339 | int extent_type = -1; |
8082510e YZ |
4340 | int ret; |
4341 | int err = 0; | |
33345d01 | 4342 | u64 ino = btrfs_ino(inode); |
28ed1345 | 4343 | u64 bytes_deleted = 0; |
1262133b JB |
4344 | bool be_nice = 0; |
4345 | bool should_throttle = 0; | |
28f75a0e | 4346 | bool should_end = 0; |
8082510e YZ |
4347 | |
4348 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4349 | |
28ed1345 CM |
4350 | /* |
4351 | * for non-free space inodes and ref cows, we want to back off from | |
4352 | * time to time | |
4353 | */ | |
4354 | if (!btrfs_is_free_space_inode(inode) && | |
4355 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4356 | be_nice = 1; | |
4357 | ||
0eb0e19c MF |
4358 | path = btrfs_alloc_path(); |
4359 | if (!path) | |
4360 | return -ENOMEM; | |
e4058b54 | 4361 | path->reada = READA_BACK; |
0eb0e19c | 4362 | |
5dc562c5 JB |
4363 | /* |
4364 | * We want to drop from the next block forward in case this new size is | |
4365 | * not block aligned since we will be keeping the last block of the | |
4366 | * extent just the way it is. | |
4367 | */ | |
27cdeb70 MX |
4368 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
4369 | root == root->fs_info->tree_root) | |
fda2832f QW |
4370 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
4371 | root->sectorsize), (u64)-1, 0); | |
8082510e | 4372 | |
16cdcec7 MX |
4373 | /* |
4374 | * This function is also used to drop the items in the log tree before | |
4375 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4376 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4377 | * items. | |
4378 | */ | |
4379 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4380 | btrfs_kill_delayed_inode_items(inode); | |
4381 | ||
33345d01 | 4382 | key.objectid = ino; |
39279cc3 | 4383 | key.offset = (u64)-1; |
5f39d397 CM |
4384 | key.type = (u8)-1; |
4385 | ||
85e21bac | 4386 | search_again: |
28ed1345 CM |
4387 | /* |
4388 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4389 | * up a huge file in a single leaf. Most of the time that | |
4390 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4391 | */ | |
ee22184b | 4392 | if (be_nice && bytes_deleted > SZ_32M) { |
28ed1345 CM |
4393 | if (btrfs_should_end_transaction(trans, root)) { |
4394 | err = -EAGAIN; | |
4395 | goto error; | |
4396 | } | |
4397 | } | |
4398 | ||
4399 | ||
b9473439 | 4400 | path->leave_spinning = 1; |
85e21bac | 4401 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4402 | if (ret < 0) { |
4403 | err = ret; | |
4404 | goto out; | |
4405 | } | |
d397712b | 4406 | |
85e21bac | 4407 | if (ret > 0) { |
e02119d5 CM |
4408 | /* there are no items in the tree for us to truncate, we're |
4409 | * done | |
4410 | */ | |
8082510e YZ |
4411 | if (path->slots[0] == 0) |
4412 | goto out; | |
85e21bac CM |
4413 | path->slots[0]--; |
4414 | } | |
4415 | ||
d397712b | 4416 | while (1) { |
39279cc3 | 4417 | fi = NULL; |
5f39d397 CM |
4418 | leaf = path->nodes[0]; |
4419 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4420 | found_type = found_key.type; |
39279cc3 | 4421 | |
33345d01 | 4422 | if (found_key.objectid != ino) |
39279cc3 | 4423 | break; |
5f39d397 | 4424 | |
85e21bac | 4425 | if (found_type < min_type) |
39279cc3 CM |
4426 | break; |
4427 | ||
5f39d397 | 4428 | item_end = found_key.offset; |
39279cc3 | 4429 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4430 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4431 | struct btrfs_file_extent_item); |
179e29e4 CM |
4432 | extent_type = btrfs_file_extent_type(leaf, fi); |
4433 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4434 | item_end += |
db94535d | 4435 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 | 4436 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4437 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4438 | path->slots[0], fi); |
39279cc3 | 4439 | } |
008630c1 | 4440 | item_end--; |
39279cc3 | 4441 | } |
8082510e YZ |
4442 | if (found_type > min_type) { |
4443 | del_item = 1; | |
4444 | } else { | |
4445 | if (item_end < new_size) | |
b888db2b | 4446 | break; |
8082510e YZ |
4447 | if (found_key.offset >= new_size) |
4448 | del_item = 1; | |
4449 | else | |
4450 | del_item = 0; | |
39279cc3 | 4451 | } |
39279cc3 | 4452 | found_extent = 0; |
39279cc3 | 4453 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4454 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4455 | goto delete; | |
4456 | ||
7f4f6e0a JB |
4457 | if (del_item) |
4458 | last_size = found_key.offset; | |
4459 | else | |
4460 | last_size = new_size; | |
4461 | ||
179e29e4 | 4462 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4463 | u64 num_dec; |
db94535d | 4464 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4465 | if (!del_item) { |
db94535d CM |
4466 | u64 orig_num_bytes = |
4467 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4468 | extent_num_bytes = ALIGN(new_size - |
4469 | found_key.offset, | |
4470 | root->sectorsize); | |
db94535d CM |
4471 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4472 | extent_num_bytes); | |
4473 | num_dec = (orig_num_bytes - | |
9069218d | 4474 | extent_num_bytes); |
27cdeb70 MX |
4475 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4476 | &root->state) && | |
4477 | extent_start != 0) | |
a76a3cd4 | 4478 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4479 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4480 | } else { |
db94535d CM |
4481 | extent_num_bytes = |
4482 | btrfs_file_extent_disk_num_bytes(leaf, | |
4483 | fi); | |
5d4f98a2 YZ |
4484 | extent_offset = found_key.offset - |
4485 | btrfs_file_extent_offset(leaf, fi); | |
4486 | ||
39279cc3 | 4487 | /* FIXME blocksize != 4096 */ |
9069218d | 4488 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4489 | if (extent_start != 0) { |
4490 | found_extent = 1; | |
27cdeb70 MX |
4491 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4492 | &root->state)) | |
a76a3cd4 | 4493 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4494 | } |
39279cc3 | 4495 | } |
9069218d | 4496 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4497 | /* |
4498 | * we can't truncate inline items that have had | |
4499 | * special encodings | |
4500 | */ | |
4501 | if (!del_item && | |
c8b97818 CM |
4502 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
4503 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
514ac8ad CM |
4504 | |
4505 | /* | |
0305cd5f FM |
4506 | * Need to release path in order to truncate a |
4507 | * compressed extent. So delete any accumulated | |
4508 | * extent items so far. | |
514ac8ad | 4509 | */ |
0305cd5f FM |
4510 | if (btrfs_file_extent_compression(leaf, fi) != |
4511 | BTRFS_COMPRESS_NONE && pending_del_nr) { | |
4512 | err = btrfs_del_items(trans, root, path, | |
4513 | pending_del_slot, | |
4514 | pending_del_nr); | |
4515 | if (err) { | |
4516 | btrfs_abort_transaction(trans, | |
4517 | root, | |
4518 | err); | |
4519 | goto error; | |
4520 | } | |
4521 | pending_del_nr = 0; | |
4522 | } | |
4523 | ||
4524 | err = truncate_inline_extent(inode, path, | |
4525 | &found_key, | |
4526 | item_end, | |
4527 | new_size); | |
4528 | if (err) { | |
4529 | btrfs_abort_transaction(trans, | |
4530 | root, err); | |
4531 | goto error; | |
4532 | } | |
27cdeb70 MX |
4533 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4534 | &root->state)) { | |
0305cd5f | 4535 | inode_sub_bytes(inode, item_end + 1 - new_size); |
9069218d | 4536 | } |
39279cc3 | 4537 | } |
179e29e4 | 4538 | delete: |
39279cc3 | 4539 | if (del_item) { |
85e21bac CM |
4540 | if (!pending_del_nr) { |
4541 | /* no pending yet, add ourselves */ | |
4542 | pending_del_slot = path->slots[0]; | |
4543 | pending_del_nr = 1; | |
4544 | } else if (pending_del_nr && | |
4545 | path->slots[0] + 1 == pending_del_slot) { | |
4546 | /* hop on the pending chunk */ | |
4547 | pending_del_nr++; | |
4548 | pending_del_slot = path->slots[0]; | |
4549 | } else { | |
d397712b | 4550 | BUG(); |
85e21bac | 4551 | } |
39279cc3 CM |
4552 | } else { |
4553 | break; | |
4554 | } | |
28f75a0e CM |
4555 | should_throttle = 0; |
4556 | ||
27cdeb70 MX |
4557 | if (found_extent && |
4558 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
4559 | root == root->fs_info->tree_root)) { | |
b9473439 | 4560 | btrfs_set_path_blocking(path); |
28ed1345 | 4561 | bytes_deleted += extent_num_bytes; |
39279cc3 | 4562 | ret = btrfs_free_extent(trans, root, extent_start, |
5d4f98a2 YZ |
4563 | extent_num_bytes, 0, |
4564 | btrfs_header_owner(leaf), | |
b06c4bf5 | 4565 | ino, extent_offset); |
39279cc3 | 4566 | BUG_ON(ret); |
1262133b | 4567 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
28ed1345 | 4568 | btrfs_async_run_delayed_refs(root, |
31b9655f | 4569 | trans->transid, |
28ed1345 | 4570 | trans->delayed_ref_updates * 2, 0); |
28f75a0e CM |
4571 | if (be_nice) { |
4572 | if (truncate_space_check(trans, root, | |
4573 | extent_num_bytes)) { | |
4574 | should_end = 1; | |
4575 | } | |
4576 | if (btrfs_should_throttle_delayed_refs(trans, | |
4577 | root)) { | |
4578 | should_throttle = 1; | |
4579 | } | |
4580 | } | |
39279cc3 | 4581 | } |
85e21bac | 4582 | |
8082510e YZ |
4583 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4584 | break; | |
4585 | ||
4586 | if (path->slots[0] == 0 || | |
1262133b | 4587 | path->slots[0] != pending_del_slot || |
28f75a0e | 4588 | should_throttle || should_end) { |
8082510e YZ |
4589 | if (pending_del_nr) { |
4590 | ret = btrfs_del_items(trans, root, path, | |
4591 | pending_del_slot, | |
4592 | pending_del_nr); | |
79787eaa JM |
4593 | if (ret) { |
4594 | btrfs_abort_transaction(trans, | |
4595 | root, ret); | |
4596 | goto error; | |
4597 | } | |
8082510e YZ |
4598 | pending_del_nr = 0; |
4599 | } | |
b3b4aa74 | 4600 | btrfs_release_path(path); |
28f75a0e | 4601 | if (should_throttle) { |
1262133b JB |
4602 | unsigned long updates = trans->delayed_ref_updates; |
4603 | if (updates) { | |
4604 | trans->delayed_ref_updates = 0; | |
4605 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4606 | if (ret && !err) | |
4607 | err = ret; | |
4608 | } | |
4609 | } | |
28f75a0e CM |
4610 | /* |
4611 | * if we failed to refill our space rsv, bail out | |
4612 | * and let the transaction restart | |
4613 | */ | |
4614 | if (should_end) { | |
4615 | err = -EAGAIN; | |
4616 | goto error; | |
4617 | } | |
85e21bac | 4618 | goto search_again; |
8082510e YZ |
4619 | } else { |
4620 | path->slots[0]--; | |
85e21bac | 4621 | } |
39279cc3 | 4622 | } |
8082510e | 4623 | out: |
85e21bac CM |
4624 | if (pending_del_nr) { |
4625 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4626 | pending_del_nr); | |
79787eaa JM |
4627 | if (ret) |
4628 | btrfs_abort_transaction(trans, root, ret); | |
85e21bac | 4629 | } |
79787eaa | 4630 | error: |
c1aa4575 | 4631 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
7f4f6e0a | 4632 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
28ed1345 | 4633 | |
39279cc3 | 4634 | btrfs_free_path(path); |
28ed1345 | 4635 | |
ee22184b | 4636 | if (be_nice && bytes_deleted > SZ_32M) { |
28ed1345 CM |
4637 | unsigned long updates = trans->delayed_ref_updates; |
4638 | if (updates) { | |
4639 | trans->delayed_ref_updates = 0; | |
4640 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4641 | if (ret && !err) | |
4642 | err = ret; | |
4643 | } | |
4644 | } | |
8082510e | 4645 | return err; |
39279cc3 CM |
4646 | } |
4647 | ||
4648 | /* | |
9703fefe | 4649 | * btrfs_truncate_block - read, zero a chunk and write a block |
2aaa6655 JB |
4650 | * @inode - inode that we're zeroing |
4651 | * @from - the offset to start zeroing | |
4652 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4653 | * offset | |
4654 | * @front - zero up to the offset instead of from the offset on | |
4655 | * | |
9703fefe | 4656 | * This will find the block for the "from" offset and cow the block and zero the |
2aaa6655 | 4657 | * part we want to zero. This is used with truncate and hole punching. |
39279cc3 | 4658 | */ |
9703fefe | 4659 | int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, |
2aaa6655 | 4660 | int front) |
39279cc3 | 4661 | { |
2aaa6655 | 4662 | struct address_space *mapping = inode->i_mapping; |
db94535d | 4663 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
4664 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4665 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4666 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 4667 | char *kaddr; |
db94535d | 4668 | u32 blocksize = root->sectorsize; |
09cbfeaf | 4669 | pgoff_t index = from >> PAGE_SHIFT; |
9703fefe | 4670 | unsigned offset = from & (blocksize - 1); |
39279cc3 | 4671 | struct page *page; |
3b16a4e3 | 4672 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4673 | int ret = 0; |
9703fefe CR |
4674 | u64 block_start; |
4675 | u64 block_end; | |
39279cc3 | 4676 | |
2aaa6655 JB |
4677 | if ((offset & (blocksize - 1)) == 0 && |
4678 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4679 | goto out; |
9703fefe | 4680 | |
7cf5b976 | 4681 | ret = btrfs_delalloc_reserve_space(inode, |
9703fefe | 4682 | round_down(from, blocksize), blocksize); |
5d5e103a JB |
4683 | if (ret) |
4684 | goto out; | |
39279cc3 | 4685 | |
211c17f5 | 4686 | again: |
3b16a4e3 | 4687 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4688 | if (!page) { |
7cf5b976 | 4689 | btrfs_delalloc_release_space(inode, |
9703fefe CR |
4690 | round_down(from, blocksize), |
4691 | blocksize); | |
ac6a2b36 | 4692 | ret = -ENOMEM; |
39279cc3 | 4693 | goto out; |
5d5e103a | 4694 | } |
e6dcd2dc | 4695 | |
9703fefe CR |
4696 | block_start = round_down(from, blocksize); |
4697 | block_end = block_start + blocksize - 1; | |
e6dcd2dc | 4698 | |
39279cc3 | 4699 | if (!PageUptodate(page)) { |
9ebefb18 | 4700 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4701 | lock_page(page); |
211c17f5 CM |
4702 | if (page->mapping != mapping) { |
4703 | unlock_page(page); | |
09cbfeaf | 4704 | put_page(page); |
211c17f5 CM |
4705 | goto again; |
4706 | } | |
39279cc3 CM |
4707 | if (!PageUptodate(page)) { |
4708 | ret = -EIO; | |
89642229 | 4709 | goto out_unlock; |
39279cc3 CM |
4710 | } |
4711 | } | |
211c17f5 | 4712 | wait_on_page_writeback(page); |
e6dcd2dc | 4713 | |
9703fefe | 4714 | lock_extent_bits(io_tree, block_start, block_end, &cached_state); |
e6dcd2dc CM |
4715 | set_page_extent_mapped(page); |
4716 | ||
9703fefe | 4717 | ordered = btrfs_lookup_ordered_extent(inode, block_start); |
e6dcd2dc | 4718 | if (ordered) { |
9703fefe | 4719 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4720 | &cached_state, GFP_NOFS); |
e6dcd2dc | 4721 | unlock_page(page); |
09cbfeaf | 4722 | put_page(page); |
eb84ae03 | 4723 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4724 | btrfs_put_ordered_extent(ordered); |
4725 | goto again; | |
4726 | } | |
4727 | ||
9703fefe | 4728 | clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end, |
9e8a4a8b LB |
4729 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4730 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4731 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4732 | |
9703fefe | 4733 | ret = btrfs_set_extent_delalloc(inode, block_start, block_end, |
2ac55d41 | 4734 | &cached_state); |
9ed74f2d | 4735 | if (ret) { |
9703fefe | 4736 | unlock_extent_cached(io_tree, block_start, block_end, |
2ac55d41 | 4737 | &cached_state, GFP_NOFS); |
9ed74f2d JB |
4738 | goto out_unlock; |
4739 | } | |
4740 | ||
9703fefe | 4741 | if (offset != blocksize) { |
2aaa6655 | 4742 | if (!len) |
9703fefe | 4743 | len = blocksize - offset; |
e6dcd2dc | 4744 | kaddr = kmap(page); |
2aaa6655 | 4745 | if (front) |
9703fefe CR |
4746 | memset(kaddr + (block_start - page_offset(page)), |
4747 | 0, offset); | |
2aaa6655 | 4748 | else |
9703fefe CR |
4749 | memset(kaddr + (block_start - page_offset(page)) + offset, |
4750 | 0, len); | |
e6dcd2dc CM |
4751 | flush_dcache_page(page); |
4752 | kunmap(page); | |
4753 | } | |
247e743c | 4754 | ClearPageChecked(page); |
e6dcd2dc | 4755 | set_page_dirty(page); |
9703fefe | 4756 | unlock_extent_cached(io_tree, block_start, block_end, &cached_state, |
2ac55d41 | 4757 | GFP_NOFS); |
39279cc3 | 4758 | |
89642229 | 4759 | out_unlock: |
5d5e103a | 4760 | if (ret) |
9703fefe CR |
4761 | btrfs_delalloc_release_space(inode, block_start, |
4762 | blocksize); | |
39279cc3 | 4763 | unlock_page(page); |
09cbfeaf | 4764 | put_page(page); |
39279cc3 CM |
4765 | out: |
4766 | return ret; | |
4767 | } | |
4768 | ||
16e7549f JB |
4769 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4770 | u64 offset, u64 len) | |
4771 | { | |
4772 | struct btrfs_trans_handle *trans; | |
4773 | int ret; | |
4774 | ||
4775 | /* | |
4776 | * Still need to make sure the inode looks like it's been updated so | |
4777 | * that any holes get logged if we fsync. | |
4778 | */ | |
4779 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { | |
4780 | BTRFS_I(inode)->last_trans = root->fs_info->generation; | |
4781 | BTRFS_I(inode)->last_sub_trans = root->log_transid; | |
4782 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4783 | return 0; | |
4784 | } | |
4785 | ||
4786 | /* | |
4787 | * 1 - for the one we're dropping | |
4788 | * 1 - for the one we're adding | |
4789 | * 1 - for updating the inode. | |
4790 | */ | |
4791 | trans = btrfs_start_transaction(root, 3); | |
4792 | if (IS_ERR(trans)) | |
4793 | return PTR_ERR(trans); | |
4794 | ||
4795 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4796 | if (ret) { | |
4797 | btrfs_abort_transaction(trans, root, ret); | |
4798 | btrfs_end_transaction(trans, root); | |
4799 | return ret; | |
4800 | } | |
4801 | ||
4802 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, | |
4803 | 0, 0, len, 0, len, 0, 0, 0); | |
4804 | if (ret) | |
4805 | btrfs_abort_transaction(trans, root, ret); | |
4806 | else | |
4807 | btrfs_update_inode(trans, root, inode); | |
4808 | btrfs_end_transaction(trans, root); | |
4809 | return ret; | |
4810 | } | |
4811 | ||
695a0d0d JB |
4812 | /* |
4813 | * This function puts in dummy file extents for the area we're creating a hole | |
4814 | * for. So if we are truncating this file to a larger size we need to insert | |
4815 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4816 | * the range between oldsize and size | |
4817 | */ | |
a41ad394 | 4818 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4819 | { |
9036c102 YZ |
4820 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4821 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4822 | struct extent_map *em = NULL; |
2ac55d41 | 4823 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4824 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
fda2832f QW |
4825 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
4826 | u64 block_end = ALIGN(size, root->sectorsize); | |
9036c102 YZ |
4827 | u64 last_byte; |
4828 | u64 cur_offset; | |
4829 | u64 hole_size; | |
9ed74f2d | 4830 | int err = 0; |
39279cc3 | 4831 | |
a71754fc | 4832 | /* |
9703fefe CR |
4833 | * If our size started in the middle of a block we need to zero out the |
4834 | * rest of the block before we expand the i_size, otherwise we could | |
a71754fc JB |
4835 | * expose stale data. |
4836 | */ | |
9703fefe | 4837 | err = btrfs_truncate_block(inode, oldsize, 0, 0); |
a71754fc JB |
4838 | if (err) |
4839 | return err; | |
4840 | ||
9036c102 YZ |
4841 | if (size <= hole_start) |
4842 | return 0; | |
4843 | ||
9036c102 YZ |
4844 | while (1) { |
4845 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4846 | |
ff13db41 | 4847 | lock_extent_bits(io_tree, hole_start, block_end - 1, |
d0082371 | 4848 | &cached_state); |
fa7c1494 MX |
4849 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
4850 | block_end - hole_start); | |
9036c102 YZ |
4851 | if (!ordered) |
4852 | break; | |
2ac55d41 JB |
4853 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4854 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4855 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4856 | btrfs_put_ordered_extent(ordered); |
4857 | } | |
39279cc3 | 4858 | |
9036c102 YZ |
4859 | cur_offset = hole_start; |
4860 | while (1) { | |
4861 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
4862 | block_end - cur_offset, 0); | |
79787eaa JM |
4863 | if (IS_ERR(em)) { |
4864 | err = PTR_ERR(em); | |
f2767956 | 4865 | em = NULL; |
79787eaa JM |
4866 | break; |
4867 | } | |
9036c102 | 4868 | last_byte = min(extent_map_end(em), block_end); |
fda2832f | 4869 | last_byte = ALIGN(last_byte , root->sectorsize); |
8082510e | 4870 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4871 | struct extent_map *hole_em; |
9036c102 | 4872 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4873 | |
16e7549f JB |
4874 | err = maybe_insert_hole(root, inode, cur_offset, |
4875 | hole_size); | |
4876 | if (err) | |
3893e33b | 4877 | break; |
5dc562c5 JB |
4878 | btrfs_drop_extent_cache(inode, cur_offset, |
4879 | cur_offset + hole_size - 1, 0); | |
4880 | hole_em = alloc_extent_map(); | |
4881 | if (!hole_em) { | |
4882 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4883 | &BTRFS_I(inode)->runtime_flags); | |
4884 | goto next; | |
4885 | } | |
4886 | hole_em->start = cur_offset; | |
4887 | hole_em->len = hole_size; | |
4888 | hole_em->orig_start = cur_offset; | |
8082510e | 4889 | |
5dc562c5 JB |
4890 | hole_em->block_start = EXTENT_MAP_HOLE; |
4891 | hole_em->block_len = 0; | |
b4939680 | 4892 | hole_em->orig_block_len = 0; |
cc95bef6 | 4893 | hole_em->ram_bytes = hole_size; |
5dc562c5 JB |
4894 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
4895 | hole_em->compress_type = BTRFS_COMPRESS_NONE; | |
16e7549f | 4896 | hole_em->generation = root->fs_info->generation; |
8082510e | 4897 | |
5dc562c5 JB |
4898 | while (1) { |
4899 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4900 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4901 | write_unlock(&em_tree->lock); |
4902 | if (err != -EEXIST) | |
4903 | break; | |
4904 | btrfs_drop_extent_cache(inode, cur_offset, | |
4905 | cur_offset + | |
4906 | hole_size - 1, 0); | |
4907 | } | |
4908 | free_extent_map(hole_em); | |
9036c102 | 4909 | } |
16e7549f | 4910 | next: |
9036c102 | 4911 | free_extent_map(em); |
a22285a6 | 4912 | em = NULL; |
9036c102 | 4913 | cur_offset = last_byte; |
8082510e | 4914 | if (cur_offset >= block_end) |
9036c102 YZ |
4915 | break; |
4916 | } | |
a22285a6 | 4917 | free_extent_map(em); |
2ac55d41 JB |
4918 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
4919 | GFP_NOFS); | |
9036c102 YZ |
4920 | return err; |
4921 | } | |
39279cc3 | 4922 | |
3972f260 | 4923 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4924 | { |
f4a2f4c5 MX |
4925 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4926 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4927 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4928 | loff_t newsize = attr->ia_size; |
4929 | int mask = attr->ia_valid; | |
8082510e YZ |
4930 | int ret; |
4931 | ||
3972f260 ES |
4932 | /* |
4933 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4934 | * special case where we need to update the times despite not having | |
4935 | * these flags set. For all other operations the VFS set these flags | |
4936 | * explicitly if it wants a timestamp update. | |
4937 | */ | |
dff6efc3 CH |
4938 | if (newsize != oldsize) { |
4939 | inode_inc_iversion(inode); | |
4940 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4941 | inode->i_ctime = inode->i_mtime = | |
4942 | current_fs_time(inode->i_sb); | |
4943 | } | |
3972f260 | 4944 | |
a41ad394 | 4945 | if (newsize > oldsize) { |
9ea24bbe FM |
4946 | /* |
4947 | * Don't do an expanding truncate while snapshoting is ongoing. | |
4948 | * This is to ensure the snapshot captures a fully consistent | |
4949 | * state of this file - if the snapshot captures this expanding | |
4950 | * truncation, it must capture all writes that happened before | |
4951 | * this truncation. | |
4952 | */ | |
0bc19f90 | 4953 | btrfs_wait_for_snapshot_creation(root); |
a41ad394 | 4954 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe FM |
4955 | if (ret) { |
4956 | btrfs_end_write_no_snapshoting(root); | |
8082510e | 4957 | return ret; |
9ea24bbe | 4958 | } |
8082510e | 4959 | |
f4a2f4c5 | 4960 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe FM |
4961 | if (IS_ERR(trans)) { |
4962 | btrfs_end_write_no_snapshoting(root); | |
f4a2f4c5 | 4963 | return PTR_ERR(trans); |
9ea24bbe | 4964 | } |
f4a2f4c5 MX |
4965 | |
4966 | i_size_write(inode, newsize); | |
4967 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
27772b68 | 4968 | pagecache_isize_extended(inode, oldsize, newsize); |
f4a2f4c5 | 4969 | ret = btrfs_update_inode(trans, root, inode); |
9ea24bbe | 4970 | btrfs_end_write_no_snapshoting(root); |
7ad85bb7 | 4971 | btrfs_end_transaction(trans, root); |
a41ad394 | 4972 | } else { |
8082510e | 4973 | |
a41ad394 JB |
4974 | /* |
4975 | * We're truncating a file that used to have good data down to | |
4976 | * zero. Make sure it gets into the ordered flush list so that | |
4977 | * any new writes get down to disk quickly. | |
4978 | */ | |
4979 | if (newsize == 0) | |
72ac3c0d JB |
4980 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
4981 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 4982 | |
f3fe820c JB |
4983 | /* |
4984 | * 1 for the orphan item we're going to add | |
4985 | * 1 for the orphan item deletion. | |
4986 | */ | |
4987 | trans = btrfs_start_transaction(root, 2); | |
4988 | if (IS_ERR(trans)) | |
4989 | return PTR_ERR(trans); | |
4990 | ||
4991 | /* | |
4992 | * We need to do this in case we fail at _any_ point during the | |
4993 | * actual truncate. Once we do the truncate_setsize we could | |
4994 | * invalidate pages which forces any outstanding ordered io to | |
4995 | * be instantly completed which will give us extents that need | |
4996 | * to be truncated. If we fail to get an orphan inode down we | |
4997 | * could have left over extents that were never meant to live, | |
01327610 | 4998 | * so we need to guarantee from this point on that everything |
f3fe820c JB |
4999 | * will be consistent. |
5000 | */ | |
5001 | ret = btrfs_orphan_add(trans, inode); | |
5002 | btrfs_end_transaction(trans, root); | |
5003 | if (ret) | |
5004 | return ret; | |
5005 | ||
a41ad394 JB |
5006 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
5007 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
5008 | |
5009 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
5010 | btrfs_inode_block_unlocked_dio(inode); | |
5011 | inode_dio_wait(inode); | |
5012 | btrfs_inode_resume_unlocked_dio(inode); | |
5013 | ||
a41ad394 | 5014 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
5015 | if (ret && inode->i_nlink) { |
5016 | int err; | |
5017 | ||
5018 | /* | |
5019 | * failed to truncate, disk_i_size is only adjusted down | |
5020 | * as we remove extents, so it should represent the true | |
5021 | * size of the inode, so reset the in memory size and | |
5022 | * delete our orphan entry. | |
5023 | */ | |
5024 | trans = btrfs_join_transaction(root); | |
5025 | if (IS_ERR(trans)) { | |
5026 | btrfs_orphan_del(NULL, inode); | |
5027 | return ret; | |
5028 | } | |
5029 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
5030 | err = btrfs_orphan_del(trans, inode); | |
5031 | if (err) | |
5032 | btrfs_abort_transaction(trans, root, err); | |
5033 | btrfs_end_transaction(trans, root); | |
5034 | } | |
8082510e YZ |
5035 | } |
5036 | ||
a41ad394 | 5037 | return ret; |
8082510e YZ |
5038 | } |
5039 | ||
9036c102 YZ |
5040 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5041 | { | |
2b0143b5 | 5042 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5043 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5044 | int err; |
39279cc3 | 5045 | |
b83cc969 LZ |
5046 | if (btrfs_root_readonly(root)) |
5047 | return -EROFS; | |
5048 | ||
9036c102 YZ |
5049 | err = inode_change_ok(inode, attr); |
5050 | if (err) | |
5051 | return err; | |
2bf5a725 | 5052 | |
5a3f23d5 | 5053 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5054 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5055 | if (err) |
5056 | return err; | |
39279cc3 | 5057 | } |
9036c102 | 5058 | |
1025774c CH |
5059 | if (attr->ia_valid) { |
5060 | setattr_copy(inode, attr); | |
0c4d2d95 | 5061 | inode_inc_iversion(inode); |
22c44fe6 | 5062 | err = btrfs_dirty_inode(inode); |
1025774c | 5063 | |
22c44fe6 | 5064 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5065 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5066 | } |
33268eaf | 5067 | |
39279cc3 CM |
5068 | return err; |
5069 | } | |
61295eb8 | 5070 | |
131e404a FDBM |
5071 | /* |
5072 | * While truncating the inode pages during eviction, we get the VFS calling | |
5073 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5074 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5075 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5076 | * extent_state structures over and over, wasting lots of time. | |
5077 | * | |
5078 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5079 | * those expensive operations on a per page basis and do only the ordered io | |
5080 | * finishing, while we release here the extent_map and extent_state structures, | |
5081 | * without the excessive merging and splitting. | |
5082 | */ | |
5083 | static void evict_inode_truncate_pages(struct inode *inode) | |
5084 | { | |
5085 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5086 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5087 | struct rb_node *node; | |
5088 | ||
5089 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5090 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5091 | |
5092 | write_lock(&map_tree->lock); | |
5093 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5094 | struct extent_map *em; | |
5095 | ||
5096 | node = rb_first(&map_tree->map); | |
5097 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5098 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5099 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5100 | remove_extent_mapping(map_tree, em); |
5101 | free_extent_map(em); | |
7064dd5c FM |
5102 | if (need_resched()) { |
5103 | write_unlock(&map_tree->lock); | |
5104 | cond_resched(); | |
5105 | write_lock(&map_tree->lock); | |
5106 | } | |
131e404a FDBM |
5107 | } |
5108 | write_unlock(&map_tree->lock); | |
5109 | ||
6ca07097 FM |
5110 | /* |
5111 | * Keep looping until we have no more ranges in the io tree. | |
5112 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5113 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5114 | * still in progress (unlocked the pages in the bio but did not yet | |
5115 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5116 | * ranges can still be locked and eviction started because before |
5117 | * submitting those bios, which are executed by a separate task (work | |
5118 | * queue kthread), inode references (inode->i_count) were not taken | |
5119 | * (which would be dropped in the end io callback of each bio). | |
5120 | * Therefore here we effectively end up waiting for those bios and | |
5121 | * anyone else holding locked ranges without having bumped the inode's | |
5122 | * reference count - if we don't do it, when they access the inode's | |
5123 | * io_tree to unlock a range it may be too late, leading to an | |
5124 | * use-after-free issue. | |
5125 | */ | |
131e404a FDBM |
5126 | spin_lock(&io_tree->lock); |
5127 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5128 | struct extent_state *state; | |
5129 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5130 | u64 start; |
5131 | u64 end; | |
131e404a FDBM |
5132 | |
5133 | node = rb_first(&io_tree->state); | |
5134 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5135 | start = state->start; |
5136 | end = state->end; | |
131e404a FDBM |
5137 | spin_unlock(&io_tree->lock); |
5138 | ||
ff13db41 | 5139 | lock_extent_bits(io_tree, start, end, &cached_state); |
b9d0b389 QW |
5140 | |
5141 | /* | |
5142 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5143 | * and its reserved space won't be freed by delayed_ref. | |
5144 | * So we need to free its reserved space here. | |
5145 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5146 | * | |
5147 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5148 | */ | |
5149 | if (state->state & EXTENT_DELALLOC) | |
5150 | btrfs_qgroup_free_data(inode, start, end - start + 1); | |
5151 | ||
6ca07097 | 5152 | clear_extent_bit(io_tree, start, end, |
131e404a FDBM |
5153 | EXTENT_LOCKED | EXTENT_DIRTY | |
5154 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5155 | EXTENT_DEFRAG, 1, 1, | |
5156 | &cached_state, GFP_NOFS); | |
131e404a | 5157 | |
7064dd5c | 5158 | cond_resched(); |
131e404a FDBM |
5159 | spin_lock(&io_tree->lock); |
5160 | } | |
5161 | spin_unlock(&io_tree->lock); | |
5162 | } | |
5163 | ||
bd555975 | 5164 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 CM |
5165 | { |
5166 | struct btrfs_trans_handle *trans; | |
5167 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5168 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5169 | int steal_from_global = 0; |
3d48d981 | 5170 | u64 min_size; |
39279cc3 CM |
5171 | int ret; |
5172 | ||
1abe9b8a | 5173 | trace_btrfs_inode_evict(inode); |
5174 | ||
3d48d981 NB |
5175 | if (!root) { |
5176 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
5177 | return; | |
5178 | } | |
5179 | ||
5180 | min_size = btrfs_calc_trunc_metadata_size(root, 1); | |
5181 | ||
131e404a FDBM |
5182 | evict_inode_truncate_pages(inode); |
5183 | ||
69e9c6c6 SB |
5184 | if (inode->i_nlink && |
5185 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5186 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
5187 | btrfs_is_free_space_inode(inode))) | |
bd555975 AV |
5188 | goto no_delete; |
5189 | ||
39279cc3 | 5190 | if (is_bad_inode(inode)) { |
7b128766 | 5191 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
5192 | goto no_delete; |
5193 | } | |
bd555975 | 5194 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
a30e577c JM |
5195 | if (!special_file(inode->i_mode)) |
5196 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5f39d397 | 5197 | |
f612496b MX |
5198 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
5199 | ||
c71bf099 | 5200 | if (root->fs_info->log_root_recovering) { |
6bf02314 | 5201 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5202 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5203 | goto no_delete; |
5204 | } | |
5205 | ||
76dda93c | 5206 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5207 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5208 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5209 | goto no_delete; |
5210 | } | |
5211 | ||
0e8c36a9 MX |
5212 | ret = btrfs_commit_inode_delayed_inode(inode); |
5213 | if (ret) { | |
5214 | btrfs_orphan_del(NULL, inode); | |
5215 | goto no_delete; | |
5216 | } | |
5217 | ||
66d8f3dd | 5218 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
4289a667 JB |
5219 | if (!rsv) { |
5220 | btrfs_orphan_del(NULL, inode); | |
5221 | goto no_delete; | |
5222 | } | |
4a338542 | 5223 | rsv->size = min_size; |
ca7e70f5 | 5224 | rsv->failfast = 1; |
726c35fa | 5225 | global_rsv = &root->fs_info->global_block_rsv; |
4289a667 | 5226 | |
dbe674a9 | 5227 | btrfs_i_size_write(inode, 0); |
5f39d397 | 5228 | |
4289a667 | 5229 | /* |
8407aa46 MX |
5230 | * This is a bit simpler than btrfs_truncate since we've already |
5231 | * reserved our space for our orphan item in the unlink, so we just | |
5232 | * need to reserve some slack space in case we add bytes and update | |
5233 | * inode item when doing the truncate. | |
4289a667 | 5234 | */ |
8082510e | 5235 | while (1) { |
08e007d2 MX |
5236 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5237 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5238 | |
5239 | /* | |
5240 | * Try and steal from the global reserve since we will | |
5241 | * likely not use this space anyway, we want to try as | |
5242 | * hard as possible to get this to work. | |
5243 | */ | |
5244 | if (ret) | |
3bce876f JB |
5245 | steal_from_global++; |
5246 | else | |
5247 | steal_from_global = 0; | |
5248 | ret = 0; | |
d68fc57b | 5249 | |
3bce876f JB |
5250 | /* |
5251 | * steal_from_global == 0: we reserved stuff, hooray! | |
5252 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5253 | * steal_from_global == 2: we've committed, still not a lot of | |
5254 | * room but maybe we'll have room in the global reserve this | |
5255 | * time. | |
5256 | * steal_from_global == 3: abandon all hope! | |
5257 | */ | |
5258 | if (steal_from_global > 2) { | |
c2cf52eb SK |
5259 | btrfs_warn(root->fs_info, |
5260 | "Could not get space for a delete, will truncate on mount %d", | |
5261 | ret); | |
4289a667 JB |
5262 | btrfs_orphan_del(NULL, inode); |
5263 | btrfs_free_block_rsv(root, rsv); | |
5264 | goto no_delete; | |
d68fc57b | 5265 | } |
7b128766 | 5266 | |
0e8c36a9 | 5267 | trans = btrfs_join_transaction(root); |
4289a667 JB |
5268 | if (IS_ERR(trans)) { |
5269 | btrfs_orphan_del(NULL, inode); | |
5270 | btrfs_free_block_rsv(root, rsv); | |
5271 | goto no_delete; | |
d68fc57b | 5272 | } |
7b128766 | 5273 | |
3bce876f | 5274 | /* |
01327610 | 5275 | * We can't just steal from the global reserve, we need to make |
3bce876f JB |
5276 | * sure there is room to do it, if not we need to commit and try |
5277 | * again. | |
5278 | */ | |
5279 | if (steal_from_global) { | |
5280 | if (!btrfs_check_space_for_delayed_refs(trans, root)) | |
5281 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, | |
25d609f8 | 5282 | min_size, 0); |
3bce876f JB |
5283 | else |
5284 | ret = -ENOSPC; | |
5285 | } | |
5286 | ||
5287 | /* | |
5288 | * Couldn't steal from the global reserve, we have too much | |
5289 | * pending stuff built up, commit the transaction and try it | |
5290 | * again. | |
5291 | */ | |
5292 | if (ret) { | |
5293 | ret = btrfs_commit_transaction(trans, root); | |
5294 | if (ret) { | |
5295 | btrfs_orphan_del(NULL, inode); | |
5296 | btrfs_free_block_rsv(root, rsv); | |
5297 | goto no_delete; | |
5298 | } | |
5299 | continue; | |
5300 | } else { | |
5301 | steal_from_global = 0; | |
5302 | } | |
5303 | ||
4289a667 JB |
5304 | trans->block_rsv = rsv; |
5305 | ||
d68fc57b | 5306 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5307 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5308 | break; |
85e21bac | 5309 | |
8407aa46 | 5310 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e YZ |
5311 | btrfs_end_transaction(trans, root); |
5312 | trans = NULL; | |
b53d3f5d | 5313 | btrfs_btree_balance_dirty(root); |
8082510e | 5314 | } |
5f39d397 | 5315 | |
4289a667 JB |
5316 | btrfs_free_block_rsv(root, rsv); |
5317 | ||
4ef31a45 JB |
5318 | /* |
5319 | * Errors here aren't a big deal, it just means we leave orphan items | |
5320 | * in the tree. They will be cleaned up on the next mount. | |
5321 | */ | |
8082510e | 5322 | if (ret == 0) { |
4289a667 | 5323 | trans->block_rsv = root->orphan_block_rsv; |
4ef31a45 JB |
5324 | btrfs_orphan_del(trans, inode); |
5325 | } else { | |
5326 | btrfs_orphan_del(NULL, inode); | |
8082510e | 5327 | } |
54aa1f4d | 5328 | |
4289a667 | 5329 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
581bb050 LZ |
5330 | if (!(root == root->fs_info->tree_root || |
5331 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) | |
33345d01 | 5332 | btrfs_return_ino(root, btrfs_ino(inode)); |
581bb050 | 5333 | |
54aa1f4d | 5334 | btrfs_end_transaction(trans, root); |
b53d3f5d | 5335 | btrfs_btree_balance_dirty(root); |
39279cc3 | 5336 | no_delete: |
89042e5a | 5337 | btrfs_remove_delayed_node(inode); |
dbd5768f | 5338 | clear_inode(inode); |
39279cc3 CM |
5339 | } |
5340 | ||
5341 | /* | |
5342 | * this returns the key found in the dir entry in the location pointer. | |
5343 | * If no dir entries were found, location->objectid is 0. | |
5344 | */ | |
5345 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5346 | struct btrfs_key *location) | |
5347 | { | |
5348 | const char *name = dentry->d_name.name; | |
5349 | int namelen = dentry->d_name.len; | |
5350 | struct btrfs_dir_item *di; | |
5351 | struct btrfs_path *path; | |
5352 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5353 | int ret = 0; |
39279cc3 CM |
5354 | |
5355 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5356 | if (!path) |
5357 | return -ENOMEM; | |
3954401f | 5358 | |
33345d01 | 5359 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
39279cc3 | 5360 | namelen, 0); |
0d9f7f3e Y |
5361 | if (IS_ERR(di)) |
5362 | ret = PTR_ERR(di); | |
d397712b | 5363 | |
c704005d | 5364 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5365 | goto out_err; |
d397712b | 5366 | |
5f39d397 | 5367 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5368 | out: |
39279cc3 CM |
5369 | btrfs_free_path(path); |
5370 | return ret; | |
3954401f CM |
5371 | out_err: |
5372 | location->objectid = 0; | |
5373 | goto out; | |
39279cc3 CM |
5374 | } |
5375 | ||
5376 | /* | |
5377 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5378 | * needs to be changed to reflect the root directory of the tree root. This | |
5379 | * is kind of like crossing a mount point. | |
5380 | */ | |
5381 | static int fixup_tree_root_location(struct btrfs_root *root, | |
4df27c4d YZ |
5382 | struct inode *dir, |
5383 | struct dentry *dentry, | |
5384 | struct btrfs_key *location, | |
5385 | struct btrfs_root **sub_root) | |
39279cc3 | 5386 | { |
4df27c4d YZ |
5387 | struct btrfs_path *path; |
5388 | struct btrfs_root *new_root; | |
5389 | struct btrfs_root_ref *ref; | |
5390 | struct extent_buffer *leaf; | |
1d4c08e0 | 5391 | struct btrfs_key key; |
4df27c4d YZ |
5392 | int ret; |
5393 | int err = 0; | |
39279cc3 | 5394 | |
4df27c4d YZ |
5395 | path = btrfs_alloc_path(); |
5396 | if (!path) { | |
5397 | err = -ENOMEM; | |
5398 | goto out; | |
5399 | } | |
39279cc3 | 5400 | |
4df27c4d | 5401 | err = -ENOENT; |
1d4c08e0 DS |
5402 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5403 | key.type = BTRFS_ROOT_REF_KEY; | |
5404 | key.offset = location->objectid; | |
5405 | ||
5406 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, | |
5407 | 0, 0); | |
4df27c4d YZ |
5408 | if (ret) { |
5409 | if (ret < 0) | |
5410 | err = ret; | |
5411 | goto out; | |
5412 | } | |
39279cc3 | 5413 | |
4df27c4d YZ |
5414 | leaf = path->nodes[0]; |
5415 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
33345d01 | 5416 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
4df27c4d YZ |
5417 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5418 | goto out; | |
39279cc3 | 5419 | |
4df27c4d YZ |
5420 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5421 | (unsigned long)(ref + 1), | |
5422 | dentry->d_name.len); | |
5423 | if (ret) | |
5424 | goto out; | |
5425 | ||
b3b4aa74 | 5426 | btrfs_release_path(path); |
4df27c4d YZ |
5427 | |
5428 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); | |
5429 | if (IS_ERR(new_root)) { | |
5430 | err = PTR_ERR(new_root); | |
5431 | goto out; | |
5432 | } | |
5433 | ||
4df27c4d YZ |
5434 | *sub_root = new_root; |
5435 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5436 | location->type = BTRFS_INODE_ITEM_KEY; | |
5437 | location->offset = 0; | |
5438 | err = 0; | |
5439 | out: | |
5440 | btrfs_free_path(path); | |
5441 | return err; | |
39279cc3 CM |
5442 | } |
5443 | ||
5d4f98a2 YZ |
5444 | static void inode_tree_add(struct inode *inode) |
5445 | { | |
5446 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5447 | struct btrfs_inode *entry; | |
03e860bd FNP |
5448 | struct rb_node **p; |
5449 | struct rb_node *parent; | |
cef21937 | 5450 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
33345d01 | 5451 | u64 ino = btrfs_ino(inode); |
5d4f98a2 | 5452 | |
1d3382cb | 5453 | if (inode_unhashed(inode)) |
76dda93c | 5454 | return; |
e1409cef | 5455 | parent = NULL; |
5d4f98a2 | 5456 | spin_lock(&root->inode_lock); |
e1409cef | 5457 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5458 | while (*p) { |
5459 | parent = *p; | |
5460 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5461 | ||
33345d01 | 5462 | if (ino < btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5463 | p = &parent->rb_left; |
33345d01 | 5464 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5465 | p = &parent->rb_right; |
5d4f98a2 YZ |
5466 | else { |
5467 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5468 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5469 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
5470 | RB_CLEAR_NODE(parent); |
5471 | spin_unlock(&root->inode_lock); | |
cef21937 | 5472 | return; |
5d4f98a2 YZ |
5473 | } |
5474 | } | |
cef21937 FDBM |
5475 | rb_link_node(new, parent, p); |
5476 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5477 | spin_unlock(&root->inode_lock); |
5478 | } | |
5479 | ||
5480 | static void inode_tree_del(struct inode *inode) | |
5481 | { | |
5482 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
76dda93c | 5483 | int empty = 0; |
5d4f98a2 | 5484 | |
03e860bd | 5485 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5486 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5487 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5488 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5489 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5490 | } |
03e860bd | 5491 | spin_unlock(&root->inode_lock); |
76dda93c | 5492 | |
69e9c6c6 | 5493 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5494 | synchronize_srcu(&root->fs_info->subvol_srcu); |
5495 | spin_lock(&root->inode_lock); | |
5496 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5497 | spin_unlock(&root->inode_lock); | |
5498 | if (empty) | |
5499 | btrfs_add_dead_root(root); | |
5500 | } | |
5501 | } | |
5502 | ||
143bede5 | 5503 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c YZ |
5504 | { |
5505 | struct rb_node *node; | |
5506 | struct rb_node *prev; | |
5507 | struct btrfs_inode *entry; | |
5508 | struct inode *inode; | |
5509 | u64 objectid = 0; | |
5510 | ||
7813b3db LB |
5511 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
5512 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
76dda93c YZ |
5513 | |
5514 | spin_lock(&root->inode_lock); | |
5515 | again: | |
5516 | node = root->inode_tree.rb_node; | |
5517 | prev = NULL; | |
5518 | while (node) { | |
5519 | prev = node; | |
5520 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5521 | ||
33345d01 | 5522 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
76dda93c | 5523 | node = node->rb_left; |
33345d01 | 5524 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
76dda93c YZ |
5525 | node = node->rb_right; |
5526 | else | |
5527 | break; | |
5528 | } | |
5529 | if (!node) { | |
5530 | while (prev) { | |
5531 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
33345d01 | 5532 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
76dda93c YZ |
5533 | node = prev; |
5534 | break; | |
5535 | } | |
5536 | prev = rb_next(prev); | |
5537 | } | |
5538 | } | |
5539 | while (node) { | |
5540 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
33345d01 | 5541 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
76dda93c YZ |
5542 | inode = igrab(&entry->vfs_inode); |
5543 | if (inode) { | |
5544 | spin_unlock(&root->inode_lock); | |
5545 | if (atomic_read(&inode->i_count) > 1) | |
5546 | d_prune_aliases(inode); | |
5547 | /* | |
45321ac5 | 5548 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5549 | * the inode cache when its usage count |
5550 | * hits zero. | |
5551 | */ | |
5552 | iput(inode); | |
5553 | cond_resched(); | |
5554 | spin_lock(&root->inode_lock); | |
5555 | goto again; | |
5556 | } | |
5557 | ||
5558 | if (cond_resched_lock(&root->inode_lock)) | |
5559 | goto again; | |
5560 | ||
5561 | node = rb_next(node); | |
5562 | } | |
5563 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5564 | } |
5565 | ||
e02119d5 CM |
5566 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5567 | { | |
5568 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5569 | inode->i_ino = args->location->objectid; |
5570 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5571 | sizeof(*args->location)); | |
e02119d5 | 5572 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5573 | return 0; |
5574 | } | |
5575 | ||
5576 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5577 | { | |
5578 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5579 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5580 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5581 | } |
5582 | ||
5d4f98a2 | 5583 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5584 | struct btrfs_key *location, |
5d4f98a2 | 5585 | struct btrfs_root *root) |
39279cc3 CM |
5586 | { |
5587 | struct inode *inode; | |
5588 | struct btrfs_iget_args args; | |
90d3e592 | 5589 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5590 | |
90d3e592 | 5591 | args.location = location; |
39279cc3 CM |
5592 | args.root = root; |
5593 | ||
778ba82b | 5594 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5595 | btrfs_init_locked_inode, |
5596 | (void *)&args); | |
5597 | return inode; | |
5598 | } | |
5599 | ||
1a54ef8c BR |
5600 | /* Get an inode object given its location and corresponding root. |
5601 | * Returns in *is_new if the inode was read from disk | |
5602 | */ | |
5603 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5604 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5605 | { |
5606 | struct inode *inode; | |
5607 | ||
90d3e592 | 5608 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5609 | if (!inode) |
5d4f98a2 | 5610 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5611 | |
5612 | if (inode->i_state & I_NEW) { | |
1a54ef8c | 5613 | btrfs_read_locked_inode(inode); |
1748f843 MF |
5614 | if (!is_bad_inode(inode)) { |
5615 | inode_tree_add(inode); | |
5616 | unlock_new_inode(inode); | |
5617 | if (new) | |
5618 | *new = 1; | |
5619 | } else { | |
e0b6d65b ST |
5620 | unlock_new_inode(inode); |
5621 | iput(inode); | |
5622 | inode = ERR_PTR(-ESTALE); | |
1748f843 MF |
5623 | } |
5624 | } | |
5625 | ||
1a54ef8c BR |
5626 | return inode; |
5627 | } | |
5628 | ||
4df27c4d YZ |
5629 | static struct inode *new_simple_dir(struct super_block *s, |
5630 | struct btrfs_key *key, | |
5631 | struct btrfs_root *root) | |
5632 | { | |
5633 | struct inode *inode = new_inode(s); | |
5634 | ||
5635 | if (!inode) | |
5636 | return ERR_PTR(-ENOMEM); | |
5637 | ||
4df27c4d YZ |
5638 | BTRFS_I(inode)->root = root; |
5639 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5640 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5641 | |
5642 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5643 | inode->i_op = &btrfs_dir_ro_inode_operations; |
4df27c4d YZ |
5644 | inode->i_fop = &simple_dir_operations; |
5645 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
04b285f3 | 5646 | inode->i_mtime = current_fs_time(inode->i_sb); |
9cc97d64 | 5647 | inode->i_atime = inode->i_mtime; |
5648 | inode->i_ctime = inode->i_mtime; | |
5649 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5650 | |
5651 | return inode; | |
5652 | } | |
5653 | ||
3de4586c | 5654 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5655 | { |
d397712b | 5656 | struct inode *inode; |
4df27c4d | 5657 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5658 | struct btrfs_root *sub_root = root; |
5659 | struct btrfs_key location; | |
76dda93c | 5660 | int index; |
b4aff1f8 | 5661 | int ret = 0; |
39279cc3 CM |
5662 | |
5663 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5664 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5665 | |
39e3c955 | 5666 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5667 | if (ret < 0) |
5668 | return ERR_PTR(ret); | |
5f39d397 | 5669 | |
4df27c4d | 5670 | if (location.objectid == 0) |
5662344b | 5671 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5672 | |
5673 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5674 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5675 | return inode; |
5676 | } | |
5677 | ||
5678 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5679 | ||
76dda93c | 5680 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
4df27c4d YZ |
5681 | ret = fixup_tree_root_location(root, dir, dentry, |
5682 | &location, &sub_root); | |
5683 | if (ret < 0) { | |
5684 | if (ret != -ENOENT) | |
5685 | inode = ERR_PTR(ret); | |
5686 | else | |
5687 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5688 | } else { | |
73f73415 | 5689 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5690 | } |
76dda93c YZ |
5691 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
5692 | ||
34d19bad | 5693 | if (!IS_ERR(inode) && root != sub_root) { |
c71bf099 YZ |
5694 | down_read(&root->fs_info->cleanup_work_sem); |
5695 | if (!(inode->i_sb->s_flags & MS_RDONLY)) | |
66b4ffd1 | 5696 | ret = btrfs_orphan_cleanup(sub_root); |
c71bf099 | 5697 | up_read(&root->fs_info->cleanup_work_sem); |
01cd3367 JB |
5698 | if (ret) { |
5699 | iput(inode); | |
66b4ffd1 | 5700 | inode = ERR_PTR(ret); |
01cd3367 | 5701 | } |
c71bf099 YZ |
5702 | } |
5703 | ||
3de4586c CM |
5704 | return inode; |
5705 | } | |
5706 | ||
fe15ce44 | 5707 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5708 | { |
5709 | struct btrfs_root *root; | |
2b0143b5 | 5710 | struct inode *inode = d_inode(dentry); |
76dda93c | 5711 | |
848cce0d | 5712 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5713 | inode = d_inode(dentry->d_parent); |
76dda93c | 5714 | |
848cce0d LZ |
5715 | if (inode) { |
5716 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5717 | if (btrfs_root_refs(&root->root_item) == 0) |
5718 | return 1; | |
848cce0d LZ |
5719 | |
5720 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) | |
5721 | return 1; | |
efefb143 | 5722 | } |
76dda93c YZ |
5723 | return 0; |
5724 | } | |
5725 | ||
b4aff1f8 JB |
5726 | static void btrfs_dentry_release(struct dentry *dentry) |
5727 | { | |
944a4515 | 5728 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5729 | } |
5730 | ||
3de4586c | 5731 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5732 | unsigned int flags) |
3de4586c | 5733 | { |
5662344b | 5734 | struct inode *inode; |
a66e7cc6 | 5735 | |
5662344b TI |
5736 | inode = btrfs_lookup_dentry(dir, dentry); |
5737 | if (IS_ERR(inode)) { | |
5738 | if (PTR_ERR(inode) == -ENOENT) | |
5739 | inode = NULL; | |
5740 | else | |
5741 | return ERR_CAST(inode); | |
5742 | } | |
5743 | ||
41d28bca | 5744 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5745 | } |
5746 | ||
16cdcec7 | 5747 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5748 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5749 | }; | |
5750 | ||
9cdda8d3 | 5751 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5752 | { |
9cdda8d3 | 5753 | struct inode *inode = file_inode(file); |
39279cc3 CM |
5754 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5755 | struct btrfs_item *item; | |
5756 | struct btrfs_dir_item *di; | |
5757 | struct btrfs_key key; | |
5f39d397 | 5758 | struct btrfs_key found_key; |
39279cc3 | 5759 | struct btrfs_path *path; |
16cdcec7 MX |
5760 | struct list_head ins_list; |
5761 | struct list_head del_list; | |
39279cc3 | 5762 | int ret; |
5f39d397 | 5763 | struct extent_buffer *leaf; |
39279cc3 | 5764 | int slot; |
39279cc3 CM |
5765 | unsigned char d_type; |
5766 | int over = 0; | |
5767 | u32 di_cur; | |
5768 | u32 di_total; | |
5769 | u32 di_len; | |
5770 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
5771 | char tmp_name[32]; |
5772 | char *name_ptr; | |
5773 | int name_len; | |
9cdda8d3 | 5774 | int is_curr = 0; /* ctx->pos points to the current index? */ |
bc4ef759 | 5775 | bool emitted; |
02dbfc99 | 5776 | bool put = false; |
39279cc3 CM |
5777 | |
5778 | /* FIXME, use a real flag for deciding about the key type */ | |
5779 | if (root->fs_info->tree_root == root) | |
5780 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 5781 | |
9cdda8d3 AV |
5782 | if (!dir_emit_dots(file, ctx)) |
5783 | return 0; | |
5784 | ||
49593bfa | 5785 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5786 | if (!path) |
5787 | return -ENOMEM; | |
ff5714cc | 5788 | |
e4058b54 | 5789 | path->reada = READA_FORWARD; |
49593bfa | 5790 | |
16cdcec7 MX |
5791 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5792 | INIT_LIST_HEAD(&ins_list); | |
5793 | INIT_LIST_HEAD(&del_list); | |
02dbfc99 OS |
5794 | put = btrfs_readdir_get_delayed_items(inode, &ins_list, |
5795 | &del_list); | |
16cdcec7 MX |
5796 | } |
5797 | ||
962a298f | 5798 | key.type = key_type; |
9cdda8d3 | 5799 | key.offset = ctx->pos; |
33345d01 | 5800 | key.objectid = btrfs_ino(inode); |
5f39d397 | 5801 | |
39279cc3 CM |
5802 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5803 | if (ret < 0) | |
5804 | goto err; | |
49593bfa | 5805 | |
bc4ef759 | 5806 | emitted = false; |
49593bfa | 5807 | while (1) { |
5f39d397 | 5808 | leaf = path->nodes[0]; |
39279cc3 | 5809 | slot = path->slots[0]; |
b9e03af0 LZ |
5810 | if (slot >= btrfs_header_nritems(leaf)) { |
5811 | ret = btrfs_next_leaf(root, path); | |
5812 | if (ret < 0) | |
5813 | goto err; | |
5814 | else if (ret > 0) | |
5815 | break; | |
5816 | continue; | |
39279cc3 | 5817 | } |
3de4586c | 5818 | |
dd3cc16b | 5819 | item = btrfs_item_nr(slot); |
5f39d397 CM |
5820 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5821 | ||
5822 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5823 | break; |
962a298f | 5824 | if (found_key.type != key_type) |
39279cc3 | 5825 | break; |
9cdda8d3 | 5826 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5827 | goto next; |
16cdcec7 MX |
5828 | if (key_type == BTRFS_DIR_INDEX_KEY && |
5829 | btrfs_should_delete_dir_index(&del_list, | |
5830 | found_key.offset)) | |
5831 | goto next; | |
5f39d397 | 5832 | |
9cdda8d3 | 5833 | ctx->pos = found_key.offset; |
16cdcec7 | 5834 | is_curr = 1; |
49593bfa | 5835 | |
39279cc3 CM |
5836 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
5837 | di_cur = 0; | |
5f39d397 | 5838 | di_total = btrfs_item_size(leaf, item); |
49593bfa DW |
5839 | |
5840 | while (di_cur < di_total) { | |
5f39d397 CM |
5841 | struct btrfs_key location; |
5842 | ||
22a94d44 JB |
5843 | if (verify_dir_item(root, leaf, di)) |
5844 | break; | |
5845 | ||
5f39d397 | 5846 | name_len = btrfs_dir_name_len(leaf, di); |
49593bfa | 5847 | if (name_len <= sizeof(tmp_name)) { |
5f39d397 CM |
5848 | name_ptr = tmp_name; |
5849 | } else { | |
49e350a4 | 5850 | name_ptr = kmalloc(name_len, GFP_KERNEL); |
49593bfa DW |
5851 | if (!name_ptr) { |
5852 | ret = -ENOMEM; | |
5853 | goto err; | |
5854 | } | |
5f39d397 CM |
5855 | } |
5856 | read_extent_buffer(leaf, name_ptr, | |
5857 | (unsigned long)(di + 1), name_len); | |
5858 | ||
5859 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
5860 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
3de4586c | 5861 | |
fede766f | 5862 | |
3de4586c | 5863 | /* is this a reference to our own snapshot? If so |
8c9c2bf7 AJ |
5864 | * skip it. |
5865 | * | |
5866 | * In contrast to old kernels, we insert the snapshot's | |
5867 | * dir item and dir index after it has been created, so | |
5868 | * we won't find a reference to our own snapshot. We | |
5869 | * still keep the following code for backward | |
5870 | * compatibility. | |
3de4586c CM |
5871 | */ |
5872 | if (location.type == BTRFS_ROOT_ITEM_KEY && | |
5873 | location.objectid == root->root_key.objectid) { | |
5874 | over = 0; | |
5875 | goto skip; | |
5876 | } | |
9cdda8d3 AV |
5877 | over = !dir_emit(ctx, name_ptr, name_len, |
5878 | location.objectid, d_type); | |
5f39d397 | 5879 | |
3de4586c | 5880 | skip: |
5f39d397 CM |
5881 | if (name_ptr != tmp_name) |
5882 | kfree(name_ptr); | |
5883 | ||
39279cc3 CM |
5884 | if (over) |
5885 | goto nopos; | |
bc4ef759 | 5886 | emitted = true; |
5103e947 | 5887 | di_len = btrfs_dir_name_len(leaf, di) + |
49593bfa | 5888 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
39279cc3 CM |
5889 | di_cur += di_len; |
5890 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
5891 | } | |
b9e03af0 LZ |
5892 | next: |
5893 | path->slots[0]++; | |
39279cc3 | 5894 | } |
49593bfa | 5895 | |
16cdcec7 MX |
5896 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5897 | if (is_curr) | |
9cdda8d3 | 5898 | ctx->pos++; |
bc4ef759 | 5899 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted); |
16cdcec7 MX |
5900 | if (ret) |
5901 | goto nopos; | |
5902 | } | |
5903 | ||
bc4ef759 DS |
5904 | /* |
5905 | * If we haven't emitted any dir entry, we must not touch ctx->pos as | |
5906 | * it was was set to the termination value in previous call. We assume | |
5907 | * that "." and ".." were emitted if we reach this point and set the | |
5908 | * termination value as well for an empty directory. | |
5909 | */ | |
5910 | if (ctx->pos > 2 && !emitted) | |
5911 | goto nopos; | |
5912 | ||
49593bfa | 5913 | /* Reached end of directory/root. Bump pos past the last item. */ |
db62efbb ZB |
5914 | ctx->pos++; |
5915 | ||
5916 | /* | |
5917 | * Stop new entries from being returned after we return the last | |
5918 | * entry. | |
5919 | * | |
5920 | * New directory entries are assigned a strictly increasing | |
5921 | * offset. This means that new entries created during readdir | |
5922 | * are *guaranteed* to be seen in the future by that readdir. | |
5923 | * This has broken buggy programs which operate on names as | |
5924 | * they're returned by readdir. Until we re-use freed offsets | |
5925 | * we have this hack to stop new entries from being returned | |
5926 | * under the assumption that they'll never reach this huge | |
5927 | * offset. | |
5928 | * | |
5929 | * This is being careful not to overflow 32bit loff_t unless the | |
5930 | * last entry requires it because doing so has broken 32bit apps | |
5931 | * in the past. | |
5932 | */ | |
5933 | if (key_type == BTRFS_DIR_INDEX_KEY) { | |
5934 | if (ctx->pos >= INT_MAX) | |
5935 | ctx->pos = LLONG_MAX; | |
5936 | else | |
5937 | ctx->pos = INT_MAX; | |
5938 | } | |
39279cc3 CM |
5939 | nopos: |
5940 | ret = 0; | |
5941 | err: | |
02dbfc99 OS |
5942 | if (put) |
5943 | btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list); | |
39279cc3 | 5944 | btrfs_free_path(path); |
39279cc3 CM |
5945 | return ret; |
5946 | } | |
5947 | ||
a9185b41 | 5948 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
5949 | { |
5950 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5951 | struct btrfs_trans_handle *trans; | |
5952 | int ret = 0; | |
0af3d00b | 5953 | bool nolock = false; |
39279cc3 | 5954 | |
72ac3c0d | 5955 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
5956 | return 0; |
5957 | ||
83eea1f1 | 5958 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
82d5902d | 5959 | nolock = true; |
0af3d00b | 5960 | |
a9185b41 | 5961 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 5962 | if (nolock) |
7a7eaa40 | 5963 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 5964 | else |
7a7eaa40 | 5965 | trans = btrfs_join_transaction(root); |
3612b495 TI |
5966 | if (IS_ERR(trans)) |
5967 | return PTR_ERR(trans); | |
a698d075 | 5968 | ret = btrfs_commit_transaction(trans, root); |
39279cc3 CM |
5969 | } |
5970 | return ret; | |
5971 | } | |
5972 | ||
5973 | /* | |
54aa1f4d | 5974 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5975 | * inode changes. But, it is most likely to find the inode in cache. |
5976 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5977 | * to keep or drop this code. | |
5978 | */ | |
48a3b636 | 5979 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 CM |
5980 | { |
5981 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5982 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5983 | int ret; |
5984 | ||
72ac3c0d | 5985 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5986 | return 0; |
39279cc3 | 5987 | |
7a7eaa40 | 5988 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5989 | if (IS_ERR(trans)) |
5990 | return PTR_ERR(trans); | |
8929ecfa YZ |
5991 | |
5992 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
5993 | if (ret && ret == -ENOSPC) { |
5994 | /* whoops, lets try again with the full transaction */ | |
5995 | btrfs_end_transaction(trans, root); | |
5996 | trans = btrfs_start_transaction(root, 1); | |
22c44fe6 JB |
5997 | if (IS_ERR(trans)) |
5998 | return PTR_ERR(trans); | |
8929ecfa | 5999 | |
94b60442 | 6000 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 6001 | } |
39279cc3 | 6002 | btrfs_end_transaction(trans, root); |
16cdcec7 MX |
6003 | if (BTRFS_I(inode)->delayed_node) |
6004 | btrfs_balance_delayed_items(root); | |
22c44fe6 JB |
6005 | |
6006 | return ret; | |
6007 | } | |
6008 | ||
6009 | /* | |
6010 | * This is a copy of file_update_time. We need this so we can return error on | |
6011 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
6012 | */ | |
e41f941a JB |
6013 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
6014 | int flags) | |
22c44fe6 | 6015 | { |
2bc55652 AB |
6016 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6017 | ||
6018 | if (btrfs_root_readonly(root)) | |
6019 | return -EROFS; | |
6020 | ||
e41f941a | 6021 | if (flags & S_VERSION) |
22c44fe6 | 6022 | inode_inc_iversion(inode); |
e41f941a JB |
6023 | if (flags & S_CTIME) |
6024 | inode->i_ctime = *now; | |
6025 | if (flags & S_MTIME) | |
6026 | inode->i_mtime = *now; | |
6027 | if (flags & S_ATIME) | |
6028 | inode->i_atime = *now; | |
6029 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
6030 | } |
6031 | ||
d352ac68 CM |
6032 | /* |
6033 | * find the highest existing sequence number in a directory | |
6034 | * and then set the in-memory index_cnt variable to reflect | |
6035 | * free sequence numbers | |
6036 | */ | |
aec7477b JB |
6037 | static int btrfs_set_inode_index_count(struct inode *inode) |
6038 | { | |
6039 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6040 | struct btrfs_key key, found_key; | |
6041 | struct btrfs_path *path; | |
6042 | struct extent_buffer *leaf; | |
6043 | int ret; | |
6044 | ||
33345d01 | 6045 | key.objectid = btrfs_ino(inode); |
962a298f | 6046 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
6047 | key.offset = (u64)-1; |
6048 | ||
6049 | path = btrfs_alloc_path(); | |
6050 | if (!path) | |
6051 | return -ENOMEM; | |
6052 | ||
6053 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6054 | if (ret < 0) | |
6055 | goto out; | |
6056 | /* FIXME: we should be able to handle this */ | |
6057 | if (ret == 0) | |
6058 | goto out; | |
6059 | ret = 0; | |
6060 | ||
6061 | /* | |
6062 | * MAGIC NUMBER EXPLANATION: | |
6063 | * since we search a directory based on f_pos we have to start at 2 | |
6064 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6065 | * else has to start at 2 | |
6066 | */ | |
6067 | if (path->slots[0] == 0) { | |
6068 | BTRFS_I(inode)->index_cnt = 2; | |
6069 | goto out; | |
6070 | } | |
6071 | ||
6072 | path->slots[0]--; | |
6073 | ||
6074 | leaf = path->nodes[0]; | |
6075 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6076 | ||
33345d01 | 6077 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6078 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
aec7477b JB |
6079 | BTRFS_I(inode)->index_cnt = 2; |
6080 | goto out; | |
6081 | } | |
6082 | ||
6083 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
6084 | out: | |
6085 | btrfs_free_path(path); | |
6086 | return ret; | |
6087 | } | |
6088 | ||
d352ac68 CM |
6089 | /* |
6090 | * helper to find a free sequence number in a given directory. This current | |
6091 | * code is very simple, later versions will do smarter things in the btree | |
6092 | */ | |
3de4586c | 6093 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
aec7477b JB |
6094 | { |
6095 | int ret = 0; | |
6096 | ||
6097 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
16cdcec7 MX |
6098 | ret = btrfs_inode_delayed_dir_index_count(dir); |
6099 | if (ret) { | |
6100 | ret = btrfs_set_inode_index_count(dir); | |
6101 | if (ret) | |
6102 | return ret; | |
6103 | } | |
aec7477b JB |
6104 | } |
6105 | ||
00e4e6b3 | 6106 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
6107 | BTRFS_I(dir)->index_cnt++; |
6108 | ||
6109 | return ret; | |
6110 | } | |
6111 | ||
b0d5d10f CM |
6112 | static int btrfs_insert_inode_locked(struct inode *inode) |
6113 | { | |
6114 | struct btrfs_iget_args args; | |
6115 | args.location = &BTRFS_I(inode)->location; | |
6116 | args.root = BTRFS_I(inode)->root; | |
6117 | ||
6118 | return insert_inode_locked4(inode, | |
6119 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6120 | btrfs_find_actor, &args); | |
6121 | } | |
6122 | ||
39279cc3 CM |
6123 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6124 | struct btrfs_root *root, | |
aec7477b | 6125 | struct inode *dir, |
9c58309d | 6126 | const char *name, int name_len, |
175a4eb7 AV |
6127 | u64 ref_objectid, u64 objectid, |
6128 | umode_t mode, u64 *index) | |
39279cc3 CM |
6129 | { |
6130 | struct inode *inode; | |
5f39d397 | 6131 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6132 | struct btrfs_key *location; |
5f39d397 | 6133 | struct btrfs_path *path; |
9c58309d CM |
6134 | struct btrfs_inode_ref *ref; |
6135 | struct btrfs_key key[2]; | |
6136 | u32 sizes[2]; | |
ef3b9af5 | 6137 | int nitems = name ? 2 : 1; |
9c58309d | 6138 | unsigned long ptr; |
39279cc3 | 6139 | int ret; |
39279cc3 | 6140 | |
5f39d397 | 6141 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6142 | if (!path) |
6143 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6144 | |
39279cc3 | 6145 | inode = new_inode(root->fs_info->sb); |
8fb27640 YS |
6146 | if (!inode) { |
6147 | btrfs_free_path(path); | |
39279cc3 | 6148 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6149 | } |
39279cc3 | 6150 | |
5762b5c9 FM |
6151 | /* |
6152 | * O_TMPFILE, set link count to 0, so that after this point, | |
6153 | * we fill in an inode item with the correct link count. | |
6154 | */ | |
6155 | if (!name) | |
6156 | set_nlink(inode, 0); | |
6157 | ||
581bb050 LZ |
6158 | /* |
6159 | * we have to initialize this early, so we can reclaim the inode | |
6160 | * number if we fail afterwards in this function. | |
6161 | */ | |
6162 | inode->i_ino = objectid; | |
6163 | ||
ef3b9af5 | 6164 | if (dir && name) { |
1abe9b8a | 6165 | trace_btrfs_inode_request(dir); |
6166 | ||
3de4586c | 6167 | ret = btrfs_set_inode_index(dir, index); |
09771430 | 6168 | if (ret) { |
8fb27640 | 6169 | btrfs_free_path(path); |
09771430 | 6170 | iput(inode); |
aec7477b | 6171 | return ERR_PTR(ret); |
09771430 | 6172 | } |
ef3b9af5 FM |
6173 | } else if (dir) { |
6174 | *index = 0; | |
aec7477b JB |
6175 | } |
6176 | /* | |
6177 | * index_cnt is ignored for everything but a dir, | |
6178 | * btrfs_get_inode_index_count has an explanation for the magic | |
6179 | * number | |
6180 | */ | |
6181 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6182 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6183 | BTRFS_I(inode)->root = root; |
e02119d5 | 6184 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6185 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6186 | |
5dc562c5 JB |
6187 | /* |
6188 | * We could have gotten an inode number from somebody who was fsynced | |
6189 | * and then removed in this same transaction, so let's just set full | |
6190 | * sync since it will be a full sync anyway and this will blow away the | |
6191 | * old info in the log. | |
6192 | */ | |
6193 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6194 | ||
9c58309d | 6195 | key[0].objectid = objectid; |
962a298f | 6196 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6197 | key[0].offset = 0; |
6198 | ||
9c58309d | 6199 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6200 | |
6201 | if (name) { | |
6202 | /* | |
6203 | * Start new inodes with an inode_ref. This is slightly more | |
6204 | * efficient for small numbers of hard links since they will | |
6205 | * be packed into one item. Extended refs will kick in if we | |
6206 | * add more hard links than can fit in the ref item. | |
6207 | */ | |
6208 | key[1].objectid = objectid; | |
962a298f | 6209 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6210 | key[1].offset = ref_objectid; |
6211 | ||
6212 | sizes[1] = name_len + sizeof(*ref); | |
6213 | } | |
9c58309d | 6214 | |
b0d5d10f CM |
6215 | location = &BTRFS_I(inode)->location; |
6216 | location->objectid = objectid; | |
6217 | location->offset = 0; | |
962a298f | 6218 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6219 | |
6220 | ret = btrfs_insert_inode_locked(inode); | |
6221 | if (ret < 0) | |
6222 | goto fail; | |
6223 | ||
b9473439 | 6224 | path->leave_spinning = 1; |
ef3b9af5 | 6225 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6226 | if (ret != 0) |
b0d5d10f | 6227 | goto fail_unlock; |
5f39d397 | 6228 | |
ecc11fab | 6229 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6230 | inode_set_bytes(inode, 0); |
9cc97d64 | 6231 | |
04b285f3 | 6232 | inode->i_mtime = current_fs_time(inode->i_sb); |
9cc97d64 | 6233 | inode->i_atime = inode->i_mtime; |
6234 | inode->i_ctime = inode->i_mtime; | |
6235 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6236 | ||
5f39d397 CM |
6237 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6238 | struct btrfs_inode_item); | |
293f7e07 LZ |
6239 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
6240 | sizeof(*inode_item)); | |
e02119d5 | 6241 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6242 | |
ef3b9af5 FM |
6243 | if (name) { |
6244 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6245 | struct btrfs_inode_ref); | |
6246 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6247 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6248 | ptr = (unsigned long)(ref + 1); | |
6249 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6250 | } | |
9c58309d | 6251 | |
5f39d397 CM |
6252 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6253 | btrfs_free_path(path); | |
6254 | ||
6cbff00f CH |
6255 | btrfs_inherit_iflags(inode, dir); |
6256 | ||
569254b0 | 6257 | if (S_ISREG(mode)) { |
3cdde224 | 6258 | if (btrfs_test_opt(root->fs_info, NODATASUM)) |
94272164 | 6259 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; |
3cdde224 | 6260 | if (btrfs_test_opt(root->fs_info, NODATACOW)) |
f2bdf9a8 JB |
6261 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6262 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6263 | } |
6264 | ||
5d4f98a2 | 6265 | inode_tree_add(inode); |
1abe9b8a | 6266 | |
6267 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6268 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6269 | |
8ea05e3a AB |
6270 | btrfs_update_root_times(trans, root); |
6271 | ||
63541927 FDBM |
6272 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6273 | if (ret) | |
6274 | btrfs_err(root->fs_info, | |
6275 | "error inheriting props for ino %llu (root %llu): %d", | |
6276 | btrfs_ino(inode), root->root_key.objectid, ret); | |
6277 | ||
39279cc3 | 6278 | return inode; |
b0d5d10f CM |
6279 | |
6280 | fail_unlock: | |
6281 | unlock_new_inode(inode); | |
5f39d397 | 6282 | fail: |
ef3b9af5 | 6283 | if (dir && name) |
aec7477b | 6284 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6285 | btrfs_free_path(path); |
09771430 | 6286 | iput(inode); |
5f39d397 | 6287 | return ERR_PTR(ret); |
39279cc3 CM |
6288 | } |
6289 | ||
6290 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6291 | { | |
6292 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6293 | } | |
6294 | ||
d352ac68 CM |
6295 | /* |
6296 | * utility function to add 'inode' into 'parent_inode' with | |
6297 | * a give name and a given sequence number. | |
6298 | * if 'add_backref' is true, also insert a backref from the | |
6299 | * inode to the parent directory. | |
6300 | */ | |
e02119d5 CM |
6301 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
6302 | struct inode *parent_inode, struct inode *inode, | |
6303 | const char *name, int name_len, int add_backref, u64 index) | |
39279cc3 | 6304 | { |
4df27c4d | 6305 | int ret = 0; |
39279cc3 | 6306 | struct btrfs_key key; |
e02119d5 | 6307 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
33345d01 LZ |
6308 | u64 ino = btrfs_ino(inode); |
6309 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6310 | |
33345d01 | 6311 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6312 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
6313 | } else { | |
33345d01 | 6314 | key.objectid = ino; |
962a298f | 6315 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6316 | key.offset = 0; |
6317 | } | |
6318 | ||
33345d01 | 6319 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6320 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
6321 | key.objectid, root->root_key.objectid, | |
33345d01 | 6322 | parent_ino, index, name, name_len); |
4df27c4d | 6323 | } else if (add_backref) { |
33345d01 LZ |
6324 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6325 | parent_ino, index); | |
4df27c4d | 6326 | } |
39279cc3 | 6327 | |
79787eaa JM |
6328 | /* Nothing to clean up yet */ |
6329 | if (ret) | |
6330 | return ret; | |
4df27c4d | 6331 | |
79787eaa JM |
6332 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6333 | parent_inode, &key, | |
6334 | btrfs_inode_type(inode), index); | |
9c52057c | 6335 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6336 | goto fail_dir_item; |
6337 | else if (ret) { | |
6338 | btrfs_abort_transaction(trans, root, ret); | |
6339 | return ret; | |
39279cc3 | 6340 | } |
79787eaa JM |
6341 | |
6342 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | |
6343 | name_len * 2); | |
0c4d2d95 | 6344 | inode_inc_iversion(parent_inode); |
04b285f3 DD |
6345 | parent_inode->i_mtime = parent_inode->i_ctime = |
6346 | current_fs_time(parent_inode->i_sb); | |
79787eaa JM |
6347 | ret = btrfs_update_inode(trans, root, parent_inode); |
6348 | if (ret) | |
6349 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 6350 | return ret; |
fe66a05a CM |
6351 | |
6352 | fail_dir_item: | |
6353 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6354 | u64 local_index; | |
6355 | int err; | |
6356 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
6357 | key.objectid, root->root_key.objectid, | |
6358 | parent_ino, &local_index, name, name_len); | |
6359 | ||
6360 | } else if (add_backref) { | |
6361 | u64 local_index; | |
6362 | int err; | |
6363 | ||
6364 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6365 | ino, parent_ino, &local_index); | |
6366 | } | |
6367 | return ret; | |
39279cc3 CM |
6368 | } |
6369 | ||
6370 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
a1b075d2 JB |
6371 | struct inode *dir, struct dentry *dentry, |
6372 | struct inode *inode, int backref, u64 index) | |
39279cc3 | 6373 | { |
a1b075d2 JB |
6374 | int err = btrfs_add_link(trans, dir, inode, |
6375 | dentry->d_name.name, dentry->d_name.len, | |
6376 | backref, index); | |
39279cc3 CM |
6377 | if (err > 0) |
6378 | err = -EEXIST; | |
6379 | return err; | |
6380 | } | |
6381 | ||
618e21d5 | 6382 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6383 | umode_t mode, dev_t rdev) |
618e21d5 JB |
6384 | { |
6385 | struct btrfs_trans_handle *trans; | |
6386 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6387 | struct inode *inode = NULL; |
618e21d5 JB |
6388 | int err; |
6389 | int drop_inode = 0; | |
6390 | u64 objectid; | |
00e4e6b3 | 6391 | u64 index = 0; |
618e21d5 | 6392 | |
9ed74f2d JB |
6393 | /* |
6394 | * 2 for inode item and ref | |
6395 | * 2 for dir items | |
6396 | * 1 for xattr if selinux is on | |
6397 | */ | |
a22285a6 YZ |
6398 | trans = btrfs_start_transaction(root, 5); |
6399 | if (IS_ERR(trans)) | |
6400 | return PTR_ERR(trans); | |
1832a6d5 | 6401 | |
581bb050 LZ |
6402 | err = btrfs_find_free_ino(root, &objectid); |
6403 | if (err) | |
6404 | goto out_unlock; | |
6405 | ||
aec7477b | 6406 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6407 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6408 | mode, &index); |
7cf96da3 TI |
6409 | if (IS_ERR(inode)) { |
6410 | err = PTR_ERR(inode); | |
618e21d5 | 6411 | goto out_unlock; |
7cf96da3 | 6412 | } |
618e21d5 | 6413 | |
ad19db71 CS |
6414 | /* |
6415 | * If the active LSM wants to access the inode during | |
6416 | * d_instantiate it needs these. Smack checks to see | |
6417 | * if the filesystem supports xattrs by looking at the | |
6418 | * ops vector. | |
6419 | */ | |
ad19db71 | 6420 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6421 | init_special_inode(inode, inode->i_mode, rdev); |
6422 | ||
6423 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6424 | if (err) |
b0d5d10f CM |
6425 | goto out_unlock_inode; |
6426 | ||
6427 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
6428 | if (err) { | |
6429 | goto out_unlock_inode; | |
6430 | } else { | |
1b4ab1bb | 6431 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6432 | unlock_new_inode(inode); |
08c422c2 | 6433 | d_instantiate(dentry, inode); |
618e21d5 | 6434 | } |
b0d5d10f | 6435 | |
618e21d5 | 6436 | out_unlock: |
7ad85bb7 | 6437 | btrfs_end_transaction(trans, root); |
c581afc8 | 6438 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6439 | btrfs_btree_balance_dirty(root); |
618e21d5 JB |
6440 | if (drop_inode) { |
6441 | inode_dec_link_count(inode); | |
6442 | iput(inode); | |
6443 | } | |
618e21d5 | 6444 | return err; |
b0d5d10f CM |
6445 | |
6446 | out_unlock_inode: | |
6447 | drop_inode = 1; | |
6448 | unlock_new_inode(inode); | |
6449 | goto out_unlock; | |
6450 | ||
618e21d5 JB |
6451 | } |
6452 | ||
39279cc3 | 6453 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6454 | umode_t mode, bool excl) |
39279cc3 CM |
6455 | { |
6456 | struct btrfs_trans_handle *trans; | |
6457 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6458 | struct inode *inode = NULL; |
43baa579 | 6459 | int drop_inode_on_err = 0; |
a22285a6 | 6460 | int err; |
39279cc3 | 6461 | u64 objectid; |
00e4e6b3 | 6462 | u64 index = 0; |
39279cc3 | 6463 | |
9ed74f2d JB |
6464 | /* |
6465 | * 2 for inode item and ref | |
6466 | * 2 for dir items | |
6467 | * 1 for xattr if selinux is on | |
6468 | */ | |
a22285a6 YZ |
6469 | trans = btrfs_start_transaction(root, 5); |
6470 | if (IS_ERR(trans)) | |
6471 | return PTR_ERR(trans); | |
9ed74f2d | 6472 | |
581bb050 LZ |
6473 | err = btrfs_find_free_ino(root, &objectid); |
6474 | if (err) | |
6475 | goto out_unlock; | |
6476 | ||
aec7477b | 6477 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6478 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6479 | mode, &index); |
7cf96da3 TI |
6480 | if (IS_ERR(inode)) { |
6481 | err = PTR_ERR(inode); | |
39279cc3 | 6482 | goto out_unlock; |
7cf96da3 | 6483 | } |
43baa579 | 6484 | drop_inode_on_err = 1; |
ad19db71 CS |
6485 | /* |
6486 | * If the active LSM wants to access the inode during | |
6487 | * d_instantiate it needs these. Smack checks to see | |
6488 | * if the filesystem supports xattrs by looking at the | |
6489 | * ops vector. | |
6490 | */ | |
6491 | inode->i_fop = &btrfs_file_operations; | |
6492 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6493 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6494 | |
6495 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6496 | if (err) | |
6497 | goto out_unlock_inode; | |
6498 | ||
6499 | err = btrfs_update_inode(trans, root, inode); | |
6500 | if (err) | |
6501 | goto out_unlock_inode; | |
ad19db71 | 6502 | |
a1b075d2 | 6503 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 6504 | if (err) |
b0d5d10f | 6505 | goto out_unlock_inode; |
43baa579 | 6506 | |
43baa579 | 6507 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6508 | unlock_new_inode(inode); |
43baa579 FB |
6509 | d_instantiate(dentry, inode); |
6510 | ||
39279cc3 | 6511 | out_unlock: |
7ad85bb7 | 6512 | btrfs_end_transaction(trans, root); |
43baa579 | 6513 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6514 | inode_dec_link_count(inode); |
6515 | iput(inode); | |
6516 | } | |
c581afc8 | 6517 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6518 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6519 | return err; |
b0d5d10f CM |
6520 | |
6521 | out_unlock_inode: | |
6522 | unlock_new_inode(inode); | |
6523 | goto out_unlock; | |
6524 | ||
39279cc3 CM |
6525 | } |
6526 | ||
6527 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6528 | struct dentry *dentry) | |
6529 | { | |
271dba45 | 6530 | struct btrfs_trans_handle *trans = NULL; |
39279cc3 | 6531 | struct btrfs_root *root = BTRFS_I(dir)->root; |
2b0143b5 | 6532 | struct inode *inode = d_inode(old_dentry); |
00e4e6b3 | 6533 | u64 index; |
39279cc3 CM |
6534 | int err; |
6535 | int drop_inode = 0; | |
6536 | ||
4a8be425 TH |
6537 | /* do not allow sys_link's with other subvols of the same device */ |
6538 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6539 | return -EXDEV; |
4a8be425 | 6540 | |
f186373f | 6541 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6542 | return -EMLINK; |
4a8be425 | 6543 | |
3de4586c | 6544 | err = btrfs_set_inode_index(dir, &index); |
aec7477b JB |
6545 | if (err) |
6546 | goto fail; | |
6547 | ||
a22285a6 | 6548 | /* |
7e6b6465 | 6549 | * 2 items for inode and inode ref |
a22285a6 | 6550 | * 2 items for dir items |
7e6b6465 | 6551 | * 1 item for parent inode |
a22285a6 | 6552 | */ |
7e6b6465 | 6553 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6554 | if (IS_ERR(trans)) { |
6555 | err = PTR_ERR(trans); | |
271dba45 | 6556 | trans = NULL; |
a22285a6 YZ |
6557 | goto fail; |
6558 | } | |
5f39d397 | 6559 | |
67de1176 MX |
6560 | /* There are several dir indexes for this inode, clear the cache. */ |
6561 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6562 | inc_nlink(inode); |
0c4d2d95 | 6563 | inode_inc_iversion(inode); |
04b285f3 | 6564 | inode->i_ctime = current_fs_time(inode->i_sb); |
7de9c6ee | 6565 | ihold(inode); |
e9976151 | 6566 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6567 | |
a1b075d2 | 6568 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
5f39d397 | 6569 | |
a5719521 | 6570 | if (err) { |
54aa1f4d | 6571 | drop_inode = 1; |
a5719521 | 6572 | } else { |
10d9f309 | 6573 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6574 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6575 | if (err) |
6576 | goto fail; | |
ef3b9af5 FM |
6577 | if (inode->i_nlink == 1) { |
6578 | /* | |
6579 | * If new hard link count is 1, it's a file created | |
6580 | * with open(2) O_TMPFILE flag. | |
6581 | */ | |
6582 | err = btrfs_orphan_del(trans, inode); | |
6583 | if (err) | |
6584 | goto fail; | |
6585 | } | |
08c422c2 | 6586 | d_instantiate(dentry, inode); |
6a912213 | 6587 | btrfs_log_new_name(trans, inode, NULL, parent); |
a5719521 | 6588 | } |
39279cc3 | 6589 | |
c581afc8 | 6590 | btrfs_balance_delayed_items(root); |
1832a6d5 | 6591 | fail: |
271dba45 FM |
6592 | if (trans) |
6593 | btrfs_end_transaction(trans, root); | |
39279cc3 CM |
6594 | if (drop_inode) { |
6595 | inode_dec_link_count(inode); | |
6596 | iput(inode); | |
6597 | } | |
b53d3f5d | 6598 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
6599 | return err; |
6600 | } | |
6601 | ||
18bb1db3 | 6602 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6603 | { |
b9d86667 | 6604 | struct inode *inode = NULL; |
39279cc3 CM |
6605 | struct btrfs_trans_handle *trans; |
6606 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6607 | int err = 0; | |
6608 | int drop_on_err = 0; | |
b9d86667 | 6609 | u64 objectid = 0; |
00e4e6b3 | 6610 | u64 index = 0; |
39279cc3 | 6611 | |
9ed74f2d JB |
6612 | /* |
6613 | * 2 items for inode and ref | |
6614 | * 2 items for dir items | |
6615 | * 1 for xattr if selinux is on | |
6616 | */ | |
a22285a6 YZ |
6617 | trans = btrfs_start_transaction(root, 5); |
6618 | if (IS_ERR(trans)) | |
6619 | return PTR_ERR(trans); | |
39279cc3 | 6620 | |
581bb050 LZ |
6621 | err = btrfs_find_free_ino(root, &objectid); |
6622 | if (err) | |
6623 | goto out_fail; | |
6624 | ||
aec7477b | 6625 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6626 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6627 | S_IFDIR | mode, &index); |
39279cc3 CM |
6628 | if (IS_ERR(inode)) { |
6629 | err = PTR_ERR(inode); | |
6630 | goto out_fail; | |
6631 | } | |
5f39d397 | 6632 | |
39279cc3 | 6633 | drop_on_err = 1; |
b0d5d10f CM |
6634 | /* these must be set before we unlock the inode */ |
6635 | inode->i_op = &btrfs_dir_inode_operations; | |
6636 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6637 | |
2a7dba39 | 6638 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6639 | if (err) |
b0d5d10f | 6640 | goto out_fail_inode; |
39279cc3 | 6641 | |
dbe674a9 | 6642 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
6643 | err = btrfs_update_inode(trans, root, inode); |
6644 | if (err) | |
b0d5d10f | 6645 | goto out_fail_inode; |
5f39d397 | 6646 | |
a1b075d2 JB |
6647 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
6648 | dentry->d_name.len, 0, index); | |
39279cc3 | 6649 | if (err) |
b0d5d10f | 6650 | goto out_fail_inode; |
5f39d397 | 6651 | |
39279cc3 | 6652 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6653 | /* |
6654 | * mkdir is special. We're unlocking after we call d_instantiate | |
6655 | * to avoid a race with nfsd calling d_instantiate. | |
6656 | */ | |
6657 | unlock_new_inode(inode); | |
39279cc3 | 6658 | drop_on_err = 0; |
39279cc3 CM |
6659 | |
6660 | out_fail: | |
7ad85bb7 | 6661 | btrfs_end_transaction(trans, root); |
c7cfb8a5 WS |
6662 | if (drop_on_err) { |
6663 | inode_dec_link_count(inode); | |
39279cc3 | 6664 | iput(inode); |
c7cfb8a5 | 6665 | } |
c581afc8 | 6666 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6667 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6668 | return err; |
b0d5d10f CM |
6669 | |
6670 | out_fail_inode: | |
6671 | unlock_new_inode(inode); | |
6672 | goto out_fail; | |
39279cc3 CM |
6673 | } |
6674 | ||
e6c4efd8 QW |
6675 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6676 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6677 | { | |
6678 | struct rb_node *next; | |
6679 | ||
6680 | next = rb_next(&em->rb_node); | |
6681 | if (!next) | |
6682 | return NULL; | |
6683 | return container_of(next, struct extent_map, rb_node); | |
6684 | } | |
6685 | ||
6686 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6687 | { | |
6688 | struct rb_node *prev; | |
6689 | ||
6690 | prev = rb_prev(&em->rb_node); | |
6691 | if (!prev) | |
6692 | return NULL; | |
6693 | return container_of(prev, struct extent_map, rb_node); | |
6694 | } | |
6695 | ||
d352ac68 | 6696 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6697 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6698 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6699 | * the best fitted new extent into the tree. |
d352ac68 | 6700 | */ |
3b951516 CM |
6701 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6702 | struct extent_map *existing, | |
e6dcd2dc | 6703 | struct extent_map *em, |
51f395ad | 6704 | u64 map_start) |
3b951516 | 6705 | { |
e6c4efd8 QW |
6706 | struct extent_map *prev; |
6707 | struct extent_map *next; | |
6708 | u64 start; | |
6709 | u64 end; | |
3b951516 | 6710 | u64 start_diff; |
3b951516 | 6711 | |
e6dcd2dc | 6712 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6713 | |
6714 | if (existing->start > map_start) { | |
6715 | next = existing; | |
6716 | prev = prev_extent_map(next); | |
6717 | } else { | |
6718 | prev = existing; | |
6719 | next = next_extent_map(prev); | |
6720 | } | |
6721 | ||
6722 | start = prev ? extent_map_end(prev) : em->start; | |
6723 | start = max_t(u64, start, em->start); | |
6724 | end = next ? next->start : extent_map_end(em); | |
6725 | end = min_t(u64, end, extent_map_end(em)); | |
6726 | start_diff = start - em->start; | |
6727 | em->start = start; | |
6728 | em->len = end - start; | |
c8b97818 CM |
6729 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6730 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6731 | em->block_start += start_diff; |
c8b97818 CM |
6732 | em->block_len -= start_diff; |
6733 | } | |
09a2a8f9 | 6734 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6735 | } |
6736 | ||
c8b97818 | 6737 | static noinline int uncompress_inline(struct btrfs_path *path, |
e40da0e5 | 6738 | struct page *page, |
c8b97818 CM |
6739 | size_t pg_offset, u64 extent_offset, |
6740 | struct btrfs_file_extent_item *item) | |
6741 | { | |
6742 | int ret; | |
6743 | struct extent_buffer *leaf = path->nodes[0]; | |
6744 | char *tmp; | |
6745 | size_t max_size; | |
6746 | unsigned long inline_size; | |
6747 | unsigned long ptr; | |
261507a0 | 6748 | int compress_type; |
c8b97818 CM |
6749 | |
6750 | WARN_ON(pg_offset != 0); | |
261507a0 | 6751 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6752 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6753 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6754 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6755 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6756 | if (!tmp) |
6757 | return -ENOMEM; | |
c8b97818 CM |
6758 | ptr = btrfs_file_extent_inline_start(item); |
6759 | ||
6760 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6761 | ||
09cbfeaf | 6762 | max_size = min_t(unsigned long, PAGE_SIZE, max_size); |
261507a0 LZ |
6763 | ret = btrfs_decompress(compress_type, tmp, page, |
6764 | extent_offset, inline_size, max_size); | |
c8b97818 | 6765 | kfree(tmp); |
166ae5a4 | 6766 | return ret; |
c8b97818 CM |
6767 | } |
6768 | ||
d352ac68 CM |
6769 | /* |
6770 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6771 | * the ugly parts come from merging extents from the disk with the in-ram |
6772 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6773 | * where the in-ram extents might be locked pending data=ordered completion. |
6774 | * | |
6775 | * This also copies inline extents directly into the page. | |
6776 | */ | |
d397712b | 6777 | |
a52d9a80 | 6778 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 6779 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
6780 | int create) |
6781 | { | |
6782 | int ret; | |
6783 | int err = 0; | |
a52d9a80 CM |
6784 | u64 extent_start = 0; |
6785 | u64 extent_end = 0; | |
33345d01 | 6786 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6787 | u32 found_type; |
f421950f | 6788 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
6789 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6790 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
6791 | struct extent_buffer *leaf; |
6792 | struct btrfs_key found_key; | |
a52d9a80 CM |
6793 | struct extent_map *em = NULL; |
6794 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 6795 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 | 6796 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6797 | const bool new_inline = !page || create; |
a52d9a80 | 6798 | |
a52d9a80 | 6799 | again: |
890871be | 6800 | read_lock(&em_tree->lock); |
d1310b2e | 6801 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d CM |
6802 | if (em) |
6803 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
890871be | 6804 | read_unlock(&em_tree->lock); |
d1310b2e | 6805 | |
a52d9a80 | 6806 | if (em) { |
e1c4b745 CM |
6807 | if (em->start > start || em->start + em->len <= start) |
6808 | free_extent_map(em); | |
6809 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6810 | free_extent_map(em); |
6811 | else | |
6812 | goto out; | |
a52d9a80 | 6813 | } |
172ddd60 | 6814 | em = alloc_extent_map(); |
a52d9a80 | 6815 | if (!em) { |
d1310b2e CM |
6816 | err = -ENOMEM; |
6817 | goto out; | |
a52d9a80 | 6818 | } |
e6dcd2dc | 6819 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
d1310b2e | 6820 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6821 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6822 | em->len = (u64)-1; |
c8b97818 | 6823 | em->block_len = (u64)-1; |
f421950f CM |
6824 | |
6825 | if (!path) { | |
6826 | path = btrfs_alloc_path(); | |
026fd317 JB |
6827 | if (!path) { |
6828 | err = -ENOMEM; | |
6829 | goto out; | |
6830 | } | |
6831 | /* | |
6832 | * Chances are we'll be called again, so go ahead and do | |
6833 | * readahead | |
6834 | */ | |
e4058b54 | 6835 | path->reada = READA_FORWARD; |
f421950f CM |
6836 | } |
6837 | ||
179e29e4 CM |
6838 | ret = btrfs_lookup_file_extent(trans, root, path, |
6839 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6840 | if (ret < 0) { |
6841 | err = ret; | |
6842 | goto out; | |
6843 | } | |
6844 | ||
6845 | if (ret != 0) { | |
6846 | if (path->slots[0] == 0) | |
6847 | goto not_found; | |
6848 | path->slots[0]--; | |
6849 | } | |
6850 | ||
5f39d397 CM |
6851 | leaf = path->nodes[0]; |
6852 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6853 | struct btrfs_file_extent_item); |
a52d9a80 | 6854 | /* are we inside the extent that was found? */ |
5f39d397 | 6855 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6856 | found_type = found_key.type; |
5f39d397 | 6857 | if (found_key.objectid != objectid || |
a52d9a80 | 6858 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6859 | /* |
6860 | * If we backup past the first extent we want to move forward | |
6861 | * and see if there is an extent in front of us, otherwise we'll | |
6862 | * say there is a hole for our whole search range which can | |
6863 | * cause problems. | |
6864 | */ | |
6865 | extent_end = start; | |
6866 | goto next; | |
a52d9a80 CM |
6867 | } |
6868 | ||
5f39d397 CM |
6869 | found_type = btrfs_file_extent_type(leaf, item); |
6870 | extent_start = found_key.offset; | |
d899e052 YZ |
6871 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6872 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6873 | extent_end = extent_start + |
db94535d | 6874 | btrfs_file_extent_num_bytes(leaf, item); |
9036c102 YZ |
6875 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6876 | size_t size; | |
514ac8ad | 6877 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
fda2832f | 6878 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
9036c102 | 6879 | } |
25a50341 | 6880 | next: |
9036c102 YZ |
6881 | if (start >= extent_end) { |
6882 | path->slots[0]++; | |
6883 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6884 | ret = btrfs_next_leaf(root, path); | |
6885 | if (ret < 0) { | |
6886 | err = ret; | |
6887 | goto out; | |
a52d9a80 | 6888 | } |
9036c102 YZ |
6889 | if (ret > 0) |
6890 | goto not_found; | |
6891 | leaf = path->nodes[0]; | |
a52d9a80 | 6892 | } |
9036c102 YZ |
6893 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6894 | if (found_key.objectid != objectid || | |
6895 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6896 | goto not_found; | |
6897 | if (start + len <= found_key.offset) | |
6898 | goto not_found; | |
e2eca69d WS |
6899 | if (start > found_key.offset) |
6900 | goto next; | |
9036c102 | 6901 | em->start = start; |
70c8a91c | 6902 | em->orig_start = start; |
9036c102 YZ |
6903 | em->len = found_key.offset - start; |
6904 | goto not_found_em; | |
6905 | } | |
6906 | ||
7ffbb598 FM |
6907 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
6908 | ||
d899e052 YZ |
6909 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6910 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
6911 | goto insert; |
6912 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 6913 | unsigned long ptr; |
a52d9a80 | 6914 | char *map; |
3326d1b0 CM |
6915 | size_t size; |
6916 | size_t extent_offset; | |
6917 | size_t copy_size; | |
a52d9a80 | 6918 | |
7ffbb598 | 6919 | if (new_inline) |
689f9346 | 6920 | goto out; |
5f39d397 | 6921 | |
514ac8ad | 6922 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 6923 | extent_offset = page_offset(page) + pg_offset - extent_start; |
09cbfeaf KS |
6924 | copy_size = min_t(u64, PAGE_SIZE - pg_offset, |
6925 | size - extent_offset); | |
3326d1b0 | 6926 | em->start = extent_start + extent_offset; |
fda2832f | 6927 | em->len = ALIGN(copy_size, root->sectorsize); |
b4939680 | 6928 | em->orig_block_len = em->len; |
70c8a91c | 6929 | em->orig_start = em->start; |
689f9346 | 6930 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 6931 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
6932 | if (btrfs_file_extent_compression(leaf, item) != |
6933 | BTRFS_COMPRESS_NONE) { | |
e40da0e5 | 6934 | ret = uncompress_inline(path, page, pg_offset, |
c8b97818 | 6935 | extent_offset, item); |
166ae5a4 ZB |
6936 | if (ret) { |
6937 | err = ret; | |
6938 | goto out; | |
6939 | } | |
c8b97818 CM |
6940 | } else { |
6941 | map = kmap(page); | |
6942 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6943 | copy_size); | |
09cbfeaf | 6944 | if (pg_offset + copy_size < PAGE_SIZE) { |
93c82d57 | 6945 | memset(map + pg_offset + copy_size, 0, |
09cbfeaf | 6946 | PAGE_SIZE - pg_offset - |
93c82d57 CM |
6947 | copy_size); |
6948 | } | |
c8b97818 CM |
6949 | kunmap(page); |
6950 | } | |
179e29e4 CM |
6951 | flush_dcache_page(page); |
6952 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 6953 | BUG(); |
179e29e4 CM |
6954 | if (!trans) { |
6955 | kunmap(page); | |
6956 | free_extent_map(em); | |
6957 | em = NULL; | |
ff5714cc | 6958 | |
b3b4aa74 | 6959 | btrfs_release_path(path); |
7a7eaa40 | 6960 | trans = btrfs_join_transaction(root); |
ff5714cc | 6961 | |
3612b495 TI |
6962 | if (IS_ERR(trans)) |
6963 | return ERR_CAST(trans); | |
179e29e4 CM |
6964 | goto again; |
6965 | } | |
c8b97818 | 6966 | map = kmap(page); |
70dec807 | 6967 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 6968 | copy_size); |
c8b97818 | 6969 | kunmap(page); |
179e29e4 | 6970 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 6971 | } |
d1310b2e | 6972 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6973 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6974 | goto insert; |
a52d9a80 CM |
6975 | } |
6976 | not_found: | |
6977 | em->start = start; | |
70c8a91c | 6978 | em->orig_start = start; |
d1310b2e | 6979 | em->len = len; |
a52d9a80 | 6980 | not_found_em: |
5f39d397 | 6981 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 6982 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 6983 | insert: |
b3b4aa74 | 6984 | btrfs_release_path(path); |
d1310b2e | 6985 | if (em->start > start || extent_map_end(em) <= start) { |
c2cf52eb | 6986 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
c1c9ff7c | 6987 | em->start, em->len, start, len); |
a52d9a80 CM |
6988 | err = -EIO; |
6989 | goto out; | |
6990 | } | |
d1310b2e CM |
6991 | |
6992 | err = 0; | |
890871be | 6993 | write_lock(&em_tree->lock); |
09a2a8f9 | 6994 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6995 | /* it is possible that someone inserted the extent into the tree |
6996 | * while we had the lock dropped. It is also possible that | |
6997 | * an overlapping map exists in the tree | |
6998 | */ | |
a52d9a80 | 6999 | if (ret == -EEXIST) { |
3b951516 | 7000 | struct extent_map *existing; |
e6dcd2dc CM |
7001 | |
7002 | ret = 0; | |
7003 | ||
e6c4efd8 QW |
7004 | existing = search_extent_mapping(em_tree, start, len); |
7005 | /* | |
7006 | * existing will always be non-NULL, since there must be | |
7007 | * extent causing the -EEXIST. | |
7008 | */ | |
8dff9c85 CM |
7009 | if (existing->start == em->start && |
7010 | extent_map_end(existing) == extent_map_end(em) && | |
7011 | em->block_start == existing->block_start) { | |
7012 | /* | |
7013 | * these two extents are the same, it happens | |
7014 | * with inlines especially | |
7015 | */ | |
7016 | free_extent_map(em); | |
7017 | em = existing; | |
7018 | err = 0; | |
7019 | ||
7020 | } else if (start >= extent_map_end(existing) || | |
32be3a1a | 7021 | start <= existing->start) { |
e6c4efd8 QW |
7022 | /* |
7023 | * The existing extent map is the one nearest to | |
7024 | * the [start, start + len) range which overlaps | |
7025 | */ | |
7026 | err = merge_extent_mapping(em_tree, existing, | |
7027 | em, start); | |
e1c4b745 | 7028 | free_extent_map(existing); |
e6c4efd8 | 7029 | if (err) { |
3b951516 CM |
7030 | free_extent_map(em); |
7031 | em = NULL; | |
7032 | } | |
7033 | } else { | |
7034 | free_extent_map(em); | |
7035 | em = existing; | |
e6dcd2dc | 7036 | err = 0; |
a52d9a80 | 7037 | } |
a52d9a80 | 7038 | } |
890871be | 7039 | write_unlock(&em_tree->lock); |
a52d9a80 | 7040 | out: |
1abe9b8a | 7041 | |
4cd8587c | 7042 | trace_btrfs_get_extent(root, em); |
1abe9b8a | 7043 | |
527afb44 | 7044 | btrfs_free_path(path); |
a52d9a80 CM |
7045 | if (trans) { |
7046 | ret = btrfs_end_transaction(trans, root); | |
d397712b | 7047 | if (!err) |
a52d9a80 CM |
7048 | err = ret; |
7049 | } | |
a52d9a80 CM |
7050 | if (err) { |
7051 | free_extent_map(em); | |
a52d9a80 CM |
7052 | return ERR_PTR(err); |
7053 | } | |
79787eaa | 7054 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
7055 | return em; |
7056 | } | |
7057 | ||
ec29ed5b CM |
7058 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
7059 | size_t pg_offset, u64 start, u64 len, | |
7060 | int create) | |
7061 | { | |
7062 | struct extent_map *em; | |
7063 | struct extent_map *hole_em = NULL; | |
7064 | u64 range_start = start; | |
7065 | u64 end; | |
7066 | u64 found; | |
7067 | u64 found_end; | |
7068 | int err = 0; | |
7069 | ||
7070 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
7071 | if (IS_ERR(em)) | |
7072 | return em; | |
7073 | if (em) { | |
7074 | /* | |
f9e4fb53 LB |
7075 | * if our em maps to |
7076 | * - a hole or | |
7077 | * - a pre-alloc extent, | |
7078 | * there might actually be delalloc bytes behind it. | |
ec29ed5b | 7079 | */ |
f9e4fb53 LB |
7080 | if (em->block_start != EXTENT_MAP_HOLE && |
7081 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
ec29ed5b CM |
7082 | return em; |
7083 | else | |
7084 | hole_em = em; | |
7085 | } | |
7086 | ||
7087 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7088 | end = start + len; | |
7089 | if (end < start) | |
7090 | end = (u64)-1; | |
7091 | else | |
7092 | end -= 1; | |
7093 | ||
7094 | em = NULL; | |
7095 | ||
7096 | /* ok, we didn't find anything, lets look for delalloc */ | |
7097 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, | |
7098 | end, len, EXTENT_DELALLOC, 1); | |
7099 | found_end = range_start + found; | |
7100 | if (found_end < range_start) | |
7101 | found_end = (u64)-1; | |
7102 | ||
7103 | /* | |
7104 | * we didn't find anything useful, return | |
7105 | * the original results from get_extent() | |
7106 | */ | |
7107 | if (range_start > end || found_end <= start) { | |
7108 | em = hole_em; | |
7109 | hole_em = NULL; | |
7110 | goto out; | |
7111 | } | |
7112 | ||
7113 | /* adjust the range_start to make sure it doesn't | |
7114 | * go backwards from the start they passed in | |
7115 | */ | |
67871254 | 7116 | range_start = max(start, range_start); |
ec29ed5b CM |
7117 | found = found_end - range_start; |
7118 | ||
7119 | if (found > 0) { | |
7120 | u64 hole_start = start; | |
7121 | u64 hole_len = len; | |
7122 | ||
172ddd60 | 7123 | em = alloc_extent_map(); |
ec29ed5b CM |
7124 | if (!em) { |
7125 | err = -ENOMEM; | |
7126 | goto out; | |
7127 | } | |
7128 | /* | |
7129 | * when btrfs_get_extent can't find anything it | |
7130 | * returns one huge hole | |
7131 | * | |
7132 | * make sure what it found really fits our range, and | |
7133 | * adjust to make sure it is based on the start from | |
7134 | * the caller | |
7135 | */ | |
7136 | if (hole_em) { | |
7137 | u64 calc_end = extent_map_end(hole_em); | |
7138 | ||
7139 | if (calc_end <= start || (hole_em->start > end)) { | |
7140 | free_extent_map(hole_em); | |
7141 | hole_em = NULL; | |
7142 | } else { | |
7143 | hole_start = max(hole_em->start, start); | |
7144 | hole_len = calc_end - hole_start; | |
7145 | } | |
7146 | } | |
7147 | em->bdev = NULL; | |
7148 | if (hole_em && range_start > hole_start) { | |
7149 | /* our hole starts before our delalloc, so we | |
7150 | * have to return just the parts of the hole | |
7151 | * that go until the delalloc starts | |
7152 | */ | |
7153 | em->len = min(hole_len, | |
7154 | range_start - hole_start); | |
7155 | em->start = hole_start; | |
7156 | em->orig_start = hole_start; | |
7157 | /* | |
7158 | * don't adjust block start at all, | |
7159 | * it is fixed at EXTENT_MAP_HOLE | |
7160 | */ | |
7161 | em->block_start = hole_em->block_start; | |
7162 | em->block_len = hole_len; | |
f9e4fb53 LB |
7163 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7164 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7165 | } else { |
7166 | em->start = range_start; | |
7167 | em->len = found; | |
7168 | em->orig_start = range_start; | |
7169 | em->block_start = EXTENT_MAP_DELALLOC; | |
7170 | em->block_len = found; | |
7171 | } | |
7172 | } else if (hole_em) { | |
7173 | return hole_em; | |
7174 | } | |
7175 | out: | |
7176 | ||
7177 | free_extent_map(hole_em); | |
7178 | if (err) { | |
7179 | free_extent_map(em); | |
7180 | return ERR_PTR(err); | |
7181 | } | |
7182 | return em; | |
7183 | } | |
7184 | ||
5f9a8a51 FM |
7185 | static struct extent_map *btrfs_create_dio_extent(struct inode *inode, |
7186 | const u64 start, | |
7187 | const u64 len, | |
7188 | const u64 orig_start, | |
7189 | const u64 block_start, | |
7190 | const u64 block_len, | |
7191 | const u64 orig_block_len, | |
7192 | const u64 ram_bytes, | |
7193 | const int type) | |
7194 | { | |
7195 | struct extent_map *em = NULL; | |
7196 | int ret; | |
7197 | ||
7198 | down_read(&BTRFS_I(inode)->dio_sem); | |
7199 | if (type != BTRFS_ORDERED_NOCOW) { | |
7200 | em = create_pinned_em(inode, start, len, orig_start, | |
7201 | block_start, block_len, orig_block_len, | |
7202 | ram_bytes, type); | |
7203 | if (IS_ERR(em)) | |
7204 | goto out; | |
7205 | } | |
7206 | ret = btrfs_add_ordered_extent_dio(inode, start, block_start, | |
7207 | len, block_len, type); | |
7208 | if (ret) { | |
7209 | if (em) { | |
7210 | free_extent_map(em); | |
7211 | btrfs_drop_extent_cache(inode, start, | |
7212 | start + len - 1, 0); | |
7213 | } | |
7214 | em = ERR_PTR(ret); | |
7215 | } | |
7216 | out: | |
7217 | up_read(&BTRFS_I(inode)->dio_sem); | |
7218 | ||
7219 | return em; | |
7220 | } | |
7221 | ||
4b46fce2 JB |
7222 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7223 | u64 start, u64 len) | |
7224 | { | |
7225 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
70c8a91c | 7226 | struct extent_map *em; |
4b46fce2 JB |
7227 | struct btrfs_key ins; |
7228 | u64 alloc_hint; | |
7229 | int ret; | |
4b46fce2 | 7230 | |
4b46fce2 | 7231 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
00361589 | 7232 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
e570fd27 | 7233 | alloc_hint, &ins, 1, 1); |
00361589 JB |
7234 | if (ret) |
7235 | return ERR_PTR(ret); | |
4b46fce2 | 7236 | |
5f9a8a51 FM |
7237 | em = btrfs_create_dio_extent(inode, start, ins.offset, start, |
7238 | ins.objectid, ins.offset, ins.offset, | |
7239 | ins.offset, 0); | |
9cfa3e34 | 7240 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
5f9a8a51 | 7241 | if (IS_ERR(em)) |
e570fd27 | 7242 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
de0ee0ed | 7243 | |
4b46fce2 JB |
7244 | return em; |
7245 | } | |
7246 | ||
46bfbb5c CM |
7247 | /* |
7248 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7249 | * block must be cow'd | |
7250 | */ | |
00361589 | 7251 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7252 | u64 *orig_start, u64 *orig_block_len, |
7253 | u64 *ram_bytes) | |
46bfbb5c | 7254 | { |
00361589 | 7255 | struct btrfs_trans_handle *trans; |
46bfbb5c CM |
7256 | struct btrfs_path *path; |
7257 | int ret; | |
7258 | struct extent_buffer *leaf; | |
7259 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7260 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7261 | struct btrfs_file_extent_item *fi; |
7262 | struct btrfs_key key; | |
7263 | u64 disk_bytenr; | |
7264 | u64 backref_offset; | |
7265 | u64 extent_end; | |
7266 | u64 num_bytes; | |
7267 | int slot; | |
7268 | int found_type; | |
7ee9e440 | 7269 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7270 | |
46bfbb5c CM |
7271 | path = btrfs_alloc_path(); |
7272 | if (!path) | |
7273 | return -ENOMEM; | |
7274 | ||
00361589 | 7275 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
46bfbb5c CM |
7276 | offset, 0); |
7277 | if (ret < 0) | |
7278 | goto out; | |
7279 | ||
7280 | slot = path->slots[0]; | |
7281 | if (ret == 1) { | |
7282 | if (slot == 0) { | |
7283 | /* can't find the item, must cow */ | |
7284 | ret = 0; | |
7285 | goto out; | |
7286 | } | |
7287 | slot--; | |
7288 | } | |
7289 | ret = 0; | |
7290 | leaf = path->nodes[0]; | |
7291 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
33345d01 | 7292 | if (key.objectid != btrfs_ino(inode) || |
46bfbb5c CM |
7293 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7294 | /* not our file or wrong item type, must cow */ | |
7295 | goto out; | |
7296 | } | |
7297 | ||
7298 | if (key.offset > offset) { | |
7299 | /* Wrong offset, must cow */ | |
7300 | goto out; | |
7301 | } | |
7302 | ||
7303 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7304 | found_type = btrfs_file_extent_type(leaf, fi); | |
7305 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7306 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7307 | /* not a regular extent, must cow */ | |
7308 | goto out; | |
7309 | } | |
7ee9e440 JB |
7310 | |
7311 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7312 | goto out; | |
7313 | ||
e77751aa MX |
7314 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7315 | if (extent_end <= offset) | |
7316 | goto out; | |
7317 | ||
46bfbb5c | 7318 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7319 | if (disk_bytenr == 0) |
7320 | goto out; | |
7321 | ||
7322 | if (btrfs_file_extent_compression(leaf, fi) || | |
7323 | btrfs_file_extent_encryption(leaf, fi) || | |
7324 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7325 | goto out; | |
7326 | ||
46bfbb5c CM |
7327 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7328 | ||
7ee9e440 JB |
7329 | if (orig_start) { |
7330 | *orig_start = key.offset - backref_offset; | |
7331 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7332 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7333 | } | |
eb384b55 | 7334 | |
46bfbb5c CM |
7335 | if (btrfs_extent_readonly(root, disk_bytenr)) |
7336 | goto out; | |
7b2b7085 MX |
7337 | |
7338 | num_bytes = min(offset + *len, extent_end) - offset; | |
7339 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7340 | u64 range_end; | |
7341 | ||
7342 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; | |
7343 | ret = test_range_bit(io_tree, offset, range_end, | |
7344 | EXTENT_DELALLOC, 0, NULL); | |
7345 | if (ret) { | |
7346 | ret = -EAGAIN; | |
7347 | goto out; | |
7348 | } | |
7349 | } | |
7350 | ||
1bda19eb | 7351 | btrfs_release_path(path); |
46bfbb5c CM |
7352 | |
7353 | /* | |
7354 | * look for other files referencing this extent, if we | |
7355 | * find any we must cow | |
7356 | */ | |
00361589 JB |
7357 | trans = btrfs_join_transaction(root); |
7358 | if (IS_ERR(trans)) { | |
7359 | ret = 0; | |
46bfbb5c | 7360 | goto out; |
00361589 JB |
7361 | } |
7362 | ||
7363 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), | |
7364 | key.offset - backref_offset, disk_bytenr); | |
7365 | btrfs_end_transaction(trans, root); | |
7366 | if (ret) { | |
7367 | ret = 0; | |
7368 | goto out; | |
7369 | } | |
46bfbb5c CM |
7370 | |
7371 | /* | |
7372 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7373 | * in this extent we are about to write. If there | |
7374 | * are any csums in that range we have to cow in order | |
7375 | * to keep the csums correct | |
7376 | */ | |
7377 | disk_bytenr += backref_offset; | |
7378 | disk_bytenr += offset - key.offset; | |
46bfbb5c CM |
7379 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
7380 | goto out; | |
7381 | /* | |
7382 | * all of the above have passed, it is safe to overwrite this extent | |
7383 | * without cow | |
7384 | */ | |
eb384b55 | 7385 | *len = num_bytes; |
46bfbb5c CM |
7386 | ret = 1; |
7387 | out: | |
7388 | btrfs_free_path(path); | |
7389 | return ret; | |
7390 | } | |
7391 | ||
fc4adbff AG |
7392 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7393 | { | |
7394 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
7395 | int found = false; | |
7396 | void **pagep = NULL; | |
7397 | struct page *page = NULL; | |
7398 | int start_idx; | |
7399 | int end_idx; | |
7400 | ||
09cbfeaf | 7401 | start_idx = start >> PAGE_SHIFT; |
fc4adbff AG |
7402 | |
7403 | /* | |
7404 | * end is the last byte in the last page. end == start is legal | |
7405 | */ | |
09cbfeaf | 7406 | end_idx = end >> PAGE_SHIFT; |
fc4adbff AG |
7407 | |
7408 | rcu_read_lock(); | |
7409 | ||
7410 | /* Most of the code in this while loop is lifted from | |
7411 | * find_get_page. It's been modified to begin searching from a | |
7412 | * page and return just the first page found in that range. If the | |
7413 | * found idx is less than or equal to the end idx then we know that | |
7414 | * a page exists. If no pages are found or if those pages are | |
7415 | * outside of the range then we're fine (yay!) */ | |
7416 | while (page == NULL && | |
7417 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7418 | page = radix_tree_deref_slot(pagep); | |
7419 | if (unlikely(!page)) | |
7420 | break; | |
7421 | ||
7422 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7423 | if (radix_tree_deref_retry(page)) { |
7424 | page = NULL; | |
fc4adbff | 7425 | continue; |
809f9016 | 7426 | } |
fc4adbff AG |
7427 | /* |
7428 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7429 | * here as an exceptional entry: so return it without | |
7430 | * attempting to raise page count. | |
7431 | */ | |
6fdef6d4 | 7432 | page = NULL; |
fc4adbff AG |
7433 | break; /* TODO: Is this relevant for this use case? */ |
7434 | } | |
7435 | ||
91405151 FM |
7436 | if (!page_cache_get_speculative(page)) { |
7437 | page = NULL; | |
fc4adbff | 7438 | continue; |
91405151 | 7439 | } |
fc4adbff AG |
7440 | |
7441 | /* | |
7442 | * Has the page moved? | |
7443 | * This is part of the lockless pagecache protocol. See | |
7444 | * include/linux/pagemap.h for details. | |
7445 | */ | |
7446 | if (unlikely(page != *pagep)) { | |
09cbfeaf | 7447 | put_page(page); |
fc4adbff AG |
7448 | page = NULL; |
7449 | } | |
7450 | } | |
7451 | ||
7452 | if (page) { | |
7453 | if (page->index <= end_idx) | |
7454 | found = true; | |
09cbfeaf | 7455 | put_page(page); |
fc4adbff AG |
7456 | } |
7457 | ||
7458 | rcu_read_unlock(); | |
7459 | return found; | |
7460 | } | |
7461 | ||
eb838e73 JB |
7462 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7463 | struct extent_state **cached_state, int writing) | |
7464 | { | |
7465 | struct btrfs_ordered_extent *ordered; | |
7466 | int ret = 0; | |
7467 | ||
7468 | while (1) { | |
7469 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
ff13db41 | 7470 | cached_state); |
eb838e73 JB |
7471 | /* |
7472 | * We're concerned with the entire range that we're going to be | |
01327610 | 7473 | * doing DIO to, so we need to make sure there's no ordered |
eb838e73 JB |
7474 | * extents in this range. |
7475 | */ | |
7476 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | |
7477 | lockend - lockstart + 1); | |
7478 | ||
7479 | /* | |
7480 | * We need to make sure there are no buffered pages in this | |
7481 | * range either, we could have raced between the invalidate in | |
7482 | * generic_file_direct_write and locking the extent. The | |
7483 | * invalidate needs to happen so that reads after a write do not | |
7484 | * get stale data. | |
7485 | */ | |
fc4adbff AG |
7486 | if (!ordered && |
7487 | (!writing || | |
7488 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7489 | break; |
7490 | ||
7491 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7492 | cached_state, GFP_NOFS); | |
7493 | ||
7494 | if (ordered) { | |
ade77029 FM |
7495 | /* |
7496 | * If we are doing a DIO read and the ordered extent we | |
7497 | * found is for a buffered write, we can not wait for it | |
7498 | * to complete and retry, because if we do so we can | |
7499 | * deadlock with concurrent buffered writes on page | |
7500 | * locks. This happens only if our DIO read covers more | |
7501 | * than one extent map, if at this point has already | |
7502 | * created an ordered extent for a previous extent map | |
7503 | * and locked its range in the inode's io tree, and a | |
7504 | * concurrent write against that previous extent map's | |
7505 | * range and this range started (we unlock the ranges | |
7506 | * in the io tree only when the bios complete and | |
7507 | * buffered writes always lock pages before attempting | |
7508 | * to lock range in the io tree). | |
7509 | */ | |
7510 | if (writing || | |
7511 | test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) | |
7512 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7513 | else | |
7514 | ret = -ENOTBLK; | |
eb838e73 JB |
7515 | btrfs_put_ordered_extent(ordered); |
7516 | } else { | |
eb838e73 | 7517 | /* |
b850ae14 FM |
7518 | * We could trigger writeback for this range (and wait |
7519 | * for it to complete) and then invalidate the pages for | |
7520 | * this range (through invalidate_inode_pages2_range()), | |
7521 | * but that can lead us to a deadlock with a concurrent | |
7522 | * call to readpages() (a buffered read or a defrag call | |
7523 | * triggered a readahead) on a page lock due to an | |
7524 | * ordered dio extent we created before but did not have | |
7525 | * yet a corresponding bio submitted (whence it can not | |
7526 | * complete), which makes readpages() wait for that | |
7527 | * ordered extent to complete while holding a lock on | |
7528 | * that page. | |
eb838e73 | 7529 | */ |
b850ae14 | 7530 | ret = -ENOTBLK; |
eb838e73 JB |
7531 | } |
7532 | ||
ade77029 FM |
7533 | if (ret) |
7534 | break; | |
7535 | ||
eb838e73 JB |
7536 | cond_resched(); |
7537 | } | |
7538 | ||
7539 | return ret; | |
7540 | } | |
7541 | ||
69ffb543 JB |
7542 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
7543 | u64 len, u64 orig_start, | |
7544 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
7545 | u64 orig_block_len, u64 ram_bytes, |
7546 | int type) | |
69ffb543 JB |
7547 | { |
7548 | struct extent_map_tree *em_tree; | |
7549 | struct extent_map *em; | |
7550 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7551 | int ret; | |
7552 | ||
7553 | em_tree = &BTRFS_I(inode)->extent_tree; | |
7554 | em = alloc_extent_map(); | |
7555 | if (!em) | |
7556 | return ERR_PTR(-ENOMEM); | |
7557 | ||
7558 | em->start = start; | |
7559 | em->orig_start = orig_start; | |
2ab28f32 JB |
7560 | em->mod_start = start; |
7561 | em->mod_len = len; | |
69ffb543 JB |
7562 | em->len = len; |
7563 | em->block_len = block_len; | |
7564 | em->block_start = block_start; | |
7565 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7566 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7567 | em->ram_bytes = ram_bytes; |
70c8a91c | 7568 | em->generation = -1; |
69ffb543 JB |
7569 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
7570 | if (type == BTRFS_ORDERED_PREALLOC) | |
b11e234d | 7571 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
69ffb543 JB |
7572 | |
7573 | do { | |
7574 | btrfs_drop_extent_cache(inode, em->start, | |
7575 | em->start + em->len - 1, 0); | |
7576 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7577 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 JB |
7578 | write_unlock(&em_tree->lock); |
7579 | } while (ret == -EEXIST); | |
7580 | ||
7581 | if (ret) { | |
7582 | free_extent_map(em); | |
7583 | return ERR_PTR(ret); | |
7584 | } | |
7585 | ||
7586 | return em; | |
7587 | } | |
7588 | ||
9c9464cc FM |
7589 | static void adjust_dio_outstanding_extents(struct inode *inode, |
7590 | struct btrfs_dio_data *dio_data, | |
7591 | const u64 len) | |
7592 | { | |
7593 | unsigned num_extents; | |
7594 | ||
7595 | num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1, | |
7596 | BTRFS_MAX_EXTENT_SIZE); | |
7597 | /* | |
7598 | * If we have an outstanding_extents count still set then we're | |
7599 | * within our reservation, otherwise we need to adjust our inode | |
7600 | * counter appropriately. | |
7601 | */ | |
7602 | if (dio_data->outstanding_extents) { | |
7603 | dio_data->outstanding_extents -= num_extents; | |
7604 | } else { | |
7605 | spin_lock(&BTRFS_I(inode)->lock); | |
7606 | BTRFS_I(inode)->outstanding_extents += num_extents; | |
7607 | spin_unlock(&BTRFS_I(inode)->lock); | |
7608 | } | |
7609 | } | |
7610 | ||
4b46fce2 JB |
7611 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7612 | struct buffer_head *bh_result, int create) | |
7613 | { | |
7614 | struct extent_map *em; | |
7615 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
eb838e73 | 7616 | struct extent_state *cached_state = NULL; |
50745b0a | 7617 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7618 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7619 | u64 lockstart, lockend; |
4b46fce2 | 7620 | u64 len = bh_result->b_size; |
eb838e73 | 7621 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7622 | int ret = 0; |
eb838e73 | 7623 | |
172a5049 | 7624 | if (create) |
3266789f | 7625 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7626 | else |
c329861d | 7627 | len = min_t(u64, len, root->sectorsize); |
eb838e73 | 7628 | |
c329861d JB |
7629 | lockstart = start; |
7630 | lockend = start + len - 1; | |
7631 | ||
e1cbbfa5 JB |
7632 | if (current->journal_info) { |
7633 | /* | |
7634 | * Need to pull our outstanding extents and set journal_info to NULL so | |
01327610 | 7635 | * that anything that needs to check if there's a transaction doesn't get |
e1cbbfa5 JB |
7636 | * confused. |
7637 | */ | |
50745b0a | 7638 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7639 | current->journal_info = NULL; |
7640 | } | |
7641 | ||
eb838e73 JB |
7642 | /* |
7643 | * If this errors out it's because we couldn't invalidate pagecache for | |
7644 | * this range and we need to fallback to buffered. | |
7645 | */ | |
9c9464cc FM |
7646 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7647 | create)) { | |
7648 | ret = -ENOTBLK; | |
7649 | goto err; | |
7650 | } | |
eb838e73 | 7651 | |
4b46fce2 | 7652 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
eb838e73 JB |
7653 | if (IS_ERR(em)) { |
7654 | ret = PTR_ERR(em); | |
7655 | goto unlock_err; | |
7656 | } | |
4b46fce2 JB |
7657 | |
7658 | /* | |
7659 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7660 | * io. INLINE is special, and we could probably kludge it in here, but | |
7661 | * it's still buffered so for safety lets just fall back to the generic | |
7662 | * buffered path. | |
7663 | * | |
7664 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7665 | * decompress it, so there will be buffering required no matter what we | |
7666 | * do, so go ahead and fallback to buffered. | |
7667 | * | |
01327610 | 7668 | * We return -ENOTBLK because that's what makes DIO go ahead and go back |
4b46fce2 JB |
7669 | * to buffered IO. Don't blame me, this is the price we pay for using |
7670 | * the generic code. | |
7671 | */ | |
7672 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7673 | em->block_start == EXTENT_MAP_INLINE) { | |
7674 | free_extent_map(em); | |
eb838e73 JB |
7675 | ret = -ENOTBLK; |
7676 | goto unlock_err; | |
4b46fce2 JB |
7677 | } |
7678 | ||
7679 | /* Just a good old fashioned hole, return */ | |
7680 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7681 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7682 | free_extent_map(em); | |
eb838e73 | 7683 | goto unlock_err; |
4b46fce2 JB |
7684 | } |
7685 | ||
7686 | /* | |
7687 | * We don't allocate a new extent in the following cases | |
7688 | * | |
7689 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7690 | * existing extent. | |
7691 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7692 | * just use the extent. | |
7693 | * | |
7694 | */ | |
46bfbb5c | 7695 | if (!create) { |
eb838e73 JB |
7696 | len = min(len, em->len - (start - em->start)); |
7697 | lockstart = start + len; | |
7698 | goto unlock; | |
46bfbb5c | 7699 | } |
4b46fce2 JB |
7700 | |
7701 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7702 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7703 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7704 | int type; |
eb384b55 | 7705 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7706 | |
7707 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7708 | type = BTRFS_ORDERED_PREALLOC; | |
7709 | else | |
7710 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7711 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7712 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7713 | |
00361589 | 7714 | if (can_nocow_extent(inode, start, &len, &orig_start, |
f78c436c FM |
7715 | &orig_block_len, &ram_bytes) == 1 && |
7716 | btrfs_inc_nocow_writers(root->fs_info, block_start)) { | |
5f9a8a51 | 7717 | struct extent_map *em2; |
0b901916 | 7718 | |
5f9a8a51 FM |
7719 | em2 = btrfs_create_dio_extent(inode, start, len, |
7720 | orig_start, block_start, | |
7721 | len, orig_block_len, | |
7722 | ram_bytes, type); | |
f78c436c | 7723 | btrfs_dec_nocow_writers(root->fs_info, block_start); |
69ffb543 JB |
7724 | if (type == BTRFS_ORDERED_PREALLOC) { |
7725 | free_extent_map(em); | |
5f9a8a51 | 7726 | em = em2; |
69ffb543 | 7727 | } |
5f9a8a51 FM |
7728 | if (em2 && IS_ERR(em2)) { |
7729 | ret = PTR_ERR(em2); | |
eb838e73 | 7730 | goto unlock_err; |
46bfbb5c CM |
7731 | } |
7732 | goto unlock; | |
4b46fce2 | 7733 | } |
4b46fce2 | 7734 | } |
00361589 | 7735 | |
46bfbb5c CM |
7736 | /* |
7737 | * this will cow the extent, reset the len in case we changed | |
7738 | * it above | |
7739 | */ | |
7740 | len = bh_result->b_size; | |
70c8a91c JB |
7741 | free_extent_map(em); |
7742 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7743 | if (IS_ERR(em)) { |
7744 | ret = PTR_ERR(em); | |
7745 | goto unlock_err; | |
7746 | } | |
46bfbb5c CM |
7747 | len = min(len, em->len - (start - em->start)); |
7748 | unlock: | |
4b46fce2 JB |
7749 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7750 | inode->i_blkbits; | |
46bfbb5c | 7751 | bh_result->b_size = len; |
4b46fce2 JB |
7752 | bh_result->b_bdev = em->bdev; |
7753 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7754 | if (create) { |
7755 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7756 | set_buffer_new(bh_result); | |
7757 | ||
7758 | /* | |
7759 | * Need to update the i_size under the extent lock so buffered | |
7760 | * readers will get the updated i_size when we unlock. | |
7761 | */ | |
7762 | if (start + len > i_size_read(inode)) | |
7763 | i_size_write(inode, start + len); | |
0934856d | 7764 | |
9c9464cc | 7765 | adjust_dio_outstanding_extents(inode, dio_data, len); |
7cf5b976 | 7766 | btrfs_free_reserved_data_space(inode, start, len); |
50745b0a | 7767 | WARN_ON(dio_data->reserve < len); |
7768 | dio_data->reserve -= len; | |
f28a4928 | 7769 | dio_data->unsubmitted_oe_range_end = start + len; |
50745b0a | 7770 | current->journal_info = dio_data; |
c3473e83 | 7771 | } |
4b46fce2 | 7772 | |
eb838e73 JB |
7773 | /* |
7774 | * In the case of write we need to clear and unlock the entire range, | |
7775 | * in the case of read we need to unlock only the end area that we | |
7776 | * aren't using if there is any left over space. | |
7777 | */ | |
24c03fa5 | 7778 | if (lockstart < lockend) { |
0934856d MX |
7779 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7780 | lockend, unlock_bits, 1, 0, | |
7781 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7782 | } else { |
eb838e73 | 7783 | free_extent_state(cached_state); |
24c03fa5 | 7784 | } |
eb838e73 | 7785 | |
4b46fce2 JB |
7786 | free_extent_map(em); |
7787 | ||
7788 | return 0; | |
eb838e73 JB |
7789 | |
7790 | unlock_err: | |
eb838e73 JB |
7791 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7792 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
9c9464cc | 7793 | err: |
50745b0a | 7794 | if (dio_data) |
7795 | current->journal_info = dio_data; | |
9c9464cc FM |
7796 | /* |
7797 | * Compensate the delalloc release we do in btrfs_direct_IO() when we | |
7798 | * write less data then expected, so that we don't underflow our inode's | |
7799 | * outstanding extents counter. | |
7800 | */ | |
7801 | if (create && dio_data) | |
7802 | adjust_dio_outstanding_extents(inode, dio_data, len); | |
7803 | ||
eb838e73 | 7804 | return ret; |
4b46fce2 JB |
7805 | } |
7806 | ||
8b110e39 MX |
7807 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
7808 | int rw, int mirror_num) | |
7809 | { | |
7810 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7811 | int ret; | |
7812 | ||
7813 | BUG_ON(rw & REQ_WRITE); | |
7814 | ||
7815 | bio_get(bio); | |
7816 | ||
7817 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, | |
7818 | BTRFS_WQ_ENDIO_DIO_REPAIR); | |
7819 | if (ret) | |
7820 | goto err; | |
7821 | ||
7822 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); | |
7823 | err: | |
7824 | bio_put(bio); | |
7825 | return ret; | |
7826 | } | |
7827 | ||
7828 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7829 | struct bio *failed_bio, | |
7830 | struct io_failure_record *failrec, | |
7831 | int failed_mirror) | |
7832 | { | |
7833 | int num_copies; | |
7834 | ||
7835 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, | |
7836 | failrec->logical, failrec->len); | |
7837 | if (num_copies == 1) { | |
7838 | /* | |
7839 | * we only have a single copy of the data, so don't bother with | |
7840 | * all the retry and error correction code that follows. no | |
7841 | * matter what the error is, it is very likely to persist. | |
7842 | */ | |
7843 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7844 | num_copies, failrec->this_mirror, failed_mirror); | |
7845 | return 0; | |
7846 | } | |
7847 | ||
7848 | failrec->failed_mirror = failed_mirror; | |
7849 | failrec->this_mirror++; | |
7850 | if (failrec->this_mirror == failed_mirror) | |
7851 | failrec->this_mirror++; | |
7852 | ||
7853 | if (failrec->this_mirror > num_copies) { | |
7854 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7855 | num_copies, failrec->this_mirror, failed_mirror); | |
7856 | return 0; | |
7857 | } | |
7858 | ||
7859 | return 1; | |
7860 | } | |
7861 | ||
7862 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, | |
2dabb324 CR |
7863 | struct page *page, unsigned int pgoff, |
7864 | u64 start, u64 end, int failed_mirror, | |
7865 | bio_end_io_t *repair_endio, void *repair_arg) | |
8b110e39 MX |
7866 | { |
7867 | struct io_failure_record *failrec; | |
7868 | struct bio *bio; | |
7869 | int isector; | |
7870 | int read_mode; | |
7871 | int ret; | |
7872 | ||
7873 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); | |
7874 | ||
7875 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7876 | if (ret) | |
7877 | return ret; | |
7878 | ||
7879 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7880 | failed_mirror); | |
7881 | if (!ret) { | |
7882 | free_io_failure(inode, failrec); | |
7883 | return -EIO; | |
7884 | } | |
7885 | ||
2dabb324 CR |
7886 | if ((failed_bio->bi_vcnt > 1) |
7887 | || (failed_bio->bi_io_vec->bv_len | |
7888 | > BTRFS_I(inode)->root->sectorsize)) | |
8b110e39 MX |
7889 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; |
7890 | else | |
7891 | read_mode = READ_SYNC; | |
7892 | ||
7893 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
7894 | isector >>= inode->i_sb->s_blocksize_bits; | |
7895 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
2dabb324 | 7896 | pgoff, isector, repair_endio, repair_arg); |
8b110e39 MX |
7897 | if (!bio) { |
7898 | free_io_failure(inode, failrec); | |
7899 | return -EIO; | |
7900 | } | |
7901 | ||
7902 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
7903 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", | |
7904 | read_mode, failrec->this_mirror, failrec->in_validation); | |
7905 | ||
7906 | ret = submit_dio_repair_bio(inode, bio, read_mode, | |
7907 | failrec->this_mirror); | |
7908 | if (ret) { | |
7909 | free_io_failure(inode, failrec); | |
7910 | bio_put(bio); | |
7911 | } | |
7912 | ||
7913 | return ret; | |
7914 | } | |
7915 | ||
7916 | struct btrfs_retry_complete { | |
7917 | struct completion done; | |
7918 | struct inode *inode; | |
7919 | u64 start; | |
7920 | int uptodate; | |
7921 | }; | |
7922 | ||
4246a0b6 | 7923 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
7924 | { |
7925 | struct btrfs_retry_complete *done = bio->bi_private; | |
2dabb324 | 7926 | struct inode *inode; |
8b110e39 MX |
7927 | struct bio_vec *bvec; |
7928 | int i; | |
7929 | ||
4246a0b6 | 7930 | if (bio->bi_error) |
8b110e39 MX |
7931 | goto end; |
7932 | ||
2dabb324 CR |
7933 | ASSERT(bio->bi_vcnt == 1); |
7934 | inode = bio->bi_io_vec->bv_page->mapping->host; | |
7935 | ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize); | |
7936 | ||
8b110e39 MX |
7937 | done->uptodate = 1; |
7938 | bio_for_each_segment_all(bvec, bio, i) | |
7939 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); | |
7940 | end: | |
7941 | complete(&done->done); | |
7942 | bio_put(bio); | |
7943 | } | |
7944 | ||
7945 | static int __btrfs_correct_data_nocsum(struct inode *inode, | |
7946 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 7947 | { |
2dabb324 | 7948 | struct btrfs_fs_info *fs_info; |
2c30c71b | 7949 | struct bio_vec *bvec; |
8b110e39 | 7950 | struct btrfs_retry_complete done; |
4b46fce2 | 7951 | u64 start; |
2dabb324 CR |
7952 | unsigned int pgoff; |
7953 | u32 sectorsize; | |
7954 | int nr_sectors; | |
2c30c71b | 7955 | int i; |
c1dc0896 | 7956 | int ret; |
4b46fce2 | 7957 | |
2dabb324 CR |
7958 | fs_info = BTRFS_I(inode)->root->fs_info; |
7959 | sectorsize = BTRFS_I(inode)->root->sectorsize; | |
7960 | ||
8b110e39 MX |
7961 | start = io_bio->logical; |
7962 | done.inode = inode; | |
7963 | ||
7964 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { | |
2dabb324 CR |
7965 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
7966 | pgoff = bvec->bv_offset; | |
7967 | ||
7968 | next_block_or_try_again: | |
8b110e39 MX |
7969 | done.uptodate = 0; |
7970 | done.start = start; | |
7971 | init_completion(&done.done); | |
7972 | ||
2dabb324 CR |
7973 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
7974 | pgoff, start, start + sectorsize - 1, | |
7975 | io_bio->mirror_num, | |
7976 | btrfs_retry_endio_nocsum, &done); | |
8b110e39 MX |
7977 | if (ret) |
7978 | return ret; | |
7979 | ||
7980 | wait_for_completion(&done.done); | |
7981 | ||
7982 | if (!done.uptodate) { | |
7983 | /* We might have another mirror, so try again */ | |
2dabb324 | 7984 | goto next_block_or_try_again; |
8b110e39 MX |
7985 | } |
7986 | ||
2dabb324 CR |
7987 | start += sectorsize; |
7988 | ||
7989 | if (nr_sectors--) { | |
7990 | pgoff += sectorsize; | |
7991 | goto next_block_or_try_again; | |
7992 | } | |
8b110e39 MX |
7993 | } |
7994 | ||
7995 | return 0; | |
7996 | } | |
7997 | ||
4246a0b6 | 7998 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
7999 | { |
8000 | struct btrfs_retry_complete *done = bio->bi_private; | |
8001 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
2dabb324 | 8002 | struct inode *inode; |
8b110e39 | 8003 | struct bio_vec *bvec; |
2dabb324 | 8004 | u64 start; |
8b110e39 MX |
8005 | int uptodate; |
8006 | int ret; | |
8007 | int i; | |
8008 | ||
4246a0b6 | 8009 | if (bio->bi_error) |
8b110e39 MX |
8010 | goto end; |
8011 | ||
8012 | uptodate = 1; | |
2dabb324 CR |
8013 | |
8014 | start = done->start; | |
8015 | ||
8016 | ASSERT(bio->bi_vcnt == 1); | |
8017 | inode = bio->bi_io_vec->bv_page->mapping->host; | |
8018 | ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize); | |
8019 | ||
8b110e39 MX |
8020 | bio_for_each_segment_all(bvec, bio, i) { |
8021 | ret = __readpage_endio_check(done->inode, io_bio, i, | |
2dabb324 CR |
8022 | bvec->bv_page, bvec->bv_offset, |
8023 | done->start, bvec->bv_len); | |
8b110e39 MX |
8024 | if (!ret) |
8025 | clean_io_failure(done->inode, done->start, | |
2dabb324 | 8026 | bvec->bv_page, bvec->bv_offset); |
8b110e39 MX |
8027 | else |
8028 | uptodate = 0; | |
8029 | } | |
8030 | ||
8031 | done->uptodate = uptodate; | |
8032 | end: | |
8033 | complete(&done->done); | |
8034 | bio_put(bio); | |
8035 | } | |
8036 | ||
8037 | static int __btrfs_subio_endio_read(struct inode *inode, | |
8038 | struct btrfs_io_bio *io_bio, int err) | |
8039 | { | |
2dabb324 | 8040 | struct btrfs_fs_info *fs_info; |
8b110e39 MX |
8041 | struct bio_vec *bvec; |
8042 | struct btrfs_retry_complete done; | |
8043 | u64 start; | |
8044 | u64 offset = 0; | |
2dabb324 CR |
8045 | u32 sectorsize; |
8046 | int nr_sectors; | |
8047 | unsigned int pgoff; | |
8048 | int csum_pos; | |
8b110e39 MX |
8049 | int i; |
8050 | int ret; | |
dc380aea | 8051 | |
2dabb324 CR |
8052 | fs_info = BTRFS_I(inode)->root->fs_info; |
8053 | sectorsize = BTRFS_I(inode)->root->sectorsize; | |
8054 | ||
8b110e39 | 8055 | err = 0; |
c1dc0896 | 8056 | start = io_bio->logical; |
8b110e39 MX |
8057 | done.inode = inode; |
8058 | ||
c1dc0896 | 8059 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
2dabb324 CR |
8060 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len); |
8061 | ||
8062 | pgoff = bvec->bv_offset; | |
8063 | next_block: | |
8064 | csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset); | |
8065 | ret = __readpage_endio_check(inode, io_bio, csum_pos, | |
8066 | bvec->bv_page, pgoff, start, | |
8067 | sectorsize); | |
8b110e39 MX |
8068 | if (likely(!ret)) |
8069 | goto next; | |
8070 | try_again: | |
8071 | done.uptodate = 0; | |
8072 | done.start = start; | |
8073 | init_completion(&done.done); | |
8074 | ||
2dabb324 CR |
8075 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, |
8076 | pgoff, start, start + sectorsize - 1, | |
8077 | io_bio->mirror_num, | |
8078 | btrfs_retry_endio, &done); | |
8b110e39 MX |
8079 | if (ret) { |
8080 | err = ret; | |
8081 | goto next; | |
8082 | } | |
8083 | ||
8084 | wait_for_completion(&done.done); | |
8085 | ||
8086 | if (!done.uptodate) { | |
8087 | /* We might have another mirror, so try again */ | |
8088 | goto try_again; | |
8089 | } | |
8090 | next: | |
2dabb324 CR |
8091 | offset += sectorsize; |
8092 | start += sectorsize; | |
8093 | ||
8094 | ASSERT(nr_sectors); | |
8095 | ||
8096 | if (--nr_sectors) { | |
8097 | pgoff += sectorsize; | |
8098 | goto next_block; | |
8099 | } | |
2c30c71b | 8100 | } |
c1dc0896 MX |
8101 | |
8102 | return err; | |
8103 | } | |
8104 | ||
8b110e39 MX |
8105 | static int btrfs_subio_endio_read(struct inode *inode, |
8106 | struct btrfs_io_bio *io_bio, int err) | |
8107 | { | |
8108 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8109 | ||
8110 | if (skip_csum) { | |
8111 | if (unlikely(err)) | |
8112 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
8113 | else | |
8114 | return 0; | |
8115 | } else { | |
8116 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
8117 | } | |
8118 | } | |
8119 | ||
4246a0b6 | 8120 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
8121 | { |
8122 | struct btrfs_dio_private *dip = bio->bi_private; | |
8123 | struct inode *inode = dip->inode; | |
8124 | struct bio *dio_bio; | |
8125 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4246a0b6 | 8126 | int err = bio->bi_error; |
c1dc0896 | 8127 | |
8b110e39 MX |
8128 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
8129 | err = btrfs_subio_endio_read(inode, io_bio, err); | |
c1dc0896 | 8130 | |
4b46fce2 | 8131 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 8132 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 8133 | dio_bio = dip->dio_bio; |
4b46fce2 | 8134 | |
4b46fce2 | 8135 | kfree(dip); |
c0da7aa1 | 8136 | |
1636d1d7 | 8137 | dio_bio->bi_error = bio->bi_error; |
4246a0b6 | 8138 | dio_end_io(dio_bio, bio->bi_error); |
23ea8e5a MX |
8139 | |
8140 | if (io_bio->end_io) | |
8141 | io_bio->end_io(io_bio, err); | |
9be3395b | 8142 | bio_put(bio); |
4b46fce2 JB |
8143 | } |
8144 | ||
14543774 FM |
8145 | static void btrfs_endio_direct_write_update_ordered(struct inode *inode, |
8146 | const u64 offset, | |
8147 | const u64 bytes, | |
8148 | const int uptodate) | |
4b46fce2 | 8149 | { |
4b46fce2 | 8150 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4b46fce2 | 8151 | struct btrfs_ordered_extent *ordered = NULL; |
14543774 FM |
8152 | u64 ordered_offset = offset; |
8153 | u64 ordered_bytes = bytes; | |
4b46fce2 JB |
8154 | int ret; |
8155 | ||
163cf09c CM |
8156 | again: |
8157 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
8158 | &ordered_offset, | |
4246a0b6 | 8159 | ordered_bytes, |
14543774 | 8160 | uptodate); |
4b46fce2 | 8161 | if (!ret) |
163cf09c | 8162 | goto out_test; |
4b46fce2 | 8163 | |
9e0af237 LB |
8164 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
8165 | finish_ordered_fn, NULL, NULL); | |
fccb5d86 QW |
8166 | btrfs_queue_work(root->fs_info->endio_write_workers, |
8167 | &ordered->work); | |
163cf09c CM |
8168 | out_test: |
8169 | /* | |
8170 | * our bio might span multiple ordered extents. If we haven't | |
8171 | * completed the accounting for the whole dio, go back and try again | |
8172 | */ | |
14543774 FM |
8173 | if (ordered_offset < offset + bytes) { |
8174 | ordered_bytes = offset + bytes - ordered_offset; | |
5fd02043 | 8175 | ordered = NULL; |
163cf09c CM |
8176 | goto again; |
8177 | } | |
14543774 FM |
8178 | } |
8179 | ||
8180 | static void btrfs_endio_direct_write(struct bio *bio) | |
8181 | { | |
8182 | struct btrfs_dio_private *dip = bio->bi_private; | |
8183 | struct bio *dio_bio = dip->dio_bio; | |
8184 | ||
8185 | btrfs_endio_direct_write_update_ordered(dip->inode, | |
8186 | dip->logical_offset, | |
8187 | dip->bytes, | |
8188 | !bio->bi_error); | |
4b46fce2 | 8189 | |
4b46fce2 | 8190 | kfree(dip); |
c0da7aa1 | 8191 | |
1636d1d7 | 8192 | dio_bio->bi_error = bio->bi_error; |
4246a0b6 | 8193 | dio_end_io(dio_bio, bio->bi_error); |
9be3395b | 8194 | bio_put(bio); |
4b46fce2 JB |
8195 | } |
8196 | ||
eaf25d93 CM |
8197 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
8198 | struct bio *bio, int mirror_num, | |
8199 | unsigned long bio_flags, u64 offset) | |
8200 | { | |
8201 | int ret; | |
8202 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
8203 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); | |
79787eaa | 8204 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8205 | return 0; |
8206 | } | |
8207 | ||
4246a0b6 | 8208 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8209 | { |
8210 | struct btrfs_dio_private *dip = bio->bi_private; | |
4246a0b6 | 8211 | int err = bio->bi_error; |
e65e1535 | 8212 | |
8b110e39 MX |
8213 | if (err) |
8214 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
8215 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", | |
8216 | btrfs_ino(dip->inode), bio->bi_rw, | |
8217 | (unsigned long long)bio->bi_iter.bi_sector, | |
8218 | bio->bi_iter.bi_size, err); | |
8219 | ||
8220 | if (dip->subio_endio) | |
8221 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8222 | |
8223 | if (err) { | |
e65e1535 MX |
8224 | dip->errors = 1; |
8225 | ||
8226 | /* | |
8227 | * before atomic variable goto zero, we must make sure | |
8228 | * dip->errors is perceived to be set. | |
8229 | */ | |
4e857c58 | 8230 | smp_mb__before_atomic(); |
e65e1535 MX |
8231 | } |
8232 | ||
8233 | /* if there are more bios still pending for this dio, just exit */ | |
8234 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8235 | goto out; | |
8236 | ||
9be3395b | 8237 | if (dip->errors) { |
e65e1535 | 8238 | bio_io_error(dip->orig_bio); |
9be3395b | 8239 | } else { |
4246a0b6 CH |
8240 | dip->dio_bio->bi_error = 0; |
8241 | bio_endio(dip->orig_bio); | |
e65e1535 MX |
8242 | } |
8243 | out: | |
8244 | bio_put(bio); | |
8245 | } | |
8246 | ||
8247 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, | |
8248 | u64 first_sector, gfp_t gfp_flags) | |
8249 | { | |
da2f0f74 | 8250 | struct bio *bio; |
22365979 | 8251 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
da2f0f74 CM |
8252 | if (bio) |
8253 | bio_associate_current(bio); | |
8254 | return bio; | |
e65e1535 MX |
8255 | } |
8256 | ||
c1dc0896 MX |
8257 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
8258 | struct inode *inode, | |
8259 | struct btrfs_dio_private *dip, | |
8260 | struct bio *bio, | |
8261 | u64 file_offset) | |
8262 | { | |
8263 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8264 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
8265 | int ret; | |
8266 | ||
8267 | /* | |
8268 | * We load all the csum data we need when we submit | |
8269 | * the first bio to reduce the csum tree search and | |
8270 | * contention. | |
8271 | */ | |
8272 | if (dip->logical_offset == file_offset) { | |
8273 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, | |
8274 | file_offset); | |
8275 | if (ret) | |
8276 | return ret; | |
8277 | } | |
8278 | ||
8279 | if (bio == dip->orig_bio) | |
8280 | return 0; | |
8281 | ||
8282 | file_offset -= dip->logical_offset; | |
8283 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8284 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8285 | ||
8286 | return 0; | |
8287 | } | |
8288 | ||
e65e1535 MX |
8289 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
8290 | int rw, u64 file_offset, int skip_sum, | |
c329861d | 8291 | int async_submit) |
e65e1535 | 8292 | { |
facc8a22 | 8293 | struct btrfs_dio_private *dip = bio->bi_private; |
e65e1535 MX |
8294 | int write = rw & REQ_WRITE; |
8295 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
8296 | int ret; | |
8297 | ||
b812ce28 JB |
8298 | if (async_submit) |
8299 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8300 | ||
e65e1535 | 8301 | bio_get(bio); |
5fd02043 JB |
8302 | |
8303 | if (!write) { | |
bfebd8b5 DS |
8304 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
8305 | BTRFS_WQ_ENDIO_DATA); | |
5fd02043 JB |
8306 | if (ret) |
8307 | goto err; | |
8308 | } | |
e65e1535 | 8309 | |
1ae39938 JB |
8310 | if (skip_sum) |
8311 | goto map; | |
8312 | ||
8313 | if (write && async_submit) { | |
e65e1535 MX |
8314 | ret = btrfs_wq_submit_bio(root->fs_info, |
8315 | inode, rw, bio, 0, 0, | |
8316 | file_offset, | |
8317 | __btrfs_submit_bio_start_direct_io, | |
8318 | __btrfs_submit_bio_done); | |
8319 | goto err; | |
1ae39938 JB |
8320 | } else if (write) { |
8321 | /* | |
8322 | * If we aren't doing async submit, calculate the csum of the | |
8323 | * bio now. | |
8324 | */ | |
8325 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); | |
8326 | if (ret) | |
8327 | goto err; | |
23ea8e5a | 8328 | } else { |
c1dc0896 MX |
8329 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
8330 | file_offset); | |
c2db1073 TI |
8331 | if (ret) |
8332 | goto err; | |
8333 | } | |
1ae39938 JB |
8334 | map: |
8335 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); | |
e65e1535 MX |
8336 | err: |
8337 | bio_put(bio); | |
8338 | return ret; | |
8339 | } | |
8340 | ||
8341 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, | |
8342 | int skip_sum) | |
8343 | { | |
8344 | struct inode *inode = dip->inode; | |
8345 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
e65e1535 MX |
8346 | struct bio *bio; |
8347 | struct bio *orig_bio = dip->orig_bio; | |
8348 | struct bio_vec *bvec = orig_bio->bi_io_vec; | |
4f024f37 | 8349 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 MX |
8350 | u64 file_offset = dip->logical_offset; |
8351 | u64 submit_len = 0; | |
8352 | u64 map_length; | |
5f4dc8fc | 8353 | u32 blocksize = root->sectorsize; |
1ae39938 | 8354 | int async_submit = 0; |
5f4dc8fc CR |
8355 | int nr_sectors; |
8356 | int ret; | |
8357 | int i; | |
e65e1535 | 8358 | |
4f024f37 | 8359 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8360 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
e65e1535 | 8361 | &map_length, NULL, 0); |
7a5c3c9b | 8362 | if (ret) |
e65e1535 | 8363 | return -EIO; |
facc8a22 | 8364 | |
4f024f37 | 8365 | if (map_length >= orig_bio->bi_iter.bi_size) { |
02f57c7a | 8366 | bio = orig_bio; |
c1dc0896 | 8367 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8368 | goto submit; |
8369 | } | |
8370 | ||
53b381b3 | 8371 | /* async crcs make it difficult to collect full stripe writes. */ |
ffe2d203 | 8372 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8373 | async_submit = 0; |
8374 | else | |
8375 | async_submit = 1; | |
8376 | ||
02f57c7a JB |
8377 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
8378 | if (!bio) | |
8379 | return -ENOMEM; | |
7a5c3c9b | 8380 | |
02f57c7a JB |
8381 | bio->bi_private = dip; |
8382 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8383 | btrfs_io_bio(bio)->logical = file_offset; |
02f57c7a JB |
8384 | atomic_inc(&dip->pending_bios); |
8385 | ||
e65e1535 | 8386 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
5f4dc8fc CR |
8387 | nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len); |
8388 | i = 0; | |
8389 | next_block: | |
8390 | if (unlikely(map_length < submit_len + blocksize || | |
8391 | bio_add_page(bio, bvec->bv_page, blocksize, | |
8392 | bvec->bv_offset + (i * blocksize)) < blocksize)) { | |
e65e1535 MX |
8393 | /* |
8394 | * inc the count before we submit the bio so | |
8395 | * we know the end IO handler won't happen before | |
8396 | * we inc the count. Otherwise, the dip might get freed | |
8397 | * before we're done setting it up | |
8398 | */ | |
8399 | atomic_inc(&dip->pending_bios); | |
8400 | ret = __btrfs_submit_dio_bio(bio, inode, rw, | |
8401 | file_offset, skip_sum, | |
c329861d | 8402 | async_submit); |
e65e1535 MX |
8403 | if (ret) { |
8404 | bio_put(bio); | |
8405 | atomic_dec(&dip->pending_bios); | |
8406 | goto out_err; | |
8407 | } | |
8408 | ||
e65e1535 MX |
8409 | start_sector += submit_len >> 9; |
8410 | file_offset += submit_len; | |
8411 | ||
8412 | submit_len = 0; | |
e65e1535 MX |
8413 | |
8414 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, | |
8415 | start_sector, GFP_NOFS); | |
8416 | if (!bio) | |
8417 | goto out_err; | |
8418 | bio->bi_private = dip; | |
8419 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8420 | btrfs_io_bio(bio)->logical = file_offset; |
e65e1535 | 8421 | |
4f024f37 | 8422 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8423 | ret = btrfs_map_block(root->fs_info, rw, |
3ec706c8 | 8424 | start_sector << 9, |
e65e1535 MX |
8425 | &map_length, NULL, 0); |
8426 | if (ret) { | |
8427 | bio_put(bio); | |
8428 | goto out_err; | |
8429 | } | |
5f4dc8fc CR |
8430 | |
8431 | goto next_block; | |
e65e1535 | 8432 | } else { |
5f4dc8fc CR |
8433 | submit_len += blocksize; |
8434 | if (--nr_sectors) { | |
8435 | i++; | |
8436 | goto next_block; | |
8437 | } | |
e65e1535 MX |
8438 | bvec++; |
8439 | } | |
8440 | } | |
8441 | ||
02f57c7a | 8442 | submit: |
e65e1535 | 8443 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
c329861d | 8444 | async_submit); |
e65e1535 MX |
8445 | if (!ret) |
8446 | return 0; | |
8447 | ||
8448 | bio_put(bio); | |
8449 | out_err: | |
8450 | dip->errors = 1; | |
8451 | /* | |
8452 | * before atomic variable goto zero, we must | |
8453 | * make sure dip->errors is perceived to be set. | |
8454 | */ | |
4e857c58 | 8455 | smp_mb__before_atomic(); |
e65e1535 MX |
8456 | if (atomic_dec_and_test(&dip->pending_bios)) |
8457 | bio_io_error(dip->orig_bio); | |
8458 | ||
8459 | /* bio_end_io() will handle error, so we needn't return it */ | |
8460 | return 0; | |
8461 | } | |
8462 | ||
9be3395b CM |
8463 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
8464 | struct inode *inode, loff_t file_offset) | |
4b46fce2 | 8465 | { |
61de718f FM |
8466 | struct btrfs_dio_private *dip = NULL; |
8467 | struct bio *io_bio = NULL; | |
23ea8e5a | 8468 | struct btrfs_io_bio *btrfs_bio; |
4b46fce2 | 8469 | int skip_sum; |
7b6d91da | 8470 | int write = rw & REQ_WRITE; |
4b46fce2 JB |
8471 | int ret = 0; |
8472 | ||
8473 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8474 | ||
9be3395b | 8475 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
9be3395b CM |
8476 | if (!io_bio) { |
8477 | ret = -ENOMEM; | |
8478 | goto free_ordered; | |
8479 | } | |
8480 | ||
c1dc0896 | 8481 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8482 | if (!dip) { |
8483 | ret = -ENOMEM; | |
61de718f | 8484 | goto free_ordered; |
4b46fce2 | 8485 | } |
4b46fce2 | 8486 | |
9be3395b | 8487 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8488 | dip->inode = inode; |
8489 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8490 | dip->bytes = dio_bio->bi_iter.bi_size; |
8491 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
9be3395b | 8492 | io_bio->bi_private = dip; |
9be3395b CM |
8493 | dip->orig_bio = io_bio; |
8494 | dip->dio_bio = dio_bio; | |
e65e1535 | 8495 | atomic_set(&dip->pending_bios, 0); |
c1dc0896 MX |
8496 | btrfs_bio = btrfs_io_bio(io_bio); |
8497 | btrfs_bio->logical = file_offset; | |
4b46fce2 | 8498 | |
c1dc0896 | 8499 | if (write) { |
9be3395b | 8500 | io_bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8501 | } else { |
9be3395b | 8502 | io_bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8503 | dip->subio_endio = btrfs_subio_endio_read; |
8504 | } | |
4b46fce2 | 8505 | |
f28a4928 FM |
8506 | /* |
8507 | * Reset the range for unsubmitted ordered extents (to a 0 length range) | |
8508 | * even if we fail to submit a bio, because in such case we do the | |
8509 | * corresponding error handling below and it must not be done a second | |
8510 | * time by btrfs_direct_IO(). | |
8511 | */ | |
8512 | if (write) { | |
8513 | struct btrfs_dio_data *dio_data = current->journal_info; | |
8514 | ||
8515 | dio_data->unsubmitted_oe_range_end = dip->logical_offset + | |
8516 | dip->bytes; | |
8517 | dio_data->unsubmitted_oe_range_start = | |
8518 | dio_data->unsubmitted_oe_range_end; | |
8519 | } | |
8520 | ||
e65e1535 MX |
8521 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
8522 | if (!ret) | |
eaf25d93 | 8523 | return; |
9be3395b | 8524 | |
23ea8e5a MX |
8525 | if (btrfs_bio->end_io) |
8526 | btrfs_bio->end_io(btrfs_bio, ret); | |
9be3395b | 8527 | |
4b46fce2 JB |
8528 | free_ordered: |
8529 | /* | |
61de718f FM |
8530 | * If we arrived here it means either we failed to submit the dip |
8531 | * or we either failed to clone the dio_bio or failed to allocate the | |
8532 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8533 | * call bio_endio against our io_bio so that we get proper resource | |
8534 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8535 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8536 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8537 | */ |
61de718f | 8538 | if (io_bio && dip) { |
4246a0b6 CH |
8539 | io_bio->bi_error = -EIO; |
8540 | bio_endio(io_bio); | |
61de718f FM |
8541 | /* |
8542 | * The end io callbacks free our dip, do the final put on io_bio | |
8543 | * and all the cleanup and final put for dio_bio (through | |
8544 | * dio_end_io()). | |
8545 | */ | |
8546 | dip = NULL; | |
8547 | io_bio = NULL; | |
8548 | } else { | |
14543774 FM |
8549 | if (write) |
8550 | btrfs_endio_direct_write_update_ordered(inode, | |
8551 | file_offset, | |
8552 | dio_bio->bi_iter.bi_size, | |
8553 | 0); | |
8554 | else | |
61de718f FM |
8555 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, |
8556 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
14543774 | 8557 | |
4246a0b6 | 8558 | dio_bio->bi_error = -EIO; |
61de718f FM |
8559 | /* |
8560 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8561 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8562 | */ | |
8563 | dio_end_io(dio_bio, ret); | |
4b46fce2 | 8564 | } |
61de718f FM |
8565 | if (io_bio) |
8566 | bio_put(io_bio); | |
8567 | kfree(dip); | |
4b46fce2 JB |
8568 | } |
8569 | ||
6f673763 | 8570 | static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb, |
28060d5d | 8571 | const struct iov_iter *iter, loff_t offset) |
5a5f79b5 CM |
8572 | { |
8573 | int seg; | |
a1b75f7d | 8574 | int i; |
5a5f79b5 CM |
8575 | unsigned blocksize_mask = root->sectorsize - 1; |
8576 | ssize_t retval = -EINVAL; | |
5a5f79b5 CM |
8577 | |
8578 | if (offset & blocksize_mask) | |
8579 | goto out; | |
8580 | ||
28060d5d AV |
8581 | if (iov_iter_alignment(iter) & blocksize_mask) |
8582 | goto out; | |
a1b75f7d | 8583 | |
28060d5d | 8584 | /* If this is a write we don't need to check anymore */ |
6f673763 | 8585 | if (iov_iter_rw(iter) == WRITE) |
28060d5d AV |
8586 | return 0; |
8587 | /* | |
8588 | * Check to make sure we don't have duplicate iov_base's in this | |
8589 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8590 | * when reading back. | |
8591 | */ | |
8592 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8593 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8594 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8595 | goto out; |
8596 | } | |
5a5f79b5 CM |
8597 | } |
8598 | retval = 0; | |
8599 | out: | |
8600 | return retval; | |
8601 | } | |
eb838e73 | 8602 | |
c8b8e32d | 8603 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
16432985 | 8604 | { |
4b46fce2 JB |
8605 | struct file *file = iocb->ki_filp; |
8606 | struct inode *inode = file->f_mapping->host; | |
50745b0a | 8607 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8608 | struct btrfs_dio_data dio_data = { 0 }; | |
c8b8e32d | 8609 | loff_t offset = iocb->ki_pos; |
0934856d | 8610 | size_t count = 0; |
2e60a51e | 8611 | int flags = 0; |
38851cc1 MX |
8612 | bool wakeup = true; |
8613 | bool relock = false; | |
0934856d | 8614 | ssize_t ret; |
4b46fce2 | 8615 | |
6f673763 | 8616 | if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset)) |
5a5f79b5 | 8617 | return 0; |
3f7c579c | 8618 | |
fe0f07d0 | 8619 | inode_dio_begin(inode); |
4e857c58 | 8620 | smp_mb__after_atomic(); |
38851cc1 | 8621 | |
0e267c44 | 8622 | /* |
41bd9ca4 MX |
8623 | * The generic stuff only does filemap_write_and_wait_range, which |
8624 | * isn't enough if we've written compressed pages to this area, so | |
8625 | * we need to flush the dirty pages again to make absolutely sure | |
8626 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8627 | */ |
a6cbcd4a | 8628 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8629 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8630 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8631 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8632 | offset + count - 1); | |
0e267c44 | 8633 | |
6f673763 | 8634 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8635 | /* |
8636 | * If the write DIO is beyond the EOF, we need update | |
8637 | * the isize, but it is protected by i_mutex. So we can | |
8638 | * not unlock the i_mutex at this case. | |
8639 | */ | |
8640 | if (offset + count <= inode->i_size) { | |
5955102c | 8641 | inode_unlock(inode); |
38851cc1 MX |
8642 | relock = true; |
8643 | } | |
7cf5b976 | 8644 | ret = btrfs_delalloc_reserve_space(inode, offset, count); |
0934856d | 8645 | if (ret) |
38851cc1 | 8646 | goto out; |
50745b0a | 8647 | dio_data.outstanding_extents = div64_u64(count + |
e1cbbfa5 JB |
8648 | BTRFS_MAX_EXTENT_SIZE - 1, |
8649 | BTRFS_MAX_EXTENT_SIZE); | |
8650 | ||
8651 | /* | |
8652 | * We need to know how many extents we reserved so that we can | |
8653 | * do the accounting properly if we go over the number we | |
8654 | * originally calculated. Abuse current->journal_info for this. | |
8655 | */ | |
50745b0a | 8656 | dio_data.reserve = round_up(count, root->sectorsize); |
f28a4928 FM |
8657 | dio_data.unsubmitted_oe_range_start = (u64)offset; |
8658 | dio_data.unsubmitted_oe_range_end = (u64)offset; | |
50745b0a | 8659 | current->journal_info = &dio_data; |
ee39b432 DS |
8660 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8661 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8662 | inode_dio_end(inode); |
38851cc1 MX |
8663 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8664 | wakeup = false; | |
0934856d MX |
8665 | } |
8666 | ||
17f8c842 OS |
8667 | ret = __blockdev_direct_IO(iocb, inode, |
8668 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, | |
c8b8e32d | 8669 | iter, btrfs_get_blocks_direct, NULL, |
17f8c842 | 8670 | btrfs_submit_direct, flags); |
6f673763 | 8671 | if (iov_iter_rw(iter) == WRITE) { |
e1cbbfa5 | 8672 | current->journal_info = NULL; |
ddba1bfc | 8673 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8674 | if (dio_data.reserve) |
7cf5b976 QW |
8675 | btrfs_delalloc_release_space(inode, offset, |
8676 | dio_data.reserve); | |
f28a4928 FM |
8677 | /* |
8678 | * On error we might have left some ordered extents | |
8679 | * without submitting corresponding bios for them, so | |
8680 | * cleanup them up to avoid other tasks getting them | |
8681 | * and waiting for them to complete forever. | |
8682 | */ | |
8683 | if (dio_data.unsubmitted_oe_range_start < | |
8684 | dio_data.unsubmitted_oe_range_end) | |
8685 | btrfs_endio_direct_write_update_ordered(inode, | |
8686 | dio_data.unsubmitted_oe_range_start, | |
8687 | dio_data.unsubmitted_oe_range_end - | |
8688 | dio_data.unsubmitted_oe_range_start, | |
8689 | 0); | |
ddba1bfc | 8690 | } else if (ret >= 0 && (size_t)ret < count) |
7cf5b976 QW |
8691 | btrfs_delalloc_release_space(inode, offset, |
8692 | count - (size_t)ret); | |
0934856d | 8693 | } |
38851cc1 | 8694 | out: |
2e60a51e | 8695 | if (wakeup) |
fe0f07d0 | 8696 | inode_dio_end(inode); |
38851cc1 | 8697 | if (relock) |
5955102c | 8698 | inode_lock(inode); |
0934856d MX |
8699 | |
8700 | return ret; | |
16432985 CM |
8701 | } |
8702 | ||
05dadc09 TI |
8703 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8704 | ||
1506fcc8 YS |
8705 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8706 | __u64 start, __u64 len) | |
8707 | { | |
05dadc09 TI |
8708 | int ret; |
8709 | ||
8710 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8711 | if (ret) | |
8712 | return ret; | |
8713 | ||
ec29ed5b | 8714 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8715 | } |
8716 | ||
a52d9a80 | 8717 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8718 | { |
d1310b2e CM |
8719 | struct extent_io_tree *tree; |
8720 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8721 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8722 | } |
1832a6d5 | 8723 | |
a52d9a80 | 8724 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8725 | { |
d1310b2e | 8726 | struct extent_io_tree *tree; |
be7bd730 JB |
8727 | struct inode *inode = page->mapping->host; |
8728 | int ret; | |
b888db2b CM |
8729 | |
8730 | if (current->flags & PF_MEMALLOC) { | |
8731 | redirty_page_for_writepage(wbc, page); | |
8732 | unlock_page(page); | |
8733 | return 0; | |
8734 | } | |
be7bd730 JB |
8735 | |
8736 | /* | |
8737 | * If we are under memory pressure we will call this directly from the | |
8738 | * VM, we need to make sure we have the inode referenced for the ordered | |
8739 | * extent. If not just return like we didn't do anything. | |
8740 | */ | |
8741 | if (!igrab(inode)) { | |
8742 | redirty_page_for_writepage(wbc, page); | |
8743 | return AOP_WRITEPAGE_ACTIVATE; | |
8744 | } | |
d1310b2e | 8745 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
be7bd730 JB |
8746 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
8747 | btrfs_add_delayed_iput(inode); | |
8748 | return ret; | |
9ebefb18 CM |
8749 | } |
8750 | ||
48a3b636 ES |
8751 | static int btrfs_writepages(struct address_space *mapping, |
8752 | struct writeback_control *wbc) | |
b293f02e | 8753 | { |
d1310b2e | 8754 | struct extent_io_tree *tree; |
771ed689 | 8755 | |
d1310b2e | 8756 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8757 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8758 | } | |
8759 | ||
3ab2fb5a CM |
8760 | static int |
8761 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8762 | struct list_head *pages, unsigned nr_pages) | |
8763 | { | |
d1310b2e CM |
8764 | struct extent_io_tree *tree; |
8765 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8766 | return extent_readpages(tree, mapping, pages, nr_pages, |
8767 | btrfs_get_extent); | |
8768 | } | |
e6dcd2dc | 8769 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8770 | { |
d1310b2e CM |
8771 | struct extent_io_tree *tree; |
8772 | struct extent_map_tree *map; | |
a52d9a80 | 8773 | int ret; |
8c2383c3 | 8774 | |
d1310b2e CM |
8775 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8776 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8777 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8778 | if (ret == 1) { |
8779 | ClearPagePrivate(page); | |
8780 | set_page_private(page, 0); | |
09cbfeaf | 8781 | put_page(page); |
39279cc3 | 8782 | } |
a52d9a80 | 8783 | return ret; |
39279cc3 CM |
8784 | } |
8785 | ||
e6dcd2dc CM |
8786 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8787 | { | |
98509cfc CM |
8788 | if (PageWriteback(page) || PageDirty(page)) |
8789 | return 0; | |
b335b003 | 8790 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
e6dcd2dc CM |
8791 | } |
8792 | ||
d47992f8 LC |
8793 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8794 | unsigned int length) | |
39279cc3 | 8795 | { |
5fd02043 | 8796 | struct inode *inode = page->mapping->host; |
d1310b2e | 8797 | struct extent_io_tree *tree; |
e6dcd2dc | 8798 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8799 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 8800 | u64 page_start = page_offset(page); |
09cbfeaf | 8801 | u64 page_end = page_start + PAGE_SIZE - 1; |
dbfdb6d1 CR |
8802 | u64 start; |
8803 | u64 end; | |
131e404a | 8804 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8805 | |
8b62b72b CM |
8806 | /* |
8807 | * we have the page locked, so new writeback can't start, | |
8808 | * and the dirty bit won't be cleared while we are here. | |
8809 | * | |
8810 | * Wait for IO on this page so that we can safely clear | |
8811 | * the PagePrivate2 bit and do ordered accounting | |
8812 | */ | |
e6dcd2dc | 8813 | wait_on_page_writeback(page); |
8b62b72b | 8814 | |
5fd02043 | 8815 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8816 | if (offset) { |
8817 | btrfs_releasepage(page, GFP_NOFS); | |
8818 | return; | |
8819 | } | |
131e404a FDBM |
8820 | |
8821 | if (!inode_evicting) | |
ff13db41 | 8822 | lock_extent_bits(tree, page_start, page_end, &cached_state); |
dbfdb6d1 CR |
8823 | again: |
8824 | start = page_start; | |
8825 | ordered = btrfs_lookup_ordered_range(inode, start, | |
8826 | page_end - start + 1); | |
e6dcd2dc | 8827 | if (ordered) { |
dbfdb6d1 | 8828 | end = min(page_end, ordered->file_offset + ordered->len - 1); |
eb84ae03 CM |
8829 | /* |
8830 | * IO on this page will never be started, so we need | |
8831 | * to account for any ordered extents now | |
8832 | */ | |
131e404a | 8833 | if (!inode_evicting) |
dbfdb6d1 | 8834 | clear_extent_bit(tree, start, end, |
131e404a FDBM |
8835 | EXTENT_DIRTY | EXTENT_DELALLOC | |
8836 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | | |
8837 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8838 | GFP_NOFS); | |
8b62b72b CM |
8839 | /* |
8840 | * whoever cleared the private bit is responsible | |
8841 | * for the finish_ordered_io | |
8842 | */ | |
77cef2ec JB |
8843 | if (TestClearPagePrivate2(page)) { |
8844 | struct btrfs_ordered_inode_tree *tree; | |
8845 | u64 new_len; | |
8846 | ||
8847 | tree = &BTRFS_I(inode)->ordered_tree; | |
8848 | ||
8849 | spin_lock_irq(&tree->lock); | |
8850 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
dbfdb6d1 | 8851 | new_len = start - ordered->file_offset; |
77cef2ec JB |
8852 | if (new_len < ordered->truncated_len) |
8853 | ordered->truncated_len = new_len; | |
8854 | spin_unlock_irq(&tree->lock); | |
8855 | ||
8856 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
dbfdb6d1 CR |
8857 | start, |
8858 | end - start + 1, 1)) | |
77cef2ec | 8859 | btrfs_finish_ordered_io(ordered); |
8b62b72b | 8860 | } |
e6dcd2dc | 8861 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8862 | if (!inode_evicting) { |
8863 | cached_state = NULL; | |
dbfdb6d1 | 8864 | lock_extent_bits(tree, start, end, |
131e404a FDBM |
8865 | &cached_state); |
8866 | } | |
dbfdb6d1 CR |
8867 | |
8868 | start = end + 1; | |
8869 | if (start < page_end) | |
8870 | goto again; | |
131e404a FDBM |
8871 | } |
8872 | ||
b9d0b389 QW |
8873 | /* |
8874 | * Qgroup reserved space handler | |
8875 | * Page here will be either | |
8876 | * 1) Already written to disk | |
8877 | * In this case, its reserved space is released from data rsv map | |
8878 | * and will be freed by delayed_ref handler finally. | |
8879 | * So even we call qgroup_free_data(), it won't decrease reserved | |
8880 | * space. | |
8881 | * 2) Not written to disk | |
8882 | * This means the reserved space should be freed here. | |
8883 | */ | |
09cbfeaf | 8884 | btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE); |
131e404a FDBM |
8885 | if (!inode_evicting) { |
8886 | clear_extent_bit(tree, page_start, page_end, | |
8887 | EXTENT_LOCKED | EXTENT_DIRTY | | |
8888 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
8889 | EXTENT_DEFRAG, 1, 1, | |
8890 | &cached_state, GFP_NOFS); | |
8891 | ||
8892 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8893 | } |
e6dcd2dc | 8894 | |
4a096752 | 8895 | ClearPageChecked(page); |
9ad6b7bc | 8896 | if (PagePrivate(page)) { |
9ad6b7bc CM |
8897 | ClearPagePrivate(page); |
8898 | set_page_private(page, 0); | |
09cbfeaf | 8899 | put_page(page); |
9ad6b7bc | 8900 | } |
39279cc3 CM |
8901 | } |
8902 | ||
9ebefb18 CM |
8903 | /* |
8904 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8905 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8906 | * be careful to check for EOF conditions here. We set the page up correctly | |
8907 | * for a written page which means we get ENOSPC checking when writing into | |
8908 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8909 | * support these features. | |
8910 | * | |
8911 | * We are not allowed to take the i_mutex here so we have to play games to | |
8912 | * protect against truncate races as the page could now be beyond EOF. Because | |
8913 | * vmtruncate() writes the inode size before removing pages, once we have the | |
8914 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
8915 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
8916 | * unlock the page. | |
8917 | */ | |
c2ec175c | 8918 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
9ebefb18 | 8919 | { |
c2ec175c | 8920 | struct page *page = vmf->page; |
496ad9aa | 8921 | struct inode *inode = file_inode(vma->vm_file); |
1832a6d5 | 8922 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
8923 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8924 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8925 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8926 | char *kaddr; |
8927 | unsigned long zero_start; | |
9ebefb18 | 8928 | loff_t size; |
1832a6d5 | 8929 | int ret; |
9998eb70 | 8930 | int reserved = 0; |
d0b7da88 | 8931 | u64 reserved_space; |
a52d9a80 | 8932 | u64 page_start; |
e6dcd2dc | 8933 | u64 page_end; |
d0b7da88 CR |
8934 | u64 end; |
8935 | ||
09cbfeaf | 8936 | reserved_space = PAGE_SIZE; |
9ebefb18 | 8937 | |
b2b5ef5c | 8938 | sb_start_pagefault(inode->i_sb); |
df480633 | 8939 | page_start = page_offset(page); |
09cbfeaf | 8940 | page_end = page_start + PAGE_SIZE - 1; |
d0b7da88 | 8941 | end = page_end; |
df480633 | 8942 | |
d0b7da88 CR |
8943 | /* |
8944 | * Reserving delalloc space after obtaining the page lock can lead to | |
8945 | * deadlock. For example, if a dirty page is locked by this function | |
8946 | * and the call to btrfs_delalloc_reserve_space() ends up triggering | |
8947 | * dirty page write out, then the btrfs_writepage() function could | |
8948 | * end up waiting indefinitely to get a lock on the page currently | |
8949 | * being processed by btrfs_page_mkwrite() function. | |
8950 | */ | |
7cf5b976 | 8951 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
d0b7da88 | 8952 | reserved_space); |
9998eb70 | 8953 | if (!ret) { |
e41f941a | 8954 | ret = file_update_time(vma->vm_file); |
9998eb70 CM |
8955 | reserved = 1; |
8956 | } | |
56a76f82 NP |
8957 | if (ret) { |
8958 | if (ret == -ENOMEM) | |
8959 | ret = VM_FAULT_OOM; | |
8960 | else /* -ENOSPC, -EIO, etc */ | |
8961 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
8962 | if (reserved) |
8963 | goto out; | |
8964 | goto out_noreserve; | |
56a76f82 | 8965 | } |
1832a6d5 | 8966 | |
56a76f82 | 8967 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8968 | again: |
9ebefb18 | 8969 | lock_page(page); |
9ebefb18 | 8970 | size = i_size_read(inode); |
a52d9a80 | 8971 | |
9ebefb18 | 8972 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8973 | (page_start >= size)) { |
9ebefb18 CM |
8974 | /* page got truncated out from underneath us */ |
8975 | goto out_unlock; | |
8976 | } | |
e6dcd2dc CM |
8977 | wait_on_page_writeback(page); |
8978 | ||
ff13db41 | 8979 | lock_extent_bits(io_tree, page_start, page_end, &cached_state); |
e6dcd2dc CM |
8980 | set_page_extent_mapped(page); |
8981 | ||
eb84ae03 CM |
8982 | /* |
8983 | * we can't set the delalloc bits if there are pending ordered | |
8984 | * extents. Drop our locks and wait for them to finish | |
8985 | */ | |
d0b7da88 | 8986 | ordered = btrfs_lookup_ordered_range(inode, page_start, page_end); |
e6dcd2dc | 8987 | if (ordered) { |
2ac55d41 JB |
8988 | unlock_extent_cached(io_tree, page_start, page_end, |
8989 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 8990 | unlock_page(page); |
eb84ae03 | 8991 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
8992 | btrfs_put_ordered_extent(ordered); |
8993 | goto again; | |
8994 | } | |
8995 | ||
09cbfeaf | 8996 | if (page->index == ((size - 1) >> PAGE_SHIFT)) { |
d0b7da88 | 8997 | reserved_space = round_up(size - page_start, root->sectorsize); |
09cbfeaf | 8998 | if (reserved_space < PAGE_SIZE) { |
d0b7da88 CR |
8999 | end = page_start + reserved_space - 1; |
9000 | spin_lock(&BTRFS_I(inode)->lock); | |
9001 | BTRFS_I(inode)->outstanding_extents++; | |
9002 | spin_unlock(&BTRFS_I(inode)->lock); | |
9003 | btrfs_delalloc_release_space(inode, page_start, | |
09cbfeaf | 9004 | PAGE_SIZE - reserved_space); |
d0b7da88 CR |
9005 | } |
9006 | } | |
9007 | ||
fbf19087 JB |
9008 | /* |
9009 | * XXX - page_mkwrite gets called every time the page is dirtied, even | |
9010 | * if it was already dirty, so for space accounting reasons we need to | |
9011 | * clear any delalloc bits for the range we are fixing to save. There | |
9012 | * is probably a better way to do this, but for now keep consistent with | |
9013 | * prepare_pages in the normal write path. | |
9014 | */ | |
d0b7da88 | 9015 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end, |
9e8a4a8b LB |
9016 | EXTENT_DIRTY | EXTENT_DELALLOC | |
9017 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 9018 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 9019 | |
d0b7da88 | 9020 | ret = btrfs_set_extent_delalloc(inode, page_start, end, |
2ac55d41 | 9021 | &cached_state); |
9ed74f2d | 9022 | if (ret) { |
2ac55d41 JB |
9023 | unlock_extent_cached(io_tree, page_start, page_end, |
9024 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
9025 | ret = VM_FAULT_SIGBUS; |
9026 | goto out_unlock; | |
9027 | } | |
e6dcd2dc | 9028 | ret = 0; |
9ebefb18 CM |
9029 | |
9030 | /* page is wholly or partially inside EOF */ | |
09cbfeaf KS |
9031 | if (page_start + PAGE_SIZE > size) |
9032 | zero_start = size & ~PAGE_MASK; | |
9ebefb18 | 9033 | else |
09cbfeaf | 9034 | zero_start = PAGE_SIZE; |
9ebefb18 | 9035 | |
09cbfeaf | 9036 | if (zero_start != PAGE_SIZE) { |
e6dcd2dc | 9037 | kaddr = kmap(page); |
09cbfeaf | 9038 | memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start); |
e6dcd2dc CM |
9039 | flush_dcache_page(page); |
9040 | kunmap(page); | |
9041 | } | |
247e743c | 9042 | ClearPageChecked(page); |
e6dcd2dc | 9043 | set_page_dirty(page); |
50a9b214 | 9044 | SetPageUptodate(page); |
5a3f23d5 | 9045 | |
257c62e1 CM |
9046 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
9047 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | |
46d8bc34 | 9048 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 9049 | |
2ac55d41 | 9050 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
9051 | |
9052 | out_unlock: | |
b2b5ef5c JK |
9053 | if (!ret) { |
9054 | sb_end_pagefault(inode->i_sb); | |
50a9b214 | 9055 | return VM_FAULT_LOCKED; |
b2b5ef5c | 9056 | } |
9ebefb18 | 9057 | unlock_page(page); |
1832a6d5 | 9058 | out: |
d0b7da88 | 9059 | btrfs_delalloc_release_space(inode, page_start, reserved_space); |
9998eb70 | 9060 | out_noreserve: |
b2b5ef5c | 9061 | sb_end_pagefault(inode->i_sb); |
9ebefb18 CM |
9062 | return ret; |
9063 | } | |
9064 | ||
a41ad394 | 9065 | static int btrfs_truncate(struct inode *inode) |
39279cc3 CM |
9066 | { |
9067 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
fcb80c2a | 9068 | struct btrfs_block_rsv *rsv; |
a71754fc | 9069 | int ret = 0; |
3893e33b | 9070 | int err = 0; |
39279cc3 | 9071 | struct btrfs_trans_handle *trans; |
dbe674a9 | 9072 | u64 mask = root->sectorsize - 1; |
07127184 | 9073 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 | 9074 | |
0ef8b726 JB |
9075 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
9076 | (u64)-1); | |
9077 | if (ret) | |
9078 | return ret; | |
39279cc3 | 9079 | |
fcb80c2a | 9080 | /* |
01327610 | 9081 | * Yes ladies and gentlemen, this is indeed ugly. The fact is we have |
fcb80c2a JB |
9082 | * 3 things going on here |
9083 | * | |
9084 | * 1) We need to reserve space for our orphan item and the space to | |
9085 | * delete our orphan item. Lord knows we don't want to have a dangling | |
9086 | * orphan item because we didn't reserve space to remove it. | |
9087 | * | |
9088 | * 2) We need to reserve space to update our inode. | |
9089 | * | |
9090 | * 3) We need to have something to cache all the space that is going to | |
9091 | * be free'd up by the truncate operation, but also have some slack | |
9092 | * space reserved in case it uses space during the truncate (thank you | |
9093 | * very much snapshotting). | |
9094 | * | |
01327610 | 9095 | * And we need these to all be separate. The fact is we can use a lot of |
fcb80c2a | 9096 | * space doing the truncate, and we have no earthly idea how much space |
01327610 | 9097 | * we will use, so we need the truncate reservation to be separate so it |
fcb80c2a JB |
9098 | * doesn't end up using space reserved for updating the inode or |
9099 | * removing the orphan item. We also need to be able to stop the | |
9100 | * transaction and start a new one, which means we need to be able to | |
9101 | * update the inode several times, and we have no idea of knowing how | |
9102 | * many times that will be, so we can't just reserve 1 item for the | |
01327610 | 9103 | * entirety of the operation, so that has to be done separately as well. |
fcb80c2a JB |
9104 | * Then there is the orphan item, which does indeed need to be held on |
9105 | * to for the whole operation, and we need nobody to touch this reserved | |
9106 | * space except the orphan code. | |
9107 | * | |
9108 | * So that leaves us with | |
9109 | * | |
9110 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
9111 | * 2) rsv - for the truncate reservation, which we will steal from the | |
9112 | * transaction reservation. | |
9113 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
9114 | * updating the inode. | |
9115 | */ | |
66d8f3dd | 9116 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
9117 | if (!rsv) |
9118 | return -ENOMEM; | |
4a338542 | 9119 | rsv->size = min_size; |
ca7e70f5 | 9120 | rsv->failfast = 1; |
f0cd846e | 9121 | |
907cbceb | 9122 | /* |
07127184 | 9123 | * 1 for the truncate slack space |
907cbceb JB |
9124 | * 1 for updating the inode. |
9125 | */ | |
f3fe820c | 9126 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
9127 | if (IS_ERR(trans)) { |
9128 | err = PTR_ERR(trans); | |
9129 | goto out; | |
9130 | } | |
f0cd846e | 9131 | |
907cbceb JB |
9132 | /* Migrate the slack space for the truncate to our reserve */ |
9133 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, | |
25d609f8 | 9134 | min_size, 0); |
fcb80c2a | 9135 | BUG_ON(ret); |
f0cd846e | 9136 | |
5dc562c5 JB |
9137 | /* |
9138 | * So if we truncate and then write and fsync we normally would just | |
9139 | * write the extents that changed, which is a problem if we need to | |
9140 | * first truncate that entire inode. So set this flag so we write out | |
9141 | * all of the extents in the inode to the sync log so we're completely | |
9142 | * safe. | |
9143 | */ | |
9144 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 9145 | trans->block_rsv = rsv; |
907cbceb | 9146 | |
8082510e YZ |
9147 | while (1) { |
9148 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
9149 | inode->i_size, | |
9150 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 9151 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 9152 | err = ret; |
8082510e | 9153 | break; |
3893e33b | 9154 | } |
39279cc3 | 9155 | |
fcb80c2a | 9156 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e | 9157 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
9158 | if (ret) { |
9159 | err = ret; | |
9160 | break; | |
9161 | } | |
ca7e70f5 | 9162 | |
8082510e | 9163 | btrfs_end_transaction(trans, root); |
b53d3f5d | 9164 | btrfs_btree_balance_dirty(root); |
ca7e70f5 JB |
9165 | |
9166 | trans = btrfs_start_transaction(root, 2); | |
9167 | if (IS_ERR(trans)) { | |
9168 | ret = err = PTR_ERR(trans); | |
9169 | trans = NULL; | |
9170 | break; | |
9171 | } | |
9172 | ||
9173 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, | |
25d609f8 | 9174 | rsv, min_size, 0); |
ca7e70f5 JB |
9175 | BUG_ON(ret); /* shouldn't happen */ |
9176 | trans->block_rsv = rsv; | |
8082510e YZ |
9177 | } |
9178 | ||
9179 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 9180 | trans->block_rsv = root->orphan_block_rsv; |
8082510e | 9181 | ret = btrfs_orphan_del(trans, inode); |
3893e33b JB |
9182 | if (ret) |
9183 | err = ret; | |
8082510e YZ |
9184 | } |
9185 | ||
917c16b2 CM |
9186 | if (trans) { |
9187 | trans->block_rsv = &root->fs_info->trans_block_rsv; | |
9188 | ret = btrfs_update_inode(trans, root, inode); | |
9189 | if (ret && !err) | |
9190 | err = ret; | |
7b128766 | 9191 | |
7ad85bb7 | 9192 | ret = btrfs_end_transaction(trans, root); |
b53d3f5d | 9193 | btrfs_btree_balance_dirty(root); |
917c16b2 | 9194 | } |
fcb80c2a JB |
9195 | out: |
9196 | btrfs_free_block_rsv(root, rsv); | |
9197 | ||
3893e33b JB |
9198 | if (ret && !err) |
9199 | err = ret; | |
a41ad394 | 9200 | |
3893e33b | 9201 | return err; |
39279cc3 CM |
9202 | } |
9203 | ||
d352ac68 CM |
9204 | /* |
9205 | * create a new subvolume directory/inode (helper for the ioctl). | |
9206 | */ | |
d2fb3437 | 9207 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
9208 | struct btrfs_root *new_root, |
9209 | struct btrfs_root *parent_root, | |
9210 | u64 new_dirid) | |
39279cc3 | 9211 | { |
39279cc3 | 9212 | struct inode *inode; |
76dda93c | 9213 | int err; |
00e4e6b3 | 9214 | u64 index = 0; |
39279cc3 | 9215 | |
12fc9d09 FA |
9216 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
9217 | new_dirid, new_dirid, | |
9218 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
9219 | &index); | |
54aa1f4d | 9220 | if (IS_ERR(inode)) |
f46b5a66 | 9221 | return PTR_ERR(inode); |
39279cc3 CM |
9222 | inode->i_op = &btrfs_dir_inode_operations; |
9223 | inode->i_fop = &btrfs_dir_file_operations; | |
9224 | ||
bfe86848 | 9225 | set_nlink(inode, 1); |
dbe674a9 | 9226 | btrfs_i_size_write(inode, 0); |
b0d5d10f | 9227 | unlock_new_inode(inode); |
3b96362c | 9228 | |
63541927 FDBM |
9229 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
9230 | if (err) | |
9231 | btrfs_err(new_root->fs_info, | |
351fd353 | 9232 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9233 | new_root->root_key.objectid, err); |
9234 | ||
76dda93c | 9235 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9236 | |
76dda93c | 9237 | iput(inode); |
ce598979 | 9238 | return err; |
39279cc3 CM |
9239 | } |
9240 | ||
39279cc3 CM |
9241 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9242 | { | |
9243 | struct btrfs_inode *ei; | |
2ead6ae7 | 9244 | struct inode *inode; |
39279cc3 CM |
9245 | |
9246 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
9247 | if (!ei) | |
9248 | return NULL; | |
2ead6ae7 YZ |
9249 | |
9250 | ei->root = NULL; | |
2ead6ae7 | 9251 | ei->generation = 0; |
15ee9bc7 | 9252 | ei->last_trans = 0; |
257c62e1 | 9253 | ei->last_sub_trans = 0; |
e02119d5 | 9254 | ei->logged_trans = 0; |
2ead6ae7 | 9255 | ei->delalloc_bytes = 0; |
47059d93 | 9256 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9257 | ei->disk_i_size = 0; |
9258 | ei->flags = 0; | |
7709cde3 | 9259 | ei->csum_bytes = 0; |
2ead6ae7 | 9260 | ei->index_cnt = (u64)-1; |
67de1176 | 9261 | ei->dir_index = 0; |
2ead6ae7 | 9262 | ei->last_unlink_trans = 0; |
46d8bc34 | 9263 | ei->last_log_commit = 0; |
8089fe62 | 9264 | ei->delayed_iput_count = 0; |
2ead6ae7 | 9265 | |
9e0baf60 JB |
9266 | spin_lock_init(&ei->lock); |
9267 | ei->outstanding_extents = 0; | |
9268 | ei->reserved_extents = 0; | |
2ead6ae7 | 9269 | |
72ac3c0d | 9270 | ei->runtime_flags = 0; |
261507a0 | 9271 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9272 | |
16cdcec7 MX |
9273 | ei->delayed_node = NULL; |
9274 | ||
9cc97d64 | 9275 | ei->i_otime.tv_sec = 0; |
9276 | ei->i_otime.tv_nsec = 0; | |
9277 | ||
2ead6ae7 | 9278 | inode = &ei->vfs_inode; |
a8067e02 | 9279 | extent_map_tree_init(&ei->extent_tree); |
f993c883 DS |
9280 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
9281 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); | |
0b32f4bb JB |
9282 | ei->io_tree.track_uptodate = 1; |
9283 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 9284 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9285 | mutex_init(&ei->log_mutex); |
f248679e | 9286 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 9287 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9288 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9289 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 | 9290 | RB_CLEAR_NODE(&ei->rb_node); |
5f9a8a51 | 9291 | init_rwsem(&ei->dio_sem); |
2ead6ae7 YZ |
9292 | |
9293 | return inode; | |
39279cc3 CM |
9294 | } |
9295 | ||
aaedb55b JB |
9296 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9297 | void btrfs_test_destroy_inode(struct inode *inode) | |
9298 | { | |
9299 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
9300 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
9301 | } | |
9302 | #endif | |
9303 | ||
fa0d7e3d NP |
9304 | static void btrfs_i_callback(struct rcu_head *head) |
9305 | { | |
9306 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
9307 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9308 | } | |
9309 | ||
39279cc3 CM |
9310 | void btrfs_destroy_inode(struct inode *inode) |
9311 | { | |
e6dcd2dc | 9312 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9313 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9314 | ||
b3d9b7a3 | 9315 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9316 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
9317 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
9318 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 JB |
9319 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
9320 | WARN_ON(BTRFS_I(inode)->csum_bytes); | |
47059d93 | 9321 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9322 | |
a6dbd429 JB |
9323 | /* |
9324 | * This can happen where we create an inode, but somebody else also | |
9325 | * created the same inode and we need to destroy the one we already | |
9326 | * created. | |
9327 | */ | |
9328 | if (!root) | |
9329 | goto free; | |
9330 | ||
8a35d95f JB |
9331 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
9332 | &BTRFS_I(inode)->runtime_flags)) { | |
c2cf52eb | 9333 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
c1c9ff7c | 9334 | btrfs_ino(inode)); |
8a35d95f | 9335 | atomic_dec(&root->orphan_inodes); |
7b128766 | 9336 | } |
7b128766 | 9337 | |
d397712b | 9338 | while (1) { |
e6dcd2dc CM |
9339 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9340 | if (!ordered) | |
9341 | break; | |
9342 | else { | |
c2cf52eb | 9343 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
c1c9ff7c | 9344 | ordered->file_offset, ordered->len); |
e6dcd2dc CM |
9345 | btrfs_remove_ordered_extent(inode, ordered); |
9346 | btrfs_put_ordered_extent(ordered); | |
9347 | btrfs_put_ordered_extent(ordered); | |
9348 | } | |
9349 | } | |
56fa9d07 | 9350 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9351 | inode_tree_del(inode); |
5b21f2ed | 9352 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
a6dbd429 | 9353 | free: |
fa0d7e3d | 9354 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9355 | } |
9356 | ||
45321ac5 | 9357 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9358 | { |
9359 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9360 | |
6379ef9f NA |
9361 | if (root == NULL) |
9362 | return 1; | |
9363 | ||
fa6ac876 | 9364 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9365 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9366 | return 1; |
76dda93c | 9367 | else |
45321ac5 | 9368 | return generic_drop_inode(inode); |
76dda93c YZ |
9369 | } |
9370 | ||
0ee0fda0 | 9371 | static void init_once(void *foo) |
39279cc3 CM |
9372 | { |
9373 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9374 | ||
9375 | inode_init_once(&ei->vfs_inode); | |
9376 | } | |
9377 | ||
9378 | void btrfs_destroy_cachep(void) | |
9379 | { | |
8c0a8537 KS |
9380 | /* |
9381 | * Make sure all delayed rcu free inodes are flushed before we | |
9382 | * destroy cache. | |
9383 | */ | |
9384 | rcu_barrier(); | |
5598e900 KM |
9385 | kmem_cache_destroy(btrfs_inode_cachep); |
9386 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
9387 | kmem_cache_destroy(btrfs_transaction_cachep); | |
9388 | kmem_cache_destroy(btrfs_path_cachep); | |
9389 | kmem_cache_destroy(btrfs_free_space_cachep); | |
39279cc3 CM |
9390 | } |
9391 | ||
9392 | int btrfs_init_cachep(void) | |
9393 | { | |
837e1972 | 9394 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 | 9395 | sizeof(struct btrfs_inode), 0, |
5d097056 VD |
9396 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT, |
9397 | init_once); | |
39279cc3 CM |
9398 | if (!btrfs_inode_cachep) |
9399 | goto fail; | |
9601e3f6 | 9400 | |
837e1972 | 9401 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 | 9402 | sizeof(struct btrfs_trans_handle), 0, |
fba4b697 | 9403 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9404 | if (!btrfs_trans_handle_cachep) |
9405 | goto fail; | |
9601e3f6 | 9406 | |
837e1972 | 9407 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
9601e3f6 | 9408 | sizeof(struct btrfs_transaction), 0, |
fba4b697 | 9409 | SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9410 | if (!btrfs_transaction_cachep) |
9411 | goto fail; | |
9601e3f6 | 9412 | |
837e1972 | 9413 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 | 9414 | sizeof(struct btrfs_path), 0, |
fba4b697 | 9415 | SLAB_MEM_SPREAD, NULL); |
39279cc3 CM |
9416 | if (!btrfs_path_cachep) |
9417 | goto fail; | |
9601e3f6 | 9418 | |
837e1972 | 9419 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 | 9420 | sizeof(struct btrfs_free_space), 0, |
fba4b697 | 9421 | SLAB_MEM_SPREAD, NULL); |
dc89e982 JB |
9422 | if (!btrfs_free_space_cachep) |
9423 | goto fail; | |
9424 | ||
39279cc3 CM |
9425 | return 0; |
9426 | fail: | |
9427 | btrfs_destroy_cachep(); | |
9428 | return -ENOMEM; | |
9429 | } | |
9430 | ||
9431 | static int btrfs_getattr(struct vfsmount *mnt, | |
9432 | struct dentry *dentry, struct kstat *stat) | |
9433 | { | |
df0af1a5 | 9434 | u64 delalloc_bytes; |
2b0143b5 | 9435 | struct inode *inode = d_inode(dentry); |
fadc0d8b DS |
9436 | u32 blocksize = inode->i_sb->s_blocksize; |
9437 | ||
39279cc3 | 9438 | generic_fillattr(inode, stat); |
0ee5dc67 | 9439 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
df0af1a5 MX |
9440 | |
9441 | spin_lock(&BTRFS_I(inode)->lock); | |
9442 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; | |
9443 | spin_unlock(&BTRFS_I(inode)->lock); | |
fadc0d8b | 9444 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9445 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9446 | return 0; |
9447 | } | |
9448 | ||
cdd1fedf DF |
9449 | static int btrfs_rename_exchange(struct inode *old_dir, |
9450 | struct dentry *old_dentry, | |
9451 | struct inode *new_dir, | |
9452 | struct dentry *new_dentry) | |
9453 | { | |
9454 | struct btrfs_trans_handle *trans; | |
9455 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
9456 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; | |
9457 | struct inode *new_inode = new_dentry->d_inode; | |
9458 | struct inode *old_inode = old_dentry->d_inode; | |
9459 | struct timespec ctime = CURRENT_TIME; | |
9460 | struct dentry *parent; | |
9461 | u64 old_ino = btrfs_ino(old_inode); | |
9462 | u64 new_ino = btrfs_ino(new_inode); | |
9463 | u64 old_idx = 0; | |
9464 | u64 new_idx = 0; | |
9465 | u64 root_objectid; | |
9466 | int ret; | |
86e8aa0e FM |
9467 | bool root_log_pinned = false; |
9468 | bool dest_log_pinned = false; | |
cdd1fedf DF |
9469 | |
9470 | /* we only allow rename subvolume link between subvolumes */ | |
9471 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) | |
9472 | return -EXDEV; | |
9473 | ||
9474 | /* close the race window with snapshot create/destroy ioctl */ | |
9475 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9476 | down_read(&root->fs_info->subvol_sem); | |
9477 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9478 | down_read(&dest->fs_info->subvol_sem); | |
9479 | ||
9480 | /* | |
9481 | * We want to reserve the absolute worst case amount of items. So if | |
9482 | * both inodes are subvols and we need to unlink them then that would | |
9483 | * require 4 item modifications, but if they are both normal inodes it | |
9484 | * would require 5 item modifications, so we'll assume their normal | |
9485 | * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items | |
9486 | * should cover the worst case number of items we'll modify. | |
9487 | */ | |
9488 | trans = btrfs_start_transaction(root, 12); | |
9489 | if (IS_ERR(trans)) { | |
9490 | ret = PTR_ERR(trans); | |
9491 | goto out_notrans; | |
9492 | } | |
9493 | ||
9494 | /* | |
9495 | * We need to find a free sequence number both in the source and | |
9496 | * in the destination directory for the exchange. | |
9497 | */ | |
9498 | ret = btrfs_set_inode_index(new_dir, &old_idx); | |
9499 | if (ret) | |
9500 | goto out_fail; | |
9501 | ret = btrfs_set_inode_index(old_dir, &new_idx); | |
9502 | if (ret) | |
9503 | goto out_fail; | |
9504 | ||
9505 | BTRFS_I(old_inode)->dir_index = 0ULL; | |
9506 | BTRFS_I(new_inode)->dir_index = 0ULL; | |
9507 | ||
9508 | /* Reference for the source. */ | |
9509 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9510 | /* force full log commit if subvolume involved. */ | |
9511 | btrfs_set_log_full_commit(root->fs_info, trans); | |
9512 | } else { | |
376e5a57 FM |
9513 | btrfs_pin_log_trans(root); |
9514 | root_log_pinned = true; | |
cdd1fedf DF |
9515 | ret = btrfs_insert_inode_ref(trans, dest, |
9516 | new_dentry->d_name.name, | |
9517 | new_dentry->d_name.len, | |
9518 | old_ino, | |
9519 | btrfs_ino(new_dir), old_idx); | |
9520 | if (ret) | |
9521 | goto out_fail; | |
cdd1fedf DF |
9522 | } |
9523 | ||
9524 | /* And now for the dest. */ | |
9525 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9526 | /* force full log commit if subvolume involved. */ | |
9527 | btrfs_set_log_full_commit(dest->fs_info, trans); | |
9528 | } else { | |
376e5a57 FM |
9529 | btrfs_pin_log_trans(dest); |
9530 | dest_log_pinned = true; | |
cdd1fedf DF |
9531 | ret = btrfs_insert_inode_ref(trans, root, |
9532 | old_dentry->d_name.name, | |
9533 | old_dentry->d_name.len, | |
9534 | new_ino, | |
9535 | btrfs_ino(old_dir), new_idx); | |
9536 | if (ret) | |
9537 | goto out_fail; | |
cdd1fedf DF |
9538 | } |
9539 | ||
9540 | /* Update inode version and ctime/mtime. */ | |
9541 | inode_inc_iversion(old_dir); | |
9542 | inode_inc_iversion(new_dir); | |
9543 | inode_inc_iversion(old_inode); | |
9544 | inode_inc_iversion(new_inode); | |
9545 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
9546 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9547 | old_inode->i_ctime = ctime; | |
9548 | new_inode->i_ctime = ctime; | |
9549 | ||
9550 | if (old_dentry->d_parent != new_dentry->d_parent) { | |
9551 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | |
9552 | btrfs_record_unlink_dir(trans, new_dir, new_inode, 1); | |
9553 | } | |
9554 | ||
9555 | /* src is a subvolume */ | |
9556 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9557 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; | |
9558 | ret = btrfs_unlink_subvol(trans, root, old_dir, | |
9559 | root_objectid, | |
9560 | old_dentry->d_name.name, | |
9561 | old_dentry->d_name.len); | |
9562 | } else { /* src is an inode */ | |
9563 | ret = __btrfs_unlink_inode(trans, root, old_dir, | |
9564 | old_dentry->d_inode, | |
9565 | old_dentry->d_name.name, | |
9566 | old_dentry->d_name.len); | |
9567 | if (!ret) | |
9568 | ret = btrfs_update_inode(trans, root, old_inode); | |
9569 | } | |
9570 | if (ret) { | |
9571 | btrfs_abort_transaction(trans, root, ret); | |
9572 | goto out_fail; | |
9573 | } | |
9574 | ||
9575 | /* dest is a subvolume */ | |
9576 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { | |
9577 | root_objectid = BTRFS_I(new_inode)->root->root_key.objectid; | |
9578 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9579 | root_objectid, | |
9580 | new_dentry->d_name.name, | |
9581 | new_dentry->d_name.len); | |
9582 | } else { /* dest is an inode */ | |
9583 | ret = __btrfs_unlink_inode(trans, dest, new_dir, | |
9584 | new_dentry->d_inode, | |
9585 | new_dentry->d_name.name, | |
9586 | new_dentry->d_name.len); | |
9587 | if (!ret) | |
9588 | ret = btrfs_update_inode(trans, dest, new_inode); | |
9589 | } | |
9590 | if (ret) { | |
9591 | btrfs_abort_transaction(trans, root, ret); | |
9592 | goto out_fail; | |
9593 | } | |
9594 | ||
9595 | ret = btrfs_add_link(trans, new_dir, old_inode, | |
9596 | new_dentry->d_name.name, | |
9597 | new_dentry->d_name.len, 0, old_idx); | |
9598 | if (ret) { | |
9599 | btrfs_abort_transaction(trans, root, ret); | |
9600 | goto out_fail; | |
9601 | } | |
9602 | ||
9603 | ret = btrfs_add_link(trans, old_dir, new_inode, | |
9604 | old_dentry->d_name.name, | |
9605 | old_dentry->d_name.len, 0, new_idx); | |
9606 | if (ret) { | |
9607 | btrfs_abort_transaction(trans, root, ret); | |
9608 | goto out_fail; | |
9609 | } | |
9610 | ||
9611 | if (old_inode->i_nlink == 1) | |
9612 | BTRFS_I(old_inode)->dir_index = old_idx; | |
9613 | if (new_inode->i_nlink == 1) | |
9614 | BTRFS_I(new_inode)->dir_index = new_idx; | |
9615 | ||
86e8aa0e | 9616 | if (root_log_pinned) { |
cdd1fedf DF |
9617 | parent = new_dentry->d_parent; |
9618 | btrfs_log_new_name(trans, old_inode, old_dir, parent); | |
9619 | btrfs_end_log_trans(root); | |
86e8aa0e | 9620 | root_log_pinned = false; |
cdd1fedf | 9621 | } |
86e8aa0e | 9622 | if (dest_log_pinned) { |
cdd1fedf DF |
9623 | parent = old_dentry->d_parent; |
9624 | btrfs_log_new_name(trans, new_inode, new_dir, parent); | |
9625 | btrfs_end_log_trans(dest); | |
86e8aa0e | 9626 | dest_log_pinned = false; |
cdd1fedf DF |
9627 | } |
9628 | out_fail: | |
86e8aa0e FM |
9629 | /* |
9630 | * If we have pinned a log and an error happened, we unpin tasks | |
9631 | * trying to sync the log and force them to fallback to a transaction | |
9632 | * commit if the log currently contains any of the inodes involved in | |
9633 | * this rename operation (to ensure we do not persist a log with an | |
9634 | * inconsistent state for any of these inodes or leading to any | |
9635 | * inconsistencies when replayed). If the transaction was aborted, the | |
9636 | * abortion reason is propagated to userspace when attempting to commit | |
9637 | * the transaction. If the log does not contain any of these inodes, we | |
9638 | * allow the tasks to sync it. | |
9639 | */ | |
9640 | if (ret && (root_log_pinned || dest_log_pinned)) { | |
9641 | if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || | |
9642 | btrfs_inode_in_log(new_dir, root->fs_info->generation) || | |
9643 | btrfs_inode_in_log(old_inode, root->fs_info->generation) || | |
9644 | (new_inode && | |
9645 | btrfs_inode_in_log(new_inode, root->fs_info->generation))) | |
9646 | btrfs_set_log_full_commit(root->fs_info, trans); | |
9647 | ||
9648 | if (root_log_pinned) { | |
9649 | btrfs_end_log_trans(root); | |
9650 | root_log_pinned = false; | |
9651 | } | |
9652 | if (dest_log_pinned) { | |
9653 | btrfs_end_log_trans(dest); | |
9654 | dest_log_pinned = false; | |
9655 | } | |
9656 | } | |
cdd1fedf DF |
9657 | ret = btrfs_end_transaction(trans, root); |
9658 | out_notrans: | |
9659 | if (new_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9660 | up_read(&dest->fs_info->subvol_sem); | |
9661 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) | |
9662 | up_read(&root->fs_info->subvol_sem); | |
9663 | ||
9664 | return ret; | |
9665 | } | |
9666 | ||
9667 | static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans, | |
9668 | struct btrfs_root *root, | |
9669 | struct inode *dir, | |
9670 | struct dentry *dentry) | |
9671 | { | |
9672 | int ret; | |
9673 | struct inode *inode; | |
9674 | u64 objectid; | |
9675 | u64 index; | |
9676 | ||
9677 | ret = btrfs_find_free_ino(root, &objectid); | |
9678 | if (ret) | |
9679 | return ret; | |
9680 | ||
9681 | inode = btrfs_new_inode(trans, root, dir, | |
9682 | dentry->d_name.name, | |
9683 | dentry->d_name.len, | |
9684 | btrfs_ino(dir), | |
9685 | objectid, | |
9686 | S_IFCHR | WHITEOUT_MODE, | |
9687 | &index); | |
9688 | ||
9689 | if (IS_ERR(inode)) { | |
9690 | ret = PTR_ERR(inode); | |
9691 | return ret; | |
9692 | } | |
9693 | ||
9694 | inode->i_op = &btrfs_special_inode_operations; | |
9695 | init_special_inode(inode, inode->i_mode, | |
9696 | WHITEOUT_DEV); | |
9697 | ||
9698 | ret = btrfs_init_inode_security(trans, inode, dir, | |
9699 | &dentry->d_name); | |
9700 | if (ret) | |
c9901618 | 9701 | goto out; |
cdd1fedf DF |
9702 | |
9703 | ret = btrfs_add_nondir(trans, dir, dentry, | |
9704 | inode, 0, index); | |
9705 | if (ret) | |
c9901618 | 9706 | goto out; |
cdd1fedf DF |
9707 | |
9708 | ret = btrfs_update_inode(trans, root, inode); | |
c9901618 | 9709 | out: |
cdd1fedf | 9710 | unlock_new_inode(inode); |
c9901618 FM |
9711 | if (ret) |
9712 | inode_dec_link_count(inode); | |
cdd1fedf DF |
9713 | iput(inode); |
9714 | ||
c9901618 | 9715 | return ret; |
cdd1fedf DF |
9716 | } |
9717 | ||
d397712b | 9718 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
cdd1fedf DF |
9719 | struct inode *new_dir, struct dentry *new_dentry, |
9720 | unsigned int flags) | |
39279cc3 CM |
9721 | { |
9722 | struct btrfs_trans_handle *trans; | |
5062af35 | 9723 | unsigned int trans_num_items; |
39279cc3 | 9724 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
4df27c4d | 9725 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9726 | struct inode *new_inode = d_inode(new_dentry); |
9727 | struct inode *old_inode = d_inode(old_dentry); | |
00e4e6b3 | 9728 | u64 index = 0; |
4df27c4d | 9729 | u64 root_objectid; |
39279cc3 | 9730 | int ret; |
33345d01 | 9731 | u64 old_ino = btrfs_ino(old_inode); |
3dc9e8f7 | 9732 | bool log_pinned = false; |
39279cc3 | 9733 | |
33345d01 | 9734 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9735 | return -EPERM; |
9736 | ||
4df27c4d | 9737 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9738 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9739 | return -EXDEV; |
9740 | ||
33345d01 LZ |
9741 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
9742 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) | |
39279cc3 | 9743 | return -ENOTEMPTY; |
5f39d397 | 9744 | |
4df27c4d YZ |
9745 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9746 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9747 | return -ENOTEMPTY; | |
9c52057c CM |
9748 | |
9749 | ||
9750 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9751 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9752 | new_dentry->d_name.name, |
9753 | new_dentry->d_name.len); | |
9754 | ||
9755 | if (ret) { | |
9756 | if (ret == -EEXIST) { | |
9757 | /* we shouldn't get | |
9758 | * eexist without a new_inode */ | |
fae7f21c | 9759 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9760 | return ret; |
9761 | } | |
9762 | } else { | |
9763 | /* maybe -EOVERFLOW */ | |
9764 | return ret; | |
9765 | } | |
9766 | } | |
9767 | ret = 0; | |
9768 | ||
5a3f23d5 | 9769 | /* |
8d875f95 CM |
9770 | * we're using rename to replace one file with another. Start IO on it |
9771 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9772 | */ |
8d875f95 | 9773 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9774 | filemap_flush(old_inode->i_mapping); |
9775 | ||
76dda93c | 9776 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9777 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9778 | down_read(&root->fs_info->subvol_sem); |
a22285a6 YZ |
9779 | /* |
9780 | * We want to reserve the absolute worst case amount of items. So if | |
9781 | * both inodes are subvols and we need to unlink them then that would | |
9782 | * require 4 item modifications, but if they are both normal inodes it | |
cdd1fedf | 9783 | * would require 5 item modifications, so we'll assume they are normal |
a22285a6 YZ |
9784 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items |
9785 | * should cover the worst case number of items we'll modify. | |
5062af35 FM |
9786 | * If our rename has the whiteout flag, we need more 5 units for the |
9787 | * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item | |
9788 | * when selinux is enabled). | |
a22285a6 | 9789 | */ |
5062af35 FM |
9790 | trans_num_items = 11; |
9791 | if (flags & RENAME_WHITEOUT) | |
9792 | trans_num_items += 5; | |
9793 | trans = btrfs_start_transaction(root, trans_num_items); | |
b44c59a8 | 9794 | if (IS_ERR(trans)) { |
cdd1fedf DF |
9795 | ret = PTR_ERR(trans); |
9796 | goto out_notrans; | |
9797 | } | |
76dda93c | 9798 | |
4df27c4d YZ |
9799 | if (dest != root) |
9800 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9801 | |
a5719521 YZ |
9802 | ret = btrfs_set_inode_index(new_dir, &index); |
9803 | if (ret) | |
9804 | goto out_fail; | |
5a3f23d5 | 9805 | |
67de1176 | 9806 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9807 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9808 | /* force full log commit if subvolume involved. */ |
995946dd | 9809 | btrfs_set_log_full_commit(root->fs_info, trans); |
4df27c4d | 9810 | } else { |
c4aba954 FM |
9811 | btrfs_pin_log_trans(root); |
9812 | log_pinned = true; | |
a5719521 YZ |
9813 | ret = btrfs_insert_inode_ref(trans, dest, |
9814 | new_dentry->d_name.name, | |
9815 | new_dentry->d_name.len, | |
33345d01 LZ |
9816 | old_ino, |
9817 | btrfs_ino(new_dir), index); | |
a5719521 YZ |
9818 | if (ret) |
9819 | goto out_fail; | |
4df27c4d | 9820 | } |
5a3f23d5 | 9821 | |
0c4d2d95 JB |
9822 | inode_inc_iversion(old_dir); |
9823 | inode_inc_iversion(new_dir); | |
9824 | inode_inc_iversion(old_inode); | |
04b285f3 DD |
9825 | old_dir->i_ctime = old_dir->i_mtime = |
9826 | new_dir->i_ctime = new_dir->i_mtime = | |
9827 | old_inode->i_ctime = current_fs_time(old_dir->i_sb); | |
5f39d397 | 9828 | |
12fcfd22 CM |
9829 | if (old_dentry->d_parent != new_dentry->d_parent) |
9830 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | |
9831 | ||
33345d01 | 9832 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
9833 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
9834 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
9835 | old_dentry->d_name.name, | |
9836 | old_dentry->d_name.len); | |
9837 | } else { | |
92986796 | 9838 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
2b0143b5 | 9839 | d_inode(old_dentry), |
92986796 AV |
9840 | old_dentry->d_name.name, |
9841 | old_dentry->d_name.len); | |
9842 | if (!ret) | |
9843 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9844 | } |
79787eaa JM |
9845 | if (ret) { |
9846 | btrfs_abort_transaction(trans, root, ret); | |
9847 | goto out_fail; | |
9848 | } | |
39279cc3 CM |
9849 | |
9850 | if (new_inode) { | |
0c4d2d95 | 9851 | inode_inc_iversion(new_inode); |
04b285f3 | 9852 | new_inode->i_ctime = current_fs_time(new_inode->i_sb); |
33345d01 | 9853 | if (unlikely(btrfs_ino(new_inode) == |
4df27c4d YZ |
9854 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9855 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
9856 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9857 | root_objectid, | |
9858 | new_dentry->d_name.name, | |
9859 | new_dentry->d_name.len); | |
9860 | BUG_ON(new_inode->i_nlink == 0); | |
9861 | } else { | |
9862 | ret = btrfs_unlink_inode(trans, dest, new_dir, | |
2b0143b5 | 9863 | d_inode(new_dentry), |
4df27c4d YZ |
9864 | new_dentry->d_name.name, |
9865 | new_dentry->d_name.len); | |
9866 | } | |
4ef31a45 | 9867 | if (!ret && new_inode->i_nlink == 0) |
2b0143b5 | 9868 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
79787eaa JM |
9869 | if (ret) { |
9870 | btrfs_abort_transaction(trans, root, ret); | |
9871 | goto out_fail; | |
9872 | } | |
39279cc3 | 9873 | } |
aec7477b | 9874 | |
4df27c4d YZ |
9875 | ret = btrfs_add_link(trans, new_dir, old_inode, |
9876 | new_dentry->d_name.name, | |
a5719521 | 9877 | new_dentry->d_name.len, 0, index); |
79787eaa JM |
9878 | if (ret) { |
9879 | btrfs_abort_transaction(trans, root, ret); | |
9880 | goto out_fail; | |
9881 | } | |
39279cc3 | 9882 | |
67de1176 MX |
9883 | if (old_inode->i_nlink == 1) |
9884 | BTRFS_I(old_inode)->dir_index = index; | |
9885 | ||
3dc9e8f7 | 9886 | if (log_pinned) { |
10d9f309 | 9887 | struct dentry *parent = new_dentry->d_parent; |
3dc9e8f7 | 9888 | |
6a912213 | 9889 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
4df27c4d | 9890 | btrfs_end_log_trans(root); |
3dc9e8f7 | 9891 | log_pinned = false; |
4df27c4d | 9892 | } |
cdd1fedf DF |
9893 | |
9894 | if (flags & RENAME_WHITEOUT) { | |
9895 | ret = btrfs_whiteout_for_rename(trans, root, old_dir, | |
9896 | old_dentry); | |
9897 | ||
9898 | if (ret) { | |
9899 | btrfs_abort_transaction(trans, root, ret); | |
9900 | goto out_fail; | |
9901 | } | |
4df27c4d | 9902 | } |
39279cc3 | 9903 | out_fail: |
3dc9e8f7 FM |
9904 | /* |
9905 | * If we have pinned the log and an error happened, we unpin tasks | |
9906 | * trying to sync the log and force them to fallback to a transaction | |
9907 | * commit if the log currently contains any of the inodes involved in | |
9908 | * this rename operation (to ensure we do not persist a log with an | |
9909 | * inconsistent state for any of these inodes or leading to any | |
9910 | * inconsistencies when replayed). If the transaction was aborted, the | |
9911 | * abortion reason is propagated to userspace when attempting to commit | |
9912 | * the transaction. If the log does not contain any of these inodes, we | |
9913 | * allow the tasks to sync it. | |
9914 | */ | |
9915 | if (ret && log_pinned) { | |
9916 | if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || | |
9917 | btrfs_inode_in_log(new_dir, root->fs_info->generation) || | |
9918 | btrfs_inode_in_log(old_inode, root->fs_info->generation) || | |
9919 | (new_inode && | |
9920 | btrfs_inode_in_log(new_inode, root->fs_info->generation))) | |
9921 | btrfs_set_log_full_commit(root->fs_info, trans); | |
9922 | ||
9923 | btrfs_end_log_trans(root); | |
9924 | log_pinned = false; | |
9925 | } | |
7ad85bb7 | 9926 | btrfs_end_transaction(trans, root); |
b44c59a8 | 9927 | out_notrans: |
33345d01 | 9928 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9929 | up_read(&root->fs_info->subvol_sem); |
9ed74f2d | 9930 | |
39279cc3 CM |
9931 | return ret; |
9932 | } | |
9933 | ||
80ace85c MS |
9934 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9935 | struct inode *new_dir, struct dentry *new_dentry, | |
9936 | unsigned int flags) | |
9937 | { | |
cdd1fedf | 9938 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
80ace85c MS |
9939 | return -EINVAL; |
9940 | ||
cdd1fedf DF |
9941 | if (flags & RENAME_EXCHANGE) |
9942 | return btrfs_rename_exchange(old_dir, old_dentry, new_dir, | |
9943 | new_dentry); | |
9944 | ||
9945 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags); | |
80ace85c MS |
9946 | } |
9947 | ||
8ccf6f19 MX |
9948 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9949 | { | |
9950 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9951 | struct inode *inode; |
8ccf6f19 MX |
9952 | |
9953 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9954 | work); | |
9f23e289 | 9955 | inode = delalloc_work->inode; |
30424601 DS |
9956 | filemap_flush(inode->i_mapping); |
9957 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9958 | &BTRFS_I(inode)->runtime_flags)) | |
9f23e289 | 9959 | filemap_flush(inode->i_mapping); |
8ccf6f19 MX |
9960 | |
9961 | if (delalloc_work->delay_iput) | |
9f23e289 | 9962 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 9963 | else |
9f23e289 | 9964 | iput(inode); |
8ccf6f19 MX |
9965 | complete(&delalloc_work->completion); |
9966 | } | |
9967 | ||
9968 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
651d494a | 9969 | int delay_iput) |
8ccf6f19 MX |
9970 | { |
9971 | struct btrfs_delalloc_work *work; | |
9972 | ||
100d5702 | 9973 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9974 | if (!work) |
9975 | return NULL; | |
9976 | ||
9977 | init_completion(&work->completion); | |
9978 | INIT_LIST_HEAD(&work->list); | |
9979 | work->inode = inode; | |
8ccf6f19 | 9980 | work->delay_iput = delay_iput; |
9e0af237 LB |
9981 | WARN_ON_ONCE(!inode); |
9982 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
9983 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
9984 | |
9985 | return work; | |
9986 | } | |
9987 | ||
9988 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
9989 | { | |
9990 | wait_for_completion(&work->completion); | |
100d5702 | 9991 | kfree(work); |
8ccf6f19 MX |
9992 | } |
9993 | ||
d352ac68 CM |
9994 | /* |
9995 | * some fairly slow code that needs optimization. This walks the list | |
9996 | * of all the inodes with pending delalloc and forces them to disk. | |
9997 | */ | |
6c255e67 MX |
9998 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
9999 | int nr) | |
ea8c2819 | 10000 | { |
ea8c2819 | 10001 | struct btrfs_inode *binode; |
5b21f2ed | 10002 | struct inode *inode; |
8ccf6f19 MX |
10003 | struct btrfs_delalloc_work *work, *next; |
10004 | struct list_head works; | |
1eafa6c7 | 10005 | struct list_head splice; |
8ccf6f19 | 10006 | int ret = 0; |
ea8c2819 | 10007 | |
8ccf6f19 | 10008 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 10009 | INIT_LIST_HEAD(&splice); |
63607cc8 | 10010 | |
573bfb72 | 10011 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
10012 | spin_lock(&root->delalloc_lock); |
10013 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
10014 | while (!list_empty(&splice)) { |
10015 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 10016 | delalloc_inodes); |
1eafa6c7 | 10017 | |
eb73c1b7 MX |
10018 | list_move_tail(&binode->delalloc_inodes, |
10019 | &root->delalloc_inodes); | |
5b21f2ed | 10020 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 10021 | if (!inode) { |
eb73c1b7 | 10022 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 10023 | continue; |
df0af1a5 | 10024 | } |
eb73c1b7 | 10025 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 | 10026 | |
651d494a | 10027 | work = btrfs_alloc_delalloc_work(inode, delay_iput); |
5d99a998 | 10028 | if (!work) { |
f4ab9ea7 JB |
10029 | if (delay_iput) |
10030 | btrfs_add_delayed_iput(inode); | |
10031 | else | |
10032 | iput(inode); | |
1eafa6c7 | 10033 | ret = -ENOMEM; |
a1ecaabb | 10034 | goto out; |
5b21f2ed | 10035 | } |
1eafa6c7 | 10036 | list_add_tail(&work->list, &works); |
a44903ab QW |
10037 | btrfs_queue_work(root->fs_info->flush_workers, |
10038 | &work->work); | |
6c255e67 MX |
10039 | ret++; |
10040 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 10041 | goto out; |
5b21f2ed | 10042 | cond_resched(); |
eb73c1b7 | 10043 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 10044 | } |
eb73c1b7 | 10045 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 10046 | |
a1ecaabb | 10047 | out: |
eb73c1b7 MX |
10048 | list_for_each_entry_safe(work, next, &works, list) { |
10049 | list_del_init(&work->list); | |
10050 | btrfs_wait_and_free_delalloc_work(work); | |
10051 | } | |
10052 | ||
10053 | if (!list_empty_careful(&splice)) { | |
10054 | spin_lock(&root->delalloc_lock); | |
10055 | list_splice_tail(&splice, &root->delalloc_inodes); | |
10056 | spin_unlock(&root->delalloc_lock); | |
10057 | } | |
573bfb72 | 10058 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
10059 | return ret; |
10060 | } | |
1eafa6c7 | 10061 | |
eb73c1b7 MX |
10062 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
10063 | { | |
10064 | int ret; | |
1eafa6c7 | 10065 | |
2c21b4d7 | 10066 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
eb73c1b7 MX |
10067 | return -EROFS; |
10068 | ||
6c255e67 MX |
10069 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
10070 | if (ret > 0) | |
10071 | ret = 0; | |
eb73c1b7 MX |
10072 | /* |
10073 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
10074 | * we have to make sure the IO is actually started and that |
10075 | * ordered extents get created before we return | |
10076 | */ | |
10077 | atomic_inc(&root->fs_info->async_submit_draining); | |
d397712b | 10078 | while (atomic_read(&root->fs_info->nr_async_submits) || |
771ed689 | 10079 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
8c8bee1d | 10080 | wait_event(root->fs_info->async_submit_wait, |
771ed689 CM |
10081 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
10082 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | |
8c8bee1d CM |
10083 | } |
10084 | atomic_dec(&root->fs_info->async_submit_draining); | |
eb73c1b7 MX |
10085 | return ret; |
10086 | } | |
10087 | ||
6c255e67 MX |
10088 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
10089 | int nr) | |
eb73c1b7 MX |
10090 | { |
10091 | struct btrfs_root *root; | |
10092 | struct list_head splice; | |
10093 | int ret; | |
10094 | ||
2c21b4d7 | 10095 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
10096 | return -EROFS; |
10097 | ||
10098 | INIT_LIST_HEAD(&splice); | |
10099 | ||
573bfb72 | 10100 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
10101 | spin_lock(&fs_info->delalloc_root_lock); |
10102 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 10103 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
10104 | root = list_first_entry(&splice, struct btrfs_root, |
10105 | delalloc_root); | |
10106 | root = btrfs_grab_fs_root(root); | |
10107 | BUG_ON(!root); | |
10108 | list_move_tail(&root->delalloc_root, | |
10109 | &fs_info->delalloc_roots); | |
10110 | spin_unlock(&fs_info->delalloc_root_lock); | |
10111 | ||
6c255e67 | 10112 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 10113 | btrfs_put_fs_root(root); |
6c255e67 | 10114 | if (ret < 0) |
eb73c1b7 MX |
10115 | goto out; |
10116 | ||
6c255e67 MX |
10117 | if (nr != -1) { |
10118 | nr -= ret; | |
10119 | WARN_ON(nr < 0); | |
10120 | } | |
eb73c1b7 | 10121 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 10122 | } |
eb73c1b7 | 10123 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 10124 | |
6c255e67 | 10125 | ret = 0; |
eb73c1b7 MX |
10126 | atomic_inc(&fs_info->async_submit_draining); |
10127 | while (atomic_read(&fs_info->nr_async_submits) || | |
10128 | atomic_read(&fs_info->async_delalloc_pages)) { | |
10129 | wait_event(fs_info->async_submit_wait, | |
10130 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
10131 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
10132 | } | |
10133 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 10134 | out: |
1eafa6c7 | 10135 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
10136 | spin_lock(&fs_info->delalloc_root_lock); |
10137 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
10138 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 10139 | } |
573bfb72 | 10140 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 10141 | return ret; |
ea8c2819 CM |
10142 | } |
10143 | ||
39279cc3 CM |
10144 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
10145 | const char *symname) | |
10146 | { | |
10147 | struct btrfs_trans_handle *trans; | |
10148 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10149 | struct btrfs_path *path; | |
10150 | struct btrfs_key key; | |
1832a6d5 | 10151 | struct inode *inode = NULL; |
39279cc3 CM |
10152 | int err; |
10153 | int drop_inode = 0; | |
10154 | u64 objectid; | |
67871254 | 10155 | u64 index = 0; |
39279cc3 CM |
10156 | int name_len; |
10157 | int datasize; | |
5f39d397 | 10158 | unsigned long ptr; |
39279cc3 | 10159 | struct btrfs_file_extent_item *ei; |
5f39d397 | 10160 | struct extent_buffer *leaf; |
39279cc3 | 10161 | |
f06becc4 | 10162 | name_len = strlen(symname); |
39279cc3 CM |
10163 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
10164 | return -ENAMETOOLONG; | |
1832a6d5 | 10165 | |
9ed74f2d JB |
10166 | /* |
10167 | * 2 items for inode item and ref | |
10168 | * 2 items for dir items | |
9269d12b FM |
10169 | * 1 item for updating parent inode item |
10170 | * 1 item for the inline extent item | |
9ed74f2d JB |
10171 | * 1 item for xattr if selinux is on |
10172 | */ | |
9269d12b | 10173 | trans = btrfs_start_transaction(root, 7); |
a22285a6 YZ |
10174 | if (IS_ERR(trans)) |
10175 | return PTR_ERR(trans); | |
1832a6d5 | 10176 | |
581bb050 LZ |
10177 | err = btrfs_find_free_ino(root, &objectid); |
10178 | if (err) | |
10179 | goto out_unlock; | |
10180 | ||
aec7477b | 10181 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 10182 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 10183 | S_IFLNK|S_IRWXUGO, &index); |
7cf96da3 TI |
10184 | if (IS_ERR(inode)) { |
10185 | err = PTR_ERR(inode); | |
39279cc3 | 10186 | goto out_unlock; |
7cf96da3 | 10187 | } |
39279cc3 | 10188 | |
ad19db71 CS |
10189 | /* |
10190 | * If the active LSM wants to access the inode during | |
10191 | * d_instantiate it needs these. Smack checks to see | |
10192 | * if the filesystem supports xattrs by looking at the | |
10193 | * ops vector. | |
10194 | */ | |
10195 | inode->i_fop = &btrfs_file_operations; | |
10196 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 10197 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
10198 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10199 | ||
10200 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
10201 | if (err) | |
10202 | goto out_unlock_inode; | |
ad19db71 | 10203 | |
39279cc3 | 10204 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10205 | if (!path) { |
10206 | err = -ENOMEM; | |
b0d5d10f | 10207 | goto out_unlock_inode; |
d8926bb3 | 10208 | } |
33345d01 | 10209 | key.objectid = btrfs_ino(inode); |
39279cc3 | 10210 | key.offset = 0; |
962a298f | 10211 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
10212 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
10213 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
10214 | datasize); | |
54aa1f4d | 10215 | if (err) { |
b0839166 | 10216 | btrfs_free_path(path); |
b0d5d10f | 10217 | goto out_unlock_inode; |
54aa1f4d | 10218 | } |
5f39d397 CM |
10219 | leaf = path->nodes[0]; |
10220 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
10221 | struct btrfs_file_extent_item); | |
10222 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
10223 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 10224 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
10225 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
10226 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
10227 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
10228 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
10229 | ||
39279cc3 | 10230 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
10231 | write_extent_buffer(leaf, symname, ptr, name_len); |
10232 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 10233 | btrfs_free_path(path); |
5f39d397 | 10234 | |
39279cc3 | 10235 | inode->i_op = &btrfs_symlink_inode_operations; |
21fc61c7 | 10236 | inode_nohighmem(inode); |
39279cc3 | 10237 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
d899e052 | 10238 | inode_set_bytes(inode, name_len); |
f06becc4 | 10239 | btrfs_i_size_write(inode, name_len); |
54aa1f4d | 10240 | err = btrfs_update_inode(trans, root, inode); |
d50866d0 FM |
10241 | /* |
10242 | * Last step, add directory indexes for our symlink inode. This is the | |
10243 | * last step to avoid extra cleanup of these indexes if an error happens | |
10244 | * elsewhere above. | |
10245 | */ | |
10246 | if (!err) | |
10247 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
b0d5d10f | 10248 | if (err) { |
54aa1f4d | 10249 | drop_inode = 1; |
b0d5d10f CM |
10250 | goto out_unlock_inode; |
10251 | } | |
10252 | ||
10253 | unlock_new_inode(inode); | |
10254 | d_instantiate(dentry, inode); | |
39279cc3 CM |
10255 | |
10256 | out_unlock: | |
7ad85bb7 | 10257 | btrfs_end_transaction(trans, root); |
39279cc3 CM |
10258 | if (drop_inode) { |
10259 | inode_dec_link_count(inode); | |
10260 | iput(inode); | |
10261 | } | |
b53d3f5d | 10262 | btrfs_btree_balance_dirty(root); |
39279cc3 | 10263 | return err; |
b0d5d10f CM |
10264 | |
10265 | out_unlock_inode: | |
10266 | drop_inode = 1; | |
10267 | unlock_new_inode(inode); | |
10268 | goto out_unlock; | |
39279cc3 | 10269 | } |
16432985 | 10270 | |
0af3d00b JB |
10271 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
10272 | u64 start, u64 num_bytes, u64 min_size, | |
10273 | loff_t actual_len, u64 *alloc_hint, | |
10274 | struct btrfs_trans_handle *trans) | |
d899e052 | 10275 | { |
5dc562c5 JB |
10276 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
10277 | struct extent_map *em; | |
d899e052 YZ |
10278 | struct btrfs_root *root = BTRFS_I(inode)->root; |
10279 | struct btrfs_key ins; | |
d899e052 | 10280 | u64 cur_offset = start; |
55a61d1d | 10281 | u64 i_size; |
154ea289 | 10282 | u64 cur_bytes; |
0b670dc4 | 10283 | u64 last_alloc = (u64)-1; |
d899e052 | 10284 | int ret = 0; |
0af3d00b | 10285 | bool own_trans = true; |
d899e052 | 10286 | |
0af3d00b JB |
10287 | if (trans) |
10288 | own_trans = false; | |
d899e052 | 10289 | while (num_bytes > 0) { |
0af3d00b JB |
10290 | if (own_trans) { |
10291 | trans = btrfs_start_transaction(root, 3); | |
10292 | if (IS_ERR(trans)) { | |
10293 | ret = PTR_ERR(trans); | |
10294 | break; | |
10295 | } | |
5a303d5d YZ |
10296 | } |
10297 | ||
ee22184b | 10298 | cur_bytes = min_t(u64, num_bytes, SZ_256M); |
154ea289 | 10299 | cur_bytes = max(cur_bytes, min_size); |
0b670dc4 JB |
10300 | /* |
10301 | * If we are severely fragmented we could end up with really | |
10302 | * small allocations, so if the allocator is returning small | |
10303 | * chunks lets make its job easier by only searching for those | |
10304 | * sized chunks. | |
10305 | */ | |
10306 | cur_bytes = min(cur_bytes, last_alloc); | |
00361589 | 10307 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
e570fd27 | 10308 | *alloc_hint, &ins, 1, 0); |
5a303d5d | 10309 | if (ret) { |
0af3d00b JB |
10310 | if (own_trans) |
10311 | btrfs_end_transaction(trans, root); | |
a22285a6 | 10312 | break; |
d899e052 | 10313 | } |
9cfa3e34 | 10314 | btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); |
5a303d5d | 10315 | |
0b670dc4 | 10316 | last_alloc = ins.offset; |
d899e052 YZ |
10317 | ret = insert_reserved_file_extent(trans, inode, |
10318 | cur_offset, ins.objectid, | |
10319 | ins.offset, ins.offset, | |
920bbbfb | 10320 | ins.offset, 0, 0, 0, |
d899e052 | 10321 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 10322 | if (ret) { |
857cc2fc | 10323 | btrfs_free_reserved_extent(root, ins.objectid, |
e570fd27 | 10324 | ins.offset, 0); |
79787eaa JM |
10325 | btrfs_abort_transaction(trans, root, ret); |
10326 | if (own_trans) | |
10327 | btrfs_end_transaction(trans, root); | |
10328 | break; | |
10329 | } | |
31193213 | 10330 | |
a1ed835e CM |
10331 | btrfs_drop_extent_cache(inode, cur_offset, |
10332 | cur_offset + ins.offset -1, 0); | |
5a303d5d | 10333 | |
5dc562c5 JB |
10334 | em = alloc_extent_map(); |
10335 | if (!em) { | |
10336 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
10337 | &BTRFS_I(inode)->runtime_flags); | |
10338 | goto next; | |
10339 | } | |
10340 | ||
10341 | em->start = cur_offset; | |
10342 | em->orig_start = cur_offset; | |
10343 | em->len = ins.offset; | |
10344 | em->block_start = ins.objectid; | |
10345 | em->block_len = ins.offset; | |
b4939680 | 10346 | em->orig_block_len = ins.offset; |
cc95bef6 | 10347 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
10348 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
10349 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
10350 | em->generation = trans->transid; | |
10351 | ||
10352 | while (1) { | |
10353 | write_lock(&em_tree->lock); | |
09a2a8f9 | 10354 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
10355 | write_unlock(&em_tree->lock); |
10356 | if (ret != -EEXIST) | |
10357 | break; | |
10358 | btrfs_drop_extent_cache(inode, cur_offset, | |
10359 | cur_offset + ins.offset - 1, | |
10360 | 0); | |
10361 | } | |
10362 | free_extent_map(em); | |
10363 | next: | |
d899e052 YZ |
10364 | num_bytes -= ins.offset; |
10365 | cur_offset += ins.offset; | |
efa56464 | 10366 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 10367 | |
0c4d2d95 | 10368 | inode_inc_iversion(inode); |
04b285f3 | 10369 | inode->i_ctime = current_fs_time(inode->i_sb); |
6cbff00f | 10370 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 10371 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
10372 | (actual_len > inode->i_size) && |
10373 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 10374 | if (cur_offset > actual_len) |
55a61d1d | 10375 | i_size = actual_len; |
d1ea6a61 | 10376 | else |
55a61d1d JB |
10377 | i_size = cur_offset; |
10378 | i_size_write(inode, i_size); | |
10379 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
10380 | } |
10381 | ||
d899e052 | 10382 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
10383 | |
10384 | if (ret) { | |
10385 | btrfs_abort_transaction(trans, root, ret); | |
10386 | if (own_trans) | |
10387 | btrfs_end_transaction(trans, root); | |
10388 | break; | |
10389 | } | |
d899e052 | 10390 | |
0af3d00b JB |
10391 | if (own_trans) |
10392 | btrfs_end_transaction(trans, root); | |
5a303d5d | 10393 | } |
d899e052 YZ |
10394 | return ret; |
10395 | } | |
10396 | ||
0af3d00b JB |
10397 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
10398 | u64 start, u64 num_bytes, u64 min_size, | |
10399 | loff_t actual_len, u64 *alloc_hint) | |
10400 | { | |
10401 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10402 | min_size, actual_len, alloc_hint, | |
10403 | NULL); | |
10404 | } | |
10405 | ||
10406 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
10407 | struct btrfs_trans_handle *trans, int mode, | |
10408 | u64 start, u64 num_bytes, u64 min_size, | |
10409 | loff_t actual_len, u64 *alloc_hint) | |
10410 | { | |
10411 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
10412 | min_size, actual_len, alloc_hint, trans); | |
10413 | } | |
10414 | ||
e6dcd2dc CM |
10415 | static int btrfs_set_page_dirty(struct page *page) |
10416 | { | |
e6dcd2dc CM |
10417 | return __set_page_dirty_nobuffers(page); |
10418 | } | |
10419 | ||
10556cb2 | 10420 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 10421 | { |
b83cc969 | 10422 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 10423 | umode_t mode = inode->i_mode; |
b83cc969 | 10424 | |
cb6db4e5 JM |
10425 | if (mask & MAY_WRITE && |
10426 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
10427 | if (btrfs_root_readonly(root)) | |
10428 | return -EROFS; | |
10429 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
10430 | return -EACCES; | |
10431 | } | |
2830ba7f | 10432 | return generic_permission(inode, mask); |
fdebe2bd | 10433 | } |
39279cc3 | 10434 | |
ef3b9af5 FM |
10435 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
10436 | { | |
10437 | struct btrfs_trans_handle *trans; | |
10438 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
10439 | struct inode *inode = NULL; | |
10440 | u64 objectid; | |
10441 | u64 index; | |
10442 | int ret = 0; | |
10443 | ||
10444 | /* | |
10445 | * 5 units required for adding orphan entry | |
10446 | */ | |
10447 | trans = btrfs_start_transaction(root, 5); | |
10448 | if (IS_ERR(trans)) | |
10449 | return PTR_ERR(trans); | |
10450 | ||
10451 | ret = btrfs_find_free_ino(root, &objectid); | |
10452 | if (ret) | |
10453 | goto out; | |
10454 | ||
10455 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
10456 | btrfs_ino(dir), objectid, mode, &index); | |
10457 | if (IS_ERR(inode)) { | |
10458 | ret = PTR_ERR(inode); | |
10459 | inode = NULL; | |
10460 | goto out; | |
10461 | } | |
10462 | ||
ef3b9af5 FM |
10463 | inode->i_fop = &btrfs_file_operations; |
10464 | inode->i_op = &btrfs_file_inode_operations; | |
10465 | ||
10466 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
10467 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
10468 | ||
b0d5d10f CM |
10469 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
10470 | if (ret) | |
10471 | goto out_inode; | |
10472 | ||
10473 | ret = btrfs_update_inode(trans, root, inode); | |
10474 | if (ret) | |
10475 | goto out_inode; | |
ef3b9af5 FM |
10476 | ret = btrfs_orphan_add(trans, inode); |
10477 | if (ret) | |
b0d5d10f | 10478 | goto out_inode; |
ef3b9af5 | 10479 | |
5762b5c9 FM |
10480 | /* |
10481 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
10482 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
10483 | * through: | |
10484 | * | |
10485 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
10486 | */ | |
10487 | set_nlink(inode, 1); | |
b0d5d10f | 10488 | unlock_new_inode(inode); |
ef3b9af5 FM |
10489 | d_tmpfile(dentry, inode); |
10490 | mark_inode_dirty(inode); | |
10491 | ||
10492 | out: | |
10493 | btrfs_end_transaction(trans, root); | |
10494 | if (ret) | |
10495 | iput(inode); | |
10496 | btrfs_balance_delayed_items(root); | |
10497 | btrfs_btree_balance_dirty(root); | |
ef3b9af5 | 10498 | return ret; |
b0d5d10f CM |
10499 | |
10500 | out_inode: | |
10501 | unlock_new_inode(inode); | |
10502 | goto out; | |
10503 | ||
ef3b9af5 FM |
10504 | } |
10505 | ||
b38ef71c FM |
10506 | /* Inspired by filemap_check_errors() */ |
10507 | int btrfs_inode_check_errors(struct inode *inode) | |
10508 | { | |
10509 | int ret = 0; | |
10510 | ||
10511 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && | |
10512 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) | |
10513 | ret = -ENOSPC; | |
10514 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && | |
10515 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) | |
10516 | ret = -EIO; | |
10517 | ||
10518 | return ret; | |
10519 | } | |
10520 | ||
6e1d5dcc | 10521 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 10522 | .getattr = btrfs_getattr, |
39279cc3 CM |
10523 | .lookup = btrfs_lookup, |
10524 | .create = btrfs_create, | |
10525 | .unlink = btrfs_unlink, | |
10526 | .link = btrfs_link, | |
10527 | .mkdir = btrfs_mkdir, | |
10528 | .rmdir = btrfs_rmdir, | |
80ace85c | 10529 | .rename2 = btrfs_rename2, |
39279cc3 CM |
10530 | .symlink = btrfs_symlink, |
10531 | .setattr = btrfs_setattr, | |
618e21d5 | 10532 | .mknod = btrfs_mknod, |
e0d46f5c | 10533 | .setxattr = generic_setxattr, |
9172abbc | 10534 | .getxattr = generic_getxattr, |
5103e947 | 10535 | .listxattr = btrfs_listxattr, |
e0d46f5c | 10536 | .removexattr = generic_removexattr, |
fdebe2bd | 10537 | .permission = btrfs_permission, |
4e34e719 | 10538 | .get_acl = btrfs_get_acl, |
996a710d | 10539 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10540 | .update_time = btrfs_update_time, |
ef3b9af5 | 10541 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10542 | }; |
6e1d5dcc | 10543 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 10544 | .lookup = btrfs_lookup, |
fdebe2bd | 10545 | .permission = btrfs_permission, |
4e34e719 | 10546 | .get_acl = btrfs_get_acl, |
996a710d | 10547 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10548 | .update_time = btrfs_update_time, |
39279cc3 | 10549 | }; |
76dda93c | 10550 | |
828c0950 | 10551 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10552 | .llseek = generic_file_llseek, |
10553 | .read = generic_read_dir, | |
02dbfc99 | 10554 | .iterate_shared = btrfs_real_readdir, |
34287aa3 | 10555 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10556 | #ifdef CONFIG_COMPAT |
4c63c245 | 10557 | .compat_ioctl = btrfs_compat_ioctl, |
39279cc3 | 10558 | #endif |
6bf13c0c | 10559 | .release = btrfs_release_file, |
e02119d5 | 10560 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10561 | }; |
10562 | ||
20e5506b | 10563 | static const struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 10564 | .fill_delalloc = run_delalloc_range, |
065631f6 | 10565 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 10566 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 10567 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 10568 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 10569 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
10570 | .set_bit_hook = btrfs_set_bit_hook, |
10571 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
10572 | .merge_extent_hook = btrfs_merge_extent_hook, |
10573 | .split_extent_hook = btrfs_split_extent_hook, | |
07157aac CM |
10574 | }; |
10575 | ||
35054394 CM |
10576 | /* |
10577 | * btrfs doesn't support the bmap operation because swapfiles | |
10578 | * use bmap to make a mapping of extents in the file. They assume | |
10579 | * these extents won't change over the life of the file and they | |
10580 | * use the bmap result to do IO directly to the drive. | |
10581 | * | |
10582 | * the btrfs bmap call would return logical addresses that aren't | |
10583 | * suitable for IO and they also will change frequently as COW | |
10584 | * operations happen. So, swapfile + btrfs == corruption. | |
10585 | * | |
10586 | * For now we're avoiding this by dropping bmap. | |
10587 | */ | |
7f09410b | 10588 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10589 | .readpage = btrfs_readpage, |
10590 | .writepage = btrfs_writepage, | |
b293f02e | 10591 | .writepages = btrfs_writepages, |
3ab2fb5a | 10592 | .readpages = btrfs_readpages, |
16432985 | 10593 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10594 | .invalidatepage = btrfs_invalidatepage, |
10595 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 10596 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10597 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
10598 | }; |
10599 | ||
7f09410b | 10600 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
10601 | .readpage = btrfs_readpage, |
10602 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
10603 | .invalidatepage = btrfs_invalidatepage, |
10604 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
10605 | }; |
10606 | ||
6e1d5dcc | 10607 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10608 | .getattr = btrfs_getattr, |
10609 | .setattr = btrfs_setattr, | |
e0d46f5c | 10610 | .setxattr = generic_setxattr, |
9172abbc | 10611 | .getxattr = generic_getxattr, |
5103e947 | 10612 | .listxattr = btrfs_listxattr, |
e0d46f5c | 10613 | .removexattr = generic_removexattr, |
fdebe2bd | 10614 | .permission = btrfs_permission, |
1506fcc8 | 10615 | .fiemap = btrfs_fiemap, |
4e34e719 | 10616 | .get_acl = btrfs_get_acl, |
996a710d | 10617 | .set_acl = btrfs_set_acl, |
e41f941a | 10618 | .update_time = btrfs_update_time, |
39279cc3 | 10619 | }; |
6e1d5dcc | 10620 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10621 | .getattr = btrfs_getattr, |
10622 | .setattr = btrfs_setattr, | |
fdebe2bd | 10623 | .permission = btrfs_permission, |
e0d46f5c | 10624 | .setxattr = generic_setxattr, |
9172abbc | 10625 | .getxattr = generic_getxattr, |
33268eaf | 10626 | .listxattr = btrfs_listxattr, |
e0d46f5c | 10627 | .removexattr = generic_removexattr, |
4e34e719 | 10628 | .get_acl = btrfs_get_acl, |
996a710d | 10629 | .set_acl = btrfs_set_acl, |
e41f941a | 10630 | .update_time = btrfs_update_time, |
618e21d5 | 10631 | }; |
6e1d5dcc | 10632 | static const struct inode_operations btrfs_symlink_inode_operations = { |
39279cc3 | 10633 | .readlink = generic_readlink, |
6b255391 | 10634 | .get_link = page_get_link, |
f209561a | 10635 | .getattr = btrfs_getattr, |
22c44fe6 | 10636 | .setattr = btrfs_setattr, |
fdebe2bd | 10637 | .permission = btrfs_permission, |
e0d46f5c | 10638 | .setxattr = generic_setxattr, |
9172abbc | 10639 | .getxattr = generic_getxattr, |
0279b4cd | 10640 | .listxattr = btrfs_listxattr, |
e0d46f5c | 10641 | .removexattr = generic_removexattr, |
e41f941a | 10642 | .update_time = btrfs_update_time, |
39279cc3 | 10643 | }; |
76dda93c | 10644 | |
82d339d9 | 10645 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 10646 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 10647 | .d_release = btrfs_dentry_release, |
76dda93c | 10648 | }; |