]>
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); | |
4246a0b6 CH |
1848 | if (ret) { |
1849 | bio->bi_error = ret; | |
1850 | bio_endio(bio); | |
1851 | } | |
61891923 | 1852 | return ret; |
44b8bd7e CM |
1853 | } |
1854 | ||
d352ac68 | 1855 | /* |
cad321ad CM |
1856 | * extent_io.c submission hook. This does the right thing for csum calculation |
1857 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1858 | */ |
b2950863 | 1859 | static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1860 | int mirror_num, unsigned long bio_flags, |
1861 | u64 bio_offset) | |
44b8bd7e CM |
1862 | { |
1863 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1864 | int ret = 0; | |
19b9bdb0 | 1865 | int skip_sum; |
0417341e | 1866 | int metadata = 0; |
b812ce28 | 1867 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1868 | |
6cbff00f | 1869 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1870 | |
83eea1f1 | 1871 | if (btrfs_is_free_space_inode(inode)) |
0417341e JM |
1872 | metadata = 2; |
1873 | ||
7b6d91da | 1874 | if (!(rw & REQ_WRITE)) { |
5fd02043 JB |
1875 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
1876 | if (ret) | |
61891923 | 1877 | goto out; |
5fd02043 | 1878 | |
d20f7043 | 1879 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1880 | ret = btrfs_submit_compressed_read(inode, bio, |
1881 | mirror_num, | |
1882 | bio_flags); | |
1883 | goto out; | |
c2db1073 TI |
1884 | } else if (!skip_sum) { |
1885 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); | |
1886 | if (ret) | |
61891923 | 1887 | goto out; |
c2db1073 | 1888 | } |
4d1b5fb4 | 1889 | goto mapit; |
b812ce28 | 1890 | } else if (async && !skip_sum) { |
17d217fe YZ |
1891 | /* csum items have already been cloned */ |
1892 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1893 | goto mapit; | |
19b9bdb0 | 1894 | /* we're doing a write, do the async checksumming */ |
61891923 | 1895 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
44b8bd7e | 1896 | inode, rw, bio, mirror_num, |
eaf25d93 CM |
1897 | bio_flags, bio_offset, |
1898 | __btrfs_submit_bio_start, | |
4a69a410 | 1899 | __btrfs_submit_bio_done); |
61891923 | 1900 | goto out; |
b812ce28 JB |
1901 | } else if (!skip_sum) { |
1902 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); | |
1903 | if (ret) | |
1904 | goto out; | |
19b9bdb0 CM |
1905 | } |
1906 | ||
0b86a832 | 1907 | mapit: |
61891923 SB |
1908 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
1909 | ||
1910 | out: | |
4246a0b6 CH |
1911 | if (ret < 0) { |
1912 | bio->bi_error = ret; | |
1913 | bio_endio(bio); | |
1914 | } | |
61891923 | 1915 | return ret; |
065631f6 | 1916 | } |
6885f308 | 1917 | |
d352ac68 CM |
1918 | /* |
1919 | * given a list of ordered sums record them in the inode. This happens | |
1920 | * at IO completion time based on sums calculated at bio submission time. | |
1921 | */ | |
ba1da2f4 | 1922 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
1923 | struct inode *inode, u64 file_offset, |
1924 | struct list_head *list) | |
1925 | { | |
e6dcd2dc CM |
1926 | struct btrfs_ordered_sum *sum; |
1927 | ||
c6e30871 | 1928 | list_for_each_entry(sum, list, list) { |
39847c4d | 1929 | trans->adding_csums = 1; |
d20f7043 CM |
1930 | btrfs_csum_file_blocks(trans, |
1931 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 1932 | trans->adding_csums = 0; |
e6dcd2dc CM |
1933 | } |
1934 | return 0; | |
1935 | } | |
1936 | ||
2ac55d41 JB |
1937 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
1938 | struct extent_state **cached_state) | |
ea8c2819 | 1939 | { |
6c1500f2 | 1940 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
ea8c2819 | 1941 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
2ac55d41 | 1942 | cached_state, GFP_NOFS); |
ea8c2819 CM |
1943 | } |
1944 | ||
d352ac68 | 1945 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
1946 | struct btrfs_writepage_fixup { |
1947 | struct page *page; | |
1948 | struct btrfs_work work; | |
1949 | }; | |
1950 | ||
b2950863 | 1951 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
1952 | { |
1953 | struct btrfs_writepage_fixup *fixup; | |
1954 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 1955 | struct extent_state *cached_state = NULL; |
247e743c CM |
1956 | struct page *page; |
1957 | struct inode *inode; | |
1958 | u64 page_start; | |
1959 | u64 page_end; | |
87826df0 | 1960 | int ret; |
247e743c CM |
1961 | |
1962 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
1963 | page = fixup->page; | |
4a096752 | 1964 | again: |
247e743c CM |
1965 | lock_page(page); |
1966 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
1967 | ClearPageChecked(page); | |
1968 | goto out_page; | |
1969 | } | |
1970 | ||
1971 | inode = page->mapping->host; | |
1972 | page_start = page_offset(page); | |
1973 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; | |
1974 | ||
2ac55d41 | 1975 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
d0082371 | 1976 | &cached_state); |
4a096752 CM |
1977 | |
1978 | /* already ordered? We're done */ | |
8b62b72b | 1979 | if (PagePrivate2(page)) |
247e743c | 1980 | goto out; |
4a096752 CM |
1981 | |
1982 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
1983 | if (ordered) { | |
2ac55d41 JB |
1984 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
1985 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
1986 | unlock_page(page); |
1987 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 1988 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
1989 | goto again; |
1990 | } | |
247e743c | 1991 | |
87826df0 JM |
1992 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
1993 | if (ret) { | |
1994 | mapping_set_error(page->mapping, ret); | |
1995 | end_extent_writepage(page, ret, page_start, page_end); | |
1996 | ClearPageChecked(page); | |
1997 | goto out; | |
1998 | } | |
1999 | ||
2ac55d41 | 2000 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
247e743c | 2001 | ClearPageChecked(page); |
87826df0 | 2002 | set_page_dirty(page); |
247e743c | 2003 | out: |
2ac55d41 JB |
2004 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2005 | &cached_state, GFP_NOFS); | |
247e743c CM |
2006 | out_page: |
2007 | unlock_page(page); | |
2008 | page_cache_release(page); | |
b897abec | 2009 | kfree(fixup); |
247e743c CM |
2010 | } |
2011 | ||
2012 | /* | |
2013 | * There are a few paths in the higher layers of the kernel that directly | |
2014 | * set the page dirty bit without asking the filesystem if it is a | |
2015 | * good idea. This causes problems because we want to make sure COW | |
2016 | * properly happens and the data=ordered rules are followed. | |
2017 | * | |
c8b97818 | 2018 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2019 | * hasn't been properly setup for IO. We kick off an async process |
2020 | * to fix it up. The async helper will wait for ordered extents, set | |
2021 | * the delalloc bit and make it safe to write the page. | |
2022 | */ | |
b2950863 | 2023 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2024 | { |
2025 | struct inode *inode = page->mapping->host; | |
2026 | struct btrfs_writepage_fixup *fixup; | |
2027 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
247e743c | 2028 | |
8b62b72b CM |
2029 | /* this page is properly in the ordered list */ |
2030 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2031 | return 0; |
2032 | ||
2033 | if (PageChecked(page)) | |
2034 | return -EAGAIN; | |
2035 | ||
2036 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2037 | if (!fixup) | |
2038 | return -EAGAIN; | |
f421950f | 2039 | |
247e743c CM |
2040 | SetPageChecked(page); |
2041 | page_cache_get(page); | |
9e0af237 LB |
2042 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2043 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2044 | fixup->page = page; |
dc6e3209 | 2045 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
87826df0 | 2046 | return -EBUSY; |
247e743c CM |
2047 | } |
2048 | ||
d899e052 YZ |
2049 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2050 | struct inode *inode, u64 file_pos, | |
2051 | u64 disk_bytenr, u64 disk_num_bytes, | |
2052 | u64 num_bytes, u64 ram_bytes, | |
2053 | u8 compression, u8 encryption, | |
2054 | u16 other_encoding, int extent_type) | |
2055 | { | |
2056 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2057 | struct btrfs_file_extent_item *fi; | |
2058 | struct btrfs_path *path; | |
2059 | struct extent_buffer *leaf; | |
2060 | struct btrfs_key ins; | |
1acae57b | 2061 | int extent_inserted = 0; |
d899e052 YZ |
2062 | int ret; |
2063 | ||
2064 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2065 | if (!path) |
2066 | return -ENOMEM; | |
d899e052 | 2067 | |
a1ed835e CM |
2068 | /* |
2069 | * we may be replacing one extent in the tree with another. | |
2070 | * The new extent is pinned in the extent map, and we don't want | |
2071 | * to drop it from the cache until it is completely in the btree. | |
2072 | * | |
2073 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2074 | * the caller is expected to unpin it and allow it to be merged | |
2075 | * with the others. | |
2076 | */ | |
1acae57b FDBM |
2077 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2078 | file_pos + num_bytes, NULL, 0, | |
2079 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2080 | if (ret) |
2081 | goto out; | |
d899e052 | 2082 | |
1acae57b FDBM |
2083 | if (!extent_inserted) { |
2084 | ins.objectid = btrfs_ino(inode); | |
2085 | ins.offset = file_pos; | |
2086 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2087 | ||
2088 | path->leave_spinning = 1; | |
2089 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2090 | sizeof(*fi)); | |
2091 | if (ret) | |
2092 | goto out; | |
2093 | } | |
d899e052 YZ |
2094 | leaf = path->nodes[0]; |
2095 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2096 | struct btrfs_file_extent_item); | |
2097 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2098 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2099 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2100 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2101 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2102 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2103 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2104 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2105 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2106 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2107 | |
d899e052 | 2108 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2109 | btrfs_release_path(path); |
d899e052 YZ |
2110 | |
2111 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2112 | |
2113 | ins.objectid = disk_bytenr; | |
2114 | ins.offset = disk_num_bytes; | |
2115 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 YZ |
2116 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2117 | root->root_key.objectid, | |
33345d01 | 2118 | btrfs_ino(inode), file_pos, &ins); |
79787eaa | 2119 | out: |
d899e052 | 2120 | btrfs_free_path(path); |
b9473439 | 2121 | |
79787eaa | 2122 | return ret; |
d899e052 YZ |
2123 | } |
2124 | ||
38c227d8 LB |
2125 | /* snapshot-aware defrag */ |
2126 | struct sa_defrag_extent_backref { | |
2127 | struct rb_node node; | |
2128 | struct old_sa_defrag_extent *old; | |
2129 | u64 root_id; | |
2130 | u64 inum; | |
2131 | u64 file_pos; | |
2132 | u64 extent_offset; | |
2133 | u64 num_bytes; | |
2134 | u64 generation; | |
2135 | }; | |
2136 | ||
2137 | struct old_sa_defrag_extent { | |
2138 | struct list_head list; | |
2139 | struct new_sa_defrag_extent *new; | |
2140 | ||
2141 | u64 extent_offset; | |
2142 | u64 bytenr; | |
2143 | u64 offset; | |
2144 | u64 len; | |
2145 | int count; | |
2146 | }; | |
2147 | ||
2148 | struct new_sa_defrag_extent { | |
2149 | struct rb_root root; | |
2150 | struct list_head head; | |
2151 | struct btrfs_path *path; | |
2152 | struct inode *inode; | |
2153 | u64 file_pos; | |
2154 | u64 len; | |
2155 | u64 bytenr; | |
2156 | u64 disk_len; | |
2157 | u8 compress_type; | |
2158 | }; | |
2159 | ||
2160 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2161 | struct sa_defrag_extent_backref *b2) | |
2162 | { | |
2163 | if (b1->root_id < b2->root_id) | |
2164 | return -1; | |
2165 | else if (b1->root_id > b2->root_id) | |
2166 | return 1; | |
2167 | ||
2168 | if (b1->inum < b2->inum) | |
2169 | return -1; | |
2170 | else if (b1->inum > b2->inum) | |
2171 | return 1; | |
2172 | ||
2173 | if (b1->file_pos < b2->file_pos) | |
2174 | return -1; | |
2175 | else if (b1->file_pos > b2->file_pos) | |
2176 | return 1; | |
2177 | ||
2178 | /* | |
2179 | * [------------------------------] ===> (a range of space) | |
2180 | * |<--->| |<---->| =============> (fs/file tree A) | |
2181 | * |<---------------------------->| ===> (fs/file tree B) | |
2182 | * | |
2183 | * A range of space can refer to two file extents in one tree while | |
2184 | * refer to only one file extent in another tree. | |
2185 | * | |
2186 | * So we may process a disk offset more than one time(two extents in A) | |
2187 | * and locate at the same extent(one extent in B), then insert two same | |
2188 | * backrefs(both refer to the extent in B). | |
2189 | */ | |
2190 | return 0; | |
2191 | } | |
2192 | ||
2193 | static void backref_insert(struct rb_root *root, | |
2194 | struct sa_defrag_extent_backref *backref) | |
2195 | { | |
2196 | struct rb_node **p = &root->rb_node; | |
2197 | struct rb_node *parent = NULL; | |
2198 | struct sa_defrag_extent_backref *entry; | |
2199 | int ret; | |
2200 | ||
2201 | while (*p) { | |
2202 | parent = *p; | |
2203 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2204 | ||
2205 | ret = backref_comp(backref, entry); | |
2206 | if (ret < 0) | |
2207 | p = &(*p)->rb_left; | |
2208 | else | |
2209 | p = &(*p)->rb_right; | |
2210 | } | |
2211 | ||
2212 | rb_link_node(&backref->node, parent, p); | |
2213 | rb_insert_color(&backref->node, root); | |
2214 | } | |
2215 | ||
2216 | /* | |
2217 | * Note the backref might has changed, and in this case we just return 0. | |
2218 | */ | |
2219 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2220 | void *ctx) | |
2221 | { | |
2222 | struct btrfs_file_extent_item *extent; | |
2223 | struct btrfs_fs_info *fs_info; | |
2224 | struct old_sa_defrag_extent *old = ctx; | |
2225 | struct new_sa_defrag_extent *new = old->new; | |
2226 | struct btrfs_path *path = new->path; | |
2227 | struct btrfs_key key; | |
2228 | struct btrfs_root *root; | |
2229 | struct sa_defrag_extent_backref *backref; | |
2230 | struct extent_buffer *leaf; | |
2231 | struct inode *inode = new->inode; | |
2232 | int slot; | |
2233 | int ret; | |
2234 | u64 extent_offset; | |
2235 | u64 num_bytes; | |
2236 | ||
2237 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
2238 | inum == btrfs_ino(inode)) | |
2239 | return 0; | |
2240 | ||
2241 | key.objectid = root_id; | |
2242 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2243 | key.offset = (u64)-1; | |
2244 | ||
2245 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2246 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2247 | if (IS_ERR(root)) { | |
2248 | if (PTR_ERR(root) == -ENOENT) | |
2249 | return 0; | |
2250 | WARN_ON(1); | |
2251 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", | |
2252 | inum, offset, root_id); | |
2253 | return PTR_ERR(root); | |
2254 | } | |
2255 | ||
2256 | key.objectid = inum; | |
2257 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2258 | if (offset > (u64)-1 << 32) | |
2259 | key.offset = 0; | |
2260 | else | |
2261 | key.offset = offset; | |
2262 | ||
2263 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2264 | if (WARN_ON(ret < 0)) |
38c227d8 | 2265 | return ret; |
50f1319c | 2266 | ret = 0; |
38c227d8 LB |
2267 | |
2268 | while (1) { | |
2269 | cond_resched(); | |
2270 | ||
2271 | leaf = path->nodes[0]; | |
2272 | slot = path->slots[0]; | |
2273 | ||
2274 | if (slot >= btrfs_header_nritems(leaf)) { | |
2275 | ret = btrfs_next_leaf(root, path); | |
2276 | if (ret < 0) { | |
2277 | goto out; | |
2278 | } else if (ret > 0) { | |
2279 | ret = 0; | |
2280 | goto out; | |
2281 | } | |
2282 | continue; | |
2283 | } | |
2284 | ||
2285 | path->slots[0]++; | |
2286 | ||
2287 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2288 | ||
2289 | if (key.objectid > inum) | |
2290 | goto out; | |
2291 | ||
2292 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2293 | continue; | |
2294 | ||
2295 | extent = btrfs_item_ptr(leaf, slot, | |
2296 | struct btrfs_file_extent_item); | |
2297 | ||
2298 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2299 | continue; | |
2300 | ||
e68afa49 LB |
2301 | /* |
2302 | * 'offset' refers to the exact key.offset, | |
2303 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2304 | * (key.offset - extent_offset). | |
2305 | */ | |
2306 | if (key.offset != offset) | |
38c227d8 LB |
2307 | continue; |
2308 | ||
e68afa49 | 2309 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2310 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2311 | |
38c227d8 LB |
2312 | if (extent_offset >= old->extent_offset + old->offset + |
2313 | old->len || extent_offset + num_bytes <= | |
2314 | old->extent_offset + old->offset) | |
2315 | continue; | |
38c227d8 LB |
2316 | break; |
2317 | } | |
2318 | ||
2319 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2320 | if (!backref) { | |
2321 | ret = -ENOENT; | |
2322 | goto out; | |
2323 | } | |
2324 | ||
2325 | backref->root_id = root_id; | |
2326 | backref->inum = inum; | |
e68afa49 | 2327 | backref->file_pos = offset; |
38c227d8 LB |
2328 | backref->num_bytes = num_bytes; |
2329 | backref->extent_offset = extent_offset; | |
2330 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2331 | backref->old = old; | |
2332 | backref_insert(&new->root, backref); | |
2333 | old->count++; | |
2334 | out: | |
2335 | btrfs_release_path(path); | |
2336 | WARN_ON(ret); | |
2337 | return ret; | |
2338 | } | |
2339 | ||
2340 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2341 | struct new_sa_defrag_extent *new) | |
2342 | { | |
2343 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; | |
2344 | struct old_sa_defrag_extent *old, *tmp; | |
2345 | int ret; | |
2346 | ||
2347 | new->path = path; | |
2348 | ||
2349 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2350 | ret = iterate_inodes_from_logical(old->bytenr + |
2351 | old->extent_offset, fs_info, | |
38c227d8 LB |
2352 | path, record_one_backref, |
2353 | old); | |
4724b106 JB |
2354 | if (ret < 0 && ret != -ENOENT) |
2355 | return false; | |
38c227d8 LB |
2356 | |
2357 | /* no backref to be processed for this extent */ | |
2358 | if (!old->count) { | |
2359 | list_del(&old->list); | |
2360 | kfree(old); | |
2361 | } | |
2362 | } | |
2363 | ||
2364 | if (list_empty(&new->head)) | |
2365 | return false; | |
2366 | ||
2367 | return true; | |
2368 | } | |
2369 | ||
2370 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2371 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2372 | struct new_sa_defrag_extent *new) |
38c227d8 | 2373 | { |
116e0024 | 2374 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2375 | return 0; |
2376 | ||
2377 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2378 | return 0; | |
2379 | ||
116e0024 LB |
2380 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2381 | return 0; | |
2382 | ||
2383 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2384 | btrfs_file_extent_other_encoding(leaf, fi)) |
2385 | return 0; | |
2386 | ||
2387 | return 1; | |
2388 | } | |
2389 | ||
2390 | /* | |
2391 | * Note the backref might has changed, and in this case we just return 0. | |
2392 | */ | |
2393 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2394 | struct sa_defrag_extent_backref *prev, | |
2395 | struct sa_defrag_extent_backref *backref) | |
2396 | { | |
2397 | struct btrfs_file_extent_item *extent; | |
2398 | struct btrfs_file_extent_item *item; | |
2399 | struct btrfs_ordered_extent *ordered; | |
2400 | struct btrfs_trans_handle *trans; | |
2401 | struct btrfs_fs_info *fs_info; | |
2402 | struct btrfs_root *root; | |
2403 | struct btrfs_key key; | |
2404 | struct extent_buffer *leaf; | |
2405 | struct old_sa_defrag_extent *old = backref->old; | |
2406 | struct new_sa_defrag_extent *new = old->new; | |
2407 | struct inode *src_inode = new->inode; | |
2408 | struct inode *inode; | |
2409 | struct extent_state *cached = NULL; | |
2410 | int ret = 0; | |
2411 | u64 start; | |
2412 | u64 len; | |
2413 | u64 lock_start; | |
2414 | u64 lock_end; | |
2415 | bool merge = false; | |
2416 | int index; | |
2417 | ||
2418 | if (prev && prev->root_id == backref->root_id && | |
2419 | prev->inum == backref->inum && | |
2420 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2421 | merge = true; | |
2422 | ||
2423 | /* step 1: get root */ | |
2424 | key.objectid = backref->root_id; | |
2425 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2426 | key.offset = (u64)-1; | |
2427 | ||
2428 | fs_info = BTRFS_I(src_inode)->root->fs_info; | |
2429 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
2430 | ||
2431 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2432 | if (IS_ERR(root)) { | |
2433 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2434 | if (PTR_ERR(root) == -ENOENT) | |
2435 | return 0; | |
2436 | return PTR_ERR(root); | |
2437 | } | |
38c227d8 | 2438 | |
bcbba5e6 WS |
2439 | if (btrfs_root_readonly(root)) { |
2440 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2441 | return 0; | |
2442 | } | |
2443 | ||
38c227d8 LB |
2444 | /* step 2: get inode */ |
2445 | key.objectid = backref->inum; | |
2446 | key.type = BTRFS_INODE_ITEM_KEY; | |
2447 | key.offset = 0; | |
2448 | ||
2449 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2450 | if (IS_ERR(inode)) { | |
2451 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2452 | return 0; | |
2453 | } | |
2454 | ||
2455 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2456 | ||
2457 | /* step 3: relink backref */ | |
2458 | lock_start = backref->file_pos; | |
2459 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2460 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2461 | 0, &cached); | |
2462 | ||
2463 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2464 | if (ordered) { | |
2465 | btrfs_put_ordered_extent(ordered); | |
2466 | goto out_unlock; | |
2467 | } | |
2468 | ||
2469 | trans = btrfs_join_transaction(root); | |
2470 | if (IS_ERR(trans)) { | |
2471 | ret = PTR_ERR(trans); | |
2472 | goto out_unlock; | |
2473 | } | |
2474 | ||
2475 | key.objectid = backref->inum; | |
2476 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2477 | key.offset = backref->file_pos; | |
2478 | ||
2479 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2480 | if (ret < 0) { | |
2481 | goto out_free_path; | |
2482 | } else if (ret > 0) { | |
2483 | ret = 0; | |
2484 | goto out_free_path; | |
2485 | } | |
2486 | ||
2487 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2488 | struct btrfs_file_extent_item); | |
2489 | ||
2490 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2491 | backref->generation) | |
2492 | goto out_free_path; | |
2493 | ||
2494 | btrfs_release_path(path); | |
2495 | ||
2496 | start = backref->file_pos; | |
2497 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2498 | start += old->extent_offset + old->offset - | |
2499 | backref->extent_offset; | |
2500 | ||
2501 | len = min(backref->extent_offset + backref->num_bytes, | |
2502 | old->extent_offset + old->offset + old->len); | |
2503 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2504 | ||
2505 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2506 | start + len, 1); | |
2507 | if (ret) | |
2508 | goto out_free_path; | |
2509 | again: | |
2510 | key.objectid = btrfs_ino(inode); | |
2511 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2512 | key.offset = start; | |
2513 | ||
a09a0a70 | 2514 | path->leave_spinning = 1; |
38c227d8 LB |
2515 | if (merge) { |
2516 | struct btrfs_file_extent_item *fi; | |
2517 | u64 extent_len; | |
2518 | struct btrfs_key found_key; | |
2519 | ||
3c9665df | 2520 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2521 | if (ret < 0) |
2522 | goto out_free_path; | |
2523 | ||
2524 | path->slots[0]--; | |
2525 | leaf = path->nodes[0]; | |
2526 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2527 | ||
2528 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2529 | struct btrfs_file_extent_item); | |
2530 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2531 | ||
116e0024 LB |
2532 | if (extent_len + found_key.offset == start && |
2533 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2534 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2535 | extent_len + len); | |
2536 | btrfs_mark_buffer_dirty(leaf); | |
2537 | inode_add_bytes(inode, len); | |
2538 | ||
2539 | ret = 1; | |
2540 | goto out_free_path; | |
2541 | } else { | |
2542 | merge = false; | |
2543 | btrfs_release_path(path); | |
2544 | goto again; | |
2545 | } | |
2546 | } | |
2547 | ||
2548 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2549 | sizeof(*extent)); | |
2550 | if (ret) { | |
2551 | btrfs_abort_transaction(trans, root, ret); | |
2552 | goto out_free_path; | |
2553 | } | |
2554 | ||
2555 | leaf = path->nodes[0]; | |
2556 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2557 | struct btrfs_file_extent_item); | |
2558 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2559 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2560 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2561 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2562 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2563 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2564 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2565 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2566 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2567 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2568 | ||
2569 | btrfs_mark_buffer_dirty(leaf); | |
2570 | inode_add_bytes(inode, len); | |
a09a0a70 | 2571 | btrfs_release_path(path); |
38c227d8 LB |
2572 | |
2573 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, | |
2574 | new->disk_len, 0, | |
2575 | backref->root_id, backref->inum, | |
2576 | new->file_pos, 0); /* start - extent_offset */ | |
2577 | if (ret) { | |
2578 | btrfs_abort_transaction(trans, root, ret); | |
2579 | goto out_free_path; | |
2580 | } | |
2581 | ||
2582 | ret = 1; | |
2583 | out_free_path: | |
2584 | btrfs_release_path(path); | |
a09a0a70 | 2585 | path->leave_spinning = 0; |
38c227d8 LB |
2586 | btrfs_end_transaction(trans, root); |
2587 | out_unlock: | |
2588 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2589 | &cached, GFP_NOFS); | |
2590 | iput(inode); | |
2591 | return ret; | |
2592 | } | |
2593 | ||
6f519564 LB |
2594 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2595 | { | |
2596 | struct old_sa_defrag_extent *old, *tmp; | |
2597 | ||
2598 | if (!new) | |
2599 | return; | |
2600 | ||
2601 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
2602 | list_del(&old->list); | |
2603 | kfree(old); | |
2604 | } | |
2605 | kfree(new); | |
2606 | } | |
2607 | ||
38c227d8 LB |
2608 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2609 | { | |
2610 | struct btrfs_path *path; | |
38c227d8 LB |
2611 | struct sa_defrag_extent_backref *backref; |
2612 | struct sa_defrag_extent_backref *prev = NULL; | |
2613 | struct inode *inode; | |
2614 | struct btrfs_root *root; | |
2615 | struct rb_node *node; | |
2616 | int ret; | |
2617 | ||
2618 | inode = new->inode; | |
2619 | root = BTRFS_I(inode)->root; | |
2620 | ||
2621 | path = btrfs_alloc_path(); | |
2622 | if (!path) | |
2623 | return; | |
2624 | ||
2625 | if (!record_extent_backrefs(path, new)) { | |
2626 | btrfs_free_path(path); | |
2627 | goto out; | |
2628 | } | |
2629 | btrfs_release_path(path); | |
2630 | ||
2631 | while (1) { | |
2632 | node = rb_first(&new->root); | |
2633 | if (!node) | |
2634 | break; | |
2635 | rb_erase(node, &new->root); | |
2636 | ||
2637 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2638 | ||
2639 | ret = relink_extent_backref(path, prev, backref); | |
2640 | WARN_ON(ret < 0); | |
2641 | ||
2642 | kfree(prev); | |
2643 | ||
2644 | if (ret == 1) | |
2645 | prev = backref; | |
2646 | else | |
2647 | prev = NULL; | |
2648 | cond_resched(); | |
2649 | } | |
2650 | kfree(prev); | |
2651 | ||
2652 | btrfs_free_path(path); | |
38c227d8 | 2653 | out: |
6f519564 LB |
2654 | free_sa_defrag_extent(new); |
2655 | ||
38c227d8 LB |
2656 | atomic_dec(&root->fs_info->defrag_running); |
2657 | wake_up(&root->fs_info->transaction_wait); | |
38c227d8 LB |
2658 | } |
2659 | ||
2660 | static struct new_sa_defrag_extent * | |
2661 | record_old_file_extents(struct inode *inode, | |
2662 | struct btrfs_ordered_extent *ordered) | |
2663 | { | |
2664 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2665 | struct btrfs_path *path; | |
2666 | struct btrfs_key key; | |
6f519564 | 2667 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2668 | struct new_sa_defrag_extent *new; |
2669 | int ret; | |
2670 | ||
2671 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2672 | if (!new) | |
2673 | return NULL; | |
2674 | ||
2675 | new->inode = inode; | |
2676 | new->file_pos = ordered->file_offset; | |
2677 | new->len = ordered->len; | |
2678 | new->bytenr = ordered->start; | |
2679 | new->disk_len = ordered->disk_len; | |
2680 | new->compress_type = ordered->compress_type; | |
2681 | new->root = RB_ROOT; | |
2682 | INIT_LIST_HEAD(&new->head); | |
2683 | ||
2684 | path = btrfs_alloc_path(); | |
2685 | if (!path) | |
2686 | goto out_kfree; | |
2687 | ||
2688 | key.objectid = btrfs_ino(inode); | |
2689 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2690 | key.offset = new->file_pos; | |
2691 | ||
2692 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2693 | if (ret < 0) | |
2694 | goto out_free_path; | |
2695 | if (ret > 0 && path->slots[0] > 0) | |
2696 | path->slots[0]--; | |
2697 | ||
2698 | /* find out all the old extents for the file range */ | |
2699 | while (1) { | |
2700 | struct btrfs_file_extent_item *extent; | |
2701 | struct extent_buffer *l; | |
2702 | int slot; | |
2703 | u64 num_bytes; | |
2704 | u64 offset; | |
2705 | u64 end; | |
2706 | u64 disk_bytenr; | |
2707 | u64 extent_offset; | |
2708 | ||
2709 | l = path->nodes[0]; | |
2710 | slot = path->slots[0]; | |
2711 | ||
2712 | if (slot >= btrfs_header_nritems(l)) { | |
2713 | ret = btrfs_next_leaf(root, path); | |
2714 | if (ret < 0) | |
6f519564 | 2715 | goto out_free_path; |
38c227d8 LB |
2716 | else if (ret > 0) |
2717 | break; | |
2718 | continue; | |
2719 | } | |
2720 | ||
2721 | btrfs_item_key_to_cpu(l, &key, slot); | |
2722 | ||
2723 | if (key.objectid != btrfs_ino(inode)) | |
2724 | break; | |
2725 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2726 | break; | |
2727 | if (key.offset >= new->file_pos + new->len) | |
2728 | break; | |
2729 | ||
2730 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2731 | ||
2732 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2733 | if (key.offset + num_bytes < new->file_pos) | |
2734 | goto next; | |
2735 | ||
2736 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2737 | if (!disk_bytenr) | |
2738 | goto next; | |
2739 | ||
2740 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2741 | ||
2742 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2743 | if (!old) | |
6f519564 | 2744 | goto out_free_path; |
38c227d8 LB |
2745 | |
2746 | offset = max(new->file_pos, key.offset); | |
2747 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2748 | ||
2749 | old->bytenr = disk_bytenr; | |
2750 | old->extent_offset = extent_offset; | |
2751 | old->offset = offset - key.offset; | |
2752 | old->len = end - offset; | |
2753 | old->new = new; | |
2754 | old->count = 0; | |
2755 | list_add_tail(&old->list, &new->head); | |
2756 | next: | |
2757 | path->slots[0]++; | |
2758 | cond_resched(); | |
2759 | } | |
2760 | ||
2761 | btrfs_free_path(path); | |
2762 | atomic_inc(&root->fs_info->defrag_running); | |
2763 | ||
2764 | return new; | |
2765 | ||
38c227d8 LB |
2766 | out_free_path: |
2767 | btrfs_free_path(path); | |
2768 | out_kfree: | |
6f519564 | 2769 | free_sa_defrag_extent(new); |
38c227d8 LB |
2770 | return NULL; |
2771 | } | |
2772 | ||
e570fd27 MX |
2773 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
2774 | u64 start, u64 len) | |
2775 | { | |
2776 | struct btrfs_block_group_cache *cache; | |
2777 | ||
2778 | cache = btrfs_lookup_block_group(root->fs_info, start); | |
2779 | ASSERT(cache); | |
2780 | ||
2781 | spin_lock(&cache->lock); | |
2782 | cache->delalloc_bytes -= len; | |
2783 | spin_unlock(&cache->lock); | |
2784 | ||
2785 | btrfs_put_block_group(cache); | |
2786 | } | |
2787 | ||
d352ac68 CM |
2788 | /* as ordered data IO finishes, this gets called so we can finish |
2789 | * an ordered extent if the range of bytes in the file it covers are | |
2790 | * fully written. | |
2791 | */ | |
5fd02043 | 2792 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2793 | { |
5fd02043 | 2794 | struct inode *inode = ordered_extent->inode; |
e6dcd2dc | 2795 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2796 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2797 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2798 | struct extent_state *cached_state = NULL; |
38c227d8 | 2799 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2800 | int compress_type = 0; |
77cef2ec JB |
2801 | int ret = 0; |
2802 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2803 | bool nolock; |
77cef2ec | 2804 | bool truncated = false; |
e6dcd2dc | 2805 | |
83eea1f1 | 2806 | nolock = btrfs_is_free_space_inode(inode); |
0cb59c99 | 2807 | |
5fd02043 JB |
2808 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2809 | ret = -EIO; | |
2810 | goto out; | |
2811 | } | |
2812 | ||
f612496b MX |
2813 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
2814 | ordered_extent->file_offset + | |
2815 | ordered_extent->len - 1); | |
2816 | ||
77cef2ec JB |
2817 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2818 | truncated = true; | |
2819 | logical_len = ordered_extent->truncated_len; | |
2820 | /* Truncated the entire extent, don't bother adding */ | |
2821 | if (!logical_len) | |
2822 | goto out; | |
2823 | } | |
2824 | ||
c2167754 | 2825 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2826 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
6c760c07 JB |
2827 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2828 | if (nolock) | |
2829 | trans = btrfs_join_transaction_nolock(root); | |
2830 | else | |
2831 | trans = btrfs_join_transaction(root); | |
2832 | if (IS_ERR(trans)) { | |
2833 | ret = PTR_ERR(trans); | |
2834 | trans = NULL; | |
2835 | goto out; | |
c2167754 | 2836 | } |
6c760c07 JB |
2837 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
2838 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2839 | if (ret) /* -ENOMEM or corruption */ | |
2840 | btrfs_abort_transaction(trans, root, ret); | |
c2167754 YZ |
2841 | goto out; |
2842 | } | |
e6dcd2dc | 2843 | |
2ac55d41 JB |
2844 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2845 | ordered_extent->file_offset + ordered_extent->len - 1, | |
d0082371 | 2846 | 0, &cached_state); |
e6dcd2dc | 2847 | |
38c227d8 LB |
2848 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2849 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2850 | EXTENT_DEFRAG, 1, cached_state); | |
2851 | if (ret) { | |
2852 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2853 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2854 | /* the inode is shared */ |
2855 | new = record_old_file_extents(inode, ordered_extent); | |
2856 | ||
2857 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2858 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2859 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2860 | } | |
2861 | ||
0cb59c99 | 2862 | if (nolock) |
7a7eaa40 | 2863 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2864 | else |
7a7eaa40 | 2865 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2866 | if (IS_ERR(trans)) { |
2867 | ret = PTR_ERR(trans); | |
2868 | trans = NULL; | |
2869 | goto out_unlock; | |
2870 | } | |
a79b7d4b | 2871 | |
0ca1f7ce | 2872 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
c2167754 | 2873 | |
c8b97818 | 2874 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2875 | compress_type = ordered_extent->compress_type; |
d899e052 | 2876 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2877 | BUG_ON(compress_type); |
920bbbfb | 2878 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
2879 | ordered_extent->file_offset, |
2880 | ordered_extent->file_offset + | |
77cef2ec | 2881 | logical_len); |
d899e052 | 2882 | } else { |
0af3d00b | 2883 | BUG_ON(root == root->fs_info->tree_root); |
d899e052 YZ |
2884 | ret = insert_reserved_file_extent(trans, inode, |
2885 | ordered_extent->file_offset, | |
2886 | ordered_extent->start, | |
2887 | ordered_extent->disk_len, | |
77cef2ec | 2888 | logical_len, logical_len, |
261507a0 | 2889 | compress_type, 0, 0, |
d899e052 | 2890 | BTRFS_FILE_EXTENT_REG); |
e570fd27 MX |
2891 | if (!ret) |
2892 | btrfs_release_delalloc_bytes(root, | |
2893 | ordered_extent->start, | |
2894 | ordered_extent->disk_len); | |
d899e052 | 2895 | } |
5dc562c5 JB |
2896 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
2897 | ordered_extent->file_offset, ordered_extent->len, | |
2898 | trans->transid); | |
79787eaa JM |
2899 | if (ret < 0) { |
2900 | btrfs_abort_transaction(trans, root, ret); | |
5fd02043 | 2901 | goto out_unlock; |
79787eaa | 2902 | } |
2ac55d41 | 2903 | |
e6dcd2dc CM |
2904 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
2905 | &ordered_extent->list); | |
2906 | ||
6c760c07 JB |
2907 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2908 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2909 | if (ret) { /* -ENOMEM or corruption */ | |
2910 | btrfs_abort_transaction(trans, root, ret); | |
2911 | goto out_unlock; | |
1ef30be1 JB |
2912 | } |
2913 | ret = 0; | |
5fd02043 JB |
2914 | out_unlock: |
2915 | unlock_extent_cached(io_tree, ordered_extent->file_offset, | |
2916 | ordered_extent->file_offset + | |
2917 | ordered_extent->len - 1, &cached_state, GFP_NOFS); | |
c2167754 | 2918 | out: |
5b0e95bf | 2919 | if (root != root->fs_info->tree_root) |
0cb59c99 | 2920 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
a698d075 MX |
2921 | if (trans) |
2922 | btrfs_end_transaction(trans, root); | |
0cb59c99 | 2923 | |
77cef2ec JB |
2924 | if (ret || truncated) { |
2925 | u64 start, end; | |
2926 | ||
2927 | if (truncated) | |
2928 | start = ordered_extent->file_offset + logical_len; | |
2929 | else | |
2930 | start = ordered_extent->file_offset; | |
2931 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
2932 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
2933 | ||
2934 | /* Drop the cache for the part of the extent we didn't write. */ | |
2935 | btrfs_drop_extent_cache(inode, start, end, 0); | |
5fd02043 | 2936 | |
0bec9ef5 JB |
2937 | /* |
2938 | * If the ordered extent had an IOERR or something else went | |
2939 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
2940 | * back to the allocator. We only free the extent in the |
2941 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 2942 | */ |
77cef2ec JB |
2943 | if ((ret || !logical_len) && |
2944 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 JB |
2945 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2946 | btrfs_free_reserved_extent(root, ordered_extent->start, | |
e570fd27 | 2947 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
2948 | } |
2949 | ||
2950 | ||
5fd02043 | 2951 | /* |
8bad3c02 LB |
2952 | * This needs to be done to make sure anybody waiting knows we are done |
2953 | * updating everything for this ordered extent. | |
5fd02043 JB |
2954 | */ |
2955 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
2956 | ||
38c227d8 | 2957 | /* for snapshot-aware defrag */ |
6f519564 LB |
2958 | if (new) { |
2959 | if (ret) { | |
2960 | free_sa_defrag_extent(new); | |
2961 | atomic_dec(&root->fs_info->defrag_running); | |
2962 | } else { | |
2963 | relink_file_extents(new); | |
2964 | } | |
2965 | } | |
38c227d8 | 2966 | |
e6dcd2dc CM |
2967 | /* once for us */ |
2968 | btrfs_put_ordered_extent(ordered_extent); | |
2969 | /* once for the tree */ | |
2970 | btrfs_put_ordered_extent(ordered_extent); | |
2971 | ||
5fd02043 JB |
2972 | return ret; |
2973 | } | |
2974 | ||
2975 | static void finish_ordered_fn(struct btrfs_work *work) | |
2976 | { | |
2977 | struct btrfs_ordered_extent *ordered_extent; | |
2978 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
2979 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
2980 | } |
2981 | ||
b2950863 | 2982 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
2983 | struct extent_state *state, int uptodate) |
2984 | { | |
5fd02043 JB |
2985 | struct inode *inode = page->mapping->host; |
2986 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2987 | struct btrfs_ordered_extent *ordered_extent = NULL; | |
9e0af237 LB |
2988 | struct btrfs_workqueue *wq; |
2989 | btrfs_work_func_t func; | |
5fd02043 | 2990 | |
1abe9b8a | 2991 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
2992 | ||
8b62b72b | 2993 | ClearPagePrivate2(page); |
5fd02043 JB |
2994 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
2995 | end - start + 1, uptodate)) | |
2996 | return 0; | |
2997 | ||
9e0af237 LB |
2998 | if (btrfs_is_free_space_inode(inode)) { |
2999 | wq = root->fs_info->endio_freespace_worker; | |
3000 | func = btrfs_freespace_write_helper; | |
3001 | } else { | |
3002 | wq = root->fs_info->endio_write_workers; | |
3003 | func = btrfs_endio_write_helper; | |
3004 | } | |
5fd02043 | 3005 | |
9e0af237 LB |
3006 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3007 | NULL); | |
3008 | btrfs_queue_work(wq, &ordered_extent->work); | |
5fd02043 JB |
3009 | |
3010 | return 0; | |
211f90e6 CM |
3011 | } |
3012 | ||
dc380aea MX |
3013 | static int __readpage_endio_check(struct inode *inode, |
3014 | struct btrfs_io_bio *io_bio, | |
3015 | int icsum, struct page *page, | |
3016 | int pgoff, u64 start, size_t len) | |
3017 | { | |
3018 | char *kaddr; | |
3019 | u32 csum_expected; | |
3020 | u32 csum = ~(u32)0; | |
3021 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, | |
3022 | DEFAULT_RATELIMIT_BURST); | |
3023 | ||
3024 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3025 | ||
3026 | kaddr = kmap_atomic(page); | |
3027 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
3028 | btrfs_csum_final(csum, (char *)&csum); | |
3029 | if (csum != csum_expected) | |
3030 | goto zeroit; | |
3031 | ||
3032 | kunmap_atomic(kaddr); | |
3033 | return 0; | |
3034 | zeroit: | |
3035 | if (__ratelimit(&_rs)) | |
f0954c66 | 3036 | btrfs_warn(BTRFS_I(inode)->root->fs_info, |
dc380aea MX |
3037 | "csum failed ino %llu off %llu csum %u expected csum %u", |
3038 | btrfs_ino(inode), start, csum, csum_expected); | |
3039 | memset(kaddr + pgoff, 1, len); | |
3040 | flush_dcache_page(page); | |
3041 | kunmap_atomic(kaddr); | |
3042 | if (csum_expected == 0) | |
3043 | return 0; | |
3044 | return -EIO; | |
3045 | } | |
3046 | ||
d352ac68 CM |
3047 | /* |
3048 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3049 | * if there's a match, we allow the bio to finish. If not, the code in |
3050 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3051 | */ |
facc8a22 MX |
3052 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3053 | u64 phy_offset, struct page *page, | |
3054 | u64 start, u64 end, int mirror) | |
07157aac | 3055 | { |
4eee4fa4 | 3056 | size_t offset = start - page_offset(page); |
07157aac | 3057 | struct inode *inode = page->mapping->host; |
d1310b2e | 3058 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3059 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3060 | |
d20f7043 CM |
3061 | if (PageChecked(page)) { |
3062 | ClearPageChecked(page); | |
dc380aea | 3063 | return 0; |
d20f7043 | 3064 | } |
6cbff00f CH |
3065 | |
3066 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3067 | return 0; |
17d217fe YZ |
3068 | |
3069 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3070 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
17d217fe YZ |
3071 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
3072 | GFP_NOFS); | |
b6cda9bc | 3073 | return 0; |
17d217fe | 3074 | } |
d20f7043 | 3075 | |
facc8a22 | 3076 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3077 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3078 | start, (size_t)(end - start + 1)); | |
07157aac | 3079 | } |
b888db2b | 3080 | |
24bbcf04 YZ |
3081 | struct delayed_iput { |
3082 | struct list_head list; | |
3083 | struct inode *inode; | |
3084 | }; | |
3085 | ||
79787eaa JM |
3086 | /* JDM: If this is fs-wide, why can't we add a pointer to |
3087 | * btrfs_inode instead and avoid the allocation? */ | |
24bbcf04 YZ |
3088 | void btrfs_add_delayed_iput(struct inode *inode) |
3089 | { | |
3090 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
3091 | struct delayed_iput *delayed; | |
3092 | ||
3093 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3094 | return; | |
3095 | ||
3096 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); | |
3097 | delayed->inode = inode; | |
3098 | ||
3099 | spin_lock(&fs_info->delayed_iput_lock); | |
3100 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); | |
3101 | spin_unlock(&fs_info->delayed_iput_lock); | |
3102 | } | |
3103 | ||
3104 | void btrfs_run_delayed_iputs(struct btrfs_root *root) | |
3105 | { | |
3106 | LIST_HEAD(list); | |
3107 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3108 | struct delayed_iput *delayed; | |
3109 | int empty; | |
3110 | ||
3111 | spin_lock(&fs_info->delayed_iput_lock); | |
3112 | empty = list_empty(&fs_info->delayed_iputs); | |
3113 | spin_unlock(&fs_info->delayed_iput_lock); | |
3114 | if (empty) | |
3115 | return; | |
3116 | ||
d7c15171 ZL |
3117 | down_read(&fs_info->delayed_iput_sem); |
3118 | ||
24bbcf04 YZ |
3119 | spin_lock(&fs_info->delayed_iput_lock); |
3120 | list_splice_init(&fs_info->delayed_iputs, &list); | |
3121 | spin_unlock(&fs_info->delayed_iput_lock); | |
3122 | ||
3123 | while (!list_empty(&list)) { | |
3124 | delayed = list_entry(list.next, struct delayed_iput, list); | |
3125 | list_del(&delayed->list); | |
3126 | iput(delayed->inode); | |
3127 | kfree(delayed); | |
3128 | } | |
d7c15171 ZL |
3129 | |
3130 | up_read(&root->fs_info->delayed_iput_sem); | |
24bbcf04 YZ |
3131 | } |
3132 | ||
d68fc57b | 3133 | /* |
42b2aa86 | 3134 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3135 | * files in the subvolume, it removes orphan item and frees block_rsv |
3136 | * structure. | |
3137 | */ | |
3138 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3139 | struct btrfs_root *root) | |
3140 | { | |
90290e19 | 3141 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3142 | int ret; |
3143 | ||
8a35d95f | 3144 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3145 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3146 | return; | |
3147 | ||
90290e19 | 3148 | spin_lock(&root->orphan_lock); |
8a35d95f | 3149 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3150 | spin_unlock(&root->orphan_lock); |
3151 | return; | |
3152 | } | |
3153 | ||
3154 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3155 | spin_unlock(&root->orphan_lock); | |
3156 | return; | |
3157 | } | |
3158 | ||
3159 | block_rsv = root->orphan_block_rsv; | |
3160 | root->orphan_block_rsv = NULL; | |
3161 | spin_unlock(&root->orphan_lock); | |
3162 | ||
27cdeb70 | 3163 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b YZ |
3164 | btrfs_root_refs(&root->root_item) > 0) { |
3165 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, | |
3166 | root->root_key.objectid); | |
4ef31a45 JB |
3167 | if (ret) |
3168 | btrfs_abort_transaction(trans, root, ret); | |
3169 | else | |
27cdeb70 MX |
3170 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3171 | &root->state); | |
d68fc57b YZ |
3172 | } |
3173 | ||
90290e19 JB |
3174 | if (block_rsv) { |
3175 | WARN_ON(block_rsv->size > 0); | |
3176 | btrfs_free_block_rsv(root, block_rsv); | |
d68fc57b YZ |
3177 | } |
3178 | } | |
3179 | ||
7b128766 JB |
3180 | /* |
3181 | * This creates an orphan entry for the given inode in case something goes | |
3182 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3183 | * |
3184 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3185 | * this function. | |
7b128766 JB |
3186 | */ |
3187 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
3188 | { | |
3189 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3190 | struct btrfs_block_rsv *block_rsv = NULL; |
3191 | int reserve = 0; | |
3192 | int insert = 0; | |
3193 | int ret; | |
7b128766 | 3194 | |
d68fc57b | 3195 | if (!root->orphan_block_rsv) { |
66d8f3dd | 3196 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
b532402e TI |
3197 | if (!block_rsv) |
3198 | return -ENOMEM; | |
d68fc57b | 3199 | } |
7b128766 | 3200 | |
d68fc57b YZ |
3201 | spin_lock(&root->orphan_lock); |
3202 | if (!root->orphan_block_rsv) { | |
3203 | root->orphan_block_rsv = block_rsv; | |
3204 | } else if (block_rsv) { | |
3205 | btrfs_free_block_rsv(root, block_rsv); | |
3206 | block_rsv = NULL; | |
7b128766 | 3207 | } |
7b128766 | 3208 | |
8a35d95f JB |
3209 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3210 | &BTRFS_I(inode)->runtime_flags)) { | |
d68fc57b YZ |
3211 | #if 0 |
3212 | /* | |
3213 | * For proper ENOSPC handling, we should do orphan | |
3214 | * cleanup when mounting. But this introduces backward | |
3215 | * compatibility issue. | |
3216 | */ | |
3217 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3218 | insert = 2; | |
3219 | else | |
3220 | insert = 1; | |
3221 | #endif | |
3222 | insert = 1; | |
321f0e70 | 3223 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3224 | } |
3225 | ||
72ac3c0d JB |
3226 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3227 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3228 | reserve = 1; |
d68fc57b | 3229 | spin_unlock(&root->orphan_lock); |
7b128766 | 3230 | |
d68fc57b YZ |
3231 | /* grab metadata reservation from transaction handle */ |
3232 | if (reserve) { | |
3233 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
79787eaa | 3234 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
d68fc57b | 3235 | } |
7b128766 | 3236 | |
d68fc57b YZ |
3237 | /* insert an orphan item to track this unlinked/truncated file */ |
3238 | if (insert >= 1) { | |
33345d01 | 3239 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3240 | if (ret) { |
703c88e0 | 3241 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3242 | if (reserve) { |
3243 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3244 | &BTRFS_I(inode)->runtime_flags); | |
3245 | btrfs_orphan_release_metadata(inode); | |
3246 | } | |
3247 | if (ret != -EEXIST) { | |
e8e7cff6 JB |
3248 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3249 | &BTRFS_I(inode)->runtime_flags); | |
4ef31a45 JB |
3250 | btrfs_abort_transaction(trans, root, ret); |
3251 | return ret; | |
3252 | } | |
79787eaa JM |
3253 | } |
3254 | ret = 0; | |
d68fc57b YZ |
3255 | } |
3256 | ||
3257 | /* insert an orphan item to track subvolume contains orphan files */ | |
3258 | if (insert >= 2) { | |
3259 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, | |
3260 | root->root_key.objectid); | |
79787eaa JM |
3261 | if (ret && ret != -EEXIST) { |
3262 | btrfs_abort_transaction(trans, root, ret); | |
3263 | return ret; | |
3264 | } | |
d68fc57b YZ |
3265 | } |
3266 | return 0; | |
7b128766 JB |
3267 | } |
3268 | ||
3269 | /* | |
3270 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3271 | * item for this particular inode. | |
3272 | */ | |
48a3b636 ES |
3273 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3274 | struct inode *inode) | |
7b128766 JB |
3275 | { |
3276 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3277 | int delete_item = 0; |
3278 | int release_rsv = 0; | |
7b128766 JB |
3279 | int ret = 0; |
3280 | ||
d68fc57b | 3281 | spin_lock(&root->orphan_lock); |
8a35d95f JB |
3282 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3283 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3284 | delete_item = 1; |
7b128766 | 3285 | |
72ac3c0d JB |
3286 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3287 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3288 | release_rsv = 1; |
d68fc57b | 3289 | spin_unlock(&root->orphan_lock); |
7b128766 | 3290 | |
703c88e0 | 3291 | if (delete_item) { |
8a35d95f | 3292 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3293 | if (trans) |
3294 | ret = btrfs_del_orphan_item(trans, root, | |
3295 | btrfs_ino(inode)); | |
8a35d95f | 3296 | } |
7b128766 | 3297 | |
703c88e0 FDBM |
3298 | if (release_rsv) |
3299 | btrfs_orphan_release_metadata(inode); | |
3300 | ||
4ef31a45 | 3301 | return ret; |
7b128766 JB |
3302 | } |
3303 | ||
3304 | /* | |
3305 | * this cleans up any orphans that may be left on the list from the last use | |
3306 | * of this root. | |
3307 | */ | |
66b4ffd1 | 3308 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 JB |
3309 | { |
3310 | struct btrfs_path *path; | |
3311 | struct extent_buffer *leaf; | |
7b128766 JB |
3312 | struct btrfs_key key, found_key; |
3313 | struct btrfs_trans_handle *trans; | |
3314 | struct inode *inode; | |
8f6d7f4f | 3315 | u64 last_objectid = 0; |
7b128766 JB |
3316 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3317 | ||
d68fc57b | 3318 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3319 | return 0; |
c71bf099 YZ |
3320 | |
3321 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3322 | if (!path) { |
3323 | ret = -ENOMEM; | |
3324 | goto out; | |
3325 | } | |
7b128766 JB |
3326 | path->reada = -1; |
3327 | ||
3328 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3329 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3330 | key.offset = (u64)-1; |
3331 | ||
7b128766 JB |
3332 | while (1) { |
3333 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3334 | if (ret < 0) |
3335 | goto out; | |
7b128766 JB |
3336 | |
3337 | /* | |
3338 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3339 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3340 | * find the key and see if we have stuff that matches |
3341 | */ | |
3342 | if (ret > 0) { | |
66b4ffd1 | 3343 | ret = 0; |
7b128766 JB |
3344 | if (path->slots[0] == 0) |
3345 | break; | |
3346 | path->slots[0]--; | |
3347 | } | |
3348 | ||
3349 | /* pull out the item */ | |
3350 | leaf = path->nodes[0]; | |
7b128766 JB |
3351 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3352 | ||
3353 | /* make sure the item matches what we want */ | |
3354 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3355 | break; | |
962a298f | 3356 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3357 | break; |
3358 | ||
3359 | /* release the path since we're done with it */ | |
b3b4aa74 | 3360 | btrfs_release_path(path); |
7b128766 JB |
3361 | |
3362 | /* | |
3363 | * this is where we are basically btrfs_lookup, without the | |
3364 | * crossing root thing. we store the inode number in the | |
3365 | * offset of the orphan item. | |
3366 | */ | |
8f6d7f4f JB |
3367 | |
3368 | if (found_key.offset == last_objectid) { | |
c2cf52eb SK |
3369 | btrfs_err(root->fs_info, |
3370 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3371 | ret = -EINVAL; |
3372 | goto out; | |
3373 | } | |
3374 | ||
3375 | last_objectid = found_key.offset; | |
3376 | ||
5d4f98a2 YZ |
3377 | found_key.objectid = found_key.offset; |
3378 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3379 | found_key.offset = 0; | |
73f73415 | 3380 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3381 | ret = PTR_ERR_OR_ZERO(inode); |
a8c9e576 | 3382 | if (ret && ret != -ESTALE) |
66b4ffd1 | 3383 | goto out; |
7b128766 | 3384 | |
f8e9e0b0 AJ |
3385 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
3386 | struct btrfs_root *dead_root; | |
3387 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3388 | int is_dead_root = 0; | |
3389 | ||
3390 | /* | |
3391 | * this is an orphan in the tree root. Currently these | |
3392 | * could come from 2 sources: | |
3393 | * a) a snapshot deletion in progress | |
3394 | * b) a free space cache inode | |
3395 | * We need to distinguish those two, as the snapshot | |
3396 | * orphan must not get deleted. | |
3397 | * find_dead_roots already ran before us, so if this | |
3398 | * is a snapshot deletion, we should find the root | |
3399 | * in the dead_roots list | |
3400 | */ | |
3401 | spin_lock(&fs_info->trans_lock); | |
3402 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3403 | root_list) { | |
3404 | if (dead_root->root_key.objectid == | |
3405 | found_key.objectid) { | |
3406 | is_dead_root = 1; | |
3407 | break; | |
3408 | } | |
3409 | } | |
3410 | spin_unlock(&fs_info->trans_lock); | |
3411 | if (is_dead_root) { | |
3412 | /* prevent this orphan from being found again */ | |
3413 | key.offset = found_key.objectid - 1; | |
3414 | continue; | |
3415 | } | |
3416 | } | |
7b128766 | 3417 | /* |
a8c9e576 JB |
3418 | * Inode is already gone but the orphan item is still there, |
3419 | * kill the orphan item. | |
7b128766 | 3420 | */ |
a8c9e576 JB |
3421 | if (ret == -ESTALE) { |
3422 | trans = btrfs_start_transaction(root, 1); | |
66b4ffd1 JB |
3423 | if (IS_ERR(trans)) { |
3424 | ret = PTR_ERR(trans); | |
3425 | goto out; | |
3426 | } | |
c2cf52eb SK |
3427 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
3428 | found_key.objectid); | |
a8c9e576 JB |
3429 | ret = btrfs_del_orphan_item(trans, root, |
3430 | found_key.objectid); | |
5b21f2ed | 3431 | btrfs_end_transaction(trans, root); |
4ef31a45 JB |
3432 | if (ret) |
3433 | goto out; | |
7b128766 JB |
3434 | continue; |
3435 | } | |
3436 | ||
a8c9e576 JB |
3437 | /* |
3438 | * add this inode to the orphan list so btrfs_orphan_del does | |
3439 | * the proper thing when we hit it | |
3440 | */ | |
8a35d95f JB |
3441 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3442 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3443 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3444 | |
7b128766 JB |
3445 | /* if we have links, this was a truncate, lets do that */ |
3446 | if (inode->i_nlink) { | |
fae7f21c | 3447 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3448 | iput(inode); |
3449 | continue; | |
3450 | } | |
7b128766 | 3451 | nr_truncate++; |
f3fe820c JB |
3452 | |
3453 | /* 1 for the orphan item deletion. */ | |
3454 | trans = btrfs_start_transaction(root, 1); | |
3455 | if (IS_ERR(trans)) { | |
c69b26b0 | 3456 | iput(inode); |
f3fe820c JB |
3457 | ret = PTR_ERR(trans); |
3458 | goto out; | |
3459 | } | |
3460 | ret = btrfs_orphan_add(trans, inode); | |
3461 | btrfs_end_transaction(trans, root); | |
c69b26b0 JB |
3462 | if (ret) { |
3463 | iput(inode); | |
f3fe820c | 3464 | goto out; |
c69b26b0 | 3465 | } |
f3fe820c | 3466 | |
66b4ffd1 | 3467 | ret = btrfs_truncate(inode); |
4a7d0f68 JB |
3468 | if (ret) |
3469 | btrfs_orphan_del(NULL, inode); | |
7b128766 JB |
3470 | } else { |
3471 | nr_unlink++; | |
3472 | } | |
3473 | ||
3474 | /* this will do delete_inode and everything for us */ | |
3475 | iput(inode); | |
66b4ffd1 JB |
3476 | if (ret) |
3477 | goto out; | |
7b128766 | 3478 | } |
3254c876 MX |
3479 | /* release the path since we're done with it */ |
3480 | btrfs_release_path(path); | |
3481 | ||
d68fc57b YZ |
3482 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3483 | ||
3484 | if (root->orphan_block_rsv) | |
3485 | btrfs_block_rsv_release(root, root->orphan_block_rsv, | |
3486 | (u64)-1); | |
3487 | ||
27cdeb70 MX |
3488 | if (root->orphan_block_rsv || |
3489 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3490 | trans = btrfs_join_transaction(root); |
66b4ffd1 JB |
3491 | if (!IS_ERR(trans)) |
3492 | btrfs_end_transaction(trans, root); | |
d68fc57b | 3493 | } |
7b128766 JB |
3494 | |
3495 | if (nr_unlink) | |
4884b476 | 3496 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3497 | if (nr_truncate) |
4884b476 | 3498 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3499 | |
3500 | out: | |
3501 | if (ret) | |
68b663d1 | 3502 | btrfs_err(root->fs_info, |
c2cf52eb | 3503 | "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3504 | btrfs_free_path(path); |
3505 | return ret; | |
7b128766 JB |
3506 | } |
3507 | ||
46a53cca CM |
3508 | /* |
3509 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3510 | * don't find any xattrs, we know there can't be any acls. | |
3511 | * | |
3512 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3513 | */ | |
3514 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3515 | int slot, u64 objectid, |
3516 | int *first_xattr_slot) | |
46a53cca CM |
3517 | { |
3518 | u32 nritems = btrfs_header_nritems(leaf); | |
3519 | struct btrfs_key found_key; | |
f23b5a59 JB |
3520 | static u64 xattr_access = 0; |
3521 | static u64 xattr_default = 0; | |
46a53cca CM |
3522 | int scanned = 0; |
3523 | ||
f23b5a59 JB |
3524 | if (!xattr_access) { |
3525 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, | |
3526 | strlen(POSIX_ACL_XATTR_ACCESS)); | |
3527 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, | |
3528 | strlen(POSIX_ACL_XATTR_DEFAULT)); | |
3529 | } | |
3530 | ||
46a53cca | 3531 | slot++; |
63541927 | 3532 | *first_xattr_slot = -1; |
46a53cca CM |
3533 | while (slot < nritems) { |
3534 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3535 | ||
3536 | /* we found a different objectid, there must not be acls */ | |
3537 | if (found_key.objectid != objectid) | |
3538 | return 0; | |
3539 | ||
3540 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3541 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3542 | if (*first_xattr_slot == -1) |
3543 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3544 | if (found_key.offset == xattr_access || |
3545 | found_key.offset == xattr_default) | |
3546 | return 1; | |
3547 | } | |
46a53cca CM |
3548 | |
3549 | /* | |
3550 | * we found a key greater than an xattr key, there can't | |
3551 | * be any acls later on | |
3552 | */ | |
3553 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3554 | return 0; | |
3555 | ||
3556 | slot++; | |
3557 | scanned++; | |
3558 | ||
3559 | /* | |
3560 | * it goes inode, inode backrefs, xattrs, extents, | |
3561 | * so if there are a ton of hard links to an inode there can | |
3562 | * be a lot of backrefs. Don't waste time searching too hard, | |
3563 | * this is just an optimization | |
3564 | */ | |
3565 | if (scanned >= 8) | |
3566 | break; | |
3567 | } | |
3568 | /* we hit the end of the leaf before we found an xattr or | |
3569 | * something larger than an xattr. We have to assume the inode | |
3570 | * has acls | |
3571 | */ | |
63541927 FDBM |
3572 | if (*first_xattr_slot == -1) |
3573 | *first_xattr_slot = slot; | |
46a53cca CM |
3574 | return 1; |
3575 | } | |
3576 | ||
d352ac68 CM |
3577 | /* |
3578 | * read an inode from the btree into the in-memory inode | |
3579 | */ | |
5d4f98a2 | 3580 | static void btrfs_read_locked_inode(struct inode *inode) |
39279cc3 CM |
3581 | { |
3582 | struct btrfs_path *path; | |
5f39d397 | 3583 | struct extent_buffer *leaf; |
39279cc3 CM |
3584 | struct btrfs_inode_item *inode_item; |
3585 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3586 | struct btrfs_key location; | |
67de1176 | 3587 | unsigned long ptr; |
46a53cca | 3588 | int maybe_acls; |
618e21d5 | 3589 | u32 rdev; |
39279cc3 | 3590 | int ret; |
2f7e33d4 | 3591 | bool filled = false; |
63541927 | 3592 | int first_xattr_slot; |
2f7e33d4 MX |
3593 | |
3594 | ret = btrfs_fill_inode(inode, &rdev); | |
3595 | if (!ret) | |
3596 | filled = true; | |
39279cc3 CM |
3597 | |
3598 | path = btrfs_alloc_path(); | |
1748f843 MF |
3599 | if (!path) |
3600 | goto make_bad; | |
3601 | ||
39279cc3 | 3602 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3603 | |
39279cc3 | 3604 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
5f39d397 | 3605 | if (ret) |
39279cc3 | 3606 | goto make_bad; |
39279cc3 | 3607 | |
5f39d397 | 3608 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3609 | |
3610 | if (filled) | |
67de1176 | 3611 | goto cache_index; |
2f7e33d4 | 3612 | |
5f39d397 CM |
3613 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3614 | struct btrfs_inode_item); | |
5f39d397 | 3615 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3616 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3617 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3618 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
dbe674a9 | 3619 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3620 | |
a937b979 DS |
3621 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3622 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3623 | |
a937b979 DS |
3624 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3625 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3626 | |
a937b979 DS |
3627 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3628 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3629 | |
9cc97d64 | 3630 | BTRFS_I(inode)->i_otime.tv_sec = |
3631 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3632 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3633 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3634 | |
a76a3cd4 | 3635 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3636 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3637 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3638 | ||
6e17d30b YD |
3639 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3640 | inode->i_generation = BTRFS_I(inode)->generation; | |
3641 | inode->i_rdev = 0; | |
3642 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3643 | ||
3644 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3645 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3646 | ||
3647 | cache_index: | |
5dc562c5 JB |
3648 | /* |
3649 | * If we were modified in the current generation and evicted from memory | |
3650 | * and then re-read we need to do a full sync since we don't have any | |
3651 | * idea about which extents were modified before we were evicted from | |
3652 | * cache. | |
6e17d30b YD |
3653 | * |
3654 | * This is required for both inode re-read from disk and delayed inode | |
3655 | * in delayed_nodes_tree. | |
5dc562c5 JB |
3656 | */ |
3657 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) | |
3658 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
3659 | &BTRFS_I(inode)->runtime_flags); | |
3660 | ||
bde6c242 FM |
3661 | /* |
3662 | * We don't persist the id of the transaction where an unlink operation | |
3663 | * against the inode was last made. So here we assume the inode might | |
3664 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3665 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3666 | * between the inode and its parent if the inode is fsync'ed and the log | |
3667 | * replayed. For example, in the scenario: | |
3668 | * | |
3669 | * touch mydir/foo | |
3670 | * ln mydir/foo mydir/bar | |
3671 | * sync | |
3672 | * unlink mydir/bar | |
3673 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3674 | * xfs_io -c fsync mydir/foo | |
3675 | * <power failure> | |
3676 | * mount fs, triggers fsync log replay | |
3677 | * | |
3678 | * We must make sure that when we fsync our inode foo we also log its | |
3679 | * parent inode, otherwise after log replay the parent still has the | |
3680 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3681 | * and doesn't have an inode ref with the name "bar" anymore. | |
3682 | * | |
3683 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
3684 | * but it guarantees correctness at the expense of ocassional full | |
3685 | * transaction commits on fsync if our inode is a directory, or if our | |
3686 | * inode is not a directory, logging its parent unnecessarily. | |
3687 | */ | |
3688 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3689 | ||
67de1176 MX |
3690 | path->slots[0]++; |
3691 | if (inode->i_nlink != 1 || | |
3692 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3693 | goto cache_acl; | |
3694 | ||
3695 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
3696 | if (location.objectid != btrfs_ino(inode)) | |
3697 | goto cache_acl; | |
3698 | ||
3699 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3700 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3701 | struct btrfs_inode_ref *ref; | |
3702 | ||
3703 | ref = (struct btrfs_inode_ref *)ptr; | |
3704 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3705 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3706 | struct btrfs_inode_extref *extref; | |
3707 | ||
3708 | extref = (struct btrfs_inode_extref *)ptr; | |
3709 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3710 | extref); | |
3711 | } | |
2f7e33d4 | 3712 | cache_acl: |
46a53cca CM |
3713 | /* |
3714 | * try to precache a NULL acl entry for files that don't have | |
3715 | * any xattrs or acls | |
3716 | */ | |
33345d01 | 3717 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
63541927 FDBM |
3718 | btrfs_ino(inode), &first_xattr_slot); |
3719 | if (first_xattr_slot != -1) { | |
3720 | path->slots[0] = first_xattr_slot; | |
3721 | ret = btrfs_load_inode_props(inode, path); | |
3722 | if (ret) | |
3723 | btrfs_err(root->fs_info, | |
351fd353 | 3724 | "error loading props for ino %llu (root %llu): %d", |
63541927 FDBM |
3725 | btrfs_ino(inode), |
3726 | root->root_key.objectid, ret); | |
3727 | } | |
3728 | btrfs_free_path(path); | |
3729 | ||
72c04902 AV |
3730 | if (!maybe_acls) |
3731 | cache_no_acl(inode); | |
46a53cca | 3732 | |
39279cc3 | 3733 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3734 | case S_IFREG: |
3735 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3736 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3737 | inode->i_fop = &btrfs_file_operations; |
3738 | inode->i_op = &btrfs_file_inode_operations; | |
3739 | break; | |
3740 | case S_IFDIR: | |
3741 | inode->i_fop = &btrfs_dir_file_operations; | |
3742 | if (root == root->fs_info->tree_root) | |
3743 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
3744 | else | |
3745 | inode->i_op = &btrfs_dir_inode_operations; | |
3746 | break; | |
3747 | case S_IFLNK: | |
3748 | inode->i_op = &btrfs_symlink_inode_operations; | |
3749 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
3750 | break; | |
618e21d5 | 3751 | default: |
0279b4cd | 3752 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3753 | init_special_inode(inode, inode->i_mode, rdev); |
3754 | break; | |
39279cc3 | 3755 | } |
6cbff00f CH |
3756 | |
3757 | btrfs_update_iflags(inode); | |
39279cc3 CM |
3758 | return; |
3759 | ||
3760 | make_bad: | |
39279cc3 | 3761 | btrfs_free_path(path); |
39279cc3 CM |
3762 | make_bad_inode(inode); |
3763 | } | |
3764 | ||
d352ac68 CM |
3765 | /* |
3766 | * given a leaf and an inode, copy the inode fields into the leaf | |
3767 | */ | |
e02119d5 CM |
3768 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3769 | struct extent_buffer *leaf, | |
5f39d397 | 3770 | struct btrfs_inode_item *item, |
39279cc3 CM |
3771 | struct inode *inode) |
3772 | { | |
51fab693 LB |
3773 | struct btrfs_map_token token; |
3774 | ||
3775 | btrfs_init_map_token(&token); | |
5f39d397 | 3776 | |
51fab693 LB |
3777 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3778 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3779 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3780 | &token); | |
3781 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3782 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3783 | |
a937b979 | 3784 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3785 | inode->i_atime.tv_sec, &token); |
a937b979 | 3786 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3787 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3788 | |
a937b979 | 3789 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3790 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3791 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3792 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3793 | |
a937b979 | 3794 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3795 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3796 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3797 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3798 | |
9cc97d64 | 3799 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3800 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3801 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3802 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3803 | ||
51fab693 LB |
3804 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3805 | &token); | |
3806 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3807 | &token); | |
3808 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3809 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3810 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3811 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3812 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3813 | } |
3814 | ||
d352ac68 CM |
3815 | /* |
3816 | * copy everything in the in-memory inode into the btree. | |
3817 | */ | |
2115133f | 3818 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3819 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3820 | { |
3821 | struct btrfs_inode_item *inode_item; | |
3822 | struct btrfs_path *path; | |
5f39d397 | 3823 | struct extent_buffer *leaf; |
39279cc3 CM |
3824 | int ret; |
3825 | ||
3826 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3827 | if (!path) |
3828 | return -ENOMEM; | |
3829 | ||
b9473439 | 3830 | path->leave_spinning = 1; |
16cdcec7 MX |
3831 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3832 | 1); | |
39279cc3 CM |
3833 | if (ret) { |
3834 | if (ret > 0) | |
3835 | ret = -ENOENT; | |
3836 | goto failed; | |
3837 | } | |
3838 | ||
5f39d397 CM |
3839 | leaf = path->nodes[0]; |
3840 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3841 | struct btrfs_inode_item); |
39279cc3 | 3842 | |
e02119d5 | 3843 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3844 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3845 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3846 | ret = 0; |
3847 | failed: | |
39279cc3 CM |
3848 | btrfs_free_path(path); |
3849 | return ret; | |
3850 | } | |
3851 | ||
2115133f CM |
3852 | /* |
3853 | * copy everything in the in-memory inode into the btree. | |
3854 | */ | |
3855 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3856 | struct btrfs_root *root, struct inode *inode) | |
3857 | { | |
3858 | int ret; | |
3859 | ||
3860 | /* | |
3861 | * If the inode is a free space inode, we can deadlock during commit | |
3862 | * if we put it into the delayed code. | |
3863 | * | |
3864 | * The data relocation inode should also be directly updated | |
3865 | * without delay | |
3866 | */ | |
83eea1f1 | 3867 | if (!btrfs_is_free_space_inode(inode) |
1d52c78a JB |
3868 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
3869 | && !root->fs_info->log_root_recovering) { | |
8ea05e3a AB |
3870 | btrfs_update_root_times(trans, root); |
3871 | ||
2115133f CM |
3872 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3873 | if (!ret) | |
3874 | btrfs_set_inode_last_trans(trans, inode); | |
3875 | return ret; | |
3876 | } | |
3877 | ||
3878 | return btrfs_update_inode_item(trans, root, inode); | |
3879 | } | |
3880 | ||
be6aef60 JB |
3881 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3882 | struct btrfs_root *root, | |
3883 | struct inode *inode) | |
2115133f CM |
3884 | { |
3885 | int ret; | |
3886 | ||
3887 | ret = btrfs_update_inode(trans, root, inode); | |
3888 | if (ret == -ENOSPC) | |
3889 | return btrfs_update_inode_item(trans, root, inode); | |
3890 | return ret; | |
3891 | } | |
3892 | ||
d352ac68 CM |
3893 | /* |
3894 | * unlink helper that gets used here in inode.c and in the tree logging | |
3895 | * recovery code. It remove a link in a directory with a given name, and | |
3896 | * also drops the back refs in the inode to the directory | |
3897 | */ | |
92986796 AV |
3898 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3899 | struct btrfs_root *root, | |
3900 | struct inode *dir, struct inode *inode, | |
3901 | const char *name, int name_len) | |
39279cc3 CM |
3902 | { |
3903 | struct btrfs_path *path; | |
39279cc3 | 3904 | int ret = 0; |
5f39d397 | 3905 | struct extent_buffer *leaf; |
39279cc3 | 3906 | struct btrfs_dir_item *di; |
5f39d397 | 3907 | struct btrfs_key key; |
aec7477b | 3908 | u64 index; |
33345d01 LZ |
3909 | u64 ino = btrfs_ino(inode); |
3910 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3911 | |
3912 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3913 | if (!path) { |
3914 | ret = -ENOMEM; | |
554233a6 | 3915 | goto out; |
54aa1f4d CM |
3916 | } |
3917 | ||
b9473439 | 3918 | path->leave_spinning = 1; |
33345d01 | 3919 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
3920 | name, name_len, -1); |
3921 | if (IS_ERR(di)) { | |
3922 | ret = PTR_ERR(di); | |
3923 | goto err; | |
3924 | } | |
3925 | if (!di) { | |
3926 | ret = -ENOENT; | |
3927 | goto err; | |
3928 | } | |
5f39d397 CM |
3929 | leaf = path->nodes[0]; |
3930 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 3931 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3932 | if (ret) |
3933 | goto err; | |
b3b4aa74 | 3934 | btrfs_release_path(path); |
39279cc3 | 3935 | |
67de1176 MX |
3936 | /* |
3937 | * If we don't have dir index, we have to get it by looking up | |
3938 | * the inode ref, since we get the inode ref, remove it directly, | |
3939 | * it is unnecessary to do delayed deletion. | |
3940 | * | |
3941 | * But if we have dir index, needn't search inode ref to get it. | |
3942 | * Since the inode ref is close to the inode item, it is better | |
3943 | * that we delay to delete it, and just do this deletion when | |
3944 | * we update the inode item. | |
3945 | */ | |
3946 | if (BTRFS_I(inode)->dir_index) { | |
3947 | ret = btrfs_delayed_delete_inode_ref(inode); | |
3948 | if (!ret) { | |
3949 | index = BTRFS_I(inode)->dir_index; | |
3950 | goto skip_backref; | |
3951 | } | |
3952 | } | |
3953 | ||
33345d01 LZ |
3954 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
3955 | dir_ino, &index); | |
aec7477b | 3956 | if (ret) { |
c2cf52eb SK |
3957 | btrfs_info(root->fs_info, |
3958 | "failed to delete reference to %.*s, inode %llu parent %llu", | |
c1c9ff7c | 3959 | name_len, name, ino, dir_ino); |
79787eaa | 3960 | btrfs_abort_transaction(trans, root, ret); |
aec7477b JB |
3961 | goto err; |
3962 | } | |
67de1176 | 3963 | skip_backref: |
16cdcec7 | 3964 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
3965 | if (ret) { |
3966 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 3967 | goto err; |
79787eaa | 3968 | } |
39279cc3 | 3969 | |
e02119d5 | 3970 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
33345d01 | 3971 | inode, dir_ino); |
79787eaa JM |
3972 | if (ret != 0 && ret != -ENOENT) { |
3973 | btrfs_abort_transaction(trans, root, ret); | |
3974 | goto err; | |
3975 | } | |
e02119d5 CM |
3976 | |
3977 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | |
3978 | dir, index); | |
6418c961 CM |
3979 | if (ret == -ENOENT) |
3980 | ret = 0; | |
d4e3991b ZB |
3981 | else if (ret) |
3982 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 CM |
3983 | err: |
3984 | btrfs_free_path(path); | |
e02119d5 CM |
3985 | if (ret) |
3986 | goto out; | |
3987 | ||
3988 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 JB |
3989 | inode_inc_iversion(inode); |
3990 | inode_inc_iversion(dir); | |
e02119d5 | 3991 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
b9959295 | 3992 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 3993 | out: |
39279cc3 CM |
3994 | return ret; |
3995 | } | |
3996 | ||
92986796 AV |
3997 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3998 | struct btrfs_root *root, | |
3999 | struct inode *dir, struct inode *inode, | |
4000 | const char *name, int name_len) | |
4001 | { | |
4002 | int ret; | |
4003 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4004 | if (!ret) { | |
8b558c5f | 4005 | drop_nlink(inode); |
92986796 AV |
4006 | ret = btrfs_update_inode(trans, root, inode); |
4007 | } | |
4008 | return ret; | |
4009 | } | |
39279cc3 | 4010 | |
a22285a6 YZ |
4011 | /* |
4012 | * helper to start transaction for unlink and rmdir. | |
4013 | * | |
d52be818 JB |
4014 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4015 | * if we cannot make our reservations the normal way try and see if there is | |
4016 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4017 | * allow the unlink to occur. | |
a22285a6 | 4018 | */ |
d52be818 | 4019 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4020 | { |
39279cc3 | 4021 | struct btrfs_trans_handle *trans; |
a22285a6 | 4022 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d YZ |
4023 | int ret; |
4024 | ||
e70bea5f JB |
4025 | /* |
4026 | * 1 for the possible orphan item | |
4027 | * 1 for the dir item | |
4028 | * 1 for the dir index | |
4029 | * 1 for the inode ref | |
e70bea5f JB |
4030 | * 1 for the inode |
4031 | */ | |
6e137ed3 | 4032 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
4033 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
4034 | return trans; | |
4df27c4d | 4035 | |
d52be818 JB |
4036 | if (PTR_ERR(trans) == -ENOSPC) { |
4037 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); | |
4df27c4d | 4038 | |
d52be818 JB |
4039 | trans = btrfs_start_transaction(root, 0); |
4040 | if (IS_ERR(trans)) | |
4041 | return trans; | |
4042 | ret = btrfs_cond_migrate_bytes(root->fs_info, | |
4043 | &root->fs_info->trans_block_rsv, | |
4044 | num_bytes, 5); | |
4045 | if (ret) { | |
4046 | btrfs_end_transaction(trans, root); | |
4047 | return ERR_PTR(ret); | |
a22285a6 | 4048 | } |
5a77d76c | 4049 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
d52be818 | 4050 | trans->bytes_reserved = num_bytes; |
a22285a6 | 4051 | } |
d52be818 | 4052 | return trans; |
a22285a6 YZ |
4053 | } |
4054 | ||
4055 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4056 | { | |
4057 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4058 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4059 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4060 | int ret; |
a22285a6 | 4061 | |
d52be818 | 4062 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4063 | if (IS_ERR(trans)) |
4064 | return PTR_ERR(trans); | |
5f39d397 | 4065 | |
2b0143b5 | 4066 | btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0); |
12fcfd22 | 4067 | |
2b0143b5 | 4068 | ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4069 | dentry->d_name.name, dentry->d_name.len); |
b532402e TI |
4070 | if (ret) |
4071 | goto out; | |
7b128766 | 4072 | |
a22285a6 | 4073 | if (inode->i_nlink == 0) { |
7b128766 | 4074 | ret = btrfs_orphan_add(trans, inode); |
b532402e TI |
4075 | if (ret) |
4076 | goto out; | |
a22285a6 | 4077 | } |
7b128766 | 4078 | |
b532402e | 4079 | out: |
d52be818 | 4080 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4081 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
4082 | return ret; |
4083 | } | |
4084 | ||
4df27c4d YZ |
4085 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4086 | struct btrfs_root *root, | |
4087 | struct inode *dir, u64 objectid, | |
4088 | const char *name, int name_len) | |
4089 | { | |
4090 | struct btrfs_path *path; | |
4091 | struct extent_buffer *leaf; | |
4092 | struct btrfs_dir_item *di; | |
4093 | struct btrfs_key key; | |
4094 | u64 index; | |
4095 | int ret; | |
33345d01 | 4096 | u64 dir_ino = btrfs_ino(dir); |
4df27c4d YZ |
4097 | |
4098 | path = btrfs_alloc_path(); | |
4099 | if (!path) | |
4100 | return -ENOMEM; | |
4101 | ||
33345d01 | 4102 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4103 | name, name_len, -1); |
79787eaa JM |
4104 | if (IS_ERR_OR_NULL(di)) { |
4105 | if (!di) | |
4106 | ret = -ENOENT; | |
4107 | else | |
4108 | ret = PTR_ERR(di); | |
4109 | goto out; | |
4110 | } | |
4df27c4d YZ |
4111 | |
4112 | leaf = path->nodes[0]; | |
4113 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4114 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4115 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa JM |
4116 | if (ret) { |
4117 | btrfs_abort_transaction(trans, root, ret); | |
4118 | goto out; | |
4119 | } | |
b3b4aa74 | 4120 | btrfs_release_path(path); |
4df27c4d YZ |
4121 | |
4122 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
4123 | objectid, root->root_key.objectid, | |
33345d01 | 4124 | dir_ino, &index, name, name_len); |
4df27c4d | 4125 | if (ret < 0) { |
79787eaa JM |
4126 | if (ret != -ENOENT) { |
4127 | btrfs_abort_transaction(trans, root, ret); | |
4128 | goto out; | |
4129 | } | |
33345d01 | 4130 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4131 | name, name_len); |
79787eaa JM |
4132 | if (IS_ERR_OR_NULL(di)) { |
4133 | if (!di) | |
4134 | ret = -ENOENT; | |
4135 | else | |
4136 | ret = PTR_ERR(di); | |
4137 | btrfs_abort_transaction(trans, root, ret); | |
4138 | goto out; | |
4139 | } | |
4df27c4d YZ |
4140 | |
4141 | leaf = path->nodes[0]; | |
4142 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4143 | btrfs_release_path(path); |
4df27c4d YZ |
4144 | index = key.offset; |
4145 | } | |
945d8962 | 4146 | btrfs_release_path(path); |
4df27c4d | 4147 | |
16cdcec7 | 4148 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
4149 | if (ret) { |
4150 | btrfs_abort_transaction(trans, root, ret); | |
4151 | goto out; | |
4152 | } | |
4df27c4d YZ |
4153 | |
4154 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 | 4155 | inode_inc_iversion(dir); |
4df27c4d | 4156 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
5a24e84c | 4157 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa JM |
4158 | if (ret) |
4159 | btrfs_abort_transaction(trans, root, ret); | |
4160 | out: | |
71d7aed0 | 4161 | btrfs_free_path(path); |
79787eaa | 4162 | return ret; |
4df27c4d YZ |
4163 | } |
4164 | ||
39279cc3 CM |
4165 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4166 | { | |
2b0143b5 | 4167 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4168 | int err = 0; |
39279cc3 | 4169 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4170 | struct btrfs_trans_handle *trans; |
39279cc3 | 4171 | |
b3ae244e | 4172 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4173 | return -ENOTEMPTY; |
b3ae244e DS |
4174 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
4175 | return -EPERM; | |
134d4512 | 4176 | |
d52be818 | 4177 | trans = __unlink_start_trans(dir); |
a22285a6 | 4178 | if (IS_ERR(trans)) |
5df6a9f6 | 4179 | return PTR_ERR(trans); |
5df6a9f6 | 4180 | |
33345d01 | 4181 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4182 | err = btrfs_unlink_subvol(trans, root, dir, |
4183 | BTRFS_I(inode)->location.objectid, | |
4184 | dentry->d_name.name, | |
4185 | dentry->d_name.len); | |
4186 | goto out; | |
4187 | } | |
4188 | ||
7b128766 JB |
4189 | err = btrfs_orphan_add(trans, inode); |
4190 | if (err) | |
4df27c4d | 4191 | goto out; |
7b128766 | 4192 | |
39279cc3 | 4193 | /* now the directory is empty */ |
2b0143b5 | 4194 | err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4195 | dentry->d_name.name, dentry->d_name.len); |
d397712b | 4196 | if (!err) |
dbe674a9 | 4197 | btrfs_i_size_write(inode, 0); |
4df27c4d | 4198 | out: |
d52be818 | 4199 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4200 | btrfs_btree_balance_dirty(root); |
3954401f | 4201 | |
39279cc3 CM |
4202 | return err; |
4203 | } | |
4204 | ||
28f75a0e CM |
4205 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4206 | struct btrfs_root *root, | |
4207 | u64 bytes_deleted) | |
4208 | { | |
4209 | int ret; | |
4210 | ||
4211 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); | |
4212 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, | |
4213 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); | |
4214 | if (!ret) | |
4215 | trans->bytes_reserved += bytes_deleted; | |
4216 | return ret; | |
4217 | ||
4218 | } | |
4219 | ||
39279cc3 CM |
4220 | /* |
4221 | * this can truncate away extent items, csum items and directory items. | |
4222 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4223 | * any higher than new_size |
39279cc3 CM |
4224 | * |
4225 | * csum items that cross the new i_size are truncated to the new size | |
4226 | * as well. | |
7b128766 JB |
4227 | * |
4228 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4229 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4230 | */ |
8082510e YZ |
4231 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4232 | struct btrfs_root *root, | |
4233 | struct inode *inode, | |
4234 | u64 new_size, u32 min_type) | |
39279cc3 | 4235 | { |
39279cc3 | 4236 | struct btrfs_path *path; |
5f39d397 | 4237 | struct extent_buffer *leaf; |
39279cc3 | 4238 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4239 | struct btrfs_key key; |
4240 | struct btrfs_key found_key; | |
39279cc3 | 4241 | u64 extent_start = 0; |
db94535d | 4242 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4243 | u64 extent_offset = 0; |
39279cc3 | 4244 | u64 item_end = 0; |
c1aa4575 | 4245 | u64 last_size = new_size; |
8082510e | 4246 | u32 found_type = (u8)-1; |
39279cc3 CM |
4247 | int found_extent; |
4248 | int del_item; | |
85e21bac CM |
4249 | int pending_del_nr = 0; |
4250 | int pending_del_slot = 0; | |
179e29e4 | 4251 | int extent_type = -1; |
8082510e YZ |
4252 | int ret; |
4253 | int err = 0; | |
33345d01 | 4254 | u64 ino = btrfs_ino(inode); |
28ed1345 | 4255 | u64 bytes_deleted = 0; |
1262133b JB |
4256 | bool be_nice = 0; |
4257 | bool should_throttle = 0; | |
28f75a0e | 4258 | bool should_end = 0; |
8082510e YZ |
4259 | |
4260 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4261 | |
28ed1345 CM |
4262 | /* |
4263 | * for non-free space inodes and ref cows, we want to back off from | |
4264 | * time to time | |
4265 | */ | |
4266 | if (!btrfs_is_free_space_inode(inode) && | |
4267 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4268 | be_nice = 1; | |
4269 | ||
0eb0e19c MF |
4270 | path = btrfs_alloc_path(); |
4271 | if (!path) | |
4272 | return -ENOMEM; | |
4273 | path->reada = -1; | |
4274 | ||
5dc562c5 JB |
4275 | /* |
4276 | * We want to drop from the next block forward in case this new size is | |
4277 | * not block aligned since we will be keeping the last block of the | |
4278 | * extent just the way it is. | |
4279 | */ | |
27cdeb70 MX |
4280 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
4281 | root == root->fs_info->tree_root) | |
fda2832f QW |
4282 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
4283 | root->sectorsize), (u64)-1, 0); | |
8082510e | 4284 | |
16cdcec7 MX |
4285 | /* |
4286 | * This function is also used to drop the items in the log tree before | |
4287 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4288 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4289 | * items. | |
4290 | */ | |
4291 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4292 | btrfs_kill_delayed_inode_items(inode); | |
4293 | ||
33345d01 | 4294 | key.objectid = ino; |
39279cc3 | 4295 | key.offset = (u64)-1; |
5f39d397 CM |
4296 | key.type = (u8)-1; |
4297 | ||
85e21bac | 4298 | search_again: |
28ed1345 CM |
4299 | /* |
4300 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4301 | * up a huge file in a single leaf. Most of the time that | |
4302 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4303 | */ | |
4304 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { | |
4305 | if (btrfs_should_end_transaction(trans, root)) { | |
4306 | err = -EAGAIN; | |
4307 | goto error; | |
4308 | } | |
4309 | } | |
4310 | ||
4311 | ||
b9473439 | 4312 | path->leave_spinning = 1; |
85e21bac | 4313 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4314 | if (ret < 0) { |
4315 | err = ret; | |
4316 | goto out; | |
4317 | } | |
d397712b | 4318 | |
85e21bac | 4319 | if (ret > 0) { |
e02119d5 CM |
4320 | /* there are no items in the tree for us to truncate, we're |
4321 | * done | |
4322 | */ | |
8082510e YZ |
4323 | if (path->slots[0] == 0) |
4324 | goto out; | |
85e21bac CM |
4325 | path->slots[0]--; |
4326 | } | |
4327 | ||
d397712b | 4328 | while (1) { |
39279cc3 | 4329 | fi = NULL; |
5f39d397 CM |
4330 | leaf = path->nodes[0]; |
4331 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4332 | found_type = found_key.type; |
39279cc3 | 4333 | |
33345d01 | 4334 | if (found_key.objectid != ino) |
39279cc3 | 4335 | break; |
5f39d397 | 4336 | |
85e21bac | 4337 | if (found_type < min_type) |
39279cc3 CM |
4338 | break; |
4339 | ||
5f39d397 | 4340 | item_end = found_key.offset; |
39279cc3 | 4341 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4342 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4343 | struct btrfs_file_extent_item); |
179e29e4 CM |
4344 | extent_type = btrfs_file_extent_type(leaf, fi); |
4345 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4346 | item_end += |
db94535d | 4347 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 | 4348 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4349 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4350 | path->slots[0], fi); |
39279cc3 | 4351 | } |
008630c1 | 4352 | item_end--; |
39279cc3 | 4353 | } |
8082510e YZ |
4354 | if (found_type > min_type) { |
4355 | del_item = 1; | |
4356 | } else { | |
4357 | if (item_end < new_size) | |
b888db2b | 4358 | break; |
8082510e YZ |
4359 | if (found_key.offset >= new_size) |
4360 | del_item = 1; | |
4361 | else | |
4362 | del_item = 0; | |
39279cc3 | 4363 | } |
39279cc3 | 4364 | found_extent = 0; |
39279cc3 | 4365 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4366 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4367 | goto delete; | |
4368 | ||
7f4f6e0a JB |
4369 | if (del_item) |
4370 | last_size = found_key.offset; | |
4371 | else | |
4372 | last_size = new_size; | |
4373 | ||
179e29e4 | 4374 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4375 | u64 num_dec; |
db94535d | 4376 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4377 | if (!del_item) { |
db94535d CM |
4378 | u64 orig_num_bytes = |
4379 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4380 | extent_num_bytes = ALIGN(new_size - |
4381 | found_key.offset, | |
4382 | root->sectorsize); | |
db94535d CM |
4383 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4384 | extent_num_bytes); | |
4385 | num_dec = (orig_num_bytes - | |
9069218d | 4386 | extent_num_bytes); |
27cdeb70 MX |
4387 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4388 | &root->state) && | |
4389 | extent_start != 0) | |
a76a3cd4 | 4390 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4391 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4392 | } else { |
db94535d CM |
4393 | extent_num_bytes = |
4394 | btrfs_file_extent_disk_num_bytes(leaf, | |
4395 | fi); | |
5d4f98a2 YZ |
4396 | extent_offset = found_key.offset - |
4397 | btrfs_file_extent_offset(leaf, fi); | |
4398 | ||
39279cc3 | 4399 | /* FIXME blocksize != 4096 */ |
9069218d | 4400 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4401 | if (extent_start != 0) { |
4402 | found_extent = 1; | |
27cdeb70 MX |
4403 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4404 | &root->state)) | |
a76a3cd4 | 4405 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4406 | } |
39279cc3 | 4407 | } |
9069218d | 4408 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4409 | /* |
4410 | * we can't truncate inline items that have had | |
4411 | * special encodings | |
4412 | */ | |
4413 | if (!del_item && | |
4414 | btrfs_file_extent_compression(leaf, fi) == 0 && | |
4415 | btrfs_file_extent_encryption(leaf, fi) == 0 && | |
4416 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
e02119d5 CM |
4417 | u32 size = new_size - found_key.offset; |
4418 | ||
27cdeb70 | 4419 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
a76a3cd4 YZ |
4420 | inode_sub_bytes(inode, item_end + 1 - |
4421 | new_size); | |
514ac8ad CM |
4422 | |
4423 | /* | |
4424 | * update the ram bytes to properly reflect | |
4425 | * the new size of our item | |
4426 | */ | |
4427 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
e02119d5 CM |
4428 | size = |
4429 | btrfs_file_extent_calc_inline_size(size); | |
afe5fea7 | 4430 | btrfs_truncate_item(root, path, size, 1); |
27cdeb70 MX |
4431 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4432 | &root->state)) { | |
a76a3cd4 YZ |
4433 | inode_sub_bytes(inode, item_end + 1 - |
4434 | found_key.offset); | |
9069218d | 4435 | } |
39279cc3 | 4436 | } |
179e29e4 | 4437 | delete: |
39279cc3 | 4438 | if (del_item) { |
85e21bac CM |
4439 | if (!pending_del_nr) { |
4440 | /* no pending yet, add ourselves */ | |
4441 | pending_del_slot = path->slots[0]; | |
4442 | pending_del_nr = 1; | |
4443 | } else if (pending_del_nr && | |
4444 | path->slots[0] + 1 == pending_del_slot) { | |
4445 | /* hop on the pending chunk */ | |
4446 | pending_del_nr++; | |
4447 | pending_del_slot = path->slots[0]; | |
4448 | } else { | |
d397712b | 4449 | BUG(); |
85e21bac | 4450 | } |
39279cc3 CM |
4451 | } else { |
4452 | break; | |
4453 | } | |
28f75a0e CM |
4454 | should_throttle = 0; |
4455 | ||
27cdeb70 MX |
4456 | if (found_extent && |
4457 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
4458 | root == root->fs_info->tree_root)) { | |
b9473439 | 4459 | btrfs_set_path_blocking(path); |
28ed1345 | 4460 | bytes_deleted += extent_num_bytes; |
39279cc3 | 4461 | ret = btrfs_free_extent(trans, root, extent_start, |
5d4f98a2 YZ |
4462 | extent_num_bytes, 0, |
4463 | btrfs_header_owner(leaf), | |
66d7e7f0 | 4464 | ino, extent_offset, 0); |
39279cc3 | 4465 | BUG_ON(ret); |
1262133b | 4466 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
28ed1345 CM |
4467 | btrfs_async_run_delayed_refs(root, |
4468 | trans->delayed_ref_updates * 2, 0); | |
28f75a0e CM |
4469 | if (be_nice) { |
4470 | if (truncate_space_check(trans, root, | |
4471 | extent_num_bytes)) { | |
4472 | should_end = 1; | |
4473 | } | |
4474 | if (btrfs_should_throttle_delayed_refs(trans, | |
4475 | root)) { | |
4476 | should_throttle = 1; | |
4477 | } | |
4478 | } | |
39279cc3 | 4479 | } |
85e21bac | 4480 | |
8082510e YZ |
4481 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4482 | break; | |
4483 | ||
4484 | if (path->slots[0] == 0 || | |
1262133b | 4485 | path->slots[0] != pending_del_slot || |
28f75a0e | 4486 | should_throttle || should_end) { |
8082510e YZ |
4487 | if (pending_del_nr) { |
4488 | ret = btrfs_del_items(trans, root, path, | |
4489 | pending_del_slot, | |
4490 | pending_del_nr); | |
79787eaa JM |
4491 | if (ret) { |
4492 | btrfs_abort_transaction(trans, | |
4493 | root, ret); | |
4494 | goto error; | |
4495 | } | |
8082510e YZ |
4496 | pending_del_nr = 0; |
4497 | } | |
b3b4aa74 | 4498 | btrfs_release_path(path); |
28f75a0e | 4499 | if (should_throttle) { |
1262133b JB |
4500 | unsigned long updates = trans->delayed_ref_updates; |
4501 | if (updates) { | |
4502 | trans->delayed_ref_updates = 0; | |
4503 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4504 | if (ret && !err) | |
4505 | err = ret; | |
4506 | } | |
4507 | } | |
28f75a0e CM |
4508 | /* |
4509 | * if we failed to refill our space rsv, bail out | |
4510 | * and let the transaction restart | |
4511 | */ | |
4512 | if (should_end) { | |
4513 | err = -EAGAIN; | |
4514 | goto error; | |
4515 | } | |
85e21bac | 4516 | goto search_again; |
8082510e YZ |
4517 | } else { |
4518 | path->slots[0]--; | |
85e21bac | 4519 | } |
39279cc3 | 4520 | } |
8082510e | 4521 | out: |
85e21bac CM |
4522 | if (pending_del_nr) { |
4523 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4524 | pending_del_nr); | |
79787eaa JM |
4525 | if (ret) |
4526 | btrfs_abort_transaction(trans, root, ret); | |
85e21bac | 4527 | } |
79787eaa | 4528 | error: |
c1aa4575 | 4529 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
7f4f6e0a | 4530 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
28ed1345 | 4531 | |
39279cc3 | 4532 | btrfs_free_path(path); |
28ed1345 | 4533 | |
28f75a0e | 4534 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { |
28ed1345 CM |
4535 | unsigned long updates = trans->delayed_ref_updates; |
4536 | if (updates) { | |
4537 | trans->delayed_ref_updates = 0; | |
4538 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4539 | if (ret && !err) | |
4540 | err = ret; | |
4541 | } | |
4542 | } | |
8082510e | 4543 | return err; |
39279cc3 CM |
4544 | } |
4545 | ||
4546 | /* | |
2aaa6655 JB |
4547 | * btrfs_truncate_page - read, zero a chunk and write a page |
4548 | * @inode - inode that we're zeroing | |
4549 | * @from - the offset to start zeroing | |
4550 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4551 | * offset | |
4552 | * @front - zero up to the offset instead of from the offset on | |
4553 | * | |
4554 | * This will find the page for the "from" offset and cow the page and zero the | |
4555 | * part we want to zero. This is used with truncate and hole punching. | |
39279cc3 | 4556 | */ |
2aaa6655 JB |
4557 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
4558 | int front) | |
39279cc3 | 4559 | { |
2aaa6655 | 4560 | struct address_space *mapping = inode->i_mapping; |
db94535d | 4561 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
4562 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4563 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4564 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 4565 | char *kaddr; |
db94535d | 4566 | u32 blocksize = root->sectorsize; |
39279cc3 CM |
4567 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
4568 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
4569 | struct page *page; | |
3b16a4e3 | 4570 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4571 | int ret = 0; |
a52d9a80 | 4572 | u64 page_start; |
e6dcd2dc | 4573 | u64 page_end; |
39279cc3 | 4574 | |
2aaa6655 JB |
4575 | if ((offset & (blocksize - 1)) == 0 && |
4576 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4577 | goto out; |
0ca1f7ce | 4578 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
5d5e103a JB |
4579 | if (ret) |
4580 | goto out; | |
39279cc3 | 4581 | |
211c17f5 | 4582 | again: |
3b16a4e3 | 4583 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4584 | if (!page) { |
0ca1f7ce | 4585 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
ac6a2b36 | 4586 | ret = -ENOMEM; |
39279cc3 | 4587 | goto out; |
5d5e103a | 4588 | } |
e6dcd2dc CM |
4589 | |
4590 | page_start = page_offset(page); | |
4591 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
4592 | ||
39279cc3 | 4593 | if (!PageUptodate(page)) { |
9ebefb18 | 4594 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4595 | lock_page(page); |
211c17f5 CM |
4596 | if (page->mapping != mapping) { |
4597 | unlock_page(page); | |
4598 | page_cache_release(page); | |
4599 | goto again; | |
4600 | } | |
39279cc3 CM |
4601 | if (!PageUptodate(page)) { |
4602 | ret = -EIO; | |
89642229 | 4603 | goto out_unlock; |
39279cc3 CM |
4604 | } |
4605 | } | |
211c17f5 | 4606 | wait_on_page_writeback(page); |
e6dcd2dc | 4607 | |
d0082371 | 4608 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
4609 | set_page_extent_mapped(page); |
4610 | ||
4611 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
4612 | if (ordered) { | |
2ac55d41 JB |
4613 | unlock_extent_cached(io_tree, page_start, page_end, |
4614 | &cached_state, GFP_NOFS); | |
e6dcd2dc CM |
4615 | unlock_page(page); |
4616 | page_cache_release(page); | |
eb84ae03 | 4617 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4618 | btrfs_put_ordered_extent(ordered); |
4619 | goto again; | |
4620 | } | |
4621 | ||
2ac55d41 | 4622 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
4623 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4624 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4625 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4626 | |
2ac55d41 JB |
4627 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
4628 | &cached_state); | |
9ed74f2d | 4629 | if (ret) { |
2ac55d41 JB |
4630 | unlock_extent_cached(io_tree, page_start, page_end, |
4631 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
4632 | goto out_unlock; |
4633 | } | |
4634 | ||
e6dcd2dc | 4635 | if (offset != PAGE_CACHE_SIZE) { |
2aaa6655 JB |
4636 | if (!len) |
4637 | len = PAGE_CACHE_SIZE - offset; | |
e6dcd2dc | 4638 | kaddr = kmap(page); |
2aaa6655 JB |
4639 | if (front) |
4640 | memset(kaddr, 0, offset); | |
4641 | else | |
4642 | memset(kaddr + offset, 0, len); | |
e6dcd2dc CM |
4643 | flush_dcache_page(page); |
4644 | kunmap(page); | |
4645 | } | |
247e743c | 4646 | ClearPageChecked(page); |
e6dcd2dc | 4647 | set_page_dirty(page); |
2ac55d41 JB |
4648 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
4649 | GFP_NOFS); | |
39279cc3 | 4650 | |
89642229 | 4651 | out_unlock: |
5d5e103a | 4652 | if (ret) |
0ca1f7ce | 4653 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
39279cc3 CM |
4654 | unlock_page(page); |
4655 | page_cache_release(page); | |
4656 | out: | |
4657 | return ret; | |
4658 | } | |
4659 | ||
16e7549f JB |
4660 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4661 | u64 offset, u64 len) | |
4662 | { | |
4663 | struct btrfs_trans_handle *trans; | |
4664 | int ret; | |
4665 | ||
4666 | /* | |
4667 | * Still need to make sure the inode looks like it's been updated so | |
4668 | * that any holes get logged if we fsync. | |
4669 | */ | |
4670 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { | |
4671 | BTRFS_I(inode)->last_trans = root->fs_info->generation; | |
4672 | BTRFS_I(inode)->last_sub_trans = root->log_transid; | |
4673 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4674 | return 0; | |
4675 | } | |
4676 | ||
4677 | /* | |
4678 | * 1 - for the one we're dropping | |
4679 | * 1 - for the one we're adding | |
4680 | * 1 - for updating the inode. | |
4681 | */ | |
4682 | trans = btrfs_start_transaction(root, 3); | |
4683 | if (IS_ERR(trans)) | |
4684 | return PTR_ERR(trans); | |
4685 | ||
4686 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4687 | if (ret) { | |
4688 | btrfs_abort_transaction(trans, root, ret); | |
4689 | btrfs_end_transaction(trans, root); | |
4690 | return ret; | |
4691 | } | |
4692 | ||
4693 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, | |
4694 | 0, 0, len, 0, len, 0, 0, 0); | |
4695 | if (ret) | |
4696 | btrfs_abort_transaction(trans, root, ret); | |
4697 | else | |
4698 | btrfs_update_inode(trans, root, inode); | |
4699 | btrfs_end_transaction(trans, root); | |
4700 | return ret; | |
4701 | } | |
4702 | ||
695a0d0d JB |
4703 | /* |
4704 | * This function puts in dummy file extents for the area we're creating a hole | |
4705 | * for. So if we are truncating this file to a larger size we need to insert | |
4706 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4707 | * the range between oldsize and size | |
4708 | */ | |
a41ad394 | 4709 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4710 | { |
9036c102 YZ |
4711 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4712 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4713 | struct extent_map *em = NULL; |
2ac55d41 | 4714 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4715 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
fda2832f QW |
4716 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
4717 | u64 block_end = ALIGN(size, root->sectorsize); | |
9036c102 YZ |
4718 | u64 last_byte; |
4719 | u64 cur_offset; | |
4720 | u64 hole_size; | |
9ed74f2d | 4721 | int err = 0; |
39279cc3 | 4722 | |
a71754fc JB |
4723 | /* |
4724 | * If our size started in the middle of a page we need to zero out the | |
4725 | * rest of the page before we expand the i_size, otherwise we could | |
4726 | * expose stale data. | |
4727 | */ | |
4728 | err = btrfs_truncate_page(inode, oldsize, 0, 0); | |
4729 | if (err) | |
4730 | return err; | |
4731 | ||
9036c102 YZ |
4732 | if (size <= hole_start) |
4733 | return 0; | |
4734 | ||
9036c102 YZ |
4735 | while (1) { |
4736 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4737 | |
2ac55d41 | 4738 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
d0082371 | 4739 | &cached_state); |
fa7c1494 MX |
4740 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
4741 | block_end - hole_start); | |
9036c102 YZ |
4742 | if (!ordered) |
4743 | break; | |
2ac55d41 JB |
4744 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4745 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4746 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4747 | btrfs_put_ordered_extent(ordered); |
4748 | } | |
39279cc3 | 4749 | |
9036c102 YZ |
4750 | cur_offset = hole_start; |
4751 | while (1) { | |
4752 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
4753 | block_end - cur_offset, 0); | |
79787eaa JM |
4754 | if (IS_ERR(em)) { |
4755 | err = PTR_ERR(em); | |
f2767956 | 4756 | em = NULL; |
79787eaa JM |
4757 | break; |
4758 | } | |
9036c102 | 4759 | last_byte = min(extent_map_end(em), block_end); |
fda2832f | 4760 | last_byte = ALIGN(last_byte , root->sectorsize); |
8082510e | 4761 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4762 | struct extent_map *hole_em; |
9036c102 | 4763 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4764 | |
16e7549f JB |
4765 | err = maybe_insert_hole(root, inode, cur_offset, |
4766 | hole_size); | |
4767 | if (err) | |
3893e33b | 4768 | break; |
5dc562c5 JB |
4769 | btrfs_drop_extent_cache(inode, cur_offset, |
4770 | cur_offset + hole_size - 1, 0); | |
4771 | hole_em = alloc_extent_map(); | |
4772 | if (!hole_em) { | |
4773 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4774 | &BTRFS_I(inode)->runtime_flags); | |
4775 | goto next; | |
4776 | } | |
4777 | hole_em->start = cur_offset; | |
4778 | hole_em->len = hole_size; | |
4779 | hole_em->orig_start = cur_offset; | |
8082510e | 4780 | |
5dc562c5 JB |
4781 | hole_em->block_start = EXTENT_MAP_HOLE; |
4782 | hole_em->block_len = 0; | |
b4939680 | 4783 | hole_em->orig_block_len = 0; |
cc95bef6 | 4784 | hole_em->ram_bytes = hole_size; |
5dc562c5 JB |
4785 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
4786 | hole_em->compress_type = BTRFS_COMPRESS_NONE; | |
16e7549f | 4787 | hole_em->generation = root->fs_info->generation; |
8082510e | 4788 | |
5dc562c5 JB |
4789 | while (1) { |
4790 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4791 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4792 | write_unlock(&em_tree->lock); |
4793 | if (err != -EEXIST) | |
4794 | break; | |
4795 | btrfs_drop_extent_cache(inode, cur_offset, | |
4796 | cur_offset + | |
4797 | hole_size - 1, 0); | |
4798 | } | |
4799 | free_extent_map(hole_em); | |
9036c102 | 4800 | } |
16e7549f | 4801 | next: |
9036c102 | 4802 | free_extent_map(em); |
a22285a6 | 4803 | em = NULL; |
9036c102 | 4804 | cur_offset = last_byte; |
8082510e | 4805 | if (cur_offset >= block_end) |
9036c102 YZ |
4806 | break; |
4807 | } | |
a22285a6 | 4808 | free_extent_map(em); |
2ac55d41 JB |
4809 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
4810 | GFP_NOFS); | |
9036c102 YZ |
4811 | return err; |
4812 | } | |
39279cc3 | 4813 | |
9ea24bbe FM |
4814 | static int wait_snapshoting_atomic_t(atomic_t *a) |
4815 | { | |
4816 | schedule(); | |
4817 | return 0; | |
4818 | } | |
4819 | ||
4820 | static void wait_for_snapshot_creation(struct btrfs_root *root) | |
4821 | { | |
4822 | while (true) { | |
4823 | int ret; | |
4824 | ||
4825 | ret = btrfs_start_write_no_snapshoting(root); | |
4826 | if (ret) | |
4827 | break; | |
4828 | wait_on_atomic_t(&root->will_be_snapshoted, | |
4829 | wait_snapshoting_atomic_t, | |
4830 | TASK_UNINTERRUPTIBLE); | |
4831 | } | |
4832 | } | |
4833 | ||
3972f260 | 4834 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4835 | { |
f4a2f4c5 MX |
4836 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4837 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4838 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4839 | loff_t newsize = attr->ia_size; |
4840 | int mask = attr->ia_valid; | |
8082510e YZ |
4841 | int ret; |
4842 | ||
3972f260 ES |
4843 | /* |
4844 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4845 | * special case where we need to update the times despite not having | |
4846 | * these flags set. For all other operations the VFS set these flags | |
4847 | * explicitly if it wants a timestamp update. | |
4848 | */ | |
dff6efc3 CH |
4849 | if (newsize != oldsize) { |
4850 | inode_inc_iversion(inode); | |
4851 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4852 | inode->i_ctime = inode->i_mtime = | |
4853 | current_fs_time(inode->i_sb); | |
4854 | } | |
3972f260 | 4855 | |
a41ad394 | 4856 | if (newsize > oldsize) { |
7caef267 | 4857 | truncate_pagecache(inode, newsize); |
9ea24bbe FM |
4858 | /* |
4859 | * Don't do an expanding truncate while snapshoting is ongoing. | |
4860 | * This is to ensure the snapshot captures a fully consistent | |
4861 | * state of this file - if the snapshot captures this expanding | |
4862 | * truncation, it must capture all writes that happened before | |
4863 | * this truncation. | |
4864 | */ | |
4865 | wait_for_snapshot_creation(root); | |
a41ad394 | 4866 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe FM |
4867 | if (ret) { |
4868 | btrfs_end_write_no_snapshoting(root); | |
8082510e | 4869 | return ret; |
9ea24bbe | 4870 | } |
8082510e | 4871 | |
f4a2f4c5 | 4872 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe FM |
4873 | if (IS_ERR(trans)) { |
4874 | btrfs_end_write_no_snapshoting(root); | |
f4a2f4c5 | 4875 | return PTR_ERR(trans); |
9ea24bbe | 4876 | } |
f4a2f4c5 MX |
4877 | |
4878 | i_size_write(inode, newsize); | |
4879 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
4880 | ret = btrfs_update_inode(trans, root, inode); | |
9ea24bbe | 4881 | btrfs_end_write_no_snapshoting(root); |
7ad85bb7 | 4882 | btrfs_end_transaction(trans, root); |
a41ad394 | 4883 | } else { |
8082510e | 4884 | |
a41ad394 JB |
4885 | /* |
4886 | * We're truncating a file that used to have good data down to | |
4887 | * zero. Make sure it gets into the ordered flush list so that | |
4888 | * any new writes get down to disk quickly. | |
4889 | */ | |
4890 | if (newsize == 0) | |
72ac3c0d JB |
4891 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
4892 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 4893 | |
f3fe820c JB |
4894 | /* |
4895 | * 1 for the orphan item we're going to add | |
4896 | * 1 for the orphan item deletion. | |
4897 | */ | |
4898 | trans = btrfs_start_transaction(root, 2); | |
4899 | if (IS_ERR(trans)) | |
4900 | return PTR_ERR(trans); | |
4901 | ||
4902 | /* | |
4903 | * We need to do this in case we fail at _any_ point during the | |
4904 | * actual truncate. Once we do the truncate_setsize we could | |
4905 | * invalidate pages which forces any outstanding ordered io to | |
4906 | * be instantly completed which will give us extents that need | |
4907 | * to be truncated. If we fail to get an orphan inode down we | |
4908 | * could have left over extents that were never meant to live, | |
4909 | * so we need to garuntee from this point on that everything | |
4910 | * will be consistent. | |
4911 | */ | |
4912 | ret = btrfs_orphan_add(trans, inode); | |
4913 | btrfs_end_transaction(trans, root); | |
4914 | if (ret) | |
4915 | return ret; | |
4916 | ||
a41ad394 JB |
4917 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
4918 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
4919 | |
4920 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
4921 | btrfs_inode_block_unlocked_dio(inode); | |
4922 | inode_dio_wait(inode); | |
4923 | btrfs_inode_resume_unlocked_dio(inode); | |
4924 | ||
a41ad394 | 4925 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
4926 | if (ret && inode->i_nlink) { |
4927 | int err; | |
4928 | ||
4929 | /* | |
4930 | * failed to truncate, disk_i_size is only adjusted down | |
4931 | * as we remove extents, so it should represent the true | |
4932 | * size of the inode, so reset the in memory size and | |
4933 | * delete our orphan entry. | |
4934 | */ | |
4935 | trans = btrfs_join_transaction(root); | |
4936 | if (IS_ERR(trans)) { | |
4937 | btrfs_orphan_del(NULL, inode); | |
4938 | return ret; | |
4939 | } | |
4940 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
4941 | err = btrfs_orphan_del(trans, inode); | |
4942 | if (err) | |
4943 | btrfs_abort_transaction(trans, root, err); | |
4944 | btrfs_end_transaction(trans, root); | |
4945 | } | |
8082510e YZ |
4946 | } |
4947 | ||
a41ad394 | 4948 | return ret; |
8082510e YZ |
4949 | } |
4950 | ||
9036c102 YZ |
4951 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
4952 | { | |
2b0143b5 | 4953 | struct inode *inode = d_inode(dentry); |
b83cc969 | 4954 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 4955 | int err; |
39279cc3 | 4956 | |
b83cc969 LZ |
4957 | if (btrfs_root_readonly(root)) |
4958 | return -EROFS; | |
4959 | ||
9036c102 YZ |
4960 | err = inode_change_ok(inode, attr); |
4961 | if (err) | |
4962 | return err; | |
2bf5a725 | 4963 | |
5a3f23d5 | 4964 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 4965 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
4966 | if (err) |
4967 | return err; | |
39279cc3 | 4968 | } |
9036c102 | 4969 | |
1025774c CH |
4970 | if (attr->ia_valid) { |
4971 | setattr_copy(inode, attr); | |
0c4d2d95 | 4972 | inode_inc_iversion(inode); |
22c44fe6 | 4973 | err = btrfs_dirty_inode(inode); |
1025774c | 4974 | |
22c44fe6 | 4975 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 4976 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 4977 | } |
33268eaf | 4978 | |
39279cc3 CM |
4979 | return err; |
4980 | } | |
61295eb8 | 4981 | |
131e404a FDBM |
4982 | /* |
4983 | * While truncating the inode pages during eviction, we get the VFS calling | |
4984 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
4985 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
4986 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
4987 | * extent_state structures over and over, wasting lots of time. | |
4988 | * | |
4989 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
4990 | * those expensive operations on a per page basis and do only the ordered io | |
4991 | * finishing, while we release here the extent_map and extent_state structures, | |
4992 | * without the excessive merging and splitting. | |
4993 | */ | |
4994 | static void evict_inode_truncate_pages(struct inode *inode) | |
4995 | { | |
4996 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
4997 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
4998 | struct rb_node *node; | |
4999 | ||
5000 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5001 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5002 | |
5003 | write_lock(&map_tree->lock); | |
5004 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5005 | struct extent_map *em; | |
5006 | ||
5007 | node = rb_first(&map_tree->map); | |
5008 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5009 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5010 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5011 | remove_extent_mapping(map_tree, em); |
5012 | free_extent_map(em); | |
7064dd5c FM |
5013 | if (need_resched()) { |
5014 | write_unlock(&map_tree->lock); | |
5015 | cond_resched(); | |
5016 | write_lock(&map_tree->lock); | |
5017 | } | |
131e404a FDBM |
5018 | } |
5019 | write_unlock(&map_tree->lock); | |
5020 | ||
6ca07097 FM |
5021 | /* |
5022 | * Keep looping until we have no more ranges in the io tree. | |
5023 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5024 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5025 | * still in progress (unlocked the pages in the bio but did not yet | |
5026 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5027 | * ranges can still be locked and eviction started because before |
5028 | * submitting those bios, which are executed by a separate task (work | |
5029 | * queue kthread), inode references (inode->i_count) were not taken | |
5030 | * (which would be dropped in the end io callback of each bio). | |
5031 | * Therefore here we effectively end up waiting for those bios and | |
5032 | * anyone else holding locked ranges without having bumped the inode's | |
5033 | * reference count - if we don't do it, when they access the inode's | |
5034 | * io_tree to unlock a range it may be too late, leading to an | |
5035 | * use-after-free issue. | |
5036 | */ | |
131e404a FDBM |
5037 | spin_lock(&io_tree->lock); |
5038 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5039 | struct extent_state *state; | |
5040 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5041 | u64 start; |
5042 | u64 end; | |
131e404a FDBM |
5043 | |
5044 | node = rb_first(&io_tree->state); | |
5045 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5046 | start = state->start; |
5047 | end = state->end; | |
131e404a FDBM |
5048 | spin_unlock(&io_tree->lock); |
5049 | ||
6ca07097 FM |
5050 | lock_extent_bits(io_tree, start, end, 0, &cached_state); |
5051 | clear_extent_bit(io_tree, start, end, | |
131e404a FDBM |
5052 | EXTENT_LOCKED | EXTENT_DIRTY | |
5053 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5054 | EXTENT_DEFRAG, 1, 1, | |
5055 | &cached_state, GFP_NOFS); | |
131e404a | 5056 | |
7064dd5c | 5057 | cond_resched(); |
131e404a FDBM |
5058 | spin_lock(&io_tree->lock); |
5059 | } | |
5060 | spin_unlock(&io_tree->lock); | |
5061 | } | |
5062 | ||
bd555975 | 5063 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 CM |
5064 | { |
5065 | struct btrfs_trans_handle *trans; | |
5066 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5067 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5068 | int steal_from_global = 0; |
07127184 | 5069 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 CM |
5070 | int ret; |
5071 | ||
1abe9b8a | 5072 | trace_btrfs_inode_evict(inode); |
5073 | ||
131e404a FDBM |
5074 | evict_inode_truncate_pages(inode); |
5075 | ||
69e9c6c6 SB |
5076 | if (inode->i_nlink && |
5077 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5078 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
5079 | btrfs_is_free_space_inode(inode))) | |
bd555975 AV |
5080 | goto no_delete; |
5081 | ||
39279cc3 | 5082 | if (is_bad_inode(inode)) { |
7b128766 | 5083 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
5084 | goto no_delete; |
5085 | } | |
bd555975 | 5086 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
a30e577c JM |
5087 | if (!special_file(inode->i_mode)) |
5088 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5f39d397 | 5089 | |
f612496b MX |
5090 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
5091 | ||
c71bf099 | 5092 | if (root->fs_info->log_root_recovering) { |
6bf02314 | 5093 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5094 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5095 | goto no_delete; |
5096 | } | |
5097 | ||
76dda93c | 5098 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5099 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5100 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5101 | goto no_delete; |
5102 | } | |
5103 | ||
0e8c36a9 MX |
5104 | ret = btrfs_commit_inode_delayed_inode(inode); |
5105 | if (ret) { | |
5106 | btrfs_orphan_del(NULL, inode); | |
5107 | goto no_delete; | |
5108 | } | |
5109 | ||
66d8f3dd | 5110 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
4289a667 JB |
5111 | if (!rsv) { |
5112 | btrfs_orphan_del(NULL, inode); | |
5113 | goto no_delete; | |
5114 | } | |
4a338542 | 5115 | rsv->size = min_size; |
ca7e70f5 | 5116 | rsv->failfast = 1; |
726c35fa | 5117 | global_rsv = &root->fs_info->global_block_rsv; |
4289a667 | 5118 | |
dbe674a9 | 5119 | btrfs_i_size_write(inode, 0); |
5f39d397 | 5120 | |
4289a667 | 5121 | /* |
8407aa46 MX |
5122 | * This is a bit simpler than btrfs_truncate since we've already |
5123 | * reserved our space for our orphan item in the unlink, so we just | |
5124 | * need to reserve some slack space in case we add bytes and update | |
5125 | * inode item when doing the truncate. | |
4289a667 | 5126 | */ |
8082510e | 5127 | while (1) { |
08e007d2 MX |
5128 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5129 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5130 | |
5131 | /* | |
5132 | * Try and steal from the global reserve since we will | |
5133 | * likely not use this space anyway, we want to try as | |
5134 | * hard as possible to get this to work. | |
5135 | */ | |
5136 | if (ret) | |
3bce876f JB |
5137 | steal_from_global++; |
5138 | else | |
5139 | steal_from_global = 0; | |
5140 | ret = 0; | |
d68fc57b | 5141 | |
3bce876f JB |
5142 | /* |
5143 | * steal_from_global == 0: we reserved stuff, hooray! | |
5144 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5145 | * steal_from_global == 2: we've committed, still not a lot of | |
5146 | * room but maybe we'll have room in the global reserve this | |
5147 | * time. | |
5148 | * steal_from_global == 3: abandon all hope! | |
5149 | */ | |
5150 | if (steal_from_global > 2) { | |
c2cf52eb SK |
5151 | btrfs_warn(root->fs_info, |
5152 | "Could not get space for a delete, will truncate on mount %d", | |
5153 | ret); | |
4289a667 JB |
5154 | btrfs_orphan_del(NULL, inode); |
5155 | btrfs_free_block_rsv(root, rsv); | |
5156 | goto no_delete; | |
d68fc57b | 5157 | } |
7b128766 | 5158 | |
0e8c36a9 | 5159 | trans = btrfs_join_transaction(root); |
4289a667 JB |
5160 | if (IS_ERR(trans)) { |
5161 | btrfs_orphan_del(NULL, inode); | |
5162 | btrfs_free_block_rsv(root, rsv); | |
5163 | goto no_delete; | |
d68fc57b | 5164 | } |
7b128766 | 5165 | |
3bce876f JB |
5166 | /* |
5167 | * We can't just steal from the global reserve, we need tomake | |
5168 | * sure there is room to do it, if not we need to commit and try | |
5169 | * again. | |
5170 | */ | |
5171 | if (steal_from_global) { | |
5172 | if (!btrfs_check_space_for_delayed_refs(trans, root)) | |
5173 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, | |
5174 | min_size); | |
5175 | else | |
5176 | ret = -ENOSPC; | |
5177 | } | |
5178 | ||
5179 | /* | |
5180 | * Couldn't steal from the global reserve, we have too much | |
5181 | * pending stuff built up, commit the transaction and try it | |
5182 | * again. | |
5183 | */ | |
5184 | if (ret) { | |
5185 | ret = btrfs_commit_transaction(trans, root); | |
5186 | if (ret) { | |
5187 | btrfs_orphan_del(NULL, inode); | |
5188 | btrfs_free_block_rsv(root, rsv); | |
5189 | goto no_delete; | |
5190 | } | |
5191 | continue; | |
5192 | } else { | |
5193 | steal_from_global = 0; | |
5194 | } | |
5195 | ||
4289a667 JB |
5196 | trans->block_rsv = rsv; |
5197 | ||
d68fc57b | 5198 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5199 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5200 | break; |
85e21bac | 5201 | |
8407aa46 | 5202 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e YZ |
5203 | btrfs_end_transaction(trans, root); |
5204 | trans = NULL; | |
b53d3f5d | 5205 | btrfs_btree_balance_dirty(root); |
8082510e | 5206 | } |
5f39d397 | 5207 | |
4289a667 JB |
5208 | btrfs_free_block_rsv(root, rsv); |
5209 | ||
4ef31a45 JB |
5210 | /* |
5211 | * Errors here aren't a big deal, it just means we leave orphan items | |
5212 | * in the tree. They will be cleaned up on the next mount. | |
5213 | */ | |
8082510e | 5214 | if (ret == 0) { |
4289a667 | 5215 | trans->block_rsv = root->orphan_block_rsv; |
4ef31a45 JB |
5216 | btrfs_orphan_del(trans, inode); |
5217 | } else { | |
5218 | btrfs_orphan_del(NULL, inode); | |
8082510e | 5219 | } |
54aa1f4d | 5220 | |
4289a667 | 5221 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
581bb050 LZ |
5222 | if (!(root == root->fs_info->tree_root || |
5223 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) | |
33345d01 | 5224 | btrfs_return_ino(root, btrfs_ino(inode)); |
581bb050 | 5225 | |
54aa1f4d | 5226 | btrfs_end_transaction(trans, root); |
b53d3f5d | 5227 | btrfs_btree_balance_dirty(root); |
39279cc3 | 5228 | no_delete: |
89042e5a | 5229 | btrfs_remove_delayed_node(inode); |
dbd5768f | 5230 | clear_inode(inode); |
8082510e | 5231 | return; |
39279cc3 CM |
5232 | } |
5233 | ||
5234 | /* | |
5235 | * this returns the key found in the dir entry in the location pointer. | |
5236 | * If no dir entries were found, location->objectid is 0. | |
5237 | */ | |
5238 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5239 | struct btrfs_key *location) | |
5240 | { | |
5241 | const char *name = dentry->d_name.name; | |
5242 | int namelen = dentry->d_name.len; | |
5243 | struct btrfs_dir_item *di; | |
5244 | struct btrfs_path *path; | |
5245 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5246 | int ret = 0; |
39279cc3 CM |
5247 | |
5248 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5249 | if (!path) |
5250 | return -ENOMEM; | |
3954401f | 5251 | |
33345d01 | 5252 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
39279cc3 | 5253 | namelen, 0); |
0d9f7f3e Y |
5254 | if (IS_ERR(di)) |
5255 | ret = PTR_ERR(di); | |
d397712b | 5256 | |
c704005d | 5257 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5258 | goto out_err; |
d397712b | 5259 | |
5f39d397 | 5260 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5261 | out: |
39279cc3 CM |
5262 | btrfs_free_path(path); |
5263 | return ret; | |
3954401f CM |
5264 | out_err: |
5265 | location->objectid = 0; | |
5266 | goto out; | |
39279cc3 CM |
5267 | } |
5268 | ||
5269 | /* | |
5270 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5271 | * needs to be changed to reflect the root directory of the tree root. This | |
5272 | * is kind of like crossing a mount point. | |
5273 | */ | |
5274 | static int fixup_tree_root_location(struct btrfs_root *root, | |
4df27c4d YZ |
5275 | struct inode *dir, |
5276 | struct dentry *dentry, | |
5277 | struct btrfs_key *location, | |
5278 | struct btrfs_root **sub_root) | |
39279cc3 | 5279 | { |
4df27c4d YZ |
5280 | struct btrfs_path *path; |
5281 | struct btrfs_root *new_root; | |
5282 | struct btrfs_root_ref *ref; | |
5283 | struct extent_buffer *leaf; | |
1d4c08e0 | 5284 | struct btrfs_key key; |
4df27c4d YZ |
5285 | int ret; |
5286 | int err = 0; | |
39279cc3 | 5287 | |
4df27c4d YZ |
5288 | path = btrfs_alloc_path(); |
5289 | if (!path) { | |
5290 | err = -ENOMEM; | |
5291 | goto out; | |
5292 | } | |
39279cc3 | 5293 | |
4df27c4d | 5294 | err = -ENOENT; |
1d4c08e0 DS |
5295 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5296 | key.type = BTRFS_ROOT_REF_KEY; | |
5297 | key.offset = location->objectid; | |
5298 | ||
5299 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, | |
5300 | 0, 0); | |
4df27c4d YZ |
5301 | if (ret) { |
5302 | if (ret < 0) | |
5303 | err = ret; | |
5304 | goto out; | |
5305 | } | |
39279cc3 | 5306 | |
4df27c4d YZ |
5307 | leaf = path->nodes[0]; |
5308 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
33345d01 | 5309 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
4df27c4d YZ |
5310 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5311 | goto out; | |
39279cc3 | 5312 | |
4df27c4d YZ |
5313 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5314 | (unsigned long)(ref + 1), | |
5315 | dentry->d_name.len); | |
5316 | if (ret) | |
5317 | goto out; | |
5318 | ||
b3b4aa74 | 5319 | btrfs_release_path(path); |
4df27c4d YZ |
5320 | |
5321 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); | |
5322 | if (IS_ERR(new_root)) { | |
5323 | err = PTR_ERR(new_root); | |
5324 | goto out; | |
5325 | } | |
5326 | ||
4df27c4d YZ |
5327 | *sub_root = new_root; |
5328 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5329 | location->type = BTRFS_INODE_ITEM_KEY; | |
5330 | location->offset = 0; | |
5331 | err = 0; | |
5332 | out: | |
5333 | btrfs_free_path(path); | |
5334 | return err; | |
39279cc3 CM |
5335 | } |
5336 | ||
5d4f98a2 YZ |
5337 | static void inode_tree_add(struct inode *inode) |
5338 | { | |
5339 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5340 | struct btrfs_inode *entry; | |
03e860bd FNP |
5341 | struct rb_node **p; |
5342 | struct rb_node *parent; | |
cef21937 | 5343 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
33345d01 | 5344 | u64 ino = btrfs_ino(inode); |
5d4f98a2 | 5345 | |
1d3382cb | 5346 | if (inode_unhashed(inode)) |
76dda93c | 5347 | return; |
e1409cef | 5348 | parent = NULL; |
5d4f98a2 | 5349 | spin_lock(&root->inode_lock); |
e1409cef | 5350 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5351 | while (*p) { |
5352 | parent = *p; | |
5353 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5354 | ||
33345d01 | 5355 | if (ino < btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5356 | p = &parent->rb_left; |
33345d01 | 5357 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5358 | p = &parent->rb_right; |
5d4f98a2 YZ |
5359 | else { |
5360 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5361 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5362 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
5363 | RB_CLEAR_NODE(parent); |
5364 | spin_unlock(&root->inode_lock); | |
cef21937 | 5365 | return; |
5d4f98a2 YZ |
5366 | } |
5367 | } | |
cef21937 FDBM |
5368 | rb_link_node(new, parent, p); |
5369 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5370 | spin_unlock(&root->inode_lock); |
5371 | } | |
5372 | ||
5373 | static void inode_tree_del(struct inode *inode) | |
5374 | { | |
5375 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
76dda93c | 5376 | int empty = 0; |
5d4f98a2 | 5377 | |
03e860bd | 5378 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5379 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5380 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5381 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5382 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5383 | } |
03e860bd | 5384 | spin_unlock(&root->inode_lock); |
76dda93c | 5385 | |
69e9c6c6 | 5386 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5387 | synchronize_srcu(&root->fs_info->subvol_srcu); |
5388 | spin_lock(&root->inode_lock); | |
5389 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5390 | spin_unlock(&root->inode_lock); | |
5391 | if (empty) | |
5392 | btrfs_add_dead_root(root); | |
5393 | } | |
5394 | } | |
5395 | ||
143bede5 | 5396 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c YZ |
5397 | { |
5398 | struct rb_node *node; | |
5399 | struct rb_node *prev; | |
5400 | struct btrfs_inode *entry; | |
5401 | struct inode *inode; | |
5402 | u64 objectid = 0; | |
5403 | ||
7813b3db LB |
5404 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
5405 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
76dda93c YZ |
5406 | |
5407 | spin_lock(&root->inode_lock); | |
5408 | again: | |
5409 | node = root->inode_tree.rb_node; | |
5410 | prev = NULL; | |
5411 | while (node) { | |
5412 | prev = node; | |
5413 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5414 | ||
33345d01 | 5415 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
76dda93c | 5416 | node = node->rb_left; |
33345d01 | 5417 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
76dda93c YZ |
5418 | node = node->rb_right; |
5419 | else | |
5420 | break; | |
5421 | } | |
5422 | if (!node) { | |
5423 | while (prev) { | |
5424 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
33345d01 | 5425 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
76dda93c YZ |
5426 | node = prev; |
5427 | break; | |
5428 | } | |
5429 | prev = rb_next(prev); | |
5430 | } | |
5431 | } | |
5432 | while (node) { | |
5433 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
33345d01 | 5434 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
76dda93c YZ |
5435 | inode = igrab(&entry->vfs_inode); |
5436 | if (inode) { | |
5437 | spin_unlock(&root->inode_lock); | |
5438 | if (atomic_read(&inode->i_count) > 1) | |
5439 | d_prune_aliases(inode); | |
5440 | /* | |
45321ac5 | 5441 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5442 | * the inode cache when its usage count |
5443 | * hits zero. | |
5444 | */ | |
5445 | iput(inode); | |
5446 | cond_resched(); | |
5447 | spin_lock(&root->inode_lock); | |
5448 | goto again; | |
5449 | } | |
5450 | ||
5451 | if (cond_resched_lock(&root->inode_lock)) | |
5452 | goto again; | |
5453 | ||
5454 | node = rb_next(node); | |
5455 | } | |
5456 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5457 | } |
5458 | ||
e02119d5 CM |
5459 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5460 | { | |
5461 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5462 | inode->i_ino = args->location->objectid; |
5463 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5464 | sizeof(*args->location)); | |
e02119d5 | 5465 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5466 | return 0; |
5467 | } | |
5468 | ||
5469 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5470 | { | |
5471 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5472 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5473 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5474 | } |
5475 | ||
5d4f98a2 | 5476 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5477 | struct btrfs_key *location, |
5d4f98a2 | 5478 | struct btrfs_root *root) |
39279cc3 CM |
5479 | { |
5480 | struct inode *inode; | |
5481 | struct btrfs_iget_args args; | |
90d3e592 | 5482 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5483 | |
90d3e592 | 5484 | args.location = location; |
39279cc3 CM |
5485 | args.root = root; |
5486 | ||
778ba82b | 5487 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5488 | btrfs_init_locked_inode, |
5489 | (void *)&args); | |
5490 | return inode; | |
5491 | } | |
5492 | ||
1a54ef8c BR |
5493 | /* Get an inode object given its location and corresponding root. |
5494 | * Returns in *is_new if the inode was read from disk | |
5495 | */ | |
5496 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5497 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5498 | { |
5499 | struct inode *inode; | |
5500 | ||
90d3e592 | 5501 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5502 | if (!inode) |
5d4f98a2 | 5503 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5504 | |
5505 | if (inode->i_state & I_NEW) { | |
1a54ef8c | 5506 | btrfs_read_locked_inode(inode); |
1748f843 MF |
5507 | if (!is_bad_inode(inode)) { |
5508 | inode_tree_add(inode); | |
5509 | unlock_new_inode(inode); | |
5510 | if (new) | |
5511 | *new = 1; | |
5512 | } else { | |
e0b6d65b ST |
5513 | unlock_new_inode(inode); |
5514 | iput(inode); | |
5515 | inode = ERR_PTR(-ESTALE); | |
1748f843 MF |
5516 | } |
5517 | } | |
5518 | ||
1a54ef8c BR |
5519 | return inode; |
5520 | } | |
5521 | ||
4df27c4d YZ |
5522 | static struct inode *new_simple_dir(struct super_block *s, |
5523 | struct btrfs_key *key, | |
5524 | struct btrfs_root *root) | |
5525 | { | |
5526 | struct inode *inode = new_inode(s); | |
5527 | ||
5528 | if (!inode) | |
5529 | return ERR_PTR(-ENOMEM); | |
5530 | ||
4df27c4d YZ |
5531 | BTRFS_I(inode)->root = root; |
5532 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5533 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5534 | |
5535 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5536 | inode->i_op = &btrfs_dir_ro_inode_operations; |
4df27c4d YZ |
5537 | inode->i_fop = &simple_dir_operations; |
5538 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
9cc97d64 | 5539 | inode->i_mtime = CURRENT_TIME; |
5540 | inode->i_atime = inode->i_mtime; | |
5541 | inode->i_ctime = inode->i_mtime; | |
5542 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5543 | |
5544 | return inode; | |
5545 | } | |
5546 | ||
3de4586c | 5547 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5548 | { |
d397712b | 5549 | struct inode *inode; |
4df27c4d | 5550 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5551 | struct btrfs_root *sub_root = root; |
5552 | struct btrfs_key location; | |
76dda93c | 5553 | int index; |
b4aff1f8 | 5554 | int ret = 0; |
39279cc3 CM |
5555 | |
5556 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5557 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5558 | |
39e3c955 | 5559 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5560 | if (ret < 0) |
5561 | return ERR_PTR(ret); | |
5f39d397 | 5562 | |
4df27c4d | 5563 | if (location.objectid == 0) |
5662344b | 5564 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5565 | |
5566 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5567 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5568 | return inode; |
5569 | } | |
5570 | ||
5571 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5572 | ||
76dda93c | 5573 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
4df27c4d YZ |
5574 | ret = fixup_tree_root_location(root, dir, dentry, |
5575 | &location, &sub_root); | |
5576 | if (ret < 0) { | |
5577 | if (ret != -ENOENT) | |
5578 | inode = ERR_PTR(ret); | |
5579 | else | |
5580 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5581 | } else { | |
73f73415 | 5582 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5583 | } |
76dda93c YZ |
5584 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
5585 | ||
34d19bad | 5586 | if (!IS_ERR(inode) && root != sub_root) { |
c71bf099 YZ |
5587 | down_read(&root->fs_info->cleanup_work_sem); |
5588 | if (!(inode->i_sb->s_flags & MS_RDONLY)) | |
66b4ffd1 | 5589 | ret = btrfs_orphan_cleanup(sub_root); |
c71bf099 | 5590 | up_read(&root->fs_info->cleanup_work_sem); |
01cd3367 JB |
5591 | if (ret) { |
5592 | iput(inode); | |
66b4ffd1 | 5593 | inode = ERR_PTR(ret); |
01cd3367 | 5594 | } |
c71bf099 YZ |
5595 | } |
5596 | ||
3de4586c CM |
5597 | return inode; |
5598 | } | |
5599 | ||
fe15ce44 | 5600 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5601 | { |
5602 | struct btrfs_root *root; | |
2b0143b5 | 5603 | struct inode *inode = d_inode(dentry); |
76dda93c | 5604 | |
848cce0d | 5605 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5606 | inode = d_inode(dentry->d_parent); |
76dda93c | 5607 | |
848cce0d LZ |
5608 | if (inode) { |
5609 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5610 | if (btrfs_root_refs(&root->root_item) == 0) |
5611 | return 1; | |
848cce0d LZ |
5612 | |
5613 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) | |
5614 | return 1; | |
efefb143 | 5615 | } |
76dda93c YZ |
5616 | return 0; |
5617 | } | |
5618 | ||
b4aff1f8 JB |
5619 | static void btrfs_dentry_release(struct dentry *dentry) |
5620 | { | |
944a4515 | 5621 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5622 | } |
5623 | ||
3de4586c | 5624 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5625 | unsigned int flags) |
3de4586c | 5626 | { |
5662344b | 5627 | struct inode *inode; |
a66e7cc6 | 5628 | |
5662344b TI |
5629 | inode = btrfs_lookup_dentry(dir, dentry); |
5630 | if (IS_ERR(inode)) { | |
5631 | if (PTR_ERR(inode) == -ENOENT) | |
5632 | inode = NULL; | |
5633 | else | |
5634 | return ERR_CAST(inode); | |
5635 | } | |
5636 | ||
41d28bca | 5637 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5638 | } |
5639 | ||
16cdcec7 | 5640 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5641 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5642 | }; | |
5643 | ||
9cdda8d3 | 5644 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5645 | { |
9cdda8d3 | 5646 | struct inode *inode = file_inode(file); |
39279cc3 CM |
5647 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5648 | struct btrfs_item *item; | |
5649 | struct btrfs_dir_item *di; | |
5650 | struct btrfs_key key; | |
5f39d397 | 5651 | struct btrfs_key found_key; |
39279cc3 | 5652 | struct btrfs_path *path; |
16cdcec7 MX |
5653 | struct list_head ins_list; |
5654 | struct list_head del_list; | |
39279cc3 | 5655 | int ret; |
5f39d397 | 5656 | struct extent_buffer *leaf; |
39279cc3 | 5657 | int slot; |
39279cc3 CM |
5658 | unsigned char d_type; |
5659 | int over = 0; | |
5660 | u32 di_cur; | |
5661 | u32 di_total; | |
5662 | u32 di_len; | |
5663 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
5664 | char tmp_name[32]; |
5665 | char *name_ptr; | |
5666 | int name_len; | |
9cdda8d3 | 5667 | int is_curr = 0; /* ctx->pos points to the current index? */ |
39279cc3 CM |
5668 | |
5669 | /* FIXME, use a real flag for deciding about the key type */ | |
5670 | if (root->fs_info->tree_root == root) | |
5671 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 5672 | |
9cdda8d3 AV |
5673 | if (!dir_emit_dots(file, ctx)) |
5674 | return 0; | |
5675 | ||
49593bfa | 5676 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5677 | if (!path) |
5678 | return -ENOMEM; | |
ff5714cc | 5679 | |
026fd317 | 5680 | path->reada = 1; |
49593bfa | 5681 | |
16cdcec7 MX |
5682 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5683 | INIT_LIST_HEAD(&ins_list); | |
5684 | INIT_LIST_HEAD(&del_list); | |
5685 | btrfs_get_delayed_items(inode, &ins_list, &del_list); | |
5686 | } | |
5687 | ||
962a298f | 5688 | key.type = key_type; |
9cdda8d3 | 5689 | key.offset = ctx->pos; |
33345d01 | 5690 | key.objectid = btrfs_ino(inode); |
5f39d397 | 5691 | |
39279cc3 CM |
5692 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5693 | if (ret < 0) | |
5694 | goto err; | |
49593bfa DW |
5695 | |
5696 | while (1) { | |
5f39d397 | 5697 | leaf = path->nodes[0]; |
39279cc3 | 5698 | slot = path->slots[0]; |
b9e03af0 LZ |
5699 | if (slot >= btrfs_header_nritems(leaf)) { |
5700 | ret = btrfs_next_leaf(root, path); | |
5701 | if (ret < 0) | |
5702 | goto err; | |
5703 | else if (ret > 0) | |
5704 | break; | |
5705 | continue; | |
39279cc3 | 5706 | } |
3de4586c | 5707 | |
dd3cc16b | 5708 | item = btrfs_item_nr(slot); |
5f39d397 CM |
5709 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5710 | ||
5711 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5712 | break; |
962a298f | 5713 | if (found_key.type != key_type) |
39279cc3 | 5714 | break; |
9cdda8d3 | 5715 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5716 | goto next; |
16cdcec7 MX |
5717 | if (key_type == BTRFS_DIR_INDEX_KEY && |
5718 | btrfs_should_delete_dir_index(&del_list, | |
5719 | found_key.offset)) | |
5720 | goto next; | |
5f39d397 | 5721 | |
9cdda8d3 | 5722 | ctx->pos = found_key.offset; |
16cdcec7 | 5723 | is_curr = 1; |
49593bfa | 5724 | |
39279cc3 CM |
5725 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
5726 | di_cur = 0; | |
5f39d397 | 5727 | di_total = btrfs_item_size(leaf, item); |
49593bfa DW |
5728 | |
5729 | while (di_cur < di_total) { | |
5f39d397 CM |
5730 | struct btrfs_key location; |
5731 | ||
22a94d44 JB |
5732 | if (verify_dir_item(root, leaf, di)) |
5733 | break; | |
5734 | ||
5f39d397 | 5735 | name_len = btrfs_dir_name_len(leaf, di); |
49593bfa | 5736 | if (name_len <= sizeof(tmp_name)) { |
5f39d397 CM |
5737 | name_ptr = tmp_name; |
5738 | } else { | |
5739 | name_ptr = kmalloc(name_len, GFP_NOFS); | |
49593bfa DW |
5740 | if (!name_ptr) { |
5741 | ret = -ENOMEM; | |
5742 | goto err; | |
5743 | } | |
5f39d397 CM |
5744 | } |
5745 | read_extent_buffer(leaf, name_ptr, | |
5746 | (unsigned long)(di + 1), name_len); | |
5747 | ||
5748 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
5749 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
3de4586c | 5750 | |
fede766f | 5751 | |
3de4586c | 5752 | /* is this a reference to our own snapshot? If so |
8c9c2bf7 AJ |
5753 | * skip it. |
5754 | * | |
5755 | * In contrast to old kernels, we insert the snapshot's | |
5756 | * dir item and dir index after it has been created, so | |
5757 | * we won't find a reference to our own snapshot. We | |
5758 | * still keep the following code for backward | |
5759 | * compatibility. | |
3de4586c CM |
5760 | */ |
5761 | if (location.type == BTRFS_ROOT_ITEM_KEY && | |
5762 | location.objectid == root->root_key.objectid) { | |
5763 | over = 0; | |
5764 | goto skip; | |
5765 | } | |
9cdda8d3 AV |
5766 | over = !dir_emit(ctx, name_ptr, name_len, |
5767 | location.objectid, d_type); | |
5f39d397 | 5768 | |
3de4586c | 5769 | skip: |
5f39d397 CM |
5770 | if (name_ptr != tmp_name) |
5771 | kfree(name_ptr); | |
5772 | ||
39279cc3 CM |
5773 | if (over) |
5774 | goto nopos; | |
5103e947 | 5775 | di_len = btrfs_dir_name_len(leaf, di) + |
49593bfa | 5776 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
39279cc3 CM |
5777 | di_cur += di_len; |
5778 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
5779 | } | |
b9e03af0 LZ |
5780 | next: |
5781 | path->slots[0]++; | |
39279cc3 | 5782 | } |
49593bfa | 5783 | |
16cdcec7 MX |
5784 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5785 | if (is_curr) | |
9cdda8d3 AV |
5786 | ctx->pos++; |
5787 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); | |
16cdcec7 MX |
5788 | if (ret) |
5789 | goto nopos; | |
5790 | } | |
5791 | ||
49593bfa | 5792 | /* Reached end of directory/root. Bump pos past the last item. */ |
db62efbb ZB |
5793 | ctx->pos++; |
5794 | ||
5795 | /* | |
5796 | * Stop new entries from being returned after we return the last | |
5797 | * entry. | |
5798 | * | |
5799 | * New directory entries are assigned a strictly increasing | |
5800 | * offset. This means that new entries created during readdir | |
5801 | * are *guaranteed* to be seen in the future by that readdir. | |
5802 | * This has broken buggy programs which operate on names as | |
5803 | * they're returned by readdir. Until we re-use freed offsets | |
5804 | * we have this hack to stop new entries from being returned | |
5805 | * under the assumption that they'll never reach this huge | |
5806 | * offset. | |
5807 | * | |
5808 | * This is being careful not to overflow 32bit loff_t unless the | |
5809 | * last entry requires it because doing so has broken 32bit apps | |
5810 | * in the past. | |
5811 | */ | |
5812 | if (key_type == BTRFS_DIR_INDEX_KEY) { | |
5813 | if (ctx->pos >= INT_MAX) | |
5814 | ctx->pos = LLONG_MAX; | |
5815 | else | |
5816 | ctx->pos = INT_MAX; | |
5817 | } | |
39279cc3 CM |
5818 | nopos: |
5819 | ret = 0; | |
5820 | err: | |
16cdcec7 MX |
5821 | if (key_type == BTRFS_DIR_INDEX_KEY) |
5822 | btrfs_put_delayed_items(&ins_list, &del_list); | |
39279cc3 | 5823 | btrfs_free_path(path); |
39279cc3 CM |
5824 | return ret; |
5825 | } | |
5826 | ||
a9185b41 | 5827 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
5828 | { |
5829 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5830 | struct btrfs_trans_handle *trans; | |
5831 | int ret = 0; | |
0af3d00b | 5832 | bool nolock = false; |
39279cc3 | 5833 | |
72ac3c0d | 5834 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
5835 | return 0; |
5836 | ||
83eea1f1 | 5837 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
82d5902d | 5838 | nolock = true; |
0af3d00b | 5839 | |
a9185b41 | 5840 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 5841 | if (nolock) |
7a7eaa40 | 5842 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 5843 | else |
7a7eaa40 | 5844 | trans = btrfs_join_transaction(root); |
3612b495 TI |
5845 | if (IS_ERR(trans)) |
5846 | return PTR_ERR(trans); | |
a698d075 | 5847 | ret = btrfs_commit_transaction(trans, root); |
39279cc3 CM |
5848 | } |
5849 | return ret; | |
5850 | } | |
5851 | ||
5852 | /* | |
54aa1f4d | 5853 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5854 | * inode changes. But, it is most likely to find the inode in cache. |
5855 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5856 | * to keep or drop this code. | |
5857 | */ | |
48a3b636 | 5858 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 CM |
5859 | { |
5860 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5861 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5862 | int ret; |
5863 | ||
72ac3c0d | 5864 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5865 | return 0; |
39279cc3 | 5866 | |
7a7eaa40 | 5867 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5868 | if (IS_ERR(trans)) |
5869 | return PTR_ERR(trans); | |
8929ecfa YZ |
5870 | |
5871 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
5872 | if (ret && ret == -ENOSPC) { |
5873 | /* whoops, lets try again with the full transaction */ | |
5874 | btrfs_end_transaction(trans, root); | |
5875 | trans = btrfs_start_transaction(root, 1); | |
22c44fe6 JB |
5876 | if (IS_ERR(trans)) |
5877 | return PTR_ERR(trans); | |
8929ecfa | 5878 | |
94b60442 | 5879 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 5880 | } |
39279cc3 | 5881 | btrfs_end_transaction(trans, root); |
16cdcec7 MX |
5882 | if (BTRFS_I(inode)->delayed_node) |
5883 | btrfs_balance_delayed_items(root); | |
22c44fe6 JB |
5884 | |
5885 | return ret; | |
5886 | } | |
5887 | ||
5888 | /* | |
5889 | * This is a copy of file_update_time. We need this so we can return error on | |
5890 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
5891 | */ | |
e41f941a JB |
5892 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
5893 | int flags) | |
22c44fe6 | 5894 | { |
2bc55652 AB |
5895 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5896 | ||
5897 | if (btrfs_root_readonly(root)) | |
5898 | return -EROFS; | |
5899 | ||
e41f941a | 5900 | if (flags & S_VERSION) |
22c44fe6 | 5901 | inode_inc_iversion(inode); |
e41f941a JB |
5902 | if (flags & S_CTIME) |
5903 | inode->i_ctime = *now; | |
5904 | if (flags & S_MTIME) | |
5905 | inode->i_mtime = *now; | |
5906 | if (flags & S_ATIME) | |
5907 | inode->i_atime = *now; | |
5908 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
5909 | } |
5910 | ||
d352ac68 CM |
5911 | /* |
5912 | * find the highest existing sequence number in a directory | |
5913 | * and then set the in-memory index_cnt variable to reflect | |
5914 | * free sequence numbers | |
5915 | */ | |
aec7477b JB |
5916 | static int btrfs_set_inode_index_count(struct inode *inode) |
5917 | { | |
5918 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5919 | struct btrfs_key key, found_key; | |
5920 | struct btrfs_path *path; | |
5921 | struct extent_buffer *leaf; | |
5922 | int ret; | |
5923 | ||
33345d01 | 5924 | key.objectid = btrfs_ino(inode); |
962a298f | 5925 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
5926 | key.offset = (u64)-1; |
5927 | ||
5928 | path = btrfs_alloc_path(); | |
5929 | if (!path) | |
5930 | return -ENOMEM; | |
5931 | ||
5932 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5933 | if (ret < 0) | |
5934 | goto out; | |
5935 | /* FIXME: we should be able to handle this */ | |
5936 | if (ret == 0) | |
5937 | goto out; | |
5938 | ret = 0; | |
5939 | ||
5940 | /* | |
5941 | * MAGIC NUMBER EXPLANATION: | |
5942 | * since we search a directory based on f_pos we have to start at 2 | |
5943 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
5944 | * else has to start at 2 | |
5945 | */ | |
5946 | if (path->slots[0] == 0) { | |
5947 | BTRFS_I(inode)->index_cnt = 2; | |
5948 | goto out; | |
5949 | } | |
5950 | ||
5951 | path->slots[0]--; | |
5952 | ||
5953 | leaf = path->nodes[0]; | |
5954 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5955 | ||
33345d01 | 5956 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 5957 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
aec7477b JB |
5958 | BTRFS_I(inode)->index_cnt = 2; |
5959 | goto out; | |
5960 | } | |
5961 | ||
5962 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
5963 | out: | |
5964 | btrfs_free_path(path); | |
5965 | return ret; | |
5966 | } | |
5967 | ||
d352ac68 CM |
5968 | /* |
5969 | * helper to find a free sequence number in a given directory. This current | |
5970 | * code is very simple, later versions will do smarter things in the btree | |
5971 | */ | |
3de4586c | 5972 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
aec7477b JB |
5973 | { |
5974 | int ret = 0; | |
5975 | ||
5976 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
16cdcec7 MX |
5977 | ret = btrfs_inode_delayed_dir_index_count(dir); |
5978 | if (ret) { | |
5979 | ret = btrfs_set_inode_index_count(dir); | |
5980 | if (ret) | |
5981 | return ret; | |
5982 | } | |
aec7477b JB |
5983 | } |
5984 | ||
00e4e6b3 | 5985 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
5986 | BTRFS_I(dir)->index_cnt++; |
5987 | ||
5988 | return ret; | |
5989 | } | |
5990 | ||
b0d5d10f CM |
5991 | static int btrfs_insert_inode_locked(struct inode *inode) |
5992 | { | |
5993 | struct btrfs_iget_args args; | |
5994 | args.location = &BTRFS_I(inode)->location; | |
5995 | args.root = BTRFS_I(inode)->root; | |
5996 | ||
5997 | return insert_inode_locked4(inode, | |
5998 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
5999 | btrfs_find_actor, &args); | |
6000 | } | |
6001 | ||
39279cc3 CM |
6002 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6003 | struct btrfs_root *root, | |
aec7477b | 6004 | struct inode *dir, |
9c58309d | 6005 | const char *name, int name_len, |
175a4eb7 AV |
6006 | u64 ref_objectid, u64 objectid, |
6007 | umode_t mode, u64 *index) | |
39279cc3 CM |
6008 | { |
6009 | struct inode *inode; | |
5f39d397 | 6010 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6011 | struct btrfs_key *location; |
5f39d397 | 6012 | struct btrfs_path *path; |
9c58309d CM |
6013 | struct btrfs_inode_ref *ref; |
6014 | struct btrfs_key key[2]; | |
6015 | u32 sizes[2]; | |
ef3b9af5 | 6016 | int nitems = name ? 2 : 1; |
9c58309d | 6017 | unsigned long ptr; |
39279cc3 | 6018 | int ret; |
39279cc3 | 6019 | |
5f39d397 | 6020 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6021 | if (!path) |
6022 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6023 | |
39279cc3 | 6024 | inode = new_inode(root->fs_info->sb); |
8fb27640 YS |
6025 | if (!inode) { |
6026 | btrfs_free_path(path); | |
39279cc3 | 6027 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6028 | } |
39279cc3 | 6029 | |
5762b5c9 FM |
6030 | /* |
6031 | * O_TMPFILE, set link count to 0, so that after this point, | |
6032 | * we fill in an inode item with the correct link count. | |
6033 | */ | |
6034 | if (!name) | |
6035 | set_nlink(inode, 0); | |
6036 | ||
581bb050 LZ |
6037 | /* |
6038 | * we have to initialize this early, so we can reclaim the inode | |
6039 | * number if we fail afterwards in this function. | |
6040 | */ | |
6041 | inode->i_ino = objectid; | |
6042 | ||
ef3b9af5 | 6043 | if (dir && name) { |
1abe9b8a | 6044 | trace_btrfs_inode_request(dir); |
6045 | ||
3de4586c | 6046 | ret = btrfs_set_inode_index(dir, index); |
09771430 | 6047 | if (ret) { |
8fb27640 | 6048 | btrfs_free_path(path); |
09771430 | 6049 | iput(inode); |
aec7477b | 6050 | return ERR_PTR(ret); |
09771430 | 6051 | } |
ef3b9af5 FM |
6052 | } else if (dir) { |
6053 | *index = 0; | |
aec7477b JB |
6054 | } |
6055 | /* | |
6056 | * index_cnt is ignored for everything but a dir, | |
6057 | * btrfs_get_inode_index_count has an explanation for the magic | |
6058 | * number | |
6059 | */ | |
6060 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6061 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6062 | BTRFS_I(inode)->root = root; |
e02119d5 | 6063 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6064 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6065 | |
5dc562c5 JB |
6066 | /* |
6067 | * We could have gotten an inode number from somebody who was fsynced | |
6068 | * and then removed in this same transaction, so let's just set full | |
6069 | * sync since it will be a full sync anyway and this will blow away the | |
6070 | * old info in the log. | |
6071 | */ | |
6072 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6073 | ||
9c58309d | 6074 | key[0].objectid = objectid; |
962a298f | 6075 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6076 | key[0].offset = 0; |
6077 | ||
9c58309d | 6078 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6079 | |
6080 | if (name) { | |
6081 | /* | |
6082 | * Start new inodes with an inode_ref. This is slightly more | |
6083 | * efficient for small numbers of hard links since they will | |
6084 | * be packed into one item. Extended refs will kick in if we | |
6085 | * add more hard links than can fit in the ref item. | |
6086 | */ | |
6087 | key[1].objectid = objectid; | |
962a298f | 6088 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6089 | key[1].offset = ref_objectid; |
6090 | ||
6091 | sizes[1] = name_len + sizeof(*ref); | |
6092 | } | |
9c58309d | 6093 | |
b0d5d10f CM |
6094 | location = &BTRFS_I(inode)->location; |
6095 | location->objectid = objectid; | |
6096 | location->offset = 0; | |
962a298f | 6097 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6098 | |
6099 | ret = btrfs_insert_inode_locked(inode); | |
6100 | if (ret < 0) | |
6101 | goto fail; | |
6102 | ||
b9473439 | 6103 | path->leave_spinning = 1; |
ef3b9af5 | 6104 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6105 | if (ret != 0) |
b0d5d10f | 6106 | goto fail_unlock; |
5f39d397 | 6107 | |
ecc11fab | 6108 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6109 | inode_set_bytes(inode, 0); |
9cc97d64 | 6110 | |
6111 | inode->i_mtime = CURRENT_TIME; | |
6112 | inode->i_atime = inode->i_mtime; | |
6113 | inode->i_ctime = inode->i_mtime; | |
6114 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6115 | ||
5f39d397 CM |
6116 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6117 | struct btrfs_inode_item); | |
293f7e07 LZ |
6118 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
6119 | sizeof(*inode_item)); | |
e02119d5 | 6120 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6121 | |
ef3b9af5 FM |
6122 | if (name) { |
6123 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6124 | struct btrfs_inode_ref); | |
6125 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6126 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6127 | ptr = (unsigned long)(ref + 1); | |
6128 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6129 | } | |
9c58309d | 6130 | |
5f39d397 CM |
6131 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6132 | btrfs_free_path(path); | |
6133 | ||
6cbff00f CH |
6134 | btrfs_inherit_iflags(inode, dir); |
6135 | ||
569254b0 | 6136 | if (S_ISREG(mode)) { |
94272164 CM |
6137 | if (btrfs_test_opt(root, NODATASUM)) |
6138 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
213490b3 | 6139 | if (btrfs_test_opt(root, NODATACOW)) |
f2bdf9a8 JB |
6140 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6141 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6142 | } |
6143 | ||
5d4f98a2 | 6144 | inode_tree_add(inode); |
1abe9b8a | 6145 | |
6146 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6147 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6148 | |
8ea05e3a AB |
6149 | btrfs_update_root_times(trans, root); |
6150 | ||
63541927 FDBM |
6151 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6152 | if (ret) | |
6153 | btrfs_err(root->fs_info, | |
6154 | "error inheriting props for ino %llu (root %llu): %d", | |
6155 | btrfs_ino(inode), root->root_key.objectid, ret); | |
6156 | ||
39279cc3 | 6157 | return inode; |
b0d5d10f CM |
6158 | |
6159 | fail_unlock: | |
6160 | unlock_new_inode(inode); | |
5f39d397 | 6161 | fail: |
ef3b9af5 | 6162 | if (dir && name) |
aec7477b | 6163 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6164 | btrfs_free_path(path); |
09771430 | 6165 | iput(inode); |
5f39d397 | 6166 | return ERR_PTR(ret); |
39279cc3 CM |
6167 | } |
6168 | ||
6169 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6170 | { | |
6171 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6172 | } | |
6173 | ||
d352ac68 CM |
6174 | /* |
6175 | * utility function to add 'inode' into 'parent_inode' with | |
6176 | * a give name and a given sequence number. | |
6177 | * if 'add_backref' is true, also insert a backref from the | |
6178 | * inode to the parent directory. | |
6179 | */ | |
e02119d5 CM |
6180 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
6181 | struct inode *parent_inode, struct inode *inode, | |
6182 | const char *name, int name_len, int add_backref, u64 index) | |
39279cc3 | 6183 | { |
4df27c4d | 6184 | int ret = 0; |
39279cc3 | 6185 | struct btrfs_key key; |
e02119d5 | 6186 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
33345d01 LZ |
6187 | u64 ino = btrfs_ino(inode); |
6188 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6189 | |
33345d01 | 6190 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6191 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
6192 | } else { | |
33345d01 | 6193 | key.objectid = ino; |
962a298f | 6194 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6195 | key.offset = 0; |
6196 | } | |
6197 | ||
33345d01 | 6198 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6199 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
6200 | key.objectid, root->root_key.objectid, | |
33345d01 | 6201 | parent_ino, index, name, name_len); |
4df27c4d | 6202 | } else if (add_backref) { |
33345d01 LZ |
6203 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6204 | parent_ino, index); | |
4df27c4d | 6205 | } |
39279cc3 | 6206 | |
79787eaa JM |
6207 | /* Nothing to clean up yet */ |
6208 | if (ret) | |
6209 | return ret; | |
4df27c4d | 6210 | |
79787eaa JM |
6211 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6212 | parent_inode, &key, | |
6213 | btrfs_inode_type(inode), index); | |
9c52057c | 6214 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6215 | goto fail_dir_item; |
6216 | else if (ret) { | |
6217 | btrfs_abort_transaction(trans, root, ret); | |
6218 | return ret; | |
39279cc3 | 6219 | } |
79787eaa JM |
6220 | |
6221 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | |
6222 | name_len * 2); | |
0c4d2d95 | 6223 | inode_inc_iversion(parent_inode); |
79787eaa JM |
6224 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
6225 | ret = btrfs_update_inode(trans, root, parent_inode); | |
6226 | if (ret) | |
6227 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 6228 | return ret; |
fe66a05a CM |
6229 | |
6230 | fail_dir_item: | |
6231 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6232 | u64 local_index; | |
6233 | int err; | |
6234 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
6235 | key.objectid, root->root_key.objectid, | |
6236 | parent_ino, &local_index, name, name_len); | |
6237 | ||
6238 | } else if (add_backref) { | |
6239 | u64 local_index; | |
6240 | int err; | |
6241 | ||
6242 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6243 | ino, parent_ino, &local_index); | |
6244 | } | |
6245 | return ret; | |
39279cc3 CM |
6246 | } |
6247 | ||
6248 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
a1b075d2 JB |
6249 | struct inode *dir, struct dentry *dentry, |
6250 | struct inode *inode, int backref, u64 index) | |
39279cc3 | 6251 | { |
a1b075d2 JB |
6252 | int err = btrfs_add_link(trans, dir, inode, |
6253 | dentry->d_name.name, dentry->d_name.len, | |
6254 | backref, index); | |
39279cc3 CM |
6255 | if (err > 0) |
6256 | err = -EEXIST; | |
6257 | return err; | |
6258 | } | |
6259 | ||
618e21d5 | 6260 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6261 | umode_t mode, dev_t rdev) |
618e21d5 JB |
6262 | { |
6263 | struct btrfs_trans_handle *trans; | |
6264 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6265 | struct inode *inode = NULL; |
618e21d5 JB |
6266 | int err; |
6267 | int drop_inode = 0; | |
6268 | u64 objectid; | |
00e4e6b3 | 6269 | u64 index = 0; |
618e21d5 JB |
6270 | |
6271 | if (!new_valid_dev(rdev)) | |
6272 | return -EINVAL; | |
6273 | ||
9ed74f2d JB |
6274 | /* |
6275 | * 2 for inode item and ref | |
6276 | * 2 for dir items | |
6277 | * 1 for xattr if selinux is on | |
6278 | */ | |
a22285a6 YZ |
6279 | trans = btrfs_start_transaction(root, 5); |
6280 | if (IS_ERR(trans)) | |
6281 | return PTR_ERR(trans); | |
1832a6d5 | 6282 | |
581bb050 LZ |
6283 | err = btrfs_find_free_ino(root, &objectid); |
6284 | if (err) | |
6285 | goto out_unlock; | |
6286 | ||
aec7477b | 6287 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6288 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6289 | mode, &index); |
7cf96da3 TI |
6290 | if (IS_ERR(inode)) { |
6291 | err = PTR_ERR(inode); | |
618e21d5 | 6292 | goto out_unlock; |
7cf96da3 | 6293 | } |
618e21d5 | 6294 | |
ad19db71 CS |
6295 | /* |
6296 | * If the active LSM wants to access the inode during | |
6297 | * d_instantiate it needs these. Smack checks to see | |
6298 | * if the filesystem supports xattrs by looking at the | |
6299 | * ops vector. | |
6300 | */ | |
ad19db71 | 6301 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6302 | init_special_inode(inode, inode->i_mode, rdev); |
6303 | ||
6304 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6305 | if (err) |
b0d5d10f CM |
6306 | goto out_unlock_inode; |
6307 | ||
6308 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
6309 | if (err) { | |
6310 | goto out_unlock_inode; | |
6311 | } else { | |
1b4ab1bb | 6312 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6313 | unlock_new_inode(inode); |
08c422c2 | 6314 | d_instantiate(dentry, inode); |
618e21d5 | 6315 | } |
b0d5d10f | 6316 | |
618e21d5 | 6317 | out_unlock: |
7ad85bb7 | 6318 | btrfs_end_transaction(trans, root); |
c581afc8 | 6319 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6320 | btrfs_btree_balance_dirty(root); |
618e21d5 JB |
6321 | if (drop_inode) { |
6322 | inode_dec_link_count(inode); | |
6323 | iput(inode); | |
6324 | } | |
618e21d5 | 6325 | return err; |
b0d5d10f CM |
6326 | |
6327 | out_unlock_inode: | |
6328 | drop_inode = 1; | |
6329 | unlock_new_inode(inode); | |
6330 | goto out_unlock; | |
6331 | ||
618e21d5 JB |
6332 | } |
6333 | ||
39279cc3 | 6334 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6335 | umode_t mode, bool excl) |
39279cc3 CM |
6336 | { |
6337 | struct btrfs_trans_handle *trans; | |
6338 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6339 | struct inode *inode = NULL; |
43baa579 | 6340 | int drop_inode_on_err = 0; |
a22285a6 | 6341 | int err; |
39279cc3 | 6342 | u64 objectid; |
00e4e6b3 | 6343 | u64 index = 0; |
39279cc3 | 6344 | |
9ed74f2d JB |
6345 | /* |
6346 | * 2 for inode item and ref | |
6347 | * 2 for dir items | |
6348 | * 1 for xattr if selinux is on | |
6349 | */ | |
a22285a6 YZ |
6350 | trans = btrfs_start_transaction(root, 5); |
6351 | if (IS_ERR(trans)) | |
6352 | return PTR_ERR(trans); | |
9ed74f2d | 6353 | |
581bb050 LZ |
6354 | err = btrfs_find_free_ino(root, &objectid); |
6355 | if (err) | |
6356 | goto out_unlock; | |
6357 | ||
aec7477b | 6358 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6359 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6360 | mode, &index); |
7cf96da3 TI |
6361 | if (IS_ERR(inode)) { |
6362 | err = PTR_ERR(inode); | |
39279cc3 | 6363 | goto out_unlock; |
7cf96da3 | 6364 | } |
43baa579 | 6365 | drop_inode_on_err = 1; |
ad19db71 CS |
6366 | /* |
6367 | * If the active LSM wants to access the inode during | |
6368 | * d_instantiate it needs these. Smack checks to see | |
6369 | * if the filesystem supports xattrs by looking at the | |
6370 | * ops vector. | |
6371 | */ | |
6372 | inode->i_fop = &btrfs_file_operations; | |
6373 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6374 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6375 | |
6376 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6377 | if (err) | |
6378 | goto out_unlock_inode; | |
6379 | ||
6380 | err = btrfs_update_inode(trans, root, inode); | |
6381 | if (err) | |
6382 | goto out_unlock_inode; | |
ad19db71 | 6383 | |
a1b075d2 | 6384 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 6385 | if (err) |
b0d5d10f | 6386 | goto out_unlock_inode; |
43baa579 | 6387 | |
43baa579 | 6388 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6389 | unlock_new_inode(inode); |
43baa579 FB |
6390 | d_instantiate(dentry, inode); |
6391 | ||
39279cc3 | 6392 | out_unlock: |
7ad85bb7 | 6393 | btrfs_end_transaction(trans, root); |
43baa579 | 6394 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6395 | inode_dec_link_count(inode); |
6396 | iput(inode); | |
6397 | } | |
c581afc8 | 6398 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6399 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6400 | return err; |
b0d5d10f CM |
6401 | |
6402 | out_unlock_inode: | |
6403 | unlock_new_inode(inode); | |
6404 | goto out_unlock; | |
6405 | ||
39279cc3 CM |
6406 | } |
6407 | ||
6408 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6409 | struct dentry *dentry) | |
6410 | { | |
6411 | struct btrfs_trans_handle *trans; | |
6412 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2b0143b5 | 6413 | struct inode *inode = d_inode(old_dentry); |
00e4e6b3 | 6414 | u64 index; |
39279cc3 CM |
6415 | int err; |
6416 | int drop_inode = 0; | |
6417 | ||
4a8be425 TH |
6418 | /* do not allow sys_link's with other subvols of the same device */ |
6419 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6420 | return -EXDEV; |
4a8be425 | 6421 | |
f186373f | 6422 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6423 | return -EMLINK; |
4a8be425 | 6424 | |
3de4586c | 6425 | err = btrfs_set_inode_index(dir, &index); |
aec7477b JB |
6426 | if (err) |
6427 | goto fail; | |
6428 | ||
a22285a6 | 6429 | /* |
7e6b6465 | 6430 | * 2 items for inode and inode ref |
a22285a6 | 6431 | * 2 items for dir items |
7e6b6465 | 6432 | * 1 item for parent inode |
a22285a6 | 6433 | */ |
7e6b6465 | 6434 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6435 | if (IS_ERR(trans)) { |
6436 | err = PTR_ERR(trans); | |
6437 | goto fail; | |
6438 | } | |
5f39d397 | 6439 | |
67de1176 MX |
6440 | /* There are several dir indexes for this inode, clear the cache. */ |
6441 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6442 | inc_nlink(inode); |
0c4d2d95 | 6443 | inode_inc_iversion(inode); |
3153495d | 6444 | inode->i_ctime = CURRENT_TIME; |
7de9c6ee | 6445 | ihold(inode); |
e9976151 | 6446 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6447 | |
a1b075d2 | 6448 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
5f39d397 | 6449 | |
a5719521 | 6450 | if (err) { |
54aa1f4d | 6451 | drop_inode = 1; |
a5719521 | 6452 | } else { |
10d9f309 | 6453 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6454 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6455 | if (err) |
6456 | goto fail; | |
ef3b9af5 FM |
6457 | if (inode->i_nlink == 1) { |
6458 | /* | |
6459 | * If new hard link count is 1, it's a file created | |
6460 | * with open(2) O_TMPFILE flag. | |
6461 | */ | |
6462 | err = btrfs_orphan_del(trans, inode); | |
6463 | if (err) | |
6464 | goto fail; | |
6465 | } | |
08c422c2 | 6466 | d_instantiate(dentry, inode); |
6a912213 | 6467 | btrfs_log_new_name(trans, inode, NULL, parent); |
a5719521 | 6468 | } |
39279cc3 | 6469 | |
7ad85bb7 | 6470 | btrfs_end_transaction(trans, root); |
c581afc8 | 6471 | btrfs_balance_delayed_items(root); |
1832a6d5 | 6472 | fail: |
39279cc3 CM |
6473 | if (drop_inode) { |
6474 | inode_dec_link_count(inode); | |
6475 | iput(inode); | |
6476 | } | |
b53d3f5d | 6477 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
6478 | return err; |
6479 | } | |
6480 | ||
18bb1db3 | 6481 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6482 | { |
b9d86667 | 6483 | struct inode *inode = NULL; |
39279cc3 CM |
6484 | struct btrfs_trans_handle *trans; |
6485 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6486 | int err = 0; | |
6487 | int drop_on_err = 0; | |
b9d86667 | 6488 | u64 objectid = 0; |
00e4e6b3 | 6489 | u64 index = 0; |
39279cc3 | 6490 | |
9ed74f2d JB |
6491 | /* |
6492 | * 2 items for inode and ref | |
6493 | * 2 items for dir items | |
6494 | * 1 for xattr if selinux is on | |
6495 | */ | |
a22285a6 YZ |
6496 | trans = btrfs_start_transaction(root, 5); |
6497 | if (IS_ERR(trans)) | |
6498 | return PTR_ERR(trans); | |
39279cc3 | 6499 | |
581bb050 LZ |
6500 | err = btrfs_find_free_ino(root, &objectid); |
6501 | if (err) | |
6502 | goto out_fail; | |
6503 | ||
aec7477b | 6504 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6505 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6506 | S_IFDIR | mode, &index); |
39279cc3 CM |
6507 | if (IS_ERR(inode)) { |
6508 | err = PTR_ERR(inode); | |
6509 | goto out_fail; | |
6510 | } | |
5f39d397 | 6511 | |
39279cc3 | 6512 | drop_on_err = 1; |
b0d5d10f CM |
6513 | /* these must be set before we unlock the inode */ |
6514 | inode->i_op = &btrfs_dir_inode_operations; | |
6515 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6516 | |
2a7dba39 | 6517 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6518 | if (err) |
b0d5d10f | 6519 | goto out_fail_inode; |
39279cc3 | 6520 | |
dbe674a9 | 6521 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
6522 | err = btrfs_update_inode(trans, root, inode); |
6523 | if (err) | |
b0d5d10f | 6524 | goto out_fail_inode; |
5f39d397 | 6525 | |
a1b075d2 JB |
6526 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
6527 | dentry->d_name.len, 0, index); | |
39279cc3 | 6528 | if (err) |
b0d5d10f | 6529 | goto out_fail_inode; |
5f39d397 | 6530 | |
39279cc3 | 6531 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6532 | /* |
6533 | * mkdir is special. We're unlocking after we call d_instantiate | |
6534 | * to avoid a race with nfsd calling d_instantiate. | |
6535 | */ | |
6536 | unlock_new_inode(inode); | |
39279cc3 | 6537 | drop_on_err = 0; |
39279cc3 CM |
6538 | |
6539 | out_fail: | |
7ad85bb7 | 6540 | btrfs_end_transaction(trans, root); |
c7cfb8a5 WS |
6541 | if (drop_on_err) { |
6542 | inode_dec_link_count(inode); | |
39279cc3 | 6543 | iput(inode); |
c7cfb8a5 | 6544 | } |
c581afc8 | 6545 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6546 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6547 | return err; |
b0d5d10f CM |
6548 | |
6549 | out_fail_inode: | |
6550 | unlock_new_inode(inode); | |
6551 | goto out_fail; | |
39279cc3 CM |
6552 | } |
6553 | ||
e6c4efd8 QW |
6554 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6555 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6556 | { | |
6557 | struct rb_node *next; | |
6558 | ||
6559 | next = rb_next(&em->rb_node); | |
6560 | if (!next) | |
6561 | return NULL; | |
6562 | return container_of(next, struct extent_map, rb_node); | |
6563 | } | |
6564 | ||
6565 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6566 | { | |
6567 | struct rb_node *prev; | |
6568 | ||
6569 | prev = rb_prev(&em->rb_node); | |
6570 | if (!prev) | |
6571 | return NULL; | |
6572 | return container_of(prev, struct extent_map, rb_node); | |
6573 | } | |
6574 | ||
d352ac68 | 6575 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6576 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6577 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6578 | * the best fitted new extent into the tree. |
d352ac68 | 6579 | */ |
3b951516 CM |
6580 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6581 | struct extent_map *existing, | |
e6dcd2dc | 6582 | struct extent_map *em, |
51f395ad | 6583 | u64 map_start) |
3b951516 | 6584 | { |
e6c4efd8 QW |
6585 | struct extent_map *prev; |
6586 | struct extent_map *next; | |
6587 | u64 start; | |
6588 | u64 end; | |
3b951516 | 6589 | u64 start_diff; |
3b951516 | 6590 | |
e6dcd2dc | 6591 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6592 | |
6593 | if (existing->start > map_start) { | |
6594 | next = existing; | |
6595 | prev = prev_extent_map(next); | |
6596 | } else { | |
6597 | prev = existing; | |
6598 | next = next_extent_map(prev); | |
6599 | } | |
6600 | ||
6601 | start = prev ? extent_map_end(prev) : em->start; | |
6602 | start = max_t(u64, start, em->start); | |
6603 | end = next ? next->start : extent_map_end(em); | |
6604 | end = min_t(u64, end, extent_map_end(em)); | |
6605 | start_diff = start - em->start; | |
6606 | em->start = start; | |
6607 | em->len = end - start; | |
c8b97818 CM |
6608 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6609 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6610 | em->block_start += start_diff; |
c8b97818 CM |
6611 | em->block_len -= start_diff; |
6612 | } | |
09a2a8f9 | 6613 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6614 | } |
6615 | ||
c8b97818 CM |
6616 | static noinline int uncompress_inline(struct btrfs_path *path, |
6617 | struct inode *inode, struct page *page, | |
6618 | size_t pg_offset, u64 extent_offset, | |
6619 | struct btrfs_file_extent_item *item) | |
6620 | { | |
6621 | int ret; | |
6622 | struct extent_buffer *leaf = path->nodes[0]; | |
6623 | char *tmp; | |
6624 | size_t max_size; | |
6625 | unsigned long inline_size; | |
6626 | unsigned long ptr; | |
261507a0 | 6627 | int compress_type; |
c8b97818 CM |
6628 | |
6629 | WARN_ON(pg_offset != 0); | |
261507a0 | 6630 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6631 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6632 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6633 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6634 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6635 | if (!tmp) |
6636 | return -ENOMEM; | |
c8b97818 CM |
6637 | ptr = btrfs_file_extent_inline_start(item); |
6638 | ||
6639 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6640 | ||
5b050f04 | 6641 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
261507a0 LZ |
6642 | ret = btrfs_decompress(compress_type, tmp, page, |
6643 | extent_offset, inline_size, max_size); | |
c8b97818 | 6644 | kfree(tmp); |
166ae5a4 | 6645 | return ret; |
c8b97818 CM |
6646 | } |
6647 | ||
d352ac68 CM |
6648 | /* |
6649 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6650 | * the ugly parts come from merging extents from the disk with the in-ram |
6651 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6652 | * where the in-ram extents might be locked pending data=ordered completion. |
6653 | * | |
6654 | * This also copies inline extents directly into the page. | |
6655 | */ | |
d397712b | 6656 | |
a52d9a80 | 6657 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 6658 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
6659 | int create) |
6660 | { | |
6661 | int ret; | |
6662 | int err = 0; | |
a52d9a80 CM |
6663 | u64 extent_start = 0; |
6664 | u64 extent_end = 0; | |
33345d01 | 6665 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6666 | u32 found_type; |
f421950f | 6667 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
6668 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6669 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
6670 | struct extent_buffer *leaf; |
6671 | struct btrfs_key found_key; | |
a52d9a80 CM |
6672 | struct extent_map *em = NULL; |
6673 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 6674 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 | 6675 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6676 | const bool new_inline = !page || create; |
a52d9a80 | 6677 | |
a52d9a80 | 6678 | again: |
890871be | 6679 | read_lock(&em_tree->lock); |
d1310b2e | 6680 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d CM |
6681 | if (em) |
6682 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
890871be | 6683 | read_unlock(&em_tree->lock); |
d1310b2e | 6684 | |
a52d9a80 | 6685 | if (em) { |
e1c4b745 CM |
6686 | if (em->start > start || em->start + em->len <= start) |
6687 | free_extent_map(em); | |
6688 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6689 | free_extent_map(em); |
6690 | else | |
6691 | goto out; | |
a52d9a80 | 6692 | } |
172ddd60 | 6693 | em = alloc_extent_map(); |
a52d9a80 | 6694 | if (!em) { |
d1310b2e CM |
6695 | err = -ENOMEM; |
6696 | goto out; | |
a52d9a80 | 6697 | } |
e6dcd2dc | 6698 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
d1310b2e | 6699 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6700 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6701 | em->len = (u64)-1; |
c8b97818 | 6702 | em->block_len = (u64)-1; |
f421950f CM |
6703 | |
6704 | if (!path) { | |
6705 | path = btrfs_alloc_path(); | |
026fd317 JB |
6706 | if (!path) { |
6707 | err = -ENOMEM; | |
6708 | goto out; | |
6709 | } | |
6710 | /* | |
6711 | * Chances are we'll be called again, so go ahead and do | |
6712 | * readahead | |
6713 | */ | |
6714 | path->reada = 1; | |
f421950f CM |
6715 | } |
6716 | ||
179e29e4 CM |
6717 | ret = btrfs_lookup_file_extent(trans, root, path, |
6718 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6719 | if (ret < 0) { |
6720 | err = ret; | |
6721 | goto out; | |
6722 | } | |
6723 | ||
6724 | if (ret != 0) { | |
6725 | if (path->slots[0] == 0) | |
6726 | goto not_found; | |
6727 | path->slots[0]--; | |
6728 | } | |
6729 | ||
5f39d397 CM |
6730 | leaf = path->nodes[0]; |
6731 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6732 | struct btrfs_file_extent_item); |
a52d9a80 | 6733 | /* are we inside the extent that was found? */ |
5f39d397 | 6734 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6735 | found_type = found_key.type; |
5f39d397 | 6736 | if (found_key.objectid != objectid || |
a52d9a80 | 6737 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6738 | /* |
6739 | * If we backup past the first extent we want to move forward | |
6740 | * and see if there is an extent in front of us, otherwise we'll | |
6741 | * say there is a hole for our whole search range which can | |
6742 | * cause problems. | |
6743 | */ | |
6744 | extent_end = start; | |
6745 | goto next; | |
a52d9a80 CM |
6746 | } |
6747 | ||
5f39d397 CM |
6748 | found_type = btrfs_file_extent_type(leaf, item); |
6749 | extent_start = found_key.offset; | |
d899e052 YZ |
6750 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6751 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6752 | extent_end = extent_start + |
db94535d | 6753 | btrfs_file_extent_num_bytes(leaf, item); |
9036c102 YZ |
6754 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6755 | size_t size; | |
514ac8ad | 6756 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
fda2832f | 6757 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
9036c102 | 6758 | } |
25a50341 | 6759 | next: |
9036c102 YZ |
6760 | if (start >= extent_end) { |
6761 | path->slots[0]++; | |
6762 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6763 | ret = btrfs_next_leaf(root, path); | |
6764 | if (ret < 0) { | |
6765 | err = ret; | |
6766 | goto out; | |
a52d9a80 | 6767 | } |
9036c102 YZ |
6768 | if (ret > 0) |
6769 | goto not_found; | |
6770 | leaf = path->nodes[0]; | |
a52d9a80 | 6771 | } |
9036c102 YZ |
6772 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6773 | if (found_key.objectid != objectid || | |
6774 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6775 | goto not_found; | |
6776 | if (start + len <= found_key.offset) | |
6777 | goto not_found; | |
e2eca69d WS |
6778 | if (start > found_key.offset) |
6779 | goto next; | |
9036c102 | 6780 | em->start = start; |
70c8a91c | 6781 | em->orig_start = start; |
9036c102 YZ |
6782 | em->len = found_key.offset - start; |
6783 | goto not_found_em; | |
6784 | } | |
6785 | ||
7ffbb598 FM |
6786 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
6787 | ||
d899e052 YZ |
6788 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6789 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
6790 | goto insert; |
6791 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 6792 | unsigned long ptr; |
a52d9a80 | 6793 | char *map; |
3326d1b0 CM |
6794 | size_t size; |
6795 | size_t extent_offset; | |
6796 | size_t copy_size; | |
a52d9a80 | 6797 | |
7ffbb598 | 6798 | if (new_inline) |
689f9346 | 6799 | goto out; |
5f39d397 | 6800 | |
514ac8ad | 6801 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 6802 | extent_offset = page_offset(page) + pg_offset - extent_start; |
70dec807 | 6803 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
3326d1b0 | 6804 | size - extent_offset); |
3326d1b0 | 6805 | em->start = extent_start + extent_offset; |
fda2832f | 6806 | em->len = ALIGN(copy_size, root->sectorsize); |
b4939680 | 6807 | em->orig_block_len = em->len; |
70c8a91c | 6808 | em->orig_start = em->start; |
689f9346 | 6809 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 6810 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
6811 | if (btrfs_file_extent_compression(leaf, item) != |
6812 | BTRFS_COMPRESS_NONE) { | |
c8b97818 CM |
6813 | ret = uncompress_inline(path, inode, page, |
6814 | pg_offset, | |
6815 | extent_offset, item); | |
166ae5a4 ZB |
6816 | if (ret) { |
6817 | err = ret; | |
6818 | goto out; | |
6819 | } | |
c8b97818 CM |
6820 | } else { |
6821 | map = kmap(page); | |
6822 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6823 | copy_size); | |
93c82d57 CM |
6824 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
6825 | memset(map + pg_offset + copy_size, 0, | |
6826 | PAGE_CACHE_SIZE - pg_offset - | |
6827 | copy_size); | |
6828 | } | |
c8b97818 CM |
6829 | kunmap(page); |
6830 | } | |
179e29e4 CM |
6831 | flush_dcache_page(page); |
6832 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 6833 | BUG(); |
179e29e4 CM |
6834 | if (!trans) { |
6835 | kunmap(page); | |
6836 | free_extent_map(em); | |
6837 | em = NULL; | |
ff5714cc | 6838 | |
b3b4aa74 | 6839 | btrfs_release_path(path); |
7a7eaa40 | 6840 | trans = btrfs_join_transaction(root); |
ff5714cc | 6841 | |
3612b495 TI |
6842 | if (IS_ERR(trans)) |
6843 | return ERR_CAST(trans); | |
179e29e4 CM |
6844 | goto again; |
6845 | } | |
c8b97818 | 6846 | map = kmap(page); |
70dec807 | 6847 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 6848 | copy_size); |
c8b97818 | 6849 | kunmap(page); |
179e29e4 | 6850 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 6851 | } |
d1310b2e | 6852 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6853 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6854 | goto insert; |
a52d9a80 CM |
6855 | } |
6856 | not_found: | |
6857 | em->start = start; | |
70c8a91c | 6858 | em->orig_start = start; |
d1310b2e | 6859 | em->len = len; |
a52d9a80 | 6860 | not_found_em: |
5f39d397 | 6861 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 6862 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 6863 | insert: |
b3b4aa74 | 6864 | btrfs_release_path(path); |
d1310b2e | 6865 | if (em->start > start || extent_map_end(em) <= start) { |
c2cf52eb | 6866 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
c1c9ff7c | 6867 | em->start, em->len, start, len); |
a52d9a80 CM |
6868 | err = -EIO; |
6869 | goto out; | |
6870 | } | |
d1310b2e CM |
6871 | |
6872 | err = 0; | |
890871be | 6873 | write_lock(&em_tree->lock); |
09a2a8f9 | 6874 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6875 | /* it is possible that someone inserted the extent into the tree |
6876 | * while we had the lock dropped. It is also possible that | |
6877 | * an overlapping map exists in the tree | |
6878 | */ | |
a52d9a80 | 6879 | if (ret == -EEXIST) { |
3b951516 | 6880 | struct extent_map *existing; |
e6dcd2dc CM |
6881 | |
6882 | ret = 0; | |
6883 | ||
e6c4efd8 QW |
6884 | existing = search_extent_mapping(em_tree, start, len); |
6885 | /* | |
6886 | * existing will always be non-NULL, since there must be | |
6887 | * extent causing the -EEXIST. | |
6888 | */ | |
6889 | if (start >= extent_map_end(existing) || | |
32be3a1a | 6890 | start <= existing->start) { |
e6c4efd8 QW |
6891 | /* |
6892 | * The existing extent map is the one nearest to | |
6893 | * the [start, start + len) range which overlaps | |
6894 | */ | |
6895 | err = merge_extent_mapping(em_tree, existing, | |
6896 | em, start); | |
e1c4b745 | 6897 | free_extent_map(existing); |
e6c4efd8 | 6898 | if (err) { |
3b951516 CM |
6899 | free_extent_map(em); |
6900 | em = NULL; | |
6901 | } | |
6902 | } else { | |
6903 | free_extent_map(em); | |
6904 | em = existing; | |
e6dcd2dc | 6905 | err = 0; |
a52d9a80 | 6906 | } |
a52d9a80 | 6907 | } |
890871be | 6908 | write_unlock(&em_tree->lock); |
a52d9a80 | 6909 | out: |
1abe9b8a | 6910 | |
4cd8587c | 6911 | trace_btrfs_get_extent(root, em); |
1abe9b8a | 6912 | |
527afb44 | 6913 | btrfs_free_path(path); |
a52d9a80 CM |
6914 | if (trans) { |
6915 | ret = btrfs_end_transaction(trans, root); | |
d397712b | 6916 | if (!err) |
a52d9a80 CM |
6917 | err = ret; |
6918 | } | |
a52d9a80 CM |
6919 | if (err) { |
6920 | free_extent_map(em); | |
a52d9a80 CM |
6921 | return ERR_PTR(err); |
6922 | } | |
79787eaa | 6923 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
6924 | return em; |
6925 | } | |
6926 | ||
ec29ed5b CM |
6927 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
6928 | size_t pg_offset, u64 start, u64 len, | |
6929 | int create) | |
6930 | { | |
6931 | struct extent_map *em; | |
6932 | struct extent_map *hole_em = NULL; | |
6933 | u64 range_start = start; | |
6934 | u64 end; | |
6935 | u64 found; | |
6936 | u64 found_end; | |
6937 | int err = 0; | |
6938 | ||
6939 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
6940 | if (IS_ERR(em)) | |
6941 | return em; | |
6942 | if (em) { | |
6943 | /* | |
f9e4fb53 LB |
6944 | * if our em maps to |
6945 | * - a hole or | |
6946 | * - a pre-alloc extent, | |
6947 | * there might actually be delalloc bytes behind it. | |
ec29ed5b | 6948 | */ |
f9e4fb53 LB |
6949 | if (em->block_start != EXTENT_MAP_HOLE && |
6950 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
ec29ed5b CM |
6951 | return em; |
6952 | else | |
6953 | hole_em = em; | |
6954 | } | |
6955 | ||
6956 | /* check to see if we've wrapped (len == -1 or similar) */ | |
6957 | end = start + len; | |
6958 | if (end < start) | |
6959 | end = (u64)-1; | |
6960 | else | |
6961 | end -= 1; | |
6962 | ||
6963 | em = NULL; | |
6964 | ||
6965 | /* ok, we didn't find anything, lets look for delalloc */ | |
6966 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, | |
6967 | end, len, EXTENT_DELALLOC, 1); | |
6968 | found_end = range_start + found; | |
6969 | if (found_end < range_start) | |
6970 | found_end = (u64)-1; | |
6971 | ||
6972 | /* | |
6973 | * we didn't find anything useful, return | |
6974 | * the original results from get_extent() | |
6975 | */ | |
6976 | if (range_start > end || found_end <= start) { | |
6977 | em = hole_em; | |
6978 | hole_em = NULL; | |
6979 | goto out; | |
6980 | } | |
6981 | ||
6982 | /* adjust the range_start to make sure it doesn't | |
6983 | * go backwards from the start they passed in | |
6984 | */ | |
67871254 | 6985 | range_start = max(start, range_start); |
ec29ed5b CM |
6986 | found = found_end - range_start; |
6987 | ||
6988 | if (found > 0) { | |
6989 | u64 hole_start = start; | |
6990 | u64 hole_len = len; | |
6991 | ||
172ddd60 | 6992 | em = alloc_extent_map(); |
ec29ed5b CM |
6993 | if (!em) { |
6994 | err = -ENOMEM; | |
6995 | goto out; | |
6996 | } | |
6997 | /* | |
6998 | * when btrfs_get_extent can't find anything it | |
6999 | * returns one huge hole | |
7000 | * | |
7001 | * make sure what it found really fits our range, and | |
7002 | * adjust to make sure it is based on the start from | |
7003 | * the caller | |
7004 | */ | |
7005 | if (hole_em) { | |
7006 | u64 calc_end = extent_map_end(hole_em); | |
7007 | ||
7008 | if (calc_end <= start || (hole_em->start > end)) { | |
7009 | free_extent_map(hole_em); | |
7010 | hole_em = NULL; | |
7011 | } else { | |
7012 | hole_start = max(hole_em->start, start); | |
7013 | hole_len = calc_end - hole_start; | |
7014 | } | |
7015 | } | |
7016 | em->bdev = NULL; | |
7017 | if (hole_em && range_start > hole_start) { | |
7018 | /* our hole starts before our delalloc, so we | |
7019 | * have to return just the parts of the hole | |
7020 | * that go until the delalloc starts | |
7021 | */ | |
7022 | em->len = min(hole_len, | |
7023 | range_start - hole_start); | |
7024 | em->start = hole_start; | |
7025 | em->orig_start = hole_start; | |
7026 | /* | |
7027 | * don't adjust block start at all, | |
7028 | * it is fixed at EXTENT_MAP_HOLE | |
7029 | */ | |
7030 | em->block_start = hole_em->block_start; | |
7031 | em->block_len = hole_len; | |
f9e4fb53 LB |
7032 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7033 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7034 | } else { |
7035 | em->start = range_start; | |
7036 | em->len = found; | |
7037 | em->orig_start = range_start; | |
7038 | em->block_start = EXTENT_MAP_DELALLOC; | |
7039 | em->block_len = found; | |
7040 | } | |
7041 | } else if (hole_em) { | |
7042 | return hole_em; | |
7043 | } | |
7044 | out: | |
7045 | ||
7046 | free_extent_map(hole_em); | |
7047 | if (err) { | |
7048 | free_extent_map(em); | |
7049 | return ERR_PTR(err); | |
7050 | } | |
7051 | return em; | |
7052 | } | |
7053 | ||
4b46fce2 JB |
7054 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7055 | u64 start, u64 len) | |
7056 | { | |
7057 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
70c8a91c | 7058 | struct extent_map *em; |
4b46fce2 JB |
7059 | struct btrfs_key ins; |
7060 | u64 alloc_hint; | |
7061 | int ret; | |
4b46fce2 | 7062 | |
4b46fce2 | 7063 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
00361589 | 7064 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
e570fd27 | 7065 | alloc_hint, &ins, 1, 1); |
00361589 JB |
7066 | if (ret) |
7067 | return ERR_PTR(ret); | |
4b46fce2 | 7068 | |
70c8a91c | 7069 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
cc95bef6 | 7070 | ins.offset, ins.offset, ins.offset, 0); |
00361589 | 7071 | if (IS_ERR(em)) { |
e570fd27 | 7072 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
00361589 JB |
7073 | return em; |
7074 | } | |
4b46fce2 JB |
7075 | |
7076 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, | |
7077 | ins.offset, ins.offset, 0); | |
7078 | if (ret) { | |
e570fd27 | 7079 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
00361589 JB |
7080 | free_extent_map(em); |
7081 | return ERR_PTR(ret); | |
4b46fce2 | 7082 | } |
00361589 | 7083 | |
4b46fce2 JB |
7084 | return em; |
7085 | } | |
7086 | ||
46bfbb5c CM |
7087 | /* |
7088 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7089 | * block must be cow'd | |
7090 | */ | |
00361589 | 7091 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7092 | u64 *orig_start, u64 *orig_block_len, |
7093 | u64 *ram_bytes) | |
46bfbb5c | 7094 | { |
00361589 | 7095 | struct btrfs_trans_handle *trans; |
46bfbb5c CM |
7096 | struct btrfs_path *path; |
7097 | int ret; | |
7098 | struct extent_buffer *leaf; | |
7099 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7100 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7101 | struct btrfs_file_extent_item *fi; |
7102 | struct btrfs_key key; | |
7103 | u64 disk_bytenr; | |
7104 | u64 backref_offset; | |
7105 | u64 extent_end; | |
7106 | u64 num_bytes; | |
7107 | int slot; | |
7108 | int found_type; | |
7ee9e440 | 7109 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7110 | |
46bfbb5c CM |
7111 | path = btrfs_alloc_path(); |
7112 | if (!path) | |
7113 | return -ENOMEM; | |
7114 | ||
00361589 | 7115 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
46bfbb5c CM |
7116 | offset, 0); |
7117 | if (ret < 0) | |
7118 | goto out; | |
7119 | ||
7120 | slot = path->slots[0]; | |
7121 | if (ret == 1) { | |
7122 | if (slot == 0) { | |
7123 | /* can't find the item, must cow */ | |
7124 | ret = 0; | |
7125 | goto out; | |
7126 | } | |
7127 | slot--; | |
7128 | } | |
7129 | ret = 0; | |
7130 | leaf = path->nodes[0]; | |
7131 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
33345d01 | 7132 | if (key.objectid != btrfs_ino(inode) || |
46bfbb5c CM |
7133 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7134 | /* not our file or wrong item type, must cow */ | |
7135 | goto out; | |
7136 | } | |
7137 | ||
7138 | if (key.offset > offset) { | |
7139 | /* Wrong offset, must cow */ | |
7140 | goto out; | |
7141 | } | |
7142 | ||
7143 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7144 | found_type = btrfs_file_extent_type(leaf, fi); | |
7145 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7146 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7147 | /* not a regular extent, must cow */ | |
7148 | goto out; | |
7149 | } | |
7ee9e440 JB |
7150 | |
7151 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7152 | goto out; | |
7153 | ||
e77751aa MX |
7154 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7155 | if (extent_end <= offset) | |
7156 | goto out; | |
7157 | ||
46bfbb5c | 7158 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7159 | if (disk_bytenr == 0) |
7160 | goto out; | |
7161 | ||
7162 | if (btrfs_file_extent_compression(leaf, fi) || | |
7163 | btrfs_file_extent_encryption(leaf, fi) || | |
7164 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7165 | goto out; | |
7166 | ||
46bfbb5c CM |
7167 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7168 | ||
7ee9e440 JB |
7169 | if (orig_start) { |
7170 | *orig_start = key.offset - backref_offset; | |
7171 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7172 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7173 | } | |
eb384b55 | 7174 | |
46bfbb5c CM |
7175 | if (btrfs_extent_readonly(root, disk_bytenr)) |
7176 | goto out; | |
7b2b7085 MX |
7177 | |
7178 | num_bytes = min(offset + *len, extent_end) - offset; | |
7179 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7180 | u64 range_end; | |
7181 | ||
7182 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; | |
7183 | ret = test_range_bit(io_tree, offset, range_end, | |
7184 | EXTENT_DELALLOC, 0, NULL); | |
7185 | if (ret) { | |
7186 | ret = -EAGAIN; | |
7187 | goto out; | |
7188 | } | |
7189 | } | |
7190 | ||
1bda19eb | 7191 | btrfs_release_path(path); |
46bfbb5c CM |
7192 | |
7193 | /* | |
7194 | * look for other files referencing this extent, if we | |
7195 | * find any we must cow | |
7196 | */ | |
00361589 JB |
7197 | trans = btrfs_join_transaction(root); |
7198 | if (IS_ERR(trans)) { | |
7199 | ret = 0; | |
46bfbb5c | 7200 | goto out; |
00361589 JB |
7201 | } |
7202 | ||
7203 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), | |
7204 | key.offset - backref_offset, disk_bytenr); | |
7205 | btrfs_end_transaction(trans, root); | |
7206 | if (ret) { | |
7207 | ret = 0; | |
7208 | goto out; | |
7209 | } | |
46bfbb5c CM |
7210 | |
7211 | /* | |
7212 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7213 | * in this extent we are about to write. If there | |
7214 | * are any csums in that range we have to cow in order | |
7215 | * to keep the csums correct | |
7216 | */ | |
7217 | disk_bytenr += backref_offset; | |
7218 | disk_bytenr += offset - key.offset; | |
46bfbb5c CM |
7219 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
7220 | goto out; | |
7221 | /* | |
7222 | * all of the above have passed, it is safe to overwrite this extent | |
7223 | * without cow | |
7224 | */ | |
eb384b55 | 7225 | *len = num_bytes; |
46bfbb5c CM |
7226 | ret = 1; |
7227 | out: | |
7228 | btrfs_free_path(path); | |
7229 | return ret; | |
7230 | } | |
7231 | ||
fc4adbff AG |
7232 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7233 | { | |
7234 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
7235 | int found = false; | |
7236 | void **pagep = NULL; | |
7237 | struct page *page = NULL; | |
7238 | int start_idx; | |
7239 | int end_idx; | |
7240 | ||
7241 | start_idx = start >> PAGE_CACHE_SHIFT; | |
7242 | ||
7243 | /* | |
7244 | * end is the last byte in the last page. end == start is legal | |
7245 | */ | |
7246 | end_idx = end >> PAGE_CACHE_SHIFT; | |
7247 | ||
7248 | rcu_read_lock(); | |
7249 | ||
7250 | /* Most of the code in this while loop is lifted from | |
7251 | * find_get_page. It's been modified to begin searching from a | |
7252 | * page and return just the first page found in that range. If the | |
7253 | * found idx is less than or equal to the end idx then we know that | |
7254 | * a page exists. If no pages are found or if those pages are | |
7255 | * outside of the range then we're fine (yay!) */ | |
7256 | while (page == NULL && | |
7257 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7258 | page = radix_tree_deref_slot(pagep); | |
7259 | if (unlikely(!page)) | |
7260 | break; | |
7261 | ||
7262 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7263 | if (radix_tree_deref_retry(page)) { |
7264 | page = NULL; | |
fc4adbff | 7265 | continue; |
809f9016 | 7266 | } |
fc4adbff AG |
7267 | /* |
7268 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7269 | * here as an exceptional entry: so return it without | |
7270 | * attempting to raise page count. | |
7271 | */ | |
6fdef6d4 | 7272 | page = NULL; |
fc4adbff AG |
7273 | break; /* TODO: Is this relevant for this use case? */ |
7274 | } | |
7275 | ||
91405151 FM |
7276 | if (!page_cache_get_speculative(page)) { |
7277 | page = NULL; | |
fc4adbff | 7278 | continue; |
91405151 | 7279 | } |
fc4adbff AG |
7280 | |
7281 | /* | |
7282 | * Has the page moved? | |
7283 | * This is part of the lockless pagecache protocol. See | |
7284 | * include/linux/pagemap.h for details. | |
7285 | */ | |
7286 | if (unlikely(page != *pagep)) { | |
7287 | page_cache_release(page); | |
7288 | page = NULL; | |
7289 | } | |
7290 | } | |
7291 | ||
7292 | if (page) { | |
7293 | if (page->index <= end_idx) | |
7294 | found = true; | |
7295 | page_cache_release(page); | |
7296 | } | |
7297 | ||
7298 | rcu_read_unlock(); | |
7299 | return found; | |
7300 | } | |
7301 | ||
eb838e73 JB |
7302 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7303 | struct extent_state **cached_state, int writing) | |
7304 | { | |
7305 | struct btrfs_ordered_extent *ordered; | |
7306 | int ret = 0; | |
7307 | ||
7308 | while (1) { | |
7309 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7310 | 0, cached_state); | |
7311 | /* | |
7312 | * We're concerned with the entire range that we're going to be | |
7313 | * doing DIO to, so we need to make sure theres no ordered | |
7314 | * extents in this range. | |
7315 | */ | |
7316 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | |
7317 | lockend - lockstart + 1); | |
7318 | ||
7319 | /* | |
7320 | * We need to make sure there are no buffered pages in this | |
7321 | * range either, we could have raced between the invalidate in | |
7322 | * generic_file_direct_write and locking the extent. The | |
7323 | * invalidate needs to happen so that reads after a write do not | |
7324 | * get stale data. | |
7325 | */ | |
fc4adbff AG |
7326 | if (!ordered && |
7327 | (!writing || | |
7328 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7329 | break; |
7330 | ||
7331 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7332 | cached_state, GFP_NOFS); | |
7333 | ||
7334 | if (ordered) { | |
7335 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7336 | btrfs_put_ordered_extent(ordered); | |
7337 | } else { | |
7338 | /* Screw you mmap */ | |
728404da | 7339 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
075bdbdb FM |
7340 | if (ret) |
7341 | break; | |
7342 | ret = filemap_fdatawait_range(inode->i_mapping, | |
7343 | lockstart, | |
7344 | lockend); | |
eb838e73 JB |
7345 | if (ret) |
7346 | break; | |
7347 | ||
7348 | /* | |
7349 | * If we found a page that couldn't be invalidated just | |
7350 | * fall back to buffered. | |
7351 | */ | |
7352 | ret = invalidate_inode_pages2_range(inode->i_mapping, | |
7353 | lockstart >> PAGE_CACHE_SHIFT, | |
7354 | lockend >> PAGE_CACHE_SHIFT); | |
7355 | if (ret) | |
7356 | break; | |
7357 | } | |
7358 | ||
7359 | cond_resched(); | |
7360 | } | |
7361 | ||
7362 | return ret; | |
7363 | } | |
7364 | ||
69ffb543 JB |
7365 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
7366 | u64 len, u64 orig_start, | |
7367 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
7368 | u64 orig_block_len, u64 ram_bytes, |
7369 | int type) | |
69ffb543 JB |
7370 | { |
7371 | struct extent_map_tree *em_tree; | |
7372 | struct extent_map *em; | |
7373 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7374 | int ret; | |
7375 | ||
7376 | em_tree = &BTRFS_I(inode)->extent_tree; | |
7377 | em = alloc_extent_map(); | |
7378 | if (!em) | |
7379 | return ERR_PTR(-ENOMEM); | |
7380 | ||
7381 | em->start = start; | |
7382 | em->orig_start = orig_start; | |
2ab28f32 JB |
7383 | em->mod_start = start; |
7384 | em->mod_len = len; | |
69ffb543 JB |
7385 | em->len = len; |
7386 | em->block_len = block_len; | |
7387 | em->block_start = block_start; | |
7388 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7389 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7390 | em->ram_bytes = ram_bytes; |
70c8a91c | 7391 | em->generation = -1; |
69ffb543 JB |
7392 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
7393 | if (type == BTRFS_ORDERED_PREALLOC) | |
b11e234d | 7394 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
69ffb543 JB |
7395 | |
7396 | do { | |
7397 | btrfs_drop_extent_cache(inode, em->start, | |
7398 | em->start + em->len - 1, 0); | |
7399 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7400 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 JB |
7401 | write_unlock(&em_tree->lock); |
7402 | } while (ret == -EEXIST); | |
7403 | ||
7404 | if (ret) { | |
7405 | free_extent_map(em); | |
7406 | return ERR_PTR(ret); | |
7407 | } | |
7408 | ||
7409 | return em; | |
7410 | } | |
7411 | ||
50745b0a | 7412 | struct btrfs_dio_data { |
7413 | u64 outstanding_extents; | |
7414 | u64 reserve; | |
7415 | }; | |
69ffb543 | 7416 | |
4b46fce2 JB |
7417 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7418 | struct buffer_head *bh_result, int create) | |
7419 | { | |
7420 | struct extent_map *em; | |
7421 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
eb838e73 | 7422 | struct extent_state *cached_state = NULL; |
50745b0a | 7423 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7424 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7425 | u64 lockstart, lockend; |
4b46fce2 | 7426 | u64 len = bh_result->b_size; |
eb838e73 | 7427 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7428 | int ret = 0; |
eb838e73 | 7429 | |
172a5049 | 7430 | if (create) |
3266789f | 7431 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7432 | else |
c329861d | 7433 | len = min_t(u64, len, root->sectorsize); |
eb838e73 | 7434 | |
c329861d JB |
7435 | lockstart = start; |
7436 | lockend = start + len - 1; | |
7437 | ||
e1cbbfa5 JB |
7438 | if (current->journal_info) { |
7439 | /* | |
7440 | * Need to pull our outstanding extents and set journal_info to NULL so | |
7441 | * that anything that needs to check if there's a transction doesn't get | |
7442 | * confused. | |
7443 | */ | |
50745b0a | 7444 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7445 | current->journal_info = NULL; |
7446 | } | |
7447 | ||
eb838e73 JB |
7448 | /* |
7449 | * If this errors out it's because we couldn't invalidate pagecache for | |
7450 | * this range and we need to fallback to buffered. | |
7451 | */ | |
7452 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) | |
7453 | return -ENOTBLK; | |
7454 | ||
4b46fce2 | 7455 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
eb838e73 JB |
7456 | if (IS_ERR(em)) { |
7457 | ret = PTR_ERR(em); | |
7458 | goto unlock_err; | |
7459 | } | |
4b46fce2 JB |
7460 | |
7461 | /* | |
7462 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7463 | * io. INLINE is special, and we could probably kludge it in here, but | |
7464 | * it's still buffered so for safety lets just fall back to the generic | |
7465 | * buffered path. | |
7466 | * | |
7467 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7468 | * decompress it, so there will be buffering required no matter what we | |
7469 | * do, so go ahead and fallback to buffered. | |
7470 | * | |
7471 | * We return -ENOTBLK because thats what makes DIO go ahead and go back | |
7472 | * to buffered IO. Don't blame me, this is the price we pay for using | |
7473 | * the generic code. | |
7474 | */ | |
7475 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7476 | em->block_start == EXTENT_MAP_INLINE) { | |
7477 | free_extent_map(em); | |
eb838e73 JB |
7478 | ret = -ENOTBLK; |
7479 | goto unlock_err; | |
4b46fce2 JB |
7480 | } |
7481 | ||
7482 | /* Just a good old fashioned hole, return */ | |
7483 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7484 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7485 | free_extent_map(em); | |
eb838e73 | 7486 | goto unlock_err; |
4b46fce2 JB |
7487 | } |
7488 | ||
7489 | /* | |
7490 | * We don't allocate a new extent in the following cases | |
7491 | * | |
7492 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7493 | * existing extent. | |
7494 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7495 | * just use the extent. | |
7496 | * | |
7497 | */ | |
46bfbb5c | 7498 | if (!create) { |
eb838e73 JB |
7499 | len = min(len, em->len - (start - em->start)); |
7500 | lockstart = start + len; | |
7501 | goto unlock; | |
46bfbb5c | 7502 | } |
4b46fce2 JB |
7503 | |
7504 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7505 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7506 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7507 | int type; |
eb384b55 | 7508 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7509 | |
7510 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7511 | type = BTRFS_ORDERED_PREALLOC; | |
7512 | else | |
7513 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7514 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7515 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7516 | |
00361589 | 7517 | if (can_nocow_extent(inode, start, &len, &orig_start, |
7ee9e440 | 7518 | &orig_block_len, &ram_bytes) == 1) { |
69ffb543 JB |
7519 | if (type == BTRFS_ORDERED_PREALLOC) { |
7520 | free_extent_map(em); | |
7521 | em = create_pinned_em(inode, start, len, | |
7522 | orig_start, | |
b4939680 | 7523 | block_start, len, |
cc95bef6 JB |
7524 | orig_block_len, |
7525 | ram_bytes, type); | |
555e1286 FM |
7526 | if (IS_ERR(em)) { |
7527 | ret = PTR_ERR(em); | |
69ffb543 | 7528 | goto unlock_err; |
555e1286 | 7529 | } |
69ffb543 JB |
7530 | } |
7531 | ||
46bfbb5c CM |
7532 | ret = btrfs_add_ordered_extent_dio(inode, start, |
7533 | block_start, len, len, type); | |
46bfbb5c CM |
7534 | if (ret) { |
7535 | free_extent_map(em); | |
eb838e73 | 7536 | goto unlock_err; |
46bfbb5c CM |
7537 | } |
7538 | goto unlock; | |
4b46fce2 | 7539 | } |
4b46fce2 | 7540 | } |
00361589 | 7541 | |
46bfbb5c CM |
7542 | /* |
7543 | * this will cow the extent, reset the len in case we changed | |
7544 | * it above | |
7545 | */ | |
7546 | len = bh_result->b_size; | |
70c8a91c JB |
7547 | free_extent_map(em); |
7548 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7549 | if (IS_ERR(em)) { |
7550 | ret = PTR_ERR(em); | |
7551 | goto unlock_err; | |
7552 | } | |
46bfbb5c CM |
7553 | len = min(len, em->len - (start - em->start)); |
7554 | unlock: | |
4b46fce2 JB |
7555 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7556 | inode->i_blkbits; | |
46bfbb5c | 7557 | bh_result->b_size = len; |
4b46fce2 JB |
7558 | bh_result->b_bdev = em->bdev; |
7559 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7560 | if (create) { |
7561 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7562 | set_buffer_new(bh_result); | |
7563 | ||
7564 | /* | |
7565 | * Need to update the i_size under the extent lock so buffered | |
7566 | * readers will get the updated i_size when we unlock. | |
7567 | */ | |
7568 | if (start + len > i_size_read(inode)) | |
7569 | i_size_write(inode, start + len); | |
0934856d | 7570 | |
e1cbbfa5 JB |
7571 | /* |
7572 | * If we have an outstanding_extents count still set then we're | |
7573 | * within our reservation, otherwise we need to adjust our inode | |
7574 | * counter appropriately. | |
7575 | */ | |
50745b0a | 7576 | if (dio_data->outstanding_extents) { |
7577 | (dio_data->outstanding_extents)--; | |
e1cbbfa5 | 7578 | } else { |
3e05bde8 JB |
7579 | spin_lock(&BTRFS_I(inode)->lock); |
7580 | BTRFS_I(inode)->outstanding_extents++; | |
7581 | spin_unlock(&BTRFS_I(inode)->lock); | |
7582 | } | |
e1cbbfa5 | 7583 | |
3266789f | 7584 | btrfs_free_reserved_data_space(inode, len); |
50745b0a | 7585 | WARN_ON(dio_data->reserve < len); |
7586 | dio_data->reserve -= len; | |
7587 | current->journal_info = dio_data; | |
c3473e83 | 7588 | } |
4b46fce2 | 7589 | |
eb838e73 JB |
7590 | /* |
7591 | * In the case of write we need to clear and unlock the entire range, | |
7592 | * in the case of read we need to unlock only the end area that we | |
7593 | * aren't using if there is any left over space. | |
7594 | */ | |
24c03fa5 | 7595 | if (lockstart < lockend) { |
0934856d MX |
7596 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7597 | lockend, unlock_bits, 1, 0, | |
7598 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7599 | } else { |
eb838e73 | 7600 | free_extent_state(cached_state); |
24c03fa5 | 7601 | } |
eb838e73 | 7602 | |
4b46fce2 JB |
7603 | free_extent_map(em); |
7604 | ||
7605 | return 0; | |
eb838e73 JB |
7606 | |
7607 | unlock_err: | |
eb838e73 JB |
7608 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7609 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
50745b0a | 7610 | if (dio_data) |
7611 | current->journal_info = dio_data; | |
eb838e73 | 7612 | return ret; |
4b46fce2 JB |
7613 | } |
7614 | ||
8b110e39 MX |
7615 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
7616 | int rw, int mirror_num) | |
7617 | { | |
7618 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7619 | int ret; | |
7620 | ||
7621 | BUG_ON(rw & REQ_WRITE); | |
7622 | ||
7623 | bio_get(bio); | |
7624 | ||
7625 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, | |
7626 | BTRFS_WQ_ENDIO_DIO_REPAIR); | |
7627 | if (ret) | |
7628 | goto err; | |
7629 | ||
7630 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); | |
7631 | err: | |
7632 | bio_put(bio); | |
7633 | return ret; | |
7634 | } | |
7635 | ||
7636 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7637 | struct bio *failed_bio, | |
7638 | struct io_failure_record *failrec, | |
7639 | int failed_mirror) | |
7640 | { | |
7641 | int num_copies; | |
7642 | ||
7643 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, | |
7644 | failrec->logical, failrec->len); | |
7645 | if (num_copies == 1) { | |
7646 | /* | |
7647 | * we only have a single copy of the data, so don't bother with | |
7648 | * all the retry and error correction code that follows. no | |
7649 | * matter what the error is, it is very likely to persist. | |
7650 | */ | |
7651 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7652 | num_copies, failrec->this_mirror, failed_mirror); | |
7653 | return 0; | |
7654 | } | |
7655 | ||
7656 | failrec->failed_mirror = failed_mirror; | |
7657 | failrec->this_mirror++; | |
7658 | if (failrec->this_mirror == failed_mirror) | |
7659 | failrec->this_mirror++; | |
7660 | ||
7661 | if (failrec->this_mirror > num_copies) { | |
7662 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7663 | num_copies, failrec->this_mirror, failed_mirror); | |
7664 | return 0; | |
7665 | } | |
7666 | ||
7667 | return 1; | |
7668 | } | |
7669 | ||
7670 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, | |
7671 | struct page *page, u64 start, u64 end, | |
7672 | int failed_mirror, bio_end_io_t *repair_endio, | |
7673 | void *repair_arg) | |
7674 | { | |
7675 | struct io_failure_record *failrec; | |
7676 | struct bio *bio; | |
7677 | int isector; | |
7678 | int read_mode; | |
7679 | int ret; | |
7680 | ||
7681 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); | |
7682 | ||
7683 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7684 | if (ret) | |
7685 | return ret; | |
7686 | ||
7687 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7688 | failed_mirror); | |
7689 | if (!ret) { | |
7690 | free_io_failure(inode, failrec); | |
7691 | return -EIO; | |
7692 | } | |
7693 | ||
7694 | if (failed_bio->bi_vcnt > 1) | |
7695 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; | |
7696 | else | |
7697 | read_mode = READ_SYNC; | |
7698 | ||
7699 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
7700 | isector >>= inode->i_sb->s_blocksize_bits; | |
7701 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
7702 | 0, isector, repair_endio, repair_arg); | |
7703 | if (!bio) { | |
7704 | free_io_failure(inode, failrec); | |
7705 | return -EIO; | |
7706 | } | |
7707 | ||
7708 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
7709 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", | |
7710 | read_mode, failrec->this_mirror, failrec->in_validation); | |
7711 | ||
7712 | ret = submit_dio_repair_bio(inode, bio, read_mode, | |
7713 | failrec->this_mirror); | |
7714 | if (ret) { | |
7715 | free_io_failure(inode, failrec); | |
7716 | bio_put(bio); | |
7717 | } | |
7718 | ||
7719 | return ret; | |
7720 | } | |
7721 | ||
7722 | struct btrfs_retry_complete { | |
7723 | struct completion done; | |
7724 | struct inode *inode; | |
7725 | u64 start; | |
7726 | int uptodate; | |
7727 | }; | |
7728 | ||
4246a0b6 | 7729 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
7730 | { |
7731 | struct btrfs_retry_complete *done = bio->bi_private; | |
7732 | struct bio_vec *bvec; | |
7733 | int i; | |
7734 | ||
4246a0b6 | 7735 | if (bio->bi_error) |
8b110e39 MX |
7736 | goto end; |
7737 | ||
7738 | done->uptodate = 1; | |
7739 | bio_for_each_segment_all(bvec, bio, i) | |
7740 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); | |
7741 | end: | |
7742 | complete(&done->done); | |
7743 | bio_put(bio); | |
7744 | } | |
7745 | ||
7746 | static int __btrfs_correct_data_nocsum(struct inode *inode, | |
7747 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 7748 | { |
2c30c71b | 7749 | struct bio_vec *bvec; |
8b110e39 | 7750 | struct btrfs_retry_complete done; |
4b46fce2 | 7751 | u64 start; |
2c30c71b | 7752 | int i; |
c1dc0896 | 7753 | int ret; |
4b46fce2 | 7754 | |
8b110e39 MX |
7755 | start = io_bio->logical; |
7756 | done.inode = inode; | |
7757 | ||
7758 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { | |
7759 | try_again: | |
7760 | done.uptodate = 0; | |
7761 | done.start = start; | |
7762 | init_completion(&done.done); | |
7763 | ||
7764 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, | |
7765 | start + bvec->bv_len - 1, | |
7766 | io_bio->mirror_num, | |
7767 | btrfs_retry_endio_nocsum, &done); | |
7768 | if (ret) | |
7769 | return ret; | |
7770 | ||
7771 | wait_for_completion(&done.done); | |
7772 | ||
7773 | if (!done.uptodate) { | |
7774 | /* We might have another mirror, so try again */ | |
7775 | goto try_again; | |
7776 | } | |
7777 | ||
7778 | start += bvec->bv_len; | |
7779 | } | |
7780 | ||
7781 | return 0; | |
7782 | } | |
7783 | ||
4246a0b6 | 7784 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
7785 | { |
7786 | struct btrfs_retry_complete *done = bio->bi_private; | |
7787 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7788 | struct bio_vec *bvec; | |
7789 | int uptodate; | |
7790 | int ret; | |
7791 | int i; | |
7792 | ||
4246a0b6 | 7793 | if (bio->bi_error) |
8b110e39 MX |
7794 | goto end; |
7795 | ||
7796 | uptodate = 1; | |
7797 | bio_for_each_segment_all(bvec, bio, i) { | |
7798 | ret = __readpage_endio_check(done->inode, io_bio, i, | |
7799 | bvec->bv_page, 0, | |
7800 | done->start, bvec->bv_len); | |
7801 | if (!ret) | |
7802 | clean_io_failure(done->inode, done->start, | |
7803 | bvec->bv_page, 0); | |
7804 | else | |
7805 | uptodate = 0; | |
7806 | } | |
7807 | ||
7808 | done->uptodate = uptodate; | |
7809 | end: | |
7810 | complete(&done->done); | |
7811 | bio_put(bio); | |
7812 | } | |
7813 | ||
7814 | static int __btrfs_subio_endio_read(struct inode *inode, | |
7815 | struct btrfs_io_bio *io_bio, int err) | |
7816 | { | |
7817 | struct bio_vec *bvec; | |
7818 | struct btrfs_retry_complete done; | |
7819 | u64 start; | |
7820 | u64 offset = 0; | |
7821 | int i; | |
7822 | int ret; | |
dc380aea | 7823 | |
8b110e39 | 7824 | err = 0; |
c1dc0896 | 7825 | start = io_bio->logical; |
8b110e39 MX |
7826 | done.inode = inode; |
7827 | ||
c1dc0896 | 7828 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
dc380aea MX |
7829 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
7830 | 0, start, bvec->bv_len); | |
8b110e39 MX |
7831 | if (likely(!ret)) |
7832 | goto next; | |
7833 | try_again: | |
7834 | done.uptodate = 0; | |
7835 | done.start = start; | |
7836 | init_completion(&done.done); | |
7837 | ||
7838 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, | |
7839 | start + bvec->bv_len - 1, | |
7840 | io_bio->mirror_num, | |
7841 | btrfs_retry_endio, &done); | |
7842 | if (ret) { | |
7843 | err = ret; | |
7844 | goto next; | |
7845 | } | |
7846 | ||
7847 | wait_for_completion(&done.done); | |
7848 | ||
7849 | if (!done.uptodate) { | |
7850 | /* We might have another mirror, so try again */ | |
7851 | goto try_again; | |
7852 | } | |
7853 | next: | |
7854 | offset += bvec->bv_len; | |
4b46fce2 | 7855 | start += bvec->bv_len; |
2c30c71b | 7856 | } |
c1dc0896 MX |
7857 | |
7858 | return err; | |
7859 | } | |
7860 | ||
8b110e39 MX |
7861 | static int btrfs_subio_endio_read(struct inode *inode, |
7862 | struct btrfs_io_bio *io_bio, int err) | |
7863 | { | |
7864 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
7865 | ||
7866 | if (skip_csum) { | |
7867 | if (unlikely(err)) | |
7868 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
7869 | else | |
7870 | return 0; | |
7871 | } else { | |
7872 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
7873 | } | |
7874 | } | |
7875 | ||
4246a0b6 | 7876 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
7877 | { |
7878 | struct btrfs_dio_private *dip = bio->bi_private; | |
7879 | struct inode *inode = dip->inode; | |
7880 | struct bio *dio_bio; | |
7881 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4246a0b6 | 7882 | int err = bio->bi_error; |
c1dc0896 | 7883 | |
8b110e39 MX |
7884 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
7885 | err = btrfs_subio_endio_read(inode, io_bio, err); | |
c1dc0896 | 7886 | |
4b46fce2 | 7887 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 7888 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 7889 | dio_bio = dip->dio_bio; |
4b46fce2 | 7890 | |
4b46fce2 | 7891 | kfree(dip); |
c0da7aa1 | 7892 | |
4246a0b6 | 7893 | dio_end_io(dio_bio, bio->bi_error); |
23ea8e5a MX |
7894 | |
7895 | if (io_bio->end_io) | |
7896 | io_bio->end_io(io_bio, err); | |
9be3395b | 7897 | bio_put(bio); |
4b46fce2 JB |
7898 | } |
7899 | ||
4246a0b6 | 7900 | static void btrfs_endio_direct_write(struct bio *bio) |
4b46fce2 JB |
7901 | { |
7902 | struct btrfs_dio_private *dip = bio->bi_private; | |
7903 | struct inode *inode = dip->inode; | |
7904 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b46fce2 | 7905 | struct btrfs_ordered_extent *ordered = NULL; |
163cf09c CM |
7906 | u64 ordered_offset = dip->logical_offset; |
7907 | u64 ordered_bytes = dip->bytes; | |
9be3395b | 7908 | struct bio *dio_bio; |
4b46fce2 JB |
7909 | int ret; |
7910 | ||
163cf09c CM |
7911 | again: |
7912 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
7913 | &ordered_offset, | |
4246a0b6 CH |
7914 | ordered_bytes, |
7915 | !bio->bi_error); | |
4b46fce2 | 7916 | if (!ret) |
163cf09c | 7917 | goto out_test; |
4b46fce2 | 7918 | |
9e0af237 LB |
7919 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
7920 | finish_ordered_fn, NULL, NULL); | |
fccb5d86 QW |
7921 | btrfs_queue_work(root->fs_info->endio_write_workers, |
7922 | &ordered->work); | |
163cf09c CM |
7923 | out_test: |
7924 | /* | |
7925 | * our bio might span multiple ordered extents. If we haven't | |
7926 | * completed the accounting for the whole dio, go back and try again | |
7927 | */ | |
7928 | if (ordered_offset < dip->logical_offset + dip->bytes) { | |
7929 | ordered_bytes = dip->logical_offset + dip->bytes - | |
7930 | ordered_offset; | |
5fd02043 | 7931 | ordered = NULL; |
163cf09c CM |
7932 | goto again; |
7933 | } | |
9be3395b | 7934 | dio_bio = dip->dio_bio; |
4b46fce2 | 7935 | |
4b46fce2 | 7936 | kfree(dip); |
c0da7aa1 | 7937 | |
4246a0b6 | 7938 | dio_end_io(dio_bio, bio->bi_error); |
9be3395b | 7939 | bio_put(bio); |
4b46fce2 JB |
7940 | } |
7941 | ||
eaf25d93 CM |
7942 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
7943 | struct bio *bio, int mirror_num, | |
7944 | unsigned long bio_flags, u64 offset) | |
7945 | { | |
7946 | int ret; | |
7947 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7948 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); | |
79787eaa | 7949 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
7950 | return 0; |
7951 | } | |
7952 | ||
4246a0b6 | 7953 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
7954 | { |
7955 | struct btrfs_dio_private *dip = bio->bi_private; | |
4246a0b6 | 7956 | int err = bio->bi_error; |
e65e1535 | 7957 | |
8b110e39 MX |
7958 | if (err) |
7959 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
7960 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", | |
7961 | btrfs_ino(dip->inode), bio->bi_rw, | |
7962 | (unsigned long long)bio->bi_iter.bi_sector, | |
7963 | bio->bi_iter.bi_size, err); | |
7964 | ||
7965 | if (dip->subio_endio) | |
7966 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
7967 | |
7968 | if (err) { | |
e65e1535 MX |
7969 | dip->errors = 1; |
7970 | ||
7971 | /* | |
7972 | * before atomic variable goto zero, we must make sure | |
7973 | * dip->errors is perceived to be set. | |
7974 | */ | |
4e857c58 | 7975 | smp_mb__before_atomic(); |
e65e1535 MX |
7976 | } |
7977 | ||
7978 | /* if there are more bios still pending for this dio, just exit */ | |
7979 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
7980 | goto out; | |
7981 | ||
9be3395b | 7982 | if (dip->errors) { |
e65e1535 | 7983 | bio_io_error(dip->orig_bio); |
9be3395b | 7984 | } else { |
4246a0b6 CH |
7985 | dip->dio_bio->bi_error = 0; |
7986 | bio_endio(dip->orig_bio); | |
e65e1535 MX |
7987 | } |
7988 | out: | |
7989 | bio_put(bio); | |
7990 | } | |
7991 | ||
7992 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, | |
7993 | u64 first_sector, gfp_t gfp_flags) | |
7994 | { | |
da2f0f74 | 7995 | struct bio *bio; |
22365979 | 7996 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
da2f0f74 CM |
7997 | if (bio) |
7998 | bio_associate_current(bio); | |
7999 | return bio; | |
e65e1535 MX |
8000 | } |
8001 | ||
c1dc0896 MX |
8002 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
8003 | struct inode *inode, | |
8004 | struct btrfs_dio_private *dip, | |
8005 | struct bio *bio, | |
8006 | u64 file_offset) | |
8007 | { | |
8008 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8009 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
8010 | int ret; | |
8011 | ||
8012 | /* | |
8013 | * We load all the csum data we need when we submit | |
8014 | * the first bio to reduce the csum tree search and | |
8015 | * contention. | |
8016 | */ | |
8017 | if (dip->logical_offset == file_offset) { | |
8018 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, | |
8019 | file_offset); | |
8020 | if (ret) | |
8021 | return ret; | |
8022 | } | |
8023 | ||
8024 | if (bio == dip->orig_bio) | |
8025 | return 0; | |
8026 | ||
8027 | file_offset -= dip->logical_offset; | |
8028 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8029 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8030 | ||
8031 | return 0; | |
8032 | } | |
8033 | ||
e65e1535 MX |
8034 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
8035 | int rw, u64 file_offset, int skip_sum, | |
c329861d | 8036 | int async_submit) |
e65e1535 | 8037 | { |
facc8a22 | 8038 | struct btrfs_dio_private *dip = bio->bi_private; |
e65e1535 MX |
8039 | int write = rw & REQ_WRITE; |
8040 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
8041 | int ret; | |
8042 | ||
b812ce28 JB |
8043 | if (async_submit) |
8044 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8045 | ||
e65e1535 | 8046 | bio_get(bio); |
5fd02043 JB |
8047 | |
8048 | if (!write) { | |
bfebd8b5 DS |
8049 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
8050 | BTRFS_WQ_ENDIO_DATA); | |
5fd02043 JB |
8051 | if (ret) |
8052 | goto err; | |
8053 | } | |
e65e1535 | 8054 | |
1ae39938 JB |
8055 | if (skip_sum) |
8056 | goto map; | |
8057 | ||
8058 | if (write && async_submit) { | |
e65e1535 MX |
8059 | ret = btrfs_wq_submit_bio(root->fs_info, |
8060 | inode, rw, bio, 0, 0, | |
8061 | file_offset, | |
8062 | __btrfs_submit_bio_start_direct_io, | |
8063 | __btrfs_submit_bio_done); | |
8064 | goto err; | |
1ae39938 JB |
8065 | } else if (write) { |
8066 | /* | |
8067 | * If we aren't doing async submit, calculate the csum of the | |
8068 | * bio now. | |
8069 | */ | |
8070 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); | |
8071 | if (ret) | |
8072 | goto err; | |
23ea8e5a | 8073 | } else { |
c1dc0896 MX |
8074 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
8075 | file_offset); | |
c2db1073 TI |
8076 | if (ret) |
8077 | goto err; | |
8078 | } | |
1ae39938 JB |
8079 | map: |
8080 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); | |
e65e1535 MX |
8081 | err: |
8082 | bio_put(bio); | |
8083 | return ret; | |
8084 | } | |
8085 | ||
8086 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, | |
8087 | int skip_sum) | |
8088 | { | |
8089 | struct inode *inode = dip->inode; | |
8090 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
e65e1535 MX |
8091 | struct bio *bio; |
8092 | struct bio *orig_bio = dip->orig_bio; | |
8093 | struct bio_vec *bvec = orig_bio->bi_io_vec; | |
4f024f37 | 8094 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 MX |
8095 | u64 file_offset = dip->logical_offset; |
8096 | u64 submit_len = 0; | |
8097 | u64 map_length; | |
8098 | int nr_pages = 0; | |
23ea8e5a | 8099 | int ret; |
1ae39938 | 8100 | int async_submit = 0; |
e65e1535 | 8101 | |
4f024f37 | 8102 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8103 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
e65e1535 | 8104 | &map_length, NULL, 0); |
7a5c3c9b | 8105 | if (ret) |
e65e1535 | 8106 | return -EIO; |
facc8a22 | 8107 | |
4f024f37 | 8108 | if (map_length >= orig_bio->bi_iter.bi_size) { |
02f57c7a | 8109 | bio = orig_bio; |
c1dc0896 | 8110 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8111 | goto submit; |
8112 | } | |
8113 | ||
53b381b3 | 8114 | /* async crcs make it difficult to collect full stripe writes. */ |
ffe2d203 | 8115 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8116 | async_submit = 0; |
8117 | else | |
8118 | async_submit = 1; | |
8119 | ||
02f57c7a JB |
8120 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
8121 | if (!bio) | |
8122 | return -ENOMEM; | |
7a5c3c9b | 8123 | |
02f57c7a JB |
8124 | bio->bi_private = dip; |
8125 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8126 | btrfs_io_bio(bio)->logical = file_offset; |
02f57c7a JB |
8127 | atomic_inc(&dip->pending_bios); |
8128 | ||
e65e1535 | 8129 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
ee39b432 | 8130 | if (map_length < submit_len + bvec->bv_len || |
e65e1535 | 8131 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
ee39b432 | 8132 | bvec->bv_offset) < bvec->bv_len) { |
e65e1535 MX |
8133 | /* |
8134 | * inc the count before we submit the bio so | |
8135 | * we know the end IO handler won't happen before | |
8136 | * we inc the count. Otherwise, the dip might get freed | |
8137 | * before we're done setting it up | |
8138 | */ | |
8139 | atomic_inc(&dip->pending_bios); | |
8140 | ret = __btrfs_submit_dio_bio(bio, inode, rw, | |
8141 | file_offset, skip_sum, | |
c329861d | 8142 | async_submit); |
e65e1535 MX |
8143 | if (ret) { |
8144 | bio_put(bio); | |
8145 | atomic_dec(&dip->pending_bios); | |
8146 | goto out_err; | |
8147 | } | |
8148 | ||
e65e1535 MX |
8149 | start_sector += submit_len >> 9; |
8150 | file_offset += submit_len; | |
8151 | ||
8152 | submit_len = 0; | |
8153 | nr_pages = 0; | |
8154 | ||
8155 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, | |
8156 | start_sector, GFP_NOFS); | |
8157 | if (!bio) | |
8158 | goto out_err; | |
8159 | bio->bi_private = dip; | |
8160 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8161 | btrfs_io_bio(bio)->logical = file_offset; |
e65e1535 | 8162 | |
4f024f37 | 8163 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8164 | ret = btrfs_map_block(root->fs_info, rw, |
3ec706c8 | 8165 | start_sector << 9, |
e65e1535 MX |
8166 | &map_length, NULL, 0); |
8167 | if (ret) { | |
8168 | bio_put(bio); | |
8169 | goto out_err; | |
8170 | } | |
8171 | } else { | |
8172 | submit_len += bvec->bv_len; | |
67871254 | 8173 | nr_pages++; |
e65e1535 MX |
8174 | bvec++; |
8175 | } | |
8176 | } | |
8177 | ||
02f57c7a | 8178 | submit: |
e65e1535 | 8179 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
c329861d | 8180 | async_submit); |
e65e1535 MX |
8181 | if (!ret) |
8182 | return 0; | |
8183 | ||
8184 | bio_put(bio); | |
8185 | out_err: | |
8186 | dip->errors = 1; | |
8187 | /* | |
8188 | * before atomic variable goto zero, we must | |
8189 | * make sure dip->errors is perceived to be set. | |
8190 | */ | |
4e857c58 | 8191 | smp_mb__before_atomic(); |
e65e1535 MX |
8192 | if (atomic_dec_and_test(&dip->pending_bios)) |
8193 | bio_io_error(dip->orig_bio); | |
8194 | ||
8195 | /* bio_end_io() will handle error, so we needn't return it */ | |
8196 | return 0; | |
8197 | } | |
8198 | ||
9be3395b CM |
8199 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
8200 | struct inode *inode, loff_t file_offset) | |
4b46fce2 | 8201 | { |
61de718f FM |
8202 | struct btrfs_dio_private *dip = NULL; |
8203 | struct bio *io_bio = NULL; | |
23ea8e5a | 8204 | struct btrfs_io_bio *btrfs_bio; |
4b46fce2 | 8205 | int skip_sum; |
7b6d91da | 8206 | int write = rw & REQ_WRITE; |
4b46fce2 JB |
8207 | int ret = 0; |
8208 | ||
8209 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8210 | ||
9be3395b | 8211 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
9be3395b CM |
8212 | if (!io_bio) { |
8213 | ret = -ENOMEM; | |
8214 | goto free_ordered; | |
8215 | } | |
8216 | ||
c1dc0896 | 8217 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8218 | if (!dip) { |
8219 | ret = -ENOMEM; | |
61de718f | 8220 | goto free_ordered; |
4b46fce2 | 8221 | } |
4b46fce2 | 8222 | |
9be3395b | 8223 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8224 | dip->inode = inode; |
8225 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8226 | dip->bytes = dio_bio->bi_iter.bi_size; |
8227 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
9be3395b | 8228 | io_bio->bi_private = dip; |
9be3395b CM |
8229 | dip->orig_bio = io_bio; |
8230 | dip->dio_bio = dio_bio; | |
e65e1535 | 8231 | atomic_set(&dip->pending_bios, 0); |
c1dc0896 MX |
8232 | btrfs_bio = btrfs_io_bio(io_bio); |
8233 | btrfs_bio->logical = file_offset; | |
4b46fce2 | 8234 | |
c1dc0896 | 8235 | if (write) { |
9be3395b | 8236 | io_bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8237 | } else { |
9be3395b | 8238 | io_bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8239 | dip->subio_endio = btrfs_subio_endio_read; |
8240 | } | |
4b46fce2 | 8241 | |
e65e1535 MX |
8242 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
8243 | if (!ret) | |
eaf25d93 | 8244 | return; |
9be3395b | 8245 | |
23ea8e5a MX |
8246 | if (btrfs_bio->end_io) |
8247 | btrfs_bio->end_io(btrfs_bio, ret); | |
9be3395b | 8248 | |
4b46fce2 JB |
8249 | free_ordered: |
8250 | /* | |
61de718f FM |
8251 | * If we arrived here it means either we failed to submit the dip |
8252 | * or we either failed to clone the dio_bio or failed to allocate the | |
8253 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8254 | * call bio_endio against our io_bio so that we get proper resource | |
8255 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8256 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8257 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8258 | */ |
61de718f | 8259 | if (io_bio && dip) { |
4246a0b6 CH |
8260 | io_bio->bi_error = -EIO; |
8261 | bio_endio(io_bio); | |
61de718f FM |
8262 | /* |
8263 | * The end io callbacks free our dip, do the final put on io_bio | |
8264 | * and all the cleanup and final put for dio_bio (through | |
8265 | * dio_end_io()). | |
8266 | */ | |
8267 | dip = NULL; | |
8268 | io_bio = NULL; | |
8269 | } else { | |
8270 | if (write) { | |
8271 | struct btrfs_ordered_extent *ordered; | |
8272 | ||
8273 | ordered = btrfs_lookup_ordered_extent(inode, | |
8274 | file_offset); | |
8275 | set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); | |
8276 | /* | |
8277 | * Decrements our ref on the ordered extent and removes | |
8278 | * the ordered extent from the inode's ordered tree, | |
8279 | * doing all the proper resource cleanup such as for the | |
8280 | * reserved space and waking up any waiters for this | |
8281 | * ordered extent (through btrfs_remove_ordered_extent). | |
8282 | */ | |
8283 | btrfs_finish_ordered_io(ordered); | |
8284 | } else { | |
8285 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, | |
8286 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
8287 | } | |
4246a0b6 | 8288 | dio_bio->bi_error = -EIO; |
61de718f FM |
8289 | /* |
8290 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8291 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8292 | */ | |
8293 | dio_end_io(dio_bio, ret); | |
4b46fce2 | 8294 | } |
61de718f FM |
8295 | if (io_bio) |
8296 | bio_put(io_bio); | |
8297 | kfree(dip); | |
4b46fce2 JB |
8298 | } |
8299 | ||
6f673763 | 8300 | static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb, |
28060d5d | 8301 | const struct iov_iter *iter, loff_t offset) |
5a5f79b5 CM |
8302 | { |
8303 | int seg; | |
a1b75f7d | 8304 | int i; |
5a5f79b5 CM |
8305 | unsigned blocksize_mask = root->sectorsize - 1; |
8306 | ssize_t retval = -EINVAL; | |
5a5f79b5 CM |
8307 | |
8308 | if (offset & blocksize_mask) | |
8309 | goto out; | |
8310 | ||
28060d5d AV |
8311 | if (iov_iter_alignment(iter) & blocksize_mask) |
8312 | goto out; | |
a1b75f7d | 8313 | |
28060d5d | 8314 | /* If this is a write we don't need to check anymore */ |
6f673763 | 8315 | if (iov_iter_rw(iter) == WRITE) |
28060d5d AV |
8316 | return 0; |
8317 | /* | |
8318 | * Check to make sure we don't have duplicate iov_base's in this | |
8319 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8320 | * when reading back. | |
8321 | */ | |
8322 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8323 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8324 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8325 | goto out; |
8326 | } | |
5a5f79b5 CM |
8327 | } |
8328 | retval = 0; | |
8329 | out: | |
8330 | return retval; | |
8331 | } | |
eb838e73 | 8332 | |
22c6186e OS |
8333 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, |
8334 | loff_t offset) | |
16432985 | 8335 | { |
4b46fce2 JB |
8336 | struct file *file = iocb->ki_filp; |
8337 | struct inode *inode = file->f_mapping->host; | |
50745b0a | 8338 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8339 | struct btrfs_dio_data dio_data = { 0 }; | |
0934856d | 8340 | size_t count = 0; |
2e60a51e | 8341 | int flags = 0; |
38851cc1 MX |
8342 | bool wakeup = true; |
8343 | bool relock = false; | |
0934856d | 8344 | ssize_t ret; |
4b46fce2 | 8345 | |
6f673763 | 8346 | if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset)) |
5a5f79b5 | 8347 | return 0; |
3f7c579c | 8348 | |
fe0f07d0 | 8349 | inode_dio_begin(inode); |
4e857c58 | 8350 | smp_mb__after_atomic(); |
38851cc1 | 8351 | |
0e267c44 | 8352 | /* |
41bd9ca4 MX |
8353 | * The generic stuff only does filemap_write_and_wait_range, which |
8354 | * isn't enough if we've written compressed pages to this area, so | |
8355 | * we need to flush the dirty pages again to make absolutely sure | |
8356 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8357 | */ |
a6cbcd4a | 8358 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8359 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8360 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8361 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8362 | offset + count - 1); | |
0e267c44 | 8363 | |
6f673763 | 8364 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8365 | /* |
8366 | * If the write DIO is beyond the EOF, we need update | |
8367 | * the isize, but it is protected by i_mutex. So we can | |
8368 | * not unlock the i_mutex at this case. | |
8369 | */ | |
8370 | if (offset + count <= inode->i_size) { | |
8371 | mutex_unlock(&inode->i_mutex); | |
8372 | relock = true; | |
8373 | } | |
0934856d MX |
8374 | ret = btrfs_delalloc_reserve_space(inode, count); |
8375 | if (ret) | |
38851cc1 | 8376 | goto out; |
50745b0a | 8377 | dio_data.outstanding_extents = div64_u64(count + |
e1cbbfa5 JB |
8378 | BTRFS_MAX_EXTENT_SIZE - 1, |
8379 | BTRFS_MAX_EXTENT_SIZE); | |
8380 | ||
8381 | /* | |
8382 | * We need to know how many extents we reserved so that we can | |
8383 | * do the accounting properly if we go over the number we | |
8384 | * originally calculated. Abuse current->journal_info for this. | |
8385 | */ | |
50745b0a | 8386 | dio_data.reserve = round_up(count, root->sectorsize); |
8387 | current->journal_info = &dio_data; | |
ee39b432 DS |
8388 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8389 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8390 | inode_dio_end(inode); |
38851cc1 MX |
8391 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8392 | wakeup = false; | |
0934856d MX |
8393 | } |
8394 | ||
17f8c842 OS |
8395 | ret = __blockdev_direct_IO(iocb, inode, |
8396 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, | |
8397 | iter, offset, btrfs_get_blocks_direct, NULL, | |
8398 | btrfs_submit_direct, flags); | |
6f673763 | 8399 | if (iov_iter_rw(iter) == WRITE) { |
e1cbbfa5 | 8400 | current->journal_info = NULL; |
ddba1bfc | 8401 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8402 | if (dio_data.reserve) |
8403 | btrfs_delalloc_release_space(inode, | |
8404 | dio_data.reserve); | |
ddba1bfc | 8405 | } else if (ret >= 0 && (size_t)ret < count) |
0934856d MX |
8406 | btrfs_delalloc_release_space(inode, |
8407 | count - (size_t)ret); | |
0934856d | 8408 | } |
38851cc1 | 8409 | out: |
2e60a51e | 8410 | if (wakeup) |
fe0f07d0 | 8411 | inode_dio_end(inode); |
38851cc1 MX |
8412 | if (relock) |
8413 | mutex_lock(&inode->i_mutex); | |
0934856d MX |
8414 | |
8415 | return ret; | |
16432985 CM |
8416 | } |
8417 | ||
05dadc09 TI |
8418 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8419 | ||
1506fcc8 YS |
8420 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8421 | __u64 start, __u64 len) | |
8422 | { | |
05dadc09 TI |
8423 | int ret; |
8424 | ||
8425 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8426 | if (ret) | |
8427 | return ret; | |
8428 | ||
ec29ed5b | 8429 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8430 | } |
8431 | ||
a52d9a80 | 8432 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8433 | { |
d1310b2e CM |
8434 | struct extent_io_tree *tree; |
8435 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8436 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8437 | } |
1832a6d5 | 8438 | |
a52d9a80 | 8439 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8440 | { |
d1310b2e | 8441 | struct extent_io_tree *tree; |
b888db2b CM |
8442 | |
8443 | ||
8444 | if (current->flags & PF_MEMALLOC) { | |
8445 | redirty_page_for_writepage(wbc, page); | |
8446 | unlock_page(page); | |
8447 | return 0; | |
8448 | } | |
d1310b2e | 8449 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
a52d9a80 | 8450 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
9ebefb18 CM |
8451 | } |
8452 | ||
48a3b636 ES |
8453 | static int btrfs_writepages(struct address_space *mapping, |
8454 | struct writeback_control *wbc) | |
b293f02e | 8455 | { |
d1310b2e | 8456 | struct extent_io_tree *tree; |
771ed689 | 8457 | |
d1310b2e | 8458 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8459 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8460 | } | |
8461 | ||
3ab2fb5a CM |
8462 | static int |
8463 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8464 | struct list_head *pages, unsigned nr_pages) | |
8465 | { | |
d1310b2e CM |
8466 | struct extent_io_tree *tree; |
8467 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8468 | return extent_readpages(tree, mapping, pages, nr_pages, |
8469 | btrfs_get_extent); | |
8470 | } | |
e6dcd2dc | 8471 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8472 | { |
d1310b2e CM |
8473 | struct extent_io_tree *tree; |
8474 | struct extent_map_tree *map; | |
a52d9a80 | 8475 | int ret; |
8c2383c3 | 8476 | |
d1310b2e CM |
8477 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8478 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8479 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8480 | if (ret == 1) { |
8481 | ClearPagePrivate(page); | |
8482 | set_page_private(page, 0); | |
8483 | page_cache_release(page); | |
39279cc3 | 8484 | } |
a52d9a80 | 8485 | return ret; |
39279cc3 CM |
8486 | } |
8487 | ||
e6dcd2dc CM |
8488 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8489 | { | |
98509cfc CM |
8490 | if (PageWriteback(page) || PageDirty(page)) |
8491 | return 0; | |
b335b003 | 8492 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
e6dcd2dc CM |
8493 | } |
8494 | ||
d47992f8 LC |
8495 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8496 | unsigned int length) | |
39279cc3 | 8497 | { |
5fd02043 | 8498 | struct inode *inode = page->mapping->host; |
d1310b2e | 8499 | struct extent_io_tree *tree; |
e6dcd2dc | 8500 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8501 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8502 | u64 page_start = page_offset(page); |
8503 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; | |
131e404a | 8504 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8505 | |
8b62b72b CM |
8506 | /* |
8507 | * we have the page locked, so new writeback can't start, | |
8508 | * and the dirty bit won't be cleared while we are here. | |
8509 | * | |
8510 | * Wait for IO on this page so that we can safely clear | |
8511 | * the PagePrivate2 bit and do ordered accounting | |
8512 | */ | |
e6dcd2dc | 8513 | wait_on_page_writeback(page); |
8b62b72b | 8514 | |
5fd02043 | 8515 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8516 | if (offset) { |
8517 | btrfs_releasepage(page, GFP_NOFS); | |
8518 | return; | |
8519 | } | |
131e404a FDBM |
8520 | |
8521 | if (!inode_evicting) | |
8522 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); | |
8523 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
e6dcd2dc | 8524 | if (ordered) { |
eb84ae03 CM |
8525 | /* |
8526 | * IO on this page will never be started, so we need | |
8527 | * to account for any ordered extents now | |
8528 | */ | |
131e404a FDBM |
8529 | if (!inode_evicting) |
8530 | clear_extent_bit(tree, page_start, page_end, | |
8531 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
8532 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | | |
8533 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8534 | GFP_NOFS); | |
8b62b72b CM |
8535 | /* |
8536 | * whoever cleared the private bit is responsible | |
8537 | * for the finish_ordered_io | |
8538 | */ | |
77cef2ec JB |
8539 | if (TestClearPagePrivate2(page)) { |
8540 | struct btrfs_ordered_inode_tree *tree; | |
8541 | u64 new_len; | |
8542 | ||
8543 | tree = &BTRFS_I(inode)->ordered_tree; | |
8544 | ||
8545 | spin_lock_irq(&tree->lock); | |
8546 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
8547 | new_len = page_start - ordered->file_offset; | |
8548 | if (new_len < ordered->truncated_len) | |
8549 | ordered->truncated_len = new_len; | |
8550 | spin_unlock_irq(&tree->lock); | |
8551 | ||
8552 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
8553 | page_start, | |
8554 | PAGE_CACHE_SIZE, 1)) | |
8555 | btrfs_finish_ordered_io(ordered); | |
8b62b72b | 8556 | } |
e6dcd2dc | 8557 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8558 | if (!inode_evicting) { |
8559 | cached_state = NULL; | |
8560 | lock_extent_bits(tree, page_start, page_end, 0, | |
8561 | &cached_state); | |
8562 | } | |
8563 | } | |
8564 | ||
8565 | if (!inode_evicting) { | |
8566 | clear_extent_bit(tree, page_start, page_end, | |
8567 | EXTENT_LOCKED | EXTENT_DIRTY | | |
8568 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
8569 | EXTENT_DEFRAG, 1, 1, | |
8570 | &cached_state, GFP_NOFS); | |
8571 | ||
8572 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8573 | } |
e6dcd2dc | 8574 | |
4a096752 | 8575 | ClearPageChecked(page); |
9ad6b7bc | 8576 | if (PagePrivate(page)) { |
9ad6b7bc CM |
8577 | ClearPagePrivate(page); |
8578 | set_page_private(page, 0); | |
8579 | page_cache_release(page); | |
8580 | } | |
39279cc3 CM |
8581 | } |
8582 | ||
9ebefb18 CM |
8583 | /* |
8584 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8585 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8586 | * be careful to check for EOF conditions here. We set the page up correctly | |
8587 | * for a written page which means we get ENOSPC checking when writing into | |
8588 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8589 | * support these features. | |
8590 | * | |
8591 | * We are not allowed to take the i_mutex here so we have to play games to | |
8592 | * protect against truncate races as the page could now be beyond EOF. Because | |
8593 | * vmtruncate() writes the inode size before removing pages, once we have the | |
8594 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
8595 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
8596 | * unlock the page. | |
8597 | */ | |
c2ec175c | 8598 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
9ebefb18 | 8599 | { |
c2ec175c | 8600 | struct page *page = vmf->page; |
496ad9aa | 8601 | struct inode *inode = file_inode(vma->vm_file); |
1832a6d5 | 8602 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
8603 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8604 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8605 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8606 | char *kaddr; |
8607 | unsigned long zero_start; | |
9ebefb18 | 8608 | loff_t size; |
1832a6d5 | 8609 | int ret; |
9998eb70 | 8610 | int reserved = 0; |
a52d9a80 | 8611 | u64 page_start; |
e6dcd2dc | 8612 | u64 page_end; |
9ebefb18 | 8613 | |
b2b5ef5c | 8614 | sb_start_pagefault(inode->i_sb); |
0ca1f7ce | 8615 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
9998eb70 | 8616 | if (!ret) { |
e41f941a | 8617 | ret = file_update_time(vma->vm_file); |
9998eb70 CM |
8618 | reserved = 1; |
8619 | } | |
56a76f82 NP |
8620 | if (ret) { |
8621 | if (ret == -ENOMEM) | |
8622 | ret = VM_FAULT_OOM; | |
8623 | else /* -ENOSPC, -EIO, etc */ | |
8624 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
8625 | if (reserved) |
8626 | goto out; | |
8627 | goto out_noreserve; | |
56a76f82 | 8628 | } |
1832a6d5 | 8629 | |
56a76f82 | 8630 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8631 | again: |
9ebefb18 | 8632 | lock_page(page); |
9ebefb18 | 8633 | size = i_size_read(inode); |
e6dcd2dc CM |
8634 | page_start = page_offset(page); |
8635 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
a52d9a80 | 8636 | |
9ebefb18 | 8637 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8638 | (page_start >= size)) { |
9ebefb18 CM |
8639 | /* page got truncated out from underneath us */ |
8640 | goto out_unlock; | |
8641 | } | |
e6dcd2dc CM |
8642 | wait_on_page_writeback(page); |
8643 | ||
d0082371 | 8644 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
8645 | set_page_extent_mapped(page); |
8646 | ||
eb84ae03 CM |
8647 | /* |
8648 | * we can't set the delalloc bits if there are pending ordered | |
8649 | * extents. Drop our locks and wait for them to finish | |
8650 | */ | |
e6dcd2dc CM |
8651 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
8652 | if (ordered) { | |
2ac55d41 JB |
8653 | unlock_extent_cached(io_tree, page_start, page_end, |
8654 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 8655 | unlock_page(page); |
eb84ae03 | 8656 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
8657 | btrfs_put_ordered_extent(ordered); |
8658 | goto again; | |
8659 | } | |
8660 | ||
fbf19087 JB |
8661 | /* |
8662 | * XXX - page_mkwrite gets called every time the page is dirtied, even | |
8663 | * if it was already dirty, so for space accounting reasons we need to | |
8664 | * clear any delalloc bits for the range we are fixing to save. There | |
8665 | * is probably a better way to do this, but for now keep consistent with | |
8666 | * prepare_pages in the normal write path. | |
8667 | */ | |
2ac55d41 | 8668 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
8669 | EXTENT_DIRTY | EXTENT_DELALLOC | |
8670 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 8671 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 8672 | |
2ac55d41 JB |
8673 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
8674 | &cached_state); | |
9ed74f2d | 8675 | if (ret) { |
2ac55d41 JB |
8676 | unlock_extent_cached(io_tree, page_start, page_end, |
8677 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
8678 | ret = VM_FAULT_SIGBUS; |
8679 | goto out_unlock; | |
8680 | } | |
e6dcd2dc | 8681 | ret = 0; |
9ebefb18 CM |
8682 | |
8683 | /* page is wholly or partially inside EOF */ | |
a52d9a80 | 8684 | if (page_start + PAGE_CACHE_SIZE > size) |
e6dcd2dc | 8685 | zero_start = size & ~PAGE_CACHE_MASK; |
9ebefb18 | 8686 | else |
e6dcd2dc | 8687 | zero_start = PAGE_CACHE_SIZE; |
9ebefb18 | 8688 | |
e6dcd2dc CM |
8689 | if (zero_start != PAGE_CACHE_SIZE) { |
8690 | kaddr = kmap(page); | |
8691 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); | |
8692 | flush_dcache_page(page); | |
8693 | kunmap(page); | |
8694 | } | |
247e743c | 8695 | ClearPageChecked(page); |
e6dcd2dc | 8696 | set_page_dirty(page); |
50a9b214 | 8697 | SetPageUptodate(page); |
5a3f23d5 | 8698 | |
257c62e1 CM |
8699 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
8700 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | |
46d8bc34 | 8701 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 8702 | |
2ac55d41 | 8703 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
8704 | |
8705 | out_unlock: | |
b2b5ef5c JK |
8706 | if (!ret) { |
8707 | sb_end_pagefault(inode->i_sb); | |
50a9b214 | 8708 | return VM_FAULT_LOCKED; |
b2b5ef5c | 8709 | } |
9ebefb18 | 8710 | unlock_page(page); |
1832a6d5 | 8711 | out: |
ec39e180 | 8712 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
9998eb70 | 8713 | out_noreserve: |
b2b5ef5c | 8714 | sb_end_pagefault(inode->i_sb); |
9ebefb18 CM |
8715 | return ret; |
8716 | } | |
8717 | ||
a41ad394 | 8718 | static int btrfs_truncate(struct inode *inode) |
39279cc3 CM |
8719 | { |
8720 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
fcb80c2a | 8721 | struct btrfs_block_rsv *rsv; |
a71754fc | 8722 | int ret = 0; |
3893e33b | 8723 | int err = 0; |
39279cc3 | 8724 | struct btrfs_trans_handle *trans; |
dbe674a9 | 8725 | u64 mask = root->sectorsize - 1; |
07127184 | 8726 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 | 8727 | |
0ef8b726 JB |
8728 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
8729 | (u64)-1); | |
8730 | if (ret) | |
8731 | return ret; | |
39279cc3 | 8732 | |
fcb80c2a JB |
8733 | /* |
8734 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have | |
8735 | * 3 things going on here | |
8736 | * | |
8737 | * 1) We need to reserve space for our orphan item and the space to | |
8738 | * delete our orphan item. Lord knows we don't want to have a dangling | |
8739 | * orphan item because we didn't reserve space to remove it. | |
8740 | * | |
8741 | * 2) We need to reserve space to update our inode. | |
8742 | * | |
8743 | * 3) We need to have something to cache all the space that is going to | |
8744 | * be free'd up by the truncate operation, but also have some slack | |
8745 | * space reserved in case it uses space during the truncate (thank you | |
8746 | * very much snapshotting). | |
8747 | * | |
8748 | * And we need these to all be seperate. The fact is we can use alot of | |
8749 | * space doing the truncate, and we have no earthly idea how much space | |
8750 | * we will use, so we need the truncate reservation to be seperate so it | |
8751 | * doesn't end up using space reserved for updating the inode or | |
8752 | * removing the orphan item. We also need to be able to stop the | |
8753 | * transaction and start a new one, which means we need to be able to | |
8754 | * update the inode several times, and we have no idea of knowing how | |
8755 | * many times that will be, so we can't just reserve 1 item for the | |
8756 | * entirety of the opration, so that has to be done seperately as well. | |
8757 | * Then there is the orphan item, which does indeed need to be held on | |
8758 | * to for the whole operation, and we need nobody to touch this reserved | |
8759 | * space except the orphan code. | |
8760 | * | |
8761 | * So that leaves us with | |
8762 | * | |
8763 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
8764 | * 2) rsv - for the truncate reservation, which we will steal from the | |
8765 | * transaction reservation. | |
8766 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
8767 | * updating the inode. | |
8768 | */ | |
66d8f3dd | 8769 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8770 | if (!rsv) |
8771 | return -ENOMEM; | |
4a338542 | 8772 | rsv->size = min_size; |
ca7e70f5 | 8773 | rsv->failfast = 1; |
f0cd846e | 8774 | |
907cbceb | 8775 | /* |
07127184 | 8776 | * 1 for the truncate slack space |
907cbceb JB |
8777 | * 1 for updating the inode. |
8778 | */ | |
f3fe820c | 8779 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
8780 | if (IS_ERR(trans)) { |
8781 | err = PTR_ERR(trans); | |
8782 | goto out; | |
8783 | } | |
f0cd846e | 8784 | |
907cbceb JB |
8785 | /* Migrate the slack space for the truncate to our reserve */ |
8786 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, | |
8787 | min_size); | |
fcb80c2a | 8788 | BUG_ON(ret); |
f0cd846e | 8789 | |
5dc562c5 JB |
8790 | /* |
8791 | * So if we truncate and then write and fsync we normally would just | |
8792 | * write the extents that changed, which is a problem if we need to | |
8793 | * first truncate that entire inode. So set this flag so we write out | |
8794 | * all of the extents in the inode to the sync log so we're completely | |
8795 | * safe. | |
8796 | */ | |
8797 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 8798 | trans->block_rsv = rsv; |
907cbceb | 8799 | |
8082510e YZ |
8800 | while (1) { |
8801 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
8802 | inode->i_size, | |
8803 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 8804 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 8805 | err = ret; |
8082510e | 8806 | break; |
3893e33b | 8807 | } |
39279cc3 | 8808 | |
fcb80c2a | 8809 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e | 8810 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
8811 | if (ret) { |
8812 | err = ret; | |
8813 | break; | |
8814 | } | |
ca7e70f5 | 8815 | |
8082510e | 8816 | btrfs_end_transaction(trans, root); |
b53d3f5d | 8817 | btrfs_btree_balance_dirty(root); |
ca7e70f5 JB |
8818 | |
8819 | trans = btrfs_start_transaction(root, 2); | |
8820 | if (IS_ERR(trans)) { | |
8821 | ret = err = PTR_ERR(trans); | |
8822 | trans = NULL; | |
8823 | break; | |
8824 | } | |
8825 | ||
8826 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, | |
8827 | rsv, min_size); | |
8828 | BUG_ON(ret); /* shouldn't happen */ | |
8829 | trans->block_rsv = rsv; | |
8082510e YZ |
8830 | } |
8831 | ||
8832 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 8833 | trans->block_rsv = root->orphan_block_rsv; |
8082510e | 8834 | ret = btrfs_orphan_del(trans, inode); |
3893e33b JB |
8835 | if (ret) |
8836 | err = ret; | |
8082510e YZ |
8837 | } |
8838 | ||
917c16b2 CM |
8839 | if (trans) { |
8840 | trans->block_rsv = &root->fs_info->trans_block_rsv; | |
8841 | ret = btrfs_update_inode(trans, root, inode); | |
8842 | if (ret && !err) | |
8843 | err = ret; | |
7b128766 | 8844 | |
7ad85bb7 | 8845 | ret = btrfs_end_transaction(trans, root); |
b53d3f5d | 8846 | btrfs_btree_balance_dirty(root); |
917c16b2 | 8847 | } |
fcb80c2a JB |
8848 | |
8849 | out: | |
8850 | btrfs_free_block_rsv(root, rsv); | |
8851 | ||
3893e33b JB |
8852 | if (ret && !err) |
8853 | err = ret; | |
a41ad394 | 8854 | |
3893e33b | 8855 | return err; |
39279cc3 CM |
8856 | } |
8857 | ||
d352ac68 CM |
8858 | /* |
8859 | * create a new subvolume directory/inode (helper for the ioctl). | |
8860 | */ | |
d2fb3437 | 8861 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
8862 | struct btrfs_root *new_root, |
8863 | struct btrfs_root *parent_root, | |
8864 | u64 new_dirid) | |
39279cc3 | 8865 | { |
39279cc3 | 8866 | struct inode *inode; |
76dda93c | 8867 | int err; |
00e4e6b3 | 8868 | u64 index = 0; |
39279cc3 | 8869 | |
12fc9d09 FA |
8870 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
8871 | new_dirid, new_dirid, | |
8872 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
8873 | &index); | |
54aa1f4d | 8874 | if (IS_ERR(inode)) |
f46b5a66 | 8875 | return PTR_ERR(inode); |
39279cc3 CM |
8876 | inode->i_op = &btrfs_dir_inode_operations; |
8877 | inode->i_fop = &btrfs_dir_file_operations; | |
8878 | ||
bfe86848 | 8879 | set_nlink(inode, 1); |
dbe674a9 | 8880 | btrfs_i_size_write(inode, 0); |
b0d5d10f | 8881 | unlock_new_inode(inode); |
3b96362c | 8882 | |
63541927 FDBM |
8883 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
8884 | if (err) | |
8885 | btrfs_err(new_root->fs_info, | |
351fd353 | 8886 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
8887 | new_root->root_key.objectid, err); |
8888 | ||
76dda93c | 8889 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 8890 | |
76dda93c | 8891 | iput(inode); |
ce598979 | 8892 | return err; |
39279cc3 CM |
8893 | } |
8894 | ||
39279cc3 CM |
8895 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
8896 | { | |
8897 | struct btrfs_inode *ei; | |
2ead6ae7 | 8898 | struct inode *inode; |
39279cc3 CM |
8899 | |
8900 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
8901 | if (!ei) | |
8902 | return NULL; | |
2ead6ae7 YZ |
8903 | |
8904 | ei->root = NULL; | |
2ead6ae7 | 8905 | ei->generation = 0; |
15ee9bc7 | 8906 | ei->last_trans = 0; |
257c62e1 | 8907 | ei->last_sub_trans = 0; |
e02119d5 | 8908 | ei->logged_trans = 0; |
2ead6ae7 | 8909 | ei->delalloc_bytes = 0; |
47059d93 | 8910 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
8911 | ei->disk_i_size = 0; |
8912 | ei->flags = 0; | |
7709cde3 | 8913 | ei->csum_bytes = 0; |
2ead6ae7 | 8914 | ei->index_cnt = (u64)-1; |
67de1176 | 8915 | ei->dir_index = 0; |
2ead6ae7 | 8916 | ei->last_unlink_trans = 0; |
46d8bc34 | 8917 | ei->last_log_commit = 0; |
2ead6ae7 | 8918 | |
9e0baf60 JB |
8919 | spin_lock_init(&ei->lock); |
8920 | ei->outstanding_extents = 0; | |
8921 | ei->reserved_extents = 0; | |
2ead6ae7 | 8922 | |
72ac3c0d | 8923 | ei->runtime_flags = 0; |
261507a0 | 8924 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 8925 | |
16cdcec7 MX |
8926 | ei->delayed_node = NULL; |
8927 | ||
9cc97d64 | 8928 | ei->i_otime.tv_sec = 0; |
8929 | ei->i_otime.tv_nsec = 0; | |
8930 | ||
2ead6ae7 | 8931 | inode = &ei->vfs_inode; |
a8067e02 | 8932 | extent_map_tree_init(&ei->extent_tree); |
f993c883 DS |
8933 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
8934 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); | |
0b32f4bb JB |
8935 | ei->io_tree.track_uptodate = 1; |
8936 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 8937 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 8938 | mutex_init(&ei->log_mutex); |
f248679e | 8939 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 8940 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 8941 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
2ead6ae7 YZ |
8942 | RB_CLEAR_NODE(&ei->rb_node); |
8943 | ||
8944 | return inode; | |
39279cc3 CM |
8945 | } |
8946 | ||
aaedb55b JB |
8947 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
8948 | void btrfs_test_destroy_inode(struct inode *inode) | |
8949 | { | |
8950 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
8951 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
8952 | } | |
8953 | #endif | |
8954 | ||
fa0d7e3d NP |
8955 | static void btrfs_i_callback(struct rcu_head *head) |
8956 | { | |
8957 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
8958 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
8959 | } | |
8960 | ||
39279cc3 CM |
8961 | void btrfs_destroy_inode(struct inode *inode) |
8962 | { | |
e6dcd2dc | 8963 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
8964 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8965 | ||
b3d9b7a3 | 8966 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 8967 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
8968 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
8969 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 JB |
8970 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
8971 | WARN_ON(BTRFS_I(inode)->csum_bytes); | |
47059d93 | 8972 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 8973 | |
a6dbd429 JB |
8974 | /* |
8975 | * This can happen where we create an inode, but somebody else also | |
8976 | * created the same inode and we need to destroy the one we already | |
8977 | * created. | |
8978 | */ | |
8979 | if (!root) | |
8980 | goto free; | |
8981 | ||
8a35d95f JB |
8982 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8983 | &BTRFS_I(inode)->runtime_flags)) { | |
c2cf52eb | 8984 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
c1c9ff7c | 8985 | btrfs_ino(inode)); |
8a35d95f | 8986 | atomic_dec(&root->orphan_inodes); |
7b128766 | 8987 | } |
7b128766 | 8988 | |
d397712b | 8989 | while (1) { |
e6dcd2dc CM |
8990 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
8991 | if (!ordered) | |
8992 | break; | |
8993 | else { | |
c2cf52eb | 8994 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
c1c9ff7c | 8995 | ordered->file_offset, ordered->len); |
e6dcd2dc CM |
8996 | btrfs_remove_ordered_extent(inode, ordered); |
8997 | btrfs_put_ordered_extent(ordered); | |
8998 | btrfs_put_ordered_extent(ordered); | |
8999 | } | |
9000 | } | |
5d4f98a2 | 9001 | inode_tree_del(inode); |
5b21f2ed | 9002 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
a6dbd429 | 9003 | free: |
fa0d7e3d | 9004 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9005 | } |
9006 | ||
45321ac5 | 9007 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9008 | { |
9009 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9010 | |
6379ef9f NA |
9011 | if (root == NULL) |
9012 | return 1; | |
9013 | ||
fa6ac876 | 9014 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9015 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9016 | return 1; |
76dda93c | 9017 | else |
45321ac5 | 9018 | return generic_drop_inode(inode); |
76dda93c YZ |
9019 | } |
9020 | ||
0ee0fda0 | 9021 | static void init_once(void *foo) |
39279cc3 CM |
9022 | { |
9023 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9024 | ||
9025 | inode_init_once(&ei->vfs_inode); | |
9026 | } | |
9027 | ||
9028 | void btrfs_destroy_cachep(void) | |
9029 | { | |
8c0a8537 KS |
9030 | /* |
9031 | * Make sure all delayed rcu free inodes are flushed before we | |
9032 | * destroy cache. | |
9033 | */ | |
9034 | rcu_barrier(); | |
39279cc3 CM |
9035 | if (btrfs_inode_cachep) |
9036 | kmem_cache_destroy(btrfs_inode_cachep); | |
9037 | if (btrfs_trans_handle_cachep) | |
9038 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
9039 | if (btrfs_transaction_cachep) | |
9040 | kmem_cache_destroy(btrfs_transaction_cachep); | |
39279cc3 CM |
9041 | if (btrfs_path_cachep) |
9042 | kmem_cache_destroy(btrfs_path_cachep); | |
dc89e982 JB |
9043 | if (btrfs_free_space_cachep) |
9044 | kmem_cache_destroy(btrfs_free_space_cachep); | |
8ccf6f19 MX |
9045 | if (btrfs_delalloc_work_cachep) |
9046 | kmem_cache_destroy(btrfs_delalloc_work_cachep); | |
39279cc3 CM |
9047 | } |
9048 | ||
9049 | int btrfs_init_cachep(void) | |
9050 | { | |
837e1972 | 9051 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 CH |
9052 | sizeof(struct btrfs_inode), 0, |
9053 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); | |
39279cc3 CM |
9054 | if (!btrfs_inode_cachep) |
9055 | goto fail; | |
9601e3f6 | 9056 | |
837e1972 | 9057 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 CH |
9058 | sizeof(struct btrfs_trans_handle), 0, |
9059 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
9060 | if (!btrfs_trans_handle_cachep) |
9061 | goto fail; | |
9601e3f6 | 9062 | |
837e1972 | 9063 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
9601e3f6 CH |
9064 | sizeof(struct btrfs_transaction), 0, |
9065 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
9066 | if (!btrfs_transaction_cachep) |
9067 | goto fail; | |
9601e3f6 | 9068 | |
837e1972 | 9069 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 CH |
9070 | sizeof(struct btrfs_path), 0, |
9071 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
9072 | if (!btrfs_path_cachep) |
9073 | goto fail; | |
9601e3f6 | 9074 | |
837e1972 | 9075 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 JB |
9076 | sizeof(struct btrfs_free_space), 0, |
9077 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
9078 | if (!btrfs_free_space_cachep) | |
9079 | goto fail; | |
9080 | ||
8ccf6f19 MX |
9081 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
9082 | sizeof(struct btrfs_delalloc_work), 0, | |
9083 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, | |
9084 | NULL); | |
9085 | if (!btrfs_delalloc_work_cachep) | |
9086 | goto fail; | |
9087 | ||
39279cc3 CM |
9088 | return 0; |
9089 | fail: | |
9090 | btrfs_destroy_cachep(); | |
9091 | return -ENOMEM; | |
9092 | } | |
9093 | ||
9094 | static int btrfs_getattr(struct vfsmount *mnt, | |
9095 | struct dentry *dentry, struct kstat *stat) | |
9096 | { | |
df0af1a5 | 9097 | u64 delalloc_bytes; |
2b0143b5 | 9098 | struct inode *inode = d_inode(dentry); |
fadc0d8b DS |
9099 | u32 blocksize = inode->i_sb->s_blocksize; |
9100 | ||
39279cc3 | 9101 | generic_fillattr(inode, stat); |
0ee5dc67 | 9102 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
d6667462 | 9103 | stat->blksize = PAGE_CACHE_SIZE; |
df0af1a5 MX |
9104 | |
9105 | spin_lock(&BTRFS_I(inode)->lock); | |
9106 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; | |
9107 | spin_unlock(&BTRFS_I(inode)->lock); | |
fadc0d8b | 9108 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9109 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9110 | return 0; |
9111 | } | |
9112 | ||
d397712b CM |
9113 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
9114 | struct inode *new_dir, struct dentry *new_dentry) | |
39279cc3 CM |
9115 | { |
9116 | struct btrfs_trans_handle *trans; | |
9117 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
4df27c4d | 9118 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9119 | struct inode *new_inode = d_inode(new_dentry); |
9120 | struct inode *old_inode = d_inode(old_dentry); | |
39279cc3 | 9121 | struct timespec ctime = CURRENT_TIME; |
00e4e6b3 | 9122 | u64 index = 0; |
4df27c4d | 9123 | u64 root_objectid; |
39279cc3 | 9124 | int ret; |
33345d01 | 9125 | u64 old_ino = btrfs_ino(old_inode); |
39279cc3 | 9126 | |
33345d01 | 9127 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9128 | return -EPERM; |
9129 | ||
4df27c4d | 9130 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9131 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9132 | return -EXDEV; |
9133 | ||
33345d01 LZ |
9134 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
9135 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) | |
39279cc3 | 9136 | return -ENOTEMPTY; |
5f39d397 | 9137 | |
4df27c4d YZ |
9138 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9139 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9140 | return -ENOTEMPTY; | |
9c52057c CM |
9141 | |
9142 | ||
9143 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9144 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9145 | new_dentry->d_name.name, |
9146 | new_dentry->d_name.len); | |
9147 | ||
9148 | if (ret) { | |
9149 | if (ret == -EEXIST) { | |
9150 | /* we shouldn't get | |
9151 | * eexist without a new_inode */ | |
fae7f21c | 9152 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9153 | return ret; |
9154 | } | |
9155 | } else { | |
9156 | /* maybe -EOVERFLOW */ | |
9157 | return ret; | |
9158 | } | |
9159 | } | |
9160 | ret = 0; | |
9161 | ||
5a3f23d5 | 9162 | /* |
8d875f95 CM |
9163 | * we're using rename to replace one file with another. Start IO on it |
9164 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9165 | */ |
8d875f95 | 9166 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9167 | filemap_flush(old_inode->i_mapping); |
9168 | ||
76dda93c | 9169 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9170 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9171 | down_read(&root->fs_info->subvol_sem); |
a22285a6 YZ |
9172 | /* |
9173 | * We want to reserve the absolute worst case amount of items. So if | |
9174 | * both inodes are subvols and we need to unlink them then that would | |
9175 | * require 4 item modifications, but if they are both normal inodes it | |
9176 | * would require 5 item modifications, so we'll assume their normal | |
9177 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items | |
9178 | * should cover the worst case number of items we'll modify. | |
9179 | */ | |
6e137ed3 | 9180 | trans = btrfs_start_transaction(root, 11); |
b44c59a8 JL |
9181 | if (IS_ERR(trans)) { |
9182 | ret = PTR_ERR(trans); | |
9183 | goto out_notrans; | |
9184 | } | |
76dda93c | 9185 | |
4df27c4d YZ |
9186 | if (dest != root) |
9187 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9188 | |
a5719521 YZ |
9189 | ret = btrfs_set_inode_index(new_dir, &index); |
9190 | if (ret) | |
9191 | goto out_fail; | |
5a3f23d5 | 9192 | |
67de1176 | 9193 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9194 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9195 | /* force full log commit if subvolume involved. */ |
995946dd | 9196 | btrfs_set_log_full_commit(root->fs_info, trans); |
4df27c4d | 9197 | } else { |
a5719521 YZ |
9198 | ret = btrfs_insert_inode_ref(trans, dest, |
9199 | new_dentry->d_name.name, | |
9200 | new_dentry->d_name.len, | |
33345d01 LZ |
9201 | old_ino, |
9202 | btrfs_ino(new_dir), index); | |
a5719521 YZ |
9203 | if (ret) |
9204 | goto out_fail; | |
4df27c4d YZ |
9205 | /* |
9206 | * this is an ugly little race, but the rename is required | |
9207 | * to make sure that if we crash, the inode is either at the | |
9208 | * old name or the new one. pinning the log transaction lets | |
9209 | * us make sure we don't allow a log commit to come in after | |
9210 | * we unlink the name but before we add the new name back in. | |
9211 | */ | |
9212 | btrfs_pin_log_trans(root); | |
9213 | } | |
5a3f23d5 | 9214 | |
0c4d2d95 JB |
9215 | inode_inc_iversion(old_dir); |
9216 | inode_inc_iversion(new_dir); | |
9217 | inode_inc_iversion(old_inode); | |
39279cc3 CM |
9218 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
9219 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9220 | old_inode->i_ctime = ctime; | |
5f39d397 | 9221 | |
12fcfd22 CM |
9222 | if (old_dentry->d_parent != new_dentry->d_parent) |
9223 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | |
9224 | ||
33345d01 | 9225 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
9226 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
9227 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
9228 | old_dentry->d_name.name, | |
9229 | old_dentry->d_name.len); | |
9230 | } else { | |
92986796 | 9231 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
2b0143b5 | 9232 | d_inode(old_dentry), |
92986796 AV |
9233 | old_dentry->d_name.name, |
9234 | old_dentry->d_name.len); | |
9235 | if (!ret) | |
9236 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9237 | } |
79787eaa JM |
9238 | if (ret) { |
9239 | btrfs_abort_transaction(trans, root, ret); | |
9240 | goto out_fail; | |
9241 | } | |
39279cc3 CM |
9242 | |
9243 | if (new_inode) { | |
0c4d2d95 | 9244 | inode_inc_iversion(new_inode); |
39279cc3 | 9245 | new_inode->i_ctime = CURRENT_TIME; |
33345d01 | 9246 | if (unlikely(btrfs_ino(new_inode) == |
4df27c4d YZ |
9247 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9248 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
9249 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9250 | root_objectid, | |
9251 | new_dentry->d_name.name, | |
9252 | new_dentry->d_name.len); | |
9253 | BUG_ON(new_inode->i_nlink == 0); | |
9254 | } else { | |
9255 | ret = btrfs_unlink_inode(trans, dest, new_dir, | |
2b0143b5 | 9256 | d_inode(new_dentry), |
4df27c4d YZ |
9257 | new_dentry->d_name.name, |
9258 | new_dentry->d_name.len); | |
9259 | } | |
4ef31a45 | 9260 | if (!ret && new_inode->i_nlink == 0) |
2b0143b5 | 9261 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
79787eaa JM |
9262 | if (ret) { |
9263 | btrfs_abort_transaction(trans, root, ret); | |
9264 | goto out_fail; | |
9265 | } | |
39279cc3 | 9266 | } |
aec7477b | 9267 | |
4df27c4d YZ |
9268 | ret = btrfs_add_link(trans, new_dir, old_inode, |
9269 | new_dentry->d_name.name, | |
a5719521 | 9270 | new_dentry->d_name.len, 0, index); |
79787eaa JM |
9271 | if (ret) { |
9272 | btrfs_abort_transaction(trans, root, ret); | |
9273 | goto out_fail; | |
9274 | } | |
39279cc3 | 9275 | |
67de1176 MX |
9276 | if (old_inode->i_nlink == 1) |
9277 | BTRFS_I(old_inode)->dir_index = index; | |
9278 | ||
33345d01 | 9279 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
10d9f309 | 9280 | struct dentry *parent = new_dentry->d_parent; |
6a912213 | 9281 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
4df27c4d YZ |
9282 | btrfs_end_log_trans(root); |
9283 | } | |
39279cc3 | 9284 | out_fail: |
7ad85bb7 | 9285 | btrfs_end_transaction(trans, root); |
b44c59a8 | 9286 | out_notrans: |
33345d01 | 9287 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9288 | up_read(&root->fs_info->subvol_sem); |
9ed74f2d | 9289 | |
39279cc3 CM |
9290 | return ret; |
9291 | } | |
9292 | ||
80ace85c MS |
9293 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9294 | struct inode *new_dir, struct dentry *new_dentry, | |
9295 | unsigned int flags) | |
9296 | { | |
9297 | if (flags & ~RENAME_NOREPLACE) | |
9298 | return -EINVAL; | |
9299 | ||
9300 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); | |
9301 | } | |
9302 | ||
8ccf6f19 MX |
9303 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9304 | { | |
9305 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9306 | struct inode *inode; |
8ccf6f19 MX |
9307 | |
9308 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9309 | work); | |
9f23e289 JB |
9310 | inode = delalloc_work->inode; |
9311 | if (delalloc_work->wait) { | |
9312 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
9313 | } else { | |
9314 | filemap_flush(inode->i_mapping); | |
9315 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9316 | &BTRFS_I(inode)->runtime_flags)) | |
9317 | filemap_flush(inode->i_mapping); | |
9318 | } | |
8ccf6f19 MX |
9319 | |
9320 | if (delalloc_work->delay_iput) | |
9f23e289 | 9321 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 9322 | else |
9f23e289 | 9323 | iput(inode); |
8ccf6f19 MX |
9324 | complete(&delalloc_work->completion); |
9325 | } | |
9326 | ||
9327 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
9328 | int wait, int delay_iput) | |
9329 | { | |
9330 | struct btrfs_delalloc_work *work; | |
9331 | ||
9332 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); | |
9333 | if (!work) | |
9334 | return NULL; | |
9335 | ||
9336 | init_completion(&work->completion); | |
9337 | INIT_LIST_HEAD(&work->list); | |
9338 | work->inode = inode; | |
9339 | work->wait = wait; | |
9340 | work->delay_iput = delay_iput; | |
9e0af237 LB |
9341 | WARN_ON_ONCE(!inode); |
9342 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
9343 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
9344 | |
9345 | return work; | |
9346 | } | |
9347 | ||
9348 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
9349 | { | |
9350 | wait_for_completion(&work->completion); | |
9351 | kmem_cache_free(btrfs_delalloc_work_cachep, work); | |
9352 | } | |
9353 | ||
d352ac68 CM |
9354 | /* |
9355 | * some fairly slow code that needs optimization. This walks the list | |
9356 | * of all the inodes with pending delalloc and forces them to disk. | |
9357 | */ | |
6c255e67 MX |
9358 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
9359 | int nr) | |
ea8c2819 | 9360 | { |
ea8c2819 | 9361 | struct btrfs_inode *binode; |
5b21f2ed | 9362 | struct inode *inode; |
8ccf6f19 MX |
9363 | struct btrfs_delalloc_work *work, *next; |
9364 | struct list_head works; | |
1eafa6c7 | 9365 | struct list_head splice; |
8ccf6f19 | 9366 | int ret = 0; |
ea8c2819 | 9367 | |
8ccf6f19 | 9368 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9369 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9370 | |
573bfb72 | 9371 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9372 | spin_lock(&root->delalloc_lock); |
9373 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9374 | while (!list_empty(&splice)) { |
9375 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9376 | delalloc_inodes); |
1eafa6c7 | 9377 | |
eb73c1b7 MX |
9378 | list_move_tail(&binode->delalloc_inodes, |
9379 | &root->delalloc_inodes); | |
5b21f2ed | 9380 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9381 | if (!inode) { |
eb73c1b7 | 9382 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9383 | continue; |
df0af1a5 | 9384 | } |
eb73c1b7 | 9385 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 MX |
9386 | |
9387 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); | |
5d99a998 | 9388 | if (!work) { |
f4ab9ea7 JB |
9389 | if (delay_iput) |
9390 | btrfs_add_delayed_iput(inode); | |
9391 | else | |
9392 | iput(inode); | |
1eafa6c7 | 9393 | ret = -ENOMEM; |
a1ecaabb | 9394 | goto out; |
5b21f2ed | 9395 | } |
1eafa6c7 | 9396 | list_add_tail(&work->list, &works); |
a44903ab QW |
9397 | btrfs_queue_work(root->fs_info->flush_workers, |
9398 | &work->work); | |
6c255e67 MX |
9399 | ret++; |
9400 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 9401 | goto out; |
5b21f2ed | 9402 | cond_resched(); |
eb73c1b7 | 9403 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9404 | } |
eb73c1b7 | 9405 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9406 | |
a1ecaabb | 9407 | out: |
eb73c1b7 MX |
9408 | list_for_each_entry_safe(work, next, &works, list) { |
9409 | list_del_init(&work->list); | |
9410 | btrfs_wait_and_free_delalloc_work(work); | |
9411 | } | |
9412 | ||
9413 | if (!list_empty_careful(&splice)) { | |
9414 | spin_lock(&root->delalloc_lock); | |
9415 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9416 | spin_unlock(&root->delalloc_lock); | |
9417 | } | |
573bfb72 | 9418 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9419 | return ret; |
9420 | } | |
1eafa6c7 | 9421 | |
eb73c1b7 MX |
9422 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
9423 | { | |
9424 | int ret; | |
1eafa6c7 | 9425 | |
2c21b4d7 | 9426 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
eb73c1b7 MX |
9427 | return -EROFS; |
9428 | ||
6c255e67 MX |
9429 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
9430 | if (ret > 0) | |
9431 | ret = 0; | |
eb73c1b7 MX |
9432 | /* |
9433 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
9434 | * we have to make sure the IO is actually started and that |
9435 | * ordered extents get created before we return | |
9436 | */ | |
9437 | atomic_inc(&root->fs_info->async_submit_draining); | |
d397712b | 9438 | while (atomic_read(&root->fs_info->nr_async_submits) || |
771ed689 | 9439 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
8c8bee1d | 9440 | wait_event(root->fs_info->async_submit_wait, |
771ed689 CM |
9441 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
9442 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | |
8c8bee1d CM |
9443 | } |
9444 | atomic_dec(&root->fs_info->async_submit_draining); | |
eb73c1b7 MX |
9445 | return ret; |
9446 | } | |
9447 | ||
6c255e67 MX |
9448 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
9449 | int nr) | |
eb73c1b7 MX |
9450 | { |
9451 | struct btrfs_root *root; | |
9452 | struct list_head splice; | |
9453 | int ret; | |
9454 | ||
2c21b4d7 | 9455 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9456 | return -EROFS; |
9457 | ||
9458 | INIT_LIST_HEAD(&splice); | |
9459 | ||
573bfb72 | 9460 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9461 | spin_lock(&fs_info->delalloc_root_lock); |
9462 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 9463 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
9464 | root = list_first_entry(&splice, struct btrfs_root, |
9465 | delalloc_root); | |
9466 | root = btrfs_grab_fs_root(root); | |
9467 | BUG_ON(!root); | |
9468 | list_move_tail(&root->delalloc_root, | |
9469 | &fs_info->delalloc_roots); | |
9470 | spin_unlock(&fs_info->delalloc_root_lock); | |
9471 | ||
6c255e67 | 9472 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 9473 | btrfs_put_fs_root(root); |
6c255e67 | 9474 | if (ret < 0) |
eb73c1b7 MX |
9475 | goto out; |
9476 | ||
6c255e67 MX |
9477 | if (nr != -1) { |
9478 | nr -= ret; | |
9479 | WARN_ON(nr < 0); | |
9480 | } | |
eb73c1b7 | 9481 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9482 | } |
eb73c1b7 | 9483 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9484 | |
6c255e67 | 9485 | ret = 0; |
eb73c1b7 MX |
9486 | atomic_inc(&fs_info->async_submit_draining); |
9487 | while (atomic_read(&fs_info->nr_async_submits) || | |
9488 | atomic_read(&fs_info->async_delalloc_pages)) { | |
9489 | wait_event(fs_info->async_submit_wait, | |
9490 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
9491 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
9492 | } | |
9493 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 9494 | out: |
1eafa6c7 | 9495 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
9496 | spin_lock(&fs_info->delalloc_root_lock); |
9497 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9498 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9499 | } |
573bfb72 | 9500 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9501 | return ret; |
ea8c2819 CM |
9502 | } |
9503 | ||
39279cc3 CM |
9504 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
9505 | const char *symname) | |
9506 | { | |
9507 | struct btrfs_trans_handle *trans; | |
9508 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9509 | struct btrfs_path *path; | |
9510 | struct btrfs_key key; | |
1832a6d5 | 9511 | struct inode *inode = NULL; |
39279cc3 CM |
9512 | int err; |
9513 | int drop_inode = 0; | |
9514 | u64 objectid; | |
67871254 | 9515 | u64 index = 0; |
39279cc3 CM |
9516 | int name_len; |
9517 | int datasize; | |
5f39d397 | 9518 | unsigned long ptr; |
39279cc3 | 9519 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9520 | struct extent_buffer *leaf; |
39279cc3 | 9521 | |
f06becc4 | 9522 | name_len = strlen(symname); |
39279cc3 CM |
9523 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
9524 | return -ENAMETOOLONG; | |
1832a6d5 | 9525 | |
9ed74f2d JB |
9526 | /* |
9527 | * 2 items for inode item and ref | |
9528 | * 2 items for dir items | |
9529 | * 1 item for xattr if selinux is on | |
9530 | */ | |
a22285a6 YZ |
9531 | trans = btrfs_start_transaction(root, 5); |
9532 | if (IS_ERR(trans)) | |
9533 | return PTR_ERR(trans); | |
1832a6d5 | 9534 | |
581bb050 LZ |
9535 | err = btrfs_find_free_ino(root, &objectid); |
9536 | if (err) | |
9537 | goto out_unlock; | |
9538 | ||
aec7477b | 9539 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 9540 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 9541 | S_IFLNK|S_IRWXUGO, &index); |
7cf96da3 TI |
9542 | if (IS_ERR(inode)) { |
9543 | err = PTR_ERR(inode); | |
39279cc3 | 9544 | goto out_unlock; |
7cf96da3 | 9545 | } |
39279cc3 | 9546 | |
ad19db71 CS |
9547 | /* |
9548 | * If the active LSM wants to access the inode during | |
9549 | * d_instantiate it needs these. Smack checks to see | |
9550 | * if the filesystem supports xattrs by looking at the | |
9551 | * ops vector. | |
9552 | */ | |
9553 | inode->i_fop = &btrfs_file_operations; | |
9554 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 9555 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
9556 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9557 | ||
9558 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
9559 | if (err) | |
9560 | goto out_unlock_inode; | |
ad19db71 | 9561 | |
a1b075d2 | 9562 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 9563 | if (err) |
b0d5d10f | 9564 | goto out_unlock_inode; |
39279cc3 CM |
9565 | |
9566 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
9567 | if (!path) { |
9568 | err = -ENOMEM; | |
b0d5d10f | 9569 | goto out_unlock_inode; |
d8926bb3 | 9570 | } |
33345d01 | 9571 | key.objectid = btrfs_ino(inode); |
39279cc3 | 9572 | key.offset = 0; |
962a298f | 9573 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9574 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9575 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9576 | datasize); | |
54aa1f4d | 9577 | if (err) { |
b0839166 | 9578 | btrfs_free_path(path); |
b0d5d10f | 9579 | goto out_unlock_inode; |
54aa1f4d | 9580 | } |
5f39d397 CM |
9581 | leaf = path->nodes[0]; |
9582 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9583 | struct btrfs_file_extent_item); | |
9584 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9585 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9586 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9587 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9588 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9589 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9590 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9591 | ||
39279cc3 | 9592 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
9593 | write_extent_buffer(leaf, symname, ptr, name_len); |
9594 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 9595 | btrfs_free_path(path); |
5f39d397 | 9596 | |
39279cc3 CM |
9597 | inode->i_op = &btrfs_symlink_inode_operations; |
9598 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
d899e052 | 9599 | inode_set_bytes(inode, name_len); |
f06becc4 | 9600 | btrfs_i_size_write(inode, name_len); |
54aa1f4d | 9601 | err = btrfs_update_inode(trans, root, inode); |
b0d5d10f | 9602 | if (err) { |
54aa1f4d | 9603 | drop_inode = 1; |
b0d5d10f CM |
9604 | goto out_unlock_inode; |
9605 | } | |
9606 | ||
9607 | unlock_new_inode(inode); | |
9608 | d_instantiate(dentry, inode); | |
39279cc3 CM |
9609 | |
9610 | out_unlock: | |
7ad85bb7 | 9611 | btrfs_end_transaction(trans, root); |
39279cc3 CM |
9612 | if (drop_inode) { |
9613 | inode_dec_link_count(inode); | |
9614 | iput(inode); | |
9615 | } | |
b53d3f5d | 9616 | btrfs_btree_balance_dirty(root); |
39279cc3 | 9617 | return err; |
b0d5d10f CM |
9618 | |
9619 | out_unlock_inode: | |
9620 | drop_inode = 1; | |
9621 | unlock_new_inode(inode); | |
9622 | goto out_unlock; | |
39279cc3 | 9623 | } |
16432985 | 9624 | |
0af3d00b JB |
9625 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9626 | u64 start, u64 num_bytes, u64 min_size, | |
9627 | loff_t actual_len, u64 *alloc_hint, | |
9628 | struct btrfs_trans_handle *trans) | |
d899e052 | 9629 | { |
5dc562c5 JB |
9630 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
9631 | struct extent_map *em; | |
d899e052 YZ |
9632 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9633 | struct btrfs_key ins; | |
d899e052 | 9634 | u64 cur_offset = start; |
55a61d1d | 9635 | u64 i_size; |
154ea289 | 9636 | u64 cur_bytes; |
d899e052 | 9637 | int ret = 0; |
0af3d00b | 9638 | bool own_trans = true; |
d899e052 | 9639 | |
0af3d00b JB |
9640 | if (trans) |
9641 | own_trans = false; | |
d899e052 | 9642 | while (num_bytes > 0) { |
0af3d00b JB |
9643 | if (own_trans) { |
9644 | trans = btrfs_start_transaction(root, 3); | |
9645 | if (IS_ERR(trans)) { | |
9646 | ret = PTR_ERR(trans); | |
9647 | break; | |
9648 | } | |
5a303d5d YZ |
9649 | } |
9650 | ||
154ea289 CM |
9651 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
9652 | cur_bytes = max(cur_bytes, min_size); | |
00361589 | 9653 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
e570fd27 | 9654 | *alloc_hint, &ins, 1, 0); |
5a303d5d | 9655 | if (ret) { |
0af3d00b JB |
9656 | if (own_trans) |
9657 | btrfs_end_transaction(trans, root); | |
a22285a6 | 9658 | break; |
d899e052 | 9659 | } |
5a303d5d | 9660 | |
d899e052 YZ |
9661 | ret = insert_reserved_file_extent(trans, inode, |
9662 | cur_offset, ins.objectid, | |
9663 | ins.offset, ins.offset, | |
920bbbfb | 9664 | ins.offset, 0, 0, 0, |
d899e052 | 9665 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 9666 | if (ret) { |
857cc2fc | 9667 | btrfs_free_reserved_extent(root, ins.objectid, |
e570fd27 | 9668 | ins.offset, 0); |
79787eaa JM |
9669 | btrfs_abort_transaction(trans, root, ret); |
9670 | if (own_trans) | |
9671 | btrfs_end_transaction(trans, root); | |
9672 | break; | |
9673 | } | |
31193213 | 9674 | |
a1ed835e CM |
9675 | btrfs_drop_extent_cache(inode, cur_offset, |
9676 | cur_offset + ins.offset -1, 0); | |
5a303d5d | 9677 | |
5dc562c5 JB |
9678 | em = alloc_extent_map(); |
9679 | if (!em) { | |
9680 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
9681 | &BTRFS_I(inode)->runtime_flags); | |
9682 | goto next; | |
9683 | } | |
9684 | ||
9685 | em->start = cur_offset; | |
9686 | em->orig_start = cur_offset; | |
9687 | em->len = ins.offset; | |
9688 | em->block_start = ins.objectid; | |
9689 | em->block_len = ins.offset; | |
b4939680 | 9690 | em->orig_block_len = ins.offset; |
cc95bef6 | 9691 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
9692 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
9693 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
9694 | em->generation = trans->transid; | |
9695 | ||
9696 | while (1) { | |
9697 | write_lock(&em_tree->lock); | |
09a2a8f9 | 9698 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
9699 | write_unlock(&em_tree->lock); |
9700 | if (ret != -EEXIST) | |
9701 | break; | |
9702 | btrfs_drop_extent_cache(inode, cur_offset, | |
9703 | cur_offset + ins.offset - 1, | |
9704 | 0); | |
9705 | } | |
9706 | free_extent_map(em); | |
9707 | next: | |
d899e052 YZ |
9708 | num_bytes -= ins.offset; |
9709 | cur_offset += ins.offset; | |
efa56464 | 9710 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 9711 | |
0c4d2d95 | 9712 | inode_inc_iversion(inode); |
d899e052 | 9713 | inode->i_ctime = CURRENT_TIME; |
6cbff00f | 9714 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 9715 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
9716 | (actual_len > inode->i_size) && |
9717 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 9718 | if (cur_offset > actual_len) |
55a61d1d | 9719 | i_size = actual_len; |
d1ea6a61 | 9720 | else |
55a61d1d JB |
9721 | i_size = cur_offset; |
9722 | i_size_write(inode, i_size); | |
9723 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
9724 | } |
9725 | ||
d899e052 | 9726 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
9727 | |
9728 | if (ret) { | |
9729 | btrfs_abort_transaction(trans, root, ret); | |
9730 | if (own_trans) | |
9731 | btrfs_end_transaction(trans, root); | |
9732 | break; | |
9733 | } | |
d899e052 | 9734 | |
0af3d00b JB |
9735 | if (own_trans) |
9736 | btrfs_end_transaction(trans, root); | |
5a303d5d | 9737 | } |
d899e052 YZ |
9738 | return ret; |
9739 | } | |
9740 | ||
0af3d00b JB |
9741 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
9742 | u64 start, u64 num_bytes, u64 min_size, | |
9743 | loff_t actual_len, u64 *alloc_hint) | |
9744 | { | |
9745 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9746 | min_size, actual_len, alloc_hint, | |
9747 | NULL); | |
9748 | } | |
9749 | ||
9750 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
9751 | struct btrfs_trans_handle *trans, int mode, | |
9752 | u64 start, u64 num_bytes, u64 min_size, | |
9753 | loff_t actual_len, u64 *alloc_hint) | |
9754 | { | |
9755 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9756 | min_size, actual_len, alloc_hint, trans); | |
9757 | } | |
9758 | ||
e6dcd2dc CM |
9759 | static int btrfs_set_page_dirty(struct page *page) |
9760 | { | |
e6dcd2dc CM |
9761 | return __set_page_dirty_nobuffers(page); |
9762 | } | |
9763 | ||
10556cb2 | 9764 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 9765 | { |
b83cc969 | 9766 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 9767 | umode_t mode = inode->i_mode; |
b83cc969 | 9768 | |
cb6db4e5 JM |
9769 | if (mask & MAY_WRITE && |
9770 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
9771 | if (btrfs_root_readonly(root)) | |
9772 | return -EROFS; | |
9773 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
9774 | return -EACCES; | |
9775 | } | |
2830ba7f | 9776 | return generic_permission(inode, mask); |
fdebe2bd | 9777 | } |
39279cc3 | 9778 | |
ef3b9af5 FM |
9779 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
9780 | { | |
9781 | struct btrfs_trans_handle *trans; | |
9782 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9783 | struct inode *inode = NULL; | |
9784 | u64 objectid; | |
9785 | u64 index; | |
9786 | int ret = 0; | |
9787 | ||
9788 | /* | |
9789 | * 5 units required for adding orphan entry | |
9790 | */ | |
9791 | trans = btrfs_start_transaction(root, 5); | |
9792 | if (IS_ERR(trans)) | |
9793 | return PTR_ERR(trans); | |
9794 | ||
9795 | ret = btrfs_find_free_ino(root, &objectid); | |
9796 | if (ret) | |
9797 | goto out; | |
9798 | ||
9799 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
9800 | btrfs_ino(dir), objectid, mode, &index); | |
9801 | if (IS_ERR(inode)) { | |
9802 | ret = PTR_ERR(inode); | |
9803 | inode = NULL; | |
9804 | goto out; | |
9805 | } | |
9806 | ||
ef3b9af5 FM |
9807 | inode->i_fop = &btrfs_file_operations; |
9808 | inode->i_op = &btrfs_file_inode_operations; | |
9809 | ||
9810 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
9811 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9812 | ||
b0d5d10f CM |
9813 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
9814 | if (ret) | |
9815 | goto out_inode; | |
9816 | ||
9817 | ret = btrfs_update_inode(trans, root, inode); | |
9818 | if (ret) | |
9819 | goto out_inode; | |
ef3b9af5 FM |
9820 | ret = btrfs_orphan_add(trans, inode); |
9821 | if (ret) | |
b0d5d10f | 9822 | goto out_inode; |
ef3b9af5 | 9823 | |
5762b5c9 FM |
9824 | /* |
9825 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
9826 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
9827 | * through: | |
9828 | * | |
9829 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
9830 | */ | |
9831 | set_nlink(inode, 1); | |
b0d5d10f | 9832 | unlock_new_inode(inode); |
ef3b9af5 FM |
9833 | d_tmpfile(dentry, inode); |
9834 | mark_inode_dirty(inode); | |
9835 | ||
9836 | out: | |
9837 | btrfs_end_transaction(trans, root); | |
9838 | if (ret) | |
9839 | iput(inode); | |
9840 | btrfs_balance_delayed_items(root); | |
9841 | btrfs_btree_balance_dirty(root); | |
ef3b9af5 | 9842 | return ret; |
b0d5d10f CM |
9843 | |
9844 | out_inode: | |
9845 | unlock_new_inode(inode); | |
9846 | goto out; | |
9847 | ||
ef3b9af5 FM |
9848 | } |
9849 | ||
b38ef71c FM |
9850 | /* Inspired by filemap_check_errors() */ |
9851 | int btrfs_inode_check_errors(struct inode *inode) | |
9852 | { | |
9853 | int ret = 0; | |
9854 | ||
9855 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && | |
9856 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) | |
9857 | ret = -ENOSPC; | |
9858 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && | |
9859 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) | |
9860 | ret = -EIO; | |
9861 | ||
9862 | return ret; | |
9863 | } | |
9864 | ||
6e1d5dcc | 9865 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 9866 | .getattr = btrfs_getattr, |
39279cc3 CM |
9867 | .lookup = btrfs_lookup, |
9868 | .create = btrfs_create, | |
9869 | .unlink = btrfs_unlink, | |
9870 | .link = btrfs_link, | |
9871 | .mkdir = btrfs_mkdir, | |
9872 | .rmdir = btrfs_rmdir, | |
80ace85c | 9873 | .rename2 = btrfs_rename2, |
39279cc3 CM |
9874 | .symlink = btrfs_symlink, |
9875 | .setattr = btrfs_setattr, | |
618e21d5 | 9876 | .mknod = btrfs_mknod, |
95819c05 CH |
9877 | .setxattr = btrfs_setxattr, |
9878 | .getxattr = btrfs_getxattr, | |
5103e947 | 9879 | .listxattr = btrfs_listxattr, |
95819c05 | 9880 | .removexattr = btrfs_removexattr, |
fdebe2bd | 9881 | .permission = btrfs_permission, |
4e34e719 | 9882 | .get_acl = btrfs_get_acl, |
996a710d | 9883 | .set_acl = btrfs_set_acl, |
93fd63c2 | 9884 | .update_time = btrfs_update_time, |
ef3b9af5 | 9885 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 9886 | }; |
6e1d5dcc | 9887 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 9888 | .lookup = btrfs_lookup, |
fdebe2bd | 9889 | .permission = btrfs_permission, |
4e34e719 | 9890 | .get_acl = btrfs_get_acl, |
996a710d | 9891 | .set_acl = btrfs_set_acl, |
93fd63c2 | 9892 | .update_time = btrfs_update_time, |
39279cc3 | 9893 | }; |
76dda93c | 9894 | |
828c0950 | 9895 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
9896 | .llseek = generic_file_llseek, |
9897 | .read = generic_read_dir, | |
9cdda8d3 | 9898 | .iterate = btrfs_real_readdir, |
34287aa3 | 9899 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 9900 | #ifdef CONFIG_COMPAT |
34287aa3 | 9901 | .compat_ioctl = btrfs_ioctl, |
39279cc3 | 9902 | #endif |
6bf13c0c | 9903 | .release = btrfs_release_file, |
e02119d5 | 9904 | .fsync = btrfs_sync_file, |
39279cc3 CM |
9905 | }; |
9906 | ||
d1310b2e | 9907 | static struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 9908 | .fill_delalloc = run_delalloc_range, |
065631f6 | 9909 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 9910 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 9911 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 9912 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 9913 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
9914 | .set_bit_hook = btrfs_set_bit_hook, |
9915 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
9916 | .merge_extent_hook = btrfs_merge_extent_hook, |
9917 | .split_extent_hook = btrfs_split_extent_hook, | |
07157aac CM |
9918 | }; |
9919 | ||
35054394 CM |
9920 | /* |
9921 | * btrfs doesn't support the bmap operation because swapfiles | |
9922 | * use bmap to make a mapping of extents in the file. They assume | |
9923 | * these extents won't change over the life of the file and they | |
9924 | * use the bmap result to do IO directly to the drive. | |
9925 | * | |
9926 | * the btrfs bmap call would return logical addresses that aren't | |
9927 | * suitable for IO and they also will change frequently as COW | |
9928 | * operations happen. So, swapfile + btrfs == corruption. | |
9929 | * | |
9930 | * For now we're avoiding this by dropping bmap. | |
9931 | */ | |
7f09410b | 9932 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
9933 | .readpage = btrfs_readpage, |
9934 | .writepage = btrfs_writepage, | |
b293f02e | 9935 | .writepages = btrfs_writepages, |
3ab2fb5a | 9936 | .readpages = btrfs_readpages, |
16432985 | 9937 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
9938 | .invalidatepage = btrfs_invalidatepage, |
9939 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 9940 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 9941 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
9942 | }; |
9943 | ||
7f09410b | 9944 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
9945 | .readpage = btrfs_readpage, |
9946 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
9947 | .invalidatepage = btrfs_invalidatepage, |
9948 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
9949 | }; |
9950 | ||
6e1d5dcc | 9951 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
9952 | .getattr = btrfs_getattr, |
9953 | .setattr = btrfs_setattr, | |
95819c05 CH |
9954 | .setxattr = btrfs_setxattr, |
9955 | .getxattr = btrfs_getxattr, | |
5103e947 | 9956 | .listxattr = btrfs_listxattr, |
95819c05 | 9957 | .removexattr = btrfs_removexattr, |
fdebe2bd | 9958 | .permission = btrfs_permission, |
1506fcc8 | 9959 | .fiemap = btrfs_fiemap, |
4e34e719 | 9960 | .get_acl = btrfs_get_acl, |
996a710d | 9961 | .set_acl = btrfs_set_acl, |
e41f941a | 9962 | .update_time = btrfs_update_time, |
39279cc3 | 9963 | }; |
6e1d5dcc | 9964 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
9965 | .getattr = btrfs_getattr, |
9966 | .setattr = btrfs_setattr, | |
fdebe2bd | 9967 | .permission = btrfs_permission, |
95819c05 CH |
9968 | .setxattr = btrfs_setxattr, |
9969 | .getxattr = btrfs_getxattr, | |
33268eaf | 9970 | .listxattr = btrfs_listxattr, |
95819c05 | 9971 | .removexattr = btrfs_removexattr, |
4e34e719 | 9972 | .get_acl = btrfs_get_acl, |
996a710d | 9973 | .set_acl = btrfs_set_acl, |
e41f941a | 9974 | .update_time = btrfs_update_time, |
618e21d5 | 9975 | }; |
6e1d5dcc | 9976 | static const struct inode_operations btrfs_symlink_inode_operations = { |
39279cc3 CM |
9977 | .readlink = generic_readlink, |
9978 | .follow_link = page_follow_link_light, | |
9979 | .put_link = page_put_link, | |
f209561a | 9980 | .getattr = btrfs_getattr, |
22c44fe6 | 9981 | .setattr = btrfs_setattr, |
fdebe2bd | 9982 | .permission = btrfs_permission, |
0279b4cd JO |
9983 | .setxattr = btrfs_setxattr, |
9984 | .getxattr = btrfs_getxattr, | |
9985 | .listxattr = btrfs_listxattr, | |
9986 | .removexattr = btrfs_removexattr, | |
e41f941a | 9987 | .update_time = btrfs_update_time, |
39279cc3 | 9988 | }; |
76dda93c | 9989 | |
82d339d9 | 9990 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 9991 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 9992 | .d_release = btrfs_dentry_release, |
76dda93c | 9993 | }; |