]>
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" |
39279cc3 CM |
63 | |
64 | struct btrfs_iget_args { | |
90d3e592 | 65 | struct btrfs_key *location; |
39279cc3 CM |
66 | struct btrfs_root *root; |
67 | }; | |
68 | ||
6e1d5dcc AD |
69 | static const struct inode_operations btrfs_dir_inode_operations; |
70 | static const struct inode_operations btrfs_symlink_inode_operations; | |
71 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
72 | static const struct inode_operations btrfs_special_inode_operations; | |
73 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b AD |
74 | static const struct address_space_operations btrfs_aops; |
75 | static const struct address_space_operations btrfs_symlink_aops; | |
828c0950 | 76 | static const struct file_operations btrfs_dir_file_operations; |
d1310b2e | 77 | static struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
78 | |
79 | static struct kmem_cache *btrfs_inode_cachep; | |
8ccf6f19 | 80 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
39279cc3 CM |
81 | struct kmem_cache *btrfs_trans_handle_cachep; |
82 | struct kmem_cache *btrfs_transaction_cachep; | |
39279cc3 | 83 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 84 | struct kmem_cache *btrfs_free_space_cachep; |
39279cc3 CM |
85 | |
86 | #define S_SHIFT 12 | |
87 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { | |
88 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, | |
89 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
90 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
91 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
92 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
93 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
94 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
95 | }; | |
96 | ||
3972f260 | 97 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
a41ad394 | 98 | static int btrfs_truncate(struct inode *inode); |
5fd02043 | 99 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
771ed689 CM |
100 | static noinline int cow_file_range(struct inode *inode, |
101 | struct page *locked_page, | |
102 | u64 start, u64 end, int *page_started, | |
103 | unsigned long *nr_written, int unlock); | |
70c8a91c JB |
104 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
105 | u64 len, u64 orig_start, | |
106 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
107 | u64 orig_block_len, u64 ram_bytes, |
108 | int type); | |
7b128766 | 109 | |
48a3b636 | 110 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 111 | |
6a3891c5 JB |
112 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
113 | void btrfs_test_inode_set_ops(struct inode *inode) | |
114 | { | |
115 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
116 | } | |
117 | #endif | |
118 | ||
f34f57a3 | 119 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
120 | struct inode *inode, struct inode *dir, |
121 | const struct qstr *qstr) | |
0279b4cd JO |
122 | { |
123 | int err; | |
124 | ||
f34f57a3 | 125 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 126 | if (!err) |
2a7dba39 | 127 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
128 | return err; |
129 | } | |
130 | ||
c8b97818 CM |
131 | /* |
132 | * this does all the hard work for inserting an inline extent into | |
133 | * the btree. The caller should have done a btrfs_drop_extents so that | |
134 | * no overlapping inline items exist in the btree | |
135 | */ | |
40f76580 | 136 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 137 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
138 | struct btrfs_root *root, struct inode *inode, |
139 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 140 | int compress_type, |
c8b97818 CM |
141 | struct page **compressed_pages) |
142 | { | |
c8b97818 CM |
143 | struct extent_buffer *leaf; |
144 | struct page *page = NULL; | |
145 | char *kaddr; | |
146 | unsigned long ptr; | |
147 | struct btrfs_file_extent_item *ei; | |
148 | int err = 0; | |
149 | int ret; | |
150 | size_t cur_size = size; | |
c8b97818 | 151 | unsigned long offset; |
c8b97818 | 152 | |
fe3f566c | 153 | if (compressed_size && compressed_pages) |
c8b97818 | 154 | cur_size = compressed_size; |
c8b97818 | 155 | |
1acae57b | 156 | inode_add_bytes(inode, size); |
c8b97818 | 157 | |
1acae57b FDBM |
158 | if (!extent_inserted) { |
159 | struct btrfs_key key; | |
160 | size_t datasize; | |
c8b97818 | 161 | |
1acae57b FDBM |
162 | key.objectid = btrfs_ino(inode); |
163 | key.offset = start; | |
962a298f | 164 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 165 | |
1acae57b FDBM |
166 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
167 | path->leave_spinning = 1; | |
168 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
169 | datasize); | |
170 | if (ret) { | |
171 | err = ret; | |
172 | goto fail; | |
173 | } | |
c8b97818 CM |
174 | } |
175 | leaf = path->nodes[0]; | |
176 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
177 | struct btrfs_file_extent_item); | |
178 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
179 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
180 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
181 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
182 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
183 | ptr = btrfs_file_extent_inline_start(ei); | |
184 | ||
261507a0 | 185 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
186 | struct page *cpage; |
187 | int i = 0; | |
d397712b | 188 | while (compressed_size > 0) { |
c8b97818 | 189 | cpage = compressed_pages[i]; |
5b050f04 | 190 | cur_size = min_t(unsigned long, compressed_size, |
c8b97818 CM |
191 | PAGE_CACHE_SIZE); |
192 | ||
7ac687d9 | 193 | kaddr = kmap_atomic(cpage); |
c8b97818 | 194 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 195 | kunmap_atomic(kaddr); |
c8b97818 CM |
196 | |
197 | i++; | |
198 | ptr += cur_size; | |
199 | compressed_size -= cur_size; | |
200 | } | |
201 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 202 | compress_type); |
c8b97818 CM |
203 | } else { |
204 | page = find_get_page(inode->i_mapping, | |
205 | start >> PAGE_CACHE_SHIFT); | |
206 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
7ac687d9 | 207 | kaddr = kmap_atomic(page); |
c8b97818 CM |
208 | offset = start & (PAGE_CACHE_SIZE - 1); |
209 | write_extent_buffer(leaf, kaddr + offset, ptr, size); | |
7ac687d9 | 210 | kunmap_atomic(kaddr); |
c8b97818 CM |
211 | page_cache_release(page); |
212 | } | |
213 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 214 | btrfs_release_path(path); |
c8b97818 | 215 | |
c2167754 YZ |
216 | /* |
217 | * we're an inline extent, so nobody can | |
218 | * extend the file past i_size without locking | |
219 | * a page we already have locked. | |
220 | * | |
221 | * We must do any isize and inode updates | |
222 | * before we unlock the pages. Otherwise we | |
223 | * could end up racing with unlink. | |
224 | */ | |
c8b97818 | 225 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 226 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 227 | |
79787eaa | 228 | return ret; |
c8b97818 | 229 | fail: |
c8b97818 CM |
230 | return err; |
231 | } | |
232 | ||
233 | ||
234 | /* | |
235 | * conditionally insert an inline extent into the file. This | |
236 | * does the checks required to make sure the data is small enough | |
237 | * to fit as an inline extent. | |
238 | */ | |
00361589 JB |
239 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
240 | struct inode *inode, u64 start, | |
241 | u64 end, size_t compressed_size, | |
242 | int compress_type, | |
243 | struct page **compressed_pages) | |
c8b97818 | 244 | { |
00361589 | 245 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
246 | u64 isize = i_size_read(inode); |
247 | u64 actual_end = min(end + 1, isize); | |
248 | u64 inline_len = actual_end - start; | |
fda2832f | 249 | u64 aligned_end = ALIGN(end, root->sectorsize); |
c8b97818 CM |
250 | u64 data_len = inline_len; |
251 | int ret; | |
1acae57b FDBM |
252 | struct btrfs_path *path; |
253 | int extent_inserted = 0; | |
254 | u32 extent_item_size; | |
c8b97818 CM |
255 | |
256 | if (compressed_size) | |
257 | data_len = compressed_size; | |
258 | ||
259 | if (start > 0 || | |
354877be WS |
260 | actual_end > PAGE_CACHE_SIZE || |
261 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || | |
c8b97818 CM |
262 | (!compressed_size && |
263 | (actual_end & (root->sectorsize - 1)) == 0) || | |
264 | end + 1 < isize || | |
265 | data_len > root->fs_info->max_inline) { | |
266 | return 1; | |
267 | } | |
268 | ||
1acae57b FDBM |
269 | path = btrfs_alloc_path(); |
270 | if (!path) | |
271 | return -ENOMEM; | |
272 | ||
00361589 | 273 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
274 | if (IS_ERR(trans)) { |
275 | btrfs_free_path(path); | |
00361589 | 276 | return PTR_ERR(trans); |
1acae57b | 277 | } |
00361589 JB |
278 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
279 | ||
1acae57b FDBM |
280 | if (compressed_size && compressed_pages) |
281 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
282 | compressed_size); | |
283 | else | |
284 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
285 | inline_len); | |
286 | ||
287 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
288 | start, aligned_end, NULL, | |
289 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 JB |
290 | if (ret) { |
291 | btrfs_abort_transaction(trans, root, ret); | |
292 | goto out; | |
293 | } | |
c8b97818 CM |
294 | |
295 | if (isize > actual_end) | |
296 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
297 | ret = insert_inline_extent(trans, path, extent_inserted, |
298 | root, inode, start, | |
c8b97818 | 299 | inline_len, compressed_size, |
fe3f566c | 300 | compress_type, compressed_pages); |
2adcac1a | 301 | if (ret && ret != -ENOSPC) { |
79787eaa | 302 | btrfs_abort_transaction(trans, root, ret); |
00361589 | 303 | goto out; |
2adcac1a | 304 | } else if (ret == -ENOSPC) { |
00361589 JB |
305 | ret = 1; |
306 | goto out; | |
79787eaa | 307 | } |
2adcac1a | 308 | |
bdc20e67 | 309 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
0ca1f7ce | 310 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
a1ed835e | 311 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
00361589 | 312 | out: |
1acae57b | 313 | btrfs_free_path(path); |
00361589 JB |
314 | btrfs_end_transaction(trans, root); |
315 | return ret; | |
c8b97818 CM |
316 | } |
317 | ||
771ed689 CM |
318 | struct async_extent { |
319 | u64 start; | |
320 | u64 ram_size; | |
321 | u64 compressed_size; | |
322 | struct page **pages; | |
323 | unsigned long nr_pages; | |
261507a0 | 324 | int compress_type; |
771ed689 CM |
325 | struct list_head list; |
326 | }; | |
327 | ||
328 | struct async_cow { | |
329 | struct inode *inode; | |
330 | struct btrfs_root *root; | |
331 | struct page *locked_page; | |
332 | u64 start; | |
333 | u64 end; | |
334 | struct list_head extents; | |
335 | struct btrfs_work work; | |
336 | }; | |
337 | ||
338 | static noinline int add_async_extent(struct async_cow *cow, | |
339 | u64 start, u64 ram_size, | |
340 | u64 compressed_size, | |
341 | struct page **pages, | |
261507a0 LZ |
342 | unsigned long nr_pages, |
343 | int compress_type) | |
771ed689 CM |
344 | { |
345 | struct async_extent *async_extent; | |
346 | ||
347 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 348 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
349 | async_extent->start = start; |
350 | async_extent->ram_size = ram_size; | |
351 | async_extent->compressed_size = compressed_size; | |
352 | async_extent->pages = pages; | |
353 | async_extent->nr_pages = nr_pages; | |
261507a0 | 354 | async_extent->compress_type = compress_type; |
771ed689 CM |
355 | list_add_tail(&async_extent->list, &cow->extents); |
356 | return 0; | |
357 | } | |
358 | ||
f79707b0 WS |
359 | static inline int inode_need_compress(struct inode *inode) |
360 | { | |
361 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
362 | ||
363 | /* force compress */ | |
364 | if (btrfs_test_opt(root, FORCE_COMPRESS)) | |
365 | return 1; | |
366 | /* bad compression ratios */ | |
367 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
368 | return 0; | |
369 | if (btrfs_test_opt(root, COMPRESS) || | |
370 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || | |
371 | BTRFS_I(inode)->force_compress) | |
372 | return 1; | |
373 | return 0; | |
374 | } | |
375 | ||
d352ac68 | 376 | /* |
771ed689 CM |
377 | * we create compressed extents in two phases. The first |
378 | * phase compresses a range of pages that have already been | |
379 | * locked (both pages and state bits are locked). | |
c8b97818 | 380 | * |
771ed689 CM |
381 | * This is done inside an ordered work queue, and the compression |
382 | * is spread across many cpus. The actual IO submission is step | |
383 | * two, and the ordered work queue takes care of making sure that | |
384 | * happens in the same order things were put onto the queue by | |
385 | * writepages and friends. | |
c8b97818 | 386 | * |
771ed689 CM |
387 | * If this code finds it can't get good compression, it puts an |
388 | * entry onto the work queue to write the uncompressed bytes. This | |
389 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
390 | * are written in the same order that the flusher thread sent them |
391 | * down. | |
d352ac68 | 392 | */ |
c44f649e | 393 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
394 | struct page *locked_page, |
395 | u64 start, u64 end, | |
396 | struct async_cow *async_cow, | |
397 | int *num_added) | |
b888db2b CM |
398 | { |
399 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
db94535d | 400 | u64 num_bytes; |
db94535d | 401 | u64 blocksize = root->sectorsize; |
c8b97818 | 402 | u64 actual_end; |
42dc7bab | 403 | u64 isize = i_size_read(inode); |
e6dcd2dc | 404 | int ret = 0; |
c8b97818 CM |
405 | struct page **pages = NULL; |
406 | unsigned long nr_pages; | |
407 | unsigned long nr_pages_ret = 0; | |
408 | unsigned long total_compressed = 0; | |
409 | unsigned long total_in = 0; | |
410 | unsigned long max_compressed = 128 * 1024; | |
771ed689 | 411 | unsigned long max_uncompressed = 128 * 1024; |
c8b97818 CM |
412 | int i; |
413 | int will_compress; | |
261507a0 | 414 | int compress_type = root->fs_info->compress_type; |
4adaa611 | 415 | int redirty = 0; |
b888db2b | 416 | |
4cb13e5d LB |
417 | /* if this is a small write inside eof, kick off a defrag */ |
418 | if ((end - start + 1) < 16 * 1024 && | |
419 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
4cb5300b CM |
420 | btrfs_add_inode_defrag(NULL, inode); |
421 | ||
42dc7bab | 422 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
423 | again: |
424 | will_compress = 0; | |
425 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; | |
426 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); | |
be20aa9d | 427 | |
f03d9301 CM |
428 | /* |
429 | * we don't want to send crud past the end of i_size through | |
430 | * compression, that's just a waste of CPU time. So, if the | |
431 | * end of the file is before the start of our current | |
432 | * requested range of bytes, we bail out to the uncompressed | |
433 | * cleanup code that can deal with all of this. | |
434 | * | |
435 | * It isn't really the fastest way to fix things, but this is a | |
436 | * very uncommon corner. | |
437 | */ | |
438 | if (actual_end <= start) | |
439 | goto cleanup_and_bail_uncompressed; | |
440 | ||
c8b97818 CM |
441 | total_compressed = actual_end - start; |
442 | ||
4bcbb332 SW |
443 | /* |
444 | * skip compression for a small file range(<=blocksize) that | |
445 | * isn't an inline extent, since it dosen't save disk space at all. | |
446 | */ | |
447 | if (total_compressed <= blocksize && | |
448 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
449 | goto cleanup_and_bail_uncompressed; | |
450 | ||
c8b97818 CM |
451 | /* we want to make sure that amount of ram required to uncompress |
452 | * an extent is reasonable, so we limit the total size in ram | |
771ed689 CM |
453 | * of a compressed extent to 128k. This is a crucial number |
454 | * because it also controls how easily we can spread reads across | |
455 | * cpus for decompression. | |
456 | * | |
457 | * We also want to make sure the amount of IO required to do | |
458 | * a random read is reasonably small, so we limit the size of | |
459 | * a compressed extent to 128k. | |
c8b97818 CM |
460 | */ |
461 | total_compressed = min(total_compressed, max_uncompressed); | |
fda2832f | 462 | num_bytes = ALIGN(end - start + 1, blocksize); |
be20aa9d | 463 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
464 | total_in = 0; |
465 | ret = 0; | |
db94535d | 466 | |
771ed689 CM |
467 | /* |
468 | * we do compression for mount -o compress and when the | |
469 | * inode has not been flagged as nocompress. This flag can | |
470 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 471 | */ |
f79707b0 | 472 | if (inode_need_compress(inode)) { |
c8b97818 | 473 | WARN_ON(pages); |
31e818fe | 474 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
475 | if (!pages) { |
476 | /* just bail out to the uncompressed code */ | |
477 | goto cont; | |
478 | } | |
c8b97818 | 479 | |
261507a0 LZ |
480 | if (BTRFS_I(inode)->force_compress) |
481 | compress_type = BTRFS_I(inode)->force_compress; | |
482 | ||
4adaa611 CM |
483 | /* |
484 | * we need to call clear_page_dirty_for_io on each | |
485 | * page in the range. Otherwise applications with the file | |
486 | * mmap'd can wander in and change the page contents while | |
487 | * we are compressing them. | |
488 | * | |
489 | * If the compression fails for any reason, we set the pages | |
490 | * dirty again later on. | |
491 | */ | |
492 | extent_range_clear_dirty_for_io(inode, start, end); | |
493 | redirty = 1; | |
261507a0 LZ |
494 | ret = btrfs_compress_pages(compress_type, |
495 | inode->i_mapping, start, | |
496 | total_compressed, pages, | |
497 | nr_pages, &nr_pages_ret, | |
498 | &total_in, | |
499 | &total_compressed, | |
500 | max_compressed); | |
c8b97818 CM |
501 | |
502 | if (!ret) { | |
503 | unsigned long offset = total_compressed & | |
504 | (PAGE_CACHE_SIZE - 1); | |
505 | struct page *page = pages[nr_pages_ret - 1]; | |
506 | char *kaddr; | |
507 | ||
508 | /* zero the tail end of the last page, we might be | |
509 | * sending it down to disk | |
510 | */ | |
511 | if (offset) { | |
7ac687d9 | 512 | kaddr = kmap_atomic(page); |
c8b97818 CM |
513 | memset(kaddr + offset, 0, |
514 | PAGE_CACHE_SIZE - offset); | |
7ac687d9 | 515 | kunmap_atomic(kaddr); |
c8b97818 CM |
516 | } |
517 | will_compress = 1; | |
518 | } | |
519 | } | |
560f7d75 | 520 | cont: |
c8b97818 CM |
521 | if (start == 0) { |
522 | /* lets try to make an inline extent */ | |
771ed689 | 523 | if (ret || total_in < (actual_end - start)) { |
c8b97818 | 524 | /* we didn't compress the entire range, try |
771ed689 | 525 | * to make an uncompressed inline extent. |
c8b97818 | 526 | */ |
00361589 JB |
527 | ret = cow_file_range_inline(root, inode, start, end, |
528 | 0, 0, NULL); | |
c8b97818 | 529 | } else { |
771ed689 | 530 | /* try making a compressed inline extent */ |
00361589 | 531 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
532 | total_compressed, |
533 | compress_type, pages); | |
c8b97818 | 534 | } |
79787eaa | 535 | if (ret <= 0) { |
151a41bc JB |
536 | unsigned long clear_flags = EXTENT_DELALLOC | |
537 | EXTENT_DEFRAG; | |
e6eb4314 FM |
538 | unsigned long page_error_op; |
539 | ||
151a41bc | 540 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
e6eb4314 | 541 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 542 | |
771ed689 | 543 | /* |
79787eaa JM |
544 | * inline extent creation worked or returned error, |
545 | * we don't need to create any more async work items. | |
546 | * Unlock and free up our temp pages. | |
771ed689 | 547 | */ |
c2790a2e | 548 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
151a41bc | 549 | clear_flags, PAGE_UNLOCK | |
c2790a2e JB |
550 | PAGE_CLEAR_DIRTY | |
551 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 552 | page_error_op | |
c2790a2e | 553 | PAGE_END_WRITEBACK); |
c8b97818 CM |
554 | goto free_pages_out; |
555 | } | |
556 | } | |
557 | ||
558 | if (will_compress) { | |
559 | /* | |
560 | * we aren't doing an inline extent round the compressed size | |
561 | * up to a block size boundary so the allocator does sane | |
562 | * things | |
563 | */ | |
fda2832f | 564 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
565 | |
566 | /* | |
567 | * one last check to make sure the compression is really a | |
568 | * win, compare the page count read with the blocks on disk | |
569 | */ | |
fda2832f | 570 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
c8b97818 CM |
571 | if (total_compressed >= total_in) { |
572 | will_compress = 0; | |
573 | } else { | |
c8b97818 CM |
574 | num_bytes = total_in; |
575 | } | |
576 | } | |
577 | if (!will_compress && pages) { | |
578 | /* | |
579 | * the compression code ran but failed to make things smaller, | |
580 | * free any pages it allocated and our page pointer array | |
581 | */ | |
582 | for (i = 0; i < nr_pages_ret; i++) { | |
70b99e69 | 583 | WARN_ON(pages[i]->mapping); |
c8b97818 CM |
584 | page_cache_release(pages[i]); |
585 | } | |
586 | kfree(pages); | |
587 | pages = NULL; | |
588 | total_compressed = 0; | |
589 | nr_pages_ret = 0; | |
590 | ||
591 | /* flag the file so we don't compress in the future */ | |
1e701a32 CM |
592 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
593 | !(BTRFS_I(inode)->force_compress)) { | |
a555f810 | 594 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 595 | } |
c8b97818 | 596 | } |
771ed689 CM |
597 | if (will_compress) { |
598 | *num_added += 1; | |
c8b97818 | 599 | |
771ed689 CM |
600 | /* the async work queues will take care of doing actual |
601 | * allocation on disk for these compressed pages, | |
602 | * and will submit them to the elevator. | |
603 | */ | |
604 | add_async_extent(async_cow, start, num_bytes, | |
261507a0 LZ |
605 | total_compressed, pages, nr_pages_ret, |
606 | compress_type); | |
179e29e4 | 607 | |
24ae6365 | 608 | if (start + num_bytes < end) { |
771ed689 CM |
609 | start += num_bytes; |
610 | pages = NULL; | |
611 | cond_resched(); | |
612 | goto again; | |
613 | } | |
614 | } else { | |
f03d9301 | 615 | cleanup_and_bail_uncompressed: |
771ed689 CM |
616 | /* |
617 | * No compression, but we still need to write the pages in | |
618 | * the file we've been given so far. redirty the locked | |
619 | * page if it corresponds to our extent and set things up | |
620 | * for the async work queue to run cow_file_range to do | |
621 | * the normal delalloc dance | |
622 | */ | |
623 | if (page_offset(locked_page) >= start && | |
624 | page_offset(locked_page) <= end) { | |
625 | __set_page_dirty_nobuffers(locked_page); | |
626 | /* unlocked later on in the async handlers */ | |
627 | } | |
4adaa611 CM |
628 | if (redirty) |
629 | extent_range_redirty_for_io(inode, start, end); | |
261507a0 LZ |
630 | add_async_extent(async_cow, start, end - start + 1, |
631 | 0, NULL, 0, BTRFS_COMPRESS_NONE); | |
771ed689 CM |
632 | *num_added += 1; |
633 | } | |
3b951516 | 634 | |
c44f649e | 635 | return; |
771ed689 CM |
636 | |
637 | free_pages_out: | |
638 | for (i = 0; i < nr_pages_ret; i++) { | |
639 | WARN_ON(pages[i]->mapping); | |
640 | page_cache_release(pages[i]); | |
641 | } | |
d397712b | 642 | kfree(pages); |
771ed689 | 643 | } |
771ed689 | 644 | |
40ae837b FM |
645 | static void free_async_extent_pages(struct async_extent *async_extent) |
646 | { | |
647 | int i; | |
648 | ||
649 | if (!async_extent->pages) | |
650 | return; | |
651 | ||
652 | for (i = 0; i < async_extent->nr_pages; i++) { | |
653 | WARN_ON(async_extent->pages[i]->mapping); | |
654 | page_cache_release(async_extent->pages[i]); | |
655 | } | |
656 | kfree(async_extent->pages); | |
657 | async_extent->nr_pages = 0; | |
658 | async_extent->pages = NULL; | |
771ed689 CM |
659 | } |
660 | ||
661 | /* | |
662 | * phase two of compressed writeback. This is the ordered portion | |
663 | * of the code, which only gets called in the order the work was | |
664 | * queued. We walk all the async extents created by compress_file_range | |
665 | * and send them down to the disk. | |
666 | */ | |
dec8f175 | 667 | static noinline void submit_compressed_extents(struct inode *inode, |
771ed689 CM |
668 | struct async_cow *async_cow) |
669 | { | |
670 | struct async_extent *async_extent; | |
671 | u64 alloc_hint = 0; | |
771ed689 CM |
672 | struct btrfs_key ins; |
673 | struct extent_map *em; | |
674 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
675 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
676 | struct extent_io_tree *io_tree; | |
f5a84ee3 | 677 | int ret = 0; |
771ed689 | 678 | |
3e04e7f1 | 679 | again: |
d397712b | 680 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
681 | async_extent = list_entry(async_cow->extents.next, |
682 | struct async_extent, list); | |
683 | list_del(&async_extent->list); | |
c8b97818 | 684 | |
771ed689 CM |
685 | io_tree = &BTRFS_I(inode)->io_tree; |
686 | ||
f5a84ee3 | 687 | retry: |
771ed689 CM |
688 | /* did the compression code fall back to uncompressed IO? */ |
689 | if (!async_extent->pages) { | |
690 | int page_started = 0; | |
691 | unsigned long nr_written = 0; | |
692 | ||
693 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 694 | async_extent->start + |
d0082371 | 695 | async_extent->ram_size - 1); |
771ed689 CM |
696 | |
697 | /* allocate blocks */ | |
f5a84ee3 JB |
698 | ret = cow_file_range(inode, async_cow->locked_page, |
699 | async_extent->start, | |
700 | async_extent->start + | |
701 | async_extent->ram_size - 1, | |
702 | &page_started, &nr_written, 0); | |
771ed689 | 703 | |
79787eaa JM |
704 | /* JDM XXX */ |
705 | ||
771ed689 CM |
706 | /* |
707 | * if page_started, cow_file_range inserted an | |
708 | * inline extent and took care of all the unlocking | |
709 | * and IO for us. Otherwise, we need to submit | |
710 | * all those pages down to the drive. | |
711 | */ | |
f5a84ee3 | 712 | if (!page_started && !ret) |
771ed689 CM |
713 | extent_write_locked_range(io_tree, |
714 | inode, async_extent->start, | |
d397712b | 715 | async_extent->start + |
771ed689 CM |
716 | async_extent->ram_size - 1, |
717 | btrfs_get_extent, | |
718 | WB_SYNC_ALL); | |
3e04e7f1 JB |
719 | else if (ret) |
720 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
721 | kfree(async_extent); |
722 | cond_resched(); | |
723 | continue; | |
724 | } | |
725 | ||
726 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 727 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 728 | |
00361589 | 729 | ret = btrfs_reserve_extent(root, |
771ed689 CM |
730 | async_extent->compressed_size, |
731 | async_extent->compressed_size, | |
e570fd27 | 732 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 733 | if (ret) { |
40ae837b | 734 | free_async_extent_pages(async_extent); |
3e04e7f1 | 735 | |
fdf8e2ea JB |
736 | if (ret == -ENOSPC) { |
737 | unlock_extent(io_tree, async_extent->start, | |
738 | async_extent->start + | |
739 | async_extent->ram_size - 1); | |
ce62003f LB |
740 | |
741 | /* | |
742 | * we need to redirty the pages if we decide to | |
743 | * fallback to uncompressed IO, otherwise we | |
744 | * will not submit these pages down to lower | |
745 | * layers. | |
746 | */ | |
747 | extent_range_redirty_for_io(inode, | |
748 | async_extent->start, | |
749 | async_extent->start + | |
750 | async_extent->ram_size - 1); | |
751 | ||
79787eaa | 752 | goto retry; |
fdf8e2ea | 753 | } |
3e04e7f1 | 754 | goto out_free; |
f5a84ee3 | 755 | } |
c2167754 YZ |
756 | /* |
757 | * here we're doing allocation and writeback of the | |
758 | * compressed pages | |
759 | */ | |
760 | btrfs_drop_extent_cache(inode, async_extent->start, | |
761 | async_extent->start + | |
762 | async_extent->ram_size - 1, 0); | |
763 | ||
172ddd60 | 764 | em = alloc_extent_map(); |
b9aa55be LB |
765 | if (!em) { |
766 | ret = -ENOMEM; | |
3e04e7f1 | 767 | goto out_free_reserve; |
b9aa55be | 768 | } |
771ed689 CM |
769 | em->start = async_extent->start; |
770 | em->len = async_extent->ram_size; | |
445a6944 | 771 | em->orig_start = em->start; |
2ab28f32 JB |
772 | em->mod_start = em->start; |
773 | em->mod_len = em->len; | |
c8b97818 | 774 | |
771ed689 CM |
775 | em->block_start = ins.objectid; |
776 | em->block_len = ins.offset; | |
b4939680 | 777 | em->orig_block_len = ins.offset; |
cc95bef6 | 778 | em->ram_bytes = async_extent->ram_size; |
771ed689 | 779 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
261507a0 | 780 | em->compress_type = async_extent->compress_type; |
771ed689 CM |
781 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
782 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
70c8a91c | 783 | em->generation = -1; |
771ed689 | 784 | |
d397712b | 785 | while (1) { |
890871be | 786 | write_lock(&em_tree->lock); |
09a2a8f9 | 787 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 788 | write_unlock(&em_tree->lock); |
771ed689 CM |
789 | if (ret != -EEXIST) { |
790 | free_extent_map(em); | |
791 | break; | |
792 | } | |
793 | btrfs_drop_extent_cache(inode, async_extent->start, | |
794 | async_extent->start + | |
795 | async_extent->ram_size - 1, 0); | |
796 | } | |
797 | ||
3e04e7f1 JB |
798 | if (ret) |
799 | goto out_free_reserve; | |
800 | ||
261507a0 LZ |
801 | ret = btrfs_add_ordered_extent_compress(inode, |
802 | async_extent->start, | |
803 | ins.objectid, | |
804 | async_extent->ram_size, | |
805 | ins.offset, | |
806 | BTRFS_ORDERED_COMPRESSED, | |
807 | async_extent->compress_type); | |
d9f85963 FM |
808 | if (ret) { |
809 | btrfs_drop_extent_cache(inode, async_extent->start, | |
810 | async_extent->start + | |
811 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 812 | goto out_free_reserve; |
d9f85963 | 813 | } |
771ed689 | 814 | |
771ed689 CM |
815 | /* |
816 | * clear dirty, set writeback and unlock the pages. | |
817 | */ | |
c2790a2e | 818 | extent_clear_unlock_delalloc(inode, async_extent->start, |
a791e35e CM |
819 | async_extent->start + |
820 | async_extent->ram_size - 1, | |
151a41bc JB |
821 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
822 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 823 | PAGE_SET_WRITEBACK); |
771ed689 | 824 | ret = btrfs_submit_compressed_write(inode, |
d397712b CM |
825 | async_extent->start, |
826 | async_extent->ram_size, | |
827 | ins.objectid, | |
828 | ins.offset, async_extent->pages, | |
829 | async_extent->nr_pages); | |
fce2a4e6 FM |
830 | if (ret) { |
831 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
832 | struct page *p = async_extent->pages[0]; | |
833 | const u64 start = async_extent->start; | |
834 | const u64 end = start + async_extent->ram_size - 1; | |
835 | ||
836 | p->mapping = inode->i_mapping; | |
837 | tree->ops->writepage_end_io_hook(p, start, end, | |
838 | NULL, 0); | |
839 | p->mapping = NULL; | |
840 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, | |
841 | PAGE_END_WRITEBACK | | |
842 | PAGE_SET_ERROR); | |
40ae837b | 843 | free_async_extent_pages(async_extent); |
fce2a4e6 | 844 | } |
771ed689 CM |
845 | alloc_hint = ins.objectid + ins.offset; |
846 | kfree(async_extent); | |
847 | cond_resched(); | |
848 | } | |
dec8f175 | 849 | return; |
3e04e7f1 | 850 | out_free_reserve: |
e570fd27 | 851 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 852 | out_free: |
c2790a2e | 853 | extent_clear_unlock_delalloc(inode, async_extent->start, |
3e04e7f1 JB |
854 | async_extent->start + |
855 | async_extent->ram_size - 1, | |
c2790a2e | 856 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
857 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
858 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
859 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
860 | PAGE_SET_ERROR); | |
40ae837b | 861 | free_async_extent_pages(async_extent); |
79787eaa | 862 | kfree(async_extent); |
3e04e7f1 | 863 | goto again; |
771ed689 CM |
864 | } |
865 | ||
4b46fce2 JB |
866 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
867 | u64 num_bytes) | |
868 | { | |
869 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
870 | struct extent_map *em; | |
871 | u64 alloc_hint = 0; | |
872 | ||
873 | read_lock(&em_tree->lock); | |
874 | em = search_extent_mapping(em_tree, start, num_bytes); | |
875 | if (em) { | |
876 | /* | |
877 | * if block start isn't an actual block number then find the | |
878 | * first block in this inode and use that as a hint. If that | |
879 | * block is also bogus then just don't worry about it. | |
880 | */ | |
881 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
882 | free_extent_map(em); | |
883 | em = search_extent_mapping(em_tree, 0, 0); | |
884 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
885 | alloc_hint = em->block_start; | |
886 | if (em) | |
887 | free_extent_map(em); | |
888 | } else { | |
889 | alloc_hint = em->block_start; | |
890 | free_extent_map(em); | |
891 | } | |
892 | } | |
893 | read_unlock(&em_tree->lock); | |
894 | ||
895 | return alloc_hint; | |
896 | } | |
897 | ||
771ed689 CM |
898 | /* |
899 | * when extent_io.c finds a delayed allocation range in the file, | |
900 | * the call backs end up in this code. The basic idea is to | |
901 | * allocate extents on disk for the range, and create ordered data structs | |
902 | * in ram to track those extents. | |
903 | * | |
904 | * locked_page is the page that writepage had locked already. We use | |
905 | * it to make sure we don't do extra locks or unlocks. | |
906 | * | |
907 | * *page_started is set to one if we unlock locked_page and do everything | |
908 | * required to start IO on it. It may be clean and already done with | |
909 | * IO when we return. | |
910 | */ | |
00361589 JB |
911 | static noinline int cow_file_range(struct inode *inode, |
912 | struct page *locked_page, | |
913 | u64 start, u64 end, int *page_started, | |
914 | unsigned long *nr_written, | |
915 | int unlock) | |
771ed689 | 916 | { |
00361589 | 917 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
918 | u64 alloc_hint = 0; |
919 | u64 num_bytes; | |
920 | unsigned long ram_size; | |
921 | u64 disk_num_bytes; | |
922 | u64 cur_alloc_size; | |
923 | u64 blocksize = root->sectorsize; | |
771ed689 CM |
924 | struct btrfs_key ins; |
925 | struct extent_map *em; | |
926 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
927 | int ret = 0; | |
928 | ||
02ecd2c2 JB |
929 | if (btrfs_is_free_space_inode(inode)) { |
930 | WARN_ON_ONCE(1); | |
29bce2f3 JB |
931 | ret = -EINVAL; |
932 | goto out_unlock; | |
02ecd2c2 | 933 | } |
771ed689 | 934 | |
fda2832f | 935 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
936 | num_bytes = max(blocksize, num_bytes); |
937 | disk_num_bytes = num_bytes; | |
771ed689 | 938 | |
4cb5300b | 939 | /* if this is a small write inside eof, kick off defrag */ |
4cb13e5d LB |
940 | if (num_bytes < 64 * 1024 && |
941 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
00361589 | 942 | btrfs_add_inode_defrag(NULL, inode); |
4cb5300b | 943 | |
771ed689 CM |
944 | if (start == 0) { |
945 | /* lets try to make an inline extent */ | |
00361589 JB |
946 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
947 | NULL); | |
771ed689 | 948 | if (ret == 0) { |
c2790a2e JB |
949 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
950 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc | 951 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
952 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
953 | PAGE_END_WRITEBACK); | |
c2167754 | 954 | |
771ed689 CM |
955 | *nr_written = *nr_written + |
956 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; | |
957 | *page_started = 1; | |
771ed689 | 958 | goto out; |
79787eaa | 959 | } else if (ret < 0) { |
79787eaa | 960 | goto out_unlock; |
771ed689 CM |
961 | } |
962 | } | |
963 | ||
964 | BUG_ON(disk_num_bytes > | |
6c41761f | 965 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
771ed689 | 966 | |
4b46fce2 | 967 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 CM |
968 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
969 | ||
d397712b | 970 | while (disk_num_bytes > 0) { |
a791e35e CM |
971 | unsigned long op; |
972 | ||
287a0ab9 | 973 | cur_alloc_size = disk_num_bytes; |
00361589 | 974 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
771ed689 | 975 | root->sectorsize, 0, alloc_hint, |
e570fd27 | 976 | &ins, 1, 1); |
00361589 | 977 | if (ret < 0) |
79787eaa | 978 | goto out_unlock; |
d397712b | 979 | |
172ddd60 | 980 | em = alloc_extent_map(); |
b9aa55be LB |
981 | if (!em) { |
982 | ret = -ENOMEM; | |
ace68bac | 983 | goto out_reserve; |
b9aa55be | 984 | } |
e6dcd2dc | 985 | em->start = start; |
445a6944 | 986 | em->orig_start = em->start; |
771ed689 CM |
987 | ram_size = ins.offset; |
988 | em->len = ins.offset; | |
2ab28f32 JB |
989 | em->mod_start = em->start; |
990 | em->mod_len = em->len; | |
c8b97818 | 991 | |
e6dcd2dc | 992 | em->block_start = ins.objectid; |
c8b97818 | 993 | em->block_len = ins.offset; |
b4939680 | 994 | em->orig_block_len = ins.offset; |
cc95bef6 | 995 | em->ram_bytes = ram_size; |
e6dcd2dc | 996 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
7f3c74fb | 997 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
70c8a91c | 998 | em->generation = -1; |
c8b97818 | 999 | |
d397712b | 1000 | while (1) { |
890871be | 1001 | write_lock(&em_tree->lock); |
09a2a8f9 | 1002 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1003 | write_unlock(&em_tree->lock); |
e6dcd2dc CM |
1004 | if (ret != -EEXIST) { |
1005 | free_extent_map(em); | |
1006 | break; | |
1007 | } | |
1008 | btrfs_drop_extent_cache(inode, start, | |
c8b97818 | 1009 | start + ram_size - 1, 0); |
e6dcd2dc | 1010 | } |
ace68bac LB |
1011 | if (ret) |
1012 | goto out_reserve; | |
e6dcd2dc | 1013 | |
98d20f67 | 1014 | cur_alloc_size = ins.offset; |
e6dcd2dc | 1015 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1016 | ram_size, cur_alloc_size, 0); |
ace68bac | 1017 | if (ret) |
d9f85963 | 1018 | goto out_drop_extent_cache; |
c8b97818 | 1019 | |
17d217fe YZ |
1020 | if (root->root_key.objectid == |
1021 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1022 | ret = btrfs_reloc_clone_csums(inode, start, | |
1023 | cur_alloc_size); | |
00361589 | 1024 | if (ret) |
d9f85963 | 1025 | goto out_drop_extent_cache; |
17d217fe YZ |
1026 | } |
1027 | ||
d397712b | 1028 | if (disk_num_bytes < cur_alloc_size) |
3b951516 | 1029 | break; |
d397712b | 1030 | |
c8b97818 CM |
1031 | /* we're not doing compressed IO, don't unlock the first |
1032 | * page (which the caller expects to stay locked), don't | |
1033 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1034 | * |
1035 | * Do set the Private2 bit so we know this page was properly | |
1036 | * setup for writepage | |
c8b97818 | 1037 | */ |
c2790a2e JB |
1038 | op = unlock ? PAGE_UNLOCK : 0; |
1039 | op |= PAGE_SET_PRIVATE2; | |
a791e35e | 1040 | |
c2790a2e JB |
1041 | extent_clear_unlock_delalloc(inode, start, |
1042 | start + ram_size - 1, locked_page, | |
1043 | EXTENT_LOCKED | EXTENT_DELALLOC, | |
1044 | op); | |
c8b97818 | 1045 | disk_num_bytes -= cur_alloc_size; |
c59f8951 CM |
1046 | num_bytes -= cur_alloc_size; |
1047 | alloc_hint = ins.objectid + ins.offset; | |
1048 | start += cur_alloc_size; | |
b888db2b | 1049 | } |
79787eaa | 1050 | out: |
be20aa9d | 1051 | return ret; |
b7d5b0a8 | 1052 | |
d9f85963 FM |
1053 | out_drop_extent_cache: |
1054 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); | |
ace68bac | 1055 | out_reserve: |
e570fd27 | 1056 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 1057 | out_unlock: |
c2790a2e | 1058 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
151a41bc JB |
1059 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
1060 | EXTENT_DELALLOC | EXTENT_DEFRAG, | |
1061 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1062 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); | |
79787eaa | 1063 | goto out; |
771ed689 | 1064 | } |
c8b97818 | 1065 | |
771ed689 CM |
1066 | /* |
1067 | * work queue call back to started compression on a file and pages | |
1068 | */ | |
1069 | static noinline void async_cow_start(struct btrfs_work *work) | |
1070 | { | |
1071 | struct async_cow *async_cow; | |
1072 | int num_added = 0; | |
1073 | async_cow = container_of(work, struct async_cow, work); | |
1074 | ||
1075 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1076 | async_cow->start, async_cow->end, async_cow, | |
1077 | &num_added); | |
8180ef88 | 1078 | if (num_added == 0) { |
cb77fcd8 | 1079 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1080 | async_cow->inode = NULL; |
8180ef88 | 1081 | } |
771ed689 CM |
1082 | } |
1083 | ||
1084 | /* | |
1085 | * work queue call back to submit previously compressed pages | |
1086 | */ | |
1087 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1088 | { | |
1089 | struct async_cow *async_cow; | |
1090 | struct btrfs_root *root; | |
1091 | unsigned long nr_pages; | |
1092 | ||
1093 | async_cow = container_of(work, struct async_cow, work); | |
1094 | ||
1095 | root = async_cow->root; | |
1096 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> | |
1097 | PAGE_CACHE_SHIFT; | |
1098 | ||
66657b31 | 1099 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
287082b0 | 1100 | 5 * 1024 * 1024 && |
771ed689 CM |
1101 | waitqueue_active(&root->fs_info->async_submit_wait)) |
1102 | wake_up(&root->fs_info->async_submit_wait); | |
1103 | ||
d397712b | 1104 | if (async_cow->inode) |
771ed689 | 1105 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1106 | } |
c8b97818 | 1107 | |
771ed689 CM |
1108 | static noinline void async_cow_free(struct btrfs_work *work) |
1109 | { | |
1110 | struct async_cow *async_cow; | |
1111 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1112 | if (async_cow->inode) |
cb77fcd8 | 1113 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1114 | kfree(async_cow); |
1115 | } | |
1116 | ||
1117 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1118 | u64 start, u64 end, int *page_started, | |
1119 | unsigned long *nr_written) | |
1120 | { | |
1121 | struct async_cow *async_cow; | |
1122 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1123 | unsigned long nr_pages; | |
1124 | u64 cur_end; | |
287082b0 | 1125 | int limit = 10 * 1024 * 1024; |
771ed689 | 1126 | |
a3429ab7 CM |
1127 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1128 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1129 | while (start < end) { |
771ed689 | 1130 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1131 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1132 | async_cow->inode = igrab(inode); |
771ed689 CM |
1133 | async_cow->root = root; |
1134 | async_cow->locked_page = locked_page; | |
1135 | async_cow->start = start; | |
1136 | ||
f79707b0 WS |
1137 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
1138 | !btrfs_test_opt(root, FORCE_COMPRESS)) | |
771ed689 CM |
1139 | cur_end = end; |
1140 | else | |
1141 | cur_end = min(end, start + 512 * 1024 - 1); | |
1142 | ||
1143 | async_cow->end = cur_end; | |
1144 | INIT_LIST_HEAD(&async_cow->extents); | |
1145 | ||
9e0af237 LB |
1146 | btrfs_init_work(&async_cow->work, |
1147 | btrfs_delalloc_helper, | |
1148 | async_cow_start, async_cow_submit, | |
1149 | async_cow_free); | |
771ed689 | 1150 | |
771ed689 CM |
1151 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
1152 | PAGE_CACHE_SHIFT; | |
1153 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); | |
1154 | ||
afe3d242 QW |
1155 | btrfs_queue_work(root->fs_info->delalloc_workers, |
1156 | &async_cow->work); | |
771ed689 CM |
1157 | |
1158 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { | |
1159 | wait_event(root->fs_info->async_submit_wait, | |
1160 | (atomic_read(&root->fs_info->async_delalloc_pages) < | |
1161 | limit)); | |
1162 | } | |
1163 | ||
d397712b | 1164 | while (atomic_read(&root->fs_info->async_submit_draining) && |
771ed689 CM |
1165 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
1166 | wait_event(root->fs_info->async_submit_wait, | |
1167 | (atomic_read(&root->fs_info->async_delalloc_pages) == | |
1168 | 0)); | |
1169 | } | |
1170 | ||
1171 | *nr_written += nr_pages; | |
1172 | start = cur_end + 1; | |
1173 | } | |
1174 | *page_started = 1; | |
1175 | return 0; | |
be20aa9d CM |
1176 | } |
1177 | ||
d397712b | 1178 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
17d217fe YZ |
1179 | u64 bytenr, u64 num_bytes) |
1180 | { | |
1181 | int ret; | |
1182 | struct btrfs_ordered_sum *sums; | |
1183 | LIST_HEAD(list); | |
1184 | ||
07d400a6 | 1185 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
a2de733c | 1186 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1187 | if (ret == 0 && list_empty(&list)) |
1188 | return 0; | |
1189 | ||
1190 | while (!list_empty(&list)) { | |
1191 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1192 | list_del(&sums->list); | |
1193 | kfree(sums); | |
1194 | } | |
1195 | return 1; | |
1196 | } | |
1197 | ||
d352ac68 CM |
1198 | /* |
1199 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1200 | * of the extents that exist in the file, and COWs the file as required. | |
1201 | * | |
1202 | * If no cow copies or snapshots exist, we write directly to the existing | |
1203 | * blocks on disk | |
1204 | */ | |
7f366cfe CM |
1205 | static noinline int run_delalloc_nocow(struct inode *inode, |
1206 | struct page *locked_page, | |
771ed689 CM |
1207 | u64 start, u64 end, int *page_started, int force, |
1208 | unsigned long *nr_written) | |
be20aa9d | 1209 | { |
be20aa9d | 1210 | struct btrfs_root *root = BTRFS_I(inode)->root; |
7ea394f1 | 1211 | struct btrfs_trans_handle *trans; |
be20aa9d | 1212 | struct extent_buffer *leaf; |
be20aa9d | 1213 | struct btrfs_path *path; |
80ff3856 | 1214 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1215 | struct btrfs_key found_key; |
80ff3856 YZ |
1216 | u64 cow_start; |
1217 | u64 cur_offset; | |
1218 | u64 extent_end; | |
5d4f98a2 | 1219 | u64 extent_offset; |
80ff3856 YZ |
1220 | u64 disk_bytenr; |
1221 | u64 num_bytes; | |
b4939680 | 1222 | u64 disk_num_bytes; |
cc95bef6 | 1223 | u64 ram_bytes; |
80ff3856 | 1224 | int extent_type; |
79787eaa | 1225 | int ret, err; |
d899e052 | 1226 | int type; |
80ff3856 YZ |
1227 | int nocow; |
1228 | int check_prev = 1; | |
82d5902d | 1229 | bool nolock; |
33345d01 | 1230 | u64 ino = btrfs_ino(inode); |
be20aa9d CM |
1231 | |
1232 | path = btrfs_alloc_path(); | |
17ca04af | 1233 | if (!path) { |
c2790a2e JB |
1234 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1235 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1236 | EXTENT_DO_ACCOUNTING | |
1237 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1238 | PAGE_CLEAR_DIRTY | |
1239 | PAGE_SET_WRITEBACK | | |
1240 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1241 | return -ENOMEM; |
17ca04af | 1242 | } |
82d5902d | 1243 | |
83eea1f1 | 1244 | nolock = btrfs_is_free_space_inode(inode); |
82d5902d LZ |
1245 | |
1246 | if (nolock) | |
7a7eaa40 | 1247 | trans = btrfs_join_transaction_nolock(root); |
82d5902d | 1248 | else |
7a7eaa40 | 1249 | trans = btrfs_join_transaction(root); |
ff5714cc | 1250 | |
79787eaa | 1251 | if (IS_ERR(trans)) { |
c2790a2e JB |
1252 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1253 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1254 | EXTENT_DO_ACCOUNTING | |
1255 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1256 | PAGE_CLEAR_DIRTY | |
1257 | PAGE_SET_WRITEBACK | | |
1258 | PAGE_END_WRITEBACK); | |
79787eaa JM |
1259 | btrfs_free_path(path); |
1260 | return PTR_ERR(trans); | |
1261 | } | |
1262 | ||
74b21075 | 1263 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
be20aa9d | 1264 | |
80ff3856 YZ |
1265 | cow_start = (u64)-1; |
1266 | cur_offset = start; | |
1267 | while (1) { | |
33345d01 | 1268 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
80ff3856 | 1269 | cur_offset, 0); |
d788a349 | 1270 | if (ret < 0) |
79787eaa | 1271 | goto error; |
80ff3856 YZ |
1272 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1273 | leaf = path->nodes[0]; | |
1274 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1275 | path->slots[0] - 1); | |
33345d01 | 1276 | if (found_key.objectid == ino && |
80ff3856 YZ |
1277 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1278 | path->slots[0]--; | |
1279 | } | |
1280 | check_prev = 0; | |
1281 | next_slot: | |
1282 | leaf = path->nodes[0]; | |
1283 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1284 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1285 | if (ret < 0) |
79787eaa | 1286 | goto error; |
80ff3856 YZ |
1287 | if (ret > 0) |
1288 | break; | |
1289 | leaf = path->nodes[0]; | |
1290 | } | |
be20aa9d | 1291 | |
80ff3856 YZ |
1292 | nocow = 0; |
1293 | disk_bytenr = 0; | |
17d217fe | 1294 | num_bytes = 0; |
80ff3856 YZ |
1295 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1296 | ||
33345d01 | 1297 | if (found_key.objectid > ino || |
80ff3856 YZ |
1298 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
1299 | found_key.offset > end) | |
1300 | break; | |
1301 | ||
1302 | if (found_key.offset > cur_offset) { | |
1303 | extent_end = found_key.offset; | |
e9061e21 | 1304 | extent_type = 0; |
80ff3856 YZ |
1305 | goto out_check; |
1306 | } | |
1307 | ||
1308 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1309 | struct btrfs_file_extent_item); | |
1310 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1311 | ||
cc95bef6 | 1312 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1313 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1314 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1315 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1316 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1317 | extent_end = found_key.offset + |
1318 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1319 | disk_num_bytes = |
1320 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1321 | if (extent_end <= start) { |
1322 | path->slots[0]++; | |
1323 | goto next_slot; | |
1324 | } | |
17d217fe YZ |
1325 | if (disk_bytenr == 0) |
1326 | goto out_check; | |
80ff3856 YZ |
1327 | if (btrfs_file_extent_compression(leaf, fi) || |
1328 | btrfs_file_extent_encryption(leaf, fi) || | |
1329 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1330 | goto out_check; | |
d899e052 YZ |
1331 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1332 | goto out_check; | |
d2fb3437 | 1333 | if (btrfs_extent_readonly(root, disk_bytenr)) |
80ff3856 | 1334 | goto out_check; |
33345d01 | 1335 | if (btrfs_cross_ref_exist(trans, root, ino, |
5d4f98a2 YZ |
1336 | found_key.offset - |
1337 | extent_offset, disk_bytenr)) | |
17d217fe | 1338 | goto out_check; |
5d4f98a2 | 1339 | disk_bytenr += extent_offset; |
17d217fe YZ |
1340 | disk_bytenr += cur_offset - found_key.offset; |
1341 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1342 | /* |
1343 | * if there are pending snapshots for this root, | |
1344 | * we fall into common COW way. | |
1345 | */ | |
1346 | if (!nolock) { | |
9ea24bbe | 1347 | err = btrfs_start_write_no_snapshoting(root); |
e9894fd3 WS |
1348 | if (!err) |
1349 | goto out_check; | |
1350 | } | |
17d217fe YZ |
1351 | /* |
1352 | * force cow if csum exists in the range. | |
1353 | * this ensure that csum for a given extent are | |
1354 | * either valid or do not exist. | |
1355 | */ | |
1356 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) | |
1357 | goto out_check; | |
80ff3856 YZ |
1358 | nocow = 1; |
1359 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1360 | extent_end = found_key.offset + | |
514ac8ad CM |
1361 | btrfs_file_extent_inline_len(leaf, |
1362 | path->slots[0], fi); | |
80ff3856 YZ |
1363 | extent_end = ALIGN(extent_end, root->sectorsize); |
1364 | } else { | |
1365 | BUG_ON(1); | |
1366 | } | |
1367 | out_check: | |
1368 | if (extent_end <= start) { | |
1369 | path->slots[0]++; | |
e9894fd3 | 1370 | if (!nolock && nocow) |
9ea24bbe | 1371 | btrfs_end_write_no_snapshoting(root); |
80ff3856 YZ |
1372 | goto next_slot; |
1373 | } | |
1374 | if (!nocow) { | |
1375 | if (cow_start == (u64)-1) | |
1376 | cow_start = cur_offset; | |
1377 | cur_offset = extent_end; | |
1378 | if (cur_offset > end) | |
1379 | break; | |
1380 | path->slots[0]++; | |
1381 | goto next_slot; | |
7ea394f1 YZ |
1382 | } |
1383 | ||
b3b4aa74 | 1384 | btrfs_release_path(path); |
80ff3856 | 1385 | if (cow_start != (u64)-1) { |
00361589 JB |
1386 | ret = cow_file_range(inode, locked_page, |
1387 | cow_start, found_key.offset - 1, | |
1388 | page_started, nr_written, 1); | |
e9894fd3 WS |
1389 | if (ret) { |
1390 | if (!nolock && nocow) | |
9ea24bbe | 1391 | btrfs_end_write_no_snapshoting(root); |
79787eaa | 1392 | goto error; |
e9894fd3 | 1393 | } |
80ff3856 | 1394 | cow_start = (u64)-1; |
7ea394f1 | 1395 | } |
80ff3856 | 1396 | |
d899e052 YZ |
1397 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
1398 | struct extent_map *em; | |
1399 | struct extent_map_tree *em_tree; | |
1400 | em_tree = &BTRFS_I(inode)->extent_tree; | |
172ddd60 | 1401 | em = alloc_extent_map(); |
79787eaa | 1402 | BUG_ON(!em); /* -ENOMEM */ |
d899e052 | 1403 | em->start = cur_offset; |
70c8a91c | 1404 | em->orig_start = found_key.offset - extent_offset; |
d899e052 YZ |
1405 | em->len = num_bytes; |
1406 | em->block_len = num_bytes; | |
1407 | em->block_start = disk_bytenr; | |
b4939680 | 1408 | em->orig_block_len = disk_num_bytes; |
cc95bef6 | 1409 | em->ram_bytes = ram_bytes; |
d899e052 | 1410 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
2ab28f32 JB |
1411 | em->mod_start = em->start; |
1412 | em->mod_len = em->len; | |
d899e052 | 1413 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
b11e234d | 1414 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
70c8a91c | 1415 | em->generation = -1; |
d899e052 | 1416 | while (1) { |
890871be | 1417 | write_lock(&em_tree->lock); |
09a2a8f9 | 1418 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1419 | write_unlock(&em_tree->lock); |
d899e052 YZ |
1420 | if (ret != -EEXIST) { |
1421 | free_extent_map(em); | |
1422 | break; | |
1423 | } | |
1424 | btrfs_drop_extent_cache(inode, em->start, | |
1425 | em->start + em->len - 1, 0); | |
1426 | } | |
1427 | type = BTRFS_ORDERED_PREALLOC; | |
1428 | } else { | |
1429 | type = BTRFS_ORDERED_NOCOW; | |
1430 | } | |
80ff3856 YZ |
1431 | |
1432 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1433 | num_bytes, num_bytes, type); |
79787eaa | 1434 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1435 | |
efa56464 YZ |
1436 | if (root->root_key.objectid == |
1437 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1438 | ret = btrfs_reloc_clone_csums(inode, cur_offset, | |
1439 | num_bytes); | |
e9894fd3 WS |
1440 | if (ret) { |
1441 | if (!nolock && nocow) | |
9ea24bbe | 1442 | btrfs_end_write_no_snapshoting(root); |
79787eaa | 1443 | goto error; |
e9894fd3 | 1444 | } |
efa56464 YZ |
1445 | } |
1446 | ||
c2790a2e JB |
1447 | extent_clear_unlock_delalloc(inode, cur_offset, |
1448 | cur_offset + num_bytes - 1, | |
1449 | locked_page, EXTENT_LOCKED | | |
1450 | EXTENT_DELALLOC, PAGE_UNLOCK | | |
1451 | PAGE_SET_PRIVATE2); | |
e9894fd3 | 1452 | if (!nolock && nocow) |
9ea24bbe | 1453 | btrfs_end_write_no_snapshoting(root); |
80ff3856 YZ |
1454 | cur_offset = extent_end; |
1455 | if (cur_offset > end) | |
1456 | break; | |
be20aa9d | 1457 | } |
b3b4aa74 | 1458 | btrfs_release_path(path); |
80ff3856 | 1459 | |
17ca04af | 1460 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1461 | cow_start = cur_offset; |
17ca04af JB |
1462 | cur_offset = end; |
1463 | } | |
1464 | ||
80ff3856 | 1465 | if (cow_start != (u64)-1) { |
00361589 JB |
1466 | ret = cow_file_range(inode, locked_page, cow_start, end, |
1467 | page_started, nr_written, 1); | |
d788a349 | 1468 | if (ret) |
79787eaa | 1469 | goto error; |
80ff3856 YZ |
1470 | } |
1471 | ||
79787eaa | 1472 | error: |
a698d075 | 1473 | err = btrfs_end_transaction(trans, root); |
79787eaa JM |
1474 | if (!ret) |
1475 | ret = err; | |
1476 | ||
17ca04af | 1477 | if (ret && cur_offset < end) |
c2790a2e JB |
1478 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
1479 | locked_page, EXTENT_LOCKED | | |
151a41bc JB |
1480 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1481 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1482 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1483 | PAGE_SET_WRITEBACK | |
1484 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1485 | btrfs_free_path(path); |
79787eaa | 1486 | return ret; |
be20aa9d CM |
1487 | } |
1488 | ||
47059d93 WS |
1489 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1490 | { | |
1491 | ||
1492 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1493 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1494 | return 0; | |
1495 | ||
1496 | /* | |
1497 | * @defrag_bytes is a hint value, no spinlock held here, | |
1498 | * if is not zero, it means the file is defragging. | |
1499 | * Force cow if given extent needs to be defragged. | |
1500 | */ | |
1501 | if (BTRFS_I(inode)->defrag_bytes && | |
1502 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1503 | EXTENT_DEFRAG, 0, NULL)) | |
1504 | return 1; | |
1505 | ||
1506 | return 0; | |
1507 | } | |
1508 | ||
d352ac68 CM |
1509 | /* |
1510 | * extent_io.c call back to do delayed allocation processing | |
1511 | */ | |
c8b97818 | 1512 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
771ed689 CM |
1513 | u64 start, u64 end, int *page_started, |
1514 | unsigned long *nr_written) | |
be20aa9d | 1515 | { |
be20aa9d | 1516 | int ret; |
47059d93 | 1517 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1518 | |
47059d93 | 1519 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1520 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1521 | page_started, 1, nr_written); |
47059d93 | 1522 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1523 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1524 | page_started, 0, nr_written); |
7816030e | 1525 | } else if (!inode_need_compress(inode)) { |
7f366cfe CM |
1526 | ret = cow_file_range(inode, locked_page, start, end, |
1527 | page_started, nr_written, 1); | |
7ddf5a42 JB |
1528 | } else { |
1529 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1530 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1531 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1532 | page_started, nr_written); |
7ddf5a42 | 1533 | } |
b888db2b CM |
1534 | return ret; |
1535 | } | |
1536 | ||
1bf85046 JM |
1537 | static void btrfs_split_extent_hook(struct inode *inode, |
1538 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1539 | { |
dcab6a3b JB |
1540 | u64 size; |
1541 | ||
0ca1f7ce | 1542 | /* not delalloc, ignore it */ |
9ed74f2d | 1543 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1544 | return; |
9ed74f2d | 1545 | |
dcab6a3b JB |
1546 | size = orig->end - orig->start + 1; |
1547 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
1548 | u64 num_extents; | |
1549 | u64 new_size; | |
1550 | ||
1551 | /* | |
ba117213 JB |
1552 | * See the explanation in btrfs_merge_extent_hook, the same |
1553 | * applies here, just in reverse. | |
dcab6a3b JB |
1554 | */ |
1555 | new_size = orig->end - split + 1; | |
ba117213 | 1556 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
dcab6a3b | 1557 | BTRFS_MAX_EXTENT_SIZE); |
ba117213 JB |
1558 | new_size = split - orig->start; |
1559 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1560 | BTRFS_MAX_EXTENT_SIZE); | |
1561 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1562 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) | |
dcab6a3b JB |
1563 | return; |
1564 | } | |
1565 | ||
9e0baf60 JB |
1566 | spin_lock(&BTRFS_I(inode)->lock); |
1567 | BTRFS_I(inode)->outstanding_extents++; | |
1568 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1569 | } |
1570 | ||
1571 | /* | |
1572 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1573 | * extents so we can keep track of new extents that are just merged onto old | |
1574 | * extents, such as when we are doing sequential writes, so we can properly | |
1575 | * account for the metadata space we'll need. | |
1576 | */ | |
1bf85046 JM |
1577 | static void btrfs_merge_extent_hook(struct inode *inode, |
1578 | struct extent_state *new, | |
1579 | struct extent_state *other) | |
9ed74f2d | 1580 | { |
dcab6a3b JB |
1581 | u64 new_size, old_size; |
1582 | u64 num_extents; | |
1583 | ||
9ed74f2d JB |
1584 | /* not delalloc, ignore it */ |
1585 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1586 | return; |
9ed74f2d | 1587 | |
8461a3de JB |
1588 | if (new->start > other->start) |
1589 | new_size = new->end - other->start + 1; | |
1590 | else | |
1591 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1592 | |
1593 | /* we're not bigger than the max, unreserve the space and go */ | |
1594 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1595 | spin_lock(&BTRFS_I(inode)->lock); | |
1596 | BTRFS_I(inode)->outstanding_extents--; | |
1597 | spin_unlock(&BTRFS_I(inode)->lock); | |
1598 | return; | |
1599 | } | |
1600 | ||
1601 | /* | |
ba117213 JB |
1602 | * We have to add up either side to figure out how many extents were |
1603 | * accounted for before we merged into one big extent. If the number of | |
1604 | * extents we accounted for is <= the amount we need for the new range | |
1605 | * then we can return, otherwise drop. Think of it like this | |
1606 | * | |
1607 | * [ 4k][MAX_SIZE] | |
1608 | * | |
1609 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1610 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1611 | * we have 1 so they are == and we can return. But in this case | |
1612 | * | |
1613 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1614 | * | |
1615 | * Each range on their own accounts for 2 extents, but merged together | |
1616 | * they are only 3 extents worth of accounting, so we need to drop in | |
1617 | * this case. | |
dcab6a3b | 1618 | */ |
ba117213 | 1619 | old_size = other->end - other->start + 1; |
dcab6a3b JB |
1620 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
1621 | BTRFS_MAX_EXTENT_SIZE); | |
ba117213 JB |
1622 | old_size = new->end - new->start + 1; |
1623 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1624 | BTRFS_MAX_EXTENT_SIZE); | |
1625 | ||
dcab6a3b | 1626 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
ba117213 | 1627 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
dcab6a3b JB |
1628 | return; |
1629 | ||
9e0baf60 JB |
1630 | spin_lock(&BTRFS_I(inode)->lock); |
1631 | BTRFS_I(inode)->outstanding_extents--; | |
1632 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1633 | } |
1634 | ||
eb73c1b7 MX |
1635 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1636 | struct inode *inode) | |
1637 | { | |
1638 | spin_lock(&root->delalloc_lock); | |
1639 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1640 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1641 | &root->delalloc_inodes); | |
1642 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1643 | &BTRFS_I(inode)->runtime_flags); | |
1644 | root->nr_delalloc_inodes++; | |
1645 | if (root->nr_delalloc_inodes == 1) { | |
1646 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1647 | BUG_ON(!list_empty(&root->delalloc_root)); | |
1648 | list_add_tail(&root->delalloc_root, | |
1649 | &root->fs_info->delalloc_roots); | |
1650 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1651 | } | |
1652 | } | |
1653 | spin_unlock(&root->delalloc_lock); | |
1654 | } | |
1655 | ||
1656 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1657 | struct inode *inode) | |
1658 | { | |
1659 | spin_lock(&root->delalloc_lock); | |
1660 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1661 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); | |
1662 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1663 | &BTRFS_I(inode)->runtime_flags); | |
1664 | root->nr_delalloc_inodes--; | |
1665 | if (!root->nr_delalloc_inodes) { | |
1666 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1667 | BUG_ON(list_empty(&root->delalloc_root)); | |
1668 | list_del_init(&root->delalloc_root); | |
1669 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1670 | } | |
1671 | } | |
1672 | spin_unlock(&root->delalloc_lock); | |
1673 | } | |
1674 | ||
d352ac68 CM |
1675 | /* |
1676 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1677 | * bytes in this file, and to maintain the list of inodes that | |
1678 | * have pending delalloc work to be done. | |
1679 | */ | |
1bf85046 | 1680 | static void btrfs_set_bit_hook(struct inode *inode, |
9ee49a04 | 1681 | struct extent_state *state, unsigned *bits) |
291d673e | 1682 | { |
9ed74f2d | 1683 | |
47059d93 WS |
1684 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1685 | WARN_ON(1); | |
75eff68e CM |
1686 | /* |
1687 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1688 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1689 | * bit, which is only set or cleared with irqs on |
1690 | */ | |
0ca1f7ce | 1691 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1692 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1693 | u64 len = state->end + 1 - state->start; |
83eea1f1 | 1694 | bool do_list = !btrfs_is_free_space_inode(inode); |
9ed74f2d | 1695 | |
9e0baf60 | 1696 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1697 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1698 | } else { |
1699 | spin_lock(&BTRFS_I(inode)->lock); | |
1700 | BTRFS_I(inode)->outstanding_extents++; | |
1701 | spin_unlock(&BTRFS_I(inode)->lock); | |
1702 | } | |
287a0ab9 | 1703 | |
6a3891c5 JB |
1704 | /* For sanity tests */ |
1705 | if (btrfs_test_is_dummy_root(root)) | |
1706 | return; | |
1707 | ||
963d678b MX |
1708 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
1709 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1710 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1711 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1712 | if (*bits & EXTENT_DEFRAG) |
1713 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1714 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1715 | &BTRFS_I(inode)->runtime_flags)) |
1716 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1717 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1718 | } |
291d673e CM |
1719 | } |
1720 | ||
d352ac68 CM |
1721 | /* |
1722 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1723 | */ | |
1bf85046 | 1724 | static void btrfs_clear_bit_hook(struct inode *inode, |
41074888 | 1725 | struct extent_state *state, |
9ee49a04 | 1726 | unsigned *bits) |
291d673e | 1727 | { |
47059d93 | 1728 | u64 len = state->end + 1 - state->start; |
dcab6a3b JB |
1729 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
1730 | BTRFS_MAX_EXTENT_SIZE); | |
47059d93 WS |
1731 | |
1732 | spin_lock(&BTRFS_I(inode)->lock); | |
1733 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) | |
1734 | BTRFS_I(inode)->defrag_bytes -= len; | |
1735 | spin_unlock(&BTRFS_I(inode)->lock); | |
1736 | ||
75eff68e CM |
1737 | /* |
1738 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1739 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1740 | * bit, which is only set or cleared with irqs on |
1741 | */ | |
0ca1f7ce | 1742 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1743 | struct btrfs_root *root = BTRFS_I(inode)->root; |
83eea1f1 | 1744 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1745 | |
9e0baf60 | 1746 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1747 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1748 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
1749 | spin_lock(&BTRFS_I(inode)->lock); | |
dcab6a3b | 1750 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
9e0baf60 JB |
1751 | spin_unlock(&BTRFS_I(inode)->lock); |
1752 | } | |
0ca1f7ce | 1753 | |
b6d08f06 JB |
1754 | /* |
1755 | * We don't reserve metadata space for space cache inodes so we | |
1756 | * don't need to call dellalloc_release_metadata if there is an | |
1757 | * error. | |
1758 | */ | |
1759 | if (*bits & EXTENT_DO_ACCOUNTING && | |
1760 | root != root->fs_info->tree_root) | |
0ca1f7ce YZ |
1761 | btrfs_delalloc_release_metadata(inode, len); |
1762 | ||
6a3891c5 JB |
1763 | /* For sanity tests. */ |
1764 | if (btrfs_test_is_dummy_root(root)) | |
1765 | return; | |
1766 | ||
0cb59c99 | 1767 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
7ee9e440 | 1768 | && do_list && !(state->state & EXTENT_NORESERVE)) |
0ca1f7ce | 1769 | btrfs_free_reserved_data_space(inode, len); |
9ed74f2d | 1770 | |
963d678b MX |
1771 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
1772 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1773 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1774 | BTRFS_I(inode)->delalloc_bytes -= len; |
0cb59c99 | 1775 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
df0af1a5 | 1776 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1777 | &BTRFS_I(inode)->runtime_flags)) |
1778 | btrfs_del_delalloc_inode(root, inode); | |
df0af1a5 | 1779 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1780 | } |
291d673e CM |
1781 | } |
1782 | ||
d352ac68 CM |
1783 | /* |
1784 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1785 | * we don't create bios that span stripes or chunks | |
1786 | */ | |
64a16701 | 1787 | int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset, |
c8b97818 CM |
1788 | size_t size, struct bio *bio, |
1789 | unsigned long bio_flags) | |
239b14b3 CM |
1790 | { |
1791 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
4f024f37 | 1792 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1793 | u64 length = 0; |
1794 | u64 map_length; | |
239b14b3 CM |
1795 | int ret; |
1796 | ||
771ed689 CM |
1797 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1798 | return 0; | |
1799 | ||
4f024f37 | 1800 | length = bio->bi_iter.bi_size; |
239b14b3 | 1801 | map_length = length; |
64a16701 | 1802 | ret = btrfs_map_block(root->fs_info, rw, logical, |
f188591e | 1803 | &map_length, NULL, 0); |
3ec706c8 | 1804 | /* Will always return 0 with map_multi == NULL */ |
3444a972 | 1805 | BUG_ON(ret < 0); |
d397712b | 1806 | if (map_length < length + size) |
239b14b3 | 1807 | return 1; |
3444a972 | 1808 | return 0; |
239b14b3 CM |
1809 | } |
1810 | ||
d352ac68 CM |
1811 | /* |
1812 | * in order to insert checksums into the metadata in large chunks, | |
1813 | * we wait until bio submission time. All the pages in the bio are | |
1814 | * checksummed and sums are attached onto the ordered extent record. | |
1815 | * | |
1816 | * At IO completion time the cums attached on the ordered extent record | |
1817 | * are inserted into the btree | |
1818 | */ | |
d397712b CM |
1819 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
1820 | struct bio *bio, int mirror_num, | |
eaf25d93 CM |
1821 | unsigned long bio_flags, |
1822 | u64 bio_offset) | |
065631f6 | 1823 | { |
065631f6 | 1824 | struct btrfs_root *root = BTRFS_I(inode)->root; |
065631f6 | 1825 | int ret = 0; |
e015640f | 1826 | |
d20f7043 | 1827 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
79787eaa | 1828 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1829 | return 0; |
1830 | } | |
e015640f | 1831 | |
4a69a410 CM |
1832 | /* |
1833 | * in order to insert checksums into the metadata in large chunks, | |
1834 | * we wait until bio submission time. All the pages in the bio are | |
1835 | * checksummed and sums are attached onto the ordered extent record. | |
1836 | * | |
1837 | * At IO completion time the cums attached on the ordered extent record | |
1838 | * are inserted into the btree | |
1839 | */ | |
b2950863 | 1840 | static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1841 | int mirror_num, unsigned long bio_flags, |
1842 | u64 bio_offset) | |
4a69a410 CM |
1843 | { |
1844 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
61891923 SB |
1845 | int ret; |
1846 | ||
1847 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); | |
1848 | if (ret) | |
1849 | bio_endio(bio, ret); | |
1850 | return ret; | |
44b8bd7e CM |
1851 | } |
1852 | ||
d352ac68 | 1853 | /* |
cad321ad CM |
1854 | * extent_io.c submission hook. This does the right thing for csum calculation |
1855 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1856 | */ |
b2950863 | 1857 | static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1858 | int mirror_num, unsigned long bio_flags, |
1859 | u64 bio_offset) | |
44b8bd7e CM |
1860 | { |
1861 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1862 | int ret = 0; | |
19b9bdb0 | 1863 | int skip_sum; |
0417341e | 1864 | int metadata = 0; |
b812ce28 | 1865 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1866 | |
6cbff00f | 1867 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1868 | |
83eea1f1 | 1869 | if (btrfs_is_free_space_inode(inode)) |
0417341e JM |
1870 | metadata = 2; |
1871 | ||
7b6d91da | 1872 | if (!(rw & REQ_WRITE)) { |
5fd02043 JB |
1873 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
1874 | if (ret) | |
61891923 | 1875 | goto out; |
5fd02043 | 1876 | |
d20f7043 | 1877 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1878 | ret = btrfs_submit_compressed_read(inode, bio, |
1879 | mirror_num, | |
1880 | bio_flags); | |
1881 | goto out; | |
c2db1073 TI |
1882 | } else if (!skip_sum) { |
1883 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); | |
1884 | if (ret) | |
61891923 | 1885 | goto out; |
c2db1073 | 1886 | } |
4d1b5fb4 | 1887 | goto mapit; |
b812ce28 | 1888 | } else if (async && !skip_sum) { |
17d217fe YZ |
1889 | /* csum items have already been cloned */ |
1890 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1891 | goto mapit; | |
19b9bdb0 | 1892 | /* we're doing a write, do the async checksumming */ |
61891923 | 1893 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
44b8bd7e | 1894 | inode, rw, bio, mirror_num, |
eaf25d93 CM |
1895 | bio_flags, bio_offset, |
1896 | __btrfs_submit_bio_start, | |
4a69a410 | 1897 | __btrfs_submit_bio_done); |
61891923 | 1898 | goto out; |
b812ce28 JB |
1899 | } else if (!skip_sum) { |
1900 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); | |
1901 | if (ret) | |
1902 | goto out; | |
19b9bdb0 CM |
1903 | } |
1904 | ||
0b86a832 | 1905 | mapit: |
61891923 SB |
1906 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
1907 | ||
1908 | out: | |
1909 | if (ret < 0) | |
1910 | bio_endio(bio, ret); | |
1911 | return ret; | |
065631f6 | 1912 | } |
6885f308 | 1913 | |
d352ac68 CM |
1914 | /* |
1915 | * given a list of ordered sums record them in the inode. This happens | |
1916 | * at IO completion time based on sums calculated at bio submission time. | |
1917 | */ | |
ba1da2f4 | 1918 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
1919 | struct inode *inode, u64 file_offset, |
1920 | struct list_head *list) | |
1921 | { | |
e6dcd2dc CM |
1922 | struct btrfs_ordered_sum *sum; |
1923 | ||
c6e30871 | 1924 | list_for_each_entry(sum, list, list) { |
39847c4d | 1925 | trans->adding_csums = 1; |
d20f7043 CM |
1926 | btrfs_csum_file_blocks(trans, |
1927 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 1928 | trans->adding_csums = 0; |
e6dcd2dc CM |
1929 | } |
1930 | return 0; | |
1931 | } | |
1932 | ||
2ac55d41 JB |
1933 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
1934 | struct extent_state **cached_state) | |
ea8c2819 | 1935 | { |
6c1500f2 | 1936 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
ea8c2819 | 1937 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
2ac55d41 | 1938 | cached_state, GFP_NOFS); |
ea8c2819 CM |
1939 | } |
1940 | ||
d352ac68 | 1941 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
1942 | struct btrfs_writepage_fixup { |
1943 | struct page *page; | |
1944 | struct btrfs_work work; | |
1945 | }; | |
1946 | ||
b2950863 | 1947 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
1948 | { |
1949 | struct btrfs_writepage_fixup *fixup; | |
1950 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 1951 | struct extent_state *cached_state = NULL; |
247e743c CM |
1952 | struct page *page; |
1953 | struct inode *inode; | |
1954 | u64 page_start; | |
1955 | u64 page_end; | |
87826df0 | 1956 | int ret; |
247e743c CM |
1957 | |
1958 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
1959 | page = fixup->page; | |
4a096752 | 1960 | again: |
247e743c CM |
1961 | lock_page(page); |
1962 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
1963 | ClearPageChecked(page); | |
1964 | goto out_page; | |
1965 | } | |
1966 | ||
1967 | inode = page->mapping->host; | |
1968 | page_start = page_offset(page); | |
1969 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; | |
1970 | ||
2ac55d41 | 1971 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
d0082371 | 1972 | &cached_state); |
4a096752 CM |
1973 | |
1974 | /* already ordered? We're done */ | |
8b62b72b | 1975 | if (PagePrivate2(page)) |
247e743c | 1976 | goto out; |
4a096752 CM |
1977 | |
1978 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
1979 | if (ordered) { | |
2ac55d41 JB |
1980 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
1981 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
1982 | unlock_page(page); |
1983 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 1984 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
1985 | goto again; |
1986 | } | |
247e743c | 1987 | |
87826df0 JM |
1988 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
1989 | if (ret) { | |
1990 | mapping_set_error(page->mapping, ret); | |
1991 | end_extent_writepage(page, ret, page_start, page_end); | |
1992 | ClearPageChecked(page); | |
1993 | goto out; | |
1994 | } | |
1995 | ||
2ac55d41 | 1996 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
247e743c | 1997 | ClearPageChecked(page); |
87826df0 | 1998 | set_page_dirty(page); |
247e743c | 1999 | out: |
2ac55d41 JB |
2000 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2001 | &cached_state, GFP_NOFS); | |
247e743c CM |
2002 | out_page: |
2003 | unlock_page(page); | |
2004 | page_cache_release(page); | |
b897abec | 2005 | kfree(fixup); |
247e743c CM |
2006 | } |
2007 | ||
2008 | /* | |
2009 | * There are a few paths in the higher layers of the kernel that directly | |
2010 | * set the page dirty bit without asking the filesystem if it is a | |
2011 | * good idea. This causes problems because we want to make sure COW | |
2012 | * properly happens and the data=ordered rules are followed. | |
2013 | * | |
c8b97818 | 2014 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2015 | * hasn't been properly setup for IO. We kick off an async process |
2016 | * to fix it up. The async helper will wait for ordered extents, set | |
2017 | * the delalloc bit and make it safe to write the page. | |
2018 | */ | |
b2950863 | 2019 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2020 | { |
2021 | struct inode *inode = page->mapping->host; | |
2022 | struct btrfs_writepage_fixup *fixup; | |
2023 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
247e743c | 2024 | |
8b62b72b CM |
2025 | /* this page is properly in the ordered list */ |
2026 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2027 | return 0; |
2028 | ||
2029 | if (PageChecked(page)) | |
2030 | return -EAGAIN; | |
2031 | ||
2032 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2033 | if (!fixup) | |
2034 | return -EAGAIN; | |
f421950f | 2035 | |
247e743c CM |
2036 | SetPageChecked(page); |
2037 | page_cache_get(page); | |
9e0af237 LB |
2038 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2039 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2040 | fixup->page = page; |
dc6e3209 | 2041 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
87826df0 | 2042 | return -EBUSY; |
247e743c CM |
2043 | } |
2044 | ||
d899e052 YZ |
2045 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2046 | struct inode *inode, u64 file_pos, | |
2047 | u64 disk_bytenr, u64 disk_num_bytes, | |
2048 | u64 num_bytes, u64 ram_bytes, | |
2049 | u8 compression, u8 encryption, | |
2050 | u16 other_encoding, int extent_type) | |
2051 | { | |
2052 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2053 | struct btrfs_file_extent_item *fi; | |
2054 | struct btrfs_path *path; | |
2055 | struct extent_buffer *leaf; | |
2056 | struct btrfs_key ins; | |
1acae57b | 2057 | int extent_inserted = 0; |
d899e052 YZ |
2058 | int ret; |
2059 | ||
2060 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2061 | if (!path) |
2062 | return -ENOMEM; | |
d899e052 | 2063 | |
a1ed835e CM |
2064 | /* |
2065 | * we may be replacing one extent in the tree with another. | |
2066 | * The new extent is pinned in the extent map, and we don't want | |
2067 | * to drop it from the cache until it is completely in the btree. | |
2068 | * | |
2069 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2070 | * the caller is expected to unpin it and allow it to be merged | |
2071 | * with the others. | |
2072 | */ | |
1acae57b FDBM |
2073 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2074 | file_pos + num_bytes, NULL, 0, | |
2075 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2076 | if (ret) |
2077 | goto out; | |
d899e052 | 2078 | |
1acae57b FDBM |
2079 | if (!extent_inserted) { |
2080 | ins.objectid = btrfs_ino(inode); | |
2081 | ins.offset = file_pos; | |
2082 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2083 | ||
2084 | path->leave_spinning = 1; | |
2085 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2086 | sizeof(*fi)); | |
2087 | if (ret) | |
2088 | goto out; | |
2089 | } | |
d899e052 YZ |
2090 | leaf = path->nodes[0]; |
2091 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2092 | struct btrfs_file_extent_item); | |
2093 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2094 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2095 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2096 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2097 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2098 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2099 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2100 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2101 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2102 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2103 | |
d899e052 | 2104 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2105 | btrfs_release_path(path); |
d899e052 YZ |
2106 | |
2107 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2108 | |
2109 | ins.objectid = disk_bytenr; | |
2110 | ins.offset = disk_num_bytes; | |
2111 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 YZ |
2112 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2113 | root->root_key.objectid, | |
33345d01 | 2114 | btrfs_ino(inode), file_pos, &ins); |
79787eaa | 2115 | out: |
d899e052 | 2116 | btrfs_free_path(path); |
b9473439 | 2117 | |
79787eaa | 2118 | return ret; |
d899e052 YZ |
2119 | } |
2120 | ||
38c227d8 LB |
2121 | /* snapshot-aware defrag */ |
2122 | struct sa_defrag_extent_backref { | |
2123 | struct rb_node node; | |
2124 | struct old_sa_defrag_extent *old; | |
2125 | u64 root_id; | |
2126 | u64 inum; | |
2127 | u64 file_pos; | |
2128 | u64 extent_offset; | |
2129 | u64 num_bytes; | |
2130 | u64 generation; | |
2131 | }; | |
2132 | ||
2133 | struct old_sa_defrag_extent { | |
2134 | struct list_head list; | |
2135 | struct new_sa_defrag_extent *new; | |
2136 | ||
2137 | u64 extent_offset; | |
2138 | u64 bytenr; | |
2139 | u64 offset; | |
2140 | u64 len; | |
2141 | int count; | |
2142 | }; | |
2143 | ||
2144 | struct new_sa_defrag_extent { | |
2145 | struct rb_root root; | |
2146 | struct list_head head; | |
2147 | struct btrfs_path *path; | |
2148 | struct inode *inode; | |
2149 | u64 file_pos; | |
2150 | u64 len; | |
2151 | u64 bytenr; | |
2152 | u64 disk_len; | |
2153 | u8 compress_type; | |
2154 | }; | |
2155 | ||
2156 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2157 | struct sa_defrag_extent_backref *b2) | |
2158 | { | |
2159 | if (b1->root_id < b2->root_id) | |
2160 | return -1; | |
2161 | else if (b1->root_id > b2->root_id) | |
2162 | return 1; | |
2163 | ||
2164 | if (b1->inum < b2->inum) | |
2165 | return -1; | |
2166 | else if (b1->inum > b2->inum) | |
2167 | return 1; | |
2168 | ||
2169 | if (b1->file_pos < b2->file_pos) | |
2170 | return -1; | |
2171 | else if (b1->file_pos > b2->file_pos) | |
2172 | return 1; | |
2173 | ||
2174 | /* | |
2175 | * [------------------------------] ===> (a range of space) | |
2176 | * |<--->| |<---->| =============> (fs/file tree A) | |
2177 | * |<---------------------------->| ===> (fs/file tree B) | |
2178 | * | |
2179 | * A range of space can refer to two file extents in one tree while | |
2180 | * refer to only one file extent in another tree. | |
2181 | * | |
2182 | * So we may process a disk offset more than one time(two extents in A) | |
2183 | * and locate at the same extent(one extent in B), then insert two same | |
2184 | * backrefs(both refer to the extent in B). | |
2185 | */ | |
2186 | return 0; | |
2187 | } | |
2188 | ||
2189 | static void backref_insert(struct rb_root *root, | |
2190 | struct sa_defrag_extent_backref *backref) | |
2191 | { | |
2192 | struct rb_node **p = &root->rb_node; | |
2193 | struct rb_node *parent = NULL; | |
2194 | struct sa_defrag_extent_backref *entry; | |
2195 | int ret; | |
2196 | ||
2197 | while (*p) { | |
2198 | parent = *p; | |
2199 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2200 | ||
2201 | ret = backref_comp(backref, entry); | |
2202 | if (ret < 0) | |
2203 | p = &(*p)->rb_left; | |
2204 | else | |
2205 | p = &(*p)->rb_right; | |
2206 | } | |
2207 | ||
2208 | rb_link_node(&backref->node, parent, p); | |
2209 | rb_insert_color(&backref->node, root); | |
2210 | } | |
2211 | ||
2212 | /* | |
2213 | * Note the backref might has changed, and in this case we just return 0. | |
2214 | */ | |
2215 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2216 | void *ctx) | |
2217 | { | |
2218 | struct btrfs_file_extent_item *extent; | |
2219 | struct btrfs_fs_info *fs_info; | |
2220 | struct old_sa_defrag_extent *old = ctx; | |
2221 | struct new_sa_defrag_extent *new = old->new; | |
2222 | struct btrfs_path *path = new->path; | |
2223 | struct btrfs_key key; | |
2224 | struct btrfs_root *root; | |
2225 | struct sa_defrag_extent_backref *backref; | |
2226 | struct extent_buffer *leaf; | |
2227 | struct inode *inode = new->inode; | |
2228 | int slot; | |
2229 | int ret; | |
2230 | u64 extent_offset; | |
2231 | u64 num_bytes; | |
2232 | ||
2233 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
2234 | inum == btrfs_ino(inode)) | |
2235 | return 0; | |
2236 | ||
2237 | key.objectid = root_id; | |
2238 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2239 | key.offset = (u64)-1; | |
2240 | ||
2241 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2242 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2243 | if (IS_ERR(root)) { | |
2244 | if (PTR_ERR(root) == -ENOENT) | |
2245 | return 0; | |
2246 | WARN_ON(1); | |
2247 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", | |
2248 | inum, offset, root_id); | |
2249 | return PTR_ERR(root); | |
2250 | } | |
2251 | ||
2252 | key.objectid = inum; | |
2253 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2254 | if (offset > (u64)-1 << 32) | |
2255 | key.offset = 0; | |
2256 | else | |
2257 | key.offset = offset; | |
2258 | ||
2259 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2260 | if (WARN_ON(ret < 0)) |
38c227d8 | 2261 | return ret; |
50f1319c | 2262 | ret = 0; |
38c227d8 LB |
2263 | |
2264 | while (1) { | |
2265 | cond_resched(); | |
2266 | ||
2267 | leaf = path->nodes[0]; | |
2268 | slot = path->slots[0]; | |
2269 | ||
2270 | if (slot >= btrfs_header_nritems(leaf)) { | |
2271 | ret = btrfs_next_leaf(root, path); | |
2272 | if (ret < 0) { | |
2273 | goto out; | |
2274 | } else if (ret > 0) { | |
2275 | ret = 0; | |
2276 | goto out; | |
2277 | } | |
2278 | continue; | |
2279 | } | |
2280 | ||
2281 | path->slots[0]++; | |
2282 | ||
2283 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2284 | ||
2285 | if (key.objectid > inum) | |
2286 | goto out; | |
2287 | ||
2288 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2289 | continue; | |
2290 | ||
2291 | extent = btrfs_item_ptr(leaf, slot, | |
2292 | struct btrfs_file_extent_item); | |
2293 | ||
2294 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2295 | continue; | |
2296 | ||
e68afa49 LB |
2297 | /* |
2298 | * 'offset' refers to the exact key.offset, | |
2299 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2300 | * (key.offset - extent_offset). | |
2301 | */ | |
2302 | if (key.offset != offset) | |
38c227d8 LB |
2303 | continue; |
2304 | ||
e68afa49 | 2305 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2306 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2307 | |
38c227d8 LB |
2308 | if (extent_offset >= old->extent_offset + old->offset + |
2309 | old->len || extent_offset + num_bytes <= | |
2310 | old->extent_offset + old->offset) | |
2311 | continue; | |
38c227d8 LB |
2312 | break; |
2313 | } | |
2314 | ||
2315 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2316 | if (!backref) { | |
2317 | ret = -ENOENT; | |
2318 | goto out; | |
2319 | } | |
2320 | ||
2321 | backref->root_id = root_id; | |
2322 | backref->inum = inum; | |
e68afa49 | 2323 | backref->file_pos = offset; |
38c227d8 LB |
2324 | backref->num_bytes = num_bytes; |
2325 | backref->extent_offset = extent_offset; | |
2326 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2327 | backref->old = old; | |
2328 | backref_insert(&new->root, backref); | |
2329 | old->count++; | |
2330 | out: | |
2331 | btrfs_release_path(path); | |
2332 | WARN_ON(ret); | |
2333 | return ret; | |
2334 | } | |
2335 | ||
2336 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2337 | struct new_sa_defrag_extent *new) | |
2338 | { | |
2339 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; | |
2340 | struct old_sa_defrag_extent *old, *tmp; | |
2341 | int ret; | |
2342 | ||
2343 | new->path = path; | |
2344 | ||
2345 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2346 | ret = iterate_inodes_from_logical(old->bytenr + |
2347 | old->extent_offset, fs_info, | |
38c227d8 LB |
2348 | path, record_one_backref, |
2349 | old); | |
4724b106 JB |
2350 | if (ret < 0 && ret != -ENOENT) |
2351 | return false; | |
38c227d8 LB |
2352 | |
2353 | /* no backref to be processed for this extent */ | |
2354 | if (!old->count) { | |
2355 | list_del(&old->list); | |
2356 | kfree(old); | |
2357 | } | |
2358 | } | |
2359 | ||
2360 | if (list_empty(&new->head)) | |
2361 | return false; | |
2362 | ||
2363 | return true; | |
2364 | } | |
2365 | ||
2366 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2367 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2368 | struct new_sa_defrag_extent *new) |
38c227d8 | 2369 | { |
116e0024 | 2370 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2371 | return 0; |
2372 | ||
2373 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2374 | return 0; | |
2375 | ||
116e0024 LB |
2376 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2377 | return 0; | |
2378 | ||
2379 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2380 | btrfs_file_extent_other_encoding(leaf, fi)) |
2381 | return 0; | |
2382 | ||
2383 | return 1; | |
2384 | } | |
2385 | ||
2386 | /* | |
2387 | * Note the backref might has changed, and in this case we just return 0. | |
2388 | */ | |
2389 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2390 | struct sa_defrag_extent_backref *prev, | |
2391 | struct sa_defrag_extent_backref *backref) | |
2392 | { | |
2393 | struct btrfs_file_extent_item *extent; | |
2394 | struct btrfs_file_extent_item *item; | |
2395 | struct btrfs_ordered_extent *ordered; | |
2396 | struct btrfs_trans_handle *trans; | |
2397 | struct btrfs_fs_info *fs_info; | |
2398 | struct btrfs_root *root; | |
2399 | struct btrfs_key key; | |
2400 | struct extent_buffer *leaf; | |
2401 | struct old_sa_defrag_extent *old = backref->old; | |
2402 | struct new_sa_defrag_extent *new = old->new; | |
2403 | struct inode *src_inode = new->inode; | |
2404 | struct inode *inode; | |
2405 | struct extent_state *cached = NULL; | |
2406 | int ret = 0; | |
2407 | u64 start; | |
2408 | u64 len; | |
2409 | u64 lock_start; | |
2410 | u64 lock_end; | |
2411 | bool merge = false; | |
2412 | int index; | |
2413 | ||
2414 | if (prev && prev->root_id == backref->root_id && | |
2415 | prev->inum == backref->inum && | |
2416 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2417 | merge = true; | |
2418 | ||
2419 | /* step 1: get root */ | |
2420 | key.objectid = backref->root_id; | |
2421 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2422 | key.offset = (u64)-1; | |
2423 | ||
2424 | fs_info = BTRFS_I(src_inode)->root->fs_info; | |
2425 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
2426 | ||
2427 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2428 | if (IS_ERR(root)) { | |
2429 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2430 | if (PTR_ERR(root) == -ENOENT) | |
2431 | return 0; | |
2432 | return PTR_ERR(root); | |
2433 | } | |
38c227d8 | 2434 | |
bcbba5e6 WS |
2435 | if (btrfs_root_readonly(root)) { |
2436 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2437 | return 0; | |
2438 | } | |
2439 | ||
38c227d8 LB |
2440 | /* step 2: get inode */ |
2441 | key.objectid = backref->inum; | |
2442 | key.type = BTRFS_INODE_ITEM_KEY; | |
2443 | key.offset = 0; | |
2444 | ||
2445 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2446 | if (IS_ERR(inode)) { | |
2447 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2448 | return 0; | |
2449 | } | |
2450 | ||
2451 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2452 | ||
2453 | /* step 3: relink backref */ | |
2454 | lock_start = backref->file_pos; | |
2455 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2456 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2457 | 0, &cached); | |
2458 | ||
2459 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2460 | if (ordered) { | |
2461 | btrfs_put_ordered_extent(ordered); | |
2462 | goto out_unlock; | |
2463 | } | |
2464 | ||
2465 | trans = btrfs_join_transaction(root); | |
2466 | if (IS_ERR(trans)) { | |
2467 | ret = PTR_ERR(trans); | |
2468 | goto out_unlock; | |
2469 | } | |
2470 | ||
2471 | key.objectid = backref->inum; | |
2472 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2473 | key.offset = backref->file_pos; | |
2474 | ||
2475 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2476 | if (ret < 0) { | |
2477 | goto out_free_path; | |
2478 | } else if (ret > 0) { | |
2479 | ret = 0; | |
2480 | goto out_free_path; | |
2481 | } | |
2482 | ||
2483 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2484 | struct btrfs_file_extent_item); | |
2485 | ||
2486 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2487 | backref->generation) | |
2488 | goto out_free_path; | |
2489 | ||
2490 | btrfs_release_path(path); | |
2491 | ||
2492 | start = backref->file_pos; | |
2493 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2494 | start += old->extent_offset + old->offset - | |
2495 | backref->extent_offset; | |
2496 | ||
2497 | len = min(backref->extent_offset + backref->num_bytes, | |
2498 | old->extent_offset + old->offset + old->len); | |
2499 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2500 | ||
2501 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2502 | start + len, 1); | |
2503 | if (ret) | |
2504 | goto out_free_path; | |
2505 | again: | |
2506 | key.objectid = btrfs_ino(inode); | |
2507 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2508 | key.offset = start; | |
2509 | ||
a09a0a70 | 2510 | path->leave_spinning = 1; |
38c227d8 LB |
2511 | if (merge) { |
2512 | struct btrfs_file_extent_item *fi; | |
2513 | u64 extent_len; | |
2514 | struct btrfs_key found_key; | |
2515 | ||
3c9665df | 2516 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2517 | if (ret < 0) |
2518 | goto out_free_path; | |
2519 | ||
2520 | path->slots[0]--; | |
2521 | leaf = path->nodes[0]; | |
2522 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2523 | ||
2524 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2525 | struct btrfs_file_extent_item); | |
2526 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2527 | ||
116e0024 LB |
2528 | if (extent_len + found_key.offset == start && |
2529 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2530 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2531 | extent_len + len); | |
2532 | btrfs_mark_buffer_dirty(leaf); | |
2533 | inode_add_bytes(inode, len); | |
2534 | ||
2535 | ret = 1; | |
2536 | goto out_free_path; | |
2537 | } else { | |
2538 | merge = false; | |
2539 | btrfs_release_path(path); | |
2540 | goto again; | |
2541 | } | |
2542 | } | |
2543 | ||
2544 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2545 | sizeof(*extent)); | |
2546 | if (ret) { | |
2547 | btrfs_abort_transaction(trans, root, ret); | |
2548 | goto out_free_path; | |
2549 | } | |
2550 | ||
2551 | leaf = path->nodes[0]; | |
2552 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2553 | struct btrfs_file_extent_item); | |
2554 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2555 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2556 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2557 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2558 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2559 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2560 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2561 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2562 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2563 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2564 | ||
2565 | btrfs_mark_buffer_dirty(leaf); | |
2566 | inode_add_bytes(inode, len); | |
a09a0a70 | 2567 | btrfs_release_path(path); |
38c227d8 LB |
2568 | |
2569 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, | |
2570 | new->disk_len, 0, | |
2571 | backref->root_id, backref->inum, | |
2572 | new->file_pos, 0); /* start - extent_offset */ | |
2573 | if (ret) { | |
2574 | btrfs_abort_transaction(trans, root, ret); | |
2575 | goto out_free_path; | |
2576 | } | |
2577 | ||
2578 | ret = 1; | |
2579 | out_free_path: | |
2580 | btrfs_release_path(path); | |
a09a0a70 | 2581 | path->leave_spinning = 0; |
38c227d8 LB |
2582 | btrfs_end_transaction(trans, root); |
2583 | out_unlock: | |
2584 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2585 | &cached, GFP_NOFS); | |
2586 | iput(inode); | |
2587 | return ret; | |
2588 | } | |
2589 | ||
6f519564 LB |
2590 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2591 | { | |
2592 | struct old_sa_defrag_extent *old, *tmp; | |
2593 | ||
2594 | if (!new) | |
2595 | return; | |
2596 | ||
2597 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
2598 | list_del(&old->list); | |
2599 | kfree(old); | |
2600 | } | |
2601 | kfree(new); | |
2602 | } | |
2603 | ||
38c227d8 LB |
2604 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2605 | { | |
2606 | struct btrfs_path *path; | |
38c227d8 LB |
2607 | struct sa_defrag_extent_backref *backref; |
2608 | struct sa_defrag_extent_backref *prev = NULL; | |
2609 | struct inode *inode; | |
2610 | struct btrfs_root *root; | |
2611 | struct rb_node *node; | |
2612 | int ret; | |
2613 | ||
2614 | inode = new->inode; | |
2615 | root = BTRFS_I(inode)->root; | |
2616 | ||
2617 | path = btrfs_alloc_path(); | |
2618 | if (!path) | |
2619 | return; | |
2620 | ||
2621 | if (!record_extent_backrefs(path, new)) { | |
2622 | btrfs_free_path(path); | |
2623 | goto out; | |
2624 | } | |
2625 | btrfs_release_path(path); | |
2626 | ||
2627 | while (1) { | |
2628 | node = rb_first(&new->root); | |
2629 | if (!node) | |
2630 | break; | |
2631 | rb_erase(node, &new->root); | |
2632 | ||
2633 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2634 | ||
2635 | ret = relink_extent_backref(path, prev, backref); | |
2636 | WARN_ON(ret < 0); | |
2637 | ||
2638 | kfree(prev); | |
2639 | ||
2640 | if (ret == 1) | |
2641 | prev = backref; | |
2642 | else | |
2643 | prev = NULL; | |
2644 | cond_resched(); | |
2645 | } | |
2646 | kfree(prev); | |
2647 | ||
2648 | btrfs_free_path(path); | |
38c227d8 | 2649 | out: |
6f519564 LB |
2650 | free_sa_defrag_extent(new); |
2651 | ||
38c227d8 LB |
2652 | atomic_dec(&root->fs_info->defrag_running); |
2653 | wake_up(&root->fs_info->transaction_wait); | |
38c227d8 LB |
2654 | } |
2655 | ||
2656 | static struct new_sa_defrag_extent * | |
2657 | record_old_file_extents(struct inode *inode, | |
2658 | struct btrfs_ordered_extent *ordered) | |
2659 | { | |
2660 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2661 | struct btrfs_path *path; | |
2662 | struct btrfs_key key; | |
6f519564 | 2663 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2664 | struct new_sa_defrag_extent *new; |
2665 | int ret; | |
2666 | ||
2667 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2668 | if (!new) | |
2669 | return NULL; | |
2670 | ||
2671 | new->inode = inode; | |
2672 | new->file_pos = ordered->file_offset; | |
2673 | new->len = ordered->len; | |
2674 | new->bytenr = ordered->start; | |
2675 | new->disk_len = ordered->disk_len; | |
2676 | new->compress_type = ordered->compress_type; | |
2677 | new->root = RB_ROOT; | |
2678 | INIT_LIST_HEAD(&new->head); | |
2679 | ||
2680 | path = btrfs_alloc_path(); | |
2681 | if (!path) | |
2682 | goto out_kfree; | |
2683 | ||
2684 | key.objectid = btrfs_ino(inode); | |
2685 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2686 | key.offset = new->file_pos; | |
2687 | ||
2688 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2689 | if (ret < 0) | |
2690 | goto out_free_path; | |
2691 | if (ret > 0 && path->slots[0] > 0) | |
2692 | path->slots[0]--; | |
2693 | ||
2694 | /* find out all the old extents for the file range */ | |
2695 | while (1) { | |
2696 | struct btrfs_file_extent_item *extent; | |
2697 | struct extent_buffer *l; | |
2698 | int slot; | |
2699 | u64 num_bytes; | |
2700 | u64 offset; | |
2701 | u64 end; | |
2702 | u64 disk_bytenr; | |
2703 | u64 extent_offset; | |
2704 | ||
2705 | l = path->nodes[0]; | |
2706 | slot = path->slots[0]; | |
2707 | ||
2708 | if (slot >= btrfs_header_nritems(l)) { | |
2709 | ret = btrfs_next_leaf(root, path); | |
2710 | if (ret < 0) | |
6f519564 | 2711 | goto out_free_path; |
38c227d8 LB |
2712 | else if (ret > 0) |
2713 | break; | |
2714 | continue; | |
2715 | } | |
2716 | ||
2717 | btrfs_item_key_to_cpu(l, &key, slot); | |
2718 | ||
2719 | if (key.objectid != btrfs_ino(inode)) | |
2720 | break; | |
2721 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2722 | break; | |
2723 | if (key.offset >= new->file_pos + new->len) | |
2724 | break; | |
2725 | ||
2726 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2727 | ||
2728 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2729 | if (key.offset + num_bytes < new->file_pos) | |
2730 | goto next; | |
2731 | ||
2732 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2733 | if (!disk_bytenr) | |
2734 | goto next; | |
2735 | ||
2736 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2737 | ||
2738 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2739 | if (!old) | |
6f519564 | 2740 | goto out_free_path; |
38c227d8 LB |
2741 | |
2742 | offset = max(new->file_pos, key.offset); | |
2743 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2744 | ||
2745 | old->bytenr = disk_bytenr; | |
2746 | old->extent_offset = extent_offset; | |
2747 | old->offset = offset - key.offset; | |
2748 | old->len = end - offset; | |
2749 | old->new = new; | |
2750 | old->count = 0; | |
2751 | list_add_tail(&old->list, &new->head); | |
2752 | next: | |
2753 | path->slots[0]++; | |
2754 | cond_resched(); | |
2755 | } | |
2756 | ||
2757 | btrfs_free_path(path); | |
2758 | atomic_inc(&root->fs_info->defrag_running); | |
2759 | ||
2760 | return new; | |
2761 | ||
38c227d8 LB |
2762 | out_free_path: |
2763 | btrfs_free_path(path); | |
2764 | out_kfree: | |
6f519564 | 2765 | free_sa_defrag_extent(new); |
38c227d8 LB |
2766 | return NULL; |
2767 | } | |
2768 | ||
e570fd27 MX |
2769 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
2770 | u64 start, u64 len) | |
2771 | { | |
2772 | struct btrfs_block_group_cache *cache; | |
2773 | ||
2774 | cache = btrfs_lookup_block_group(root->fs_info, start); | |
2775 | ASSERT(cache); | |
2776 | ||
2777 | spin_lock(&cache->lock); | |
2778 | cache->delalloc_bytes -= len; | |
2779 | spin_unlock(&cache->lock); | |
2780 | ||
2781 | btrfs_put_block_group(cache); | |
2782 | } | |
2783 | ||
d352ac68 CM |
2784 | /* as ordered data IO finishes, this gets called so we can finish |
2785 | * an ordered extent if the range of bytes in the file it covers are | |
2786 | * fully written. | |
2787 | */ | |
5fd02043 | 2788 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2789 | { |
5fd02043 | 2790 | struct inode *inode = ordered_extent->inode; |
e6dcd2dc | 2791 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2792 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2793 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2794 | struct extent_state *cached_state = NULL; |
38c227d8 | 2795 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2796 | int compress_type = 0; |
77cef2ec JB |
2797 | int ret = 0; |
2798 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2799 | bool nolock; |
77cef2ec | 2800 | bool truncated = false; |
e6dcd2dc | 2801 | |
83eea1f1 | 2802 | nolock = btrfs_is_free_space_inode(inode); |
0cb59c99 | 2803 | |
5fd02043 JB |
2804 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2805 | ret = -EIO; | |
2806 | goto out; | |
2807 | } | |
2808 | ||
f612496b MX |
2809 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
2810 | ordered_extent->file_offset + | |
2811 | ordered_extent->len - 1); | |
2812 | ||
77cef2ec JB |
2813 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2814 | truncated = true; | |
2815 | logical_len = ordered_extent->truncated_len; | |
2816 | /* Truncated the entire extent, don't bother adding */ | |
2817 | if (!logical_len) | |
2818 | goto out; | |
2819 | } | |
2820 | ||
c2167754 | 2821 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2822 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
6c760c07 JB |
2823 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2824 | if (nolock) | |
2825 | trans = btrfs_join_transaction_nolock(root); | |
2826 | else | |
2827 | trans = btrfs_join_transaction(root); | |
2828 | if (IS_ERR(trans)) { | |
2829 | ret = PTR_ERR(trans); | |
2830 | trans = NULL; | |
2831 | goto out; | |
c2167754 | 2832 | } |
6c760c07 JB |
2833 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
2834 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2835 | if (ret) /* -ENOMEM or corruption */ | |
2836 | btrfs_abort_transaction(trans, root, ret); | |
c2167754 YZ |
2837 | goto out; |
2838 | } | |
e6dcd2dc | 2839 | |
2ac55d41 JB |
2840 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2841 | ordered_extent->file_offset + ordered_extent->len - 1, | |
d0082371 | 2842 | 0, &cached_state); |
e6dcd2dc | 2843 | |
38c227d8 LB |
2844 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2845 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2846 | EXTENT_DEFRAG, 1, cached_state); | |
2847 | if (ret) { | |
2848 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2849 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2850 | /* the inode is shared */ |
2851 | new = record_old_file_extents(inode, ordered_extent); | |
2852 | ||
2853 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2854 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2855 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2856 | } | |
2857 | ||
0cb59c99 | 2858 | if (nolock) |
7a7eaa40 | 2859 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2860 | else |
7a7eaa40 | 2861 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2862 | if (IS_ERR(trans)) { |
2863 | ret = PTR_ERR(trans); | |
2864 | trans = NULL; | |
2865 | goto out_unlock; | |
2866 | } | |
a79b7d4b | 2867 | |
0ca1f7ce | 2868 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
c2167754 | 2869 | |
c8b97818 | 2870 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2871 | compress_type = ordered_extent->compress_type; |
d899e052 | 2872 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2873 | BUG_ON(compress_type); |
920bbbfb | 2874 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
2875 | ordered_extent->file_offset, |
2876 | ordered_extent->file_offset + | |
77cef2ec | 2877 | logical_len); |
d899e052 | 2878 | } else { |
0af3d00b | 2879 | BUG_ON(root == root->fs_info->tree_root); |
d899e052 YZ |
2880 | ret = insert_reserved_file_extent(trans, inode, |
2881 | ordered_extent->file_offset, | |
2882 | ordered_extent->start, | |
2883 | ordered_extent->disk_len, | |
77cef2ec | 2884 | logical_len, logical_len, |
261507a0 | 2885 | compress_type, 0, 0, |
d899e052 | 2886 | BTRFS_FILE_EXTENT_REG); |
e570fd27 MX |
2887 | if (!ret) |
2888 | btrfs_release_delalloc_bytes(root, | |
2889 | ordered_extent->start, | |
2890 | ordered_extent->disk_len); | |
d899e052 | 2891 | } |
5dc562c5 JB |
2892 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
2893 | ordered_extent->file_offset, ordered_extent->len, | |
2894 | trans->transid); | |
79787eaa JM |
2895 | if (ret < 0) { |
2896 | btrfs_abort_transaction(trans, root, ret); | |
5fd02043 | 2897 | goto out_unlock; |
79787eaa | 2898 | } |
2ac55d41 | 2899 | |
e6dcd2dc CM |
2900 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
2901 | &ordered_extent->list); | |
2902 | ||
6c760c07 JB |
2903 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2904 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2905 | if (ret) { /* -ENOMEM or corruption */ | |
2906 | btrfs_abort_transaction(trans, root, ret); | |
2907 | goto out_unlock; | |
1ef30be1 JB |
2908 | } |
2909 | ret = 0; | |
5fd02043 JB |
2910 | out_unlock: |
2911 | unlock_extent_cached(io_tree, ordered_extent->file_offset, | |
2912 | ordered_extent->file_offset + | |
2913 | ordered_extent->len - 1, &cached_state, GFP_NOFS); | |
c2167754 | 2914 | out: |
5b0e95bf | 2915 | if (root != root->fs_info->tree_root) |
0cb59c99 | 2916 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
a698d075 MX |
2917 | if (trans) |
2918 | btrfs_end_transaction(trans, root); | |
0cb59c99 | 2919 | |
77cef2ec JB |
2920 | if (ret || truncated) { |
2921 | u64 start, end; | |
2922 | ||
2923 | if (truncated) | |
2924 | start = ordered_extent->file_offset + logical_len; | |
2925 | else | |
2926 | start = ordered_extent->file_offset; | |
2927 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
2928 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
2929 | ||
2930 | /* Drop the cache for the part of the extent we didn't write. */ | |
2931 | btrfs_drop_extent_cache(inode, start, end, 0); | |
5fd02043 | 2932 | |
0bec9ef5 JB |
2933 | /* |
2934 | * If the ordered extent had an IOERR or something else went | |
2935 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
2936 | * back to the allocator. We only free the extent in the |
2937 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 2938 | */ |
77cef2ec JB |
2939 | if ((ret || !logical_len) && |
2940 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 JB |
2941 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2942 | btrfs_free_reserved_extent(root, ordered_extent->start, | |
e570fd27 | 2943 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
2944 | } |
2945 | ||
2946 | ||
5fd02043 | 2947 | /* |
8bad3c02 LB |
2948 | * This needs to be done to make sure anybody waiting knows we are done |
2949 | * updating everything for this ordered extent. | |
5fd02043 JB |
2950 | */ |
2951 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
2952 | ||
38c227d8 | 2953 | /* for snapshot-aware defrag */ |
6f519564 LB |
2954 | if (new) { |
2955 | if (ret) { | |
2956 | free_sa_defrag_extent(new); | |
2957 | atomic_dec(&root->fs_info->defrag_running); | |
2958 | } else { | |
2959 | relink_file_extents(new); | |
2960 | } | |
2961 | } | |
38c227d8 | 2962 | |
e6dcd2dc CM |
2963 | /* once for us */ |
2964 | btrfs_put_ordered_extent(ordered_extent); | |
2965 | /* once for the tree */ | |
2966 | btrfs_put_ordered_extent(ordered_extent); | |
2967 | ||
5fd02043 JB |
2968 | return ret; |
2969 | } | |
2970 | ||
2971 | static void finish_ordered_fn(struct btrfs_work *work) | |
2972 | { | |
2973 | struct btrfs_ordered_extent *ordered_extent; | |
2974 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
2975 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
2976 | } |
2977 | ||
b2950863 | 2978 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
2979 | struct extent_state *state, int uptodate) |
2980 | { | |
5fd02043 JB |
2981 | struct inode *inode = page->mapping->host; |
2982 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2983 | struct btrfs_ordered_extent *ordered_extent = NULL; | |
9e0af237 LB |
2984 | struct btrfs_workqueue *wq; |
2985 | btrfs_work_func_t func; | |
5fd02043 | 2986 | |
1abe9b8a | 2987 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
2988 | ||
8b62b72b | 2989 | ClearPagePrivate2(page); |
5fd02043 JB |
2990 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
2991 | end - start + 1, uptodate)) | |
2992 | return 0; | |
2993 | ||
9e0af237 LB |
2994 | if (btrfs_is_free_space_inode(inode)) { |
2995 | wq = root->fs_info->endio_freespace_worker; | |
2996 | func = btrfs_freespace_write_helper; | |
2997 | } else { | |
2998 | wq = root->fs_info->endio_write_workers; | |
2999 | func = btrfs_endio_write_helper; | |
3000 | } | |
5fd02043 | 3001 | |
9e0af237 LB |
3002 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3003 | NULL); | |
3004 | btrfs_queue_work(wq, &ordered_extent->work); | |
5fd02043 JB |
3005 | |
3006 | return 0; | |
211f90e6 CM |
3007 | } |
3008 | ||
dc380aea MX |
3009 | static int __readpage_endio_check(struct inode *inode, |
3010 | struct btrfs_io_bio *io_bio, | |
3011 | int icsum, struct page *page, | |
3012 | int pgoff, u64 start, size_t len) | |
3013 | { | |
3014 | char *kaddr; | |
3015 | u32 csum_expected; | |
3016 | u32 csum = ~(u32)0; | |
3017 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, | |
3018 | DEFAULT_RATELIMIT_BURST); | |
3019 | ||
3020 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3021 | ||
3022 | kaddr = kmap_atomic(page); | |
3023 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
3024 | btrfs_csum_final(csum, (char *)&csum); | |
3025 | if (csum != csum_expected) | |
3026 | goto zeroit; | |
3027 | ||
3028 | kunmap_atomic(kaddr); | |
3029 | return 0; | |
3030 | zeroit: | |
3031 | if (__ratelimit(&_rs)) | |
f0954c66 | 3032 | btrfs_warn(BTRFS_I(inode)->root->fs_info, |
dc380aea MX |
3033 | "csum failed ino %llu off %llu csum %u expected csum %u", |
3034 | btrfs_ino(inode), start, csum, csum_expected); | |
3035 | memset(kaddr + pgoff, 1, len); | |
3036 | flush_dcache_page(page); | |
3037 | kunmap_atomic(kaddr); | |
3038 | if (csum_expected == 0) | |
3039 | return 0; | |
3040 | return -EIO; | |
3041 | } | |
3042 | ||
d352ac68 CM |
3043 | /* |
3044 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3045 | * if there's a match, we allow the bio to finish. If not, the code in |
3046 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3047 | */ |
facc8a22 MX |
3048 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3049 | u64 phy_offset, struct page *page, | |
3050 | u64 start, u64 end, int mirror) | |
07157aac | 3051 | { |
4eee4fa4 | 3052 | size_t offset = start - page_offset(page); |
07157aac | 3053 | struct inode *inode = page->mapping->host; |
d1310b2e | 3054 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3055 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3056 | |
d20f7043 CM |
3057 | if (PageChecked(page)) { |
3058 | ClearPageChecked(page); | |
dc380aea | 3059 | return 0; |
d20f7043 | 3060 | } |
6cbff00f CH |
3061 | |
3062 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3063 | return 0; |
17d217fe YZ |
3064 | |
3065 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3066 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
17d217fe YZ |
3067 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
3068 | GFP_NOFS); | |
b6cda9bc | 3069 | return 0; |
17d217fe | 3070 | } |
d20f7043 | 3071 | |
facc8a22 | 3072 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3073 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3074 | start, (size_t)(end - start + 1)); | |
07157aac | 3075 | } |
b888db2b | 3076 | |
24bbcf04 YZ |
3077 | struct delayed_iput { |
3078 | struct list_head list; | |
3079 | struct inode *inode; | |
3080 | }; | |
3081 | ||
79787eaa JM |
3082 | /* JDM: If this is fs-wide, why can't we add a pointer to |
3083 | * btrfs_inode instead and avoid the allocation? */ | |
24bbcf04 YZ |
3084 | void btrfs_add_delayed_iput(struct inode *inode) |
3085 | { | |
3086 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
3087 | struct delayed_iput *delayed; | |
3088 | ||
3089 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3090 | return; | |
3091 | ||
3092 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); | |
3093 | delayed->inode = inode; | |
3094 | ||
3095 | spin_lock(&fs_info->delayed_iput_lock); | |
3096 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); | |
3097 | spin_unlock(&fs_info->delayed_iput_lock); | |
3098 | } | |
3099 | ||
3100 | void btrfs_run_delayed_iputs(struct btrfs_root *root) | |
3101 | { | |
3102 | LIST_HEAD(list); | |
3103 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3104 | struct delayed_iput *delayed; | |
3105 | int empty; | |
3106 | ||
3107 | spin_lock(&fs_info->delayed_iput_lock); | |
3108 | empty = list_empty(&fs_info->delayed_iputs); | |
3109 | spin_unlock(&fs_info->delayed_iput_lock); | |
3110 | if (empty) | |
3111 | return; | |
3112 | ||
d7c15171 ZL |
3113 | down_read(&fs_info->delayed_iput_sem); |
3114 | ||
24bbcf04 YZ |
3115 | spin_lock(&fs_info->delayed_iput_lock); |
3116 | list_splice_init(&fs_info->delayed_iputs, &list); | |
3117 | spin_unlock(&fs_info->delayed_iput_lock); | |
3118 | ||
3119 | while (!list_empty(&list)) { | |
3120 | delayed = list_entry(list.next, struct delayed_iput, list); | |
3121 | list_del(&delayed->list); | |
3122 | iput(delayed->inode); | |
3123 | kfree(delayed); | |
3124 | } | |
d7c15171 ZL |
3125 | |
3126 | up_read(&root->fs_info->delayed_iput_sem); | |
24bbcf04 YZ |
3127 | } |
3128 | ||
d68fc57b | 3129 | /* |
42b2aa86 | 3130 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3131 | * files in the subvolume, it removes orphan item and frees block_rsv |
3132 | * structure. | |
3133 | */ | |
3134 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3135 | struct btrfs_root *root) | |
3136 | { | |
90290e19 | 3137 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3138 | int ret; |
3139 | ||
8a35d95f | 3140 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3141 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3142 | return; | |
3143 | ||
90290e19 | 3144 | spin_lock(&root->orphan_lock); |
8a35d95f | 3145 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3146 | spin_unlock(&root->orphan_lock); |
3147 | return; | |
3148 | } | |
3149 | ||
3150 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3151 | spin_unlock(&root->orphan_lock); | |
3152 | return; | |
3153 | } | |
3154 | ||
3155 | block_rsv = root->orphan_block_rsv; | |
3156 | root->orphan_block_rsv = NULL; | |
3157 | spin_unlock(&root->orphan_lock); | |
3158 | ||
27cdeb70 | 3159 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b YZ |
3160 | btrfs_root_refs(&root->root_item) > 0) { |
3161 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, | |
3162 | root->root_key.objectid); | |
4ef31a45 JB |
3163 | if (ret) |
3164 | btrfs_abort_transaction(trans, root, ret); | |
3165 | else | |
27cdeb70 MX |
3166 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3167 | &root->state); | |
d68fc57b YZ |
3168 | } |
3169 | ||
90290e19 JB |
3170 | if (block_rsv) { |
3171 | WARN_ON(block_rsv->size > 0); | |
3172 | btrfs_free_block_rsv(root, block_rsv); | |
d68fc57b YZ |
3173 | } |
3174 | } | |
3175 | ||
7b128766 JB |
3176 | /* |
3177 | * This creates an orphan entry for the given inode in case something goes | |
3178 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3179 | * |
3180 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3181 | * this function. | |
7b128766 JB |
3182 | */ |
3183 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
3184 | { | |
3185 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3186 | struct btrfs_block_rsv *block_rsv = NULL; |
3187 | int reserve = 0; | |
3188 | int insert = 0; | |
3189 | int ret; | |
7b128766 | 3190 | |
d68fc57b | 3191 | if (!root->orphan_block_rsv) { |
66d8f3dd | 3192 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
b532402e TI |
3193 | if (!block_rsv) |
3194 | return -ENOMEM; | |
d68fc57b | 3195 | } |
7b128766 | 3196 | |
d68fc57b YZ |
3197 | spin_lock(&root->orphan_lock); |
3198 | if (!root->orphan_block_rsv) { | |
3199 | root->orphan_block_rsv = block_rsv; | |
3200 | } else if (block_rsv) { | |
3201 | btrfs_free_block_rsv(root, block_rsv); | |
3202 | block_rsv = NULL; | |
7b128766 | 3203 | } |
7b128766 | 3204 | |
8a35d95f JB |
3205 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3206 | &BTRFS_I(inode)->runtime_flags)) { | |
d68fc57b YZ |
3207 | #if 0 |
3208 | /* | |
3209 | * For proper ENOSPC handling, we should do orphan | |
3210 | * cleanup when mounting. But this introduces backward | |
3211 | * compatibility issue. | |
3212 | */ | |
3213 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3214 | insert = 2; | |
3215 | else | |
3216 | insert = 1; | |
3217 | #endif | |
3218 | insert = 1; | |
321f0e70 | 3219 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3220 | } |
3221 | ||
72ac3c0d JB |
3222 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3223 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3224 | reserve = 1; |
d68fc57b | 3225 | spin_unlock(&root->orphan_lock); |
7b128766 | 3226 | |
d68fc57b YZ |
3227 | /* grab metadata reservation from transaction handle */ |
3228 | if (reserve) { | |
3229 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
79787eaa | 3230 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
d68fc57b | 3231 | } |
7b128766 | 3232 | |
d68fc57b YZ |
3233 | /* insert an orphan item to track this unlinked/truncated file */ |
3234 | if (insert >= 1) { | |
33345d01 | 3235 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3236 | if (ret) { |
703c88e0 | 3237 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3238 | if (reserve) { |
3239 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3240 | &BTRFS_I(inode)->runtime_flags); | |
3241 | btrfs_orphan_release_metadata(inode); | |
3242 | } | |
3243 | if (ret != -EEXIST) { | |
e8e7cff6 JB |
3244 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3245 | &BTRFS_I(inode)->runtime_flags); | |
4ef31a45 JB |
3246 | btrfs_abort_transaction(trans, root, ret); |
3247 | return ret; | |
3248 | } | |
79787eaa JM |
3249 | } |
3250 | ret = 0; | |
d68fc57b YZ |
3251 | } |
3252 | ||
3253 | /* insert an orphan item to track subvolume contains orphan files */ | |
3254 | if (insert >= 2) { | |
3255 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, | |
3256 | root->root_key.objectid); | |
79787eaa JM |
3257 | if (ret && ret != -EEXIST) { |
3258 | btrfs_abort_transaction(trans, root, ret); | |
3259 | return ret; | |
3260 | } | |
d68fc57b YZ |
3261 | } |
3262 | return 0; | |
7b128766 JB |
3263 | } |
3264 | ||
3265 | /* | |
3266 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3267 | * item for this particular inode. | |
3268 | */ | |
48a3b636 ES |
3269 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3270 | struct inode *inode) | |
7b128766 JB |
3271 | { |
3272 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3273 | int delete_item = 0; |
3274 | int release_rsv = 0; | |
7b128766 JB |
3275 | int ret = 0; |
3276 | ||
d68fc57b | 3277 | spin_lock(&root->orphan_lock); |
8a35d95f JB |
3278 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3279 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3280 | delete_item = 1; |
7b128766 | 3281 | |
72ac3c0d JB |
3282 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3283 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3284 | release_rsv = 1; |
d68fc57b | 3285 | spin_unlock(&root->orphan_lock); |
7b128766 | 3286 | |
703c88e0 | 3287 | if (delete_item) { |
8a35d95f | 3288 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3289 | if (trans) |
3290 | ret = btrfs_del_orphan_item(trans, root, | |
3291 | btrfs_ino(inode)); | |
8a35d95f | 3292 | } |
7b128766 | 3293 | |
703c88e0 FDBM |
3294 | if (release_rsv) |
3295 | btrfs_orphan_release_metadata(inode); | |
3296 | ||
4ef31a45 | 3297 | return ret; |
7b128766 JB |
3298 | } |
3299 | ||
3300 | /* | |
3301 | * this cleans up any orphans that may be left on the list from the last use | |
3302 | * of this root. | |
3303 | */ | |
66b4ffd1 | 3304 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 JB |
3305 | { |
3306 | struct btrfs_path *path; | |
3307 | struct extent_buffer *leaf; | |
7b128766 JB |
3308 | struct btrfs_key key, found_key; |
3309 | struct btrfs_trans_handle *trans; | |
3310 | struct inode *inode; | |
8f6d7f4f | 3311 | u64 last_objectid = 0; |
7b128766 JB |
3312 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3313 | ||
d68fc57b | 3314 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3315 | return 0; |
c71bf099 YZ |
3316 | |
3317 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3318 | if (!path) { |
3319 | ret = -ENOMEM; | |
3320 | goto out; | |
3321 | } | |
7b128766 JB |
3322 | path->reada = -1; |
3323 | ||
3324 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3325 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3326 | key.offset = (u64)-1; |
3327 | ||
7b128766 JB |
3328 | while (1) { |
3329 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3330 | if (ret < 0) |
3331 | goto out; | |
7b128766 JB |
3332 | |
3333 | /* | |
3334 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3335 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3336 | * find the key and see if we have stuff that matches |
3337 | */ | |
3338 | if (ret > 0) { | |
66b4ffd1 | 3339 | ret = 0; |
7b128766 JB |
3340 | if (path->slots[0] == 0) |
3341 | break; | |
3342 | path->slots[0]--; | |
3343 | } | |
3344 | ||
3345 | /* pull out the item */ | |
3346 | leaf = path->nodes[0]; | |
7b128766 JB |
3347 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3348 | ||
3349 | /* make sure the item matches what we want */ | |
3350 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3351 | break; | |
962a298f | 3352 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3353 | break; |
3354 | ||
3355 | /* release the path since we're done with it */ | |
b3b4aa74 | 3356 | btrfs_release_path(path); |
7b128766 JB |
3357 | |
3358 | /* | |
3359 | * this is where we are basically btrfs_lookup, without the | |
3360 | * crossing root thing. we store the inode number in the | |
3361 | * offset of the orphan item. | |
3362 | */ | |
8f6d7f4f JB |
3363 | |
3364 | if (found_key.offset == last_objectid) { | |
c2cf52eb SK |
3365 | btrfs_err(root->fs_info, |
3366 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3367 | ret = -EINVAL; |
3368 | goto out; | |
3369 | } | |
3370 | ||
3371 | last_objectid = found_key.offset; | |
3372 | ||
5d4f98a2 YZ |
3373 | found_key.objectid = found_key.offset; |
3374 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3375 | found_key.offset = 0; | |
73f73415 | 3376 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3377 | ret = PTR_ERR_OR_ZERO(inode); |
a8c9e576 | 3378 | if (ret && ret != -ESTALE) |
66b4ffd1 | 3379 | goto out; |
7b128766 | 3380 | |
f8e9e0b0 AJ |
3381 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
3382 | struct btrfs_root *dead_root; | |
3383 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3384 | int is_dead_root = 0; | |
3385 | ||
3386 | /* | |
3387 | * this is an orphan in the tree root. Currently these | |
3388 | * could come from 2 sources: | |
3389 | * a) a snapshot deletion in progress | |
3390 | * b) a free space cache inode | |
3391 | * We need to distinguish those two, as the snapshot | |
3392 | * orphan must not get deleted. | |
3393 | * find_dead_roots already ran before us, so if this | |
3394 | * is a snapshot deletion, we should find the root | |
3395 | * in the dead_roots list | |
3396 | */ | |
3397 | spin_lock(&fs_info->trans_lock); | |
3398 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3399 | root_list) { | |
3400 | if (dead_root->root_key.objectid == | |
3401 | found_key.objectid) { | |
3402 | is_dead_root = 1; | |
3403 | break; | |
3404 | } | |
3405 | } | |
3406 | spin_unlock(&fs_info->trans_lock); | |
3407 | if (is_dead_root) { | |
3408 | /* prevent this orphan from being found again */ | |
3409 | key.offset = found_key.objectid - 1; | |
3410 | continue; | |
3411 | } | |
3412 | } | |
7b128766 | 3413 | /* |
a8c9e576 JB |
3414 | * Inode is already gone but the orphan item is still there, |
3415 | * kill the orphan item. | |
7b128766 | 3416 | */ |
a8c9e576 JB |
3417 | if (ret == -ESTALE) { |
3418 | trans = btrfs_start_transaction(root, 1); | |
66b4ffd1 JB |
3419 | if (IS_ERR(trans)) { |
3420 | ret = PTR_ERR(trans); | |
3421 | goto out; | |
3422 | } | |
c2cf52eb SK |
3423 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
3424 | found_key.objectid); | |
a8c9e576 JB |
3425 | ret = btrfs_del_orphan_item(trans, root, |
3426 | found_key.objectid); | |
5b21f2ed | 3427 | btrfs_end_transaction(trans, root); |
4ef31a45 JB |
3428 | if (ret) |
3429 | goto out; | |
7b128766 JB |
3430 | continue; |
3431 | } | |
3432 | ||
a8c9e576 JB |
3433 | /* |
3434 | * add this inode to the orphan list so btrfs_orphan_del does | |
3435 | * the proper thing when we hit it | |
3436 | */ | |
8a35d95f JB |
3437 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3438 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3439 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3440 | |
7b128766 JB |
3441 | /* if we have links, this was a truncate, lets do that */ |
3442 | if (inode->i_nlink) { | |
fae7f21c | 3443 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3444 | iput(inode); |
3445 | continue; | |
3446 | } | |
7b128766 | 3447 | nr_truncate++; |
f3fe820c JB |
3448 | |
3449 | /* 1 for the orphan item deletion. */ | |
3450 | trans = btrfs_start_transaction(root, 1); | |
3451 | if (IS_ERR(trans)) { | |
c69b26b0 | 3452 | iput(inode); |
f3fe820c JB |
3453 | ret = PTR_ERR(trans); |
3454 | goto out; | |
3455 | } | |
3456 | ret = btrfs_orphan_add(trans, inode); | |
3457 | btrfs_end_transaction(trans, root); | |
c69b26b0 JB |
3458 | if (ret) { |
3459 | iput(inode); | |
f3fe820c | 3460 | goto out; |
c69b26b0 | 3461 | } |
f3fe820c | 3462 | |
66b4ffd1 | 3463 | ret = btrfs_truncate(inode); |
4a7d0f68 JB |
3464 | if (ret) |
3465 | btrfs_orphan_del(NULL, inode); | |
7b128766 JB |
3466 | } else { |
3467 | nr_unlink++; | |
3468 | } | |
3469 | ||
3470 | /* this will do delete_inode and everything for us */ | |
3471 | iput(inode); | |
66b4ffd1 JB |
3472 | if (ret) |
3473 | goto out; | |
7b128766 | 3474 | } |
3254c876 MX |
3475 | /* release the path since we're done with it */ |
3476 | btrfs_release_path(path); | |
3477 | ||
d68fc57b YZ |
3478 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3479 | ||
3480 | if (root->orphan_block_rsv) | |
3481 | btrfs_block_rsv_release(root, root->orphan_block_rsv, | |
3482 | (u64)-1); | |
3483 | ||
27cdeb70 MX |
3484 | if (root->orphan_block_rsv || |
3485 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3486 | trans = btrfs_join_transaction(root); |
66b4ffd1 JB |
3487 | if (!IS_ERR(trans)) |
3488 | btrfs_end_transaction(trans, root); | |
d68fc57b | 3489 | } |
7b128766 JB |
3490 | |
3491 | if (nr_unlink) | |
4884b476 | 3492 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3493 | if (nr_truncate) |
4884b476 | 3494 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3495 | |
3496 | out: | |
3497 | if (ret) | |
68b663d1 | 3498 | btrfs_err(root->fs_info, |
c2cf52eb | 3499 | "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3500 | btrfs_free_path(path); |
3501 | return ret; | |
7b128766 JB |
3502 | } |
3503 | ||
46a53cca CM |
3504 | /* |
3505 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3506 | * don't find any xattrs, we know there can't be any acls. | |
3507 | * | |
3508 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3509 | */ | |
3510 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3511 | int slot, u64 objectid, |
3512 | int *first_xattr_slot) | |
46a53cca CM |
3513 | { |
3514 | u32 nritems = btrfs_header_nritems(leaf); | |
3515 | struct btrfs_key found_key; | |
f23b5a59 JB |
3516 | static u64 xattr_access = 0; |
3517 | static u64 xattr_default = 0; | |
46a53cca CM |
3518 | int scanned = 0; |
3519 | ||
f23b5a59 JB |
3520 | if (!xattr_access) { |
3521 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, | |
3522 | strlen(POSIX_ACL_XATTR_ACCESS)); | |
3523 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, | |
3524 | strlen(POSIX_ACL_XATTR_DEFAULT)); | |
3525 | } | |
3526 | ||
46a53cca | 3527 | slot++; |
63541927 | 3528 | *first_xattr_slot = -1; |
46a53cca CM |
3529 | while (slot < nritems) { |
3530 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3531 | ||
3532 | /* we found a different objectid, there must not be acls */ | |
3533 | if (found_key.objectid != objectid) | |
3534 | return 0; | |
3535 | ||
3536 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3537 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3538 | if (*first_xattr_slot == -1) |
3539 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3540 | if (found_key.offset == xattr_access || |
3541 | found_key.offset == xattr_default) | |
3542 | return 1; | |
3543 | } | |
46a53cca CM |
3544 | |
3545 | /* | |
3546 | * we found a key greater than an xattr key, there can't | |
3547 | * be any acls later on | |
3548 | */ | |
3549 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3550 | return 0; | |
3551 | ||
3552 | slot++; | |
3553 | scanned++; | |
3554 | ||
3555 | /* | |
3556 | * it goes inode, inode backrefs, xattrs, extents, | |
3557 | * so if there are a ton of hard links to an inode there can | |
3558 | * be a lot of backrefs. Don't waste time searching too hard, | |
3559 | * this is just an optimization | |
3560 | */ | |
3561 | if (scanned >= 8) | |
3562 | break; | |
3563 | } | |
3564 | /* we hit the end of the leaf before we found an xattr or | |
3565 | * something larger than an xattr. We have to assume the inode | |
3566 | * has acls | |
3567 | */ | |
63541927 FDBM |
3568 | if (*first_xattr_slot == -1) |
3569 | *first_xattr_slot = slot; | |
46a53cca CM |
3570 | return 1; |
3571 | } | |
3572 | ||
d352ac68 CM |
3573 | /* |
3574 | * read an inode from the btree into the in-memory inode | |
3575 | */ | |
5d4f98a2 | 3576 | static void btrfs_read_locked_inode(struct inode *inode) |
39279cc3 CM |
3577 | { |
3578 | struct btrfs_path *path; | |
5f39d397 | 3579 | struct extent_buffer *leaf; |
39279cc3 CM |
3580 | struct btrfs_inode_item *inode_item; |
3581 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3582 | struct btrfs_key location; | |
67de1176 | 3583 | unsigned long ptr; |
46a53cca | 3584 | int maybe_acls; |
618e21d5 | 3585 | u32 rdev; |
39279cc3 | 3586 | int ret; |
2f7e33d4 | 3587 | bool filled = false; |
63541927 | 3588 | int first_xattr_slot; |
2f7e33d4 MX |
3589 | |
3590 | ret = btrfs_fill_inode(inode, &rdev); | |
3591 | if (!ret) | |
3592 | filled = true; | |
39279cc3 CM |
3593 | |
3594 | path = btrfs_alloc_path(); | |
1748f843 MF |
3595 | if (!path) |
3596 | goto make_bad; | |
3597 | ||
39279cc3 | 3598 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3599 | |
39279cc3 | 3600 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
5f39d397 | 3601 | if (ret) |
39279cc3 | 3602 | goto make_bad; |
39279cc3 | 3603 | |
5f39d397 | 3604 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3605 | |
3606 | if (filled) | |
67de1176 | 3607 | goto cache_index; |
2f7e33d4 | 3608 | |
5f39d397 CM |
3609 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3610 | struct btrfs_inode_item); | |
5f39d397 | 3611 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3612 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3613 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3614 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
dbe674a9 | 3615 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3616 | |
a937b979 DS |
3617 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3618 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3619 | |
a937b979 DS |
3620 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3621 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3622 | |
a937b979 DS |
3623 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3624 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3625 | |
9cc97d64 | 3626 | BTRFS_I(inode)->i_otime.tv_sec = |
3627 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3628 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3629 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3630 | |
a76a3cd4 | 3631 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3632 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3633 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3634 | ||
6e17d30b YD |
3635 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3636 | inode->i_generation = BTRFS_I(inode)->generation; | |
3637 | inode->i_rdev = 0; | |
3638 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3639 | ||
3640 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3641 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3642 | ||
3643 | cache_index: | |
5dc562c5 JB |
3644 | /* |
3645 | * If we were modified in the current generation and evicted from memory | |
3646 | * and then re-read we need to do a full sync since we don't have any | |
3647 | * idea about which extents were modified before we were evicted from | |
3648 | * cache. | |
6e17d30b YD |
3649 | * |
3650 | * This is required for both inode re-read from disk and delayed inode | |
3651 | * in delayed_nodes_tree. | |
5dc562c5 JB |
3652 | */ |
3653 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) | |
3654 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
3655 | &BTRFS_I(inode)->runtime_flags); | |
3656 | ||
bde6c242 FM |
3657 | /* |
3658 | * We don't persist the id of the transaction where an unlink operation | |
3659 | * against the inode was last made. So here we assume the inode might | |
3660 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3661 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3662 | * between the inode and its parent if the inode is fsync'ed and the log | |
3663 | * replayed. For example, in the scenario: | |
3664 | * | |
3665 | * touch mydir/foo | |
3666 | * ln mydir/foo mydir/bar | |
3667 | * sync | |
3668 | * unlink mydir/bar | |
3669 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3670 | * xfs_io -c fsync mydir/foo | |
3671 | * <power failure> | |
3672 | * mount fs, triggers fsync log replay | |
3673 | * | |
3674 | * We must make sure that when we fsync our inode foo we also log its | |
3675 | * parent inode, otherwise after log replay the parent still has the | |
3676 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3677 | * and doesn't have an inode ref with the name "bar" anymore. | |
3678 | * | |
3679 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
3680 | * but it guarantees correctness at the expense of ocassional full | |
3681 | * transaction commits on fsync if our inode is a directory, or if our | |
3682 | * inode is not a directory, logging its parent unnecessarily. | |
3683 | */ | |
3684 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3685 | ||
67de1176 MX |
3686 | path->slots[0]++; |
3687 | if (inode->i_nlink != 1 || | |
3688 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3689 | goto cache_acl; | |
3690 | ||
3691 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
3692 | if (location.objectid != btrfs_ino(inode)) | |
3693 | goto cache_acl; | |
3694 | ||
3695 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3696 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3697 | struct btrfs_inode_ref *ref; | |
3698 | ||
3699 | ref = (struct btrfs_inode_ref *)ptr; | |
3700 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3701 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3702 | struct btrfs_inode_extref *extref; | |
3703 | ||
3704 | extref = (struct btrfs_inode_extref *)ptr; | |
3705 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3706 | extref); | |
3707 | } | |
2f7e33d4 | 3708 | cache_acl: |
46a53cca CM |
3709 | /* |
3710 | * try to precache a NULL acl entry for files that don't have | |
3711 | * any xattrs or acls | |
3712 | */ | |
33345d01 | 3713 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
63541927 FDBM |
3714 | btrfs_ino(inode), &first_xattr_slot); |
3715 | if (first_xattr_slot != -1) { | |
3716 | path->slots[0] = first_xattr_slot; | |
3717 | ret = btrfs_load_inode_props(inode, path); | |
3718 | if (ret) | |
3719 | btrfs_err(root->fs_info, | |
351fd353 | 3720 | "error loading props for ino %llu (root %llu): %d", |
63541927 FDBM |
3721 | btrfs_ino(inode), |
3722 | root->root_key.objectid, ret); | |
3723 | } | |
3724 | btrfs_free_path(path); | |
3725 | ||
72c04902 AV |
3726 | if (!maybe_acls) |
3727 | cache_no_acl(inode); | |
46a53cca | 3728 | |
39279cc3 | 3729 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3730 | case S_IFREG: |
3731 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3732 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3733 | inode->i_fop = &btrfs_file_operations; |
3734 | inode->i_op = &btrfs_file_inode_operations; | |
3735 | break; | |
3736 | case S_IFDIR: | |
3737 | inode->i_fop = &btrfs_dir_file_operations; | |
3738 | if (root == root->fs_info->tree_root) | |
3739 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
3740 | else | |
3741 | inode->i_op = &btrfs_dir_inode_operations; | |
3742 | break; | |
3743 | case S_IFLNK: | |
3744 | inode->i_op = &btrfs_symlink_inode_operations; | |
3745 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
3746 | break; | |
618e21d5 | 3747 | default: |
0279b4cd | 3748 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3749 | init_special_inode(inode, inode->i_mode, rdev); |
3750 | break; | |
39279cc3 | 3751 | } |
6cbff00f CH |
3752 | |
3753 | btrfs_update_iflags(inode); | |
39279cc3 CM |
3754 | return; |
3755 | ||
3756 | make_bad: | |
39279cc3 | 3757 | btrfs_free_path(path); |
39279cc3 CM |
3758 | make_bad_inode(inode); |
3759 | } | |
3760 | ||
d352ac68 CM |
3761 | /* |
3762 | * given a leaf and an inode, copy the inode fields into the leaf | |
3763 | */ | |
e02119d5 CM |
3764 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3765 | struct extent_buffer *leaf, | |
5f39d397 | 3766 | struct btrfs_inode_item *item, |
39279cc3 CM |
3767 | struct inode *inode) |
3768 | { | |
51fab693 LB |
3769 | struct btrfs_map_token token; |
3770 | ||
3771 | btrfs_init_map_token(&token); | |
5f39d397 | 3772 | |
51fab693 LB |
3773 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3774 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3775 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3776 | &token); | |
3777 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3778 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3779 | |
a937b979 | 3780 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3781 | inode->i_atime.tv_sec, &token); |
a937b979 | 3782 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3783 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3784 | |
a937b979 | 3785 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3786 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3787 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3788 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3789 | |
a937b979 | 3790 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3791 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3792 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3793 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3794 | |
9cc97d64 | 3795 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3796 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3797 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3798 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3799 | ||
51fab693 LB |
3800 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3801 | &token); | |
3802 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3803 | &token); | |
3804 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3805 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3806 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3807 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3808 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3809 | } |
3810 | ||
d352ac68 CM |
3811 | /* |
3812 | * copy everything in the in-memory inode into the btree. | |
3813 | */ | |
2115133f | 3814 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3815 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3816 | { |
3817 | struct btrfs_inode_item *inode_item; | |
3818 | struct btrfs_path *path; | |
5f39d397 | 3819 | struct extent_buffer *leaf; |
39279cc3 CM |
3820 | int ret; |
3821 | ||
3822 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3823 | if (!path) |
3824 | return -ENOMEM; | |
3825 | ||
b9473439 | 3826 | path->leave_spinning = 1; |
16cdcec7 MX |
3827 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3828 | 1); | |
39279cc3 CM |
3829 | if (ret) { |
3830 | if (ret > 0) | |
3831 | ret = -ENOENT; | |
3832 | goto failed; | |
3833 | } | |
3834 | ||
5f39d397 CM |
3835 | leaf = path->nodes[0]; |
3836 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3837 | struct btrfs_inode_item); |
39279cc3 | 3838 | |
e02119d5 | 3839 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3840 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3841 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3842 | ret = 0; |
3843 | failed: | |
39279cc3 CM |
3844 | btrfs_free_path(path); |
3845 | return ret; | |
3846 | } | |
3847 | ||
2115133f CM |
3848 | /* |
3849 | * copy everything in the in-memory inode into the btree. | |
3850 | */ | |
3851 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3852 | struct btrfs_root *root, struct inode *inode) | |
3853 | { | |
3854 | int ret; | |
3855 | ||
3856 | /* | |
3857 | * If the inode is a free space inode, we can deadlock during commit | |
3858 | * if we put it into the delayed code. | |
3859 | * | |
3860 | * The data relocation inode should also be directly updated | |
3861 | * without delay | |
3862 | */ | |
83eea1f1 | 3863 | if (!btrfs_is_free_space_inode(inode) |
1d52c78a JB |
3864 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
3865 | && !root->fs_info->log_root_recovering) { | |
8ea05e3a AB |
3866 | btrfs_update_root_times(trans, root); |
3867 | ||
2115133f CM |
3868 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3869 | if (!ret) | |
3870 | btrfs_set_inode_last_trans(trans, inode); | |
3871 | return ret; | |
3872 | } | |
3873 | ||
3874 | return btrfs_update_inode_item(trans, root, inode); | |
3875 | } | |
3876 | ||
be6aef60 JB |
3877 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3878 | struct btrfs_root *root, | |
3879 | struct inode *inode) | |
2115133f CM |
3880 | { |
3881 | int ret; | |
3882 | ||
3883 | ret = btrfs_update_inode(trans, root, inode); | |
3884 | if (ret == -ENOSPC) | |
3885 | return btrfs_update_inode_item(trans, root, inode); | |
3886 | return ret; | |
3887 | } | |
3888 | ||
d352ac68 CM |
3889 | /* |
3890 | * unlink helper that gets used here in inode.c and in the tree logging | |
3891 | * recovery code. It remove a link in a directory with a given name, and | |
3892 | * also drops the back refs in the inode to the directory | |
3893 | */ | |
92986796 AV |
3894 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3895 | struct btrfs_root *root, | |
3896 | struct inode *dir, struct inode *inode, | |
3897 | const char *name, int name_len) | |
39279cc3 CM |
3898 | { |
3899 | struct btrfs_path *path; | |
39279cc3 | 3900 | int ret = 0; |
5f39d397 | 3901 | struct extent_buffer *leaf; |
39279cc3 | 3902 | struct btrfs_dir_item *di; |
5f39d397 | 3903 | struct btrfs_key key; |
aec7477b | 3904 | u64 index; |
33345d01 LZ |
3905 | u64 ino = btrfs_ino(inode); |
3906 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3907 | |
3908 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3909 | if (!path) { |
3910 | ret = -ENOMEM; | |
554233a6 | 3911 | goto out; |
54aa1f4d CM |
3912 | } |
3913 | ||
b9473439 | 3914 | path->leave_spinning = 1; |
33345d01 | 3915 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
3916 | name, name_len, -1); |
3917 | if (IS_ERR(di)) { | |
3918 | ret = PTR_ERR(di); | |
3919 | goto err; | |
3920 | } | |
3921 | if (!di) { | |
3922 | ret = -ENOENT; | |
3923 | goto err; | |
3924 | } | |
5f39d397 CM |
3925 | leaf = path->nodes[0]; |
3926 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 3927 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3928 | if (ret) |
3929 | goto err; | |
b3b4aa74 | 3930 | btrfs_release_path(path); |
39279cc3 | 3931 | |
67de1176 MX |
3932 | /* |
3933 | * If we don't have dir index, we have to get it by looking up | |
3934 | * the inode ref, since we get the inode ref, remove it directly, | |
3935 | * it is unnecessary to do delayed deletion. | |
3936 | * | |
3937 | * But if we have dir index, needn't search inode ref to get it. | |
3938 | * Since the inode ref is close to the inode item, it is better | |
3939 | * that we delay to delete it, and just do this deletion when | |
3940 | * we update the inode item. | |
3941 | */ | |
3942 | if (BTRFS_I(inode)->dir_index) { | |
3943 | ret = btrfs_delayed_delete_inode_ref(inode); | |
3944 | if (!ret) { | |
3945 | index = BTRFS_I(inode)->dir_index; | |
3946 | goto skip_backref; | |
3947 | } | |
3948 | } | |
3949 | ||
33345d01 LZ |
3950 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
3951 | dir_ino, &index); | |
aec7477b | 3952 | if (ret) { |
c2cf52eb SK |
3953 | btrfs_info(root->fs_info, |
3954 | "failed to delete reference to %.*s, inode %llu parent %llu", | |
c1c9ff7c | 3955 | name_len, name, ino, dir_ino); |
79787eaa | 3956 | btrfs_abort_transaction(trans, root, ret); |
aec7477b JB |
3957 | goto err; |
3958 | } | |
67de1176 | 3959 | skip_backref: |
16cdcec7 | 3960 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
3961 | if (ret) { |
3962 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 3963 | goto err; |
79787eaa | 3964 | } |
39279cc3 | 3965 | |
e02119d5 | 3966 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
33345d01 | 3967 | inode, dir_ino); |
79787eaa JM |
3968 | if (ret != 0 && ret != -ENOENT) { |
3969 | btrfs_abort_transaction(trans, root, ret); | |
3970 | goto err; | |
3971 | } | |
e02119d5 CM |
3972 | |
3973 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | |
3974 | dir, index); | |
6418c961 CM |
3975 | if (ret == -ENOENT) |
3976 | ret = 0; | |
d4e3991b ZB |
3977 | else if (ret) |
3978 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 CM |
3979 | err: |
3980 | btrfs_free_path(path); | |
e02119d5 CM |
3981 | if (ret) |
3982 | goto out; | |
3983 | ||
3984 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 JB |
3985 | inode_inc_iversion(inode); |
3986 | inode_inc_iversion(dir); | |
e02119d5 | 3987 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
b9959295 | 3988 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 3989 | out: |
39279cc3 CM |
3990 | return ret; |
3991 | } | |
3992 | ||
92986796 AV |
3993 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3994 | struct btrfs_root *root, | |
3995 | struct inode *dir, struct inode *inode, | |
3996 | const char *name, int name_len) | |
3997 | { | |
3998 | int ret; | |
3999 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4000 | if (!ret) { | |
8b558c5f | 4001 | drop_nlink(inode); |
92986796 AV |
4002 | ret = btrfs_update_inode(trans, root, inode); |
4003 | } | |
4004 | return ret; | |
4005 | } | |
39279cc3 | 4006 | |
a22285a6 YZ |
4007 | /* |
4008 | * helper to start transaction for unlink and rmdir. | |
4009 | * | |
d52be818 JB |
4010 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4011 | * if we cannot make our reservations the normal way try and see if there is | |
4012 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4013 | * allow the unlink to occur. | |
a22285a6 | 4014 | */ |
d52be818 | 4015 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4016 | { |
39279cc3 | 4017 | struct btrfs_trans_handle *trans; |
a22285a6 | 4018 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d YZ |
4019 | int ret; |
4020 | ||
e70bea5f JB |
4021 | /* |
4022 | * 1 for the possible orphan item | |
4023 | * 1 for the dir item | |
4024 | * 1 for the dir index | |
4025 | * 1 for the inode ref | |
e70bea5f JB |
4026 | * 1 for the inode |
4027 | */ | |
6e137ed3 | 4028 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
4029 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
4030 | return trans; | |
4df27c4d | 4031 | |
d52be818 JB |
4032 | if (PTR_ERR(trans) == -ENOSPC) { |
4033 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); | |
4df27c4d | 4034 | |
d52be818 JB |
4035 | trans = btrfs_start_transaction(root, 0); |
4036 | if (IS_ERR(trans)) | |
4037 | return trans; | |
4038 | ret = btrfs_cond_migrate_bytes(root->fs_info, | |
4039 | &root->fs_info->trans_block_rsv, | |
4040 | num_bytes, 5); | |
4041 | if (ret) { | |
4042 | btrfs_end_transaction(trans, root); | |
4043 | return ERR_PTR(ret); | |
a22285a6 | 4044 | } |
5a77d76c | 4045 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
d52be818 | 4046 | trans->bytes_reserved = num_bytes; |
a22285a6 | 4047 | } |
d52be818 | 4048 | return trans; |
a22285a6 YZ |
4049 | } |
4050 | ||
4051 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4052 | { | |
4053 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4054 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4055 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4056 | int ret; |
a22285a6 | 4057 | |
d52be818 | 4058 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4059 | if (IS_ERR(trans)) |
4060 | return PTR_ERR(trans); | |
5f39d397 | 4061 | |
2b0143b5 | 4062 | btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0); |
12fcfd22 | 4063 | |
2b0143b5 | 4064 | ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4065 | dentry->d_name.name, dentry->d_name.len); |
b532402e TI |
4066 | if (ret) |
4067 | goto out; | |
7b128766 | 4068 | |
a22285a6 | 4069 | if (inode->i_nlink == 0) { |
7b128766 | 4070 | ret = btrfs_orphan_add(trans, inode); |
b532402e TI |
4071 | if (ret) |
4072 | goto out; | |
a22285a6 | 4073 | } |
7b128766 | 4074 | |
b532402e | 4075 | out: |
d52be818 | 4076 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4077 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
4078 | return ret; |
4079 | } | |
4080 | ||
4df27c4d YZ |
4081 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4082 | struct btrfs_root *root, | |
4083 | struct inode *dir, u64 objectid, | |
4084 | const char *name, int name_len) | |
4085 | { | |
4086 | struct btrfs_path *path; | |
4087 | struct extent_buffer *leaf; | |
4088 | struct btrfs_dir_item *di; | |
4089 | struct btrfs_key key; | |
4090 | u64 index; | |
4091 | int ret; | |
33345d01 | 4092 | u64 dir_ino = btrfs_ino(dir); |
4df27c4d YZ |
4093 | |
4094 | path = btrfs_alloc_path(); | |
4095 | if (!path) | |
4096 | return -ENOMEM; | |
4097 | ||
33345d01 | 4098 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4099 | name, name_len, -1); |
79787eaa JM |
4100 | if (IS_ERR_OR_NULL(di)) { |
4101 | if (!di) | |
4102 | ret = -ENOENT; | |
4103 | else | |
4104 | ret = PTR_ERR(di); | |
4105 | goto out; | |
4106 | } | |
4df27c4d YZ |
4107 | |
4108 | leaf = path->nodes[0]; | |
4109 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4110 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4111 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa JM |
4112 | if (ret) { |
4113 | btrfs_abort_transaction(trans, root, ret); | |
4114 | goto out; | |
4115 | } | |
b3b4aa74 | 4116 | btrfs_release_path(path); |
4df27c4d YZ |
4117 | |
4118 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
4119 | objectid, root->root_key.objectid, | |
33345d01 | 4120 | dir_ino, &index, name, name_len); |
4df27c4d | 4121 | if (ret < 0) { |
79787eaa JM |
4122 | if (ret != -ENOENT) { |
4123 | btrfs_abort_transaction(trans, root, ret); | |
4124 | goto out; | |
4125 | } | |
33345d01 | 4126 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4127 | name, name_len); |
79787eaa JM |
4128 | if (IS_ERR_OR_NULL(di)) { |
4129 | if (!di) | |
4130 | ret = -ENOENT; | |
4131 | else | |
4132 | ret = PTR_ERR(di); | |
4133 | btrfs_abort_transaction(trans, root, ret); | |
4134 | goto out; | |
4135 | } | |
4df27c4d YZ |
4136 | |
4137 | leaf = path->nodes[0]; | |
4138 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4139 | btrfs_release_path(path); |
4df27c4d YZ |
4140 | index = key.offset; |
4141 | } | |
945d8962 | 4142 | btrfs_release_path(path); |
4df27c4d | 4143 | |
16cdcec7 | 4144 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
4145 | if (ret) { |
4146 | btrfs_abort_transaction(trans, root, ret); | |
4147 | goto out; | |
4148 | } | |
4df27c4d YZ |
4149 | |
4150 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 | 4151 | inode_inc_iversion(dir); |
4df27c4d | 4152 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
5a24e84c | 4153 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa JM |
4154 | if (ret) |
4155 | btrfs_abort_transaction(trans, root, ret); | |
4156 | out: | |
71d7aed0 | 4157 | btrfs_free_path(path); |
79787eaa | 4158 | return ret; |
4df27c4d YZ |
4159 | } |
4160 | ||
39279cc3 CM |
4161 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4162 | { | |
2b0143b5 | 4163 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4164 | int err = 0; |
39279cc3 | 4165 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4166 | struct btrfs_trans_handle *trans; |
39279cc3 | 4167 | |
b3ae244e | 4168 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4169 | return -ENOTEMPTY; |
b3ae244e DS |
4170 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
4171 | return -EPERM; | |
134d4512 | 4172 | |
d52be818 | 4173 | trans = __unlink_start_trans(dir); |
a22285a6 | 4174 | if (IS_ERR(trans)) |
5df6a9f6 | 4175 | return PTR_ERR(trans); |
5df6a9f6 | 4176 | |
33345d01 | 4177 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4178 | err = btrfs_unlink_subvol(trans, root, dir, |
4179 | BTRFS_I(inode)->location.objectid, | |
4180 | dentry->d_name.name, | |
4181 | dentry->d_name.len); | |
4182 | goto out; | |
4183 | } | |
4184 | ||
7b128766 JB |
4185 | err = btrfs_orphan_add(trans, inode); |
4186 | if (err) | |
4df27c4d | 4187 | goto out; |
7b128766 | 4188 | |
39279cc3 | 4189 | /* now the directory is empty */ |
2b0143b5 | 4190 | err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4191 | dentry->d_name.name, dentry->d_name.len); |
d397712b | 4192 | if (!err) |
dbe674a9 | 4193 | btrfs_i_size_write(inode, 0); |
4df27c4d | 4194 | out: |
d52be818 | 4195 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4196 | btrfs_btree_balance_dirty(root); |
3954401f | 4197 | |
39279cc3 CM |
4198 | return err; |
4199 | } | |
4200 | ||
28f75a0e CM |
4201 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4202 | struct btrfs_root *root, | |
4203 | u64 bytes_deleted) | |
4204 | { | |
4205 | int ret; | |
4206 | ||
4207 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); | |
4208 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, | |
4209 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); | |
4210 | if (!ret) | |
4211 | trans->bytes_reserved += bytes_deleted; | |
4212 | return ret; | |
4213 | ||
4214 | } | |
4215 | ||
39279cc3 CM |
4216 | /* |
4217 | * this can truncate away extent items, csum items and directory items. | |
4218 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4219 | * any higher than new_size |
39279cc3 CM |
4220 | * |
4221 | * csum items that cross the new i_size are truncated to the new size | |
4222 | * as well. | |
7b128766 JB |
4223 | * |
4224 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4225 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4226 | */ |
8082510e YZ |
4227 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4228 | struct btrfs_root *root, | |
4229 | struct inode *inode, | |
4230 | u64 new_size, u32 min_type) | |
39279cc3 | 4231 | { |
39279cc3 | 4232 | struct btrfs_path *path; |
5f39d397 | 4233 | struct extent_buffer *leaf; |
39279cc3 | 4234 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4235 | struct btrfs_key key; |
4236 | struct btrfs_key found_key; | |
39279cc3 | 4237 | u64 extent_start = 0; |
db94535d | 4238 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4239 | u64 extent_offset = 0; |
39279cc3 | 4240 | u64 item_end = 0; |
c1aa4575 | 4241 | u64 last_size = new_size; |
8082510e | 4242 | u32 found_type = (u8)-1; |
39279cc3 CM |
4243 | int found_extent; |
4244 | int del_item; | |
85e21bac CM |
4245 | int pending_del_nr = 0; |
4246 | int pending_del_slot = 0; | |
179e29e4 | 4247 | int extent_type = -1; |
8082510e YZ |
4248 | int ret; |
4249 | int err = 0; | |
33345d01 | 4250 | u64 ino = btrfs_ino(inode); |
28ed1345 | 4251 | u64 bytes_deleted = 0; |
1262133b JB |
4252 | bool be_nice = 0; |
4253 | bool should_throttle = 0; | |
28f75a0e | 4254 | bool should_end = 0; |
8082510e YZ |
4255 | |
4256 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4257 | |
28ed1345 CM |
4258 | /* |
4259 | * for non-free space inodes and ref cows, we want to back off from | |
4260 | * time to time | |
4261 | */ | |
4262 | if (!btrfs_is_free_space_inode(inode) && | |
4263 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4264 | be_nice = 1; | |
4265 | ||
0eb0e19c MF |
4266 | path = btrfs_alloc_path(); |
4267 | if (!path) | |
4268 | return -ENOMEM; | |
4269 | path->reada = -1; | |
4270 | ||
5dc562c5 JB |
4271 | /* |
4272 | * We want to drop from the next block forward in case this new size is | |
4273 | * not block aligned since we will be keeping the last block of the | |
4274 | * extent just the way it is. | |
4275 | */ | |
27cdeb70 MX |
4276 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
4277 | root == root->fs_info->tree_root) | |
fda2832f QW |
4278 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
4279 | root->sectorsize), (u64)-1, 0); | |
8082510e | 4280 | |
16cdcec7 MX |
4281 | /* |
4282 | * This function is also used to drop the items in the log tree before | |
4283 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4284 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4285 | * items. | |
4286 | */ | |
4287 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4288 | btrfs_kill_delayed_inode_items(inode); | |
4289 | ||
33345d01 | 4290 | key.objectid = ino; |
39279cc3 | 4291 | key.offset = (u64)-1; |
5f39d397 CM |
4292 | key.type = (u8)-1; |
4293 | ||
85e21bac | 4294 | search_again: |
28ed1345 CM |
4295 | /* |
4296 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4297 | * up a huge file in a single leaf. Most of the time that | |
4298 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4299 | */ | |
4300 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { | |
4301 | if (btrfs_should_end_transaction(trans, root)) { | |
4302 | err = -EAGAIN; | |
4303 | goto error; | |
4304 | } | |
4305 | } | |
4306 | ||
4307 | ||
b9473439 | 4308 | path->leave_spinning = 1; |
85e21bac | 4309 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4310 | if (ret < 0) { |
4311 | err = ret; | |
4312 | goto out; | |
4313 | } | |
d397712b | 4314 | |
85e21bac | 4315 | if (ret > 0) { |
e02119d5 CM |
4316 | /* there are no items in the tree for us to truncate, we're |
4317 | * done | |
4318 | */ | |
8082510e YZ |
4319 | if (path->slots[0] == 0) |
4320 | goto out; | |
85e21bac CM |
4321 | path->slots[0]--; |
4322 | } | |
4323 | ||
d397712b | 4324 | while (1) { |
39279cc3 | 4325 | fi = NULL; |
5f39d397 CM |
4326 | leaf = path->nodes[0]; |
4327 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4328 | found_type = found_key.type; |
39279cc3 | 4329 | |
33345d01 | 4330 | if (found_key.objectid != ino) |
39279cc3 | 4331 | break; |
5f39d397 | 4332 | |
85e21bac | 4333 | if (found_type < min_type) |
39279cc3 CM |
4334 | break; |
4335 | ||
5f39d397 | 4336 | item_end = found_key.offset; |
39279cc3 | 4337 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4338 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4339 | struct btrfs_file_extent_item); |
179e29e4 CM |
4340 | extent_type = btrfs_file_extent_type(leaf, fi); |
4341 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4342 | item_end += |
db94535d | 4343 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 | 4344 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4345 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4346 | path->slots[0], fi); |
39279cc3 | 4347 | } |
008630c1 | 4348 | item_end--; |
39279cc3 | 4349 | } |
8082510e YZ |
4350 | if (found_type > min_type) { |
4351 | del_item = 1; | |
4352 | } else { | |
4353 | if (item_end < new_size) | |
b888db2b | 4354 | break; |
8082510e YZ |
4355 | if (found_key.offset >= new_size) |
4356 | del_item = 1; | |
4357 | else | |
4358 | del_item = 0; | |
39279cc3 | 4359 | } |
39279cc3 | 4360 | found_extent = 0; |
39279cc3 | 4361 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4362 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4363 | goto delete; | |
4364 | ||
7f4f6e0a JB |
4365 | if (del_item) |
4366 | last_size = found_key.offset; | |
4367 | else | |
4368 | last_size = new_size; | |
4369 | ||
179e29e4 | 4370 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4371 | u64 num_dec; |
db94535d | 4372 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4373 | if (!del_item) { |
db94535d CM |
4374 | u64 orig_num_bytes = |
4375 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4376 | extent_num_bytes = ALIGN(new_size - |
4377 | found_key.offset, | |
4378 | root->sectorsize); | |
db94535d CM |
4379 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4380 | extent_num_bytes); | |
4381 | num_dec = (orig_num_bytes - | |
9069218d | 4382 | extent_num_bytes); |
27cdeb70 MX |
4383 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4384 | &root->state) && | |
4385 | extent_start != 0) | |
a76a3cd4 | 4386 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4387 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4388 | } else { |
db94535d CM |
4389 | extent_num_bytes = |
4390 | btrfs_file_extent_disk_num_bytes(leaf, | |
4391 | fi); | |
5d4f98a2 YZ |
4392 | extent_offset = found_key.offset - |
4393 | btrfs_file_extent_offset(leaf, fi); | |
4394 | ||
39279cc3 | 4395 | /* FIXME blocksize != 4096 */ |
9069218d | 4396 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4397 | if (extent_start != 0) { |
4398 | found_extent = 1; | |
27cdeb70 MX |
4399 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4400 | &root->state)) | |
a76a3cd4 | 4401 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4402 | } |
39279cc3 | 4403 | } |
9069218d | 4404 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4405 | /* |
4406 | * we can't truncate inline items that have had | |
4407 | * special encodings | |
4408 | */ | |
4409 | if (!del_item && | |
4410 | btrfs_file_extent_compression(leaf, fi) == 0 && | |
4411 | btrfs_file_extent_encryption(leaf, fi) == 0 && | |
4412 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
e02119d5 CM |
4413 | u32 size = new_size - found_key.offset; |
4414 | ||
27cdeb70 | 4415 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
a76a3cd4 YZ |
4416 | inode_sub_bytes(inode, item_end + 1 - |
4417 | new_size); | |
514ac8ad CM |
4418 | |
4419 | /* | |
4420 | * update the ram bytes to properly reflect | |
4421 | * the new size of our item | |
4422 | */ | |
4423 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
e02119d5 CM |
4424 | size = |
4425 | btrfs_file_extent_calc_inline_size(size); | |
afe5fea7 | 4426 | btrfs_truncate_item(root, path, size, 1); |
27cdeb70 MX |
4427 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4428 | &root->state)) { | |
a76a3cd4 YZ |
4429 | inode_sub_bytes(inode, item_end + 1 - |
4430 | found_key.offset); | |
9069218d | 4431 | } |
39279cc3 | 4432 | } |
179e29e4 | 4433 | delete: |
39279cc3 | 4434 | if (del_item) { |
85e21bac CM |
4435 | if (!pending_del_nr) { |
4436 | /* no pending yet, add ourselves */ | |
4437 | pending_del_slot = path->slots[0]; | |
4438 | pending_del_nr = 1; | |
4439 | } else if (pending_del_nr && | |
4440 | path->slots[0] + 1 == pending_del_slot) { | |
4441 | /* hop on the pending chunk */ | |
4442 | pending_del_nr++; | |
4443 | pending_del_slot = path->slots[0]; | |
4444 | } else { | |
d397712b | 4445 | BUG(); |
85e21bac | 4446 | } |
39279cc3 CM |
4447 | } else { |
4448 | break; | |
4449 | } | |
28f75a0e CM |
4450 | should_throttle = 0; |
4451 | ||
27cdeb70 MX |
4452 | if (found_extent && |
4453 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
4454 | root == root->fs_info->tree_root)) { | |
b9473439 | 4455 | btrfs_set_path_blocking(path); |
28ed1345 | 4456 | bytes_deleted += extent_num_bytes; |
39279cc3 | 4457 | ret = btrfs_free_extent(trans, root, extent_start, |
5d4f98a2 YZ |
4458 | extent_num_bytes, 0, |
4459 | btrfs_header_owner(leaf), | |
66d7e7f0 | 4460 | ino, extent_offset, 0); |
39279cc3 | 4461 | BUG_ON(ret); |
1262133b | 4462 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
28ed1345 CM |
4463 | btrfs_async_run_delayed_refs(root, |
4464 | trans->delayed_ref_updates * 2, 0); | |
28f75a0e CM |
4465 | if (be_nice) { |
4466 | if (truncate_space_check(trans, root, | |
4467 | extent_num_bytes)) { | |
4468 | should_end = 1; | |
4469 | } | |
4470 | if (btrfs_should_throttle_delayed_refs(trans, | |
4471 | root)) { | |
4472 | should_throttle = 1; | |
4473 | } | |
4474 | } | |
39279cc3 | 4475 | } |
85e21bac | 4476 | |
8082510e YZ |
4477 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4478 | break; | |
4479 | ||
4480 | if (path->slots[0] == 0 || | |
1262133b | 4481 | path->slots[0] != pending_del_slot || |
28f75a0e | 4482 | should_throttle || should_end) { |
8082510e YZ |
4483 | if (pending_del_nr) { |
4484 | ret = btrfs_del_items(trans, root, path, | |
4485 | pending_del_slot, | |
4486 | pending_del_nr); | |
79787eaa JM |
4487 | if (ret) { |
4488 | btrfs_abort_transaction(trans, | |
4489 | root, ret); | |
4490 | goto error; | |
4491 | } | |
8082510e YZ |
4492 | pending_del_nr = 0; |
4493 | } | |
b3b4aa74 | 4494 | btrfs_release_path(path); |
28f75a0e | 4495 | if (should_throttle) { |
1262133b JB |
4496 | unsigned long updates = trans->delayed_ref_updates; |
4497 | if (updates) { | |
4498 | trans->delayed_ref_updates = 0; | |
4499 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4500 | if (ret && !err) | |
4501 | err = ret; | |
4502 | } | |
4503 | } | |
28f75a0e CM |
4504 | /* |
4505 | * if we failed to refill our space rsv, bail out | |
4506 | * and let the transaction restart | |
4507 | */ | |
4508 | if (should_end) { | |
4509 | err = -EAGAIN; | |
4510 | goto error; | |
4511 | } | |
85e21bac | 4512 | goto search_again; |
8082510e YZ |
4513 | } else { |
4514 | path->slots[0]--; | |
85e21bac | 4515 | } |
39279cc3 | 4516 | } |
8082510e | 4517 | out: |
85e21bac CM |
4518 | if (pending_del_nr) { |
4519 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4520 | pending_del_nr); | |
79787eaa JM |
4521 | if (ret) |
4522 | btrfs_abort_transaction(trans, root, ret); | |
85e21bac | 4523 | } |
79787eaa | 4524 | error: |
c1aa4575 | 4525 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
7f4f6e0a | 4526 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
28ed1345 | 4527 | |
39279cc3 | 4528 | btrfs_free_path(path); |
28ed1345 | 4529 | |
28f75a0e | 4530 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { |
28ed1345 CM |
4531 | unsigned long updates = trans->delayed_ref_updates; |
4532 | if (updates) { | |
4533 | trans->delayed_ref_updates = 0; | |
4534 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4535 | if (ret && !err) | |
4536 | err = ret; | |
4537 | } | |
4538 | } | |
8082510e | 4539 | return err; |
39279cc3 CM |
4540 | } |
4541 | ||
4542 | /* | |
2aaa6655 JB |
4543 | * btrfs_truncate_page - read, zero a chunk and write a page |
4544 | * @inode - inode that we're zeroing | |
4545 | * @from - the offset to start zeroing | |
4546 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4547 | * offset | |
4548 | * @front - zero up to the offset instead of from the offset on | |
4549 | * | |
4550 | * This will find the page for the "from" offset and cow the page and zero the | |
4551 | * part we want to zero. This is used with truncate and hole punching. | |
39279cc3 | 4552 | */ |
2aaa6655 JB |
4553 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
4554 | int front) | |
39279cc3 | 4555 | { |
2aaa6655 | 4556 | struct address_space *mapping = inode->i_mapping; |
db94535d | 4557 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
4558 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4559 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4560 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 4561 | char *kaddr; |
db94535d | 4562 | u32 blocksize = root->sectorsize; |
39279cc3 CM |
4563 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
4564 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
4565 | struct page *page; | |
3b16a4e3 | 4566 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4567 | int ret = 0; |
a52d9a80 | 4568 | u64 page_start; |
e6dcd2dc | 4569 | u64 page_end; |
39279cc3 | 4570 | |
2aaa6655 JB |
4571 | if ((offset & (blocksize - 1)) == 0 && |
4572 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4573 | goto out; |
0ca1f7ce | 4574 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
5d5e103a JB |
4575 | if (ret) |
4576 | goto out; | |
39279cc3 | 4577 | |
211c17f5 | 4578 | again: |
3b16a4e3 | 4579 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4580 | if (!page) { |
0ca1f7ce | 4581 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
ac6a2b36 | 4582 | ret = -ENOMEM; |
39279cc3 | 4583 | goto out; |
5d5e103a | 4584 | } |
e6dcd2dc CM |
4585 | |
4586 | page_start = page_offset(page); | |
4587 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
4588 | ||
39279cc3 | 4589 | if (!PageUptodate(page)) { |
9ebefb18 | 4590 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4591 | lock_page(page); |
211c17f5 CM |
4592 | if (page->mapping != mapping) { |
4593 | unlock_page(page); | |
4594 | page_cache_release(page); | |
4595 | goto again; | |
4596 | } | |
39279cc3 CM |
4597 | if (!PageUptodate(page)) { |
4598 | ret = -EIO; | |
89642229 | 4599 | goto out_unlock; |
39279cc3 CM |
4600 | } |
4601 | } | |
211c17f5 | 4602 | wait_on_page_writeback(page); |
e6dcd2dc | 4603 | |
d0082371 | 4604 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
4605 | set_page_extent_mapped(page); |
4606 | ||
4607 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
4608 | if (ordered) { | |
2ac55d41 JB |
4609 | unlock_extent_cached(io_tree, page_start, page_end, |
4610 | &cached_state, GFP_NOFS); | |
e6dcd2dc CM |
4611 | unlock_page(page); |
4612 | page_cache_release(page); | |
eb84ae03 | 4613 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4614 | btrfs_put_ordered_extent(ordered); |
4615 | goto again; | |
4616 | } | |
4617 | ||
2ac55d41 | 4618 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
4619 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4620 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4621 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4622 | |
2ac55d41 JB |
4623 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
4624 | &cached_state); | |
9ed74f2d | 4625 | if (ret) { |
2ac55d41 JB |
4626 | unlock_extent_cached(io_tree, page_start, page_end, |
4627 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
4628 | goto out_unlock; |
4629 | } | |
4630 | ||
e6dcd2dc | 4631 | if (offset != PAGE_CACHE_SIZE) { |
2aaa6655 JB |
4632 | if (!len) |
4633 | len = PAGE_CACHE_SIZE - offset; | |
e6dcd2dc | 4634 | kaddr = kmap(page); |
2aaa6655 JB |
4635 | if (front) |
4636 | memset(kaddr, 0, offset); | |
4637 | else | |
4638 | memset(kaddr + offset, 0, len); | |
e6dcd2dc CM |
4639 | flush_dcache_page(page); |
4640 | kunmap(page); | |
4641 | } | |
247e743c | 4642 | ClearPageChecked(page); |
e6dcd2dc | 4643 | set_page_dirty(page); |
2ac55d41 JB |
4644 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
4645 | GFP_NOFS); | |
39279cc3 | 4646 | |
89642229 | 4647 | out_unlock: |
5d5e103a | 4648 | if (ret) |
0ca1f7ce | 4649 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
39279cc3 CM |
4650 | unlock_page(page); |
4651 | page_cache_release(page); | |
4652 | out: | |
4653 | return ret; | |
4654 | } | |
4655 | ||
16e7549f JB |
4656 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4657 | u64 offset, u64 len) | |
4658 | { | |
4659 | struct btrfs_trans_handle *trans; | |
4660 | int ret; | |
4661 | ||
4662 | /* | |
4663 | * Still need to make sure the inode looks like it's been updated so | |
4664 | * that any holes get logged if we fsync. | |
4665 | */ | |
4666 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { | |
4667 | BTRFS_I(inode)->last_trans = root->fs_info->generation; | |
4668 | BTRFS_I(inode)->last_sub_trans = root->log_transid; | |
4669 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4670 | return 0; | |
4671 | } | |
4672 | ||
4673 | /* | |
4674 | * 1 - for the one we're dropping | |
4675 | * 1 - for the one we're adding | |
4676 | * 1 - for updating the inode. | |
4677 | */ | |
4678 | trans = btrfs_start_transaction(root, 3); | |
4679 | if (IS_ERR(trans)) | |
4680 | return PTR_ERR(trans); | |
4681 | ||
4682 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4683 | if (ret) { | |
4684 | btrfs_abort_transaction(trans, root, ret); | |
4685 | btrfs_end_transaction(trans, root); | |
4686 | return ret; | |
4687 | } | |
4688 | ||
4689 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, | |
4690 | 0, 0, len, 0, len, 0, 0, 0); | |
4691 | if (ret) | |
4692 | btrfs_abort_transaction(trans, root, ret); | |
4693 | else | |
4694 | btrfs_update_inode(trans, root, inode); | |
4695 | btrfs_end_transaction(trans, root); | |
4696 | return ret; | |
4697 | } | |
4698 | ||
695a0d0d JB |
4699 | /* |
4700 | * This function puts in dummy file extents for the area we're creating a hole | |
4701 | * for. So if we are truncating this file to a larger size we need to insert | |
4702 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4703 | * the range between oldsize and size | |
4704 | */ | |
a41ad394 | 4705 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4706 | { |
9036c102 YZ |
4707 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4708 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4709 | struct extent_map *em = NULL; |
2ac55d41 | 4710 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4711 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
fda2832f QW |
4712 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
4713 | u64 block_end = ALIGN(size, root->sectorsize); | |
9036c102 YZ |
4714 | u64 last_byte; |
4715 | u64 cur_offset; | |
4716 | u64 hole_size; | |
9ed74f2d | 4717 | int err = 0; |
39279cc3 | 4718 | |
a71754fc JB |
4719 | /* |
4720 | * If our size started in the middle of a page we need to zero out the | |
4721 | * rest of the page before we expand the i_size, otherwise we could | |
4722 | * expose stale data. | |
4723 | */ | |
4724 | err = btrfs_truncate_page(inode, oldsize, 0, 0); | |
4725 | if (err) | |
4726 | return err; | |
4727 | ||
9036c102 YZ |
4728 | if (size <= hole_start) |
4729 | return 0; | |
4730 | ||
9036c102 YZ |
4731 | while (1) { |
4732 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4733 | |
2ac55d41 | 4734 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
d0082371 | 4735 | &cached_state); |
fa7c1494 MX |
4736 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
4737 | block_end - hole_start); | |
9036c102 YZ |
4738 | if (!ordered) |
4739 | break; | |
2ac55d41 JB |
4740 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4741 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4742 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4743 | btrfs_put_ordered_extent(ordered); |
4744 | } | |
39279cc3 | 4745 | |
9036c102 YZ |
4746 | cur_offset = hole_start; |
4747 | while (1) { | |
4748 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
4749 | block_end - cur_offset, 0); | |
79787eaa JM |
4750 | if (IS_ERR(em)) { |
4751 | err = PTR_ERR(em); | |
f2767956 | 4752 | em = NULL; |
79787eaa JM |
4753 | break; |
4754 | } | |
9036c102 | 4755 | last_byte = min(extent_map_end(em), block_end); |
fda2832f | 4756 | last_byte = ALIGN(last_byte , root->sectorsize); |
8082510e | 4757 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4758 | struct extent_map *hole_em; |
9036c102 | 4759 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4760 | |
16e7549f JB |
4761 | err = maybe_insert_hole(root, inode, cur_offset, |
4762 | hole_size); | |
4763 | if (err) | |
3893e33b | 4764 | break; |
5dc562c5 JB |
4765 | btrfs_drop_extent_cache(inode, cur_offset, |
4766 | cur_offset + hole_size - 1, 0); | |
4767 | hole_em = alloc_extent_map(); | |
4768 | if (!hole_em) { | |
4769 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4770 | &BTRFS_I(inode)->runtime_flags); | |
4771 | goto next; | |
4772 | } | |
4773 | hole_em->start = cur_offset; | |
4774 | hole_em->len = hole_size; | |
4775 | hole_em->orig_start = cur_offset; | |
8082510e | 4776 | |
5dc562c5 JB |
4777 | hole_em->block_start = EXTENT_MAP_HOLE; |
4778 | hole_em->block_len = 0; | |
b4939680 | 4779 | hole_em->orig_block_len = 0; |
cc95bef6 | 4780 | hole_em->ram_bytes = hole_size; |
5dc562c5 JB |
4781 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
4782 | hole_em->compress_type = BTRFS_COMPRESS_NONE; | |
16e7549f | 4783 | hole_em->generation = root->fs_info->generation; |
8082510e | 4784 | |
5dc562c5 JB |
4785 | while (1) { |
4786 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4787 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4788 | write_unlock(&em_tree->lock); |
4789 | if (err != -EEXIST) | |
4790 | break; | |
4791 | btrfs_drop_extent_cache(inode, cur_offset, | |
4792 | cur_offset + | |
4793 | hole_size - 1, 0); | |
4794 | } | |
4795 | free_extent_map(hole_em); | |
9036c102 | 4796 | } |
16e7549f | 4797 | next: |
9036c102 | 4798 | free_extent_map(em); |
a22285a6 | 4799 | em = NULL; |
9036c102 | 4800 | cur_offset = last_byte; |
8082510e | 4801 | if (cur_offset >= block_end) |
9036c102 YZ |
4802 | break; |
4803 | } | |
a22285a6 | 4804 | free_extent_map(em); |
2ac55d41 JB |
4805 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
4806 | GFP_NOFS); | |
9036c102 YZ |
4807 | return err; |
4808 | } | |
39279cc3 | 4809 | |
9ea24bbe FM |
4810 | static int wait_snapshoting_atomic_t(atomic_t *a) |
4811 | { | |
4812 | schedule(); | |
4813 | return 0; | |
4814 | } | |
4815 | ||
4816 | static void wait_for_snapshot_creation(struct btrfs_root *root) | |
4817 | { | |
4818 | while (true) { | |
4819 | int ret; | |
4820 | ||
4821 | ret = btrfs_start_write_no_snapshoting(root); | |
4822 | if (ret) | |
4823 | break; | |
4824 | wait_on_atomic_t(&root->will_be_snapshoted, | |
4825 | wait_snapshoting_atomic_t, | |
4826 | TASK_UNINTERRUPTIBLE); | |
4827 | } | |
4828 | } | |
4829 | ||
3972f260 | 4830 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4831 | { |
f4a2f4c5 MX |
4832 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4833 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4834 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4835 | loff_t newsize = attr->ia_size; |
4836 | int mask = attr->ia_valid; | |
8082510e YZ |
4837 | int ret; |
4838 | ||
3972f260 ES |
4839 | /* |
4840 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4841 | * special case where we need to update the times despite not having | |
4842 | * these flags set. For all other operations the VFS set these flags | |
4843 | * explicitly if it wants a timestamp update. | |
4844 | */ | |
dff6efc3 CH |
4845 | if (newsize != oldsize) { |
4846 | inode_inc_iversion(inode); | |
4847 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4848 | inode->i_ctime = inode->i_mtime = | |
4849 | current_fs_time(inode->i_sb); | |
4850 | } | |
3972f260 | 4851 | |
a41ad394 | 4852 | if (newsize > oldsize) { |
7caef267 | 4853 | truncate_pagecache(inode, newsize); |
9ea24bbe FM |
4854 | /* |
4855 | * Don't do an expanding truncate while snapshoting is ongoing. | |
4856 | * This is to ensure the snapshot captures a fully consistent | |
4857 | * state of this file - if the snapshot captures this expanding | |
4858 | * truncation, it must capture all writes that happened before | |
4859 | * this truncation. | |
4860 | */ | |
4861 | wait_for_snapshot_creation(root); | |
a41ad394 | 4862 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe FM |
4863 | if (ret) { |
4864 | btrfs_end_write_no_snapshoting(root); | |
8082510e | 4865 | return ret; |
9ea24bbe | 4866 | } |
8082510e | 4867 | |
f4a2f4c5 | 4868 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe FM |
4869 | if (IS_ERR(trans)) { |
4870 | btrfs_end_write_no_snapshoting(root); | |
f4a2f4c5 | 4871 | return PTR_ERR(trans); |
9ea24bbe | 4872 | } |
f4a2f4c5 MX |
4873 | |
4874 | i_size_write(inode, newsize); | |
4875 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
4876 | ret = btrfs_update_inode(trans, root, inode); | |
9ea24bbe | 4877 | btrfs_end_write_no_snapshoting(root); |
7ad85bb7 | 4878 | btrfs_end_transaction(trans, root); |
a41ad394 | 4879 | } else { |
8082510e | 4880 | |
a41ad394 JB |
4881 | /* |
4882 | * We're truncating a file that used to have good data down to | |
4883 | * zero. Make sure it gets into the ordered flush list so that | |
4884 | * any new writes get down to disk quickly. | |
4885 | */ | |
4886 | if (newsize == 0) | |
72ac3c0d JB |
4887 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
4888 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 4889 | |
f3fe820c JB |
4890 | /* |
4891 | * 1 for the orphan item we're going to add | |
4892 | * 1 for the orphan item deletion. | |
4893 | */ | |
4894 | trans = btrfs_start_transaction(root, 2); | |
4895 | if (IS_ERR(trans)) | |
4896 | return PTR_ERR(trans); | |
4897 | ||
4898 | /* | |
4899 | * We need to do this in case we fail at _any_ point during the | |
4900 | * actual truncate. Once we do the truncate_setsize we could | |
4901 | * invalidate pages which forces any outstanding ordered io to | |
4902 | * be instantly completed which will give us extents that need | |
4903 | * to be truncated. If we fail to get an orphan inode down we | |
4904 | * could have left over extents that were never meant to live, | |
4905 | * so we need to garuntee from this point on that everything | |
4906 | * will be consistent. | |
4907 | */ | |
4908 | ret = btrfs_orphan_add(trans, inode); | |
4909 | btrfs_end_transaction(trans, root); | |
4910 | if (ret) | |
4911 | return ret; | |
4912 | ||
a41ad394 JB |
4913 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
4914 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
4915 | |
4916 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
4917 | btrfs_inode_block_unlocked_dio(inode); | |
4918 | inode_dio_wait(inode); | |
4919 | btrfs_inode_resume_unlocked_dio(inode); | |
4920 | ||
a41ad394 | 4921 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
4922 | if (ret && inode->i_nlink) { |
4923 | int err; | |
4924 | ||
4925 | /* | |
4926 | * failed to truncate, disk_i_size is only adjusted down | |
4927 | * as we remove extents, so it should represent the true | |
4928 | * size of the inode, so reset the in memory size and | |
4929 | * delete our orphan entry. | |
4930 | */ | |
4931 | trans = btrfs_join_transaction(root); | |
4932 | if (IS_ERR(trans)) { | |
4933 | btrfs_orphan_del(NULL, inode); | |
4934 | return ret; | |
4935 | } | |
4936 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
4937 | err = btrfs_orphan_del(trans, inode); | |
4938 | if (err) | |
4939 | btrfs_abort_transaction(trans, root, err); | |
4940 | btrfs_end_transaction(trans, root); | |
4941 | } | |
8082510e YZ |
4942 | } |
4943 | ||
a41ad394 | 4944 | return ret; |
8082510e YZ |
4945 | } |
4946 | ||
9036c102 YZ |
4947 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
4948 | { | |
2b0143b5 | 4949 | struct inode *inode = d_inode(dentry); |
b83cc969 | 4950 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 4951 | int err; |
39279cc3 | 4952 | |
b83cc969 LZ |
4953 | if (btrfs_root_readonly(root)) |
4954 | return -EROFS; | |
4955 | ||
9036c102 YZ |
4956 | err = inode_change_ok(inode, attr); |
4957 | if (err) | |
4958 | return err; | |
2bf5a725 | 4959 | |
5a3f23d5 | 4960 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 4961 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
4962 | if (err) |
4963 | return err; | |
39279cc3 | 4964 | } |
9036c102 | 4965 | |
1025774c CH |
4966 | if (attr->ia_valid) { |
4967 | setattr_copy(inode, attr); | |
0c4d2d95 | 4968 | inode_inc_iversion(inode); |
22c44fe6 | 4969 | err = btrfs_dirty_inode(inode); |
1025774c | 4970 | |
22c44fe6 | 4971 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 4972 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 4973 | } |
33268eaf | 4974 | |
39279cc3 CM |
4975 | return err; |
4976 | } | |
61295eb8 | 4977 | |
131e404a FDBM |
4978 | /* |
4979 | * While truncating the inode pages during eviction, we get the VFS calling | |
4980 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
4981 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
4982 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
4983 | * extent_state structures over and over, wasting lots of time. | |
4984 | * | |
4985 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
4986 | * those expensive operations on a per page basis and do only the ordered io | |
4987 | * finishing, while we release here the extent_map and extent_state structures, | |
4988 | * without the excessive merging and splitting. | |
4989 | */ | |
4990 | static void evict_inode_truncate_pages(struct inode *inode) | |
4991 | { | |
4992 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
4993 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
4994 | struct rb_node *node; | |
4995 | ||
4996 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 4997 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
4998 | |
4999 | write_lock(&map_tree->lock); | |
5000 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5001 | struct extent_map *em; | |
5002 | ||
5003 | node = rb_first(&map_tree->map); | |
5004 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5005 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5006 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5007 | remove_extent_mapping(map_tree, em); |
5008 | free_extent_map(em); | |
7064dd5c FM |
5009 | if (need_resched()) { |
5010 | write_unlock(&map_tree->lock); | |
5011 | cond_resched(); | |
5012 | write_lock(&map_tree->lock); | |
5013 | } | |
131e404a FDBM |
5014 | } |
5015 | write_unlock(&map_tree->lock); | |
5016 | ||
6ca07097 FM |
5017 | /* |
5018 | * Keep looping until we have no more ranges in the io tree. | |
5019 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5020 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5021 | * still in progress (unlocked the pages in the bio but did not yet | |
5022 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5023 | * ranges can still be locked and eviction started because before |
5024 | * submitting those bios, which are executed by a separate task (work | |
5025 | * queue kthread), inode references (inode->i_count) were not taken | |
5026 | * (which would be dropped in the end io callback of each bio). | |
5027 | * Therefore here we effectively end up waiting for those bios and | |
5028 | * anyone else holding locked ranges without having bumped the inode's | |
5029 | * reference count - if we don't do it, when they access the inode's | |
5030 | * io_tree to unlock a range it may be too late, leading to an | |
5031 | * use-after-free issue. | |
5032 | */ | |
131e404a FDBM |
5033 | spin_lock(&io_tree->lock); |
5034 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5035 | struct extent_state *state; | |
5036 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5037 | u64 start; |
5038 | u64 end; | |
131e404a FDBM |
5039 | |
5040 | node = rb_first(&io_tree->state); | |
5041 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5042 | start = state->start; |
5043 | end = state->end; | |
131e404a FDBM |
5044 | spin_unlock(&io_tree->lock); |
5045 | ||
6ca07097 FM |
5046 | lock_extent_bits(io_tree, start, end, 0, &cached_state); |
5047 | clear_extent_bit(io_tree, start, end, | |
131e404a FDBM |
5048 | EXTENT_LOCKED | EXTENT_DIRTY | |
5049 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5050 | EXTENT_DEFRAG, 1, 1, | |
5051 | &cached_state, GFP_NOFS); | |
131e404a | 5052 | |
7064dd5c | 5053 | cond_resched(); |
131e404a FDBM |
5054 | spin_lock(&io_tree->lock); |
5055 | } | |
5056 | spin_unlock(&io_tree->lock); | |
5057 | } | |
5058 | ||
bd555975 | 5059 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 CM |
5060 | { |
5061 | struct btrfs_trans_handle *trans; | |
5062 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5063 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5064 | int steal_from_global = 0; |
07127184 | 5065 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 CM |
5066 | int ret; |
5067 | ||
1abe9b8a | 5068 | trace_btrfs_inode_evict(inode); |
5069 | ||
131e404a FDBM |
5070 | evict_inode_truncate_pages(inode); |
5071 | ||
69e9c6c6 SB |
5072 | if (inode->i_nlink && |
5073 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5074 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
5075 | btrfs_is_free_space_inode(inode))) | |
bd555975 AV |
5076 | goto no_delete; |
5077 | ||
39279cc3 | 5078 | if (is_bad_inode(inode)) { |
7b128766 | 5079 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
5080 | goto no_delete; |
5081 | } | |
bd555975 | 5082 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
4a096752 | 5083 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
5f39d397 | 5084 | |
f612496b MX |
5085 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
5086 | ||
c71bf099 | 5087 | if (root->fs_info->log_root_recovering) { |
6bf02314 | 5088 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5089 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5090 | goto no_delete; |
5091 | } | |
5092 | ||
76dda93c | 5093 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5094 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5095 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5096 | goto no_delete; |
5097 | } | |
5098 | ||
0e8c36a9 MX |
5099 | ret = btrfs_commit_inode_delayed_inode(inode); |
5100 | if (ret) { | |
5101 | btrfs_orphan_del(NULL, inode); | |
5102 | goto no_delete; | |
5103 | } | |
5104 | ||
66d8f3dd | 5105 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
4289a667 JB |
5106 | if (!rsv) { |
5107 | btrfs_orphan_del(NULL, inode); | |
5108 | goto no_delete; | |
5109 | } | |
4a338542 | 5110 | rsv->size = min_size; |
ca7e70f5 | 5111 | rsv->failfast = 1; |
726c35fa | 5112 | global_rsv = &root->fs_info->global_block_rsv; |
4289a667 | 5113 | |
dbe674a9 | 5114 | btrfs_i_size_write(inode, 0); |
5f39d397 | 5115 | |
4289a667 | 5116 | /* |
8407aa46 MX |
5117 | * This is a bit simpler than btrfs_truncate since we've already |
5118 | * reserved our space for our orphan item in the unlink, so we just | |
5119 | * need to reserve some slack space in case we add bytes and update | |
5120 | * inode item when doing the truncate. | |
4289a667 | 5121 | */ |
8082510e | 5122 | while (1) { |
08e007d2 MX |
5123 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5124 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5125 | |
5126 | /* | |
5127 | * Try and steal from the global reserve since we will | |
5128 | * likely not use this space anyway, we want to try as | |
5129 | * hard as possible to get this to work. | |
5130 | */ | |
5131 | if (ret) | |
3bce876f JB |
5132 | steal_from_global++; |
5133 | else | |
5134 | steal_from_global = 0; | |
5135 | ret = 0; | |
d68fc57b | 5136 | |
3bce876f JB |
5137 | /* |
5138 | * steal_from_global == 0: we reserved stuff, hooray! | |
5139 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5140 | * steal_from_global == 2: we've committed, still not a lot of | |
5141 | * room but maybe we'll have room in the global reserve this | |
5142 | * time. | |
5143 | * steal_from_global == 3: abandon all hope! | |
5144 | */ | |
5145 | if (steal_from_global > 2) { | |
c2cf52eb SK |
5146 | btrfs_warn(root->fs_info, |
5147 | "Could not get space for a delete, will truncate on mount %d", | |
5148 | ret); | |
4289a667 JB |
5149 | btrfs_orphan_del(NULL, inode); |
5150 | btrfs_free_block_rsv(root, rsv); | |
5151 | goto no_delete; | |
d68fc57b | 5152 | } |
7b128766 | 5153 | |
0e8c36a9 | 5154 | trans = btrfs_join_transaction(root); |
4289a667 JB |
5155 | if (IS_ERR(trans)) { |
5156 | btrfs_orphan_del(NULL, inode); | |
5157 | btrfs_free_block_rsv(root, rsv); | |
5158 | goto no_delete; | |
d68fc57b | 5159 | } |
7b128766 | 5160 | |
3bce876f JB |
5161 | /* |
5162 | * We can't just steal from the global reserve, we need tomake | |
5163 | * sure there is room to do it, if not we need to commit and try | |
5164 | * again. | |
5165 | */ | |
5166 | if (steal_from_global) { | |
5167 | if (!btrfs_check_space_for_delayed_refs(trans, root)) | |
5168 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, | |
5169 | min_size); | |
5170 | else | |
5171 | ret = -ENOSPC; | |
5172 | } | |
5173 | ||
5174 | /* | |
5175 | * Couldn't steal from the global reserve, we have too much | |
5176 | * pending stuff built up, commit the transaction and try it | |
5177 | * again. | |
5178 | */ | |
5179 | if (ret) { | |
5180 | ret = btrfs_commit_transaction(trans, root); | |
5181 | if (ret) { | |
5182 | btrfs_orphan_del(NULL, inode); | |
5183 | btrfs_free_block_rsv(root, rsv); | |
5184 | goto no_delete; | |
5185 | } | |
5186 | continue; | |
5187 | } else { | |
5188 | steal_from_global = 0; | |
5189 | } | |
5190 | ||
4289a667 JB |
5191 | trans->block_rsv = rsv; |
5192 | ||
d68fc57b | 5193 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5194 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5195 | break; |
85e21bac | 5196 | |
8407aa46 | 5197 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e YZ |
5198 | btrfs_end_transaction(trans, root); |
5199 | trans = NULL; | |
b53d3f5d | 5200 | btrfs_btree_balance_dirty(root); |
8082510e | 5201 | } |
5f39d397 | 5202 | |
4289a667 JB |
5203 | btrfs_free_block_rsv(root, rsv); |
5204 | ||
4ef31a45 JB |
5205 | /* |
5206 | * Errors here aren't a big deal, it just means we leave orphan items | |
5207 | * in the tree. They will be cleaned up on the next mount. | |
5208 | */ | |
8082510e | 5209 | if (ret == 0) { |
4289a667 | 5210 | trans->block_rsv = root->orphan_block_rsv; |
4ef31a45 JB |
5211 | btrfs_orphan_del(trans, inode); |
5212 | } else { | |
5213 | btrfs_orphan_del(NULL, inode); | |
8082510e | 5214 | } |
54aa1f4d | 5215 | |
4289a667 | 5216 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
581bb050 LZ |
5217 | if (!(root == root->fs_info->tree_root || |
5218 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) | |
33345d01 | 5219 | btrfs_return_ino(root, btrfs_ino(inode)); |
581bb050 | 5220 | |
54aa1f4d | 5221 | btrfs_end_transaction(trans, root); |
b53d3f5d | 5222 | btrfs_btree_balance_dirty(root); |
39279cc3 | 5223 | no_delete: |
89042e5a | 5224 | btrfs_remove_delayed_node(inode); |
dbd5768f | 5225 | clear_inode(inode); |
8082510e | 5226 | return; |
39279cc3 CM |
5227 | } |
5228 | ||
5229 | /* | |
5230 | * this returns the key found in the dir entry in the location pointer. | |
5231 | * If no dir entries were found, location->objectid is 0. | |
5232 | */ | |
5233 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5234 | struct btrfs_key *location) | |
5235 | { | |
5236 | const char *name = dentry->d_name.name; | |
5237 | int namelen = dentry->d_name.len; | |
5238 | struct btrfs_dir_item *di; | |
5239 | struct btrfs_path *path; | |
5240 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5241 | int ret = 0; |
39279cc3 CM |
5242 | |
5243 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5244 | if (!path) |
5245 | return -ENOMEM; | |
3954401f | 5246 | |
33345d01 | 5247 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
39279cc3 | 5248 | namelen, 0); |
0d9f7f3e Y |
5249 | if (IS_ERR(di)) |
5250 | ret = PTR_ERR(di); | |
d397712b | 5251 | |
c704005d | 5252 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5253 | goto out_err; |
d397712b | 5254 | |
5f39d397 | 5255 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5256 | out: |
39279cc3 CM |
5257 | btrfs_free_path(path); |
5258 | return ret; | |
3954401f CM |
5259 | out_err: |
5260 | location->objectid = 0; | |
5261 | goto out; | |
39279cc3 CM |
5262 | } |
5263 | ||
5264 | /* | |
5265 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5266 | * needs to be changed to reflect the root directory of the tree root. This | |
5267 | * is kind of like crossing a mount point. | |
5268 | */ | |
5269 | static int fixup_tree_root_location(struct btrfs_root *root, | |
4df27c4d YZ |
5270 | struct inode *dir, |
5271 | struct dentry *dentry, | |
5272 | struct btrfs_key *location, | |
5273 | struct btrfs_root **sub_root) | |
39279cc3 | 5274 | { |
4df27c4d YZ |
5275 | struct btrfs_path *path; |
5276 | struct btrfs_root *new_root; | |
5277 | struct btrfs_root_ref *ref; | |
5278 | struct extent_buffer *leaf; | |
1d4c08e0 | 5279 | struct btrfs_key key; |
4df27c4d YZ |
5280 | int ret; |
5281 | int err = 0; | |
39279cc3 | 5282 | |
4df27c4d YZ |
5283 | path = btrfs_alloc_path(); |
5284 | if (!path) { | |
5285 | err = -ENOMEM; | |
5286 | goto out; | |
5287 | } | |
39279cc3 | 5288 | |
4df27c4d | 5289 | err = -ENOENT; |
1d4c08e0 DS |
5290 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5291 | key.type = BTRFS_ROOT_REF_KEY; | |
5292 | key.offset = location->objectid; | |
5293 | ||
5294 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, | |
5295 | 0, 0); | |
4df27c4d YZ |
5296 | if (ret) { |
5297 | if (ret < 0) | |
5298 | err = ret; | |
5299 | goto out; | |
5300 | } | |
39279cc3 | 5301 | |
4df27c4d YZ |
5302 | leaf = path->nodes[0]; |
5303 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
33345d01 | 5304 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
4df27c4d YZ |
5305 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5306 | goto out; | |
39279cc3 | 5307 | |
4df27c4d YZ |
5308 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5309 | (unsigned long)(ref + 1), | |
5310 | dentry->d_name.len); | |
5311 | if (ret) | |
5312 | goto out; | |
5313 | ||
b3b4aa74 | 5314 | btrfs_release_path(path); |
4df27c4d YZ |
5315 | |
5316 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); | |
5317 | if (IS_ERR(new_root)) { | |
5318 | err = PTR_ERR(new_root); | |
5319 | goto out; | |
5320 | } | |
5321 | ||
4df27c4d YZ |
5322 | *sub_root = new_root; |
5323 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5324 | location->type = BTRFS_INODE_ITEM_KEY; | |
5325 | location->offset = 0; | |
5326 | err = 0; | |
5327 | out: | |
5328 | btrfs_free_path(path); | |
5329 | return err; | |
39279cc3 CM |
5330 | } |
5331 | ||
5d4f98a2 YZ |
5332 | static void inode_tree_add(struct inode *inode) |
5333 | { | |
5334 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5335 | struct btrfs_inode *entry; | |
03e860bd NP |
5336 | struct rb_node **p; |
5337 | struct rb_node *parent; | |
cef21937 | 5338 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
33345d01 | 5339 | u64 ino = btrfs_ino(inode); |
5d4f98a2 | 5340 | |
1d3382cb | 5341 | if (inode_unhashed(inode)) |
76dda93c | 5342 | return; |
e1409cef | 5343 | parent = NULL; |
5d4f98a2 | 5344 | spin_lock(&root->inode_lock); |
e1409cef | 5345 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5346 | while (*p) { |
5347 | parent = *p; | |
5348 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5349 | ||
33345d01 | 5350 | if (ino < btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5351 | p = &parent->rb_left; |
33345d01 | 5352 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5353 | p = &parent->rb_right; |
5d4f98a2 YZ |
5354 | else { |
5355 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5356 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5357 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd NP |
5358 | RB_CLEAR_NODE(parent); |
5359 | spin_unlock(&root->inode_lock); | |
cef21937 | 5360 | return; |
5d4f98a2 YZ |
5361 | } |
5362 | } | |
cef21937 FDBM |
5363 | rb_link_node(new, parent, p); |
5364 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5365 | spin_unlock(&root->inode_lock); |
5366 | } | |
5367 | ||
5368 | static void inode_tree_del(struct inode *inode) | |
5369 | { | |
5370 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
76dda93c | 5371 | int empty = 0; |
5d4f98a2 | 5372 | |
03e860bd | 5373 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5374 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5375 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5376 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5377 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5378 | } |
03e860bd | 5379 | spin_unlock(&root->inode_lock); |
76dda93c | 5380 | |
69e9c6c6 | 5381 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5382 | synchronize_srcu(&root->fs_info->subvol_srcu); |
5383 | spin_lock(&root->inode_lock); | |
5384 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5385 | spin_unlock(&root->inode_lock); | |
5386 | if (empty) | |
5387 | btrfs_add_dead_root(root); | |
5388 | } | |
5389 | } | |
5390 | ||
143bede5 | 5391 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c YZ |
5392 | { |
5393 | struct rb_node *node; | |
5394 | struct rb_node *prev; | |
5395 | struct btrfs_inode *entry; | |
5396 | struct inode *inode; | |
5397 | u64 objectid = 0; | |
5398 | ||
7813b3db LB |
5399 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
5400 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
76dda93c YZ |
5401 | |
5402 | spin_lock(&root->inode_lock); | |
5403 | again: | |
5404 | node = root->inode_tree.rb_node; | |
5405 | prev = NULL; | |
5406 | while (node) { | |
5407 | prev = node; | |
5408 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5409 | ||
33345d01 | 5410 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
76dda93c | 5411 | node = node->rb_left; |
33345d01 | 5412 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
76dda93c YZ |
5413 | node = node->rb_right; |
5414 | else | |
5415 | break; | |
5416 | } | |
5417 | if (!node) { | |
5418 | while (prev) { | |
5419 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
33345d01 | 5420 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
76dda93c YZ |
5421 | node = prev; |
5422 | break; | |
5423 | } | |
5424 | prev = rb_next(prev); | |
5425 | } | |
5426 | } | |
5427 | while (node) { | |
5428 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
33345d01 | 5429 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
76dda93c YZ |
5430 | inode = igrab(&entry->vfs_inode); |
5431 | if (inode) { | |
5432 | spin_unlock(&root->inode_lock); | |
5433 | if (atomic_read(&inode->i_count) > 1) | |
5434 | d_prune_aliases(inode); | |
5435 | /* | |
45321ac5 | 5436 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5437 | * the inode cache when its usage count |
5438 | * hits zero. | |
5439 | */ | |
5440 | iput(inode); | |
5441 | cond_resched(); | |
5442 | spin_lock(&root->inode_lock); | |
5443 | goto again; | |
5444 | } | |
5445 | ||
5446 | if (cond_resched_lock(&root->inode_lock)) | |
5447 | goto again; | |
5448 | ||
5449 | node = rb_next(node); | |
5450 | } | |
5451 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5452 | } |
5453 | ||
e02119d5 CM |
5454 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5455 | { | |
5456 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5457 | inode->i_ino = args->location->objectid; |
5458 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5459 | sizeof(*args->location)); | |
e02119d5 | 5460 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5461 | return 0; |
5462 | } | |
5463 | ||
5464 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5465 | { | |
5466 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5467 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5468 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5469 | } |
5470 | ||
5d4f98a2 | 5471 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5472 | struct btrfs_key *location, |
5d4f98a2 | 5473 | struct btrfs_root *root) |
39279cc3 CM |
5474 | { |
5475 | struct inode *inode; | |
5476 | struct btrfs_iget_args args; | |
90d3e592 | 5477 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5478 | |
90d3e592 | 5479 | args.location = location; |
39279cc3 CM |
5480 | args.root = root; |
5481 | ||
778ba82b | 5482 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5483 | btrfs_init_locked_inode, |
5484 | (void *)&args); | |
5485 | return inode; | |
5486 | } | |
5487 | ||
1a54ef8c BR |
5488 | /* Get an inode object given its location and corresponding root. |
5489 | * Returns in *is_new if the inode was read from disk | |
5490 | */ | |
5491 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5492 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5493 | { |
5494 | struct inode *inode; | |
5495 | ||
90d3e592 | 5496 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5497 | if (!inode) |
5d4f98a2 | 5498 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5499 | |
5500 | if (inode->i_state & I_NEW) { | |
1a54ef8c | 5501 | btrfs_read_locked_inode(inode); |
1748f843 MF |
5502 | if (!is_bad_inode(inode)) { |
5503 | inode_tree_add(inode); | |
5504 | unlock_new_inode(inode); | |
5505 | if (new) | |
5506 | *new = 1; | |
5507 | } else { | |
e0b6d65b ST |
5508 | unlock_new_inode(inode); |
5509 | iput(inode); | |
5510 | inode = ERR_PTR(-ESTALE); | |
1748f843 MF |
5511 | } |
5512 | } | |
5513 | ||
1a54ef8c BR |
5514 | return inode; |
5515 | } | |
5516 | ||
4df27c4d YZ |
5517 | static struct inode *new_simple_dir(struct super_block *s, |
5518 | struct btrfs_key *key, | |
5519 | struct btrfs_root *root) | |
5520 | { | |
5521 | struct inode *inode = new_inode(s); | |
5522 | ||
5523 | if (!inode) | |
5524 | return ERR_PTR(-ENOMEM); | |
5525 | ||
4df27c4d YZ |
5526 | BTRFS_I(inode)->root = root; |
5527 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5528 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5529 | |
5530 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5531 | inode->i_op = &btrfs_dir_ro_inode_operations; |
4df27c4d YZ |
5532 | inode->i_fop = &simple_dir_operations; |
5533 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
9cc97d64 | 5534 | inode->i_mtime = CURRENT_TIME; |
5535 | inode->i_atime = inode->i_mtime; | |
5536 | inode->i_ctime = inode->i_mtime; | |
5537 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5538 | |
5539 | return inode; | |
5540 | } | |
5541 | ||
3de4586c | 5542 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5543 | { |
d397712b | 5544 | struct inode *inode; |
4df27c4d | 5545 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5546 | struct btrfs_root *sub_root = root; |
5547 | struct btrfs_key location; | |
76dda93c | 5548 | int index; |
b4aff1f8 | 5549 | int ret = 0; |
39279cc3 CM |
5550 | |
5551 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5552 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5553 | |
39e3c955 | 5554 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5555 | if (ret < 0) |
5556 | return ERR_PTR(ret); | |
5f39d397 | 5557 | |
4df27c4d | 5558 | if (location.objectid == 0) |
5662344b | 5559 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5560 | |
5561 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5562 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5563 | return inode; |
5564 | } | |
5565 | ||
5566 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5567 | ||
76dda93c | 5568 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
4df27c4d YZ |
5569 | ret = fixup_tree_root_location(root, dir, dentry, |
5570 | &location, &sub_root); | |
5571 | if (ret < 0) { | |
5572 | if (ret != -ENOENT) | |
5573 | inode = ERR_PTR(ret); | |
5574 | else | |
5575 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5576 | } else { | |
73f73415 | 5577 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5578 | } |
76dda93c YZ |
5579 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
5580 | ||
34d19bad | 5581 | if (!IS_ERR(inode) && root != sub_root) { |
c71bf099 YZ |
5582 | down_read(&root->fs_info->cleanup_work_sem); |
5583 | if (!(inode->i_sb->s_flags & MS_RDONLY)) | |
66b4ffd1 | 5584 | ret = btrfs_orphan_cleanup(sub_root); |
c71bf099 | 5585 | up_read(&root->fs_info->cleanup_work_sem); |
01cd3367 JB |
5586 | if (ret) { |
5587 | iput(inode); | |
66b4ffd1 | 5588 | inode = ERR_PTR(ret); |
01cd3367 | 5589 | } |
c71bf099 YZ |
5590 | } |
5591 | ||
3de4586c CM |
5592 | return inode; |
5593 | } | |
5594 | ||
fe15ce44 | 5595 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5596 | { |
5597 | struct btrfs_root *root; | |
2b0143b5 | 5598 | struct inode *inode = d_inode(dentry); |
76dda93c | 5599 | |
848cce0d | 5600 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5601 | inode = d_inode(dentry->d_parent); |
76dda93c | 5602 | |
848cce0d LZ |
5603 | if (inode) { |
5604 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5605 | if (btrfs_root_refs(&root->root_item) == 0) |
5606 | return 1; | |
848cce0d LZ |
5607 | |
5608 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) | |
5609 | return 1; | |
efefb143 | 5610 | } |
76dda93c YZ |
5611 | return 0; |
5612 | } | |
5613 | ||
b4aff1f8 JB |
5614 | static void btrfs_dentry_release(struct dentry *dentry) |
5615 | { | |
944a4515 | 5616 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5617 | } |
5618 | ||
3de4586c | 5619 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5620 | unsigned int flags) |
3de4586c | 5621 | { |
5662344b | 5622 | struct inode *inode; |
a66e7cc6 | 5623 | |
5662344b TI |
5624 | inode = btrfs_lookup_dentry(dir, dentry); |
5625 | if (IS_ERR(inode)) { | |
5626 | if (PTR_ERR(inode) == -ENOENT) | |
5627 | inode = NULL; | |
5628 | else | |
5629 | return ERR_CAST(inode); | |
5630 | } | |
5631 | ||
41d28bca | 5632 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5633 | } |
5634 | ||
16cdcec7 | 5635 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5636 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5637 | }; | |
5638 | ||
9cdda8d3 | 5639 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5640 | { |
9cdda8d3 | 5641 | struct inode *inode = file_inode(file); |
39279cc3 CM |
5642 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5643 | struct btrfs_item *item; | |
5644 | struct btrfs_dir_item *di; | |
5645 | struct btrfs_key key; | |
5f39d397 | 5646 | struct btrfs_key found_key; |
39279cc3 | 5647 | struct btrfs_path *path; |
16cdcec7 MX |
5648 | struct list_head ins_list; |
5649 | struct list_head del_list; | |
39279cc3 | 5650 | int ret; |
5f39d397 | 5651 | struct extent_buffer *leaf; |
39279cc3 | 5652 | int slot; |
39279cc3 CM |
5653 | unsigned char d_type; |
5654 | int over = 0; | |
5655 | u32 di_cur; | |
5656 | u32 di_total; | |
5657 | u32 di_len; | |
5658 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
5659 | char tmp_name[32]; |
5660 | char *name_ptr; | |
5661 | int name_len; | |
9cdda8d3 | 5662 | int is_curr = 0; /* ctx->pos points to the current index? */ |
39279cc3 CM |
5663 | |
5664 | /* FIXME, use a real flag for deciding about the key type */ | |
5665 | if (root->fs_info->tree_root == root) | |
5666 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 5667 | |
9cdda8d3 AV |
5668 | if (!dir_emit_dots(file, ctx)) |
5669 | return 0; | |
5670 | ||
49593bfa | 5671 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5672 | if (!path) |
5673 | return -ENOMEM; | |
ff5714cc | 5674 | |
026fd317 | 5675 | path->reada = 1; |
49593bfa | 5676 | |
16cdcec7 MX |
5677 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5678 | INIT_LIST_HEAD(&ins_list); | |
5679 | INIT_LIST_HEAD(&del_list); | |
5680 | btrfs_get_delayed_items(inode, &ins_list, &del_list); | |
5681 | } | |
5682 | ||
962a298f | 5683 | key.type = key_type; |
9cdda8d3 | 5684 | key.offset = ctx->pos; |
33345d01 | 5685 | key.objectid = btrfs_ino(inode); |
5f39d397 | 5686 | |
39279cc3 CM |
5687 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5688 | if (ret < 0) | |
5689 | goto err; | |
49593bfa DW |
5690 | |
5691 | while (1) { | |
5f39d397 | 5692 | leaf = path->nodes[0]; |
39279cc3 | 5693 | slot = path->slots[0]; |
b9e03af0 LZ |
5694 | if (slot >= btrfs_header_nritems(leaf)) { |
5695 | ret = btrfs_next_leaf(root, path); | |
5696 | if (ret < 0) | |
5697 | goto err; | |
5698 | else if (ret > 0) | |
5699 | break; | |
5700 | continue; | |
39279cc3 | 5701 | } |
3de4586c | 5702 | |
dd3cc16b | 5703 | item = btrfs_item_nr(slot); |
5f39d397 CM |
5704 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5705 | ||
5706 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5707 | break; |
962a298f | 5708 | if (found_key.type != key_type) |
39279cc3 | 5709 | break; |
9cdda8d3 | 5710 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5711 | goto next; |
16cdcec7 MX |
5712 | if (key_type == BTRFS_DIR_INDEX_KEY && |
5713 | btrfs_should_delete_dir_index(&del_list, | |
5714 | found_key.offset)) | |
5715 | goto next; | |
5f39d397 | 5716 | |
9cdda8d3 | 5717 | ctx->pos = found_key.offset; |
16cdcec7 | 5718 | is_curr = 1; |
49593bfa | 5719 | |
39279cc3 CM |
5720 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
5721 | di_cur = 0; | |
5f39d397 | 5722 | di_total = btrfs_item_size(leaf, item); |
49593bfa DW |
5723 | |
5724 | while (di_cur < di_total) { | |
5f39d397 CM |
5725 | struct btrfs_key location; |
5726 | ||
22a94d44 JB |
5727 | if (verify_dir_item(root, leaf, di)) |
5728 | break; | |
5729 | ||
5f39d397 | 5730 | name_len = btrfs_dir_name_len(leaf, di); |
49593bfa | 5731 | if (name_len <= sizeof(tmp_name)) { |
5f39d397 CM |
5732 | name_ptr = tmp_name; |
5733 | } else { | |
5734 | name_ptr = kmalloc(name_len, GFP_NOFS); | |
49593bfa DW |
5735 | if (!name_ptr) { |
5736 | ret = -ENOMEM; | |
5737 | goto err; | |
5738 | } | |
5f39d397 CM |
5739 | } |
5740 | read_extent_buffer(leaf, name_ptr, | |
5741 | (unsigned long)(di + 1), name_len); | |
5742 | ||
5743 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
5744 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
3de4586c | 5745 | |
fede766f | 5746 | |
3de4586c | 5747 | /* is this a reference to our own snapshot? If so |
8c9c2bf7 AJ |
5748 | * skip it. |
5749 | * | |
5750 | * In contrast to old kernels, we insert the snapshot's | |
5751 | * dir item and dir index after it has been created, so | |
5752 | * we won't find a reference to our own snapshot. We | |
5753 | * still keep the following code for backward | |
5754 | * compatibility. | |
3de4586c CM |
5755 | */ |
5756 | if (location.type == BTRFS_ROOT_ITEM_KEY && | |
5757 | location.objectid == root->root_key.objectid) { | |
5758 | over = 0; | |
5759 | goto skip; | |
5760 | } | |
9cdda8d3 AV |
5761 | over = !dir_emit(ctx, name_ptr, name_len, |
5762 | location.objectid, d_type); | |
5f39d397 | 5763 | |
3de4586c | 5764 | skip: |
5f39d397 CM |
5765 | if (name_ptr != tmp_name) |
5766 | kfree(name_ptr); | |
5767 | ||
39279cc3 CM |
5768 | if (over) |
5769 | goto nopos; | |
5103e947 | 5770 | di_len = btrfs_dir_name_len(leaf, di) + |
49593bfa | 5771 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
39279cc3 CM |
5772 | di_cur += di_len; |
5773 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
5774 | } | |
b9e03af0 LZ |
5775 | next: |
5776 | path->slots[0]++; | |
39279cc3 | 5777 | } |
49593bfa | 5778 | |
16cdcec7 MX |
5779 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5780 | if (is_curr) | |
9cdda8d3 AV |
5781 | ctx->pos++; |
5782 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); | |
16cdcec7 MX |
5783 | if (ret) |
5784 | goto nopos; | |
5785 | } | |
5786 | ||
49593bfa | 5787 | /* Reached end of directory/root. Bump pos past the last item. */ |
db62efbb ZB |
5788 | ctx->pos++; |
5789 | ||
5790 | /* | |
5791 | * Stop new entries from being returned after we return the last | |
5792 | * entry. | |
5793 | * | |
5794 | * New directory entries are assigned a strictly increasing | |
5795 | * offset. This means that new entries created during readdir | |
5796 | * are *guaranteed* to be seen in the future by that readdir. | |
5797 | * This has broken buggy programs which operate on names as | |
5798 | * they're returned by readdir. Until we re-use freed offsets | |
5799 | * we have this hack to stop new entries from being returned | |
5800 | * under the assumption that they'll never reach this huge | |
5801 | * offset. | |
5802 | * | |
5803 | * This is being careful not to overflow 32bit loff_t unless the | |
5804 | * last entry requires it because doing so has broken 32bit apps | |
5805 | * in the past. | |
5806 | */ | |
5807 | if (key_type == BTRFS_DIR_INDEX_KEY) { | |
5808 | if (ctx->pos >= INT_MAX) | |
5809 | ctx->pos = LLONG_MAX; | |
5810 | else | |
5811 | ctx->pos = INT_MAX; | |
5812 | } | |
39279cc3 CM |
5813 | nopos: |
5814 | ret = 0; | |
5815 | err: | |
16cdcec7 MX |
5816 | if (key_type == BTRFS_DIR_INDEX_KEY) |
5817 | btrfs_put_delayed_items(&ins_list, &del_list); | |
39279cc3 | 5818 | btrfs_free_path(path); |
39279cc3 CM |
5819 | return ret; |
5820 | } | |
5821 | ||
a9185b41 | 5822 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
5823 | { |
5824 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5825 | struct btrfs_trans_handle *trans; | |
5826 | int ret = 0; | |
0af3d00b | 5827 | bool nolock = false; |
39279cc3 | 5828 | |
72ac3c0d | 5829 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
5830 | return 0; |
5831 | ||
83eea1f1 | 5832 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
82d5902d | 5833 | nolock = true; |
0af3d00b | 5834 | |
a9185b41 | 5835 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 5836 | if (nolock) |
7a7eaa40 | 5837 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 5838 | else |
7a7eaa40 | 5839 | trans = btrfs_join_transaction(root); |
3612b495 TI |
5840 | if (IS_ERR(trans)) |
5841 | return PTR_ERR(trans); | |
a698d075 | 5842 | ret = btrfs_commit_transaction(trans, root); |
39279cc3 CM |
5843 | } |
5844 | return ret; | |
5845 | } | |
5846 | ||
5847 | /* | |
54aa1f4d | 5848 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5849 | * inode changes. But, it is most likely to find the inode in cache. |
5850 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5851 | * to keep or drop this code. | |
5852 | */ | |
48a3b636 | 5853 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 CM |
5854 | { |
5855 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5856 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5857 | int ret; |
5858 | ||
72ac3c0d | 5859 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5860 | return 0; |
39279cc3 | 5861 | |
7a7eaa40 | 5862 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5863 | if (IS_ERR(trans)) |
5864 | return PTR_ERR(trans); | |
8929ecfa YZ |
5865 | |
5866 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
5867 | if (ret && ret == -ENOSPC) { |
5868 | /* whoops, lets try again with the full transaction */ | |
5869 | btrfs_end_transaction(trans, root); | |
5870 | trans = btrfs_start_transaction(root, 1); | |
22c44fe6 JB |
5871 | if (IS_ERR(trans)) |
5872 | return PTR_ERR(trans); | |
8929ecfa | 5873 | |
94b60442 | 5874 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 5875 | } |
39279cc3 | 5876 | btrfs_end_transaction(trans, root); |
16cdcec7 MX |
5877 | if (BTRFS_I(inode)->delayed_node) |
5878 | btrfs_balance_delayed_items(root); | |
22c44fe6 JB |
5879 | |
5880 | return ret; | |
5881 | } | |
5882 | ||
5883 | /* | |
5884 | * This is a copy of file_update_time. We need this so we can return error on | |
5885 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
5886 | */ | |
e41f941a JB |
5887 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
5888 | int flags) | |
22c44fe6 | 5889 | { |
2bc55652 AB |
5890 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5891 | ||
5892 | if (btrfs_root_readonly(root)) | |
5893 | return -EROFS; | |
5894 | ||
e41f941a | 5895 | if (flags & S_VERSION) |
22c44fe6 | 5896 | inode_inc_iversion(inode); |
e41f941a JB |
5897 | if (flags & S_CTIME) |
5898 | inode->i_ctime = *now; | |
5899 | if (flags & S_MTIME) | |
5900 | inode->i_mtime = *now; | |
5901 | if (flags & S_ATIME) | |
5902 | inode->i_atime = *now; | |
5903 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
5904 | } |
5905 | ||
d352ac68 CM |
5906 | /* |
5907 | * find the highest existing sequence number in a directory | |
5908 | * and then set the in-memory index_cnt variable to reflect | |
5909 | * free sequence numbers | |
5910 | */ | |
aec7477b JB |
5911 | static int btrfs_set_inode_index_count(struct inode *inode) |
5912 | { | |
5913 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5914 | struct btrfs_key key, found_key; | |
5915 | struct btrfs_path *path; | |
5916 | struct extent_buffer *leaf; | |
5917 | int ret; | |
5918 | ||
33345d01 | 5919 | key.objectid = btrfs_ino(inode); |
962a298f | 5920 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
5921 | key.offset = (u64)-1; |
5922 | ||
5923 | path = btrfs_alloc_path(); | |
5924 | if (!path) | |
5925 | return -ENOMEM; | |
5926 | ||
5927 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5928 | if (ret < 0) | |
5929 | goto out; | |
5930 | /* FIXME: we should be able to handle this */ | |
5931 | if (ret == 0) | |
5932 | goto out; | |
5933 | ret = 0; | |
5934 | ||
5935 | /* | |
5936 | * MAGIC NUMBER EXPLANATION: | |
5937 | * since we search a directory based on f_pos we have to start at 2 | |
5938 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
5939 | * else has to start at 2 | |
5940 | */ | |
5941 | if (path->slots[0] == 0) { | |
5942 | BTRFS_I(inode)->index_cnt = 2; | |
5943 | goto out; | |
5944 | } | |
5945 | ||
5946 | path->slots[0]--; | |
5947 | ||
5948 | leaf = path->nodes[0]; | |
5949 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5950 | ||
33345d01 | 5951 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 5952 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
aec7477b JB |
5953 | BTRFS_I(inode)->index_cnt = 2; |
5954 | goto out; | |
5955 | } | |
5956 | ||
5957 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
5958 | out: | |
5959 | btrfs_free_path(path); | |
5960 | return ret; | |
5961 | } | |
5962 | ||
d352ac68 CM |
5963 | /* |
5964 | * helper to find a free sequence number in a given directory. This current | |
5965 | * code is very simple, later versions will do smarter things in the btree | |
5966 | */ | |
3de4586c | 5967 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
aec7477b JB |
5968 | { |
5969 | int ret = 0; | |
5970 | ||
5971 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
16cdcec7 MX |
5972 | ret = btrfs_inode_delayed_dir_index_count(dir); |
5973 | if (ret) { | |
5974 | ret = btrfs_set_inode_index_count(dir); | |
5975 | if (ret) | |
5976 | return ret; | |
5977 | } | |
aec7477b JB |
5978 | } |
5979 | ||
00e4e6b3 | 5980 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
5981 | BTRFS_I(dir)->index_cnt++; |
5982 | ||
5983 | return ret; | |
5984 | } | |
5985 | ||
b0d5d10f CM |
5986 | static int btrfs_insert_inode_locked(struct inode *inode) |
5987 | { | |
5988 | struct btrfs_iget_args args; | |
5989 | args.location = &BTRFS_I(inode)->location; | |
5990 | args.root = BTRFS_I(inode)->root; | |
5991 | ||
5992 | return insert_inode_locked4(inode, | |
5993 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
5994 | btrfs_find_actor, &args); | |
5995 | } | |
5996 | ||
39279cc3 CM |
5997 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
5998 | struct btrfs_root *root, | |
aec7477b | 5999 | struct inode *dir, |
9c58309d | 6000 | const char *name, int name_len, |
175a4eb7 AV |
6001 | u64 ref_objectid, u64 objectid, |
6002 | umode_t mode, u64 *index) | |
39279cc3 CM |
6003 | { |
6004 | struct inode *inode; | |
5f39d397 | 6005 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6006 | struct btrfs_key *location; |
5f39d397 | 6007 | struct btrfs_path *path; |
9c58309d CM |
6008 | struct btrfs_inode_ref *ref; |
6009 | struct btrfs_key key[2]; | |
6010 | u32 sizes[2]; | |
ef3b9af5 | 6011 | int nitems = name ? 2 : 1; |
9c58309d | 6012 | unsigned long ptr; |
39279cc3 | 6013 | int ret; |
39279cc3 | 6014 | |
5f39d397 | 6015 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6016 | if (!path) |
6017 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6018 | |
39279cc3 | 6019 | inode = new_inode(root->fs_info->sb); |
8fb27640 YS |
6020 | if (!inode) { |
6021 | btrfs_free_path(path); | |
39279cc3 | 6022 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6023 | } |
39279cc3 | 6024 | |
5762b5c9 FM |
6025 | /* |
6026 | * O_TMPFILE, set link count to 0, so that after this point, | |
6027 | * we fill in an inode item with the correct link count. | |
6028 | */ | |
6029 | if (!name) | |
6030 | set_nlink(inode, 0); | |
6031 | ||
581bb050 LZ |
6032 | /* |
6033 | * we have to initialize this early, so we can reclaim the inode | |
6034 | * number if we fail afterwards in this function. | |
6035 | */ | |
6036 | inode->i_ino = objectid; | |
6037 | ||
ef3b9af5 | 6038 | if (dir && name) { |
1abe9b8a | 6039 | trace_btrfs_inode_request(dir); |
6040 | ||
3de4586c | 6041 | ret = btrfs_set_inode_index(dir, index); |
09771430 | 6042 | if (ret) { |
8fb27640 | 6043 | btrfs_free_path(path); |
09771430 | 6044 | iput(inode); |
aec7477b | 6045 | return ERR_PTR(ret); |
09771430 | 6046 | } |
ef3b9af5 FM |
6047 | } else if (dir) { |
6048 | *index = 0; | |
aec7477b JB |
6049 | } |
6050 | /* | |
6051 | * index_cnt is ignored for everything but a dir, | |
6052 | * btrfs_get_inode_index_count has an explanation for the magic | |
6053 | * number | |
6054 | */ | |
6055 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6056 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6057 | BTRFS_I(inode)->root = root; |
e02119d5 | 6058 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6059 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6060 | |
5dc562c5 JB |
6061 | /* |
6062 | * We could have gotten an inode number from somebody who was fsynced | |
6063 | * and then removed in this same transaction, so let's just set full | |
6064 | * sync since it will be a full sync anyway and this will blow away the | |
6065 | * old info in the log. | |
6066 | */ | |
6067 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6068 | ||
9c58309d | 6069 | key[0].objectid = objectid; |
962a298f | 6070 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6071 | key[0].offset = 0; |
6072 | ||
9c58309d | 6073 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6074 | |
6075 | if (name) { | |
6076 | /* | |
6077 | * Start new inodes with an inode_ref. This is slightly more | |
6078 | * efficient for small numbers of hard links since they will | |
6079 | * be packed into one item. Extended refs will kick in if we | |
6080 | * add more hard links than can fit in the ref item. | |
6081 | */ | |
6082 | key[1].objectid = objectid; | |
962a298f | 6083 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6084 | key[1].offset = ref_objectid; |
6085 | ||
6086 | sizes[1] = name_len + sizeof(*ref); | |
6087 | } | |
9c58309d | 6088 | |
b0d5d10f CM |
6089 | location = &BTRFS_I(inode)->location; |
6090 | location->objectid = objectid; | |
6091 | location->offset = 0; | |
962a298f | 6092 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6093 | |
6094 | ret = btrfs_insert_inode_locked(inode); | |
6095 | if (ret < 0) | |
6096 | goto fail; | |
6097 | ||
b9473439 | 6098 | path->leave_spinning = 1; |
ef3b9af5 | 6099 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6100 | if (ret != 0) |
b0d5d10f | 6101 | goto fail_unlock; |
5f39d397 | 6102 | |
ecc11fab | 6103 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6104 | inode_set_bytes(inode, 0); |
9cc97d64 | 6105 | |
6106 | inode->i_mtime = CURRENT_TIME; | |
6107 | inode->i_atime = inode->i_mtime; | |
6108 | inode->i_ctime = inode->i_mtime; | |
6109 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6110 | ||
5f39d397 CM |
6111 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6112 | struct btrfs_inode_item); | |
293f7e07 LZ |
6113 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
6114 | sizeof(*inode_item)); | |
e02119d5 | 6115 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6116 | |
ef3b9af5 FM |
6117 | if (name) { |
6118 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6119 | struct btrfs_inode_ref); | |
6120 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6121 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6122 | ptr = (unsigned long)(ref + 1); | |
6123 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6124 | } | |
9c58309d | 6125 | |
5f39d397 CM |
6126 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6127 | btrfs_free_path(path); | |
6128 | ||
6cbff00f CH |
6129 | btrfs_inherit_iflags(inode, dir); |
6130 | ||
569254b0 | 6131 | if (S_ISREG(mode)) { |
94272164 CM |
6132 | if (btrfs_test_opt(root, NODATASUM)) |
6133 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
213490b3 | 6134 | if (btrfs_test_opt(root, NODATACOW)) |
f2bdf9a8 JB |
6135 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6136 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6137 | } |
6138 | ||
5d4f98a2 | 6139 | inode_tree_add(inode); |
1abe9b8a | 6140 | |
6141 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6142 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6143 | |
8ea05e3a AB |
6144 | btrfs_update_root_times(trans, root); |
6145 | ||
63541927 FDBM |
6146 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6147 | if (ret) | |
6148 | btrfs_err(root->fs_info, | |
6149 | "error inheriting props for ino %llu (root %llu): %d", | |
6150 | btrfs_ino(inode), root->root_key.objectid, ret); | |
6151 | ||
39279cc3 | 6152 | return inode; |
b0d5d10f CM |
6153 | |
6154 | fail_unlock: | |
6155 | unlock_new_inode(inode); | |
5f39d397 | 6156 | fail: |
ef3b9af5 | 6157 | if (dir && name) |
aec7477b | 6158 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6159 | btrfs_free_path(path); |
09771430 | 6160 | iput(inode); |
5f39d397 | 6161 | return ERR_PTR(ret); |
39279cc3 CM |
6162 | } |
6163 | ||
6164 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6165 | { | |
6166 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6167 | } | |
6168 | ||
d352ac68 CM |
6169 | /* |
6170 | * utility function to add 'inode' into 'parent_inode' with | |
6171 | * a give name and a given sequence number. | |
6172 | * if 'add_backref' is true, also insert a backref from the | |
6173 | * inode to the parent directory. | |
6174 | */ | |
e02119d5 CM |
6175 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
6176 | struct inode *parent_inode, struct inode *inode, | |
6177 | const char *name, int name_len, int add_backref, u64 index) | |
39279cc3 | 6178 | { |
4df27c4d | 6179 | int ret = 0; |
39279cc3 | 6180 | struct btrfs_key key; |
e02119d5 | 6181 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
33345d01 LZ |
6182 | u64 ino = btrfs_ino(inode); |
6183 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6184 | |
33345d01 | 6185 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6186 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
6187 | } else { | |
33345d01 | 6188 | key.objectid = ino; |
962a298f | 6189 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6190 | key.offset = 0; |
6191 | } | |
6192 | ||
33345d01 | 6193 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6194 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
6195 | key.objectid, root->root_key.objectid, | |
33345d01 | 6196 | parent_ino, index, name, name_len); |
4df27c4d | 6197 | } else if (add_backref) { |
33345d01 LZ |
6198 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6199 | parent_ino, index); | |
4df27c4d | 6200 | } |
39279cc3 | 6201 | |
79787eaa JM |
6202 | /* Nothing to clean up yet */ |
6203 | if (ret) | |
6204 | return ret; | |
4df27c4d | 6205 | |
79787eaa JM |
6206 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6207 | parent_inode, &key, | |
6208 | btrfs_inode_type(inode), index); | |
9c52057c | 6209 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6210 | goto fail_dir_item; |
6211 | else if (ret) { | |
6212 | btrfs_abort_transaction(trans, root, ret); | |
6213 | return ret; | |
39279cc3 | 6214 | } |
79787eaa JM |
6215 | |
6216 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | |
6217 | name_len * 2); | |
0c4d2d95 | 6218 | inode_inc_iversion(parent_inode); |
79787eaa JM |
6219 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
6220 | ret = btrfs_update_inode(trans, root, parent_inode); | |
6221 | if (ret) | |
6222 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 6223 | return ret; |
fe66a05a CM |
6224 | |
6225 | fail_dir_item: | |
6226 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6227 | u64 local_index; | |
6228 | int err; | |
6229 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
6230 | key.objectid, root->root_key.objectid, | |
6231 | parent_ino, &local_index, name, name_len); | |
6232 | ||
6233 | } else if (add_backref) { | |
6234 | u64 local_index; | |
6235 | int err; | |
6236 | ||
6237 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6238 | ino, parent_ino, &local_index); | |
6239 | } | |
6240 | return ret; | |
39279cc3 CM |
6241 | } |
6242 | ||
6243 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
a1b075d2 JB |
6244 | struct inode *dir, struct dentry *dentry, |
6245 | struct inode *inode, int backref, u64 index) | |
39279cc3 | 6246 | { |
a1b075d2 JB |
6247 | int err = btrfs_add_link(trans, dir, inode, |
6248 | dentry->d_name.name, dentry->d_name.len, | |
6249 | backref, index); | |
39279cc3 CM |
6250 | if (err > 0) |
6251 | err = -EEXIST; | |
6252 | return err; | |
6253 | } | |
6254 | ||
618e21d5 | 6255 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6256 | umode_t mode, dev_t rdev) |
618e21d5 JB |
6257 | { |
6258 | struct btrfs_trans_handle *trans; | |
6259 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6260 | struct inode *inode = NULL; |
618e21d5 JB |
6261 | int err; |
6262 | int drop_inode = 0; | |
6263 | u64 objectid; | |
00e4e6b3 | 6264 | u64 index = 0; |
618e21d5 JB |
6265 | |
6266 | if (!new_valid_dev(rdev)) | |
6267 | return -EINVAL; | |
6268 | ||
9ed74f2d JB |
6269 | /* |
6270 | * 2 for inode item and ref | |
6271 | * 2 for dir items | |
6272 | * 1 for xattr if selinux is on | |
6273 | */ | |
a22285a6 YZ |
6274 | trans = btrfs_start_transaction(root, 5); |
6275 | if (IS_ERR(trans)) | |
6276 | return PTR_ERR(trans); | |
1832a6d5 | 6277 | |
581bb050 LZ |
6278 | err = btrfs_find_free_ino(root, &objectid); |
6279 | if (err) | |
6280 | goto out_unlock; | |
6281 | ||
aec7477b | 6282 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6283 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6284 | mode, &index); |
7cf96da3 TI |
6285 | if (IS_ERR(inode)) { |
6286 | err = PTR_ERR(inode); | |
618e21d5 | 6287 | goto out_unlock; |
7cf96da3 | 6288 | } |
618e21d5 | 6289 | |
ad19db71 CS |
6290 | /* |
6291 | * If the active LSM wants to access the inode during | |
6292 | * d_instantiate it needs these. Smack checks to see | |
6293 | * if the filesystem supports xattrs by looking at the | |
6294 | * ops vector. | |
6295 | */ | |
ad19db71 | 6296 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6297 | init_special_inode(inode, inode->i_mode, rdev); |
6298 | ||
6299 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6300 | if (err) |
b0d5d10f CM |
6301 | goto out_unlock_inode; |
6302 | ||
6303 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
6304 | if (err) { | |
6305 | goto out_unlock_inode; | |
6306 | } else { | |
1b4ab1bb | 6307 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6308 | unlock_new_inode(inode); |
08c422c2 | 6309 | d_instantiate(dentry, inode); |
618e21d5 | 6310 | } |
b0d5d10f | 6311 | |
618e21d5 | 6312 | out_unlock: |
7ad85bb7 | 6313 | btrfs_end_transaction(trans, root); |
c581afc8 | 6314 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6315 | btrfs_btree_balance_dirty(root); |
618e21d5 JB |
6316 | if (drop_inode) { |
6317 | inode_dec_link_count(inode); | |
6318 | iput(inode); | |
6319 | } | |
618e21d5 | 6320 | return err; |
b0d5d10f CM |
6321 | |
6322 | out_unlock_inode: | |
6323 | drop_inode = 1; | |
6324 | unlock_new_inode(inode); | |
6325 | goto out_unlock; | |
6326 | ||
618e21d5 JB |
6327 | } |
6328 | ||
39279cc3 | 6329 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6330 | umode_t mode, bool excl) |
39279cc3 CM |
6331 | { |
6332 | struct btrfs_trans_handle *trans; | |
6333 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6334 | struct inode *inode = NULL; |
43baa579 | 6335 | int drop_inode_on_err = 0; |
a22285a6 | 6336 | int err; |
39279cc3 | 6337 | u64 objectid; |
00e4e6b3 | 6338 | u64 index = 0; |
39279cc3 | 6339 | |
9ed74f2d JB |
6340 | /* |
6341 | * 2 for inode item and ref | |
6342 | * 2 for dir items | |
6343 | * 1 for xattr if selinux is on | |
6344 | */ | |
a22285a6 YZ |
6345 | trans = btrfs_start_transaction(root, 5); |
6346 | if (IS_ERR(trans)) | |
6347 | return PTR_ERR(trans); | |
9ed74f2d | 6348 | |
581bb050 LZ |
6349 | err = btrfs_find_free_ino(root, &objectid); |
6350 | if (err) | |
6351 | goto out_unlock; | |
6352 | ||
aec7477b | 6353 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6354 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6355 | mode, &index); |
7cf96da3 TI |
6356 | if (IS_ERR(inode)) { |
6357 | err = PTR_ERR(inode); | |
39279cc3 | 6358 | goto out_unlock; |
7cf96da3 | 6359 | } |
43baa579 | 6360 | drop_inode_on_err = 1; |
ad19db71 CS |
6361 | /* |
6362 | * If the active LSM wants to access the inode during | |
6363 | * d_instantiate it needs these. Smack checks to see | |
6364 | * if the filesystem supports xattrs by looking at the | |
6365 | * ops vector. | |
6366 | */ | |
6367 | inode->i_fop = &btrfs_file_operations; | |
6368 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6369 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6370 | |
6371 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6372 | if (err) | |
6373 | goto out_unlock_inode; | |
6374 | ||
6375 | err = btrfs_update_inode(trans, root, inode); | |
6376 | if (err) | |
6377 | goto out_unlock_inode; | |
ad19db71 | 6378 | |
a1b075d2 | 6379 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 6380 | if (err) |
b0d5d10f | 6381 | goto out_unlock_inode; |
43baa579 | 6382 | |
43baa579 | 6383 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6384 | unlock_new_inode(inode); |
43baa579 FB |
6385 | d_instantiate(dentry, inode); |
6386 | ||
39279cc3 | 6387 | out_unlock: |
7ad85bb7 | 6388 | btrfs_end_transaction(trans, root); |
43baa579 | 6389 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6390 | inode_dec_link_count(inode); |
6391 | iput(inode); | |
6392 | } | |
c581afc8 | 6393 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6394 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6395 | return err; |
b0d5d10f CM |
6396 | |
6397 | out_unlock_inode: | |
6398 | unlock_new_inode(inode); | |
6399 | goto out_unlock; | |
6400 | ||
39279cc3 CM |
6401 | } |
6402 | ||
6403 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6404 | struct dentry *dentry) | |
6405 | { | |
6406 | struct btrfs_trans_handle *trans; | |
6407 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2b0143b5 | 6408 | struct inode *inode = d_inode(old_dentry); |
00e4e6b3 | 6409 | u64 index; |
39279cc3 CM |
6410 | int err; |
6411 | int drop_inode = 0; | |
6412 | ||
4a8be425 TH |
6413 | /* do not allow sys_link's with other subvols of the same device */ |
6414 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6415 | return -EXDEV; |
4a8be425 | 6416 | |
f186373f | 6417 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6418 | return -EMLINK; |
4a8be425 | 6419 | |
3de4586c | 6420 | err = btrfs_set_inode_index(dir, &index); |
aec7477b JB |
6421 | if (err) |
6422 | goto fail; | |
6423 | ||
a22285a6 | 6424 | /* |
7e6b6465 | 6425 | * 2 items for inode and inode ref |
a22285a6 | 6426 | * 2 items for dir items |
7e6b6465 | 6427 | * 1 item for parent inode |
a22285a6 | 6428 | */ |
7e6b6465 | 6429 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6430 | if (IS_ERR(trans)) { |
6431 | err = PTR_ERR(trans); | |
6432 | goto fail; | |
6433 | } | |
5f39d397 | 6434 | |
67de1176 MX |
6435 | /* There are several dir indexes for this inode, clear the cache. */ |
6436 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6437 | inc_nlink(inode); |
0c4d2d95 | 6438 | inode_inc_iversion(inode); |
3153495d | 6439 | inode->i_ctime = CURRENT_TIME; |
7de9c6ee | 6440 | ihold(inode); |
e9976151 | 6441 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6442 | |
a1b075d2 | 6443 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
5f39d397 | 6444 | |
a5719521 | 6445 | if (err) { |
54aa1f4d | 6446 | drop_inode = 1; |
a5719521 | 6447 | } else { |
10d9f309 | 6448 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6449 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6450 | if (err) |
6451 | goto fail; | |
ef3b9af5 FM |
6452 | if (inode->i_nlink == 1) { |
6453 | /* | |
6454 | * If new hard link count is 1, it's a file created | |
6455 | * with open(2) O_TMPFILE flag. | |
6456 | */ | |
6457 | err = btrfs_orphan_del(trans, inode); | |
6458 | if (err) | |
6459 | goto fail; | |
6460 | } | |
08c422c2 | 6461 | d_instantiate(dentry, inode); |
6a912213 | 6462 | btrfs_log_new_name(trans, inode, NULL, parent); |
a5719521 | 6463 | } |
39279cc3 | 6464 | |
7ad85bb7 | 6465 | btrfs_end_transaction(trans, root); |
c581afc8 | 6466 | btrfs_balance_delayed_items(root); |
1832a6d5 | 6467 | fail: |
39279cc3 CM |
6468 | if (drop_inode) { |
6469 | inode_dec_link_count(inode); | |
6470 | iput(inode); | |
6471 | } | |
b53d3f5d | 6472 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
6473 | return err; |
6474 | } | |
6475 | ||
18bb1db3 | 6476 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6477 | { |
b9d86667 | 6478 | struct inode *inode = NULL; |
39279cc3 CM |
6479 | struct btrfs_trans_handle *trans; |
6480 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6481 | int err = 0; | |
6482 | int drop_on_err = 0; | |
b9d86667 | 6483 | u64 objectid = 0; |
00e4e6b3 | 6484 | u64 index = 0; |
39279cc3 | 6485 | |
9ed74f2d JB |
6486 | /* |
6487 | * 2 items for inode and ref | |
6488 | * 2 items for dir items | |
6489 | * 1 for xattr if selinux is on | |
6490 | */ | |
a22285a6 YZ |
6491 | trans = btrfs_start_transaction(root, 5); |
6492 | if (IS_ERR(trans)) | |
6493 | return PTR_ERR(trans); | |
39279cc3 | 6494 | |
581bb050 LZ |
6495 | err = btrfs_find_free_ino(root, &objectid); |
6496 | if (err) | |
6497 | goto out_fail; | |
6498 | ||
aec7477b | 6499 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6500 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6501 | S_IFDIR | mode, &index); |
39279cc3 CM |
6502 | if (IS_ERR(inode)) { |
6503 | err = PTR_ERR(inode); | |
6504 | goto out_fail; | |
6505 | } | |
5f39d397 | 6506 | |
39279cc3 | 6507 | drop_on_err = 1; |
b0d5d10f CM |
6508 | /* these must be set before we unlock the inode */ |
6509 | inode->i_op = &btrfs_dir_inode_operations; | |
6510 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6511 | |
2a7dba39 | 6512 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6513 | if (err) |
b0d5d10f | 6514 | goto out_fail_inode; |
39279cc3 | 6515 | |
dbe674a9 | 6516 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
6517 | err = btrfs_update_inode(trans, root, inode); |
6518 | if (err) | |
b0d5d10f | 6519 | goto out_fail_inode; |
5f39d397 | 6520 | |
a1b075d2 JB |
6521 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
6522 | dentry->d_name.len, 0, index); | |
39279cc3 | 6523 | if (err) |
b0d5d10f | 6524 | goto out_fail_inode; |
5f39d397 | 6525 | |
39279cc3 | 6526 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6527 | /* |
6528 | * mkdir is special. We're unlocking after we call d_instantiate | |
6529 | * to avoid a race with nfsd calling d_instantiate. | |
6530 | */ | |
6531 | unlock_new_inode(inode); | |
39279cc3 | 6532 | drop_on_err = 0; |
39279cc3 CM |
6533 | |
6534 | out_fail: | |
7ad85bb7 | 6535 | btrfs_end_transaction(trans, root); |
c7cfb8a5 WS |
6536 | if (drop_on_err) { |
6537 | inode_dec_link_count(inode); | |
39279cc3 | 6538 | iput(inode); |
c7cfb8a5 | 6539 | } |
c581afc8 | 6540 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6541 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6542 | return err; |
b0d5d10f CM |
6543 | |
6544 | out_fail_inode: | |
6545 | unlock_new_inode(inode); | |
6546 | goto out_fail; | |
39279cc3 CM |
6547 | } |
6548 | ||
e6c4efd8 QW |
6549 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6550 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6551 | { | |
6552 | struct rb_node *next; | |
6553 | ||
6554 | next = rb_next(&em->rb_node); | |
6555 | if (!next) | |
6556 | return NULL; | |
6557 | return container_of(next, struct extent_map, rb_node); | |
6558 | } | |
6559 | ||
6560 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6561 | { | |
6562 | struct rb_node *prev; | |
6563 | ||
6564 | prev = rb_prev(&em->rb_node); | |
6565 | if (!prev) | |
6566 | return NULL; | |
6567 | return container_of(prev, struct extent_map, rb_node); | |
6568 | } | |
6569 | ||
d352ac68 | 6570 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6571 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6572 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6573 | * the best fitted new extent into the tree. |
d352ac68 | 6574 | */ |
3b951516 CM |
6575 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6576 | struct extent_map *existing, | |
e6dcd2dc | 6577 | struct extent_map *em, |
51f395ad | 6578 | u64 map_start) |
3b951516 | 6579 | { |
e6c4efd8 QW |
6580 | struct extent_map *prev; |
6581 | struct extent_map *next; | |
6582 | u64 start; | |
6583 | u64 end; | |
3b951516 | 6584 | u64 start_diff; |
3b951516 | 6585 | |
e6dcd2dc | 6586 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6587 | |
6588 | if (existing->start > map_start) { | |
6589 | next = existing; | |
6590 | prev = prev_extent_map(next); | |
6591 | } else { | |
6592 | prev = existing; | |
6593 | next = next_extent_map(prev); | |
6594 | } | |
6595 | ||
6596 | start = prev ? extent_map_end(prev) : em->start; | |
6597 | start = max_t(u64, start, em->start); | |
6598 | end = next ? next->start : extent_map_end(em); | |
6599 | end = min_t(u64, end, extent_map_end(em)); | |
6600 | start_diff = start - em->start; | |
6601 | em->start = start; | |
6602 | em->len = end - start; | |
c8b97818 CM |
6603 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6604 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6605 | em->block_start += start_diff; |
c8b97818 CM |
6606 | em->block_len -= start_diff; |
6607 | } | |
09a2a8f9 | 6608 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6609 | } |
6610 | ||
c8b97818 CM |
6611 | static noinline int uncompress_inline(struct btrfs_path *path, |
6612 | struct inode *inode, struct page *page, | |
6613 | size_t pg_offset, u64 extent_offset, | |
6614 | struct btrfs_file_extent_item *item) | |
6615 | { | |
6616 | int ret; | |
6617 | struct extent_buffer *leaf = path->nodes[0]; | |
6618 | char *tmp; | |
6619 | size_t max_size; | |
6620 | unsigned long inline_size; | |
6621 | unsigned long ptr; | |
261507a0 | 6622 | int compress_type; |
c8b97818 CM |
6623 | |
6624 | WARN_ON(pg_offset != 0); | |
261507a0 | 6625 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6626 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6627 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6628 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6629 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6630 | if (!tmp) |
6631 | return -ENOMEM; | |
c8b97818 CM |
6632 | ptr = btrfs_file_extent_inline_start(item); |
6633 | ||
6634 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6635 | ||
5b050f04 | 6636 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
261507a0 LZ |
6637 | ret = btrfs_decompress(compress_type, tmp, page, |
6638 | extent_offset, inline_size, max_size); | |
c8b97818 | 6639 | kfree(tmp); |
166ae5a4 | 6640 | return ret; |
c8b97818 CM |
6641 | } |
6642 | ||
d352ac68 CM |
6643 | /* |
6644 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6645 | * the ugly parts come from merging extents from the disk with the in-ram |
6646 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6647 | * where the in-ram extents might be locked pending data=ordered completion. |
6648 | * | |
6649 | * This also copies inline extents directly into the page. | |
6650 | */ | |
d397712b | 6651 | |
a52d9a80 | 6652 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 6653 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
6654 | int create) |
6655 | { | |
6656 | int ret; | |
6657 | int err = 0; | |
a52d9a80 CM |
6658 | u64 extent_start = 0; |
6659 | u64 extent_end = 0; | |
33345d01 | 6660 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6661 | u32 found_type; |
f421950f | 6662 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
6663 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6664 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
6665 | struct extent_buffer *leaf; |
6666 | struct btrfs_key found_key; | |
a52d9a80 CM |
6667 | struct extent_map *em = NULL; |
6668 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 6669 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 | 6670 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6671 | const bool new_inline = !page || create; |
a52d9a80 | 6672 | |
a52d9a80 | 6673 | again: |
890871be | 6674 | read_lock(&em_tree->lock); |
d1310b2e | 6675 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d CM |
6676 | if (em) |
6677 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
890871be | 6678 | read_unlock(&em_tree->lock); |
d1310b2e | 6679 | |
a52d9a80 | 6680 | if (em) { |
e1c4b745 CM |
6681 | if (em->start > start || em->start + em->len <= start) |
6682 | free_extent_map(em); | |
6683 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6684 | free_extent_map(em); |
6685 | else | |
6686 | goto out; | |
a52d9a80 | 6687 | } |
172ddd60 | 6688 | em = alloc_extent_map(); |
a52d9a80 | 6689 | if (!em) { |
d1310b2e CM |
6690 | err = -ENOMEM; |
6691 | goto out; | |
a52d9a80 | 6692 | } |
e6dcd2dc | 6693 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
d1310b2e | 6694 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6695 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6696 | em->len = (u64)-1; |
c8b97818 | 6697 | em->block_len = (u64)-1; |
f421950f CM |
6698 | |
6699 | if (!path) { | |
6700 | path = btrfs_alloc_path(); | |
026fd317 JB |
6701 | if (!path) { |
6702 | err = -ENOMEM; | |
6703 | goto out; | |
6704 | } | |
6705 | /* | |
6706 | * Chances are we'll be called again, so go ahead and do | |
6707 | * readahead | |
6708 | */ | |
6709 | path->reada = 1; | |
f421950f CM |
6710 | } |
6711 | ||
179e29e4 CM |
6712 | ret = btrfs_lookup_file_extent(trans, root, path, |
6713 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6714 | if (ret < 0) { |
6715 | err = ret; | |
6716 | goto out; | |
6717 | } | |
6718 | ||
6719 | if (ret != 0) { | |
6720 | if (path->slots[0] == 0) | |
6721 | goto not_found; | |
6722 | path->slots[0]--; | |
6723 | } | |
6724 | ||
5f39d397 CM |
6725 | leaf = path->nodes[0]; |
6726 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6727 | struct btrfs_file_extent_item); |
a52d9a80 | 6728 | /* are we inside the extent that was found? */ |
5f39d397 | 6729 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6730 | found_type = found_key.type; |
5f39d397 | 6731 | if (found_key.objectid != objectid || |
a52d9a80 | 6732 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6733 | /* |
6734 | * If we backup past the first extent we want to move forward | |
6735 | * and see if there is an extent in front of us, otherwise we'll | |
6736 | * say there is a hole for our whole search range which can | |
6737 | * cause problems. | |
6738 | */ | |
6739 | extent_end = start; | |
6740 | goto next; | |
a52d9a80 CM |
6741 | } |
6742 | ||
5f39d397 CM |
6743 | found_type = btrfs_file_extent_type(leaf, item); |
6744 | extent_start = found_key.offset; | |
d899e052 YZ |
6745 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6746 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6747 | extent_end = extent_start + |
db94535d | 6748 | btrfs_file_extent_num_bytes(leaf, item); |
9036c102 YZ |
6749 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6750 | size_t size; | |
514ac8ad | 6751 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
fda2832f | 6752 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
9036c102 | 6753 | } |
25a50341 | 6754 | next: |
9036c102 YZ |
6755 | if (start >= extent_end) { |
6756 | path->slots[0]++; | |
6757 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6758 | ret = btrfs_next_leaf(root, path); | |
6759 | if (ret < 0) { | |
6760 | err = ret; | |
6761 | goto out; | |
a52d9a80 | 6762 | } |
9036c102 YZ |
6763 | if (ret > 0) |
6764 | goto not_found; | |
6765 | leaf = path->nodes[0]; | |
a52d9a80 | 6766 | } |
9036c102 YZ |
6767 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6768 | if (found_key.objectid != objectid || | |
6769 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6770 | goto not_found; | |
6771 | if (start + len <= found_key.offset) | |
6772 | goto not_found; | |
e2eca69d WS |
6773 | if (start > found_key.offset) |
6774 | goto next; | |
9036c102 | 6775 | em->start = start; |
70c8a91c | 6776 | em->orig_start = start; |
9036c102 YZ |
6777 | em->len = found_key.offset - start; |
6778 | goto not_found_em; | |
6779 | } | |
6780 | ||
7ffbb598 FM |
6781 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
6782 | ||
d899e052 YZ |
6783 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6784 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
6785 | goto insert; |
6786 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 6787 | unsigned long ptr; |
a52d9a80 | 6788 | char *map; |
3326d1b0 CM |
6789 | size_t size; |
6790 | size_t extent_offset; | |
6791 | size_t copy_size; | |
a52d9a80 | 6792 | |
7ffbb598 | 6793 | if (new_inline) |
689f9346 | 6794 | goto out; |
5f39d397 | 6795 | |
514ac8ad | 6796 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 6797 | extent_offset = page_offset(page) + pg_offset - extent_start; |
70dec807 | 6798 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
3326d1b0 | 6799 | size - extent_offset); |
3326d1b0 | 6800 | em->start = extent_start + extent_offset; |
fda2832f | 6801 | em->len = ALIGN(copy_size, root->sectorsize); |
b4939680 | 6802 | em->orig_block_len = em->len; |
70c8a91c | 6803 | em->orig_start = em->start; |
689f9346 | 6804 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 6805 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
6806 | if (btrfs_file_extent_compression(leaf, item) != |
6807 | BTRFS_COMPRESS_NONE) { | |
c8b97818 CM |
6808 | ret = uncompress_inline(path, inode, page, |
6809 | pg_offset, | |
6810 | extent_offset, item); | |
166ae5a4 ZB |
6811 | if (ret) { |
6812 | err = ret; | |
6813 | goto out; | |
6814 | } | |
c8b97818 CM |
6815 | } else { |
6816 | map = kmap(page); | |
6817 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6818 | copy_size); | |
93c82d57 CM |
6819 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
6820 | memset(map + pg_offset + copy_size, 0, | |
6821 | PAGE_CACHE_SIZE - pg_offset - | |
6822 | copy_size); | |
6823 | } | |
c8b97818 CM |
6824 | kunmap(page); |
6825 | } | |
179e29e4 CM |
6826 | flush_dcache_page(page); |
6827 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 6828 | BUG(); |
179e29e4 CM |
6829 | if (!trans) { |
6830 | kunmap(page); | |
6831 | free_extent_map(em); | |
6832 | em = NULL; | |
ff5714cc | 6833 | |
b3b4aa74 | 6834 | btrfs_release_path(path); |
7a7eaa40 | 6835 | trans = btrfs_join_transaction(root); |
ff5714cc | 6836 | |
3612b495 TI |
6837 | if (IS_ERR(trans)) |
6838 | return ERR_CAST(trans); | |
179e29e4 CM |
6839 | goto again; |
6840 | } | |
c8b97818 | 6841 | map = kmap(page); |
70dec807 | 6842 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 6843 | copy_size); |
c8b97818 | 6844 | kunmap(page); |
179e29e4 | 6845 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 6846 | } |
d1310b2e | 6847 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6848 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6849 | goto insert; |
a52d9a80 CM |
6850 | } |
6851 | not_found: | |
6852 | em->start = start; | |
70c8a91c | 6853 | em->orig_start = start; |
d1310b2e | 6854 | em->len = len; |
a52d9a80 | 6855 | not_found_em: |
5f39d397 | 6856 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 6857 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 6858 | insert: |
b3b4aa74 | 6859 | btrfs_release_path(path); |
d1310b2e | 6860 | if (em->start > start || extent_map_end(em) <= start) { |
c2cf52eb | 6861 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
c1c9ff7c | 6862 | em->start, em->len, start, len); |
a52d9a80 CM |
6863 | err = -EIO; |
6864 | goto out; | |
6865 | } | |
d1310b2e CM |
6866 | |
6867 | err = 0; | |
890871be | 6868 | write_lock(&em_tree->lock); |
09a2a8f9 | 6869 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6870 | /* it is possible that someone inserted the extent into the tree |
6871 | * while we had the lock dropped. It is also possible that | |
6872 | * an overlapping map exists in the tree | |
6873 | */ | |
a52d9a80 | 6874 | if (ret == -EEXIST) { |
3b951516 | 6875 | struct extent_map *existing; |
e6dcd2dc CM |
6876 | |
6877 | ret = 0; | |
6878 | ||
e6c4efd8 QW |
6879 | existing = search_extent_mapping(em_tree, start, len); |
6880 | /* | |
6881 | * existing will always be non-NULL, since there must be | |
6882 | * extent causing the -EEXIST. | |
6883 | */ | |
6884 | if (start >= extent_map_end(existing) || | |
32be3a1a | 6885 | start <= existing->start) { |
e6c4efd8 QW |
6886 | /* |
6887 | * The existing extent map is the one nearest to | |
6888 | * the [start, start + len) range which overlaps | |
6889 | */ | |
6890 | err = merge_extent_mapping(em_tree, existing, | |
6891 | em, start); | |
e1c4b745 | 6892 | free_extent_map(existing); |
e6c4efd8 | 6893 | if (err) { |
3b951516 CM |
6894 | free_extent_map(em); |
6895 | em = NULL; | |
6896 | } | |
6897 | } else { | |
6898 | free_extent_map(em); | |
6899 | em = existing; | |
e6dcd2dc | 6900 | err = 0; |
a52d9a80 | 6901 | } |
a52d9a80 | 6902 | } |
890871be | 6903 | write_unlock(&em_tree->lock); |
a52d9a80 | 6904 | out: |
1abe9b8a | 6905 | |
4cd8587c | 6906 | trace_btrfs_get_extent(root, em); |
1abe9b8a | 6907 | |
527afb44 | 6908 | btrfs_free_path(path); |
a52d9a80 CM |
6909 | if (trans) { |
6910 | ret = btrfs_end_transaction(trans, root); | |
d397712b | 6911 | if (!err) |
a52d9a80 CM |
6912 | err = ret; |
6913 | } | |
a52d9a80 CM |
6914 | if (err) { |
6915 | free_extent_map(em); | |
a52d9a80 CM |
6916 | return ERR_PTR(err); |
6917 | } | |
79787eaa | 6918 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
6919 | return em; |
6920 | } | |
6921 | ||
ec29ed5b CM |
6922 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
6923 | size_t pg_offset, u64 start, u64 len, | |
6924 | int create) | |
6925 | { | |
6926 | struct extent_map *em; | |
6927 | struct extent_map *hole_em = NULL; | |
6928 | u64 range_start = start; | |
6929 | u64 end; | |
6930 | u64 found; | |
6931 | u64 found_end; | |
6932 | int err = 0; | |
6933 | ||
6934 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
6935 | if (IS_ERR(em)) | |
6936 | return em; | |
6937 | if (em) { | |
6938 | /* | |
f9e4fb53 LB |
6939 | * if our em maps to |
6940 | * - a hole or | |
6941 | * - a pre-alloc extent, | |
6942 | * there might actually be delalloc bytes behind it. | |
ec29ed5b | 6943 | */ |
f9e4fb53 LB |
6944 | if (em->block_start != EXTENT_MAP_HOLE && |
6945 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
ec29ed5b CM |
6946 | return em; |
6947 | else | |
6948 | hole_em = em; | |
6949 | } | |
6950 | ||
6951 | /* check to see if we've wrapped (len == -1 or similar) */ | |
6952 | end = start + len; | |
6953 | if (end < start) | |
6954 | end = (u64)-1; | |
6955 | else | |
6956 | end -= 1; | |
6957 | ||
6958 | em = NULL; | |
6959 | ||
6960 | /* ok, we didn't find anything, lets look for delalloc */ | |
6961 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, | |
6962 | end, len, EXTENT_DELALLOC, 1); | |
6963 | found_end = range_start + found; | |
6964 | if (found_end < range_start) | |
6965 | found_end = (u64)-1; | |
6966 | ||
6967 | /* | |
6968 | * we didn't find anything useful, return | |
6969 | * the original results from get_extent() | |
6970 | */ | |
6971 | if (range_start > end || found_end <= start) { | |
6972 | em = hole_em; | |
6973 | hole_em = NULL; | |
6974 | goto out; | |
6975 | } | |
6976 | ||
6977 | /* adjust the range_start to make sure it doesn't | |
6978 | * go backwards from the start they passed in | |
6979 | */ | |
67871254 | 6980 | range_start = max(start, range_start); |
ec29ed5b CM |
6981 | found = found_end - range_start; |
6982 | ||
6983 | if (found > 0) { | |
6984 | u64 hole_start = start; | |
6985 | u64 hole_len = len; | |
6986 | ||
172ddd60 | 6987 | em = alloc_extent_map(); |
ec29ed5b CM |
6988 | if (!em) { |
6989 | err = -ENOMEM; | |
6990 | goto out; | |
6991 | } | |
6992 | /* | |
6993 | * when btrfs_get_extent can't find anything it | |
6994 | * returns one huge hole | |
6995 | * | |
6996 | * make sure what it found really fits our range, and | |
6997 | * adjust to make sure it is based on the start from | |
6998 | * the caller | |
6999 | */ | |
7000 | if (hole_em) { | |
7001 | u64 calc_end = extent_map_end(hole_em); | |
7002 | ||
7003 | if (calc_end <= start || (hole_em->start > end)) { | |
7004 | free_extent_map(hole_em); | |
7005 | hole_em = NULL; | |
7006 | } else { | |
7007 | hole_start = max(hole_em->start, start); | |
7008 | hole_len = calc_end - hole_start; | |
7009 | } | |
7010 | } | |
7011 | em->bdev = NULL; | |
7012 | if (hole_em && range_start > hole_start) { | |
7013 | /* our hole starts before our delalloc, so we | |
7014 | * have to return just the parts of the hole | |
7015 | * that go until the delalloc starts | |
7016 | */ | |
7017 | em->len = min(hole_len, | |
7018 | range_start - hole_start); | |
7019 | em->start = hole_start; | |
7020 | em->orig_start = hole_start; | |
7021 | /* | |
7022 | * don't adjust block start at all, | |
7023 | * it is fixed at EXTENT_MAP_HOLE | |
7024 | */ | |
7025 | em->block_start = hole_em->block_start; | |
7026 | em->block_len = hole_len; | |
f9e4fb53 LB |
7027 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7028 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7029 | } else { |
7030 | em->start = range_start; | |
7031 | em->len = found; | |
7032 | em->orig_start = range_start; | |
7033 | em->block_start = EXTENT_MAP_DELALLOC; | |
7034 | em->block_len = found; | |
7035 | } | |
7036 | } else if (hole_em) { | |
7037 | return hole_em; | |
7038 | } | |
7039 | out: | |
7040 | ||
7041 | free_extent_map(hole_em); | |
7042 | if (err) { | |
7043 | free_extent_map(em); | |
7044 | return ERR_PTR(err); | |
7045 | } | |
7046 | return em; | |
7047 | } | |
7048 | ||
4b46fce2 JB |
7049 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7050 | u64 start, u64 len) | |
7051 | { | |
7052 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
70c8a91c | 7053 | struct extent_map *em; |
4b46fce2 JB |
7054 | struct btrfs_key ins; |
7055 | u64 alloc_hint; | |
7056 | int ret; | |
4b46fce2 | 7057 | |
4b46fce2 | 7058 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
00361589 | 7059 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
e570fd27 | 7060 | alloc_hint, &ins, 1, 1); |
00361589 JB |
7061 | if (ret) |
7062 | return ERR_PTR(ret); | |
4b46fce2 | 7063 | |
70c8a91c | 7064 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
cc95bef6 | 7065 | ins.offset, ins.offset, ins.offset, 0); |
00361589 | 7066 | if (IS_ERR(em)) { |
e570fd27 | 7067 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
00361589 JB |
7068 | return em; |
7069 | } | |
4b46fce2 JB |
7070 | |
7071 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, | |
7072 | ins.offset, ins.offset, 0); | |
7073 | if (ret) { | |
e570fd27 | 7074 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
00361589 JB |
7075 | free_extent_map(em); |
7076 | return ERR_PTR(ret); | |
4b46fce2 | 7077 | } |
00361589 | 7078 | |
4b46fce2 JB |
7079 | return em; |
7080 | } | |
7081 | ||
46bfbb5c CM |
7082 | /* |
7083 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7084 | * block must be cow'd | |
7085 | */ | |
00361589 | 7086 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7087 | u64 *orig_start, u64 *orig_block_len, |
7088 | u64 *ram_bytes) | |
46bfbb5c | 7089 | { |
00361589 | 7090 | struct btrfs_trans_handle *trans; |
46bfbb5c CM |
7091 | struct btrfs_path *path; |
7092 | int ret; | |
7093 | struct extent_buffer *leaf; | |
7094 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7095 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7096 | struct btrfs_file_extent_item *fi; |
7097 | struct btrfs_key key; | |
7098 | u64 disk_bytenr; | |
7099 | u64 backref_offset; | |
7100 | u64 extent_end; | |
7101 | u64 num_bytes; | |
7102 | int slot; | |
7103 | int found_type; | |
7ee9e440 | 7104 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7105 | |
46bfbb5c CM |
7106 | path = btrfs_alloc_path(); |
7107 | if (!path) | |
7108 | return -ENOMEM; | |
7109 | ||
00361589 | 7110 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
46bfbb5c CM |
7111 | offset, 0); |
7112 | if (ret < 0) | |
7113 | goto out; | |
7114 | ||
7115 | slot = path->slots[0]; | |
7116 | if (ret == 1) { | |
7117 | if (slot == 0) { | |
7118 | /* can't find the item, must cow */ | |
7119 | ret = 0; | |
7120 | goto out; | |
7121 | } | |
7122 | slot--; | |
7123 | } | |
7124 | ret = 0; | |
7125 | leaf = path->nodes[0]; | |
7126 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
33345d01 | 7127 | if (key.objectid != btrfs_ino(inode) || |
46bfbb5c CM |
7128 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7129 | /* not our file or wrong item type, must cow */ | |
7130 | goto out; | |
7131 | } | |
7132 | ||
7133 | if (key.offset > offset) { | |
7134 | /* Wrong offset, must cow */ | |
7135 | goto out; | |
7136 | } | |
7137 | ||
7138 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7139 | found_type = btrfs_file_extent_type(leaf, fi); | |
7140 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7141 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7142 | /* not a regular extent, must cow */ | |
7143 | goto out; | |
7144 | } | |
7ee9e440 JB |
7145 | |
7146 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7147 | goto out; | |
7148 | ||
e77751aa MX |
7149 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7150 | if (extent_end <= offset) | |
7151 | goto out; | |
7152 | ||
46bfbb5c | 7153 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7154 | if (disk_bytenr == 0) |
7155 | goto out; | |
7156 | ||
7157 | if (btrfs_file_extent_compression(leaf, fi) || | |
7158 | btrfs_file_extent_encryption(leaf, fi) || | |
7159 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7160 | goto out; | |
7161 | ||
46bfbb5c CM |
7162 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7163 | ||
7ee9e440 JB |
7164 | if (orig_start) { |
7165 | *orig_start = key.offset - backref_offset; | |
7166 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7167 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7168 | } | |
eb384b55 | 7169 | |
46bfbb5c CM |
7170 | if (btrfs_extent_readonly(root, disk_bytenr)) |
7171 | goto out; | |
7b2b7085 MX |
7172 | |
7173 | num_bytes = min(offset + *len, extent_end) - offset; | |
7174 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7175 | u64 range_end; | |
7176 | ||
7177 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; | |
7178 | ret = test_range_bit(io_tree, offset, range_end, | |
7179 | EXTENT_DELALLOC, 0, NULL); | |
7180 | if (ret) { | |
7181 | ret = -EAGAIN; | |
7182 | goto out; | |
7183 | } | |
7184 | } | |
7185 | ||
1bda19eb | 7186 | btrfs_release_path(path); |
46bfbb5c CM |
7187 | |
7188 | /* | |
7189 | * look for other files referencing this extent, if we | |
7190 | * find any we must cow | |
7191 | */ | |
00361589 JB |
7192 | trans = btrfs_join_transaction(root); |
7193 | if (IS_ERR(trans)) { | |
7194 | ret = 0; | |
46bfbb5c | 7195 | goto out; |
00361589 JB |
7196 | } |
7197 | ||
7198 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), | |
7199 | key.offset - backref_offset, disk_bytenr); | |
7200 | btrfs_end_transaction(trans, root); | |
7201 | if (ret) { | |
7202 | ret = 0; | |
7203 | goto out; | |
7204 | } | |
46bfbb5c CM |
7205 | |
7206 | /* | |
7207 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7208 | * in this extent we are about to write. If there | |
7209 | * are any csums in that range we have to cow in order | |
7210 | * to keep the csums correct | |
7211 | */ | |
7212 | disk_bytenr += backref_offset; | |
7213 | disk_bytenr += offset - key.offset; | |
46bfbb5c CM |
7214 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
7215 | goto out; | |
7216 | /* | |
7217 | * all of the above have passed, it is safe to overwrite this extent | |
7218 | * without cow | |
7219 | */ | |
eb384b55 | 7220 | *len = num_bytes; |
46bfbb5c CM |
7221 | ret = 1; |
7222 | out: | |
7223 | btrfs_free_path(path); | |
7224 | return ret; | |
7225 | } | |
7226 | ||
fc4adbff AG |
7227 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7228 | { | |
7229 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
7230 | int found = false; | |
7231 | void **pagep = NULL; | |
7232 | struct page *page = NULL; | |
7233 | int start_idx; | |
7234 | int end_idx; | |
7235 | ||
7236 | start_idx = start >> PAGE_CACHE_SHIFT; | |
7237 | ||
7238 | /* | |
7239 | * end is the last byte in the last page. end == start is legal | |
7240 | */ | |
7241 | end_idx = end >> PAGE_CACHE_SHIFT; | |
7242 | ||
7243 | rcu_read_lock(); | |
7244 | ||
7245 | /* Most of the code in this while loop is lifted from | |
7246 | * find_get_page. It's been modified to begin searching from a | |
7247 | * page and return just the first page found in that range. If the | |
7248 | * found idx is less than or equal to the end idx then we know that | |
7249 | * a page exists. If no pages are found or if those pages are | |
7250 | * outside of the range then we're fine (yay!) */ | |
7251 | while (page == NULL && | |
7252 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7253 | page = radix_tree_deref_slot(pagep); | |
7254 | if (unlikely(!page)) | |
7255 | break; | |
7256 | ||
7257 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7258 | if (radix_tree_deref_retry(page)) { |
7259 | page = NULL; | |
fc4adbff | 7260 | continue; |
809f9016 | 7261 | } |
fc4adbff AG |
7262 | /* |
7263 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7264 | * here as an exceptional entry: so return it without | |
7265 | * attempting to raise page count. | |
7266 | */ | |
6fdef6d4 | 7267 | page = NULL; |
fc4adbff AG |
7268 | break; /* TODO: Is this relevant for this use case? */ |
7269 | } | |
7270 | ||
91405151 FM |
7271 | if (!page_cache_get_speculative(page)) { |
7272 | page = NULL; | |
fc4adbff | 7273 | continue; |
91405151 | 7274 | } |
fc4adbff AG |
7275 | |
7276 | /* | |
7277 | * Has the page moved? | |
7278 | * This is part of the lockless pagecache protocol. See | |
7279 | * include/linux/pagemap.h for details. | |
7280 | */ | |
7281 | if (unlikely(page != *pagep)) { | |
7282 | page_cache_release(page); | |
7283 | page = NULL; | |
7284 | } | |
7285 | } | |
7286 | ||
7287 | if (page) { | |
7288 | if (page->index <= end_idx) | |
7289 | found = true; | |
7290 | page_cache_release(page); | |
7291 | } | |
7292 | ||
7293 | rcu_read_unlock(); | |
7294 | return found; | |
7295 | } | |
7296 | ||
eb838e73 JB |
7297 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7298 | struct extent_state **cached_state, int writing) | |
7299 | { | |
7300 | struct btrfs_ordered_extent *ordered; | |
7301 | int ret = 0; | |
7302 | ||
7303 | while (1) { | |
7304 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7305 | 0, cached_state); | |
7306 | /* | |
7307 | * We're concerned with the entire range that we're going to be | |
7308 | * doing DIO to, so we need to make sure theres no ordered | |
7309 | * extents in this range. | |
7310 | */ | |
7311 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | |
7312 | lockend - lockstart + 1); | |
7313 | ||
7314 | /* | |
7315 | * We need to make sure there are no buffered pages in this | |
7316 | * range either, we could have raced between the invalidate in | |
7317 | * generic_file_direct_write and locking the extent. The | |
7318 | * invalidate needs to happen so that reads after a write do not | |
7319 | * get stale data. | |
7320 | */ | |
fc4adbff AG |
7321 | if (!ordered && |
7322 | (!writing || | |
7323 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7324 | break; |
7325 | ||
7326 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7327 | cached_state, GFP_NOFS); | |
7328 | ||
7329 | if (ordered) { | |
7330 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7331 | btrfs_put_ordered_extent(ordered); | |
7332 | } else { | |
7333 | /* Screw you mmap */ | |
728404da | 7334 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
075bdbdb FM |
7335 | if (ret) |
7336 | break; | |
7337 | ret = filemap_fdatawait_range(inode->i_mapping, | |
7338 | lockstart, | |
7339 | lockend); | |
eb838e73 JB |
7340 | if (ret) |
7341 | break; | |
7342 | ||
7343 | /* | |
7344 | * If we found a page that couldn't be invalidated just | |
7345 | * fall back to buffered. | |
7346 | */ | |
7347 | ret = invalidate_inode_pages2_range(inode->i_mapping, | |
7348 | lockstart >> PAGE_CACHE_SHIFT, | |
7349 | lockend >> PAGE_CACHE_SHIFT); | |
7350 | if (ret) | |
7351 | break; | |
7352 | } | |
7353 | ||
7354 | cond_resched(); | |
7355 | } | |
7356 | ||
7357 | return ret; | |
7358 | } | |
7359 | ||
69ffb543 JB |
7360 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
7361 | u64 len, u64 orig_start, | |
7362 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
7363 | u64 orig_block_len, u64 ram_bytes, |
7364 | int type) | |
69ffb543 JB |
7365 | { |
7366 | struct extent_map_tree *em_tree; | |
7367 | struct extent_map *em; | |
7368 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7369 | int ret; | |
7370 | ||
7371 | em_tree = &BTRFS_I(inode)->extent_tree; | |
7372 | em = alloc_extent_map(); | |
7373 | if (!em) | |
7374 | return ERR_PTR(-ENOMEM); | |
7375 | ||
7376 | em->start = start; | |
7377 | em->orig_start = orig_start; | |
2ab28f32 JB |
7378 | em->mod_start = start; |
7379 | em->mod_len = len; | |
69ffb543 JB |
7380 | em->len = len; |
7381 | em->block_len = block_len; | |
7382 | em->block_start = block_start; | |
7383 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7384 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7385 | em->ram_bytes = ram_bytes; |
70c8a91c | 7386 | em->generation = -1; |
69ffb543 JB |
7387 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
7388 | if (type == BTRFS_ORDERED_PREALLOC) | |
b11e234d | 7389 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
69ffb543 JB |
7390 | |
7391 | do { | |
7392 | btrfs_drop_extent_cache(inode, em->start, | |
7393 | em->start + em->len - 1, 0); | |
7394 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7395 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 JB |
7396 | write_unlock(&em_tree->lock); |
7397 | } while (ret == -EEXIST); | |
7398 | ||
7399 | if (ret) { | |
7400 | free_extent_map(em); | |
7401 | return ERR_PTR(ret); | |
7402 | } | |
7403 | ||
7404 | return em; | |
7405 | } | |
7406 | ||
7407 | ||
4b46fce2 JB |
7408 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7409 | struct buffer_head *bh_result, int create) | |
7410 | { | |
7411 | struct extent_map *em; | |
7412 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
eb838e73 | 7413 | struct extent_state *cached_state = NULL; |
4b46fce2 | 7414 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7415 | u64 lockstart, lockend; |
4b46fce2 | 7416 | u64 len = bh_result->b_size; |
e1cbbfa5 | 7417 | u64 *outstanding_extents = NULL; |
eb838e73 | 7418 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7419 | int ret = 0; |
eb838e73 | 7420 | |
172a5049 | 7421 | if (create) |
3266789f | 7422 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7423 | else |
c329861d | 7424 | len = min_t(u64, len, root->sectorsize); |
eb838e73 | 7425 | |
c329861d JB |
7426 | lockstart = start; |
7427 | lockend = start + len - 1; | |
7428 | ||
e1cbbfa5 JB |
7429 | if (current->journal_info) { |
7430 | /* | |
7431 | * Need to pull our outstanding extents and set journal_info to NULL so | |
7432 | * that anything that needs to check if there's a transction doesn't get | |
7433 | * confused. | |
7434 | */ | |
7435 | outstanding_extents = current->journal_info; | |
7436 | current->journal_info = NULL; | |
7437 | } | |
7438 | ||
eb838e73 JB |
7439 | /* |
7440 | * If this errors out it's because we couldn't invalidate pagecache for | |
7441 | * this range and we need to fallback to buffered. | |
7442 | */ | |
7443 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) | |
7444 | return -ENOTBLK; | |
7445 | ||
4b46fce2 | 7446 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
eb838e73 JB |
7447 | if (IS_ERR(em)) { |
7448 | ret = PTR_ERR(em); | |
7449 | goto unlock_err; | |
7450 | } | |
4b46fce2 JB |
7451 | |
7452 | /* | |
7453 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7454 | * io. INLINE is special, and we could probably kludge it in here, but | |
7455 | * it's still buffered so for safety lets just fall back to the generic | |
7456 | * buffered path. | |
7457 | * | |
7458 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7459 | * decompress it, so there will be buffering required no matter what we | |
7460 | * do, so go ahead and fallback to buffered. | |
7461 | * | |
7462 | * We return -ENOTBLK because thats what makes DIO go ahead and go back | |
7463 | * to buffered IO. Don't blame me, this is the price we pay for using | |
7464 | * the generic code. | |
7465 | */ | |
7466 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7467 | em->block_start == EXTENT_MAP_INLINE) { | |
7468 | free_extent_map(em); | |
eb838e73 JB |
7469 | ret = -ENOTBLK; |
7470 | goto unlock_err; | |
4b46fce2 JB |
7471 | } |
7472 | ||
7473 | /* Just a good old fashioned hole, return */ | |
7474 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7475 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7476 | free_extent_map(em); | |
eb838e73 | 7477 | goto unlock_err; |
4b46fce2 JB |
7478 | } |
7479 | ||
7480 | /* | |
7481 | * We don't allocate a new extent in the following cases | |
7482 | * | |
7483 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7484 | * existing extent. | |
7485 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7486 | * just use the extent. | |
7487 | * | |
7488 | */ | |
46bfbb5c | 7489 | if (!create) { |
eb838e73 JB |
7490 | len = min(len, em->len - (start - em->start)); |
7491 | lockstart = start + len; | |
7492 | goto unlock; | |
46bfbb5c | 7493 | } |
4b46fce2 JB |
7494 | |
7495 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7496 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7497 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7498 | int type; |
eb384b55 | 7499 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7500 | |
7501 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7502 | type = BTRFS_ORDERED_PREALLOC; | |
7503 | else | |
7504 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7505 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7506 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7507 | |
00361589 | 7508 | if (can_nocow_extent(inode, start, &len, &orig_start, |
7ee9e440 | 7509 | &orig_block_len, &ram_bytes) == 1) { |
69ffb543 JB |
7510 | if (type == BTRFS_ORDERED_PREALLOC) { |
7511 | free_extent_map(em); | |
7512 | em = create_pinned_em(inode, start, len, | |
7513 | orig_start, | |
b4939680 | 7514 | block_start, len, |
cc95bef6 JB |
7515 | orig_block_len, |
7516 | ram_bytes, type); | |
555e1286 FM |
7517 | if (IS_ERR(em)) { |
7518 | ret = PTR_ERR(em); | |
69ffb543 | 7519 | goto unlock_err; |
555e1286 | 7520 | } |
69ffb543 JB |
7521 | } |
7522 | ||
46bfbb5c CM |
7523 | ret = btrfs_add_ordered_extent_dio(inode, start, |
7524 | block_start, len, len, type); | |
46bfbb5c CM |
7525 | if (ret) { |
7526 | free_extent_map(em); | |
eb838e73 | 7527 | goto unlock_err; |
46bfbb5c CM |
7528 | } |
7529 | goto unlock; | |
4b46fce2 | 7530 | } |
4b46fce2 | 7531 | } |
00361589 | 7532 | |
46bfbb5c CM |
7533 | /* |
7534 | * this will cow the extent, reset the len in case we changed | |
7535 | * it above | |
7536 | */ | |
7537 | len = bh_result->b_size; | |
70c8a91c JB |
7538 | free_extent_map(em); |
7539 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7540 | if (IS_ERR(em)) { |
7541 | ret = PTR_ERR(em); | |
7542 | goto unlock_err; | |
7543 | } | |
46bfbb5c CM |
7544 | len = min(len, em->len - (start - em->start)); |
7545 | unlock: | |
4b46fce2 JB |
7546 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7547 | inode->i_blkbits; | |
46bfbb5c | 7548 | bh_result->b_size = len; |
4b46fce2 JB |
7549 | bh_result->b_bdev = em->bdev; |
7550 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7551 | if (create) { |
7552 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7553 | set_buffer_new(bh_result); | |
7554 | ||
7555 | /* | |
7556 | * Need to update the i_size under the extent lock so buffered | |
7557 | * readers will get the updated i_size when we unlock. | |
7558 | */ | |
7559 | if (start + len > i_size_read(inode)) | |
7560 | i_size_write(inode, start + len); | |
0934856d | 7561 | |
e1cbbfa5 JB |
7562 | /* |
7563 | * If we have an outstanding_extents count still set then we're | |
7564 | * within our reservation, otherwise we need to adjust our inode | |
7565 | * counter appropriately. | |
7566 | */ | |
7567 | if (*outstanding_extents) { | |
7568 | (*outstanding_extents)--; | |
7569 | } else { | |
3e05bde8 JB |
7570 | spin_lock(&BTRFS_I(inode)->lock); |
7571 | BTRFS_I(inode)->outstanding_extents++; | |
7572 | spin_unlock(&BTRFS_I(inode)->lock); | |
7573 | } | |
e1cbbfa5 JB |
7574 | |
7575 | current->journal_info = outstanding_extents; | |
3266789f | 7576 | btrfs_free_reserved_data_space(inode, len); |
ddba1bfc | 7577 | set_bit(BTRFS_INODE_DIO_READY, &BTRFS_I(inode)->runtime_flags); |
c3473e83 | 7578 | } |
4b46fce2 | 7579 | |
eb838e73 JB |
7580 | /* |
7581 | * In the case of write we need to clear and unlock the entire range, | |
7582 | * in the case of read we need to unlock only the end area that we | |
7583 | * aren't using if there is any left over space. | |
7584 | */ | |
24c03fa5 | 7585 | if (lockstart < lockend) { |
0934856d MX |
7586 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7587 | lockend, unlock_bits, 1, 0, | |
7588 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7589 | } else { |
eb838e73 | 7590 | free_extent_state(cached_state); |
24c03fa5 | 7591 | } |
eb838e73 | 7592 | |
4b46fce2 JB |
7593 | free_extent_map(em); |
7594 | ||
7595 | return 0; | |
eb838e73 JB |
7596 | |
7597 | unlock_err: | |
eb838e73 JB |
7598 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7599 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
e1cbbfa5 JB |
7600 | if (outstanding_extents) |
7601 | current->journal_info = outstanding_extents; | |
eb838e73 | 7602 | return ret; |
4b46fce2 JB |
7603 | } |
7604 | ||
8b110e39 MX |
7605 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
7606 | int rw, int mirror_num) | |
7607 | { | |
7608 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7609 | int ret; | |
7610 | ||
7611 | BUG_ON(rw & REQ_WRITE); | |
7612 | ||
7613 | bio_get(bio); | |
7614 | ||
7615 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, | |
7616 | BTRFS_WQ_ENDIO_DIO_REPAIR); | |
7617 | if (ret) | |
7618 | goto err; | |
7619 | ||
7620 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); | |
7621 | err: | |
7622 | bio_put(bio); | |
7623 | return ret; | |
7624 | } | |
7625 | ||
7626 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7627 | struct bio *failed_bio, | |
7628 | struct io_failure_record *failrec, | |
7629 | int failed_mirror) | |
7630 | { | |
7631 | int num_copies; | |
7632 | ||
7633 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, | |
7634 | failrec->logical, failrec->len); | |
7635 | if (num_copies == 1) { | |
7636 | /* | |
7637 | * we only have a single copy of the data, so don't bother with | |
7638 | * all the retry and error correction code that follows. no | |
7639 | * matter what the error is, it is very likely to persist. | |
7640 | */ | |
7641 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7642 | num_copies, failrec->this_mirror, failed_mirror); | |
7643 | return 0; | |
7644 | } | |
7645 | ||
7646 | failrec->failed_mirror = failed_mirror; | |
7647 | failrec->this_mirror++; | |
7648 | if (failrec->this_mirror == failed_mirror) | |
7649 | failrec->this_mirror++; | |
7650 | ||
7651 | if (failrec->this_mirror > num_copies) { | |
7652 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7653 | num_copies, failrec->this_mirror, failed_mirror); | |
7654 | return 0; | |
7655 | } | |
7656 | ||
7657 | return 1; | |
7658 | } | |
7659 | ||
7660 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, | |
7661 | struct page *page, u64 start, u64 end, | |
7662 | int failed_mirror, bio_end_io_t *repair_endio, | |
7663 | void *repair_arg) | |
7664 | { | |
7665 | struct io_failure_record *failrec; | |
7666 | struct bio *bio; | |
7667 | int isector; | |
7668 | int read_mode; | |
7669 | int ret; | |
7670 | ||
7671 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); | |
7672 | ||
7673 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7674 | if (ret) | |
7675 | return ret; | |
7676 | ||
7677 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7678 | failed_mirror); | |
7679 | if (!ret) { | |
7680 | free_io_failure(inode, failrec); | |
7681 | return -EIO; | |
7682 | } | |
7683 | ||
7684 | if (failed_bio->bi_vcnt > 1) | |
7685 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; | |
7686 | else | |
7687 | read_mode = READ_SYNC; | |
7688 | ||
7689 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
7690 | isector >>= inode->i_sb->s_blocksize_bits; | |
7691 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
7692 | 0, isector, repair_endio, repair_arg); | |
7693 | if (!bio) { | |
7694 | free_io_failure(inode, failrec); | |
7695 | return -EIO; | |
7696 | } | |
7697 | ||
7698 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
7699 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", | |
7700 | read_mode, failrec->this_mirror, failrec->in_validation); | |
7701 | ||
7702 | ret = submit_dio_repair_bio(inode, bio, read_mode, | |
7703 | failrec->this_mirror); | |
7704 | if (ret) { | |
7705 | free_io_failure(inode, failrec); | |
7706 | bio_put(bio); | |
7707 | } | |
7708 | ||
7709 | return ret; | |
7710 | } | |
7711 | ||
7712 | struct btrfs_retry_complete { | |
7713 | struct completion done; | |
7714 | struct inode *inode; | |
7715 | u64 start; | |
7716 | int uptodate; | |
7717 | }; | |
7718 | ||
7719 | static void btrfs_retry_endio_nocsum(struct bio *bio, int err) | |
7720 | { | |
7721 | struct btrfs_retry_complete *done = bio->bi_private; | |
7722 | struct bio_vec *bvec; | |
7723 | int i; | |
7724 | ||
7725 | if (err) | |
7726 | goto end; | |
7727 | ||
7728 | done->uptodate = 1; | |
7729 | bio_for_each_segment_all(bvec, bio, i) | |
7730 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); | |
7731 | end: | |
7732 | complete(&done->done); | |
7733 | bio_put(bio); | |
7734 | } | |
7735 | ||
7736 | static int __btrfs_correct_data_nocsum(struct inode *inode, | |
7737 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 7738 | { |
2c30c71b | 7739 | struct bio_vec *bvec; |
8b110e39 | 7740 | struct btrfs_retry_complete done; |
4b46fce2 | 7741 | u64 start; |
2c30c71b | 7742 | int i; |
c1dc0896 | 7743 | int ret; |
4b46fce2 | 7744 | |
8b110e39 MX |
7745 | start = io_bio->logical; |
7746 | done.inode = inode; | |
7747 | ||
7748 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { | |
7749 | try_again: | |
7750 | done.uptodate = 0; | |
7751 | done.start = start; | |
7752 | init_completion(&done.done); | |
7753 | ||
7754 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, | |
7755 | start + bvec->bv_len - 1, | |
7756 | io_bio->mirror_num, | |
7757 | btrfs_retry_endio_nocsum, &done); | |
7758 | if (ret) | |
7759 | return ret; | |
7760 | ||
7761 | wait_for_completion(&done.done); | |
7762 | ||
7763 | if (!done.uptodate) { | |
7764 | /* We might have another mirror, so try again */ | |
7765 | goto try_again; | |
7766 | } | |
7767 | ||
7768 | start += bvec->bv_len; | |
7769 | } | |
7770 | ||
7771 | return 0; | |
7772 | } | |
7773 | ||
7774 | static void btrfs_retry_endio(struct bio *bio, int err) | |
7775 | { | |
7776 | struct btrfs_retry_complete *done = bio->bi_private; | |
7777 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7778 | struct bio_vec *bvec; | |
7779 | int uptodate; | |
7780 | int ret; | |
7781 | int i; | |
7782 | ||
7783 | if (err) | |
7784 | goto end; | |
7785 | ||
7786 | uptodate = 1; | |
7787 | bio_for_each_segment_all(bvec, bio, i) { | |
7788 | ret = __readpage_endio_check(done->inode, io_bio, i, | |
7789 | bvec->bv_page, 0, | |
7790 | done->start, bvec->bv_len); | |
7791 | if (!ret) | |
7792 | clean_io_failure(done->inode, done->start, | |
7793 | bvec->bv_page, 0); | |
7794 | else | |
7795 | uptodate = 0; | |
7796 | } | |
7797 | ||
7798 | done->uptodate = uptodate; | |
7799 | end: | |
7800 | complete(&done->done); | |
7801 | bio_put(bio); | |
7802 | } | |
7803 | ||
7804 | static int __btrfs_subio_endio_read(struct inode *inode, | |
7805 | struct btrfs_io_bio *io_bio, int err) | |
7806 | { | |
7807 | struct bio_vec *bvec; | |
7808 | struct btrfs_retry_complete done; | |
7809 | u64 start; | |
7810 | u64 offset = 0; | |
7811 | int i; | |
7812 | int ret; | |
dc380aea | 7813 | |
8b110e39 | 7814 | err = 0; |
c1dc0896 | 7815 | start = io_bio->logical; |
8b110e39 MX |
7816 | done.inode = inode; |
7817 | ||
c1dc0896 | 7818 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
dc380aea MX |
7819 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
7820 | 0, start, bvec->bv_len); | |
8b110e39 MX |
7821 | if (likely(!ret)) |
7822 | goto next; | |
7823 | try_again: | |
7824 | done.uptodate = 0; | |
7825 | done.start = start; | |
7826 | init_completion(&done.done); | |
7827 | ||
7828 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, | |
7829 | start + bvec->bv_len - 1, | |
7830 | io_bio->mirror_num, | |
7831 | btrfs_retry_endio, &done); | |
7832 | if (ret) { | |
7833 | err = ret; | |
7834 | goto next; | |
7835 | } | |
7836 | ||
7837 | wait_for_completion(&done.done); | |
7838 | ||
7839 | if (!done.uptodate) { | |
7840 | /* We might have another mirror, so try again */ | |
7841 | goto try_again; | |
7842 | } | |
7843 | next: | |
7844 | offset += bvec->bv_len; | |
4b46fce2 | 7845 | start += bvec->bv_len; |
2c30c71b | 7846 | } |
c1dc0896 MX |
7847 | |
7848 | return err; | |
7849 | } | |
7850 | ||
8b110e39 MX |
7851 | static int btrfs_subio_endio_read(struct inode *inode, |
7852 | struct btrfs_io_bio *io_bio, int err) | |
7853 | { | |
7854 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
7855 | ||
7856 | if (skip_csum) { | |
7857 | if (unlikely(err)) | |
7858 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
7859 | else | |
7860 | return 0; | |
7861 | } else { | |
7862 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
7863 | } | |
7864 | } | |
7865 | ||
c1dc0896 MX |
7866 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
7867 | { | |
7868 | struct btrfs_dio_private *dip = bio->bi_private; | |
7869 | struct inode *inode = dip->inode; | |
7870 | struct bio *dio_bio; | |
7871 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7872 | ||
8b110e39 MX |
7873 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
7874 | err = btrfs_subio_endio_read(inode, io_bio, err); | |
c1dc0896 | 7875 | |
4b46fce2 | 7876 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 7877 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 7878 | dio_bio = dip->dio_bio; |
4b46fce2 | 7879 | |
4b46fce2 | 7880 | kfree(dip); |
c0da7aa1 JB |
7881 | |
7882 | /* If we had a csum failure make sure to clear the uptodate flag */ | |
7883 | if (err) | |
9be3395b CM |
7884 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
7885 | dio_end_io(dio_bio, err); | |
23ea8e5a MX |
7886 | |
7887 | if (io_bio->end_io) | |
7888 | io_bio->end_io(io_bio, err); | |
9be3395b | 7889 | bio_put(bio); |
4b46fce2 JB |
7890 | } |
7891 | ||
7892 | static void btrfs_endio_direct_write(struct bio *bio, int err) | |
7893 | { | |
7894 | struct btrfs_dio_private *dip = bio->bi_private; | |
7895 | struct inode *inode = dip->inode; | |
7896 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b46fce2 | 7897 | struct btrfs_ordered_extent *ordered = NULL; |
163cf09c CM |
7898 | u64 ordered_offset = dip->logical_offset; |
7899 | u64 ordered_bytes = dip->bytes; | |
9be3395b | 7900 | struct bio *dio_bio; |
4b46fce2 JB |
7901 | int ret; |
7902 | ||
163cf09c CM |
7903 | again: |
7904 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
7905 | &ordered_offset, | |
5fd02043 | 7906 | ordered_bytes, !err); |
4b46fce2 | 7907 | if (!ret) |
163cf09c | 7908 | goto out_test; |
4b46fce2 | 7909 | |
9e0af237 LB |
7910 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
7911 | finish_ordered_fn, NULL, NULL); | |
fccb5d86 QW |
7912 | btrfs_queue_work(root->fs_info->endio_write_workers, |
7913 | &ordered->work); | |
163cf09c CM |
7914 | out_test: |
7915 | /* | |
7916 | * our bio might span multiple ordered extents. If we haven't | |
7917 | * completed the accounting for the whole dio, go back and try again | |
7918 | */ | |
7919 | if (ordered_offset < dip->logical_offset + dip->bytes) { | |
7920 | ordered_bytes = dip->logical_offset + dip->bytes - | |
7921 | ordered_offset; | |
5fd02043 | 7922 | ordered = NULL; |
163cf09c CM |
7923 | goto again; |
7924 | } | |
9be3395b | 7925 | dio_bio = dip->dio_bio; |
4b46fce2 | 7926 | |
4b46fce2 | 7927 | kfree(dip); |
c0da7aa1 JB |
7928 | |
7929 | /* If we had an error make sure to clear the uptodate flag */ | |
7930 | if (err) | |
9be3395b CM |
7931 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
7932 | dio_end_io(dio_bio, err); | |
7933 | bio_put(bio); | |
4b46fce2 JB |
7934 | } |
7935 | ||
eaf25d93 CM |
7936 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
7937 | struct bio *bio, int mirror_num, | |
7938 | unsigned long bio_flags, u64 offset) | |
7939 | { | |
7940 | int ret; | |
7941 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7942 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); | |
79787eaa | 7943 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
7944 | return 0; |
7945 | } | |
7946 | ||
e65e1535 MX |
7947 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
7948 | { | |
7949 | struct btrfs_dio_private *dip = bio->bi_private; | |
7950 | ||
8b110e39 MX |
7951 | if (err) |
7952 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
7953 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", | |
7954 | btrfs_ino(dip->inode), bio->bi_rw, | |
7955 | (unsigned long long)bio->bi_iter.bi_sector, | |
7956 | bio->bi_iter.bi_size, err); | |
7957 | ||
7958 | if (dip->subio_endio) | |
7959 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
7960 | |
7961 | if (err) { | |
e65e1535 MX |
7962 | dip->errors = 1; |
7963 | ||
7964 | /* | |
7965 | * before atomic variable goto zero, we must make sure | |
7966 | * dip->errors is perceived to be set. | |
7967 | */ | |
4e857c58 | 7968 | smp_mb__before_atomic(); |
e65e1535 MX |
7969 | } |
7970 | ||
7971 | /* if there are more bios still pending for this dio, just exit */ | |
7972 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
7973 | goto out; | |
7974 | ||
9be3395b | 7975 | if (dip->errors) { |
e65e1535 | 7976 | bio_io_error(dip->orig_bio); |
9be3395b CM |
7977 | } else { |
7978 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); | |
e65e1535 MX |
7979 | bio_endio(dip->orig_bio, 0); |
7980 | } | |
7981 | out: | |
7982 | bio_put(bio); | |
7983 | } | |
7984 | ||
7985 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, | |
7986 | u64 first_sector, gfp_t gfp_flags) | |
7987 | { | |
7988 | int nr_vecs = bio_get_nr_vecs(bdev); | |
da2f0f74 CM |
7989 | struct bio *bio; |
7990 | bio = btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); | |
7991 | if (bio) | |
7992 | bio_associate_current(bio); | |
7993 | return bio; | |
e65e1535 MX |
7994 | } |
7995 | ||
c1dc0896 MX |
7996 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
7997 | struct inode *inode, | |
7998 | struct btrfs_dio_private *dip, | |
7999 | struct bio *bio, | |
8000 | u64 file_offset) | |
8001 | { | |
8002 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8003 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
8004 | int ret; | |
8005 | ||
8006 | /* | |
8007 | * We load all the csum data we need when we submit | |
8008 | * the first bio to reduce the csum tree search and | |
8009 | * contention. | |
8010 | */ | |
8011 | if (dip->logical_offset == file_offset) { | |
8012 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, | |
8013 | file_offset); | |
8014 | if (ret) | |
8015 | return ret; | |
8016 | } | |
8017 | ||
8018 | if (bio == dip->orig_bio) | |
8019 | return 0; | |
8020 | ||
8021 | file_offset -= dip->logical_offset; | |
8022 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8023 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8024 | ||
8025 | return 0; | |
8026 | } | |
8027 | ||
e65e1535 MX |
8028 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
8029 | int rw, u64 file_offset, int skip_sum, | |
c329861d | 8030 | int async_submit) |
e65e1535 | 8031 | { |
facc8a22 | 8032 | struct btrfs_dio_private *dip = bio->bi_private; |
e65e1535 MX |
8033 | int write = rw & REQ_WRITE; |
8034 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
8035 | int ret; | |
8036 | ||
b812ce28 JB |
8037 | if (async_submit) |
8038 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8039 | ||
e65e1535 | 8040 | bio_get(bio); |
5fd02043 JB |
8041 | |
8042 | if (!write) { | |
bfebd8b5 DS |
8043 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
8044 | BTRFS_WQ_ENDIO_DATA); | |
5fd02043 JB |
8045 | if (ret) |
8046 | goto err; | |
8047 | } | |
e65e1535 | 8048 | |
1ae39938 JB |
8049 | if (skip_sum) |
8050 | goto map; | |
8051 | ||
8052 | if (write && async_submit) { | |
e65e1535 MX |
8053 | ret = btrfs_wq_submit_bio(root->fs_info, |
8054 | inode, rw, bio, 0, 0, | |
8055 | file_offset, | |
8056 | __btrfs_submit_bio_start_direct_io, | |
8057 | __btrfs_submit_bio_done); | |
8058 | goto err; | |
1ae39938 JB |
8059 | } else if (write) { |
8060 | /* | |
8061 | * If we aren't doing async submit, calculate the csum of the | |
8062 | * bio now. | |
8063 | */ | |
8064 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); | |
8065 | if (ret) | |
8066 | goto err; | |
23ea8e5a | 8067 | } else { |
c1dc0896 MX |
8068 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
8069 | file_offset); | |
c2db1073 TI |
8070 | if (ret) |
8071 | goto err; | |
8072 | } | |
1ae39938 JB |
8073 | map: |
8074 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); | |
e65e1535 MX |
8075 | err: |
8076 | bio_put(bio); | |
8077 | return ret; | |
8078 | } | |
8079 | ||
8080 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, | |
8081 | int skip_sum) | |
8082 | { | |
8083 | struct inode *inode = dip->inode; | |
8084 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
e65e1535 MX |
8085 | struct bio *bio; |
8086 | struct bio *orig_bio = dip->orig_bio; | |
8087 | struct bio_vec *bvec = orig_bio->bi_io_vec; | |
4f024f37 | 8088 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 MX |
8089 | u64 file_offset = dip->logical_offset; |
8090 | u64 submit_len = 0; | |
8091 | u64 map_length; | |
8092 | int nr_pages = 0; | |
23ea8e5a | 8093 | int ret; |
1ae39938 | 8094 | int async_submit = 0; |
e65e1535 | 8095 | |
4f024f37 | 8096 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8097 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
e65e1535 | 8098 | &map_length, NULL, 0); |
7a5c3c9b | 8099 | if (ret) |
e65e1535 | 8100 | return -EIO; |
facc8a22 | 8101 | |
4f024f37 | 8102 | if (map_length >= orig_bio->bi_iter.bi_size) { |
02f57c7a | 8103 | bio = orig_bio; |
c1dc0896 | 8104 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8105 | goto submit; |
8106 | } | |
8107 | ||
53b381b3 | 8108 | /* async crcs make it difficult to collect full stripe writes. */ |
ffe2d203 | 8109 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8110 | async_submit = 0; |
8111 | else | |
8112 | async_submit = 1; | |
8113 | ||
02f57c7a JB |
8114 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
8115 | if (!bio) | |
8116 | return -ENOMEM; | |
7a5c3c9b | 8117 | |
02f57c7a JB |
8118 | bio->bi_private = dip; |
8119 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8120 | btrfs_io_bio(bio)->logical = file_offset; |
02f57c7a JB |
8121 | atomic_inc(&dip->pending_bios); |
8122 | ||
e65e1535 | 8123 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
ee39b432 | 8124 | if (map_length < submit_len + bvec->bv_len || |
e65e1535 | 8125 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
ee39b432 | 8126 | bvec->bv_offset) < bvec->bv_len) { |
e65e1535 MX |
8127 | /* |
8128 | * inc the count before we submit the bio so | |
8129 | * we know the end IO handler won't happen before | |
8130 | * we inc the count. Otherwise, the dip might get freed | |
8131 | * before we're done setting it up | |
8132 | */ | |
8133 | atomic_inc(&dip->pending_bios); | |
8134 | ret = __btrfs_submit_dio_bio(bio, inode, rw, | |
8135 | file_offset, skip_sum, | |
c329861d | 8136 | async_submit); |
e65e1535 MX |
8137 | if (ret) { |
8138 | bio_put(bio); | |
8139 | atomic_dec(&dip->pending_bios); | |
8140 | goto out_err; | |
8141 | } | |
8142 | ||
e65e1535 MX |
8143 | start_sector += submit_len >> 9; |
8144 | file_offset += submit_len; | |
8145 | ||
8146 | submit_len = 0; | |
8147 | nr_pages = 0; | |
8148 | ||
8149 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, | |
8150 | start_sector, GFP_NOFS); | |
8151 | if (!bio) | |
8152 | goto out_err; | |
8153 | bio->bi_private = dip; | |
8154 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8155 | btrfs_io_bio(bio)->logical = file_offset; |
e65e1535 | 8156 | |
4f024f37 | 8157 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8158 | ret = btrfs_map_block(root->fs_info, rw, |
3ec706c8 | 8159 | start_sector << 9, |
e65e1535 MX |
8160 | &map_length, NULL, 0); |
8161 | if (ret) { | |
8162 | bio_put(bio); | |
8163 | goto out_err; | |
8164 | } | |
8165 | } else { | |
8166 | submit_len += bvec->bv_len; | |
67871254 | 8167 | nr_pages++; |
e65e1535 MX |
8168 | bvec++; |
8169 | } | |
8170 | } | |
8171 | ||
02f57c7a | 8172 | submit: |
e65e1535 | 8173 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
c329861d | 8174 | async_submit); |
e65e1535 MX |
8175 | if (!ret) |
8176 | return 0; | |
8177 | ||
8178 | bio_put(bio); | |
8179 | out_err: | |
8180 | dip->errors = 1; | |
8181 | /* | |
8182 | * before atomic variable goto zero, we must | |
8183 | * make sure dip->errors is perceived to be set. | |
8184 | */ | |
4e857c58 | 8185 | smp_mb__before_atomic(); |
e65e1535 MX |
8186 | if (atomic_dec_and_test(&dip->pending_bios)) |
8187 | bio_io_error(dip->orig_bio); | |
8188 | ||
8189 | /* bio_end_io() will handle error, so we needn't return it */ | |
8190 | return 0; | |
8191 | } | |
8192 | ||
9be3395b CM |
8193 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
8194 | struct inode *inode, loff_t file_offset) | |
4b46fce2 | 8195 | { |
61de718f FM |
8196 | struct btrfs_dio_private *dip = NULL; |
8197 | struct bio *io_bio = NULL; | |
23ea8e5a | 8198 | struct btrfs_io_bio *btrfs_bio; |
4b46fce2 | 8199 | int skip_sum; |
7b6d91da | 8200 | int write = rw & REQ_WRITE; |
4b46fce2 JB |
8201 | int ret = 0; |
8202 | ||
8203 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8204 | ||
9be3395b | 8205 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
9be3395b CM |
8206 | if (!io_bio) { |
8207 | ret = -ENOMEM; | |
8208 | goto free_ordered; | |
8209 | } | |
8210 | ||
c1dc0896 | 8211 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8212 | if (!dip) { |
8213 | ret = -ENOMEM; | |
61de718f | 8214 | goto free_ordered; |
4b46fce2 | 8215 | } |
4b46fce2 | 8216 | |
9be3395b | 8217 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8218 | dip->inode = inode; |
8219 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8220 | dip->bytes = dio_bio->bi_iter.bi_size; |
8221 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
9be3395b | 8222 | io_bio->bi_private = dip; |
9be3395b CM |
8223 | dip->orig_bio = io_bio; |
8224 | dip->dio_bio = dio_bio; | |
e65e1535 | 8225 | atomic_set(&dip->pending_bios, 0); |
c1dc0896 MX |
8226 | btrfs_bio = btrfs_io_bio(io_bio); |
8227 | btrfs_bio->logical = file_offset; | |
4b46fce2 | 8228 | |
c1dc0896 | 8229 | if (write) { |
9be3395b | 8230 | io_bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8231 | } else { |
9be3395b | 8232 | io_bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8233 | dip->subio_endio = btrfs_subio_endio_read; |
8234 | } | |
4b46fce2 | 8235 | |
e65e1535 MX |
8236 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
8237 | if (!ret) | |
eaf25d93 | 8238 | return; |
9be3395b | 8239 | |
23ea8e5a MX |
8240 | if (btrfs_bio->end_io) |
8241 | btrfs_bio->end_io(btrfs_bio, ret); | |
9be3395b | 8242 | |
4b46fce2 JB |
8243 | free_ordered: |
8244 | /* | |
61de718f FM |
8245 | * If we arrived here it means either we failed to submit the dip |
8246 | * or we either failed to clone the dio_bio or failed to allocate the | |
8247 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8248 | * call bio_endio against our io_bio so that we get proper resource | |
8249 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8250 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8251 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8252 | */ |
61de718f FM |
8253 | if (io_bio && dip) { |
8254 | bio_endio(io_bio, ret); | |
8255 | /* | |
8256 | * The end io callbacks free our dip, do the final put on io_bio | |
8257 | * and all the cleanup and final put for dio_bio (through | |
8258 | * dio_end_io()). | |
8259 | */ | |
8260 | dip = NULL; | |
8261 | io_bio = NULL; | |
8262 | } else { | |
8263 | if (write) { | |
8264 | struct btrfs_ordered_extent *ordered; | |
8265 | ||
8266 | ordered = btrfs_lookup_ordered_extent(inode, | |
8267 | file_offset); | |
8268 | set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); | |
8269 | /* | |
8270 | * Decrements our ref on the ordered extent and removes | |
8271 | * the ordered extent from the inode's ordered tree, | |
8272 | * doing all the proper resource cleanup such as for the | |
8273 | * reserved space and waking up any waiters for this | |
8274 | * ordered extent (through btrfs_remove_ordered_extent). | |
8275 | */ | |
8276 | btrfs_finish_ordered_io(ordered); | |
8277 | } else { | |
8278 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, | |
8279 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
8280 | } | |
8281 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); | |
8282 | /* | |
8283 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8284 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8285 | */ | |
8286 | dio_end_io(dio_bio, ret); | |
4b46fce2 | 8287 | } |
61de718f FM |
8288 | if (io_bio) |
8289 | bio_put(io_bio); | |
8290 | kfree(dip); | |
4b46fce2 JB |
8291 | } |
8292 | ||
6f673763 | 8293 | static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb, |
28060d5d | 8294 | const struct iov_iter *iter, loff_t offset) |
5a5f79b5 CM |
8295 | { |
8296 | int seg; | |
a1b75f7d | 8297 | int i; |
5a5f79b5 CM |
8298 | unsigned blocksize_mask = root->sectorsize - 1; |
8299 | ssize_t retval = -EINVAL; | |
5a5f79b5 CM |
8300 | |
8301 | if (offset & blocksize_mask) | |
8302 | goto out; | |
8303 | ||
28060d5d AV |
8304 | if (iov_iter_alignment(iter) & blocksize_mask) |
8305 | goto out; | |
a1b75f7d | 8306 | |
28060d5d | 8307 | /* If this is a write we don't need to check anymore */ |
6f673763 | 8308 | if (iov_iter_rw(iter) == WRITE) |
28060d5d AV |
8309 | return 0; |
8310 | /* | |
8311 | * Check to make sure we don't have duplicate iov_base's in this | |
8312 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8313 | * when reading back. | |
8314 | */ | |
8315 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8316 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8317 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8318 | goto out; |
8319 | } | |
5a5f79b5 CM |
8320 | } |
8321 | retval = 0; | |
8322 | out: | |
8323 | return retval; | |
8324 | } | |
eb838e73 | 8325 | |
22c6186e OS |
8326 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, |
8327 | loff_t offset) | |
16432985 | 8328 | { |
4b46fce2 JB |
8329 | struct file *file = iocb->ki_filp; |
8330 | struct inode *inode = file->f_mapping->host; | |
e1cbbfa5 | 8331 | u64 outstanding_extents = 0; |
0934856d | 8332 | size_t count = 0; |
2e60a51e | 8333 | int flags = 0; |
38851cc1 MX |
8334 | bool wakeup = true; |
8335 | bool relock = false; | |
0934856d | 8336 | ssize_t ret; |
4b46fce2 | 8337 | |
6f673763 | 8338 | if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset)) |
5a5f79b5 | 8339 | return 0; |
3f7c579c | 8340 | |
fe0f07d0 | 8341 | inode_dio_begin(inode); |
4e857c58 | 8342 | smp_mb__after_atomic(); |
38851cc1 | 8343 | |
0e267c44 | 8344 | /* |
41bd9ca4 MX |
8345 | * The generic stuff only does filemap_write_and_wait_range, which |
8346 | * isn't enough if we've written compressed pages to this area, so | |
8347 | * we need to flush the dirty pages again to make absolutely sure | |
8348 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8349 | */ |
a6cbcd4a | 8350 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8351 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8352 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8353 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8354 | offset + count - 1); | |
0e267c44 | 8355 | |
6f673763 | 8356 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8357 | /* |
8358 | * If the write DIO is beyond the EOF, we need update | |
8359 | * the isize, but it is protected by i_mutex. So we can | |
8360 | * not unlock the i_mutex at this case. | |
8361 | */ | |
8362 | if (offset + count <= inode->i_size) { | |
8363 | mutex_unlock(&inode->i_mutex); | |
8364 | relock = true; | |
8365 | } | |
0934856d MX |
8366 | ret = btrfs_delalloc_reserve_space(inode, count); |
8367 | if (ret) | |
38851cc1 | 8368 | goto out; |
e1cbbfa5 JB |
8369 | outstanding_extents = div64_u64(count + |
8370 | BTRFS_MAX_EXTENT_SIZE - 1, | |
8371 | BTRFS_MAX_EXTENT_SIZE); | |
8372 | ||
8373 | /* | |
8374 | * We need to know how many extents we reserved so that we can | |
8375 | * do the accounting properly if we go over the number we | |
8376 | * originally calculated. Abuse current->journal_info for this. | |
8377 | */ | |
8378 | current->journal_info = &outstanding_extents; | |
ee39b432 DS |
8379 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8380 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8381 | inode_dio_end(inode); |
38851cc1 MX |
8382 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8383 | wakeup = false; | |
0934856d MX |
8384 | } |
8385 | ||
17f8c842 OS |
8386 | ret = __blockdev_direct_IO(iocb, inode, |
8387 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, | |
8388 | iter, offset, btrfs_get_blocks_direct, NULL, | |
8389 | btrfs_submit_direct, flags); | |
6f673763 | 8390 | if (iov_iter_rw(iter) == WRITE) { |
e1cbbfa5 | 8391 | current->journal_info = NULL; |
ddba1bfc LB |
8392 | if (ret < 0 && ret != -EIOCBQUEUED) { |
8393 | /* | |
8394 | * If the error comes from submitting stage, | |
8395 | * btrfs_get_blocsk_direct() has free'd data space, | |
8396 | * and metadata space will be handled by | |
8397 | * finish_ordered_fn, don't do that again to make | |
8398 | * sure bytes_may_use is correct. | |
8399 | */ | |
8400 | if (!test_and_clear_bit(BTRFS_INODE_DIO_READY, | |
8401 | &BTRFS_I(inode)->runtime_flags)) | |
8402 | btrfs_delalloc_release_space(inode, count); | |
8403 | } else if (ret >= 0 && (size_t)ret < count) | |
0934856d MX |
8404 | btrfs_delalloc_release_space(inode, |
8405 | count - (size_t)ret); | |
0934856d | 8406 | } |
38851cc1 | 8407 | out: |
2e60a51e | 8408 | if (wakeup) |
fe0f07d0 | 8409 | inode_dio_end(inode); |
38851cc1 MX |
8410 | if (relock) |
8411 | mutex_lock(&inode->i_mutex); | |
0934856d MX |
8412 | |
8413 | return ret; | |
16432985 CM |
8414 | } |
8415 | ||
05dadc09 TI |
8416 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8417 | ||
1506fcc8 YS |
8418 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8419 | __u64 start, __u64 len) | |
8420 | { | |
05dadc09 TI |
8421 | int ret; |
8422 | ||
8423 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8424 | if (ret) | |
8425 | return ret; | |
8426 | ||
ec29ed5b | 8427 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8428 | } |
8429 | ||
a52d9a80 | 8430 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8431 | { |
d1310b2e CM |
8432 | struct extent_io_tree *tree; |
8433 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8434 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8435 | } |
1832a6d5 | 8436 | |
a52d9a80 | 8437 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8438 | { |
d1310b2e | 8439 | struct extent_io_tree *tree; |
b888db2b CM |
8440 | |
8441 | ||
8442 | if (current->flags & PF_MEMALLOC) { | |
8443 | redirty_page_for_writepage(wbc, page); | |
8444 | unlock_page(page); | |
8445 | return 0; | |
8446 | } | |
d1310b2e | 8447 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
a52d9a80 | 8448 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
9ebefb18 CM |
8449 | } |
8450 | ||
48a3b636 ES |
8451 | static int btrfs_writepages(struct address_space *mapping, |
8452 | struct writeback_control *wbc) | |
b293f02e | 8453 | { |
d1310b2e | 8454 | struct extent_io_tree *tree; |
771ed689 | 8455 | |
d1310b2e | 8456 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8457 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8458 | } | |
8459 | ||
3ab2fb5a CM |
8460 | static int |
8461 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8462 | struct list_head *pages, unsigned nr_pages) | |
8463 | { | |
d1310b2e CM |
8464 | struct extent_io_tree *tree; |
8465 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8466 | return extent_readpages(tree, mapping, pages, nr_pages, |
8467 | btrfs_get_extent); | |
8468 | } | |
e6dcd2dc | 8469 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8470 | { |
d1310b2e CM |
8471 | struct extent_io_tree *tree; |
8472 | struct extent_map_tree *map; | |
a52d9a80 | 8473 | int ret; |
8c2383c3 | 8474 | |
d1310b2e CM |
8475 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8476 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8477 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8478 | if (ret == 1) { |
8479 | ClearPagePrivate(page); | |
8480 | set_page_private(page, 0); | |
8481 | page_cache_release(page); | |
39279cc3 | 8482 | } |
a52d9a80 | 8483 | return ret; |
39279cc3 CM |
8484 | } |
8485 | ||
e6dcd2dc CM |
8486 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8487 | { | |
98509cfc CM |
8488 | if (PageWriteback(page) || PageDirty(page)) |
8489 | return 0; | |
b335b003 | 8490 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
e6dcd2dc CM |
8491 | } |
8492 | ||
d47992f8 LC |
8493 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8494 | unsigned int length) | |
39279cc3 | 8495 | { |
5fd02043 | 8496 | struct inode *inode = page->mapping->host; |
d1310b2e | 8497 | struct extent_io_tree *tree; |
e6dcd2dc | 8498 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8499 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8500 | u64 page_start = page_offset(page); |
8501 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; | |
131e404a | 8502 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8503 | |
8b62b72b CM |
8504 | /* |
8505 | * we have the page locked, so new writeback can't start, | |
8506 | * and the dirty bit won't be cleared while we are here. | |
8507 | * | |
8508 | * Wait for IO on this page so that we can safely clear | |
8509 | * the PagePrivate2 bit and do ordered accounting | |
8510 | */ | |
e6dcd2dc | 8511 | wait_on_page_writeback(page); |
8b62b72b | 8512 | |
5fd02043 | 8513 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8514 | if (offset) { |
8515 | btrfs_releasepage(page, GFP_NOFS); | |
8516 | return; | |
8517 | } | |
131e404a FDBM |
8518 | |
8519 | if (!inode_evicting) | |
8520 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); | |
8521 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
e6dcd2dc | 8522 | if (ordered) { |
eb84ae03 CM |
8523 | /* |
8524 | * IO on this page will never be started, so we need | |
8525 | * to account for any ordered extents now | |
8526 | */ | |
131e404a FDBM |
8527 | if (!inode_evicting) |
8528 | clear_extent_bit(tree, page_start, page_end, | |
8529 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
8530 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | | |
8531 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8532 | GFP_NOFS); | |
8b62b72b CM |
8533 | /* |
8534 | * whoever cleared the private bit is responsible | |
8535 | * for the finish_ordered_io | |
8536 | */ | |
77cef2ec JB |
8537 | if (TestClearPagePrivate2(page)) { |
8538 | struct btrfs_ordered_inode_tree *tree; | |
8539 | u64 new_len; | |
8540 | ||
8541 | tree = &BTRFS_I(inode)->ordered_tree; | |
8542 | ||
8543 | spin_lock_irq(&tree->lock); | |
8544 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
8545 | new_len = page_start - ordered->file_offset; | |
8546 | if (new_len < ordered->truncated_len) | |
8547 | ordered->truncated_len = new_len; | |
8548 | spin_unlock_irq(&tree->lock); | |
8549 | ||
8550 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
8551 | page_start, | |
8552 | PAGE_CACHE_SIZE, 1)) | |
8553 | btrfs_finish_ordered_io(ordered); | |
8b62b72b | 8554 | } |
e6dcd2dc | 8555 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8556 | if (!inode_evicting) { |
8557 | cached_state = NULL; | |
8558 | lock_extent_bits(tree, page_start, page_end, 0, | |
8559 | &cached_state); | |
8560 | } | |
8561 | } | |
8562 | ||
8563 | if (!inode_evicting) { | |
8564 | clear_extent_bit(tree, page_start, page_end, | |
8565 | EXTENT_LOCKED | EXTENT_DIRTY | | |
8566 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
8567 | EXTENT_DEFRAG, 1, 1, | |
8568 | &cached_state, GFP_NOFS); | |
8569 | ||
8570 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8571 | } |
e6dcd2dc | 8572 | |
4a096752 | 8573 | ClearPageChecked(page); |
9ad6b7bc | 8574 | if (PagePrivate(page)) { |
9ad6b7bc CM |
8575 | ClearPagePrivate(page); |
8576 | set_page_private(page, 0); | |
8577 | page_cache_release(page); | |
8578 | } | |
39279cc3 CM |
8579 | } |
8580 | ||
9ebefb18 CM |
8581 | /* |
8582 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8583 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8584 | * be careful to check for EOF conditions here. We set the page up correctly | |
8585 | * for a written page which means we get ENOSPC checking when writing into | |
8586 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8587 | * support these features. | |
8588 | * | |
8589 | * We are not allowed to take the i_mutex here so we have to play games to | |
8590 | * protect against truncate races as the page could now be beyond EOF. Because | |
8591 | * vmtruncate() writes the inode size before removing pages, once we have the | |
8592 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
8593 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
8594 | * unlock the page. | |
8595 | */ | |
c2ec175c | 8596 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
9ebefb18 | 8597 | { |
c2ec175c | 8598 | struct page *page = vmf->page; |
496ad9aa | 8599 | struct inode *inode = file_inode(vma->vm_file); |
1832a6d5 | 8600 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
8601 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8602 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8603 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8604 | char *kaddr; |
8605 | unsigned long zero_start; | |
9ebefb18 | 8606 | loff_t size; |
1832a6d5 | 8607 | int ret; |
9998eb70 | 8608 | int reserved = 0; |
a52d9a80 | 8609 | u64 page_start; |
e6dcd2dc | 8610 | u64 page_end; |
9ebefb18 | 8611 | |
b2b5ef5c | 8612 | sb_start_pagefault(inode->i_sb); |
0ca1f7ce | 8613 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
9998eb70 | 8614 | if (!ret) { |
e41f941a | 8615 | ret = file_update_time(vma->vm_file); |
9998eb70 CM |
8616 | reserved = 1; |
8617 | } | |
56a76f82 NP |
8618 | if (ret) { |
8619 | if (ret == -ENOMEM) | |
8620 | ret = VM_FAULT_OOM; | |
8621 | else /* -ENOSPC, -EIO, etc */ | |
8622 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
8623 | if (reserved) |
8624 | goto out; | |
8625 | goto out_noreserve; | |
56a76f82 | 8626 | } |
1832a6d5 | 8627 | |
56a76f82 | 8628 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8629 | again: |
9ebefb18 | 8630 | lock_page(page); |
9ebefb18 | 8631 | size = i_size_read(inode); |
e6dcd2dc CM |
8632 | page_start = page_offset(page); |
8633 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
a52d9a80 | 8634 | |
9ebefb18 | 8635 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8636 | (page_start >= size)) { |
9ebefb18 CM |
8637 | /* page got truncated out from underneath us */ |
8638 | goto out_unlock; | |
8639 | } | |
e6dcd2dc CM |
8640 | wait_on_page_writeback(page); |
8641 | ||
d0082371 | 8642 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
8643 | set_page_extent_mapped(page); |
8644 | ||
eb84ae03 CM |
8645 | /* |
8646 | * we can't set the delalloc bits if there are pending ordered | |
8647 | * extents. Drop our locks and wait for them to finish | |
8648 | */ | |
e6dcd2dc CM |
8649 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
8650 | if (ordered) { | |
2ac55d41 JB |
8651 | unlock_extent_cached(io_tree, page_start, page_end, |
8652 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 8653 | unlock_page(page); |
eb84ae03 | 8654 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
8655 | btrfs_put_ordered_extent(ordered); |
8656 | goto again; | |
8657 | } | |
8658 | ||
fbf19087 JB |
8659 | /* |
8660 | * XXX - page_mkwrite gets called every time the page is dirtied, even | |
8661 | * if it was already dirty, so for space accounting reasons we need to | |
8662 | * clear any delalloc bits for the range we are fixing to save. There | |
8663 | * is probably a better way to do this, but for now keep consistent with | |
8664 | * prepare_pages in the normal write path. | |
8665 | */ | |
2ac55d41 | 8666 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
8667 | EXTENT_DIRTY | EXTENT_DELALLOC | |
8668 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 8669 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 8670 | |
2ac55d41 JB |
8671 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
8672 | &cached_state); | |
9ed74f2d | 8673 | if (ret) { |
2ac55d41 JB |
8674 | unlock_extent_cached(io_tree, page_start, page_end, |
8675 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
8676 | ret = VM_FAULT_SIGBUS; |
8677 | goto out_unlock; | |
8678 | } | |
e6dcd2dc | 8679 | ret = 0; |
9ebefb18 CM |
8680 | |
8681 | /* page is wholly or partially inside EOF */ | |
a52d9a80 | 8682 | if (page_start + PAGE_CACHE_SIZE > size) |
e6dcd2dc | 8683 | zero_start = size & ~PAGE_CACHE_MASK; |
9ebefb18 | 8684 | else |
e6dcd2dc | 8685 | zero_start = PAGE_CACHE_SIZE; |
9ebefb18 | 8686 | |
e6dcd2dc CM |
8687 | if (zero_start != PAGE_CACHE_SIZE) { |
8688 | kaddr = kmap(page); | |
8689 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); | |
8690 | flush_dcache_page(page); | |
8691 | kunmap(page); | |
8692 | } | |
247e743c | 8693 | ClearPageChecked(page); |
e6dcd2dc | 8694 | set_page_dirty(page); |
50a9b214 | 8695 | SetPageUptodate(page); |
5a3f23d5 | 8696 | |
257c62e1 CM |
8697 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
8698 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | |
46d8bc34 | 8699 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 8700 | |
2ac55d41 | 8701 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
8702 | |
8703 | out_unlock: | |
b2b5ef5c JK |
8704 | if (!ret) { |
8705 | sb_end_pagefault(inode->i_sb); | |
50a9b214 | 8706 | return VM_FAULT_LOCKED; |
b2b5ef5c | 8707 | } |
9ebefb18 | 8708 | unlock_page(page); |
1832a6d5 | 8709 | out: |
ec39e180 | 8710 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
9998eb70 | 8711 | out_noreserve: |
b2b5ef5c | 8712 | sb_end_pagefault(inode->i_sb); |
9ebefb18 CM |
8713 | return ret; |
8714 | } | |
8715 | ||
a41ad394 | 8716 | static int btrfs_truncate(struct inode *inode) |
39279cc3 CM |
8717 | { |
8718 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
fcb80c2a | 8719 | struct btrfs_block_rsv *rsv; |
a71754fc | 8720 | int ret = 0; |
3893e33b | 8721 | int err = 0; |
39279cc3 | 8722 | struct btrfs_trans_handle *trans; |
dbe674a9 | 8723 | u64 mask = root->sectorsize - 1; |
07127184 | 8724 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 | 8725 | |
0ef8b726 JB |
8726 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
8727 | (u64)-1); | |
8728 | if (ret) | |
8729 | return ret; | |
39279cc3 | 8730 | |
fcb80c2a JB |
8731 | /* |
8732 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have | |
8733 | * 3 things going on here | |
8734 | * | |
8735 | * 1) We need to reserve space for our orphan item and the space to | |
8736 | * delete our orphan item. Lord knows we don't want to have a dangling | |
8737 | * orphan item because we didn't reserve space to remove it. | |
8738 | * | |
8739 | * 2) We need to reserve space to update our inode. | |
8740 | * | |
8741 | * 3) We need to have something to cache all the space that is going to | |
8742 | * be free'd up by the truncate operation, but also have some slack | |
8743 | * space reserved in case it uses space during the truncate (thank you | |
8744 | * very much snapshotting). | |
8745 | * | |
8746 | * And we need these to all be seperate. The fact is we can use alot of | |
8747 | * space doing the truncate, and we have no earthly idea how much space | |
8748 | * we will use, so we need the truncate reservation to be seperate so it | |
8749 | * doesn't end up using space reserved for updating the inode or | |
8750 | * removing the orphan item. We also need to be able to stop the | |
8751 | * transaction and start a new one, which means we need to be able to | |
8752 | * update the inode several times, and we have no idea of knowing how | |
8753 | * many times that will be, so we can't just reserve 1 item for the | |
8754 | * entirety of the opration, so that has to be done seperately as well. | |
8755 | * Then there is the orphan item, which does indeed need to be held on | |
8756 | * to for the whole operation, and we need nobody to touch this reserved | |
8757 | * space except the orphan code. | |
8758 | * | |
8759 | * So that leaves us with | |
8760 | * | |
8761 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
8762 | * 2) rsv - for the truncate reservation, which we will steal from the | |
8763 | * transaction reservation. | |
8764 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
8765 | * updating the inode. | |
8766 | */ | |
66d8f3dd | 8767 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8768 | if (!rsv) |
8769 | return -ENOMEM; | |
4a338542 | 8770 | rsv->size = min_size; |
ca7e70f5 | 8771 | rsv->failfast = 1; |
f0cd846e | 8772 | |
907cbceb | 8773 | /* |
07127184 | 8774 | * 1 for the truncate slack space |
907cbceb JB |
8775 | * 1 for updating the inode. |
8776 | */ | |
f3fe820c | 8777 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
8778 | if (IS_ERR(trans)) { |
8779 | err = PTR_ERR(trans); | |
8780 | goto out; | |
8781 | } | |
f0cd846e | 8782 | |
907cbceb JB |
8783 | /* Migrate the slack space for the truncate to our reserve */ |
8784 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, | |
8785 | min_size); | |
fcb80c2a | 8786 | BUG_ON(ret); |
f0cd846e | 8787 | |
5dc562c5 JB |
8788 | /* |
8789 | * So if we truncate and then write and fsync we normally would just | |
8790 | * write the extents that changed, which is a problem if we need to | |
8791 | * first truncate that entire inode. So set this flag so we write out | |
8792 | * all of the extents in the inode to the sync log so we're completely | |
8793 | * safe. | |
8794 | */ | |
8795 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 8796 | trans->block_rsv = rsv; |
907cbceb | 8797 | |
8082510e YZ |
8798 | while (1) { |
8799 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
8800 | inode->i_size, | |
8801 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 8802 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 8803 | err = ret; |
8082510e | 8804 | break; |
3893e33b | 8805 | } |
39279cc3 | 8806 | |
fcb80c2a | 8807 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e | 8808 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
8809 | if (ret) { |
8810 | err = ret; | |
8811 | break; | |
8812 | } | |
ca7e70f5 | 8813 | |
8082510e | 8814 | btrfs_end_transaction(trans, root); |
b53d3f5d | 8815 | btrfs_btree_balance_dirty(root); |
ca7e70f5 JB |
8816 | |
8817 | trans = btrfs_start_transaction(root, 2); | |
8818 | if (IS_ERR(trans)) { | |
8819 | ret = err = PTR_ERR(trans); | |
8820 | trans = NULL; | |
8821 | break; | |
8822 | } | |
8823 | ||
8824 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, | |
8825 | rsv, min_size); | |
8826 | BUG_ON(ret); /* shouldn't happen */ | |
8827 | trans->block_rsv = rsv; | |
8082510e YZ |
8828 | } |
8829 | ||
8830 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 8831 | trans->block_rsv = root->orphan_block_rsv; |
8082510e | 8832 | ret = btrfs_orphan_del(trans, inode); |
3893e33b JB |
8833 | if (ret) |
8834 | err = ret; | |
8082510e YZ |
8835 | } |
8836 | ||
917c16b2 CM |
8837 | if (trans) { |
8838 | trans->block_rsv = &root->fs_info->trans_block_rsv; | |
8839 | ret = btrfs_update_inode(trans, root, inode); | |
8840 | if (ret && !err) | |
8841 | err = ret; | |
7b128766 | 8842 | |
7ad85bb7 | 8843 | ret = btrfs_end_transaction(trans, root); |
b53d3f5d | 8844 | btrfs_btree_balance_dirty(root); |
917c16b2 | 8845 | } |
fcb80c2a JB |
8846 | |
8847 | out: | |
8848 | btrfs_free_block_rsv(root, rsv); | |
8849 | ||
3893e33b JB |
8850 | if (ret && !err) |
8851 | err = ret; | |
a41ad394 | 8852 | |
3893e33b | 8853 | return err; |
39279cc3 CM |
8854 | } |
8855 | ||
d352ac68 CM |
8856 | /* |
8857 | * create a new subvolume directory/inode (helper for the ioctl). | |
8858 | */ | |
d2fb3437 | 8859 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
8860 | struct btrfs_root *new_root, |
8861 | struct btrfs_root *parent_root, | |
8862 | u64 new_dirid) | |
39279cc3 | 8863 | { |
39279cc3 | 8864 | struct inode *inode; |
76dda93c | 8865 | int err; |
00e4e6b3 | 8866 | u64 index = 0; |
39279cc3 | 8867 | |
12fc9d09 FA |
8868 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
8869 | new_dirid, new_dirid, | |
8870 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
8871 | &index); | |
54aa1f4d | 8872 | if (IS_ERR(inode)) |
f46b5a66 | 8873 | return PTR_ERR(inode); |
39279cc3 CM |
8874 | inode->i_op = &btrfs_dir_inode_operations; |
8875 | inode->i_fop = &btrfs_dir_file_operations; | |
8876 | ||
bfe86848 | 8877 | set_nlink(inode, 1); |
dbe674a9 | 8878 | btrfs_i_size_write(inode, 0); |
b0d5d10f | 8879 | unlock_new_inode(inode); |
3b96362c | 8880 | |
63541927 FDBM |
8881 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
8882 | if (err) | |
8883 | btrfs_err(new_root->fs_info, | |
351fd353 | 8884 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
8885 | new_root->root_key.objectid, err); |
8886 | ||
76dda93c | 8887 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 8888 | |
76dda93c | 8889 | iput(inode); |
ce598979 | 8890 | return err; |
39279cc3 CM |
8891 | } |
8892 | ||
39279cc3 CM |
8893 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8894 | { | |
8895 | struct btrfs_inode *ei; | |
2ead6ae7 | 8896 | struct inode *inode; |
39279cc3 CM |
8897 | |
8898 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
8899 | if (!ei) | |
8900 | return NULL; | |
2ead6ae7 YZ |
8901 | |
8902 | ei->root = NULL; | |
2ead6ae7 | 8903 | ei->generation = 0; |
15ee9bc7 | 8904 | ei->last_trans = 0; |
257c62e1 | 8905 | ei->last_sub_trans = 0; |
e02119d5 | 8906 | ei->logged_trans = 0; |
2ead6ae7 | 8907 | ei->delalloc_bytes = 0; |
47059d93 | 8908 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8909 | ei->disk_i_size = 0; |
8910 | ei->flags = 0; | |
7709cde3 | 8911 | ei->csum_bytes = 0; |
2ead6ae7 | 8912 | ei->index_cnt = (u64)-1; |
67de1176 | 8913 | ei->dir_index = 0; |
2ead6ae7 | 8914 | ei->last_unlink_trans = 0; |
46d8bc34 | 8915 | ei->last_log_commit = 0; |
2ead6ae7 | 8916 | |
9e0baf60 JB |
8917 | spin_lock_init(&ei->lock); |
8918 | ei->outstanding_extents = 0; | |
8919 | ei->reserved_extents = 0; | |
2ead6ae7 | 8920 | |
72ac3c0d | 8921 | ei->runtime_flags = 0; |
261507a0 | 8922 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8923 | |
16cdcec7 MX |
8924 | ei->delayed_node = NULL; |
8925 | ||
9cc97d64 | 8926 | ei->i_otime.tv_sec = 0; |
8927 | ei->i_otime.tv_nsec = 0; | |
8928 | ||
2ead6ae7 | 8929 | inode = &ei->vfs_inode; |
a8067e02 | 8930 | extent_map_tree_init(&ei->extent_tree); |
f993c883 DS |
8931 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
8932 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); | |
0b32f4bb JB |
8933 | ei->io_tree.track_uptodate = 1; |
8934 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 8935 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 8936 | mutex_init(&ei->log_mutex); |
f248679e | 8937 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 8938 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 8939 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
2ead6ae7 YZ |
8940 | RB_CLEAR_NODE(&ei->rb_node); |
8941 | ||
8942 | return inode; | |
39279cc3 CM |
8943 | } |
8944 | ||
aaedb55b JB |
8945 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8946 | void btrfs_test_destroy_inode(struct inode *inode) | |
8947 | { | |
8948 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
8949 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
8950 | } | |
8951 | #endif | |
8952 | ||
fa0d7e3d NP |
8953 | static void btrfs_i_callback(struct rcu_head *head) |
8954 | { | |
8955 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
8956 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8957 | } | |
8958 | ||
39279cc3 CM |
8959 | void btrfs_destroy_inode(struct inode *inode) |
8960 | { | |
e6dcd2dc | 8961 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
8962 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8963 | ||
b3d9b7a3 | 8964 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 8965 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
8966 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
8967 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 JB |
8968 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
8969 | WARN_ON(BTRFS_I(inode)->csum_bytes); | |
47059d93 | 8970 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 8971 | |
a6dbd429 JB |
8972 | /* |
8973 | * This can happen where we create an inode, but somebody else also | |
8974 | * created the same inode and we need to destroy the one we already | |
8975 | * created. | |
8976 | */ | |
8977 | if (!root) | |
8978 | goto free; | |
8979 | ||
8a35d95f JB |
8980 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8981 | &BTRFS_I(inode)->runtime_flags)) { | |
c2cf52eb | 8982 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
c1c9ff7c | 8983 | btrfs_ino(inode)); |
8a35d95f | 8984 | atomic_dec(&root->orphan_inodes); |
7b128766 | 8985 | } |
7b128766 | 8986 | |
d397712b | 8987 | while (1) { |
e6dcd2dc CM |
8988 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
8989 | if (!ordered) | |
8990 | break; | |
8991 | else { | |
c2cf52eb | 8992 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
c1c9ff7c | 8993 | ordered->file_offset, ordered->len); |
e6dcd2dc CM |
8994 | btrfs_remove_ordered_extent(inode, ordered); |
8995 | btrfs_put_ordered_extent(ordered); | |
8996 | btrfs_put_ordered_extent(ordered); | |
8997 | } | |
8998 | } | |
5d4f98a2 | 8999 | inode_tree_del(inode); |
5b21f2ed | 9000 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
a6dbd429 | 9001 | free: |
fa0d7e3d | 9002 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9003 | } |
9004 | ||
45321ac5 | 9005 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9006 | { |
9007 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9008 | |
6379ef9f NA |
9009 | if (root == NULL) |
9010 | return 1; | |
9011 | ||
fa6ac876 | 9012 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9013 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9014 | return 1; |
76dda93c | 9015 | else |
45321ac5 | 9016 | return generic_drop_inode(inode); |
76dda93c YZ |
9017 | } |
9018 | ||
0ee0fda0 | 9019 | static void init_once(void *foo) |
39279cc3 CM |
9020 | { |
9021 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9022 | ||
9023 | inode_init_once(&ei->vfs_inode); | |
9024 | } | |
9025 | ||
9026 | void btrfs_destroy_cachep(void) | |
9027 | { | |
8c0a8537 KS |
9028 | /* |
9029 | * Make sure all delayed rcu free inodes are flushed before we | |
9030 | * destroy cache. | |
9031 | */ | |
9032 | rcu_barrier(); | |
39279cc3 CM |
9033 | if (btrfs_inode_cachep) |
9034 | kmem_cache_destroy(btrfs_inode_cachep); | |
9035 | if (btrfs_trans_handle_cachep) | |
9036 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
9037 | if (btrfs_transaction_cachep) | |
9038 | kmem_cache_destroy(btrfs_transaction_cachep); | |
39279cc3 CM |
9039 | if (btrfs_path_cachep) |
9040 | kmem_cache_destroy(btrfs_path_cachep); | |
dc89e982 JB |
9041 | if (btrfs_free_space_cachep) |
9042 | kmem_cache_destroy(btrfs_free_space_cachep); | |
8ccf6f19 MX |
9043 | if (btrfs_delalloc_work_cachep) |
9044 | kmem_cache_destroy(btrfs_delalloc_work_cachep); | |
39279cc3 CM |
9045 | } |
9046 | ||
9047 | int btrfs_init_cachep(void) | |
9048 | { | |
837e1972 | 9049 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 CH |
9050 | sizeof(struct btrfs_inode), 0, |
9051 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); | |
39279cc3 CM |
9052 | if (!btrfs_inode_cachep) |
9053 | goto fail; | |
9601e3f6 | 9054 | |
837e1972 | 9055 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 CH |
9056 | sizeof(struct btrfs_trans_handle), 0, |
9057 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
9058 | if (!btrfs_trans_handle_cachep) |
9059 | goto fail; | |
9601e3f6 | 9060 | |
837e1972 | 9061 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
9601e3f6 CH |
9062 | sizeof(struct btrfs_transaction), 0, |
9063 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
9064 | if (!btrfs_transaction_cachep) |
9065 | goto fail; | |
9601e3f6 | 9066 | |
837e1972 | 9067 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 CH |
9068 | sizeof(struct btrfs_path), 0, |
9069 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
9070 | if (!btrfs_path_cachep) |
9071 | goto fail; | |
9601e3f6 | 9072 | |
837e1972 | 9073 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 JB |
9074 | sizeof(struct btrfs_free_space), 0, |
9075 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
9076 | if (!btrfs_free_space_cachep) | |
9077 | goto fail; | |
9078 | ||
8ccf6f19 MX |
9079 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
9080 | sizeof(struct btrfs_delalloc_work), 0, | |
9081 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, | |
9082 | NULL); | |
9083 | if (!btrfs_delalloc_work_cachep) | |
9084 | goto fail; | |
9085 | ||
39279cc3 CM |
9086 | return 0; |
9087 | fail: | |
9088 | btrfs_destroy_cachep(); | |
9089 | return -ENOMEM; | |
9090 | } | |
9091 | ||
9092 | static int btrfs_getattr(struct vfsmount *mnt, | |
9093 | struct dentry *dentry, struct kstat *stat) | |
9094 | { | |
df0af1a5 | 9095 | u64 delalloc_bytes; |
2b0143b5 | 9096 | struct inode *inode = d_inode(dentry); |
fadc0d8b DS |
9097 | u32 blocksize = inode->i_sb->s_blocksize; |
9098 | ||
39279cc3 | 9099 | generic_fillattr(inode, stat); |
0ee5dc67 | 9100 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
d6667462 | 9101 | stat->blksize = PAGE_CACHE_SIZE; |
df0af1a5 MX |
9102 | |
9103 | spin_lock(&BTRFS_I(inode)->lock); | |
9104 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; | |
9105 | spin_unlock(&BTRFS_I(inode)->lock); | |
fadc0d8b | 9106 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9107 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9108 | return 0; |
9109 | } | |
9110 | ||
d397712b CM |
9111 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
9112 | struct inode *new_dir, struct dentry *new_dentry) | |
39279cc3 CM |
9113 | { |
9114 | struct btrfs_trans_handle *trans; | |
9115 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
4df27c4d | 9116 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9117 | struct inode *new_inode = d_inode(new_dentry); |
9118 | struct inode *old_inode = d_inode(old_dentry); | |
39279cc3 | 9119 | struct timespec ctime = CURRENT_TIME; |
00e4e6b3 | 9120 | u64 index = 0; |
4df27c4d | 9121 | u64 root_objectid; |
39279cc3 | 9122 | int ret; |
33345d01 | 9123 | u64 old_ino = btrfs_ino(old_inode); |
39279cc3 | 9124 | |
33345d01 | 9125 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9126 | return -EPERM; |
9127 | ||
4df27c4d | 9128 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9129 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9130 | return -EXDEV; |
9131 | ||
33345d01 LZ |
9132 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
9133 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) | |
39279cc3 | 9134 | return -ENOTEMPTY; |
5f39d397 | 9135 | |
4df27c4d YZ |
9136 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9137 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9138 | return -ENOTEMPTY; | |
9c52057c CM |
9139 | |
9140 | ||
9141 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9142 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9143 | new_dentry->d_name.name, |
9144 | new_dentry->d_name.len); | |
9145 | ||
9146 | if (ret) { | |
9147 | if (ret == -EEXIST) { | |
9148 | /* we shouldn't get | |
9149 | * eexist without a new_inode */ | |
fae7f21c | 9150 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9151 | return ret; |
9152 | } | |
9153 | } else { | |
9154 | /* maybe -EOVERFLOW */ | |
9155 | return ret; | |
9156 | } | |
9157 | } | |
9158 | ret = 0; | |
9159 | ||
5a3f23d5 | 9160 | /* |
8d875f95 CM |
9161 | * we're using rename to replace one file with another. Start IO on it |
9162 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9163 | */ |
8d875f95 | 9164 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9165 | filemap_flush(old_inode->i_mapping); |
9166 | ||
76dda93c | 9167 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9168 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9169 | down_read(&root->fs_info->subvol_sem); |
a22285a6 YZ |
9170 | /* |
9171 | * We want to reserve the absolute worst case amount of items. So if | |
9172 | * both inodes are subvols and we need to unlink them then that would | |
9173 | * require 4 item modifications, but if they are both normal inodes it | |
9174 | * would require 5 item modifications, so we'll assume their normal | |
9175 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items | |
9176 | * should cover the worst case number of items we'll modify. | |
9177 | */ | |
6e137ed3 | 9178 | trans = btrfs_start_transaction(root, 11); |
b44c59a8 JL |
9179 | if (IS_ERR(trans)) { |
9180 | ret = PTR_ERR(trans); | |
9181 | goto out_notrans; | |
9182 | } | |
76dda93c | 9183 | |
4df27c4d YZ |
9184 | if (dest != root) |
9185 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9186 | |
a5719521 YZ |
9187 | ret = btrfs_set_inode_index(new_dir, &index); |
9188 | if (ret) | |
9189 | goto out_fail; | |
5a3f23d5 | 9190 | |
67de1176 | 9191 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9192 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9193 | /* force full log commit if subvolume involved. */ |
995946dd | 9194 | btrfs_set_log_full_commit(root->fs_info, trans); |
4df27c4d | 9195 | } else { |
a5719521 YZ |
9196 | ret = btrfs_insert_inode_ref(trans, dest, |
9197 | new_dentry->d_name.name, | |
9198 | new_dentry->d_name.len, | |
33345d01 LZ |
9199 | old_ino, |
9200 | btrfs_ino(new_dir), index); | |
a5719521 YZ |
9201 | if (ret) |
9202 | goto out_fail; | |
4df27c4d YZ |
9203 | /* |
9204 | * this is an ugly little race, but the rename is required | |
9205 | * to make sure that if we crash, the inode is either at the | |
9206 | * old name or the new one. pinning the log transaction lets | |
9207 | * us make sure we don't allow a log commit to come in after | |
9208 | * we unlink the name but before we add the new name back in. | |
9209 | */ | |
9210 | btrfs_pin_log_trans(root); | |
9211 | } | |
5a3f23d5 | 9212 | |
0c4d2d95 JB |
9213 | inode_inc_iversion(old_dir); |
9214 | inode_inc_iversion(new_dir); | |
9215 | inode_inc_iversion(old_inode); | |
39279cc3 CM |
9216 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
9217 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9218 | old_inode->i_ctime = ctime; | |
5f39d397 | 9219 | |
12fcfd22 CM |
9220 | if (old_dentry->d_parent != new_dentry->d_parent) |
9221 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | |
9222 | ||
33345d01 | 9223 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
9224 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
9225 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
9226 | old_dentry->d_name.name, | |
9227 | old_dentry->d_name.len); | |
9228 | } else { | |
92986796 | 9229 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
2b0143b5 | 9230 | d_inode(old_dentry), |
92986796 AV |
9231 | old_dentry->d_name.name, |
9232 | old_dentry->d_name.len); | |
9233 | if (!ret) | |
9234 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9235 | } |
79787eaa JM |
9236 | if (ret) { |
9237 | btrfs_abort_transaction(trans, root, ret); | |
9238 | goto out_fail; | |
9239 | } | |
39279cc3 CM |
9240 | |
9241 | if (new_inode) { | |
0c4d2d95 | 9242 | inode_inc_iversion(new_inode); |
39279cc3 | 9243 | new_inode->i_ctime = CURRENT_TIME; |
33345d01 | 9244 | if (unlikely(btrfs_ino(new_inode) == |
4df27c4d YZ |
9245 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9246 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
9247 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9248 | root_objectid, | |
9249 | new_dentry->d_name.name, | |
9250 | new_dentry->d_name.len); | |
9251 | BUG_ON(new_inode->i_nlink == 0); | |
9252 | } else { | |
9253 | ret = btrfs_unlink_inode(trans, dest, new_dir, | |
2b0143b5 | 9254 | d_inode(new_dentry), |
4df27c4d YZ |
9255 | new_dentry->d_name.name, |
9256 | new_dentry->d_name.len); | |
9257 | } | |
4ef31a45 | 9258 | if (!ret && new_inode->i_nlink == 0) |
2b0143b5 | 9259 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
79787eaa JM |
9260 | if (ret) { |
9261 | btrfs_abort_transaction(trans, root, ret); | |
9262 | goto out_fail; | |
9263 | } | |
39279cc3 | 9264 | } |
aec7477b | 9265 | |
4df27c4d YZ |
9266 | ret = btrfs_add_link(trans, new_dir, old_inode, |
9267 | new_dentry->d_name.name, | |
a5719521 | 9268 | new_dentry->d_name.len, 0, index); |
79787eaa JM |
9269 | if (ret) { |
9270 | btrfs_abort_transaction(trans, root, ret); | |
9271 | goto out_fail; | |
9272 | } | |
39279cc3 | 9273 | |
67de1176 MX |
9274 | if (old_inode->i_nlink == 1) |
9275 | BTRFS_I(old_inode)->dir_index = index; | |
9276 | ||
33345d01 | 9277 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
10d9f309 | 9278 | struct dentry *parent = new_dentry->d_parent; |
6a912213 | 9279 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
4df27c4d YZ |
9280 | btrfs_end_log_trans(root); |
9281 | } | |
39279cc3 | 9282 | out_fail: |
7ad85bb7 | 9283 | btrfs_end_transaction(trans, root); |
b44c59a8 | 9284 | out_notrans: |
33345d01 | 9285 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9286 | up_read(&root->fs_info->subvol_sem); |
9ed74f2d | 9287 | |
39279cc3 CM |
9288 | return ret; |
9289 | } | |
9290 | ||
80ace85c MS |
9291 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9292 | struct inode *new_dir, struct dentry *new_dentry, | |
9293 | unsigned int flags) | |
9294 | { | |
9295 | if (flags & ~RENAME_NOREPLACE) | |
9296 | return -EINVAL; | |
9297 | ||
9298 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); | |
9299 | } | |
9300 | ||
8ccf6f19 MX |
9301 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9302 | { | |
9303 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9304 | struct inode *inode; |
8ccf6f19 MX |
9305 | |
9306 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9307 | work); | |
9f23e289 JB |
9308 | inode = delalloc_work->inode; |
9309 | if (delalloc_work->wait) { | |
9310 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
9311 | } else { | |
9312 | filemap_flush(inode->i_mapping); | |
9313 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9314 | &BTRFS_I(inode)->runtime_flags)) | |
9315 | filemap_flush(inode->i_mapping); | |
9316 | } | |
8ccf6f19 MX |
9317 | |
9318 | if (delalloc_work->delay_iput) | |
9f23e289 | 9319 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 9320 | else |
9f23e289 | 9321 | iput(inode); |
8ccf6f19 MX |
9322 | complete(&delalloc_work->completion); |
9323 | } | |
9324 | ||
9325 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
9326 | int wait, int delay_iput) | |
9327 | { | |
9328 | struct btrfs_delalloc_work *work; | |
9329 | ||
9330 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); | |
9331 | if (!work) | |
9332 | return NULL; | |
9333 | ||
9334 | init_completion(&work->completion); | |
9335 | INIT_LIST_HEAD(&work->list); | |
9336 | work->inode = inode; | |
9337 | work->wait = wait; | |
9338 | work->delay_iput = delay_iput; | |
9e0af237 LB |
9339 | WARN_ON_ONCE(!inode); |
9340 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
9341 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
9342 | |
9343 | return work; | |
9344 | } | |
9345 | ||
9346 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
9347 | { | |
9348 | wait_for_completion(&work->completion); | |
9349 | kmem_cache_free(btrfs_delalloc_work_cachep, work); | |
9350 | } | |
9351 | ||
d352ac68 CM |
9352 | /* |
9353 | * some fairly slow code that needs optimization. This walks the list | |
9354 | * of all the inodes with pending delalloc and forces them to disk. | |
9355 | */ | |
6c255e67 MX |
9356 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
9357 | int nr) | |
ea8c2819 | 9358 | { |
ea8c2819 | 9359 | struct btrfs_inode *binode; |
5b21f2ed | 9360 | struct inode *inode; |
8ccf6f19 MX |
9361 | struct btrfs_delalloc_work *work, *next; |
9362 | struct list_head works; | |
1eafa6c7 | 9363 | struct list_head splice; |
8ccf6f19 | 9364 | int ret = 0; |
ea8c2819 | 9365 | |
8ccf6f19 | 9366 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9367 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9368 | |
573bfb72 | 9369 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9370 | spin_lock(&root->delalloc_lock); |
9371 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9372 | while (!list_empty(&splice)) { |
9373 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9374 | delalloc_inodes); |
1eafa6c7 | 9375 | |
eb73c1b7 MX |
9376 | list_move_tail(&binode->delalloc_inodes, |
9377 | &root->delalloc_inodes); | |
5b21f2ed | 9378 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9379 | if (!inode) { |
eb73c1b7 | 9380 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9381 | continue; |
df0af1a5 | 9382 | } |
eb73c1b7 | 9383 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 MX |
9384 | |
9385 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); | |
5d99a998 | 9386 | if (!work) { |
f4ab9ea7 JB |
9387 | if (delay_iput) |
9388 | btrfs_add_delayed_iput(inode); | |
9389 | else | |
9390 | iput(inode); | |
1eafa6c7 | 9391 | ret = -ENOMEM; |
a1ecaabb | 9392 | goto out; |
5b21f2ed | 9393 | } |
1eafa6c7 | 9394 | list_add_tail(&work->list, &works); |
a44903ab QW |
9395 | btrfs_queue_work(root->fs_info->flush_workers, |
9396 | &work->work); | |
6c255e67 MX |
9397 | ret++; |
9398 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 9399 | goto out; |
5b21f2ed | 9400 | cond_resched(); |
eb73c1b7 | 9401 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9402 | } |
eb73c1b7 | 9403 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9404 | |
a1ecaabb | 9405 | out: |
eb73c1b7 MX |
9406 | list_for_each_entry_safe(work, next, &works, list) { |
9407 | list_del_init(&work->list); | |
9408 | btrfs_wait_and_free_delalloc_work(work); | |
9409 | } | |
9410 | ||
9411 | if (!list_empty_careful(&splice)) { | |
9412 | spin_lock(&root->delalloc_lock); | |
9413 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9414 | spin_unlock(&root->delalloc_lock); | |
9415 | } | |
573bfb72 | 9416 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9417 | return ret; |
9418 | } | |
1eafa6c7 | 9419 | |
eb73c1b7 MX |
9420 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
9421 | { | |
9422 | int ret; | |
1eafa6c7 | 9423 | |
2c21b4d7 | 9424 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
eb73c1b7 MX |
9425 | return -EROFS; |
9426 | ||
6c255e67 MX |
9427 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
9428 | if (ret > 0) | |
9429 | ret = 0; | |
eb73c1b7 MX |
9430 | /* |
9431 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
9432 | * we have to make sure the IO is actually started and that |
9433 | * ordered extents get created before we return | |
9434 | */ | |
9435 | atomic_inc(&root->fs_info->async_submit_draining); | |
d397712b | 9436 | while (atomic_read(&root->fs_info->nr_async_submits) || |
771ed689 | 9437 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
8c8bee1d | 9438 | wait_event(root->fs_info->async_submit_wait, |
771ed689 CM |
9439 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
9440 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | |
8c8bee1d CM |
9441 | } |
9442 | atomic_dec(&root->fs_info->async_submit_draining); | |
eb73c1b7 MX |
9443 | return ret; |
9444 | } | |
9445 | ||
6c255e67 MX |
9446 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
9447 | int nr) | |
eb73c1b7 MX |
9448 | { |
9449 | struct btrfs_root *root; | |
9450 | struct list_head splice; | |
9451 | int ret; | |
9452 | ||
2c21b4d7 | 9453 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9454 | return -EROFS; |
9455 | ||
9456 | INIT_LIST_HEAD(&splice); | |
9457 | ||
573bfb72 | 9458 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9459 | spin_lock(&fs_info->delalloc_root_lock); |
9460 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 9461 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
9462 | root = list_first_entry(&splice, struct btrfs_root, |
9463 | delalloc_root); | |
9464 | root = btrfs_grab_fs_root(root); | |
9465 | BUG_ON(!root); | |
9466 | list_move_tail(&root->delalloc_root, | |
9467 | &fs_info->delalloc_roots); | |
9468 | spin_unlock(&fs_info->delalloc_root_lock); | |
9469 | ||
6c255e67 | 9470 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 9471 | btrfs_put_fs_root(root); |
6c255e67 | 9472 | if (ret < 0) |
eb73c1b7 MX |
9473 | goto out; |
9474 | ||
6c255e67 MX |
9475 | if (nr != -1) { |
9476 | nr -= ret; | |
9477 | WARN_ON(nr < 0); | |
9478 | } | |
eb73c1b7 | 9479 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9480 | } |
eb73c1b7 | 9481 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9482 | |
6c255e67 | 9483 | ret = 0; |
eb73c1b7 MX |
9484 | atomic_inc(&fs_info->async_submit_draining); |
9485 | while (atomic_read(&fs_info->nr_async_submits) || | |
9486 | atomic_read(&fs_info->async_delalloc_pages)) { | |
9487 | wait_event(fs_info->async_submit_wait, | |
9488 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
9489 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
9490 | } | |
9491 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 9492 | out: |
1eafa6c7 | 9493 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
9494 | spin_lock(&fs_info->delalloc_root_lock); |
9495 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9496 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9497 | } |
573bfb72 | 9498 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9499 | return ret; |
ea8c2819 CM |
9500 | } |
9501 | ||
39279cc3 CM |
9502 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
9503 | const char *symname) | |
9504 | { | |
9505 | struct btrfs_trans_handle *trans; | |
9506 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9507 | struct btrfs_path *path; | |
9508 | struct btrfs_key key; | |
1832a6d5 | 9509 | struct inode *inode = NULL; |
39279cc3 CM |
9510 | int err; |
9511 | int drop_inode = 0; | |
9512 | u64 objectid; | |
67871254 | 9513 | u64 index = 0; |
39279cc3 CM |
9514 | int name_len; |
9515 | int datasize; | |
5f39d397 | 9516 | unsigned long ptr; |
39279cc3 | 9517 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9518 | struct extent_buffer *leaf; |
39279cc3 | 9519 | |
f06becc4 | 9520 | name_len = strlen(symname); |
39279cc3 CM |
9521 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
9522 | return -ENAMETOOLONG; | |
1832a6d5 | 9523 | |
9ed74f2d JB |
9524 | /* |
9525 | * 2 items for inode item and ref | |
9526 | * 2 items for dir items | |
9527 | * 1 item for xattr if selinux is on | |
9528 | */ | |
a22285a6 YZ |
9529 | trans = btrfs_start_transaction(root, 5); |
9530 | if (IS_ERR(trans)) | |
9531 | return PTR_ERR(trans); | |
1832a6d5 | 9532 | |
581bb050 LZ |
9533 | err = btrfs_find_free_ino(root, &objectid); |
9534 | if (err) | |
9535 | goto out_unlock; | |
9536 | ||
aec7477b | 9537 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 9538 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 9539 | S_IFLNK|S_IRWXUGO, &index); |
7cf96da3 TI |
9540 | if (IS_ERR(inode)) { |
9541 | err = PTR_ERR(inode); | |
39279cc3 | 9542 | goto out_unlock; |
7cf96da3 | 9543 | } |
39279cc3 | 9544 | |
ad19db71 CS |
9545 | /* |
9546 | * If the active LSM wants to access the inode during | |
9547 | * d_instantiate it needs these. Smack checks to see | |
9548 | * if the filesystem supports xattrs by looking at the | |
9549 | * ops vector. | |
9550 | */ | |
9551 | inode->i_fop = &btrfs_file_operations; | |
9552 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 9553 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
9554 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9555 | ||
9556 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
9557 | if (err) | |
9558 | goto out_unlock_inode; | |
ad19db71 | 9559 | |
a1b075d2 | 9560 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 9561 | if (err) |
b0d5d10f | 9562 | goto out_unlock_inode; |
39279cc3 CM |
9563 | |
9564 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
9565 | if (!path) { |
9566 | err = -ENOMEM; | |
b0d5d10f | 9567 | goto out_unlock_inode; |
d8926bb3 | 9568 | } |
33345d01 | 9569 | key.objectid = btrfs_ino(inode); |
39279cc3 | 9570 | key.offset = 0; |
962a298f | 9571 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9572 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9573 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9574 | datasize); | |
54aa1f4d | 9575 | if (err) { |
b0839166 | 9576 | btrfs_free_path(path); |
b0d5d10f | 9577 | goto out_unlock_inode; |
54aa1f4d | 9578 | } |
5f39d397 CM |
9579 | leaf = path->nodes[0]; |
9580 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9581 | struct btrfs_file_extent_item); | |
9582 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9583 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9584 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9585 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9586 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9587 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9588 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9589 | ||
39279cc3 | 9590 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
9591 | write_extent_buffer(leaf, symname, ptr, name_len); |
9592 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 9593 | btrfs_free_path(path); |
5f39d397 | 9594 | |
39279cc3 CM |
9595 | inode->i_op = &btrfs_symlink_inode_operations; |
9596 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
d899e052 | 9597 | inode_set_bytes(inode, name_len); |
f06becc4 | 9598 | btrfs_i_size_write(inode, name_len); |
54aa1f4d | 9599 | err = btrfs_update_inode(trans, root, inode); |
b0d5d10f | 9600 | if (err) { |
54aa1f4d | 9601 | drop_inode = 1; |
b0d5d10f CM |
9602 | goto out_unlock_inode; |
9603 | } | |
9604 | ||
9605 | unlock_new_inode(inode); | |
9606 | d_instantiate(dentry, inode); | |
39279cc3 CM |
9607 | |
9608 | out_unlock: | |
7ad85bb7 | 9609 | btrfs_end_transaction(trans, root); |
39279cc3 CM |
9610 | if (drop_inode) { |
9611 | inode_dec_link_count(inode); | |
9612 | iput(inode); | |
9613 | } | |
b53d3f5d | 9614 | btrfs_btree_balance_dirty(root); |
39279cc3 | 9615 | return err; |
b0d5d10f CM |
9616 | |
9617 | out_unlock_inode: | |
9618 | drop_inode = 1; | |
9619 | unlock_new_inode(inode); | |
9620 | goto out_unlock; | |
39279cc3 | 9621 | } |
16432985 | 9622 | |
0af3d00b JB |
9623 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9624 | u64 start, u64 num_bytes, u64 min_size, | |
9625 | loff_t actual_len, u64 *alloc_hint, | |
9626 | struct btrfs_trans_handle *trans) | |
d899e052 | 9627 | { |
5dc562c5 JB |
9628 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
9629 | struct extent_map *em; | |
d899e052 YZ |
9630 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9631 | struct btrfs_key ins; | |
d899e052 | 9632 | u64 cur_offset = start; |
55a61d1d | 9633 | u64 i_size; |
154ea289 | 9634 | u64 cur_bytes; |
d899e052 | 9635 | int ret = 0; |
0af3d00b | 9636 | bool own_trans = true; |
d899e052 | 9637 | |
0af3d00b JB |
9638 | if (trans) |
9639 | own_trans = false; | |
d899e052 | 9640 | while (num_bytes > 0) { |
0af3d00b JB |
9641 | if (own_trans) { |
9642 | trans = btrfs_start_transaction(root, 3); | |
9643 | if (IS_ERR(trans)) { | |
9644 | ret = PTR_ERR(trans); | |
9645 | break; | |
9646 | } | |
5a303d5d YZ |
9647 | } |
9648 | ||
154ea289 CM |
9649 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
9650 | cur_bytes = max(cur_bytes, min_size); | |
00361589 | 9651 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
e570fd27 | 9652 | *alloc_hint, &ins, 1, 0); |
5a303d5d | 9653 | if (ret) { |
0af3d00b JB |
9654 | if (own_trans) |
9655 | btrfs_end_transaction(trans, root); | |
a22285a6 | 9656 | break; |
d899e052 | 9657 | } |
5a303d5d | 9658 | |
d899e052 YZ |
9659 | ret = insert_reserved_file_extent(trans, inode, |
9660 | cur_offset, ins.objectid, | |
9661 | ins.offset, ins.offset, | |
920bbbfb | 9662 | ins.offset, 0, 0, 0, |
d899e052 | 9663 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 9664 | if (ret) { |
857cc2fc | 9665 | btrfs_free_reserved_extent(root, ins.objectid, |
e570fd27 | 9666 | ins.offset, 0); |
79787eaa JM |
9667 | btrfs_abort_transaction(trans, root, ret); |
9668 | if (own_trans) | |
9669 | btrfs_end_transaction(trans, root); | |
9670 | break; | |
9671 | } | |
31193213 | 9672 | |
a1ed835e CM |
9673 | btrfs_drop_extent_cache(inode, cur_offset, |
9674 | cur_offset + ins.offset -1, 0); | |
5a303d5d | 9675 | |
5dc562c5 JB |
9676 | em = alloc_extent_map(); |
9677 | if (!em) { | |
9678 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
9679 | &BTRFS_I(inode)->runtime_flags); | |
9680 | goto next; | |
9681 | } | |
9682 | ||
9683 | em->start = cur_offset; | |
9684 | em->orig_start = cur_offset; | |
9685 | em->len = ins.offset; | |
9686 | em->block_start = ins.objectid; | |
9687 | em->block_len = ins.offset; | |
b4939680 | 9688 | em->orig_block_len = ins.offset; |
cc95bef6 | 9689 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
9690 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
9691 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
9692 | em->generation = trans->transid; | |
9693 | ||
9694 | while (1) { | |
9695 | write_lock(&em_tree->lock); | |
09a2a8f9 | 9696 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
9697 | write_unlock(&em_tree->lock); |
9698 | if (ret != -EEXIST) | |
9699 | break; | |
9700 | btrfs_drop_extent_cache(inode, cur_offset, | |
9701 | cur_offset + ins.offset - 1, | |
9702 | 0); | |
9703 | } | |
9704 | free_extent_map(em); | |
9705 | next: | |
d899e052 YZ |
9706 | num_bytes -= ins.offset; |
9707 | cur_offset += ins.offset; | |
efa56464 | 9708 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 9709 | |
0c4d2d95 | 9710 | inode_inc_iversion(inode); |
d899e052 | 9711 | inode->i_ctime = CURRENT_TIME; |
6cbff00f | 9712 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 9713 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
9714 | (actual_len > inode->i_size) && |
9715 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 9716 | if (cur_offset > actual_len) |
55a61d1d | 9717 | i_size = actual_len; |
d1ea6a61 | 9718 | else |
55a61d1d JB |
9719 | i_size = cur_offset; |
9720 | i_size_write(inode, i_size); | |
9721 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
9722 | } |
9723 | ||
d899e052 | 9724 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
9725 | |
9726 | if (ret) { | |
9727 | btrfs_abort_transaction(trans, root, ret); | |
9728 | if (own_trans) | |
9729 | btrfs_end_transaction(trans, root); | |
9730 | break; | |
9731 | } | |
d899e052 | 9732 | |
0af3d00b JB |
9733 | if (own_trans) |
9734 | btrfs_end_transaction(trans, root); | |
5a303d5d | 9735 | } |
d899e052 YZ |
9736 | return ret; |
9737 | } | |
9738 | ||
0af3d00b JB |
9739 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
9740 | u64 start, u64 num_bytes, u64 min_size, | |
9741 | loff_t actual_len, u64 *alloc_hint) | |
9742 | { | |
9743 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9744 | min_size, actual_len, alloc_hint, | |
9745 | NULL); | |
9746 | } | |
9747 | ||
9748 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
9749 | struct btrfs_trans_handle *trans, int mode, | |
9750 | u64 start, u64 num_bytes, u64 min_size, | |
9751 | loff_t actual_len, u64 *alloc_hint) | |
9752 | { | |
9753 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9754 | min_size, actual_len, alloc_hint, trans); | |
9755 | } | |
9756 | ||
e6dcd2dc CM |
9757 | static int btrfs_set_page_dirty(struct page *page) |
9758 | { | |
e6dcd2dc CM |
9759 | return __set_page_dirty_nobuffers(page); |
9760 | } | |
9761 | ||
10556cb2 | 9762 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 9763 | { |
b83cc969 | 9764 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 9765 | umode_t mode = inode->i_mode; |
b83cc969 | 9766 | |
cb6db4e5 JM |
9767 | if (mask & MAY_WRITE && |
9768 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
9769 | if (btrfs_root_readonly(root)) | |
9770 | return -EROFS; | |
9771 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
9772 | return -EACCES; | |
9773 | } | |
2830ba7f | 9774 | return generic_permission(inode, mask); |
fdebe2bd | 9775 | } |
39279cc3 | 9776 | |
ef3b9af5 FM |
9777 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
9778 | { | |
9779 | struct btrfs_trans_handle *trans; | |
9780 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9781 | struct inode *inode = NULL; | |
9782 | u64 objectid; | |
9783 | u64 index; | |
9784 | int ret = 0; | |
9785 | ||
9786 | /* | |
9787 | * 5 units required for adding orphan entry | |
9788 | */ | |
9789 | trans = btrfs_start_transaction(root, 5); | |
9790 | if (IS_ERR(trans)) | |
9791 | return PTR_ERR(trans); | |
9792 | ||
9793 | ret = btrfs_find_free_ino(root, &objectid); | |
9794 | if (ret) | |
9795 | goto out; | |
9796 | ||
9797 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
9798 | btrfs_ino(dir), objectid, mode, &index); | |
9799 | if (IS_ERR(inode)) { | |
9800 | ret = PTR_ERR(inode); | |
9801 | inode = NULL; | |
9802 | goto out; | |
9803 | } | |
9804 | ||
ef3b9af5 FM |
9805 | inode->i_fop = &btrfs_file_operations; |
9806 | inode->i_op = &btrfs_file_inode_operations; | |
9807 | ||
9808 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
9809 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9810 | ||
b0d5d10f CM |
9811 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
9812 | if (ret) | |
9813 | goto out_inode; | |
9814 | ||
9815 | ret = btrfs_update_inode(trans, root, inode); | |
9816 | if (ret) | |
9817 | goto out_inode; | |
ef3b9af5 FM |
9818 | ret = btrfs_orphan_add(trans, inode); |
9819 | if (ret) | |
b0d5d10f | 9820 | goto out_inode; |
ef3b9af5 | 9821 | |
5762b5c9 FM |
9822 | /* |
9823 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
9824 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
9825 | * through: | |
9826 | * | |
9827 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
9828 | */ | |
9829 | set_nlink(inode, 1); | |
b0d5d10f | 9830 | unlock_new_inode(inode); |
ef3b9af5 FM |
9831 | d_tmpfile(dentry, inode); |
9832 | mark_inode_dirty(inode); | |
9833 | ||
9834 | out: | |
9835 | btrfs_end_transaction(trans, root); | |
9836 | if (ret) | |
9837 | iput(inode); | |
9838 | btrfs_balance_delayed_items(root); | |
9839 | btrfs_btree_balance_dirty(root); | |
ef3b9af5 | 9840 | return ret; |
b0d5d10f CM |
9841 | |
9842 | out_inode: | |
9843 | unlock_new_inode(inode); | |
9844 | goto out; | |
9845 | ||
ef3b9af5 FM |
9846 | } |
9847 | ||
b38ef71c FM |
9848 | /* Inspired by filemap_check_errors() */ |
9849 | int btrfs_inode_check_errors(struct inode *inode) | |
9850 | { | |
9851 | int ret = 0; | |
9852 | ||
9853 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && | |
9854 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) | |
9855 | ret = -ENOSPC; | |
9856 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && | |
9857 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) | |
9858 | ret = -EIO; | |
9859 | ||
9860 | return ret; | |
9861 | } | |
9862 | ||
6e1d5dcc | 9863 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 9864 | .getattr = btrfs_getattr, |
39279cc3 CM |
9865 | .lookup = btrfs_lookup, |
9866 | .create = btrfs_create, | |
9867 | .unlink = btrfs_unlink, | |
9868 | .link = btrfs_link, | |
9869 | .mkdir = btrfs_mkdir, | |
9870 | .rmdir = btrfs_rmdir, | |
80ace85c | 9871 | .rename2 = btrfs_rename2, |
39279cc3 CM |
9872 | .symlink = btrfs_symlink, |
9873 | .setattr = btrfs_setattr, | |
618e21d5 | 9874 | .mknod = btrfs_mknod, |
95819c05 CH |
9875 | .setxattr = btrfs_setxattr, |
9876 | .getxattr = btrfs_getxattr, | |
5103e947 | 9877 | .listxattr = btrfs_listxattr, |
95819c05 | 9878 | .removexattr = btrfs_removexattr, |
fdebe2bd | 9879 | .permission = btrfs_permission, |
4e34e719 | 9880 | .get_acl = btrfs_get_acl, |
996a710d | 9881 | .set_acl = btrfs_set_acl, |
93fd63c2 | 9882 | .update_time = btrfs_update_time, |
ef3b9af5 | 9883 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 9884 | }; |
6e1d5dcc | 9885 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 9886 | .lookup = btrfs_lookup, |
fdebe2bd | 9887 | .permission = btrfs_permission, |
4e34e719 | 9888 | .get_acl = btrfs_get_acl, |
996a710d | 9889 | .set_acl = btrfs_set_acl, |
93fd63c2 | 9890 | .update_time = btrfs_update_time, |
39279cc3 | 9891 | }; |
76dda93c | 9892 | |
828c0950 | 9893 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
9894 | .llseek = generic_file_llseek, |
9895 | .read = generic_read_dir, | |
9cdda8d3 | 9896 | .iterate = btrfs_real_readdir, |
34287aa3 | 9897 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 9898 | #ifdef CONFIG_COMPAT |
34287aa3 | 9899 | .compat_ioctl = btrfs_ioctl, |
39279cc3 | 9900 | #endif |
6bf13c0c | 9901 | .release = btrfs_release_file, |
e02119d5 | 9902 | .fsync = btrfs_sync_file, |
39279cc3 CM |
9903 | }; |
9904 | ||
d1310b2e | 9905 | static struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 9906 | .fill_delalloc = run_delalloc_range, |
065631f6 | 9907 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 9908 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 9909 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 9910 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 9911 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
9912 | .set_bit_hook = btrfs_set_bit_hook, |
9913 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
9914 | .merge_extent_hook = btrfs_merge_extent_hook, |
9915 | .split_extent_hook = btrfs_split_extent_hook, | |
07157aac CM |
9916 | }; |
9917 | ||
35054394 CM |
9918 | /* |
9919 | * btrfs doesn't support the bmap operation because swapfiles | |
9920 | * use bmap to make a mapping of extents in the file. They assume | |
9921 | * these extents won't change over the life of the file and they | |
9922 | * use the bmap result to do IO directly to the drive. | |
9923 | * | |
9924 | * the btrfs bmap call would return logical addresses that aren't | |
9925 | * suitable for IO and they also will change frequently as COW | |
9926 | * operations happen. So, swapfile + btrfs == corruption. | |
9927 | * | |
9928 | * For now we're avoiding this by dropping bmap. | |
9929 | */ | |
7f09410b | 9930 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
9931 | .readpage = btrfs_readpage, |
9932 | .writepage = btrfs_writepage, | |
b293f02e | 9933 | .writepages = btrfs_writepages, |
3ab2fb5a | 9934 | .readpages = btrfs_readpages, |
16432985 | 9935 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
9936 | .invalidatepage = btrfs_invalidatepage, |
9937 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 9938 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 9939 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
9940 | }; |
9941 | ||
7f09410b | 9942 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
9943 | .readpage = btrfs_readpage, |
9944 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
9945 | .invalidatepage = btrfs_invalidatepage, |
9946 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
9947 | }; |
9948 | ||
6e1d5dcc | 9949 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
9950 | .getattr = btrfs_getattr, |
9951 | .setattr = btrfs_setattr, | |
95819c05 CH |
9952 | .setxattr = btrfs_setxattr, |
9953 | .getxattr = btrfs_getxattr, | |
5103e947 | 9954 | .listxattr = btrfs_listxattr, |
95819c05 | 9955 | .removexattr = btrfs_removexattr, |
fdebe2bd | 9956 | .permission = btrfs_permission, |
1506fcc8 | 9957 | .fiemap = btrfs_fiemap, |
4e34e719 | 9958 | .get_acl = btrfs_get_acl, |
996a710d | 9959 | .set_acl = btrfs_set_acl, |
e41f941a | 9960 | .update_time = btrfs_update_time, |
39279cc3 | 9961 | }; |
6e1d5dcc | 9962 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
9963 | .getattr = btrfs_getattr, |
9964 | .setattr = btrfs_setattr, | |
fdebe2bd | 9965 | .permission = btrfs_permission, |
95819c05 CH |
9966 | .setxattr = btrfs_setxattr, |
9967 | .getxattr = btrfs_getxattr, | |
33268eaf | 9968 | .listxattr = btrfs_listxattr, |
95819c05 | 9969 | .removexattr = btrfs_removexattr, |
4e34e719 | 9970 | .get_acl = btrfs_get_acl, |
996a710d | 9971 | .set_acl = btrfs_set_acl, |
e41f941a | 9972 | .update_time = btrfs_update_time, |
618e21d5 | 9973 | }; |
6e1d5dcc | 9974 | static const struct inode_operations btrfs_symlink_inode_operations = { |
39279cc3 CM |
9975 | .readlink = generic_readlink, |
9976 | .follow_link = page_follow_link_light, | |
9977 | .put_link = page_put_link, | |
f209561a | 9978 | .getattr = btrfs_getattr, |
22c44fe6 | 9979 | .setattr = btrfs_setattr, |
fdebe2bd | 9980 | .permission = btrfs_permission, |
0279b4cd JO |
9981 | .setxattr = btrfs_setxattr, |
9982 | .getxattr = btrfs_getxattr, | |
9983 | .listxattr = btrfs_listxattr, | |
9984 | .removexattr = btrfs_removexattr, | |
e41f941a | 9985 | .update_time = btrfs_update_time, |
39279cc3 | 9986 | }; |
76dda93c | 9987 | |
82d339d9 | 9988 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 9989 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 9990 | .d_release = btrfs_dentry_release, |
76dda93c | 9991 | }; |