]>
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; |
20e5506b | 77 | static const struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
78 | |
79 | static struct kmem_cache *btrfs_inode_cachep; | |
80 | struct kmem_cache *btrfs_trans_handle_cachep; | |
81 | struct kmem_cache *btrfs_transaction_cachep; | |
39279cc3 | 82 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 83 | struct kmem_cache *btrfs_free_space_cachep; |
39279cc3 CM |
84 | |
85 | #define S_SHIFT 12 | |
86 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { | |
87 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, | |
88 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
89 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
90 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
91 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
92 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
93 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
94 | }; | |
95 | ||
3972f260 | 96 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
a41ad394 | 97 | static int btrfs_truncate(struct inode *inode); |
5fd02043 | 98 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
771ed689 CM |
99 | static noinline int cow_file_range(struct inode *inode, |
100 | struct page *locked_page, | |
101 | u64 start, u64 end, int *page_started, | |
102 | unsigned long *nr_written, int unlock); | |
70c8a91c JB |
103 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
104 | u64 len, u64 orig_start, | |
105 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
106 | u64 orig_block_len, u64 ram_bytes, |
107 | int type); | |
7b128766 | 108 | |
48a3b636 | 109 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 110 | |
6a3891c5 JB |
111 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
112 | void btrfs_test_inode_set_ops(struct inode *inode) | |
113 | { | |
114 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
115 | } | |
116 | #endif | |
117 | ||
f34f57a3 | 118 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
119 | struct inode *inode, struct inode *dir, |
120 | const struct qstr *qstr) | |
0279b4cd JO |
121 | { |
122 | int err; | |
123 | ||
f34f57a3 | 124 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 125 | if (!err) |
2a7dba39 | 126 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
127 | return err; |
128 | } | |
129 | ||
c8b97818 CM |
130 | /* |
131 | * this does all the hard work for inserting an inline extent into | |
132 | * the btree. The caller should have done a btrfs_drop_extents so that | |
133 | * no overlapping inline items exist in the btree | |
134 | */ | |
40f76580 | 135 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 136 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
137 | struct btrfs_root *root, struct inode *inode, |
138 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 139 | int compress_type, |
c8b97818 CM |
140 | struct page **compressed_pages) |
141 | { | |
c8b97818 CM |
142 | struct extent_buffer *leaf; |
143 | struct page *page = NULL; | |
144 | char *kaddr; | |
145 | unsigned long ptr; | |
146 | struct btrfs_file_extent_item *ei; | |
147 | int err = 0; | |
148 | int ret; | |
149 | size_t cur_size = size; | |
c8b97818 | 150 | unsigned long offset; |
c8b97818 | 151 | |
fe3f566c | 152 | if (compressed_size && compressed_pages) |
c8b97818 | 153 | cur_size = compressed_size; |
c8b97818 | 154 | |
1acae57b | 155 | inode_add_bytes(inode, size); |
c8b97818 | 156 | |
1acae57b FDBM |
157 | if (!extent_inserted) { |
158 | struct btrfs_key key; | |
159 | size_t datasize; | |
c8b97818 | 160 | |
1acae57b FDBM |
161 | key.objectid = btrfs_ino(inode); |
162 | key.offset = start; | |
962a298f | 163 | key.type = BTRFS_EXTENT_DATA_KEY; |
c8b97818 | 164 | |
1acae57b FDBM |
165 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
166 | path->leave_spinning = 1; | |
167 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
168 | datasize); | |
169 | if (ret) { | |
170 | err = ret; | |
171 | goto fail; | |
172 | } | |
c8b97818 CM |
173 | } |
174 | leaf = path->nodes[0]; | |
175 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
176 | struct btrfs_file_extent_item); | |
177 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
178 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
179 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
180 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
181 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
182 | ptr = btrfs_file_extent_inline_start(ei); | |
183 | ||
261507a0 | 184 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
185 | struct page *cpage; |
186 | int i = 0; | |
d397712b | 187 | while (compressed_size > 0) { |
c8b97818 | 188 | cpage = compressed_pages[i]; |
5b050f04 | 189 | cur_size = min_t(unsigned long, compressed_size, |
c8b97818 CM |
190 | PAGE_CACHE_SIZE); |
191 | ||
7ac687d9 | 192 | kaddr = kmap_atomic(cpage); |
c8b97818 | 193 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 194 | kunmap_atomic(kaddr); |
c8b97818 CM |
195 | |
196 | i++; | |
197 | ptr += cur_size; | |
198 | compressed_size -= cur_size; | |
199 | } | |
200 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 201 | compress_type); |
c8b97818 CM |
202 | } else { |
203 | page = find_get_page(inode->i_mapping, | |
204 | start >> PAGE_CACHE_SHIFT); | |
205 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
7ac687d9 | 206 | kaddr = kmap_atomic(page); |
c8b97818 CM |
207 | offset = start & (PAGE_CACHE_SIZE - 1); |
208 | write_extent_buffer(leaf, kaddr + offset, ptr, size); | |
7ac687d9 | 209 | kunmap_atomic(kaddr); |
c8b97818 CM |
210 | page_cache_release(page); |
211 | } | |
212 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 213 | btrfs_release_path(path); |
c8b97818 | 214 | |
c2167754 YZ |
215 | /* |
216 | * we're an inline extent, so nobody can | |
217 | * extend the file past i_size without locking | |
218 | * a page we already have locked. | |
219 | * | |
220 | * We must do any isize and inode updates | |
221 | * before we unlock the pages. Otherwise we | |
222 | * could end up racing with unlink. | |
223 | */ | |
c8b97818 | 224 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 225 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 226 | |
79787eaa | 227 | return ret; |
c8b97818 | 228 | fail: |
c8b97818 CM |
229 | return err; |
230 | } | |
231 | ||
232 | ||
233 | /* | |
234 | * conditionally insert an inline extent into the file. This | |
235 | * does the checks required to make sure the data is small enough | |
236 | * to fit as an inline extent. | |
237 | */ | |
00361589 JB |
238 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
239 | struct inode *inode, u64 start, | |
240 | u64 end, size_t compressed_size, | |
241 | int compress_type, | |
242 | struct page **compressed_pages) | |
c8b97818 | 243 | { |
00361589 | 244 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
245 | u64 isize = i_size_read(inode); |
246 | u64 actual_end = min(end + 1, isize); | |
247 | u64 inline_len = actual_end - start; | |
fda2832f | 248 | u64 aligned_end = ALIGN(end, root->sectorsize); |
c8b97818 CM |
249 | u64 data_len = inline_len; |
250 | int ret; | |
1acae57b FDBM |
251 | struct btrfs_path *path; |
252 | int extent_inserted = 0; | |
253 | u32 extent_item_size; | |
c8b97818 CM |
254 | |
255 | if (compressed_size) | |
256 | data_len = compressed_size; | |
257 | ||
258 | if (start > 0 || | |
354877be WS |
259 | actual_end > PAGE_CACHE_SIZE || |
260 | data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) || | |
c8b97818 CM |
261 | (!compressed_size && |
262 | (actual_end & (root->sectorsize - 1)) == 0) || | |
263 | end + 1 < isize || | |
264 | data_len > root->fs_info->max_inline) { | |
265 | return 1; | |
266 | } | |
267 | ||
1acae57b FDBM |
268 | path = btrfs_alloc_path(); |
269 | if (!path) | |
270 | return -ENOMEM; | |
271 | ||
00361589 | 272 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
273 | if (IS_ERR(trans)) { |
274 | btrfs_free_path(path); | |
00361589 | 275 | return PTR_ERR(trans); |
1acae57b | 276 | } |
00361589 JB |
277 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
278 | ||
1acae57b FDBM |
279 | if (compressed_size && compressed_pages) |
280 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
281 | compressed_size); | |
282 | else | |
283 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
284 | inline_len); | |
285 | ||
286 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
287 | start, aligned_end, NULL, | |
288 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 JB |
289 | if (ret) { |
290 | btrfs_abort_transaction(trans, root, ret); | |
291 | goto out; | |
292 | } | |
c8b97818 CM |
293 | |
294 | if (isize > actual_end) | |
295 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
296 | ret = insert_inline_extent(trans, path, extent_inserted, |
297 | root, inode, start, | |
c8b97818 | 298 | inline_len, compressed_size, |
fe3f566c | 299 | compress_type, compressed_pages); |
2adcac1a | 300 | if (ret && ret != -ENOSPC) { |
79787eaa | 301 | btrfs_abort_transaction(trans, root, ret); |
00361589 | 302 | goto out; |
2adcac1a | 303 | } else if (ret == -ENOSPC) { |
00361589 JB |
304 | ret = 1; |
305 | goto out; | |
79787eaa | 306 | } |
2adcac1a | 307 | |
bdc20e67 | 308 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
0ca1f7ce | 309 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
a1ed835e | 310 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
00361589 | 311 | out: |
94ed938a QW |
312 | /* |
313 | * Don't forget to free the reserved space, as for inlined extent | |
314 | * it won't count as data extent, free them directly here. | |
315 | * And at reserve time, it's always aligned to page size, so | |
316 | * just free one page here. | |
317 | */ | |
318 | btrfs_qgroup_free_data(inode, 0, PAGE_CACHE_SIZE); | |
1acae57b | 319 | btrfs_free_path(path); |
00361589 JB |
320 | btrfs_end_transaction(trans, root); |
321 | return ret; | |
c8b97818 CM |
322 | } |
323 | ||
771ed689 CM |
324 | struct async_extent { |
325 | u64 start; | |
326 | u64 ram_size; | |
327 | u64 compressed_size; | |
328 | struct page **pages; | |
329 | unsigned long nr_pages; | |
261507a0 | 330 | int compress_type; |
771ed689 CM |
331 | struct list_head list; |
332 | }; | |
333 | ||
334 | struct async_cow { | |
335 | struct inode *inode; | |
336 | struct btrfs_root *root; | |
337 | struct page *locked_page; | |
338 | u64 start; | |
339 | u64 end; | |
340 | struct list_head extents; | |
341 | struct btrfs_work work; | |
342 | }; | |
343 | ||
344 | static noinline int add_async_extent(struct async_cow *cow, | |
345 | u64 start, u64 ram_size, | |
346 | u64 compressed_size, | |
347 | struct page **pages, | |
261507a0 LZ |
348 | unsigned long nr_pages, |
349 | int compress_type) | |
771ed689 CM |
350 | { |
351 | struct async_extent *async_extent; | |
352 | ||
353 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 354 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
355 | async_extent->start = start; |
356 | async_extent->ram_size = ram_size; | |
357 | async_extent->compressed_size = compressed_size; | |
358 | async_extent->pages = pages; | |
359 | async_extent->nr_pages = nr_pages; | |
261507a0 | 360 | async_extent->compress_type = compress_type; |
771ed689 CM |
361 | list_add_tail(&async_extent->list, &cow->extents); |
362 | return 0; | |
363 | } | |
364 | ||
f79707b0 WS |
365 | static inline int inode_need_compress(struct inode *inode) |
366 | { | |
367 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
368 | ||
369 | /* force compress */ | |
370 | if (btrfs_test_opt(root, FORCE_COMPRESS)) | |
371 | return 1; | |
372 | /* bad compression ratios */ | |
373 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) | |
374 | return 0; | |
375 | if (btrfs_test_opt(root, COMPRESS) || | |
376 | BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || | |
377 | BTRFS_I(inode)->force_compress) | |
378 | return 1; | |
379 | return 0; | |
380 | } | |
381 | ||
d352ac68 | 382 | /* |
771ed689 CM |
383 | * we create compressed extents in two phases. The first |
384 | * phase compresses a range of pages that have already been | |
385 | * locked (both pages and state bits are locked). | |
c8b97818 | 386 | * |
771ed689 CM |
387 | * This is done inside an ordered work queue, and the compression |
388 | * is spread across many cpus. The actual IO submission is step | |
389 | * two, and the ordered work queue takes care of making sure that | |
390 | * happens in the same order things were put onto the queue by | |
391 | * writepages and friends. | |
c8b97818 | 392 | * |
771ed689 CM |
393 | * If this code finds it can't get good compression, it puts an |
394 | * entry onto the work queue to write the uncompressed bytes. This | |
395 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
396 | * are written in the same order that the flusher thread sent them |
397 | * down. | |
d352ac68 | 398 | */ |
c44f649e | 399 | static noinline void compress_file_range(struct inode *inode, |
771ed689 CM |
400 | struct page *locked_page, |
401 | u64 start, u64 end, | |
402 | struct async_cow *async_cow, | |
403 | int *num_added) | |
b888db2b CM |
404 | { |
405 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
db94535d | 406 | u64 num_bytes; |
db94535d | 407 | u64 blocksize = root->sectorsize; |
c8b97818 | 408 | u64 actual_end; |
42dc7bab | 409 | u64 isize = i_size_read(inode); |
e6dcd2dc | 410 | int ret = 0; |
c8b97818 CM |
411 | struct page **pages = NULL; |
412 | unsigned long nr_pages; | |
413 | unsigned long nr_pages_ret = 0; | |
414 | unsigned long total_compressed = 0; | |
415 | unsigned long total_in = 0; | |
416 | unsigned long max_compressed = 128 * 1024; | |
771ed689 | 417 | unsigned long max_uncompressed = 128 * 1024; |
c8b97818 CM |
418 | int i; |
419 | int will_compress; | |
261507a0 | 420 | int compress_type = root->fs_info->compress_type; |
4adaa611 | 421 | int redirty = 0; |
b888db2b | 422 | |
4cb13e5d LB |
423 | /* if this is a small write inside eof, kick off a defrag */ |
424 | if ((end - start + 1) < 16 * 1024 && | |
425 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
4cb5300b CM |
426 | btrfs_add_inode_defrag(NULL, inode); |
427 | ||
42dc7bab | 428 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
429 | again: |
430 | will_compress = 0; | |
431 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; | |
432 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); | |
be20aa9d | 433 | |
f03d9301 CM |
434 | /* |
435 | * we don't want to send crud past the end of i_size through | |
436 | * compression, that's just a waste of CPU time. So, if the | |
437 | * end of the file is before the start of our current | |
438 | * requested range of bytes, we bail out to the uncompressed | |
439 | * cleanup code that can deal with all of this. | |
440 | * | |
441 | * It isn't really the fastest way to fix things, but this is a | |
442 | * very uncommon corner. | |
443 | */ | |
444 | if (actual_end <= start) | |
445 | goto cleanup_and_bail_uncompressed; | |
446 | ||
c8b97818 CM |
447 | total_compressed = actual_end - start; |
448 | ||
4bcbb332 SW |
449 | /* |
450 | * skip compression for a small file range(<=blocksize) that | |
451 | * isn't an inline extent, since it dosen't save disk space at all. | |
452 | */ | |
453 | if (total_compressed <= blocksize && | |
454 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
455 | goto cleanup_and_bail_uncompressed; | |
456 | ||
c8b97818 CM |
457 | /* we want to make sure that amount of ram required to uncompress |
458 | * an extent is reasonable, so we limit the total size in ram | |
771ed689 CM |
459 | * of a compressed extent to 128k. This is a crucial number |
460 | * because it also controls how easily we can spread reads across | |
461 | * cpus for decompression. | |
462 | * | |
463 | * We also want to make sure the amount of IO required to do | |
464 | * a random read is reasonably small, so we limit the size of | |
465 | * a compressed extent to 128k. | |
c8b97818 CM |
466 | */ |
467 | total_compressed = min(total_compressed, max_uncompressed); | |
fda2832f | 468 | num_bytes = ALIGN(end - start + 1, blocksize); |
be20aa9d | 469 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
470 | total_in = 0; |
471 | ret = 0; | |
db94535d | 472 | |
771ed689 CM |
473 | /* |
474 | * we do compression for mount -o compress and when the | |
475 | * inode has not been flagged as nocompress. This flag can | |
476 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 477 | */ |
f79707b0 | 478 | if (inode_need_compress(inode)) { |
c8b97818 | 479 | WARN_ON(pages); |
31e818fe | 480 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
560f7d75 LZ |
481 | if (!pages) { |
482 | /* just bail out to the uncompressed code */ | |
483 | goto cont; | |
484 | } | |
c8b97818 | 485 | |
261507a0 LZ |
486 | if (BTRFS_I(inode)->force_compress) |
487 | compress_type = BTRFS_I(inode)->force_compress; | |
488 | ||
4adaa611 CM |
489 | /* |
490 | * we need to call clear_page_dirty_for_io on each | |
491 | * page in the range. Otherwise applications with the file | |
492 | * mmap'd can wander in and change the page contents while | |
493 | * we are compressing them. | |
494 | * | |
495 | * If the compression fails for any reason, we set the pages | |
496 | * dirty again later on. | |
497 | */ | |
498 | extent_range_clear_dirty_for_io(inode, start, end); | |
499 | redirty = 1; | |
261507a0 LZ |
500 | ret = btrfs_compress_pages(compress_type, |
501 | inode->i_mapping, start, | |
502 | total_compressed, pages, | |
503 | nr_pages, &nr_pages_ret, | |
504 | &total_in, | |
505 | &total_compressed, | |
506 | max_compressed); | |
c8b97818 CM |
507 | |
508 | if (!ret) { | |
509 | unsigned long offset = total_compressed & | |
510 | (PAGE_CACHE_SIZE - 1); | |
511 | struct page *page = pages[nr_pages_ret - 1]; | |
512 | char *kaddr; | |
513 | ||
514 | /* zero the tail end of the last page, we might be | |
515 | * sending it down to disk | |
516 | */ | |
517 | if (offset) { | |
7ac687d9 | 518 | kaddr = kmap_atomic(page); |
c8b97818 CM |
519 | memset(kaddr + offset, 0, |
520 | PAGE_CACHE_SIZE - offset); | |
7ac687d9 | 521 | kunmap_atomic(kaddr); |
c8b97818 CM |
522 | } |
523 | will_compress = 1; | |
524 | } | |
525 | } | |
560f7d75 | 526 | cont: |
c8b97818 CM |
527 | if (start == 0) { |
528 | /* lets try to make an inline extent */ | |
771ed689 | 529 | if (ret || total_in < (actual_end - start)) { |
c8b97818 | 530 | /* we didn't compress the entire range, try |
771ed689 | 531 | * to make an uncompressed inline extent. |
c8b97818 | 532 | */ |
00361589 JB |
533 | ret = cow_file_range_inline(root, inode, start, end, |
534 | 0, 0, NULL); | |
c8b97818 | 535 | } else { |
771ed689 | 536 | /* try making a compressed inline extent */ |
00361589 | 537 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
538 | total_compressed, |
539 | compress_type, pages); | |
c8b97818 | 540 | } |
79787eaa | 541 | if (ret <= 0) { |
151a41bc JB |
542 | unsigned long clear_flags = EXTENT_DELALLOC | |
543 | EXTENT_DEFRAG; | |
e6eb4314 FM |
544 | unsigned long page_error_op; |
545 | ||
151a41bc | 546 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; |
e6eb4314 | 547 | page_error_op = ret < 0 ? PAGE_SET_ERROR : 0; |
151a41bc | 548 | |
771ed689 | 549 | /* |
79787eaa JM |
550 | * inline extent creation worked or returned error, |
551 | * we don't need to create any more async work items. | |
552 | * Unlock and free up our temp pages. | |
771ed689 | 553 | */ |
c2790a2e | 554 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
151a41bc | 555 | clear_flags, PAGE_UNLOCK | |
c2790a2e JB |
556 | PAGE_CLEAR_DIRTY | |
557 | PAGE_SET_WRITEBACK | | |
e6eb4314 | 558 | page_error_op | |
c2790a2e | 559 | PAGE_END_WRITEBACK); |
c8b97818 CM |
560 | goto free_pages_out; |
561 | } | |
562 | } | |
563 | ||
564 | if (will_compress) { | |
565 | /* | |
566 | * we aren't doing an inline extent round the compressed size | |
567 | * up to a block size boundary so the allocator does sane | |
568 | * things | |
569 | */ | |
fda2832f | 570 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
571 | |
572 | /* | |
573 | * one last check to make sure the compression is really a | |
574 | * win, compare the page count read with the blocks on disk | |
575 | */ | |
fda2832f | 576 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
c8b97818 CM |
577 | if (total_compressed >= total_in) { |
578 | will_compress = 0; | |
579 | } else { | |
c8b97818 CM |
580 | num_bytes = total_in; |
581 | } | |
582 | } | |
583 | if (!will_compress && pages) { | |
584 | /* | |
585 | * the compression code ran but failed to make things smaller, | |
586 | * free any pages it allocated and our page pointer array | |
587 | */ | |
588 | for (i = 0; i < nr_pages_ret; i++) { | |
70b99e69 | 589 | WARN_ON(pages[i]->mapping); |
c8b97818 CM |
590 | page_cache_release(pages[i]); |
591 | } | |
592 | kfree(pages); | |
593 | pages = NULL; | |
594 | total_compressed = 0; | |
595 | nr_pages_ret = 0; | |
596 | ||
597 | /* flag the file so we don't compress in the future */ | |
1e701a32 CM |
598 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
599 | !(BTRFS_I(inode)->force_compress)) { | |
a555f810 | 600 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 601 | } |
c8b97818 | 602 | } |
771ed689 CM |
603 | if (will_compress) { |
604 | *num_added += 1; | |
c8b97818 | 605 | |
771ed689 CM |
606 | /* the async work queues will take care of doing actual |
607 | * allocation on disk for these compressed pages, | |
608 | * and will submit them to the elevator. | |
609 | */ | |
610 | add_async_extent(async_cow, start, num_bytes, | |
261507a0 LZ |
611 | total_compressed, pages, nr_pages_ret, |
612 | compress_type); | |
179e29e4 | 613 | |
24ae6365 | 614 | if (start + num_bytes < end) { |
771ed689 CM |
615 | start += num_bytes; |
616 | pages = NULL; | |
617 | cond_resched(); | |
618 | goto again; | |
619 | } | |
620 | } else { | |
f03d9301 | 621 | cleanup_and_bail_uncompressed: |
771ed689 CM |
622 | /* |
623 | * No compression, but we still need to write the pages in | |
624 | * the file we've been given so far. redirty the locked | |
625 | * page if it corresponds to our extent and set things up | |
626 | * for the async work queue to run cow_file_range to do | |
627 | * the normal delalloc dance | |
628 | */ | |
629 | if (page_offset(locked_page) >= start && | |
630 | page_offset(locked_page) <= end) { | |
631 | __set_page_dirty_nobuffers(locked_page); | |
632 | /* unlocked later on in the async handlers */ | |
633 | } | |
4adaa611 CM |
634 | if (redirty) |
635 | extent_range_redirty_for_io(inode, start, end); | |
261507a0 LZ |
636 | add_async_extent(async_cow, start, end - start + 1, |
637 | 0, NULL, 0, BTRFS_COMPRESS_NONE); | |
771ed689 CM |
638 | *num_added += 1; |
639 | } | |
3b951516 | 640 | |
c44f649e | 641 | return; |
771ed689 CM |
642 | |
643 | free_pages_out: | |
644 | for (i = 0; i < nr_pages_ret; i++) { | |
645 | WARN_ON(pages[i]->mapping); | |
646 | page_cache_release(pages[i]); | |
647 | } | |
d397712b | 648 | kfree(pages); |
771ed689 | 649 | } |
771ed689 | 650 | |
40ae837b FM |
651 | static void free_async_extent_pages(struct async_extent *async_extent) |
652 | { | |
653 | int i; | |
654 | ||
655 | if (!async_extent->pages) | |
656 | return; | |
657 | ||
658 | for (i = 0; i < async_extent->nr_pages; i++) { | |
659 | WARN_ON(async_extent->pages[i]->mapping); | |
660 | page_cache_release(async_extent->pages[i]); | |
661 | } | |
662 | kfree(async_extent->pages); | |
663 | async_extent->nr_pages = 0; | |
664 | async_extent->pages = NULL; | |
771ed689 CM |
665 | } |
666 | ||
667 | /* | |
668 | * phase two of compressed writeback. This is the ordered portion | |
669 | * of the code, which only gets called in the order the work was | |
670 | * queued. We walk all the async extents created by compress_file_range | |
671 | * and send them down to the disk. | |
672 | */ | |
dec8f175 | 673 | static noinline void submit_compressed_extents(struct inode *inode, |
771ed689 CM |
674 | struct async_cow *async_cow) |
675 | { | |
676 | struct async_extent *async_extent; | |
677 | u64 alloc_hint = 0; | |
771ed689 CM |
678 | struct btrfs_key ins; |
679 | struct extent_map *em; | |
680 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
681 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
682 | struct extent_io_tree *io_tree; | |
f5a84ee3 | 683 | int ret = 0; |
771ed689 | 684 | |
3e04e7f1 | 685 | again: |
d397712b | 686 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
687 | async_extent = list_entry(async_cow->extents.next, |
688 | struct async_extent, list); | |
689 | list_del(&async_extent->list); | |
c8b97818 | 690 | |
771ed689 CM |
691 | io_tree = &BTRFS_I(inode)->io_tree; |
692 | ||
f5a84ee3 | 693 | retry: |
771ed689 CM |
694 | /* did the compression code fall back to uncompressed IO? */ |
695 | if (!async_extent->pages) { | |
696 | int page_started = 0; | |
697 | unsigned long nr_written = 0; | |
698 | ||
699 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 700 | async_extent->start + |
d0082371 | 701 | async_extent->ram_size - 1); |
771ed689 CM |
702 | |
703 | /* allocate blocks */ | |
f5a84ee3 JB |
704 | ret = cow_file_range(inode, async_cow->locked_page, |
705 | async_extent->start, | |
706 | async_extent->start + | |
707 | async_extent->ram_size - 1, | |
708 | &page_started, &nr_written, 0); | |
771ed689 | 709 | |
79787eaa JM |
710 | /* JDM XXX */ |
711 | ||
771ed689 CM |
712 | /* |
713 | * if page_started, cow_file_range inserted an | |
714 | * inline extent and took care of all the unlocking | |
715 | * and IO for us. Otherwise, we need to submit | |
716 | * all those pages down to the drive. | |
717 | */ | |
f5a84ee3 | 718 | if (!page_started && !ret) |
771ed689 CM |
719 | extent_write_locked_range(io_tree, |
720 | inode, async_extent->start, | |
d397712b | 721 | async_extent->start + |
771ed689 CM |
722 | async_extent->ram_size - 1, |
723 | btrfs_get_extent, | |
724 | WB_SYNC_ALL); | |
3e04e7f1 JB |
725 | else if (ret) |
726 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
727 | kfree(async_extent); |
728 | cond_resched(); | |
729 | continue; | |
730 | } | |
731 | ||
732 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 733 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 734 | |
00361589 | 735 | ret = btrfs_reserve_extent(root, |
771ed689 CM |
736 | async_extent->compressed_size, |
737 | async_extent->compressed_size, | |
e570fd27 | 738 | 0, alloc_hint, &ins, 1, 1); |
f5a84ee3 | 739 | if (ret) { |
40ae837b | 740 | free_async_extent_pages(async_extent); |
3e04e7f1 | 741 | |
fdf8e2ea JB |
742 | if (ret == -ENOSPC) { |
743 | unlock_extent(io_tree, async_extent->start, | |
744 | async_extent->start + | |
745 | async_extent->ram_size - 1); | |
ce62003f LB |
746 | |
747 | /* | |
748 | * we need to redirty the pages if we decide to | |
749 | * fallback to uncompressed IO, otherwise we | |
750 | * will not submit these pages down to lower | |
751 | * layers. | |
752 | */ | |
753 | extent_range_redirty_for_io(inode, | |
754 | async_extent->start, | |
755 | async_extent->start + | |
756 | async_extent->ram_size - 1); | |
757 | ||
79787eaa | 758 | goto retry; |
fdf8e2ea | 759 | } |
3e04e7f1 | 760 | goto out_free; |
f5a84ee3 | 761 | } |
c2167754 YZ |
762 | /* |
763 | * here we're doing allocation and writeback of the | |
764 | * compressed pages | |
765 | */ | |
766 | btrfs_drop_extent_cache(inode, async_extent->start, | |
767 | async_extent->start + | |
768 | async_extent->ram_size - 1, 0); | |
769 | ||
172ddd60 | 770 | em = alloc_extent_map(); |
b9aa55be LB |
771 | if (!em) { |
772 | ret = -ENOMEM; | |
3e04e7f1 | 773 | goto out_free_reserve; |
b9aa55be | 774 | } |
771ed689 CM |
775 | em->start = async_extent->start; |
776 | em->len = async_extent->ram_size; | |
445a6944 | 777 | em->orig_start = em->start; |
2ab28f32 JB |
778 | em->mod_start = em->start; |
779 | em->mod_len = em->len; | |
c8b97818 | 780 | |
771ed689 CM |
781 | em->block_start = ins.objectid; |
782 | em->block_len = ins.offset; | |
b4939680 | 783 | em->orig_block_len = ins.offset; |
cc95bef6 | 784 | em->ram_bytes = async_extent->ram_size; |
771ed689 | 785 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
261507a0 | 786 | em->compress_type = async_extent->compress_type; |
771ed689 CM |
787 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
788 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
70c8a91c | 789 | em->generation = -1; |
771ed689 | 790 | |
d397712b | 791 | while (1) { |
890871be | 792 | write_lock(&em_tree->lock); |
09a2a8f9 | 793 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 794 | write_unlock(&em_tree->lock); |
771ed689 CM |
795 | if (ret != -EEXIST) { |
796 | free_extent_map(em); | |
797 | break; | |
798 | } | |
799 | btrfs_drop_extent_cache(inode, async_extent->start, | |
800 | async_extent->start + | |
801 | async_extent->ram_size - 1, 0); | |
802 | } | |
803 | ||
3e04e7f1 JB |
804 | if (ret) |
805 | goto out_free_reserve; | |
806 | ||
261507a0 LZ |
807 | ret = btrfs_add_ordered_extent_compress(inode, |
808 | async_extent->start, | |
809 | ins.objectid, | |
810 | async_extent->ram_size, | |
811 | ins.offset, | |
812 | BTRFS_ORDERED_COMPRESSED, | |
813 | async_extent->compress_type); | |
d9f85963 FM |
814 | if (ret) { |
815 | btrfs_drop_extent_cache(inode, async_extent->start, | |
816 | async_extent->start + | |
817 | async_extent->ram_size - 1, 0); | |
3e04e7f1 | 818 | goto out_free_reserve; |
d9f85963 | 819 | } |
771ed689 | 820 | |
771ed689 CM |
821 | /* |
822 | * clear dirty, set writeback and unlock the pages. | |
823 | */ | |
c2790a2e | 824 | extent_clear_unlock_delalloc(inode, async_extent->start, |
a791e35e CM |
825 | async_extent->start + |
826 | async_extent->ram_size - 1, | |
151a41bc JB |
827 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
828 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 829 | PAGE_SET_WRITEBACK); |
771ed689 | 830 | ret = btrfs_submit_compressed_write(inode, |
d397712b CM |
831 | async_extent->start, |
832 | async_extent->ram_size, | |
833 | ins.objectid, | |
834 | ins.offset, async_extent->pages, | |
835 | async_extent->nr_pages); | |
fce2a4e6 FM |
836 | if (ret) { |
837 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
838 | struct page *p = async_extent->pages[0]; | |
839 | const u64 start = async_extent->start; | |
840 | const u64 end = start + async_extent->ram_size - 1; | |
841 | ||
842 | p->mapping = inode->i_mapping; | |
843 | tree->ops->writepage_end_io_hook(p, start, end, | |
844 | NULL, 0); | |
845 | p->mapping = NULL; | |
846 | extent_clear_unlock_delalloc(inode, start, end, NULL, 0, | |
847 | PAGE_END_WRITEBACK | | |
848 | PAGE_SET_ERROR); | |
40ae837b | 849 | free_async_extent_pages(async_extent); |
fce2a4e6 | 850 | } |
771ed689 CM |
851 | alloc_hint = ins.objectid + ins.offset; |
852 | kfree(async_extent); | |
853 | cond_resched(); | |
854 | } | |
dec8f175 | 855 | return; |
3e04e7f1 | 856 | out_free_reserve: |
e570fd27 | 857 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 858 | out_free: |
c2790a2e | 859 | extent_clear_unlock_delalloc(inode, async_extent->start, |
3e04e7f1 JB |
860 | async_extent->start + |
861 | async_extent->ram_size - 1, | |
c2790a2e | 862 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
863 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
864 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
704de49d FM |
865 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK | |
866 | PAGE_SET_ERROR); | |
40ae837b | 867 | free_async_extent_pages(async_extent); |
79787eaa | 868 | kfree(async_extent); |
3e04e7f1 | 869 | goto again; |
771ed689 CM |
870 | } |
871 | ||
4b46fce2 JB |
872 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
873 | u64 num_bytes) | |
874 | { | |
875 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
876 | struct extent_map *em; | |
877 | u64 alloc_hint = 0; | |
878 | ||
879 | read_lock(&em_tree->lock); | |
880 | em = search_extent_mapping(em_tree, start, num_bytes); | |
881 | if (em) { | |
882 | /* | |
883 | * if block start isn't an actual block number then find the | |
884 | * first block in this inode and use that as a hint. If that | |
885 | * block is also bogus then just don't worry about it. | |
886 | */ | |
887 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
888 | free_extent_map(em); | |
889 | em = search_extent_mapping(em_tree, 0, 0); | |
890 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
891 | alloc_hint = em->block_start; | |
892 | if (em) | |
893 | free_extent_map(em); | |
894 | } else { | |
895 | alloc_hint = em->block_start; | |
896 | free_extent_map(em); | |
897 | } | |
898 | } | |
899 | read_unlock(&em_tree->lock); | |
900 | ||
901 | return alloc_hint; | |
902 | } | |
903 | ||
771ed689 CM |
904 | /* |
905 | * when extent_io.c finds a delayed allocation range in the file, | |
906 | * the call backs end up in this code. The basic idea is to | |
907 | * allocate extents on disk for the range, and create ordered data structs | |
908 | * in ram to track those extents. | |
909 | * | |
910 | * locked_page is the page that writepage had locked already. We use | |
911 | * it to make sure we don't do extra locks or unlocks. | |
912 | * | |
913 | * *page_started is set to one if we unlock locked_page and do everything | |
914 | * required to start IO on it. It may be clean and already done with | |
915 | * IO when we return. | |
916 | */ | |
00361589 JB |
917 | static noinline int cow_file_range(struct inode *inode, |
918 | struct page *locked_page, | |
919 | u64 start, u64 end, int *page_started, | |
920 | unsigned long *nr_written, | |
921 | int unlock) | |
771ed689 | 922 | { |
00361589 | 923 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
924 | u64 alloc_hint = 0; |
925 | u64 num_bytes; | |
926 | unsigned long ram_size; | |
927 | u64 disk_num_bytes; | |
928 | u64 cur_alloc_size; | |
929 | u64 blocksize = root->sectorsize; | |
771ed689 CM |
930 | struct btrfs_key ins; |
931 | struct extent_map *em; | |
932 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
933 | int ret = 0; | |
934 | ||
02ecd2c2 JB |
935 | if (btrfs_is_free_space_inode(inode)) { |
936 | WARN_ON_ONCE(1); | |
29bce2f3 JB |
937 | ret = -EINVAL; |
938 | goto out_unlock; | |
02ecd2c2 | 939 | } |
771ed689 | 940 | |
fda2832f | 941 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
942 | num_bytes = max(blocksize, num_bytes); |
943 | disk_num_bytes = num_bytes; | |
771ed689 | 944 | |
4cb5300b | 945 | /* if this is a small write inside eof, kick off defrag */ |
4cb13e5d LB |
946 | if (num_bytes < 64 * 1024 && |
947 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
00361589 | 948 | btrfs_add_inode_defrag(NULL, inode); |
4cb5300b | 949 | |
771ed689 CM |
950 | if (start == 0) { |
951 | /* lets try to make an inline extent */ | |
00361589 JB |
952 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
953 | NULL); | |
771ed689 | 954 | if (ret == 0) { |
c2790a2e JB |
955 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
956 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc | 957 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
958 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
959 | PAGE_END_WRITEBACK); | |
c2167754 | 960 | |
771ed689 CM |
961 | *nr_written = *nr_written + |
962 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; | |
963 | *page_started = 1; | |
771ed689 | 964 | goto out; |
79787eaa | 965 | } else if (ret < 0) { |
79787eaa | 966 | goto out_unlock; |
771ed689 CM |
967 | } |
968 | } | |
969 | ||
970 | BUG_ON(disk_num_bytes > | |
6c41761f | 971 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
771ed689 | 972 | |
4b46fce2 | 973 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 CM |
974 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
975 | ||
d397712b | 976 | while (disk_num_bytes > 0) { |
a791e35e CM |
977 | unsigned long op; |
978 | ||
287a0ab9 | 979 | cur_alloc_size = disk_num_bytes; |
00361589 | 980 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
771ed689 | 981 | root->sectorsize, 0, alloc_hint, |
e570fd27 | 982 | &ins, 1, 1); |
00361589 | 983 | if (ret < 0) |
79787eaa | 984 | goto out_unlock; |
d397712b | 985 | |
172ddd60 | 986 | em = alloc_extent_map(); |
b9aa55be LB |
987 | if (!em) { |
988 | ret = -ENOMEM; | |
ace68bac | 989 | goto out_reserve; |
b9aa55be | 990 | } |
e6dcd2dc | 991 | em->start = start; |
445a6944 | 992 | em->orig_start = em->start; |
771ed689 CM |
993 | ram_size = ins.offset; |
994 | em->len = ins.offset; | |
2ab28f32 JB |
995 | em->mod_start = em->start; |
996 | em->mod_len = em->len; | |
c8b97818 | 997 | |
e6dcd2dc | 998 | em->block_start = ins.objectid; |
c8b97818 | 999 | em->block_len = ins.offset; |
b4939680 | 1000 | em->orig_block_len = ins.offset; |
cc95bef6 | 1001 | em->ram_bytes = ram_size; |
e6dcd2dc | 1002 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
7f3c74fb | 1003 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
70c8a91c | 1004 | em->generation = -1; |
c8b97818 | 1005 | |
d397712b | 1006 | while (1) { |
890871be | 1007 | write_lock(&em_tree->lock); |
09a2a8f9 | 1008 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1009 | write_unlock(&em_tree->lock); |
e6dcd2dc CM |
1010 | if (ret != -EEXIST) { |
1011 | free_extent_map(em); | |
1012 | break; | |
1013 | } | |
1014 | btrfs_drop_extent_cache(inode, start, | |
c8b97818 | 1015 | start + ram_size - 1, 0); |
e6dcd2dc | 1016 | } |
ace68bac LB |
1017 | if (ret) |
1018 | goto out_reserve; | |
e6dcd2dc | 1019 | |
98d20f67 | 1020 | cur_alloc_size = ins.offset; |
e6dcd2dc | 1021 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 1022 | ram_size, cur_alloc_size, 0); |
ace68bac | 1023 | if (ret) |
d9f85963 | 1024 | goto out_drop_extent_cache; |
c8b97818 | 1025 | |
17d217fe YZ |
1026 | if (root->root_key.objectid == |
1027 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1028 | ret = btrfs_reloc_clone_csums(inode, start, | |
1029 | cur_alloc_size); | |
00361589 | 1030 | if (ret) |
d9f85963 | 1031 | goto out_drop_extent_cache; |
17d217fe YZ |
1032 | } |
1033 | ||
d397712b | 1034 | if (disk_num_bytes < cur_alloc_size) |
3b951516 | 1035 | break; |
d397712b | 1036 | |
c8b97818 CM |
1037 | /* we're not doing compressed IO, don't unlock the first |
1038 | * page (which the caller expects to stay locked), don't | |
1039 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
1040 | * |
1041 | * Do set the Private2 bit so we know this page was properly | |
1042 | * setup for writepage | |
c8b97818 | 1043 | */ |
c2790a2e JB |
1044 | op = unlock ? PAGE_UNLOCK : 0; |
1045 | op |= PAGE_SET_PRIVATE2; | |
a791e35e | 1046 | |
c2790a2e JB |
1047 | extent_clear_unlock_delalloc(inode, start, |
1048 | start + ram_size - 1, locked_page, | |
1049 | EXTENT_LOCKED | EXTENT_DELALLOC, | |
1050 | op); | |
c8b97818 | 1051 | disk_num_bytes -= cur_alloc_size; |
c59f8951 CM |
1052 | num_bytes -= cur_alloc_size; |
1053 | alloc_hint = ins.objectid + ins.offset; | |
1054 | start += cur_alloc_size; | |
b888db2b | 1055 | } |
79787eaa | 1056 | out: |
be20aa9d | 1057 | return ret; |
b7d5b0a8 | 1058 | |
d9f85963 FM |
1059 | out_drop_extent_cache: |
1060 | btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); | |
ace68bac | 1061 | out_reserve: |
e570fd27 | 1062 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
79787eaa | 1063 | out_unlock: |
c2790a2e | 1064 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
151a41bc JB |
1065 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
1066 | EXTENT_DELALLOC | EXTENT_DEFRAG, | |
1067 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
1068 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); | |
79787eaa | 1069 | goto out; |
771ed689 | 1070 | } |
c8b97818 | 1071 | |
771ed689 CM |
1072 | /* |
1073 | * work queue call back to started compression on a file and pages | |
1074 | */ | |
1075 | static noinline void async_cow_start(struct btrfs_work *work) | |
1076 | { | |
1077 | struct async_cow *async_cow; | |
1078 | int num_added = 0; | |
1079 | async_cow = container_of(work, struct async_cow, work); | |
1080 | ||
1081 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1082 | async_cow->start, async_cow->end, async_cow, | |
1083 | &num_added); | |
8180ef88 | 1084 | if (num_added == 0) { |
cb77fcd8 | 1085 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1086 | async_cow->inode = NULL; |
8180ef88 | 1087 | } |
771ed689 CM |
1088 | } |
1089 | ||
1090 | /* | |
1091 | * work queue call back to submit previously compressed pages | |
1092 | */ | |
1093 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1094 | { | |
1095 | struct async_cow *async_cow; | |
1096 | struct btrfs_root *root; | |
1097 | unsigned long nr_pages; | |
1098 | ||
1099 | async_cow = container_of(work, struct async_cow, work); | |
1100 | ||
1101 | root = async_cow->root; | |
1102 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> | |
1103 | PAGE_CACHE_SHIFT; | |
1104 | ||
ee863954 DS |
1105 | /* |
1106 | * atomic_sub_return implies a barrier for waitqueue_active | |
1107 | */ | |
66657b31 | 1108 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
287082b0 | 1109 | 5 * 1024 * 1024 && |
771ed689 CM |
1110 | waitqueue_active(&root->fs_info->async_submit_wait)) |
1111 | wake_up(&root->fs_info->async_submit_wait); | |
1112 | ||
d397712b | 1113 | if (async_cow->inode) |
771ed689 | 1114 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1115 | } |
c8b97818 | 1116 | |
771ed689 CM |
1117 | static noinline void async_cow_free(struct btrfs_work *work) |
1118 | { | |
1119 | struct async_cow *async_cow; | |
1120 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1121 | if (async_cow->inode) |
cb77fcd8 | 1122 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1123 | kfree(async_cow); |
1124 | } | |
1125 | ||
1126 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1127 | u64 start, u64 end, int *page_started, | |
1128 | unsigned long *nr_written) | |
1129 | { | |
1130 | struct async_cow *async_cow; | |
1131 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1132 | unsigned long nr_pages; | |
1133 | u64 cur_end; | |
287082b0 | 1134 | int limit = 10 * 1024 * 1024; |
771ed689 | 1135 | |
a3429ab7 CM |
1136 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1137 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1138 | while (start < end) { |
771ed689 | 1139 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1140 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1141 | async_cow->inode = igrab(inode); |
771ed689 CM |
1142 | async_cow->root = root; |
1143 | async_cow->locked_page = locked_page; | |
1144 | async_cow->start = start; | |
1145 | ||
f79707b0 WS |
1146 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && |
1147 | !btrfs_test_opt(root, FORCE_COMPRESS)) | |
771ed689 CM |
1148 | cur_end = end; |
1149 | else | |
1150 | cur_end = min(end, start + 512 * 1024 - 1); | |
1151 | ||
1152 | async_cow->end = cur_end; | |
1153 | INIT_LIST_HEAD(&async_cow->extents); | |
1154 | ||
9e0af237 LB |
1155 | btrfs_init_work(&async_cow->work, |
1156 | btrfs_delalloc_helper, | |
1157 | async_cow_start, async_cow_submit, | |
1158 | async_cow_free); | |
771ed689 | 1159 | |
771ed689 CM |
1160 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
1161 | PAGE_CACHE_SHIFT; | |
1162 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); | |
1163 | ||
afe3d242 QW |
1164 | btrfs_queue_work(root->fs_info->delalloc_workers, |
1165 | &async_cow->work); | |
771ed689 CM |
1166 | |
1167 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { | |
1168 | wait_event(root->fs_info->async_submit_wait, | |
1169 | (atomic_read(&root->fs_info->async_delalloc_pages) < | |
1170 | limit)); | |
1171 | } | |
1172 | ||
d397712b | 1173 | while (atomic_read(&root->fs_info->async_submit_draining) && |
771ed689 CM |
1174 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
1175 | wait_event(root->fs_info->async_submit_wait, | |
1176 | (atomic_read(&root->fs_info->async_delalloc_pages) == | |
1177 | 0)); | |
1178 | } | |
1179 | ||
1180 | *nr_written += nr_pages; | |
1181 | start = cur_end + 1; | |
1182 | } | |
1183 | *page_started = 1; | |
1184 | return 0; | |
be20aa9d CM |
1185 | } |
1186 | ||
d397712b | 1187 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
17d217fe YZ |
1188 | u64 bytenr, u64 num_bytes) |
1189 | { | |
1190 | int ret; | |
1191 | struct btrfs_ordered_sum *sums; | |
1192 | LIST_HEAD(list); | |
1193 | ||
07d400a6 | 1194 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
a2de733c | 1195 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1196 | if (ret == 0 && list_empty(&list)) |
1197 | return 0; | |
1198 | ||
1199 | while (!list_empty(&list)) { | |
1200 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1201 | list_del(&sums->list); | |
1202 | kfree(sums); | |
1203 | } | |
1204 | return 1; | |
1205 | } | |
1206 | ||
d352ac68 CM |
1207 | /* |
1208 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1209 | * of the extents that exist in the file, and COWs the file as required. | |
1210 | * | |
1211 | * If no cow copies or snapshots exist, we write directly to the existing | |
1212 | * blocks on disk | |
1213 | */ | |
7f366cfe CM |
1214 | static noinline int run_delalloc_nocow(struct inode *inode, |
1215 | struct page *locked_page, | |
771ed689 CM |
1216 | u64 start, u64 end, int *page_started, int force, |
1217 | unsigned long *nr_written) | |
be20aa9d | 1218 | { |
be20aa9d | 1219 | struct btrfs_root *root = BTRFS_I(inode)->root; |
7ea394f1 | 1220 | struct btrfs_trans_handle *trans; |
be20aa9d | 1221 | struct extent_buffer *leaf; |
be20aa9d | 1222 | struct btrfs_path *path; |
80ff3856 | 1223 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1224 | struct btrfs_key found_key; |
80ff3856 YZ |
1225 | u64 cow_start; |
1226 | u64 cur_offset; | |
1227 | u64 extent_end; | |
5d4f98a2 | 1228 | u64 extent_offset; |
80ff3856 YZ |
1229 | u64 disk_bytenr; |
1230 | u64 num_bytes; | |
b4939680 | 1231 | u64 disk_num_bytes; |
cc95bef6 | 1232 | u64 ram_bytes; |
80ff3856 | 1233 | int extent_type; |
79787eaa | 1234 | int ret, err; |
d899e052 | 1235 | int type; |
80ff3856 YZ |
1236 | int nocow; |
1237 | int check_prev = 1; | |
82d5902d | 1238 | bool nolock; |
33345d01 | 1239 | u64 ino = btrfs_ino(inode); |
be20aa9d CM |
1240 | |
1241 | path = btrfs_alloc_path(); | |
17ca04af | 1242 | if (!path) { |
c2790a2e JB |
1243 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1244 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1245 | EXTENT_DO_ACCOUNTING | |
1246 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1247 | PAGE_CLEAR_DIRTY | |
1248 | PAGE_SET_WRITEBACK | | |
1249 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1250 | return -ENOMEM; |
17ca04af | 1251 | } |
82d5902d | 1252 | |
83eea1f1 | 1253 | nolock = btrfs_is_free_space_inode(inode); |
82d5902d LZ |
1254 | |
1255 | if (nolock) | |
7a7eaa40 | 1256 | trans = btrfs_join_transaction_nolock(root); |
82d5902d | 1257 | else |
7a7eaa40 | 1258 | trans = btrfs_join_transaction(root); |
ff5714cc | 1259 | |
79787eaa | 1260 | if (IS_ERR(trans)) { |
c2790a2e JB |
1261 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1262 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1263 | EXTENT_DO_ACCOUNTING | |
1264 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1265 | PAGE_CLEAR_DIRTY | |
1266 | PAGE_SET_WRITEBACK | | |
1267 | PAGE_END_WRITEBACK); | |
79787eaa JM |
1268 | btrfs_free_path(path); |
1269 | return PTR_ERR(trans); | |
1270 | } | |
1271 | ||
74b21075 | 1272 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
be20aa9d | 1273 | |
80ff3856 YZ |
1274 | cow_start = (u64)-1; |
1275 | cur_offset = start; | |
1276 | while (1) { | |
33345d01 | 1277 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
80ff3856 | 1278 | cur_offset, 0); |
d788a349 | 1279 | if (ret < 0) |
79787eaa | 1280 | goto error; |
80ff3856 YZ |
1281 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1282 | leaf = path->nodes[0]; | |
1283 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1284 | path->slots[0] - 1); | |
33345d01 | 1285 | if (found_key.objectid == ino && |
80ff3856 YZ |
1286 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1287 | path->slots[0]--; | |
1288 | } | |
1289 | check_prev = 0; | |
1290 | next_slot: | |
1291 | leaf = path->nodes[0]; | |
1292 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1293 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1294 | if (ret < 0) |
79787eaa | 1295 | goto error; |
80ff3856 YZ |
1296 | if (ret > 0) |
1297 | break; | |
1298 | leaf = path->nodes[0]; | |
1299 | } | |
be20aa9d | 1300 | |
80ff3856 YZ |
1301 | nocow = 0; |
1302 | disk_bytenr = 0; | |
17d217fe | 1303 | num_bytes = 0; |
80ff3856 YZ |
1304 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1305 | ||
1d512cb7 FM |
1306 | if (found_key.objectid > ino) |
1307 | break; | |
1308 | if (WARN_ON_ONCE(found_key.objectid < ino) || | |
1309 | found_key.type < BTRFS_EXTENT_DATA_KEY) { | |
1310 | path->slots[0]++; | |
1311 | goto next_slot; | |
1312 | } | |
1313 | if (found_key.type > BTRFS_EXTENT_DATA_KEY || | |
80ff3856 YZ |
1314 | found_key.offset > end) |
1315 | break; | |
1316 | ||
1317 | if (found_key.offset > cur_offset) { | |
1318 | extent_end = found_key.offset; | |
e9061e21 | 1319 | extent_type = 0; |
80ff3856 YZ |
1320 | goto out_check; |
1321 | } | |
1322 | ||
1323 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1324 | struct btrfs_file_extent_item); | |
1325 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1326 | ||
cc95bef6 | 1327 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1328 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1329 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1330 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1331 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1332 | extent_end = found_key.offset + |
1333 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1334 | disk_num_bytes = |
1335 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1336 | if (extent_end <= start) { |
1337 | path->slots[0]++; | |
1338 | goto next_slot; | |
1339 | } | |
17d217fe YZ |
1340 | if (disk_bytenr == 0) |
1341 | goto out_check; | |
80ff3856 YZ |
1342 | if (btrfs_file_extent_compression(leaf, fi) || |
1343 | btrfs_file_extent_encryption(leaf, fi) || | |
1344 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1345 | goto out_check; | |
d899e052 YZ |
1346 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1347 | goto out_check; | |
d2fb3437 | 1348 | if (btrfs_extent_readonly(root, disk_bytenr)) |
80ff3856 | 1349 | goto out_check; |
33345d01 | 1350 | if (btrfs_cross_ref_exist(trans, root, ino, |
5d4f98a2 YZ |
1351 | found_key.offset - |
1352 | extent_offset, disk_bytenr)) | |
17d217fe | 1353 | goto out_check; |
5d4f98a2 | 1354 | disk_bytenr += extent_offset; |
17d217fe YZ |
1355 | disk_bytenr += cur_offset - found_key.offset; |
1356 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
e9894fd3 WS |
1357 | /* |
1358 | * if there are pending snapshots for this root, | |
1359 | * we fall into common COW way. | |
1360 | */ | |
1361 | if (!nolock) { | |
9ea24bbe | 1362 | err = btrfs_start_write_no_snapshoting(root); |
e9894fd3 WS |
1363 | if (!err) |
1364 | goto out_check; | |
1365 | } | |
17d217fe YZ |
1366 | /* |
1367 | * force cow if csum exists in the range. | |
1368 | * this ensure that csum for a given extent are | |
1369 | * either valid or do not exist. | |
1370 | */ | |
1371 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) | |
1372 | goto out_check; | |
80ff3856 YZ |
1373 | nocow = 1; |
1374 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1375 | extent_end = found_key.offset + | |
514ac8ad CM |
1376 | btrfs_file_extent_inline_len(leaf, |
1377 | path->slots[0], fi); | |
80ff3856 YZ |
1378 | extent_end = ALIGN(extent_end, root->sectorsize); |
1379 | } else { | |
1380 | BUG_ON(1); | |
1381 | } | |
1382 | out_check: | |
1383 | if (extent_end <= start) { | |
1384 | path->slots[0]++; | |
e9894fd3 | 1385 | if (!nolock && nocow) |
9ea24bbe | 1386 | btrfs_end_write_no_snapshoting(root); |
80ff3856 YZ |
1387 | goto next_slot; |
1388 | } | |
1389 | if (!nocow) { | |
1390 | if (cow_start == (u64)-1) | |
1391 | cow_start = cur_offset; | |
1392 | cur_offset = extent_end; | |
1393 | if (cur_offset > end) | |
1394 | break; | |
1395 | path->slots[0]++; | |
1396 | goto next_slot; | |
7ea394f1 YZ |
1397 | } |
1398 | ||
b3b4aa74 | 1399 | btrfs_release_path(path); |
80ff3856 | 1400 | if (cow_start != (u64)-1) { |
00361589 JB |
1401 | ret = cow_file_range(inode, locked_page, |
1402 | cow_start, found_key.offset - 1, | |
1403 | page_started, nr_written, 1); | |
e9894fd3 WS |
1404 | if (ret) { |
1405 | if (!nolock && nocow) | |
9ea24bbe | 1406 | btrfs_end_write_no_snapshoting(root); |
79787eaa | 1407 | goto error; |
e9894fd3 | 1408 | } |
80ff3856 | 1409 | cow_start = (u64)-1; |
7ea394f1 | 1410 | } |
80ff3856 | 1411 | |
d899e052 YZ |
1412 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
1413 | struct extent_map *em; | |
1414 | struct extent_map_tree *em_tree; | |
1415 | em_tree = &BTRFS_I(inode)->extent_tree; | |
172ddd60 | 1416 | em = alloc_extent_map(); |
79787eaa | 1417 | BUG_ON(!em); /* -ENOMEM */ |
d899e052 | 1418 | em->start = cur_offset; |
70c8a91c | 1419 | em->orig_start = found_key.offset - extent_offset; |
d899e052 YZ |
1420 | em->len = num_bytes; |
1421 | em->block_len = num_bytes; | |
1422 | em->block_start = disk_bytenr; | |
b4939680 | 1423 | em->orig_block_len = disk_num_bytes; |
cc95bef6 | 1424 | em->ram_bytes = ram_bytes; |
d899e052 | 1425 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
2ab28f32 JB |
1426 | em->mod_start = em->start; |
1427 | em->mod_len = em->len; | |
d899e052 | 1428 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
b11e234d | 1429 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
70c8a91c | 1430 | em->generation = -1; |
d899e052 | 1431 | while (1) { |
890871be | 1432 | write_lock(&em_tree->lock); |
09a2a8f9 | 1433 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1434 | write_unlock(&em_tree->lock); |
d899e052 YZ |
1435 | if (ret != -EEXIST) { |
1436 | free_extent_map(em); | |
1437 | break; | |
1438 | } | |
1439 | btrfs_drop_extent_cache(inode, em->start, | |
1440 | em->start + em->len - 1, 0); | |
1441 | } | |
1442 | type = BTRFS_ORDERED_PREALLOC; | |
1443 | } else { | |
1444 | type = BTRFS_ORDERED_NOCOW; | |
1445 | } | |
80ff3856 YZ |
1446 | |
1447 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1448 | num_bytes, num_bytes, type); |
79787eaa | 1449 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1450 | |
efa56464 YZ |
1451 | if (root->root_key.objectid == |
1452 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1453 | ret = btrfs_reloc_clone_csums(inode, cur_offset, | |
1454 | num_bytes); | |
e9894fd3 WS |
1455 | if (ret) { |
1456 | if (!nolock && nocow) | |
9ea24bbe | 1457 | btrfs_end_write_no_snapshoting(root); |
79787eaa | 1458 | goto error; |
e9894fd3 | 1459 | } |
efa56464 YZ |
1460 | } |
1461 | ||
c2790a2e JB |
1462 | extent_clear_unlock_delalloc(inode, cur_offset, |
1463 | cur_offset + num_bytes - 1, | |
1464 | locked_page, EXTENT_LOCKED | | |
1465 | EXTENT_DELALLOC, PAGE_UNLOCK | | |
1466 | PAGE_SET_PRIVATE2); | |
e9894fd3 | 1467 | if (!nolock && nocow) |
9ea24bbe | 1468 | btrfs_end_write_no_snapshoting(root); |
80ff3856 YZ |
1469 | cur_offset = extent_end; |
1470 | if (cur_offset > end) | |
1471 | break; | |
be20aa9d | 1472 | } |
b3b4aa74 | 1473 | btrfs_release_path(path); |
80ff3856 | 1474 | |
17ca04af | 1475 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1476 | cow_start = cur_offset; |
17ca04af JB |
1477 | cur_offset = end; |
1478 | } | |
1479 | ||
80ff3856 | 1480 | if (cow_start != (u64)-1) { |
00361589 JB |
1481 | ret = cow_file_range(inode, locked_page, cow_start, end, |
1482 | page_started, nr_written, 1); | |
d788a349 | 1483 | if (ret) |
79787eaa | 1484 | goto error; |
80ff3856 YZ |
1485 | } |
1486 | ||
79787eaa | 1487 | error: |
a698d075 | 1488 | err = btrfs_end_transaction(trans, root); |
79787eaa JM |
1489 | if (!ret) |
1490 | ret = err; | |
1491 | ||
17ca04af | 1492 | if (ret && cur_offset < end) |
c2790a2e JB |
1493 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
1494 | locked_page, EXTENT_LOCKED | | |
151a41bc JB |
1495 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1496 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1497 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1498 | PAGE_SET_WRITEBACK | |
1499 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1500 | btrfs_free_path(path); |
79787eaa | 1501 | return ret; |
be20aa9d CM |
1502 | } |
1503 | ||
47059d93 WS |
1504 | static inline int need_force_cow(struct inode *inode, u64 start, u64 end) |
1505 | { | |
1506 | ||
1507 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
1508 | !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)) | |
1509 | return 0; | |
1510 | ||
1511 | /* | |
1512 | * @defrag_bytes is a hint value, no spinlock held here, | |
1513 | * if is not zero, it means the file is defragging. | |
1514 | * Force cow if given extent needs to be defragged. | |
1515 | */ | |
1516 | if (BTRFS_I(inode)->defrag_bytes && | |
1517 | test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1518 | EXTENT_DEFRAG, 0, NULL)) | |
1519 | return 1; | |
1520 | ||
1521 | return 0; | |
1522 | } | |
1523 | ||
d352ac68 CM |
1524 | /* |
1525 | * extent_io.c call back to do delayed allocation processing | |
1526 | */ | |
c8b97818 | 1527 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
771ed689 CM |
1528 | u64 start, u64 end, int *page_started, |
1529 | unsigned long *nr_written) | |
be20aa9d | 1530 | { |
be20aa9d | 1531 | int ret; |
47059d93 | 1532 | int force_cow = need_force_cow(inode, start, end); |
a2135011 | 1533 | |
47059d93 | 1534 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) { |
c8b97818 | 1535 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1536 | page_started, 1, nr_written); |
47059d93 | 1537 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { |
d899e052 | 1538 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1539 | page_started, 0, nr_written); |
7816030e | 1540 | } else if (!inode_need_compress(inode)) { |
7f366cfe CM |
1541 | ret = cow_file_range(inode, locked_page, start, end, |
1542 | page_started, nr_written, 1); | |
7ddf5a42 JB |
1543 | } else { |
1544 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1545 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1546 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1547 | page_started, nr_written); |
7ddf5a42 | 1548 | } |
b888db2b CM |
1549 | return ret; |
1550 | } | |
1551 | ||
1bf85046 JM |
1552 | static void btrfs_split_extent_hook(struct inode *inode, |
1553 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1554 | { |
dcab6a3b JB |
1555 | u64 size; |
1556 | ||
0ca1f7ce | 1557 | /* not delalloc, ignore it */ |
9ed74f2d | 1558 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1559 | return; |
9ed74f2d | 1560 | |
dcab6a3b JB |
1561 | size = orig->end - orig->start + 1; |
1562 | if (size > BTRFS_MAX_EXTENT_SIZE) { | |
1563 | u64 num_extents; | |
1564 | u64 new_size; | |
1565 | ||
1566 | /* | |
ba117213 JB |
1567 | * See the explanation in btrfs_merge_extent_hook, the same |
1568 | * applies here, just in reverse. | |
dcab6a3b JB |
1569 | */ |
1570 | new_size = orig->end - split + 1; | |
ba117213 | 1571 | num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
dcab6a3b | 1572 | BTRFS_MAX_EXTENT_SIZE); |
ba117213 JB |
1573 | new_size = split - orig->start; |
1574 | num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1575 | BTRFS_MAX_EXTENT_SIZE); | |
1576 | if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1577 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) | |
dcab6a3b JB |
1578 | return; |
1579 | } | |
1580 | ||
9e0baf60 JB |
1581 | spin_lock(&BTRFS_I(inode)->lock); |
1582 | BTRFS_I(inode)->outstanding_extents++; | |
1583 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1584 | } |
1585 | ||
1586 | /* | |
1587 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1588 | * extents so we can keep track of new extents that are just merged onto old | |
1589 | * extents, such as when we are doing sequential writes, so we can properly | |
1590 | * account for the metadata space we'll need. | |
1591 | */ | |
1bf85046 JM |
1592 | static void btrfs_merge_extent_hook(struct inode *inode, |
1593 | struct extent_state *new, | |
1594 | struct extent_state *other) | |
9ed74f2d | 1595 | { |
dcab6a3b JB |
1596 | u64 new_size, old_size; |
1597 | u64 num_extents; | |
1598 | ||
9ed74f2d JB |
1599 | /* not delalloc, ignore it */ |
1600 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1601 | return; |
9ed74f2d | 1602 | |
8461a3de JB |
1603 | if (new->start > other->start) |
1604 | new_size = new->end - other->start + 1; | |
1605 | else | |
1606 | new_size = other->end - new->start + 1; | |
dcab6a3b JB |
1607 | |
1608 | /* we're not bigger than the max, unreserve the space and go */ | |
1609 | if (new_size <= BTRFS_MAX_EXTENT_SIZE) { | |
1610 | spin_lock(&BTRFS_I(inode)->lock); | |
1611 | BTRFS_I(inode)->outstanding_extents--; | |
1612 | spin_unlock(&BTRFS_I(inode)->lock); | |
1613 | return; | |
1614 | } | |
1615 | ||
1616 | /* | |
ba117213 JB |
1617 | * We have to add up either side to figure out how many extents were |
1618 | * accounted for before we merged into one big extent. If the number of | |
1619 | * extents we accounted for is <= the amount we need for the new range | |
1620 | * then we can return, otherwise drop. Think of it like this | |
1621 | * | |
1622 | * [ 4k][MAX_SIZE] | |
1623 | * | |
1624 | * So we've grown the extent by a MAX_SIZE extent, this would mean we | |
1625 | * need 2 outstanding extents, on one side we have 1 and the other side | |
1626 | * we have 1 so they are == and we can return. But in this case | |
1627 | * | |
1628 | * [MAX_SIZE+4k][MAX_SIZE+4k] | |
1629 | * | |
1630 | * Each range on their own accounts for 2 extents, but merged together | |
1631 | * they are only 3 extents worth of accounting, so we need to drop in | |
1632 | * this case. | |
dcab6a3b | 1633 | */ |
ba117213 | 1634 | old_size = other->end - other->start + 1; |
dcab6a3b JB |
1635 | num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, |
1636 | BTRFS_MAX_EXTENT_SIZE); | |
ba117213 JB |
1637 | old_size = new->end - new->start + 1; |
1638 | num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, | |
1639 | BTRFS_MAX_EXTENT_SIZE); | |
1640 | ||
dcab6a3b | 1641 | if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, |
ba117213 | 1642 | BTRFS_MAX_EXTENT_SIZE) >= num_extents) |
dcab6a3b JB |
1643 | return; |
1644 | ||
9e0baf60 JB |
1645 | spin_lock(&BTRFS_I(inode)->lock); |
1646 | BTRFS_I(inode)->outstanding_extents--; | |
1647 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1648 | } |
1649 | ||
eb73c1b7 MX |
1650 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1651 | struct inode *inode) | |
1652 | { | |
1653 | spin_lock(&root->delalloc_lock); | |
1654 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1655 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1656 | &root->delalloc_inodes); | |
1657 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1658 | &BTRFS_I(inode)->runtime_flags); | |
1659 | root->nr_delalloc_inodes++; | |
1660 | if (root->nr_delalloc_inodes == 1) { | |
1661 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1662 | BUG_ON(!list_empty(&root->delalloc_root)); | |
1663 | list_add_tail(&root->delalloc_root, | |
1664 | &root->fs_info->delalloc_roots); | |
1665 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1666 | } | |
1667 | } | |
1668 | spin_unlock(&root->delalloc_lock); | |
1669 | } | |
1670 | ||
1671 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1672 | struct inode *inode) | |
1673 | { | |
1674 | spin_lock(&root->delalloc_lock); | |
1675 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1676 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); | |
1677 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1678 | &BTRFS_I(inode)->runtime_flags); | |
1679 | root->nr_delalloc_inodes--; | |
1680 | if (!root->nr_delalloc_inodes) { | |
1681 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1682 | BUG_ON(list_empty(&root->delalloc_root)); | |
1683 | list_del_init(&root->delalloc_root); | |
1684 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1685 | } | |
1686 | } | |
1687 | spin_unlock(&root->delalloc_lock); | |
1688 | } | |
1689 | ||
d352ac68 CM |
1690 | /* |
1691 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1692 | * bytes in this file, and to maintain the list of inodes that | |
1693 | * have pending delalloc work to be done. | |
1694 | */ | |
1bf85046 | 1695 | static void btrfs_set_bit_hook(struct inode *inode, |
9ee49a04 | 1696 | struct extent_state *state, unsigned *bits) |
291d673e | 1697 | { |
9ed74f2d | 1698 | |
47059d93 WS |
1699 | if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) |
1700 | WARN_ON(1); | |
75eff68e CM |
1701 | /* |
1702 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1703 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1704 | * bit, which is only set or cleared with irqs on |
1705 | */ | |
0ca1f7ce | 1706 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1707 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1708 | u64 len = state->end + 1 - state->start; |
83eea1f1 | 1709 | bool do_list = !btrfs_is_free_space_inode(inode); |
9ed74f2d | 1710 | |
9e0baf60 | 1711 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1712 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1713 | } else { |
1714 | spin_lock(&BTRFS_I(inode)->lock); | |
1715 | BTRFS_I(inode)->outstanding_extents++; | |
1716 | spin_unlock(&BTRFS_I(inode)->lock); | |
1717 | } | |
287a0ab9 | 1718 | |
6a3891c5 JB |
1719 | /* For sanity tests */ |
1720 | if (btrfs_test_is_dummy_root(root)) | |
1721 | return; | |
1722 | ||
963d678b MX |
1723 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
1724 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1725 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1726 | BTRFS_I(inode)->delalloc_bytes += len; |
47059d93 WS |
1727 | if (*bits & EXTENT_DEFRAG) |
1728 | BTRFS_I(inode)->defrag_bytes += len; | |
df0af1a5 | 1729 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1730 | &BTRFS_I(inode)->runtime_flags)) |
1731 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1732 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1733 | } |
291d673e CM |
1734 | } |
1735 | ||
d352ac68 CM |
1736 | /* |
1737 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1738 | */ | |
1bf85046 | 1739 | static void btrfs_clear_bit_hook(struct inode *inode, |
41074888 | 1740 | struct extent_state *state, |
9ee49a04 | 1741 | unsigned *bits) |
291d673e | 1742 | { |
47059d93 | 1743 | u64 len = state->end + 1 - state->start; |
dcab6a3b JB |
1744 | u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, |
1745 | BTRFS_MAX_EXTENT_SIZE); | |
47059d93 WS |
1746 | |
1747 | spin_lock(&BTRFS_I(inode)->lock); | |
1748 | if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) | |
1749 | BTRFS_I(inode)->defrag_bytes -= len; | |
1750 | spin_unlock(&BTRFS_I(inode)->lock); | |
1751 | ||
75eff68e CM |
1752 | /* |
1753 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1754 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1755 | * bit, which is only set or cleared with irqs on |
1756 | */ | |
0ca1f7ce | 1757 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1758 | struct btrfs_root *root = BTRFS_I(inode)->root; |
83eea1f1 | 1759 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1760 | |
9e0baf60 | 1761 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1762 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1763 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
1764 | spin_lock(&BTRFS_I(inode)->lock); | |
dcab6a3b | 1765 | BTRFS_I(inode)->outstanding_extents -= num_extents; |
9e0baf60 JB |
1766 | spin_unlock(&BTRFS_I(inode)->lock); |
1767 | } | |
0ca1f7ce | 1768 | |
b6d08f06 JB |
1769 | /* |
1770 | * We don't reserve metadata space for space cache inodes so we | |
1771 | * don't need to call dellalloc_release_metadata if there is an | |
1772 | * error. | |
1773 | */ | |
1774 | if (*bits & EXTENT_DO_ACCOUNTING && | |
1775 | root != root->fs_info->tree_root) | |
0ca1f7ce YZ |
1776 | btrfs_delalloc_release_metadata(inode, len); |
1777 | ||
6a3891c5 JB |
1778 | /* For sanity tests. */ |
1779 | if (btrfs_test_is_dummy_root(root)) | |
1780 | return; | |
1781 | ||
0cb59c99 | 1782 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
7ee9e440 | 1783 | && do_list && !(state->state & EXTENT_NORESERVE)) |
51773bec QW |
1784 | btrfs_free_reserved_data_space_noquota(inode, |
1785 | state->start, len); | |
9ed74f2d | 1786 | |
963d678b MX |
1787 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
1788 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1789 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1790 | BTRFS_I(inode)->delalloc_bytes -= len; |
0cb59c99 | 1791 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
df0af1a5 | 1792 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1793 | &BTRFS_I(inode)->runtime_flags)) |
1794 | btrfs_del_delalloc_inode(root, inode); | |
df0af1a5 | 1795 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1796 | } |
291d673e CM |
1797 | } |
1798 | ||
d352ac68 CM |
1799 | /* |
1800 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1801 | * we don't create bios that span stripes or chunks | |
1802 | */ | |
64a16701 | 1803 | int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset, |
c8b97818 CM |
1804 | size_t size, struct bio *bio, |
1805 | unsigned long bio_flags) | |
239b14b3 CM |
1806 | { |
1807 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
4f024f37 | 1808 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
239b14b3 CM |
1809 | u64 length = 0; |
1810 | u64 map_length; | |
239b14b3 CM |
1811 | int ret; |
1812 | ||
771ed689 CM |
1813 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1814 | return 0; | |
1815 | ||
4f024f37 | 1816 | length = bio->bi_iter.bi_size; |
239b14b3 | 1817 | map_length = length; |
64a16701 | 1818 | ret = btrfs_map_block(root->fs_info, rw, logical, |
f188591e | 1819 | &map_length, NULL, 0); |
3ec706c8 | 1820 | /* Will always return 0 with map_multi == NULL */ |
3444a972 | 1821 | BUG_ON(ret < 0); |
d397712b | 1822 | if (map_length < length + size) |
239b14b3 | 1823 | return 1; |
3444a972 | 1824 | return 0; |
239b14b3 CM |
1825 | } |
1826 | ||
d352ac68 CM |
1827 | /* |
1828 | * in order to insert checksums into the metadata in large chunks, | |
1829 | * we wait until bio submission time. All the pages in the bio are | |
1830 | * checksummed and sums are attached onto the ordered extent record. | |
1831 | * | |
1832 | * At IO completion time the cums attached on the ordered extent record | |
1833 | * are inserted into the btree | |
1834 | */ | |
d397712b CM |
1835 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
1836 | struct bio *bio, int mirror_num, | |
eaf25d93 CM |
1837 | unsigned long bio_flags, |
1838 | u64 bio_offset) | |
065631f6 | 1839 | { |
065631f6 | 1840 | struct btrfs_root *root = BTRFS_I(inode)->root; |
065631f6 | 1841 | int ret = 0; |
e015640f | 1842 | |
d20f7043 | 1843 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
79787eaa | 1844 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1845 | return 0; |
1846 | } | |
e015640f | 1847 | |
4a69a410 CM |
1848 | /* |
1849 | * in order to insert checksums into the metadata in large chunks, | |
1850 | * we wait until bio submission time. All the pages in the bio are | |
1851 | * checksummed and sums are attached onto the ordered extent record. | |
1852 | * | |
1853 | * At IO completion time the cums attached on the ordered extent record | |
1854 | * are inserted into the btree | |
1855 | */ | |
b2950863 | 1856 | static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1857 | int mirror_num, unsigned long bio_flags, |
1858 | u64 bio_offset) | |
4a69a410 CM |
1859 | { |
1860 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
61891923 SB |
1861 | int ret; |
1862 | ||
1863 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); | |
4246a0b6 CH |
1864 | if (ret) { |
1865 | bio->bi_error = ret; | |
1866 | bio_endio(bio); | |
1867 | } | |
61891923 | 1868 | return ret; |
44b8bd7e CM |
1869 | } |
1870 | ||
d352ac68 | 1871 | /* |
cad321ad CM |
1872 | * extent_io.c submission hook. This does the right thing for csum calculation |
1873 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1874 | */ |
b2950863 | 1875 | static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1876 | int mirror_num, unsigned long bio_flags, |
1877 | u64 bio_offset) | |
44b8bd7e CM |
1878 | { |
1879 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
0d51e28a | 1880 | enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; |
44b8bd7e | 1881 | int ret = 0; |
19b9bdb0 | 1882 | int skip_sum; |
b812ce28 | 1883 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1884 | |
6cbff00f | 1885 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1886 | |
83eea1f1 | 1887 | if (btrfs_is_free_space_inode(inode)) |
0d51e28a | 1888 | metadata = BTRFS_WQ_ENDIO_FREE_SPACE; |
0417341e | 1889 | |
7b6d91da | 1890 | if (!(rw & REQ_WRITE)) { |
5fd02043 JB |
1891 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
1892 | if (ret) | |
61891923 | 1893 | goto out; |
5fd02043 | 1894 | |
d20f7043 | 1895 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1896 | ret = btrfs_submit_compressed_read(inode, bio, |
1897 | mirror_num, | |
1898 | bio_flags); | |
1899 | goto out; | |
c2db1073 TI |
1900 | } else if (!skip_sum) { |
1901 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); | |
1902 | if (ret) | |
61891923 | 1903 | goto out; |
c2db1073 | 1904 | } |
4d1b5fb4 | 1905 | goto mapit; |
b812ce28 | 1906 | } else if (async && !skip_sum) { |
17d217fe YZ |
1907 | /* csum items have already been cloned */ |
1908 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1909 | goto mapit; | |
19b9bdb0 | 1910 | /* we're doing a write, do the async checksumming */ |
61891923 | 1911 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
44b8bd7e | 1912 | inode, rw, bio, mirror_num, |
eaf25d93 CM |
1913 | bio_flags, bio_offset, |
1914 | __btrfs_submit_bio_start, | |
4a69a410 | 1915 | __btrfs_submit_bio_done); |
61891923 | 1916 | goto out; |
b812ce28 JB |
1917 | } else if (!skip_sum) { |
1918 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); | |
1919 | if (ret) | |
1920 | goto out; | |
19b9bdb0 CM |
1921 | } |
1922 | ||
0b86a832 | 1923 | mapit: |
61891923 SB |
1924 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
1925 | ||
1926 | out: | |
4246a0b6 CH |
1927 | if (ret < 0) { |
1928 | bio->bi_error = ret; | |
1929 | bio_endio(bio); | |
1930 | } | |
61891923 | 1931 | return ret; |
065631f6 | 1932 | } |
6885f308 | 1933 | |
d352ac68 CM |
1934 | /* |
1935 | * given a list of ordered sums record them in the inode. This happens | |
1936 | * at IO completion time based on sums calculated at bio submission time. | |
1937 | */ | |
ba1da2f4 | 1938 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
1939 | struct inode *inode, u64 file_offset, |
1940 | struct list_head *list) | |
1941 | { | |
e6dcd2dc CM |
1942 | struct btrfs_ordered_sum *sum; |
1943 | ||
c6e30871 | 1944 | list_for_each_entry(sum, list, list) { |
39847c4d | 1945 | trans->adding_csums = 1; |
d20f7043 CM |
1946 | btrfs_csum_file_blocks(trans, |
1947 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 1948 | trans->adding_csums = 0; |
e6dcd2dc CM |
1949 | } |
1950 | return 0; | |
1951 | } | |
1952 | ||
2ac55d41 JB |
1953 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
1954 | struct extent_state **cached_state) | |
ea8c2819 | 1955 | { |
6c1500f2 | 1956 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
ea8c2819 | 1957 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
2ac55d41 | 1958 | cached_state, GFP_NOFS); |
ea8c2819 CM |
1959 | } |
1960 | ||
d352ac68 | 1961 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
1962 | struct btrfs_writepage_fixup { |
1963 | struct page *page; | |
1964 | struct btrfs_work work; | |
1965 | }; | |
1966 | ||
b2950863 | 1967 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
1968 | { |
1969 | struct btrfs_writepage_fixup *fixup; | |
1970 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 1971 | struct extent_state *cached_state = NULL; |
247e743c CM |
1972 | struct page *page; |
1973 | struct inode *inode; | |
1974 | u64 page_start; | |
1975 | u64 page_end; | |
87826df0 | 1976 | int ret; |
247e743c CM |
1977 | |
1978 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
1979 | page = fixup->page; | |
4a096752 | 1980 | again: |
247e743c CM |
1981 | lock_page(page); |
1982 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
1983 | ClearPageChecked(page); | |
1984 | goto out_page; | |
1985 | } | |
1986 | ||
1987 | inode = page->mapping->host; | |
1988 | page_start = page_offset(page); | |
1989 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; | |
1990 | ||
2ac55d41 | 1991 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
d0082371 | 1992 | &cached_state); |
4a096752 CM |
1993 | |
1994 | /* already ordered? We're done */ | |
8b62b72b | 1995 | if (PagePrivate2(page)) |
247e743c | 1996 | goto out; |
4a096752 CM |
1997 | |
1998 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
1999 | if (ordered) { | |
2ac55d41 JB |
2000 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
2001 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
2002 | unlock_page(page); |
2003 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 2004 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
2005 | goto again; |
2006 | } | |
247e743c | 2007 | |
7cf5b976 QW |
2008 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
2009 | PAGE_CACHE_SIZE); | |
87826df0 JM |
2010 | if (ret) { |
2011 | mapping_set_error(page->mapping, ret); | |
2012 | end_extent_writepage(page, ret, page_start, page_end); | |
2013 | ClearPageChecked(page); | |
2014 | goto out; | |
2015 | } | |
2016 | ||
2ac55d41 | 2017 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
247e743c | 2018 | ClearPageChecked(page); |
87826df0 | 2019 | set_page_dirty(page); |
247e743c | 2020 | out: |
2ac55d41 JB |
2021 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
2022 | &cached_state, GFP_NOFS); | |
247e743c CM |
2023 | out_page: |
2024 | unlock_page(page); | |
2025 | page_cache_release(page); | |
b897abec | 2026 | kfree(fixup); |
247e743c CM |
2027 | } |
2028 | ||
2029 | /* | |
2030 | * There are a few paths in the higher layers of the kernel that directly | |
2031 | * set the page dirty bit without asking the filesystem if it is a | |
2032 | * good idea. This causes problems because we want to make sure COW | |
2033 | * properly happens and the data=ordered rules are followed. | |
2034 | * | |
c8b97818 | 2035 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
2036 | * hasn't been properly setup for IO. We kick off an async process |
2037 | * to fix it up. The async helper will wait for ordered extents, set | |
2038 | * the delalloc bit and make it safe to write the page. | |
2039 | */ | |
b2950863 | 2040 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
2041 | { |
2042 | struct inode *inode = page->mapping->host; | |
2043 | struct btrfs_writepage_fixup *fixup; | |
2044 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
247e743c | 2045 | |
8b62b72b CM |
2046 | /* this page is properly in the ordered list */ |
2047 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
2048 | return 0; |
2049 | ||
2050 | if (PageChecked(page)) | |
2051 | return -EAGAIN; | |
2052 | ||
2053 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
2054 | if (!fixup) | |
2055 | return -EAGAIN; | |
f421950f | 2056 | |
247e743c CM |
2057 | SetPageChecked(page); |
2058 | page_cache_get(page); | |
9e0af237 LB |
2059 | btrfs_init_work(&fixup->work, btrfs_fixup_helper, |
2060 | btrfs_writepage_fixup_worker, NULL, NULL); | |
247e743c | 2061 | fixup->page = page; |
dc6e3209 | 2062 | btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); |
87826df0 | 2063 | return -EBUSY; |
247e743c CM |
2064 | } |
2065 | ||
d899e052 YZ |
2066 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
2067 | struct inode *inode, u64 file_pos, | |
2068 | u64 disk_bytenr, u64 disk_num_bytes, | |
2069 | u64 num_bytes, u64 ram_bytes, | |
2070 | u8 compression, u8 encryption, | |
2071 | u16 other_encoding, int extent_type) | |
2072 | { | |
2073 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2074 | struct btrfs_file_extent_item *fi; | |
2075 | struct btrfs_path *path; | |
2076 | struct extent_buffer *leaf; | |
2077 | struct btrfs_key ins; | |
1acae57b | 2078 | int extent_inserted = 0; |
d899e052 YZ |
2079 | int ret; |
2080 | ||
2081 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
2082 | if (!path) |
2083 | return -ENOMEM; | |
d899e052 | 2084 | |
a1ed835e CM |
2085 | /* |
2086 | * we may be replacing one extent in the tree with another. | |
2087 | * The new extent is pinned in the extent map, and we don't want | |
2088 | * to drop it from the cache until it is completely in the btree. | |
2089 | * | |
2090 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
2091 | * the caller is expected to unpin it and allow it to be merged | |
2092 | * with the others. | |
2093 | */ | |
1acae57b FDBM |
2094 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
2095 | file_pos + num_bytes, NULL, 0, | |
2096 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
2097 | if (ret) |
2098 | goto out; | |
d899e052 | 2099 | |
1acae57b FDBM |
2100 | if (!extent_inserted) { |
2101 | ins.objectid = btrfs_ino(inode); | |
2102 | ins.offset = file_pos; | |
2103 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
2104 | ||
2105 | path->leave_spinning = 1; | |
2106 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
2107 | sizeof(*fi)); | |
2108 | if (ret) | |
2109 | goto out; | |
2110 | } | |
d899e052 YZ |
2111 | leaf = path->nodes[0]; |
2112 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2113 | struct btrfs_file_extent_item); | |
2114 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
2115 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
2116 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
2117 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
2118 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
2119 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
2120 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
2121 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
2122 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
2123 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 2124 | |
d899e052 | 2125 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 2126 | btrfs_release_path(path); |
d899e052 YZ |
2127 | |
2128 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
2129 | |
2130 | ins.objectid = disk_bytenr; | |
2131 | ins.offset = disk_num_bytes; | |
2132 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 YZ |
2133 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
2134 | root->root_key.objectid, | |
5846a3c2 QW |
2135 | btrfs_ino(inode), file_pos, |
2136 | ram_bytes, &ins); | |
297d750b | 2137 | /* |
5846a3c2 QW |
2138 | * Release the reserved range from inode dirty range map, as it is |
2139 | * already moved into delayed_ref_head | |
297d750b QW |
2140 | */ |
2141 | btrfs_qgroup_release_data(inode, file_pos, ram_bytes); | |
79787eaa | 2142 | out: |
d899e052 | 2143 | btrfs_free_path(path); |
b9473439 | 2144 | |
79787eaa | 2145 | return ret; |
d899e052 YZ |
2146 | } |
2147 | ||
38c227d8 LB |
2148 | /* snapshot-aware defrag */ |
2149 | struct sa_defrag_extent_backref { | |
2150 | struct rb_node node; | |
2151 | struct old_sa_defrag_extent *old; | |
2152 | u64 root_id; | |
2153 | u64 inum; | |
2154 | u64 file_pos; | |
2155 | u64 extent_offset; | |
2156 | u64 num_bytes; | |
2157 | u64 generation; | |
2158 | }; | |
2159 | ||
2160 | struct old_sa_defrag_extent { | |
2161 | struct list_head list; | |
2162 | struct new_sa_defrag_extent *new; | |
2163 | ||
2164 | u64 extent_offset; | |
2165 | u64 bytenr; | |
2166 | u64 offset; | |
2167 | u64 len; | |
2168 | int count; | |
2169 | }; | |
2170 | ||
2171 | struct new_sa_defrag_extent { | |
2172 | struct rb_root root; | |
2173 | struct list_head head; | |
2174 | struct btrfs_path *path; | |
2175 | struct inode *inode; | |
2176 | u64 file_pos; | |
2177 | u64 len; | |
2178 | u64 bytenr; | |
2179 | u64 disk_len; | |
2180 | u8 compress_type; | |
2181 | }; | |
2182 | ||
2183 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
2184 | struct sa_defrag_extent_backref *b2) | |
2185 | { | |
2186 | if (b1->root_id < b2->root_id) | |
2187 | return -1; | |
2188 | else if (b1->root_id > b2->root_id) | |
2189 | return 1; | |
2190 | ||
2191 | if (b1->inum < b2->inum) | |
2192 | return -1; | |
2193 | else if (b1->inum > b2->inum) | |
2194 | return 1; | |
2195 | ||
2196 | if (b1->file_pos < b2->file_pos) | |
2197 | return -1; | |
2198 | else if (b1->file_pos > b2->file_pos) | |
2199 | return 1; | |
2200 | ||
2201 | /* | |
2202 | * [------------------------------] ===> (a range of space) | |
2203 | * |<--->| |<---->| =============> (fs/file tree A) | |
2204 | * |<---------------------------->| ===> (fs/file tree B) | |
2205 | * | |
2206 | * A range of space can refer to two file extents in one tree while | |
2207 | * refer to only one file extent in another tree. | |
2208 | * | |
2209 | * So we may process a disk offset more than one time(two extents in A) | |
2210 | * and locate at the same extent(one extent in B), then insert two same | |
2211 | * backrefs(both refer to the extent in B). | |
2212 | */ | |
2213 | return 0; | |
2214 | } | |
2215 | ||
2216 | static void backref_insert(struct rb_root *root, | |
2217 | struct sa_defrag_extent_backref *backref) | |
2218 | { | |
2219 | struct rb_node **p = &root->rb_node; | |
2220 | struct rb_node *parent = NULL; | |
2221 | struct sa_defrag_extent_backref *entry; | |
2222 | int ret; | |
2223 | ||
2224 | while (*p) { | |
2225 | parent = *p; | |
2226 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2227 | ||
2228 | ret = backref_comp(backref, entry); | |
2229 | if (ret < 0) | |
2230 | p = &(*p)->rb_left; | |
2231 | else | |
2232 | p = &(*p)->rb_right; | |
2233 | } | |
2234 | ||
2235 | rb_link_node(&backref->node, parent, p); | |
2236 | rb_insert_color(&backref->node, root); | |
2237 | } | |
2238 | ||
2239 | /* | |
2240 | * Note the backref might has changed, and in this case we just return 0. | |
2241 | */ | |
2242 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2243 | void *ctx) | |
2244 | { | |
2245 | struct btrfs_file_extent_item *extent; | |
2246 | struct btrfs_fs_info *fs_info; | |
2247 | struct old_sa_defrag_extent *old = ctx; | |
2248 | struct new_sa_defrag_extent *new = old->new; | |
2249 | struct btrfs_path *path = new->path; | |
2250 | struct btrfs_key key; | |
2251 | struct btrfs_root *root; | |
2252 | struct sa_defrag_extent_backref *backref; | |
2253 | struct extent_buffer *leaf; | |
2254 | struct inode *inode = new->inode; | |
2255 | int slot; | |
2256 | int ret; | |
2257 | u64 extent_offset; | |
2258 | u64 num_bytes; | |
2259 | ||
2260 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
2261 | inum == btrfs_ino(inode)) | |
2262 | return 0; | |
2263 | ||
2264 | key.objectid = root_id; | |
2265 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2266 | key.offset = (u64)-1; | |
2267 | ||
2268 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2269 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2270 | if (IS_ERR(root)) { | |
2271 | if (PTR_ERR(root) == -ENOENT) | |
2272 | return 0; | |
2273 | WARN_ON(1); | |
2274 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", | |
2275 | inum, offset, root_id); | |
2276 | return PTR_ERR(root); | |
2277 | } | |
2278 | ||
2279 | key.objectid = inum; | |
2280 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2281 | if (offset > (u64)-1 << 32) | |
2282 | key.offset = 0; | |
2283 | else | |
2284 | key.offset = offset; | |
2285 | ||
2286 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2287 | if (WARN_ON(ret < 0)) |
38c227d8 | 2288 | return ret; |
50f1319c | 2289 | ret = 0; |
38c227d8 LB |
2290 | |
2291 | while (1) { | |
2292 | cond_resched(); | |
2293 | ||
2294 | leaf = path->nodes[0]; | |
2295 | slot = path->slots[0]; | |
2296 | ||
2297 | if (slot >= btrfs_header_nritems(leaf)) { | |
2298 | ret = btrfs_next_leaf(root, path); | |
2299 | if (ret < 0) { | |
2300 | goto out; | |
2301 | } else if (ret > 0) { | |
2302 | ret = 0; | |
2303 | goto out; | |
2304 | } | |
2305 | continue; | |
2306 | } | |
2307 | ||
2308 | path->slots[0]++; | |
2309 | ||
2310 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2311 | ||
2312 | if (key.objectid > inum) | |
2313 | goto out; | |
2314 | ||
2315 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2316 | continue; | |
2317 | ||
2318 | extent = btrfs_item_ptr(leaf, slot, | |
2319 | struct btrfs_file_extent_item); | |
2320 | ||
2321 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2322 | continue; | |
2323 | ||
e68afa49 LB |
2324 | /* |
2325 | * 'offset' refers to the exact key.offset, | |
2326 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2327 | * (key.offset - extent_offset). | |
2328 | */ | |
2329 | if (key.offset != offset) | |
38c227d8 LB |
2330 | continue; |
2331 | ||
e68afa49 | 2332 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2333 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2334 | |
38c227d8 LB |
2335 | if (extent_offset >= old->extent_offset + old->offset + |
2336 | old->len || extent_offset + num_bytes <= | |
2337 | old->extent_offset + old->offset) | |
2338 | continue; | |
38c227d8 LB |
2339 | break; |
2340 | } | |
2341 | ||
2342 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2343 | if (!backref) { | |
2344 | ret = -ENOENT; | |
2345 | goto out; | |
2346 | } | |
2347 | ||
2348 | backref->root_id = root_id; | |
2349 | backref->inum = inum; | |
e68afa49 | 2350 | backref->file_pos = offset; |
38c227d8 LB |
2351 | backref->num_bytes = num_bytes; |
2352 | backref->extent_offset = extent_offset; | |
2353 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2354 | backref->old = old; | |
2355 | backref_insert(&new->root, backref); | |
2356 | old->count++; | |
2357 | out: | |
2358 | btrfs_release_path(path); | |
2359 | WARN_ON(ret); | |
2360 | return ret; | |
2361 | } | |
2362 | ||
2363 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2364 | struct new_sa_defrag_extent *new) | |
2365 | { | |
2366 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; | |
2367 | struct old_sa_defrag_extent *old, *tmp; | |
2368 | int ret; | |
2369 | ||
2370 | new->path = path; | |
2371 | ||
2372 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2373 | ret = iterate_inodes_from_logical(old->bytenr + |
2374 | old->extent_offset, fs_info, | |
38c227d8 LB |
2375 | path, record_one_backref, |
2376 | old); | |
4724b106 JB |
2377 | if (ret < 0 && ret != -ENOENT) |
2378 | return false; | |
38c227d8 LB |
2379 | |
2380 | /* no backref to be processed for this extent */ | |
2381 | if (!old->count) { | |
2382 | list_del(&old->list); | |
2383 | kfree(old); | |
2384 | } | |
2385 | } | |
2386 | ||
2387 | if (list_empty(&new->head)) | |
2388 | return false; | |
2389 | ||
2390 | return true; | |
2391 | } | |
2392 | ||
2393 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2394 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2395 | struct new_sa_defrag_extent *new) |
38c227d8 | 2396 | { |
116e0024 | 2397 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2398 | return 0; |
2399 | ||
2400 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2401 | return 0; | |
2402 | ||
116e0024 LB |
2403 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2404 | return 0; | |
2405 | ||
2406 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2407 | btrfs_file_extent_other_encoding(leaf, fi)) |
2408 | return 0; | |
2409 | ||
2410 | return 1; | |
2411 | } | |
2412 | ||
2413 | /* | |
2414 | * Note the backref might has changed, and in this case we just return 0. | |
2415 | */ | |
2416 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2417 | struct sa_defrag_extent_backref *prev, | |
2418 | struct sa_defrag_extent_backref *backref) | |
2419 | { | |
2420 | struct btrfs_file_extent_item *extent; | |
2421 | struct btrfs_file_extent_item *item; | |
2422 | struct btrfs_ordered_extent *ordered; | |
2423 | struct btrfs_trans_handle *trans; | |
2424 | struct btrfs_fs_info *fs_info; | |
2425 | struct btrfs_root *root; | |
2426 | struct btrfs_key key; | |
2427 | struct extent_buffer *leaf; | |
2428 | struct old_sa_defrag_extent *old = backref->old; | |
2429 | struct new_sa_defrag_extent *new = old->new; | |
2430 | struct inode *src_inode = new->inode; | |
2431 | struct inode *inode; | |
2432 | struct extent_state *cached = NULL; | |
2433 | int ret = 0; | |
2434 | u64 start; | |
2435 | u64 len; | |
2436 | u64 lock_start; | |
2437 | u64 lock_end; | |
2438 | bool merge = false; | |
2439 | int index; | |
2440 | ||
2441 | if (prev && prev->root_id == backref->root_id && | |
2442 | prev->inum == backref->inum && | |
2443 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2444 | merge = true; | |
2445 | ||
2446 | /* step 1: get root */ | |
2447 | key.objectid = backref->root_id; | |
2448 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2449 | key.offset = (u64)-1; | |
2450 | ||
2451 | fs_info = BTRFS_I(src_inode)->root->fs_info; | |
2452 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
2453 | ||
2454 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2455 | if (IS_ERR(root)) { | |
2456 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2457 | if (PTR_ERR(root) == -ENOENT) | |
2458 | return 0; | |
2459 | return PTR_ERR(root); | |
2460 | } | |
38c227d8 | 2461 | |
bcbba5e6 WS |
2462 | if (btrfs_root_readonly(root)) { |
2463 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2464 | return 0; | |
2465 | } | |
2466 | ||
38c227d8 LB |
2467 | /* step 2: get inode */ |
2468 | key.objectid = backref->inum; | |
2469 | key.type = BTRFS_INODE_ITEM_KEY; | |
2470 | key.offset = 0; | |
2471 | ||
2472 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2473 | if (IS_ERR(inode)) { | |
2474 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2475 | return 0; | |
2476 | } | |
2477 | ||
2478 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2479 | ||
2480 | /* step 3: relink backref */ | |
2481 | lock_start = backref->file_pos; | |
2482 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2483 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2484 | 0, &cached); | |
2485 | ||
2486 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2487 | if (ordered) { | |
2488 | btrfs_put_ordered_extent(ordered); | |
2489 | goto out_unlock; | |
2490 | } | |
2491 | ||
2492 | trans = btrfs_join_transaction(root); | |
2493 | if (IS_ERR(trans)) { | |
2494 | ret = PTR_ERR(trans); | |
2495 | goto out_unlock; | |
2496 | } | |
2497 | ||
2498 | key.objectid = backref->inum; | |
2499 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2500 | key.offset = backref->file_pos; | |
2501 | ||
2502 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2503 | if (ret < 0) { | |
2504 | goto out_free_path; | |
2505 | } else if (ret > 0) { | |
2506 | ret = 0; | |
2507 | goto out_free_path; | |
2508 | } | |
2509 | ||
2510 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2511 | struct btrfs_file_extent_item); | |
2512 | ||
2513 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2514 | backref->generation) | |
2515 | goto out_free_path; | |
2516 | ||
2517 | btrfs_release_path(path); | |
2518 | ||
2519 | start = backref->file_pos; | |
2520 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2521 | start += old->extent_offset + old->offset - | |
2522 | backref->extent_offset; | |
2523 | ||
2524 | len = min(backref->extent_offset + backref->num_bytes, | |
2525 | old->extent_offset + old->offset + old->len); | |
2526 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2527 | ||
2528 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2529 | start + len, 1); | |
2530 | if (ret) | |
2531 | goto out_free_path; | |
2532 | again: | |
2533 | key.objectid = btrfs_ino(inode); | |
2534 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2535 | key.offset = start; | |
2536 | ||
a09a0a70 | 2537 | path->leave_spinning = 1; |
38c227d8 LB |
2538 | if (merge) { |
2539 | struct btrfs_file_extent_item *fi; | |
2540 | u64 extent_len; | |
2541 | struct btrfs_key found_key; | |
2542 | ||
3c9665df | 2543 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
38c227d8 LB |
2544 | if (ret < 0) |
2545 | goto out_free_path; | |
2546 | ||
2547 | path->slots[0]--; | |
2548 | leaf = path->nodes[0]; | |
2549 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2550 | ||
2551 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2552 | struct btrfs_file_extent_item); | |
2553 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2554 | ||
116e0024 LB |
2555 | if (extent_len + found_key.offset == start && |
2556 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2557 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2558 | extent_len + len); | |
2559 | btrfs_mark_buffer_dirty(leaf); | |
2560 | inode_add_bytes(inode, len); | |
2561 | ||
2562 | ret = 1; | |
2563 | goto out_free_path; | |
2564 | } else { | |
2565 | merge = false; | |
2566 | btrfs_release_path(path); | |
2567 | goto again; | |
2568 | } | |
2569 | } | |
2570 | ||
2571 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2572 | sizeof(*extent)); | |
2573 | if (ret) { | |
2574 | btrfs_abort_transaction(trans, root, ret); | |
2575 | goto out_free_path; | |
2576 | } | |
2577 | ||
2578 | leaf = path->nodes[0]; | |
2579 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2580 | struct btrfs_file_extent_item); | |
2581 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2582 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2583 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2584 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2585 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2586 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2587 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2588 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2589 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2590 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2591 | ||
2592 | btrfs_mark_buffer_dirty(leaf); | |
2593 | inode_add_bytes(inode, len); | |
a09a0a70 | 2594 | btrfs_release_path(path); |
38c227d8 LB |
2595 | |
2596 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, | |
2597 | new->disk_len, 0, | |
2598 | backref->root_id, backref->inum, | |
b06c4bf5 | 2599 | new->file_pos); /* start - extent_offset */ |
38c227d8 LB |
2600 | if (ret) { |
2601 | btrfs_abort_transaction(trans, root, ret); | |
2602 | goto out_free_path; | |
2603 | } | |
2604 | ||
2605 | ret = 1; | |
2606 | out_free_path: | |
2607 | btrfs_release_path(path); | |
a09a0a70 | 2608 | path->leave_spinning = 0; |
38c227d8 LB |
2609 | btrfs_end_transaction(trans, root); |
2610 | out_unlock: | |
2611 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2612 | &cached, GFP_NOFS); | |
2613 | iput(inode); | |
2614 | return ret; | |
2615 | } | |
2616 | ||
6f519564 LB |
2617 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2618 | { | |
2619 | struct old_sa_defrag_extent *old, *tmp; | |
2620 | ||
2621 | if (!new) | |
2622 | return; | |
2623 | ||
2624 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
6f519564 LB |
2625 | kfree(old); |
2626 | } | |
2627 | kfree(new); | |
2628 | } | |
2629 | ||
38c227d8 LB |
2630 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2631 | { | |
2632 | struct btrfs_path *path; | |
38c227d8 LB |
2633 | struct sa_defrag_extent_backref *backref; |
2634 | struct sa_defrag_extent_backref *prev = NULL; | |
2635 | struct inode *inode; | |
2636 | struct btrfs_root *root; | |
2637 | struct rb_node *node; | |
2638 | int ret; | |
2639 | ||
2640 | inode = new->inode; | |
2641 | root = BTRFS_I(inode)->root; | |
2642 | ||
2643 | path = btrfs_alloc_path(); | |
2644 | if (!path) | |
2645 | return; | |
2646 | ||
2647 | if (!record_extent_backrefs(path, new)) { | |
2648 | btrfs_free_path(path); | |
2649 | goto out; | |
2650 | } | |
2651 | btrfs_release_path(path); | |
2652 | ||
2653 | while (1) { | |
2654 | node = rb_first(&new->root); | |
2655 | if (!node) | |
2656 | break; | |
2657 | rb_erase(node, &new->root); | |
2658 | ||
2659 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2660 | ||
2661 | ret = relink_extent_backref(path, prev, backref); | |
2662 | WARN_ON(ret < 0); | |
2663 | ||
2664 | kfree(prev); | |
2665 | ||
2666 | if (ret == 1) | |
2667 | prev = backref; | |
2668 | else | |
2669 | prev = NULL; | |
2670 | cond_resched(); | |
2671 | } | |
2672 | kfree(prev); | |
2673 | ||
2674 | btrfs_free_path(path); | |
38c227d8 | 2675 | out: |
6f519564 LB |
2676 | free_sa_defrag_extent(new); |
2677 | ||
38c227d8 LB |
2678 | atomic_dec(&root->fs_info->defrag_running); |
2679 | wake_up(&root->fs_info->transaction_wait); | |
38c227d8 LB |
2680 | } |
2681 | ||
2682 | static struct new_sa_defrag_extent * | |
2683 | record_old_file_extents(struct inode *inode, | |
2684 | struct btrfs_ordered_extent *ordered) | |
2685 | { | |
2686 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2687 | struct btrfs_path *path; | |
2688 | struct btrfs_key key; | |
6f519564 | 2689 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2690 | struct new_sa_defrag_extent *new; |
2691 | int ret; | |
2692 | ||
2693 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2694 | if (!new) | |
2695 | return NULL; | |
2696 | ||
2697 | new->inode = inode; | |
2698 | new->file_pos = ordered->file_offset; | |
2699 | new->len = ordered->len; | |
2700 | new->bytenr = ordered->start; | |
2701 | new->disk_len = ordered->disk_len; | |
2702 | new->compress_type = ordered->compress_type; | |
2703 | new->root = RB_ROOT; | |
2704 | INIT_LIST_HEAD(&new->head); | |
2705 | ||
2706 | path = btrfs_alloc_path(); | |
2707 | if (!path) | |
2708 | goto out_kfree; | |
2709 | ||
2710 | key.objectid = btrfs_ino(inode); | |
2711 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2712 | key.offset = new->file_pos; | |
2713 | ||
2714 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2715 | if (ret < 0) | |
2716 | goto out_free_path; | |
2717 | if (ret > 0 && path->slots[0] > 0) | |
2718 | path->slots[0]--; | |
2719 | ||
2720 | /* find out all the old extents for the file range */ | |
2721 | while (1) { | |
2722 | struct btrfs_file_extent_item *extent; | |
2723 | struct extent_buffer *l; | |
2724 | int slot; | |
2725 | u64 num_bytes; | |
2726 | u64 offset; | |
2727 | u64 end; | |
2728 | u64 disk_bytenr; | |
2729 | u64 extent_offset; | |
2730 | ||
2731 | l = path->nodes[0]; | |
2732 | slot = path->slots[0]; | |
2733 | ||
2734 | if (slot >= btrfs_header_nritems(l)) { | |
2735 | ret = btrfs_next_leaf(root, path); | |
2736 | if (ret < 0) | |
6f519564 | 2737 | goto out_free_path; |
38c227d8 LB |
2738 | else if (ret > 0) |
2739 | break; | |
2740 | continue; | |
2741 | } | |
2742 | ||
2743 | btrfs_item_key_to_cpu(l, &key, slot); | |
2744 | ||
2745 | if (key.objectid != btrfs_ino(inode)) | |
2746 | break; | |
2747 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2748 | break; | |
2749 | if (key.offset >= new->file_pos + new->len) | |
2750 | break; | |
2751 | ||
2752 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2753 | ||
2754 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2755 | if (key.offset + num_bytes < new->file_pos) | |
2756 | goto next; | |
2757 | ||
2758 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2759 | if (!disk_bytenr) | |
2760 | goto next; | |
2761 | ||
2762 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2763 | ||
2764 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2765 | if (!old) | |
6f519564 | 2766 | goto out_free_path; |
38c227d8 LB |
2767 | |
2768 | offset = max(new->file_pos, key.offset); | |
2769 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2770 | ||
2771 | old->bytenr = disk_bytenr; | |
2772 | old->extent_offset = extent_offset; | |
2773 | old->offset = offset - key.offset; | |
2774 | old->len = end - offset; | |
2775 | old->new = new; | |
2776 | old->count = 0; | |
2777 | list_add_tail(&old->list, &new->head); | |
2778 | next: | |
2779 | path->slots[0]++; | |
2780 | cond_resched(); | |
2781 | } | |
2782 | ||
2783 | btrfs_free_path(path); | |
2784 | atomic_inc(&root->fs_info->defrag_running); | |
2785 | ||
2786 | return new; | |
2787 | ||
38c227d8 LB |
2788 | out_free_path: |
2789 | btrfs_free_path(path); | |
2790 | out_kfree: | |
6f519564 | 2791 | free_sa_defrag_extent(new); |
38c227d8 LB |
2792 | return NULL; |
2793 | } | |
2794 | ||
e570fd27 MX |
2795 | static void btrfs_release_delalloc_bytes(struct btrfs_root *root, |
2796 | u64 start, u64 len) | |
2797 | { | |
2798 | struct btrfs_block_group_cache *cache; | |
2799 | ||
2800 | cache = btrfs_lookup_block_group(root->fs_info, start); | |
2801 | ASSERT(cache); | |
2802 | ||
2803 | spin_lock(&cache->lock); | |
2804 | cache->delalloc_bytes -= len; | |
2805 | spin_unlock(&cache->lock); | |
2806 | ||
2807 | btrfs_put_block_group(cache); | |
2808 | } | |
2809 | ||
d352ac68 CM |
2810 | /* as ordered data IO finishes, this gets called so we can finish |
2811 | * an ordered extent if the range of bytes in the file it covers are | |
2812 | * fully written. | |
2813 | */ | |
5fd02043 | 2814 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2815 | { |
5fd02043 | 2816 | struct inode *inode = ordered_extent->inode; |
e6dcd2dc | 2817 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2818 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2819 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2820 | struct extent_state *cached_state = NULL; |
38c227d8 | 2821 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2822 | int compress_type = 0; |
77cef2ec JB |
2823 | int ret = 0; |
2824 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2825 | bool nolock; |
77cef2ec | 2826 | bool truncated = false; |
e6dcd2dc | 2827 | |
83eea1f1 | 2828 | nolock = btrfs_is_free_space_inode(inode); |
0cb59c99 | 2829 | |
5fd02043 JB |
2830 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2831 | ret = -EIO; | |
2832 | goto out; | |
2833 | } | |
2834 | ||
f612496b MX |
2835 | btrfs_free_io_failure_record(inode, ordered_extent->file_offset, |
2836 | ordered_extent->file_offset + | |
2837 | ordered_extent->len - 1); | |
2838 | ||
77cef2ec JB |
2839 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2840 | truncated = true; | |
2841 | logical_len = ordered_extent->truncated_len; | |
2842 | /* Truncated the entire extent, don't bother adding */ | |
2843 | if (!logical_len) | |
2844 | goto out; | |
2845 | } | |
2846 | ||
c2167754 | 2847 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2848 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
94ed938a QW |
2849 | |
2850 | /* | |
2851 | * For mwrite(mmap + memset to write) case, we still reserve | |
2852 | * space for NOCOW range. | |
2853 | * As NOCOW won't cause a new delayed ref, just free the space | |
2854 | */ | |
2855 | btrfs_qgroup_free_data(inode, ordered_extent->file_offset, | |
2856 | ordered_extent->len); | |
6c760c07 JB |
2857 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2858 | if (nolock) | |
2859 | trans = btrfs_join_transaction_nolock(root); | |
2860 | else | |
2861 | trans = btrfs_join_transaction(root); | |
2862 | if (IS_ERR(trans)) { | |
2863 | ret = PTR_ERR(trans); | |
2864 | trans = NULL; | |
2865 | goto out; | |
c2167754 | 2866 | } |
6c760c07 JB |
2867 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
2868 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2869 | if (ret) /* -ENOMEM or corruption */ | |
2870 | btrfs_abort_transaction(trans, root, ret); | |
c2167754 YZ |
2871 | goto out; |
2872 | } | |
e6dcd2dc | 2873 | |
2ac55d41 JB |
2874 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2875 | ordered_extent->file_offset + ordered_extent->len - 1, | |
d0082371 | 2876 | 0, &cached_state); |
e6dcd2dc | 2877 | |
38c227d8 LB |
2878 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2879 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2880 | EXTENT_DEFRAG, 1, cached_state); | |
2881 | if (ret) { | |
2882 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
8101c8db | 2883 | if (0 && last_snapshot >= BTRFS_I(inode)->generation) |
38c227d8 LB |
2884 | /* the inode is shared */ |
2885 | new = record_old_file_extents(inode, ordered_extent); | |
2886 | ||
2887 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2888 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2889 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2890 | } | |
2891 | ||
0cb59c99 | 2892 | if (nolock) |
7a7eaa40 | 2893 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2894 | else |
7a7eaa40 | 2895 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2896 | if (IS_ERR(trans)) { |
2897 | ret = PTR_ERR(trans); | |
2898 | trans = NULL; | |
2899 | goto out_unlock; | |
2900 | } | |
a79b7d4b | 2901 | |
0ca1f7ce | 2902 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
c2167754 | 2903 | |
c8b97818 | 2904 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2905 | compress_type = ordered_extent->compress_type; |
d899e052 | 2906 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2907 | BUG_ON(compress_type); |
920bbbfb | 2908 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
2909 | ordered_extent->file_offset, |
2910 | ordered_extent->file_offset + | |
77cef2ec | 2911 | logical_len); |
d899e052 | 2912 | } else { |
0af3d00b | 2913 | BUG_ON(root == root->fs_info->tree_root); |
d899e052 YZ |
2914 | ret = insert_reserved_file_extent(trans, inode, |
2915 | ordered_extent->file_offset, | |
2916 | ordered_extent->start, | |
2917 | ordered_extent->disk_len, | |
77cef2ec | 2918 | logical_len, logical_len, |
261507a0 | 2919 | compress_type, 0, 0, |
d899e052 | 2920 | BTRFS_FILE_EXTENT_REG); |
e570fd27 MX |
2921 | if (!ret) |
2922 | btrfs_release_delalloc_bytes(root, | |
2923 | ordered_extent->start, | |
2924 | ordered_extent->disk_len); | |
d899e052 | 2925 | } |
5dc562c5 JB |
2926 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
2927 | ordered_extent->file_offset, ordered_extent->len, | |
2928 | trans->transid); | |
79787eaa JM |
2929 | if (ret < 0) { |
2930 | btrfs_abort_transaction(trans, root, ret); | |
5fd02043 | 2931 | goto out_unlock; |
79787eaa | 2932 | } |
2ac55d41 | 2933 | |
e6dcd2dc CM |
2934 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
2935 | &ordered_extent->list); | |
2936 | ||
6c760c07 JB |
2937 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2938 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2939 | if (ret) { /* -ENOMEM or corruption */ | |
2940 | btrfs_abort_transaction(trans, root, ret); | |
2941 | goto out_unlock; | |
1ef30be1 JB |
2942 | } |
2943 | ret = 0; | |
5fd02043 JB |
2944 | out_unlock: |
2945 | unlock_extent_cached(io_tree, ordered_extent->file_offset, | |
2946 | ordered_extent->file_offset + | |
2947 | ordered_extent->len - 1, &cached_state, GFP_NOFS); | |
c2167754 | 2948 | out: |
5b0e95bf | 2949 | if (root != root->fs_info->tree_root) |
0cb59c99 | 2950 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
a698d075 MX |
2951 | if (trans) |
2952 | btrfs_end_transaction(trans, root); | |
0cb59c99 | 2953 | |
77cef2ec JB |
2954 | if (ret || truncated) { |
2955 | u64 start, end; | |
2956 | ||
2957 | if (truncated) | |
2958 | start = ordered_extent->file_offset + logical_len; | |
2959 | else | |
2960 | start = ordered_extent->file_offset; | |
2961 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
2962 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
2963 | ||
2964 | /* Drop the cache for the part of the extent we didn't write. */ | |
2965 | btrfs_drop_extent_cache(inode, start, end, 0); | |
5fd02043 | 2966 | |
0bec9ef5 JB |
2967 | /* |
2968 | * If the ordered extent had an IOERR or something else went | |
2969 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
2970 | * back to the allocator. We only free the extent in the |
2971 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 2972 | */ |
77cef2ec JB |
2973 | if ((ret || !logical_len) && |
2974 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 JB |
2975 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2976 | btrfs_free_reserved_extent(root, ordered_extent->start, | |
e570fd27 | 2977 | ordered_extent->disk_len, 1); |
0bec9ef5 JB |
2978 | } |
2979 | ||
2980 | ||
5fd02043 | 2981 | /* |
8bad3c02 LB |
2982 | * This needs to be done to make sure anybody waiting knows we are done |
2983 | * updating everything for this ordered extent. | |
5fd02043 JB |
2984 | */ |
2985 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
2986 | ||
38c227d8 | 2987 | /* for snapshot-aware defrag */ |
6f519564 LB |
2988 | if (new) { |
2989 | if (ret) { | |
2990 | free_sa_defrag_extent(new); | |
2991 | atomic_dec(&root->fs_info->defrag_running); | |
2992 | } else { | |
2993 | relink_file_extents(new); | |
2994 | } | |
2995 | } | |
38c227d8 | 2996 | |
e6dcd2dc CM |
2997 | /* once for us */ |
2998 | btrfs_put_ordered_extent(ordered_extent); | |
2999 | /* once for the tree */ | |
3000 | btrfs_put_ordered_extent(ordered_extent); | |
3001 | ||
5fd02043 JB |
3002 | return ret; |
3003 | } | |
3004 | ||
3005 | static void finish_ordered_fn(struct btrfs_work *work) | |
3006 | { | |
3007 | struct btrfs_ordered_extent *ordered_extent; | |
3008 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
3009 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
3010 | } |
3011 | ||
b2950863 | 3012 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
3013 | struct extent_state *state, int uptodate) |
3014 | { | |
5fd02043 JB |
3015 | struct inode *inode = page->mapping->host; |
3016 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3017 | struct btrfs_ordered_extent *ordered_extent = NULL; | |
9e0af237 LB |
3018 | struct btrfs_workqueue *wq; |
3019 | btrfs_work_func_t func; | |
5fd02043 | 3020 | |
1abe9b8a | 3021 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
3022 | ||
8b62b72b | 3023 | ClearPagePrivate2(page); |
5fd02043 JB |
3024 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
3025 | end - start + 1, uptodate)) | |
3026 | return 0; | |
3027 | ||
9e0af237 LB |
3028 | if (btrfs_is_free_space_inode(inode)) { |
3029 | wq = root->fs_info->endio_freespace_worker; | |
3030 | func = btrfs_freespace_write_helper; | |
3031 | } else { | |
3032 | wq = root->fs_info->endio_write_workers; | |
3033 | func = btrfs_endio_write_helper; | |
3034 | } | |
5fd02043 | 3035 | |
9e0af237 LB |
3036 | btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL, |
3037 | NULL); | |
3038 | btrfs_queue_work(wq, &ordered_extent->work); | |
5fd02043 JB |
3039 | |
3040 | return 0; | |
211f90e6 CM |
3041 | } |
3042 | ||
dc380aea MX |
3043 | static int __readpage_endio_check(struct inode *inode, |
3044 | struct btrfs_io_bio *io_bio, | |
3045 | int icsum, struct page *page, | |
3046 | int pgoff, u64 start, size_t len) | |
3047 | { | |
3048 | char *kaddr; | |
3049 | u32 csum_expected; | |
3050 | u32 csum = ~(u32)0; | |
dc380aea MX |
3051 | |
3052 | csum_expected = *(((u32 *)io_bio->csum) + icsum); | |
3053 | ||
3054 | kaddr = kmap_atomic(page); | |
3055 | csum = btrfs_csum_data(kaddr + pgoff, csum, len); | |
3056 | btrfs_csum_final(csum, (char *)&csum); | |
3057 | if (csum != csum_expected) | |
3058 | goto zeroit; | |
3059 | ||
3060 | kunmap_atomic(kaddr); | |
3061 | return 0; | |
3062 | zeroit: | |
94647322 DS |
3063 | btrfs_warn_rl(BTRFS_I(inode)->root->fs_info, |
3064 | "csum failed ino %llu off %llu csum %u expected csum %u", | |
dc380aea MX |
3065 | btrfs_ino(inode), start, csum, csum_expected); |
3066 | memset(kaddr + pgoff, 1, len); | |
3067 | flush_dcache_page(page); | |
3068 | kunmap_atomic(kaddr); | |
3069 | if (csum_expected == 0) | |
3070 | return 0; | |
3071 | return -EIO; | |
3072 | } | |
3073 | ||
d352ac68 CM |
3074 | /* |
3075 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
3076 | * if there's a match, we allow the bio to finish. If not, the code in |
3077 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 3078 | */ |
facc8a22 MX |
3079 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
3080 | u64 phy_offset, struct page *page, | |
3081 | u64 start, u64 end, int mirror) | |
07157aac | 3082 | { |
4eee4fa4 | 3083 | size_t offset = start - page_offset(page); |
07157aac | 3084 | struct inode *inode = page->mapping->host; |
d1310b2e | 3085 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
ff79f819 | 3086 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 3087 | |
d20f7043 CM |
3088 | if (PageChecked(page)) { |
3089 | ClearPageChecked(page); | |
dc380aea | 3090 | return 0; |
d20f7043 | 3091 | } |
6cbff00f CH |
3092 | |
3093 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
dc380aea | 3094 | return 0; |
17d217fe YZ |
3095 | |
3096 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 3097 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
17d217fe YZ |
3098 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
3099 | GFP_NOFS); | |
b6cda9bc | 3100 | return 0; |
17d217fe | 3101 | } |
d20f7043 | 3102 | |
facc8a22 | 3103 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
dc380aea MX |
3104 | return __readpage_endio_check(inode, io_bio, phy_offset, page, offset, |
3105 | start, (size_t)(end - start + 1)); | |
07157aac | 3106 | } |
b888db2b | 3107 | |
24bbcf04 YZ |
3108 | void btrfs_add_delayed_iput(struct inode *inode) |
3109 | { | |
3110 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
8089fe62 | 3111 | struct btrfs_inode *binode = BTRFS_I(inode); |
24bbcf04 YZ |
3112 | |
3113 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
3114 | return; | |
3115 | ||
24bbcf04 | 3116 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3117 | if (binode->delayed_iput_count == 0) { |
3118 | ASSERT(list_empty(&binode->delayed_iput)); | |
3119 | list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs); | |
3120 | } else { | |
3121 | binode->delayed_iput_count++; | |
3122 | } | |
24bbcf04 YZ |
3123 | spin_unlock(&fs_info->delayed_iput_lock); |
3124 | } | |
3125 | ||
3126 | void btrfs_run_delayed_iputs(struct btrfs_root *root) | |
3127 | { | |
24bbcf04 | 3128 | struct btrfs_fs_info *fs_info = root->fs_info; |
24bbcf04 | 3129 | |
d7c15171 | 3130 | down_read(&fs_info->delayed_iput_sem); |
24bbcf04 | 3131 | spin_lock(&fs_info->delayed_iput_lock); |
8089fe62 DS |
3132 | while (!list_empty(&fs_info->delayed_iputs)) { |
3133 | struct btrfs_inode *inode; | |
3134 | ||
3135 | inode = list_first_entry(&fs_info->delayed_iputs, | |
3136 | struct btrfs_inode, delayed_iput); | |
3137 | if (inode->delayed_iput_count) { | |
3138 | inode->delayed_iput_count--; | |
3139 | list_move_tail(&inode->delayed_iput, | |
3140 | &fs_info->delayed_iputs); | |
3141 | } else { | |
3142 | list_del_init(&inode->delayed_iput); | |
3143 | } | |
3144 | spin_unlock(&fs_info->delayed_iput_lock); | |
3145 | iput(&inode->vfs_inode); | |
3146 | spin_lock(&fs_info->delayed_iput_lock); | |
24bbcf04 | 3147 | } |
8089fe62 | 3148 | spin_unlock(&fs_info->delayed_iput_lock); |
d7c15171 | 3149 | up_read(&root->fs_info->delayed_iput_sem); |
24bbcf04 YZ |
3150 | } |
3151 | ||
d68fc57b | 3152 | /* |
42b2aa86 | 3153 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
3154 | * files in the subvolume, it removes orphan item and frees block_rsv |
3155 | * structure. | |
3156 | */ | |
3157 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
3158 | struct btrfs_root *root) | |
3159 | { | |
90290e19 | 3160 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
3161 | int ret; |
3162 | ||
8a35d95f | 3163 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
3164 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
3165 | return; | |
3166 | ||
90290e19 | 3167 | spin_lock(&root->orphan_lock); |
8a35d95f | 3168 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
3169 | spin_unlock(&root->orphan_lock); |
3170 | return; | |
3171 | } | |
3172 | ||
3173 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
3174 | spin_unlock(&root->orphan_lock); | |
3175 | return; | |
3176 | } | |
3177 | ||
3178 | block_rsv = root->orphan_block_rsv; | |
3179 | root->orphan_block_rsv = NULL; | |
3180 | spin_unlock(&root->orphan_lock); | |
3181 | ||
27cdeb70 | 3182 | if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && |
d68fc57b YZ |
3183 | btrfs_root_refs(&root->root_item) > 0) { |
3184 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, | |
3185 | root->root_key.objectid); | |
4ef31a45 JB |
3186 | if (ret) |
3187 | btrfs_abort_transaction(trans, root, ret); | |
3188 | else | |
27cdeb70 MX |
3189 | clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, |
3190 | &root->state); | |
d68fc57b YZ |
3191 | } |
3192 | ||
90290e19 JB |
3193 | if (block_rsv) { |
3194 | WARN_ON(block_rsv->size > 0); | |
3195 | btrfs_free_block_rsv(root, block_rsv); | |
d68fc57b YZ |
3196 | } |
3197 | } | |
3198 | ||
7b128766 JB |
3199 | /* |
3200 | * This creates an orphan entry for the given inode in case something goes | |
3201 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
3202 | * |
3203 | * NOTE: caller of this function should reserve 5 units of metadata for | |
3204 | * this function. | |
7b128766 JB |
3205 | */ |
3206 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
3207 | { | |
3208 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3209 | struct btrfs_block_rsv *block_rsv = NULL; |
3210 | int reserve = 0; | |
3211 | int insert = 0; | |
3212 | int ret; | |
7b128766 | 3213 | |
d68fc57b | 3214 | if (!root->orphan_block_rsv) { |
66d8f3dd | 3215 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
b532402e TI |
3216 | if (!block_rsv) |
3217 | return -ENOMEM; | |
d68fc57b | 3218 | } |
7b128766 | 3219 | |
d68fc57b YZ |
3220 | spin_lock(&root->orphan_lock); |
3221 | if (!root->orphan_block_rsv) { | |
3222 | root->orphan_block_rsv = block_rsv; | |
3223 | } else if (block_rsv) { | |
3224 | btrfs_free_block_rsv(root, block_rsv); | |
3225 | block_rsv = NULL; | |
7b128766 | 3226 | } |
7b128766 | 3227 | |
8a35d95f JB |
3228 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3229 | &BTRFS_I(inode)->runtime_flags)) { | |
d68fc57b YZ |
3230 | #if 0 |
3231 | /* | |
3232 | * For proper ENOSPC handling, we should do orphan | |
3233 | * cleanup when mounting. But this introduces backward | |
3234 | * compatibility issue. | |
3235 | */ | |
3236 | if (!xchg(&root->orphan_item_inserted, 1)) | |
3237 | insert = 2; | |
3238 | else | |
3239 | insert = 1; | |
3240 | #endif | |
3241 | insert = 1; | |
321f0e70 | 3242 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
3243 | } |
3244 | ||
72ac3c0d JB |
3245 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3246 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3247 | reserve = 1; |
d68fc57b | 3248 | spin_unlock(&root->orphan_lock); |
7b128766 | 3249 | |
d68fc57b YZ |
3250 | /* grab metadata reservation from transaction handle */ |
3251 | if (reserve) { | |
3252 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
79787eaa | 3253 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
d68fc57b | 3254 | } |
7b128766 | 3255 | |
d68fc57b YZ |
3256 | /* insert an orphan item to track this unlinked/truncated file */ |
3257 | if (insert >= 1) { | |
33345d01 | 3258 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 3259 | if (ret) { |
703c88e0 | 3260 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
3261 | if (reserve) { |
3262 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
3263 | &BTRFS_I(inode)->runtime_flags); | |
3264 | btrfs_orphan_release_metadata(inode); | |
3265 | } | |
3266 | if (ret != -EEXIST) { | |
e8e7cff6 JB |
3267 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3268 | &BTRFS_I(inode)->runtime_flags); | |
4ef31a45 JB |
3269 | btrfs_abort_transaction(trans, root, ret); |
3270 | return ret; | |
3271 | } | |
79787eaa JM |
3272 | } |
3273 | ret = 0; | |
d68fc57b YZ |
3274 | } |
3275 | ||
3276 | /* insert an orphan item to track subvolume contains orphan files */ | |
3277 | if (insert >= 2) { | |
3278 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, | |
3279 | root->root_key.objectid); | |
79787eaa JM |
3280 | if (ret && ret != -EEXIST) { |
3281 | btrfs_abort_transaction(trans, root, ret); | |
3282 | return ret; | |
3283 | } | |
d68fc57b YZ |
3284 | } |
3285 | return 0; | |
7b128766 JB |
3286 | } |
3287 | ||
3288 | /* | |
3289 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3290 | * item for this particular inode. | |
3291 | */ | |
48a3b636 ES |
3292 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3293 | struct inode *inode) | |
7b128766 JB |
3294 | { |
3295 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3296 | int delete_item = 0; |
3297 | int release_rsv = 0; | |
7b128766 JB |
3298 | int ret = 0; |
3299 | ||
d68fc57b | 3300 | spin_lock(&root->orphan_lock); |
8a35d95f JB |
3301 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3302 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3303 | delete_item = 1; |
7b128766 | 3304 | |
72ac3c0d JB |
3305 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3306 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3307 | release_rsv = 1; |
d68fc57b | 3308 | spin_unlock(&root->orphan_lock); |
7b128766 | 3309 | |
703c88e0 | 3310 | if (delete_item) { |
8a35d95f | 3311 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3312 | if (trans) |
3313 | ret = btrfs_del_orphan_item(trans, root, | |
3314 | btrfs_ino(inode)); | |
8a35d95f | 3315 | } |
7b128766 | 3316 | |
703c88e0 FDBM |
3317 | if (release_rsv) |
3318 | btrfs_orphan_release_metadata(inode); | |
3319 | ||
4ef31a45 | 3320 | return ret; |
7b128766 JB |
3321 | } |
3322 | ||
3323 | /* | |
3324 | * this cleans up any orphans that may be left on the list from the last use | |
3325 | * of this root. | |
3326 | */ | |
66b4ffd1 | 3327 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 JB |
3328 | { |
3329 | struct btrfs_path *path; | |
3330 | struct extent_buffer *leaf; | |
7b128766 JB |
3331 | struct btrfs_key key, found_key; |
3332 | struct btrfs_trans_handle *trans; | |
3333 | struct inode *inode; | |
8f6d7f4f | 3334 | u64 last_objectid = 0; |
7b128766 JB |
3335 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3336 | ||
d68fc57b | 3337 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3338 | return 0; |
c71bf099 YZ |
3339 | |
3340 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3341 | if (!path) { |
3342 | ret = -ENOMEM; | |
3343 | goto out; | |
3344 | } | |
7b128766 JB |
3345 | path->reada = -1; |
3346 | ||
3347 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
962a298f | 3348 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
7b128766 JB |
3349 | key.offset = (u64)-1; |
3350 | ||
7b128766 JB |
3351 | while (1) { |
3352 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3353 | if (ret < 0) |
3354 | goto out; | |
7b128766 JB |
3355 | |
3356 | /* | |
3357 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3358 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3359 | * find the key and see if we have stuff that matches |
3360 | */ | |
3361 | if (ret > 0) { | |
66b4ffd1 | 3362 | ret = 0; |
7b128766 JB |
3363 | if (path->slots[0] == 0) |
3364 | break; | |
3365 | path->slots[0]--; | |
3366 | } | |
3367 | ||
3368 | /* pull out the item */ | |
3369 | leaf = path->nodes[0]; | |
7b128766 JB |
3370 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3371 | ||
3372 | /* make sure the item matches what we want */ | |
3373 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3374 | break; | |
962a298f | 3375 | if (found_key.type != BTRFS_ORPHAN_ITEM_KEY) |
7b128766 JB |
3376 | break; |
3377 | ||
3378 | /* release the path since we're done with it */ | |
b3b4aa74 | 3379 | btrfs_release_path(path); |
7b128766 JB |
3380 | |
3381 | /* | |
3382 | * this is where we are basically btrfs_lookup, without the | |
3383 | * crossing root thing. we store the inode number in the | |
3384 | * offset of the orphan item. | |
3385 | */ | |
8f6d7f4f JB |
3386 | |
3387 | if (found_key.offset == last_objectid) { | |
c2cf52eb SK |
3388 | btrfs_err(root->fs_info, |
3389 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3390 | ret = -EINVAL; |
3391 | goto out; | |
3392 | } | |
3393 | ||
3394 | last_objectid = found_key.offset; | |
3395 | ||
5d4f98a2 YZ |
3396 | found_key.objectid = found_key.offset; |
3397 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3398 | found_key.offset = 0; | |
73f73415 | 3399 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3400 | ret = PTR_ERR_OR_ZERO(inode); |
a8c9e576 | 3401 | if (ret && ret != -ESTALE) |
66b4ffd1 | 3402 | goto out; |
7b128766 | 3403 | |
f8e9e0b0 AJ |
3404 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
3405 | struct btrfs_root *dead_root; | |
3406 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3407 | int is_dead_root = 0; | |
3408 | ||
3409 | /* | |
3410 | * this is an orphan in the tree root. Currently these | |
3411 | * could come from 2 sources: | |
3412 | * a) a snapshot deletion in progress | |
3413 | * b) a free space cache inode | |
3414 | * We need to distinguish those two, as the snapshot | |
3415 | * orphan must not get deleted. | |
3416 | * find_dead_roots already ran before us, so if this | |
3417 | * is a snapshot deletion, we should find the root | |
3418 | * in the dead_roots list | |
3419 | */ | |
3420 | spin_lock(&fs_info->trans_lock); | |
3421 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3422 | root_list) { | |
3423 | if (dead_root->root_key.objectid == | |
3424 | found_key.objectid) { | |
3425 | is_dead_root = 1; | |
3426 | break; | |
3427 | } | |
3428 | } | |
3429 | spin_unlock(&fs_info->trans_lock); | |
3430 | if (is_dead_root) { | |
3431 | /* prevent this orphan from being found again */ | |
3432 | key.offset = found_key.objectid - 1; | |
3433 | continue; | |
3434 | } | |
3435 | } | |
7b128766 | 3436 | /* |
a8c9e576 JB |
3437 | * Inode is already gone but the orphan item is still there, |
3438 | * kill the orphan item. | |
7b128766 | 3439 | */ |
a8c9e576 JB |
3440 | if (ret == -ESTALE) { |
3441 | trans = btrfs_start_transaction(root, 1); | |
66b4ffd1 JB |
3442 | if (IS_ERR(trans)) { |
3443 | ret = PTR_ERR(trans); | |
3444 | goto out; | |
3445 | } | |
c2cf52eb SK |
3446 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
3447 | found_key.objectid); | |
a8c9e576 JB |
3448 | ret = btrfs_del_orphan_item(trans, root, |
3449 | found_key.objectid); | |
5b21f2ed | 3450 | btrfs_end_transaction(trans, root); |
4ef31a45 JB |
3451 | if (ret) |
3452 | goto out; | |
7b128766 JB |
3453 | continue; |
3454 | } | |
3455 | ||
a8c9e576 JB |
3456 | /* |
3457 | * add this inode to the orphan list so btrfs_orphan_del does | |
3458 | * the proper thing when we hit it | |
3459 | */ | |
8a35d95f JB |
3460 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3461 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3462 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3463 | |
7b128766 JB |
3464 | /* if we have links, this was a truncate, lets do that */ |
3465 | if (inode->i_nlink) { | |
fae7f21c | 3466 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3467 | iput(inode); |
3468 | continue; | |
3469 | } | |
7b128766 | 3470 | nr_truncate++; |
f3fe820c JB |
3471 | |
3472 | /* 1 for the orphan item deletion. */ | |
3473 | trans = btrfs_start_transaction(root, 1); | |
3474 | if (IS_ERR(trans)) { | |
c69b26b0 | 3475 | iput(inode); |
f3fe820c JB |
3476 | ret = PTR_ERR(trans); |
3477 | goto out; | |
3478 | } | |
3479 | ret = btrfs_orphan_add(trans, inode); | |
3480 | btrfs_end_transaction(trans, root); | |
c69b26b0 JB |
3481 | if (ret) { |
3482 | iput(inode); | |
f3fe820c | 3483 | goto out; |
c69b26b0 | 3484 | } |
f3fe820c | 3485 | |
66b4ffd1 | 3486 | ret = btrfs_truncate(inode); |
4a7d0f68 JB |
3487 | if (ret) |
3488 | btrfs_orphan_del(NULL, inode); | |
7b128766 JB |
3489 | } else { |
3490 | nr_unlink++; | |
3491 | } | |
3492 | ||
3493 | /* this will do delete_inode and everything for us */ | |
3494 | iput(inode); | |
66b4ffd1 JB |
3495 | if (ret) |
3496 | goto out; | |
7b128766 | 3497 | } |
3254c876 MX |
3498 | /* release the path since we're done with it */ |
3499 | btrfs_release_path(path); | |
3500 | ||
d68fc57b YZ |
3501 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3502 | ||
3503 | if (root->orphan_block_rsv) | |
3504 | btrfs_block_rsv_release(root, root->orphan_block_rsv, | |
3505 | (u64)-1); | |
3506 | ||
27cdeb70 MX |
3507 | if (root->orphan_block_rsv || |
3508 | test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) { | |
7a7eaa40 | 3509 | trans = btrfs_join_transaction(root); |
66b4ffd1 JB |
3510 | if (!IS_ERR(trans)) |
3511 | btrfs_end_transaction(trans, root); | |
d68fc57b | 3512 | } |
7b128766 JB |
3513 | |
3514 | if (nr_unlink) | |
4884b476 | 3515 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3516 | if (nr_truncate) |
4884b476 | 3517 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3518 | |
3519 | out: | |
3520 | if (ret) | |
68b663d1 | 3521 | btrfs_err(root->fs_info, |
c2cf52eb | 3522 | "could not do orphan cleanup %d", ret); |
66b4ffd1 JB |
3523 | btrfs_free_path(path); |
3524 | return ret; | |
7b128766 JB |
3525 | } |
3526 | ||
46a53cca CM |
3527 | /* |
3528 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3529 | * don't find any xattrs, we know there can't be any acls. | |
3530 | * | |
3531 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3532 | */ | |
3533 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
63541927 FDBM |
3534 | int slot, u64 objectid, |
3535 | int *first_xattr_slot) | |
46a53cca CM |
3536 | { |
3537 | u32 nritems = btrfs_header_nritems(leaf); | |
3538 | struct btrfs_key found_key; | |
f23b5a59 JB |
3539 | static u64 xattr_access = 0; |
3540 | static u64 xattr_default = 0; | |
46a53cca CM |
3541 | int scanned = 0; |
3542 | ||
f23b5a59 JB |
3543 | if (!xattr_access) { |
3544 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, | |
3545 | strlen(POSIX_ACL_XATTR_ACCESS)); | |
3546 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, | |
3547 | strlen(POSIX_ACL_XATTR_DEFAULT)); | |
3548 | } | |
3549 | ||
46a53cca | 3550 | slot++; |
63541927 | 3551 | *first_xattr_slot = -1; |
46a53cca CM |
3552 | while (slot < nritems) { |
3553 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3554 | ||
3555 | /* we found a different objectid, there must not be acls */ | |
3556 | if (found_key.objectid != objectid) | |
3557 | return 0; | |
3558 | ||
3559 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 | 3560 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
63541927 FDBM |
3561 | if (*first_xattr_slot == -1) |
3562 | *first_xattr_slot = slot; | |
f23b5a59 JB |
3563 | if (found_key.offset == xattr_access || |
3564 | found_key.offset == xattr_default) | |
3565 | return 1; | |
3566 | } | |
46a53cca CM |
3567 | |
3568 | /* | |
3569 | * we found a key greater than an xattr key, there can't | |
3570 | * be any acls later on | |
3571 | */ | |
3572 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3573 | return 0; | |
3574 | ||
3575 | slot++; | |
3576 | scanned++; | |
3577 | ||
3578 | /* | |
3579 | * it goes inode, inode backrefs, xattrs, extents, | |
3580 | * so if there are a ton of hard links to an inode there can | |
3581 | * be a lot of backrefs. Don't waste time searching too hard, | |
3582 | * this is just an optimization | |
3583 | */ | |
3584 | if (scanned >= 8) | |
3585 | break; | |
3586 | } | |
3587 | /* we hit the end of the leaf before we found an xattr or | |
3588 | * something larger than an xattr. We have to assume the inode | |
3589 | * has acls | |
3590 | */ | |
63541927 FDBM |
3591 | if (*first_xattr_slot == -1) |
3592 | *first_xattr_slot = slot; | |
46a53cca CM |
3593 | return 1; |
3594 | } | |
3595 | ||
d352ac68 CM |
3596 | /* |
3597 | * read an inode from the btree into the in-memory inode | |
3598 | */ | |
5d4f98a2 | 3599 | static void btrfs_read_locked_inode(struct inode *inode) |
39279cc3 CM |
3600 | { |
3601 | struct btrfs_path *path; | |
5f39d397 | 3602 | struct extent_buffer *leaf; |
39279cc3 CM |
3603 | struct btrfs_inode_item *inode_item; |
3604 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3605 | struct btrfs_key location; | |
67de1176 | 3606 | unsigned long ptr; |
46a53cca | 3607 | int maybe_acls; |
618e21d5 | 3608 | u32 rdev; |
39279cc3 | 3609 | int ret; |
2f7e33d4 | 3610 | bool filled = false; |
63541927 | 3611 | int first_xattr_slot; |
2f7e33d4 MX |
3612 | |
3613 | ret = btrfs_fill_inode(inode, &rdev); | |
3614 | if (!ret) | |
3615 | filled = true; | |
39279cc3 CM |
3616 | |
3617 | path = btrfs_alloc_path(); | |
1748f843 MF |
3618 | if (!path) |
3619 | goto make_bad; | |
3620 | ||
39279cc3 | 3621 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3622 | |
39279cc3 | 3623 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
5f39d397 | 3624 | if (ret) |
39279cc3 | 3625 | goto make_bad; |
39279cc3 | 3626 | |
5f39d397 | 3627 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3628 | |
3629 | if (filled) | |
67de1176 | 3630 | goto cache_index; |
2f7e33d4 | 3631 | |
5f39d397 CM |
3632 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3633 | struct btrfs_inode_item); | |
5f39d397 | 3634 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3635 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3636 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3637 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
dbe674a9 | 3638 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 | 3639 | |
a937b979 DS |
3640 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime); |
3641 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime); | |
5f39d397 | 3642 | |
a937b979 DS |
3643 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime); |
3644 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime); | |
5f39d397 | 3645 | |
a937b979 DS |
3646 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime); |
3647 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime); | |
5f39d397 | 3648 | |
9cc97d64 | 3649 | BTRFS_I(inode)->i_otime.tv_sec = |
3650 | btrfs_timespec_sec(leaf, &inode_item->otime); | |
3651 | BTRFS_I(inode)->i_otime.tv_nsec = | |
3652 | btrfs_timespec_nsec(leaf, &inode_item->otime); | |
5f39d397 | 3653 | |
a76a3cd4 | 3654 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3655 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3656 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3657 | ||
6e17d30b YD |
3658 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
3659 | inode->i_generation = BTRFS_I(inode)->generation; | |
3660 | inode->i_rdev = 0; | |
3661 | rdev = btrfs_inode_rdev(leaf, inode_item); | |
3662 | ||
3663 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
3664 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); | |
3665 | ||
3666 | cache_index: | |
5dc562c5 JB |
3667 | /* |
3668 | * If we were modified in the current generation and evicted from memory | |
3669 | * and then re-read we need to do a full sync since we don't have any | |
3670 | * idea about which extents were modified before we were evicted from | |
3671 | * cache. | |
6e17d30b YD |
3672 | * |
3673 | * This is required for both inode re-read from disk and delayed inode | |
3674 | * in delayed_nodes_tree. | |
5dc562c5 JB |
3675 | */ |
3676 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) | |
3677 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
3678 | &BTRFS_I(inode)->runtime_flags); | |
3679 | ||
bde6c242 FM |
3680 | /* |
3681 | * We don't persist the id of the transaction where an unlink operation | |
3682 | * against the inode was last made. So here we assume the inode might | |
3683 | * have been evicted, and therefore the exact value of last_unlink_trans | |
3684 | * lost, and set it to last_trans to avoid metadata inconsistencies | |
3685 | * between the inode and its parent if the inode is fsync'ed and the log | |
3686 | * replayed. For example, in the scenario: | |
3687 | * | |
3688 | * touch mydir/foo | |
3689 | * ln mydir/foo mydir/bar | |
3690 | * sync | |
3691 | * unlink mydir/bar | |
3692 | * echo 2 > /proc/sys/vm/drop_caches # evicts inode | |
3693 | * xfs_io -c fsync mydir/foo | |
3694 | * <power failure> | |
3695 | * mount fs, triggers fsync log replay | |
3696 | * | |
3697 | * We must make sure that when we fsync our inode foo we also log its | |
3698 | * parent inode, otherwise after log replay the parent still has the | |
3699 | * dentry with the "bar" name but our inode foo has a link count of 1 | |
3700 | * and doesn't have an inode ref with the name "bar" anymore. | |
3701 | * | |
3702 | * Setting last_unlink_trans to last_trans is a pessimistic approach, | |
3703 | * but it guarantees correctness at the expense of ocassional full | |
3704 | * transaction commits on fsync if our inode is a directory, or if our | |
3705 | * inode is not a directory, logging its parent unnecessarily. | |
3706 | */ | |
3707 | BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans; | |
3708 | ||
67de1176 MX |
3709 | path->slots[0]++; |
3710 | if (inode->i_nlink != 1 || | |
3711 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3712 | goto cache_acl; | |
3713 | ||
3714 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
3715 | if (location.objectid != btrfs_ino(inode)) | |
3716 | goto cache_acl; | |
3717 | ||
3718 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3719 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3720 | struct btrfs_inode_ref *ref; | |
3721 | ||
3722 | ref = (struct btrfs_inode_ref *)ptr; | |
3723 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3724 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3725 | struct btrfs_inode_extref *extref; | |
3726 | ||
3727 | extref = (struct btrfs_inode_extref *)ptr; | |
3728 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3729 | extref); | |
3730 | } | |
2f7e33d4 | 3731 | cache_acl: |
46a53cca CM |
3732 | /* |
3733 | * try to precache a NULL acl entry for files that don't have | |
3734 | * any xattrs or acls | |
3735 | */ | |
33345d01 | 3736 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
63541927 FDBM |
3737 | btrfs_ino(inode), &first_xattr_slot); |
3738 | if (first_xattr_slot != -1) { | |
3739 | path->slots[0] = first_xattr_slot; | |
3740 | ret = btrfs_load_inode_props(inode, path); | |
3741 | if (ret) | |
3742 | btrfs_err(root->fs_info, | |
351fd353 | 3743 | "error loading props for ino %llu (root %llu): %d", |
63541927 FDBM |
3744 | btrfs_ino(inode), |
3745 | root->root_key.objectid, ret); | |
3746 | } | |
3747 | btrfs_free_path(path); | |
3748 | ||
72c04902 AV |
3749 | if (!maybe_acls) |
3750 | cache_no_acl(inode); | |
46a53cca | 3751 | |
39279cc3 | 3752 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3753 | case S_IFREG: |
3754 | inode->i_mapping->a_ops = &btrfs_aops; | |
d1310b2e | 3755 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3756 | inode->i_fop = &btrfs_file_operations; |
3757 | inode->i_op = &btrfs_file_inode_operations; | |
3758 | break; | |
3759 | case S_IFDIR: | |
3760 | inode->i_fop = &btrfs_dir_file_operations; | |
3761 | if (root == root->fs_info->tree_root) | |
3762 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
3763 | else | |
3764 | inode->i_op = &btrfs_dir_inode_operations; | |
3765 | break; | |
3766 | case S_IFLNK: | |
3767 | inode->i_op = &btrfs_symlink_inode_operations; | |
3768 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
3769 | break; | |
618e21d5 | 3770 | default: |
0279b4cd | 3771 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3772 | init_special_inode(inode, inode->i_mode, rdev); |
3773 | break; | |
39279cc3 | 3774 | } |
6cbff00f CH |
3775 | |
3776 | btrfs_update_iflags(inode); | |
39279cc3 CM |
3777 | return; |
3778 | ||
3779 | make_bad: | |
39279cc3 | 3780 | btrfs_free_path(path); |
39279cc3 CM |
3781 | make_bad_inode(inode); |
3782 | } | |
3783 | ||
d352ac68 CM |
3784 | /* |
3785 | * given a leaf and an inode, copy the inode fields into the leaf | |
3786 | */ | |
e02119d5 CM |
3787 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3788 | struct extent_buffer *leaf, | |
5f39d397 | 3789 | struct btrfs_inode_item *item, |
39279cc3 CM |
3790 | struct inode *inode) |
3791 | { | |
51fab693 LB |
3792 | struct btrfs_map_token token; |
3793 | ||
3794 | btrfs_init_map_token(&token); | |
5f39d397 | 3795 | |
51fab693 LB |
3796 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3797 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3798 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3799 | &token); | |
3800 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3801 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3802 | |
a937b979 | 3803 | btrfs_set_token_timespec_sec(leaf, &item->atime, |
51fab693 | 3804 | inode->i_atime.tv_sec, &token); |
a937b979 | 3805 | btrfs_set_token_timespec_nsec(leaf, &item->atime, |
51fab693 | 3806 | inode->i_atime.tv_nsec, &token); |
5f39d397 | 3807 | |
a937b979 | 3808 | btrfs_set_token_timespec_sec(leaf, &item->mtime, |
51fab693 | 3809 | inode->i_mtime.tv_sec, &token); |
a937b979 | 3810 | btrfs_set_token_timespec_nsec(leaf, &item->mtime, |
51fab693 | 3811 | inode->i_mtime.tv_nsec, &token); |
5f39d397 | 3812 | |
a937b979 | 3813 | btrfs_set_token_timespec_sec(leaf, &item->ctime, |
51fab693 | 3814 | inode->i_ctime.tv_sec, &token); |
a937b979 | 3815 | btrfs_set_token_timespec_nsec(leaf, &item->ctime, |
51fab693 | 3816 | inode->i_ctime.tv_nsec, &token); |
5f39d397 | 3817 | |
9cc97d64 | 3818 | btrfs_set_token_timespec_sec(leaf, &item->otime, |
3819 | BTRFS_I(inode)->i_otime.tv_sec, &token); | |
3820 | btrfs_set_token_timespec_nsec(leaf, &item->otime, | |
3821 | BTRFS_I(inode)->i_otime.tv_nsec, &token); | |
3822 | ||
51fab693 LB |
3823 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3824 | &token); | |
3825 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3826 | &token); | |
3827 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3828 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3829 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3830 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3831 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3832 | } |
3833 | ||
d352ac68 CM |
3834 | /* |
3835 | * copy everything in the in-memory inode into the btree. | |
3836 | */ | |
2115133f | 3837 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3838 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3839 | { |
3840 | struct btrfs_inode_item *inode_item; | |
3841 | struct btrfs_path *path; | |
5f39d397 | 3842 | struct extent_buffer *leaf; |
39279cc3 CM |
3843 | int ret; |
3844 | ||
3845 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3846 | if (!path) |
3847 | return -ENOMEM; | |
3848 | ||
b9473439 | 3849 | path->leave_spinning = 1; |
16cdcec7 MX |
3850 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3851 | 1); | |
39279cc3 CM |
3852 | if (ret) { |
3853 | if (ret > 0) | |
3854 | ret = -ENOENT; | |
3855 | goto failed; | |
3856 | } | |
3857 | ||
5f39d397 CM |
3858 | leaf = path->nodes[0]; |
3859 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3860 | struct btrfs_inode_item); |
39279cc3 | 3861 | |
e02119d5 | 3862 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3863 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3864 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3865 | ret = 0; |
3866 | failed: | |
39279cc3 CM |
3867 | btrfs_free_path(path); |
3868 | return ret; | |
3869 | } | |
3870 | ||
2115133f CM |
3871 | /* |
3872 | * copy everything in the in-memory inode into the btree. | |
3873 | */ | |
3874 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3875 | struct btrfs_root *root, struct inode *inode) | |
3876 | { | |
3877 | int ret; | |
3878 | ||
3879 | /* | |
3880 | * If the inode is a free space inode, we can deadlock during commit | |
3881 | * if we put it into the delayed code. | |
3882 | * | |
3883 | * The data relocation inode should also be directly updated | |
3884 | * without delay | |
3885 | */ | |
83eea1f1 | 3886 | if (!btrfs_is_free_space_inode(inode) |
1d52c78a JB |
3887 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
3888 | && !root->fs_info->log_root_recovering) { | |
8ea05e3a AB |
3889 | btrfs_update_root_times(trans, root); |
3890 | ||
2115133f CM |
3891 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3892 | if (!ret) | |
3893 | btrfs_set_inode_last_trans(trans, inode); | |
3894 | return ret; | |
3895 | } | |
3896 | ||
3897 | return btrfs_update_inode_item(trans, root, inode); | |
3898 | } | |
3899 | ||
be6aef60 JB |
3900 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3901 | struct btrfs_root *root, | |
3902 | struct inode *inode) | |
2115133f CM |
3903 | { |
3904 | int ret; | |
3905 | ||
3906 | ret = btrfs_update_inode(trans, root, inode); | |
3907 | if (ret == -ENOSPC) | |
3908 | return btrfs_update_inode_item(trans, root, inode); | |
3909 | return ret; | |
3910 | } | |
3911 | ||
d352ac68 CM |
3912 | /* |
3913 | * unlink helper that gets used here in inode.c and in the tree logging | |
3914 | * recovery code. It remove a link in a directory with a given name, and | |
3915 | * also drops the back refs in the inode to the directory | |
3916 | */ | |
92986796 AV |
3917 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3918 | struct btrfs_root *root, | |
3919 | struct inode *dir, struct inode *inode, | |
3920 | const char *name, int name_len) | |
39279cc3 CM |
3921 | { |
3922 | struct btrfs_path *path; | |
39279cc3 | 3923 | int ret = 0; |
5f39d397 | 3924 | struct extent_buffer *leaf; |
39279cc3 | 3925 | struct btrfs_dir_item *di; |
5f39d397 | 3926 | struct btrfs_key key; |
aec7477b | 3927 | u64 index; |
33345d01 LZ |
3928 | u64 ino = btrfs_ino(inode); |
3929 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3930 | |
3931 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3932 | if (!path) { |
3933 | ret = -ENOMEM; | |
554233a6 | 3934 | goto out; |
54aa1f4d CM |
3935 | } |
3936 | ||
b9473439 | 3937 | path->leave_spinning = 1; |
33345d01 | 3938 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
3939 | name, name_len, -1); |
3940 | if (IS_ERR(di)) { | |
3941 | ret = PTR_ERR(di); | |
3942 | goto err; | |
3943 | } | |
3944 | if (!di) { | |
3945 | ret = -ENOENT; | |
3946 | goto err; | |
3947 | } | |
5f39d397 CM |
3948 | leaf = path->nodes[0]; |
3949 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 3950 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3951 | if (ret) |
3952 | goto err; | |
b3b4aa74 | 3953 | btrfs_release_path(path); |
39279cc3 | 3954 | |
67de1176 MX |
3955 | /* |
3956 | * If we don't have dir index, we have to get it by looking up | |
3957 | * the inode ref, since we get the inode ref, remove it directly, | |
3958 | * it is unnecessary to do delayed deletion. | |
3959 | * | |
3960 | * But if we have dir index, needn't search inode ref to get it. | |
3961 | * Since the inode ref is close to the inode item, it is better | |
3962 | * that we delay to delete it, and just do this deletion when | |
3963 | * we update the inode item. | |
3964 | */ | |
3965 | if (BTRFS_I(inode)->dir_index) { | |
3966 | ret = btrfs_delayed_delete_inode_ref(inode); | |
3967 | if (!ret) { | |
3968 | index = BTRFS_I(inode)->dir_index; | |
3969 | goto skip_backref; | |
3970 | } | |
3971 | } | |
3972 | ||
33345d01 LZ |
3973 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
3974 | dir_ino, &index); | |
aec7477b | 3975 | if (ret) { |
c2cf52eb SK |
3976 | btrfs_info(root->fs_info, |
3977 | "failed to delete reference to %.*s, inode %llu parent %llu", | |
c1c9ff7c | 3978 | name_len, name, ino, dir_ino); |
79787eaa | 3979 | btrfs_abort_transaction(trans, root, ret); |
aec7477b JB |
3980 | goto err; |
3981 | } | |
67de1176 | 3982 | skip_backref: |
16cdcec7 | 3983 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
3984 | if (ret) { |
3985 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 3986 | goto err; |
79787eaa | 3987 | } |
39279cc3 | 3988 | |
e02119d5 | 3989 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
33345d01 | 3990 | inode, dir_ino); |
79787eaa JM |
3991 | if (ret != 0 && ret != -ENOENT) { |
3992 | btrfs_abort_transaction(trans, root, ret); | |
3993 | goto err; | |
3994 | } | |
e02119d5 CM |
3995 | |
3996 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | |
3997 | dir, index); | |
6418c961 CM |
3998 | if (ret == -ENOENT) |
3999 | ret = 0; | |
d4e3991b ZB |
4000 | else if (ret) |
4001 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 CM |
4002 | err: |
4003 | btrfs_free_path(path); | |
e02119d5 CM |
4004 | if (ret) |
4005 | goto out; | |
4006 | ||
4007 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 JB |
4008 | inode_inc_iversion(inode); |
4009 | inode_inc_iversion(dir); | |
e02119d5 | 4010 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
b9959295 | 4011 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 4012 | out: |
39279cc3 CM |
4013 | return ret; |
4014 | } | |
4015 | ||
92986796 AV |
4016 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
4017 | struct btrfs_root *root, | |
4018 | struct inode *dir, struct inode *inode, | |
4019 | const char *name, int name_len) | |
4020 | { | |
4021 | int ret; | |
4022 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
4023 | if (!ret) { | |
8b558c5f | 4024 | drop_nlink(inode); |
92986796 AV |
4025 | ret = btrfs_update_inode(trans, root, inode); |
4026 | } | |
4027 | return ret; | |
4028 | } | |
39279cc3 | 4029 | |
a22285a6 YZ |
4030 | /* |
4031 | * helper to start transaction for unlink and rmdir. | |
4032 | * | |
d52be818 JB |
4033 | * unlink and rmdir are special in btrfs, they do not always free space, so |
4034 | * if we cannot make our reservations the normal way try and see if there is | |
4035 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
4036 | * allow the unlink to occur. | |
a22285a6 | 4037 | */ |
d52be818 | 4038 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 4039 | { |
a22285a6 | 4040 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d | 4041 | |
e70bea5f JB |
4042 | /* |
4043 | * 1 for the possible orphan item | |
4044 | * 1 for the dir item | |
4045 | * 1 for the dir index | |
4046 | * 1 for the inode ref | |
e70bea5f JB |
4047 | * 1 for the inode |
4048 | */ | |
8eab77ff | 4049 | return btrfs_start_transaction_fallback_global_rsv(root, 5, 5); |
a22285a6 YZ |
4050 | } |
4051 | ||
4052 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
4053 | { | |
4054 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4055 | struct btrfs_trans_handle *trans; | |
2b0143b5 | 4056 | struct inode *inode = d_inode(dentry); |
a22285a6 | 4057 | int ret; |
a22285a6 | 4058 | |
d52be818 | 4059 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
4060 | if (IS_ERR(trans)) |
4061 | return PTR_ERR(trans); | |
5f39d397 | 4062 | |
2b0143b5 | 4063 | btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0); |
12fcfd22 | 4064 | |
2b0143b5 | 4065 | ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4066 | dentry->d_name.name, dentry->d_name.len); |
b532402e TI |
4067 | if (ret) |
4068 | goto out; | |
7b128766 | 4069 | |
a22285a6 | 4070 | if (inode->i_nlink == 0) { |
7b128766 | 4071 | ret = btrfs_orphan_add(trans, inode); |
b532402e TI |
4072 | if (ret) |
4073 | goto out; | |
a22285a6 | 4074 | } |
7b128766 | 4075 | |
b532402e | 4076 | out: |
d52be818 | 4077 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4078 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
4079 | return ret; |
4080 | } | |
4081 | ||
4df27c4d YZ |
4082 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
4083 | struct btrfs_root *root, | |
4084 | struct inode *dir, u64 objectid, | |
4085 | const char *name, int name_len) | |
4086 | { | |
4087 | struct btrfs_path *path; | |
4088 | struct extent_buffer *leaf; | |
4089 | struct btrfs_dir_item *di; | |
4090 | struct btrfs_key key; | |
4091 | u64 index; | |
4092 | int ret; | |
33345d01 | 4093 | u64 dir_ino = btrfs_ino(dir); |
4df27c4d YZ |
4094 | |
4095 | path = btrfs_alloc_path(); | |
4096 | if (!path) | |
4097 | return -ENOMEM; | |
4098 | ||
33345d01 | 4099 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 4100 | name, name_len, -1); |
79787eaa JM |
4101 | if (IS_ERR_OR_NULL(di)) { |
4102 | if (!di) | |
4103 | ret = -ENOENT; | |
4104 | else | |
4105 | ret = PTR_ERR(di); | |
4106 | goto out; | |
4107 | } | |
4df27c4d YZ |
4108 | |
4109 | leaf = path->nodes[0]; | |
4110 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
4111 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
4112 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa JM |
4113 | if (ret) { |
4114 | btrfs_abort_transaction(trans, root, ret); | |
4115 | goto out; | |
4116 | } | |
b3b4aa74 | 4117 | btrfs_release_path(path); |
4df27c4d YZ |
4118 | |
4119 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
4120 | objectid, root->root_key.objectid, | |
33345d01 | 4121 | dir_ino, &index, name, name_len); |
4df27c4d | 4122 | if (ret < 0) { |
79787eaa JM |
4123 | if (ret != -ENOENT) { |
4124 | btrfs_abort_transaction(trans, root, ret); | |
4125 | goto out; | |
4126 | } | |
33345d01 | 4127 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 4128 | name, name_len); |
79787eaa JM |
4129 | if (IS_ERR_OR_NULL(di)) { |
4130 | if (!di) | |
4131 | ret = -ENOENT; | |
4132 | else | |
4133 | ret = PTR_ERR(di); | |
4134 | btrfs_abort_transaction(trans, root, ret); | |
4135 | goto out; | |
4136 | } | |
4df27c4d YZ |
4137 | |
4138 | leaf = path->nodes[0]; | |
4139 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 4140 | btrfs_release_path(path); |
4df27c4d YZ |
4141 | index = key.offset; |
4142 | } | |
945d8962 | 4143 | btrfs_release_path(path); |
4df27c4d | 4144 | |
16cdcec7 | 4145 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
4146 | if (ret) { |
4147 | btrfs_abort_transaction(trans, root, ret); | |
4148 | goto out; | |
4149 | } | |
4df27c4d YZ |
4150 | |
4151 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 | 4152 | inode_inc_iversion(dir); |
4df27c4d | 4153 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
5a24e84c | 4154 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa JM |
4155 | if (ret) |
4156 | btrfs_abort_transaction(trans, root, ret); | |
4157 | out: | |
71d7aed0 | 4158 | btrfs_free_path(path); |
79787eaa | 4159 | return ret; |
4df27c4d YZ |
4160 | } |
4161 | ||
39279cc3 CM |
4162 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
4163 | { | |
2b0143b5 | 4164 | struct inode *inode = d_inode(dentry); |
1832a6d5 | 4165 | int err = 0; |
39279cc3 | 4166 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 4167 | struct btrfs_trans_handle *trans; |
39279cc3 | 4168 | |
b3ae244e | 4169 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 4170 | return -ENOTEMPTY; |
b3ae244e DS |
4171 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
4172 | return -EPERM; | |
134d4512 | 4173 | |
d52be818 | 4174 | trans = __unlink_start_trans(dir); |
a22285a6 | 4175 | if (IS_ERR(trans)) |
5df6a9f6 | 4176 | return PTR_ERR(trans); |
5df6a9f6 | 4177 | |
33345d01 | 4178 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
4179 | err = btrfs_unlink_subvol(trans, root, dir, |
4180 | BTRFS_I(inode)->location.objectid, | |
4181 | dentry->d_name.name, | |
4182 | dentry->d_name.len); | |
4183 | goto out; | |
4184 | } | |
4185 | ||
7b128766 JB |
4186 | err = btrfs_orphan_add(trans, inode); |
4187 | if (err) | |
4df27c4d | 4188 | goto out; |
7b128766 | 4189 | |
39279cc3 | 4190 | /* now the directory is empty */ |
2b0143b5 | 4191 | err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry), |
e02119d5 | 4192 | dentry->d_name.name, dentry->d_name.len); |
d397712b | 4193 | if (!err) |
dbe674a9 | 4194 | btrfs_i_size_write(inode, 0); |
4df27c4d | 4195 | out: |
d52be818 | 4196 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4197 | btrfs_btree_balance_dirty(root); |
3954401f | 4198 | |
39279cc3 CM |
4199 | return err; |
4200 | } | |
4201 | ||
28f75a0e CM |
4202 | static int truncate_space_check(struct btrfs_trans_handle *trans, |
4203 | struct btrfs_root *root, | |
4204 | u64 bytes_deleted) | |
4205 | { | |
4206 | int ret; | |
4207 | ||
4208 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); | |
4209 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, | |
4210 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); | |
4211 | if (!ret) | |
4212 | trans->bytes_reserved += bytes_deleted; | |
4213 | return ret; | |
4214 | ||
4215 | } | |
4216 | ||
0305cd5f FM |
4217 | static int truncate_inline_extent(struct inode *inode, |
4218 | struct btrfs_path *path, | |
4219 | struct btrfs_key *found_key, | |
4220 | const u64 item_end, | |
4221 | const u64 new_size) | |
4222 | { | |
4223 | struct extent_buffer *leaf = path->nodes[0]; | |
4224 | int slot = path->slots[0]; | |
4225 | struct btrfs_file_extent_item *fi; | |
4226 | u32 size = (u32)(new_size - found_key->offset); | |
4227 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4228 | ||
4229 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
4230 | ||
4231 | if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) { | |
4232 | loff_t offset = new_size; | |
4233 | loff_t page_end = ALIGN(offset, PAGE_CACHE_SIZE); | |
4234 | ||
4235 | /* | |
4236 | * Zero out the remaining of the last page of our inline extent, | |
4237 | * instead of directly truncating our inline extent here - that | |
4238 | * would be much more complex (decompressing all the data, then | |
4239 | * compressing the truncated data, which might be bigger than | |
4240 | * the size of the inline extent, resize the extent, etc). | |
4241 | * We release the path because to get the page we might need to | |
4242 | * read the extent item from disk (data not in the page cache). | |
4243 | */ | |
4244 | btrfs_release_path(path); | |
4245 | return btrfs_truncate_page(inode, offset, page_end - offset, 0); | |
4246 | } | |
4247 | ||
4248 | btrfs_set_file_extent_ram_bytes(leaf, fi, size); | |
4249 | size = btrfs_file_extent_calc_inline_size(size); | |
4250 | btrfs_truncate_item(root, path, size, 1); | |
4251 | ||
4252 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4253 | inode_sub_bytes(inode, item_end + 1 - new_size); | |
4254 | ||
4255 | return 0; | |
4256 | } | |
4257 | ||
39279cc3 CM |
4258 | /* |
4259 | * this can truncate away extent items, csum items and directory items. | |
4260 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 4261 | * any higher than new_size |
39279cc3 CM |
4262 | * |
4263 | * csum items that cross the new i_size are truncated to the new size | |
4264 | * as well. | |
7b128766 JB |
4265 | * |
4266 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
4267 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 4268 | */ |
8082510e YZ |
4269 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
4270 | struct btrfs_root *root, | |
4271 | struct inode *inode, | |
4272 | u64 new_size, u32 min_type) | |
39279cc3 | 4273 | { |
39279cc3 | 4274 | struct btrfs_path *path; |
5f39d397 | 4275 | struct extent_buffer *leaf; |
39279cc3 | 4276 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
4277 | struct btrfs_key key; |
4278 | struct btrfs_key found_key; | |
39279cc3 | 4279 | u64 extent_start = 0; |
db94535d | 4280 | u64 extent_num_bytes = 0; |
5d4f98a2 | 4281 | u64 extent_offset = 0; |
39279cc3 | 4282 | u64 item_end = 0; |
c1aa4575 | 4283 | u64 last_size = new_size; |
8082510e | 4284 | u32 found_type = (u8)-1; |
39279cc3 CM |
4285 | int found_extent; |
4286 | int del_item; | |
85e21bac CM |
4287 | int pending_del_nr = 0; |
4288 | int pending_del_slot = 0; | |
179e29e4 | 4289 | int extent_type = -1; |
8082510e YZ |
4290 | int ret; |
4291 | int err = 0; | |
33345d01 | 4292 | u64 ino = btrfs_ino(inode); |
28ed1345 | 4293 | u64 bytes_deleted = 0; |
1262133b JB |
4294 | bool be_nice = 0; |
4295 | bool should_throttle = 0; | |
28f75a0e | 4296 | bool should_end = 0; |
8082510e YZ |
4297 | |
4298 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 4299 | |
28ed1345 CM |
4300 | /* |
4301 | * for non-free space inodes and ref cows, we want to back off from | |
4302 | * time to time | |
4303 | */ | |
4304 | if (!btrfs_is_free_space_inode(inode) && | |
4305 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | |
4306 | be_nice = 1; | |
4307 | ||
0eb0e19c MF |
4308 | path = btrfs_alloc_path(); |
4309 | if (!path) | |
4310 | return -ENOMEM; | |
4311 | path->reada = -1; | |
4312 | ||
5dc562c5 JB |
4313 | /* |
4314 | * We want to drop from the next block forward in case this new size is | |
4315 | * not block aligned since we will be keeping the last block of the | |
4316 | * extent just the way it is. | |
4317 | */ | |
27cdeb70 MX |
4318 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
4319 | root == root->fs_info->tree_root) | |
fda2832f QW |
4320 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
4321 | root->sectorsize), (u64)-1, 0); | |
8082510e | 4322 | |
16cdcec7 MX |
4323 | /* |
4324 | * This function is also used to drop the items in the log tree before | |
4325 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
4326 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
4327 | * items. | |
4328 | */ | |
4329 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
4330 | btrfs_kill_delayed_inode_items(inode); | |
4331 | ||
33345d01 | 4332 | key.objectid = ino; |
39279cc3 | 4333 | key.offset = (u64)-1; |
5f39d397 CM |
4334 | key.type = (u8)-1; |
4335 | ||
85e21bac | 4336 | search_again: |
28ed1345 CM |
4337 | /* |
4338 | * with a 16K leaf size and 128MB extents, you can actually queue | |
4339 | * up a huge file in a single leaf. Most of the time that | |
4340 | * bytes_deleted is > 0, it will be huge by the time we get here | |
4341 | */ | |
4342 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { | |
4343 | if (btrfs_should_end_transaction(trans, root)) { | |
4344 | err = -EAGAIN; | |
4345 | goto error; | |
4346 | } | |
4347 | } | |
4348 | ||
4349 | ||
b9473439 | 4350 | path->leave_spinning = 1; |
85e21bac | 4351 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
4352 | if (ret < 0) { |
4353 | err = ret; | |
4354 | goto out; | |
4355 | } | |
d397712b | 4356 | |
85e21bac | 4357 | if (ret > 0) { |
e02119d5 CM |
4358 | /* there are no items in the tree for us to truncate, we're |
4359 | * done | |
4360 | */ | |
8082510e YZ |
4361 | if (path->slots[0] == 0) |
4362 | goto out; | |
85e21bac CM |
4363 | path->slots[0]--; |
4364 | } | |
4365 | ||
d397712b | 4366 | while (1) { |
39279cc3 | 4367 | fi = NULL; |
5f39d397 CM |
4368 | leaf = path->nodes[0]; |
4369 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
962a298f | 4370 | found_type = found_key.type; |
39279cc3 | 4371 | |
33345d01 | 4372 | if (found_key.objectid != ino) |
39279cc3 | 4373 | break; |
5f39d397 | 4374 | |
85e21bac | 4375 | if (found_type < min_type) |
39279cc3 CM |
4376 | break; |
4377 | ||
5f39d397 | 4378 | item_end = found_key.offset; |
39279cc3 | 4379 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4380 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4381 | struct btrfs_file_extent_item); |
179e29e4 CM |
4382 | extent_type = btrfs_file_extent_type(leaf, fi); |
4383 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4384 | item_end += |
db94535d | 4385 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 | 4386 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4387 | item_end += btrfs_file_extent_inline_len(leaf, |
514ac8ad | 4388 | path->slots[0], fi); |
39279cc3 | 4389 | } |
008630c1 | 4390 | item_end--; |
39279cc3 | 4391 | } |
8082510e YZ |
4392 | if (found_type > min_type) { |
4393 | del_item = 1; | |
4394 | } else { | |
4395 | if (item_end < new_size) | |
b888db2b | 4396 | break; |
8082510e YZ |
4397 | if (found_key.offset >= new_size) |
4398 | del_item = 1; | |
4399 | else | |
4400 | del_item = 0; | |
39279cc3 | 4401 | } |
39279cc3 | 4402 | found_extent = 0; |
39279cc3 | 4403 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4404 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4405 | goto delete; | |
4406 | ||
7f4f6e0a JB |
4407 | if (del_item) |
4408 | last_size = found_key.offset; | |
4409 | else | |
4410 | last_size = new_size; | |
4411 | ||
179e29e4 | 4412 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4413 | u64 num_dec; |
db94535d | 4414 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4415 | if (!del_item) { |
db94535d CM |
4416 | u64 orig_num_bytes = |
4417 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4418 | extent_num_bytes = ALIGN(new_size - |
4419 | found_key.offset, | |
4420 | root->sectorsize); | |
db94535d CM |
4421 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4422 | extent_num_bytes); | |
4423 | num_dec = (orig_num_bytes - | |
9069218d | 4424 | extent_num_bytes); |
27cdeb70 MX |
4425 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4426 | &root->state) && | |
4427 | extent_start != 0) | |
a76a3cd4 | 4428 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4429 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4430 | } else { |
db94535d CM |
4431 | extent_num_bytes = |
4432 | btrfs_file_extent_disk_num_bytes(leaf, | |
4433 | fi); | |
5d4f98a2 YZ |
4434 | extent_offset = found_key.offset - |
4435 | btrfs_file_extent_offset(leaf, fi); | |
4436 | ||
39279cc3 | 4437 | /* FIXME blocksize != 4096 */ |
9069218d | 4438 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4439 | if (extent_start != 0) { |
4440 | found_extent = 1; | |
27cdeb70 MX |
4441 | if (test_bit(BTRFS_ROOT_REF_COWS, |
4442 | &root->state)) | |
a76a3cd4 | 4443 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4444 | } |
39279cc3 | 4445 | } |
9069218d | 4446 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4447 | /* |
4448 | * we can't truncate inline items that have had | |
4449 | * special encodings | |
4450 | */ | |
4451 | if (!del_item && | |
c8b97818 CM |
4452 | btrfs_file_extent_encryption(leaf, fi) == 0 && |
4453 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
514ac8ad CM |
4454 | |
4455 | /* | |
0305cd5f FM |
4456 | * Need to release path in order to truncate a |
4457 | * compressed extent. So delete any accumulated | |
4458 | * extent items so far. | |
514ac8ad | 4459 | */ |
0305cd5f FM |
4460 | if (btrfs_file_extent_compression(leaf, fi) != |
4461 | BTRFS_COMPRESS_NONE && pending_del_nr) { | |
4462 | err = btrfs_del_items(trans, root, path, | |
4463 | pending_del_slot, | |
4464 | pending_del_nr); | |
4465 | if (err) { | |
4466 | btrfs_abort_transaction(trans, | |
4467 | root, | |
4468 | err); | |
4469 | goto error; | |
4470 | } | |
4471 | pending_del_nr = 0; | |
4472 | } | |
4473 | ||
4474 | err = truncate_inline_extent(inode, path, | |
4475 | &found_key, | |
4476 | item_end, | |
4477 | new_size); | |
4478 | if (err) { | |
4479 | btrfs_abort_transaction(trans, | |
4480 | root, err); | |
4481 | goto error; | |
4482 | } | |
27cdeb70 MX |
4483 | } else if (test_bit(BTRFS_ROOT_REF_COWS, |
4484 | &root->state)) { | |
0305cd5f | 4485 | inode_sub_bytes(inode, item_end + 1 - new_size); |
9069218d | 4486 | } |
39279cc3 | 4487 | } |
179e29e4 | 4488 | delete: |
39279cc3 | 4489 | if (del_item) { |
85e21bac CM |
4490 | if (!pending_del_nr) { |
4491 | /* no pending yet, add ourselves */ | |
4492 | pending_del_slot = path->slots[0]; | |
4493 | pending_del_nr = 1; | |
4494 | } else if (pending_del_nr && | |
4495 | path->slots[0] + 1 == pending_del_slot) { | |
4496 | /* hop on the pending chunk */ | |
4497 | pending_del_nr++; | |
4498 | pending_del_slot = path->slots[0]; | |
4499 | } else { | |
d397712b | 4500 | BUG(); |
85e21bac | 4501 | } |
39279cc3 CM |
4502 | } else { |
4503 | break; | |
4504 | } | |
28f75a0e CM |
4505 | should_throttle = 0; |
4506 | ||
27cdeb70 MX |
4507 | if (found_extent && |
4508 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | |
4509 | root == root->fs_info->tree_root)) { | |
b9473439 | 4510 | btrfs_set_path_blocking(path); |
28ed1345 | 4511 | bytes_deleted += extent_num_bytes; |
39279cc3 | 4512 | ret = btrfs_free_extent(trans, root, extent_start, |
5d4f98a2 YZ |
4513 | extent_num_bytes, 0, |
4514 | btrfs_header_owner(leaf), | |
b06c4bf5 | 4515 | ino, extent_offset); |
39279cc3 | 4516 | BUG_ON(ret); |
1262133b | 4517 | if (btrfs_should_throttle_delayed_refs(trans, root)) |
28ed1345 CM |
4518 | btrfs_async_run_delayed_refs(root, |
4519 | trans->delayed_ref_updates * 2, 0); | |
28f75a0e CM |
4520 | if (be_nice) { |
4521 | if (truncate_space_check(trans, root, | |
4522 | extent_num_bytes)) { | |
4523 | should_end = 1; | |
4524 | } | |
4525 | if (btrfs_should_throttle_delayed_refs(trans, | |
4526 | root)) { | |
4527 | should_throttle = 1; | |
4528 | } | |
4529 | } | |
39279cc3 | 4530 | } |
85e21bac | 4531 | |
8082510e YZ |
4532 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4533 | break; | |
4534 | ||
4535 | if (path->slots[0] == 0 || | |
1262133b | 4536 | path->slots[0] != pending_del_slot || |
28f75a0e | 4537 | should_throttle || should_end) { |
8082510e YZ |
4538 | if (pending_del_nr) { |
4539 | ret = btrfs_del_items(trans, root, path, | |
4540 | pending_del_slot, | |
4541 | pending_del_nr); | |
79787eaa JM |
4542 | if (ret) { |
4543 | btrfs_abort_transaction(trans, | |
4544 | root, ret); | |
4545 | goto error; | |
4546 | } | |
8082510e YZ |
4547 | pending_del_nr = 0; |
4548 | } | |
b3b4aa74 | 4549 | btrfs_release_path(path); |
28f75a0e | 4550 | if (should_throttle) { |
1262133b JB |
4551 | unsigned long updates = trans->delayed_ref_updates; |
4552 | if (updates) { | |
4553 | trans->delayed_ref_updates = 0; | |
4554 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4555 | if (ret && !err) | |
4556 | err = ret; | |
4557 | } | |
4558 | } | |
28f75a0e CM |
4559 | /* |
4560 | * if we failed to refill our space rsv, bail out | |
4561 | * and let the transaction restart | |
4562 | */ | |
4563 | if (should_end) { | |
4564 | err = -EAGAIN; | |
4565 | goto error; | |
4566 | } | |
85e21bac | 4567 | goto search_again; |
8082510e YZ |
4568 | } else { |
4569 | path->slots[0]--; | |
85e21bac | 4570 | } |
39279cc3 | 4571 | } |
8082510e | 4572 | out: |
85e21bac CM |
4573 | if (pending_del_nr) { |
4574 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4575 | pending_del_nr); | |
79787eaa JM |
4576 | if (ret) |
4577 | btrfs_abort_transaction(trans, root, ret); | |
85e21bac | 4578 | } |
79787eaa | 4579 | error: |
c1aa4575 | 4580 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
7f4f6e0a | 4581 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
28ed1345 | 4582 | |
39279cc3 | 4583 | btrfs_free_path(path); |
28ed1345 | 4584 | |
28f75a0e | 4585 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { |
28ed1345 CM |
4586 | unsigned long updates = trans->delayed_ref_updates; |
4587 | if (updates) { | |
4588 | trans->delayed_ref_updates = 0; | |
4589 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | |
4590 | if (ret && !err) | |
4591 | err = ret; | |
4592 | } | |
4593 | } | |
8082510e | 4594 | return err; |
39279cc3 CM |
4595 | } |
4596 | ||
4597 | /* | |
2aaa6655 JB |
4598 | * btrfs_truncate_page - read, zero a chunk and write a page |
4599 | * @inode - inode that we're zeroing | |
4600 | * @from - the offset to start zeroing | |
4601 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4602 | * offset | |
4603 | * @front - zero up to the offset instead of from the offset on | |
4604 | * | |
4605 | * This will find the page for the "from" offset and cow the page and zero the | |
4606 | * part we want to zero. This is used with truncate and hole punching. | |
39279cc3 | 4607 | */ |
2aaa6655 JB |
4608 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
4609 | int front) | |
39279cc3 | 4610 | { |
2aaa6655 | 4611 | struct address_space *mapping = inode->i_mapping; |
db94535d | 4612 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
4613 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4614 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4615 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 4616 | char *kaddr; |
db94535d | 4617 | u32 blocksize = root->sectorsize; |
39279cc3 CM |
4618 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
4619 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
4620 | struct page *page; | |
3b16a4e3 | 4621 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4622 | int ret = 0; |
a52d9a80 | 4623 | u64 page_start; |
e6dcd2dc | 4624 | u64 page_end; |
39279cc3 | 4625 | |
2aaa6655 JB |
4626 | if ((offset & (blocksize - 1)) == 0 && |
4627 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4628 | goto out; |
7cf5b976 | 4629 | ret = btrfs_delalloc_reserve_space(inode, |
df480633 | 4630 | round_down(from, PAGE_CACHE_SIZE), PAGE_CACHE_SIZE); |
5d5e103a JB |
4631 | if (ret) |
4632 | goto out; | |
39279cc3 | 4633 | |
211c17f5 | 4634 | again: |
3b16a4e3 | 4635 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4636 | if (!page) { |
7cf5b976 | 4637 | btrfs_delalloc_release_space(inode, |
df480633 QW |
4638 | round_down(from, PAGE_CACHE_SIZE), |
4639 | PAGE_CACHE_SIZE); | |
ac6a2b36 | 4640 | ret = -ENOMEM; |
39279cc3 | 4641 | goto out; |
5d5e103a | 4642 | } |
e6dcd2dc CM |
4643 | |
4644 | page_start = page_offset(page); | |
4645 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
4646 | ||
39279cc3 | 4647 | if (!PageUptodate(page)) { |
9ebefb18 | 4648 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4649 | lock_page(page); |
211c17f5 CM |
4650 | if (page->mapping != mapping) { |
4651 | unlock_page(page); | |
4652 | page_cache_release(page); | |
4653 | goto again; | |
4654 | } | |
39279cc3 CM |
4655 | if (!PageUptodate(page)) { |
4656 | ret = -EIO; | |
89642229 | 4657 | goto out_unlock; |
39279cc3 CM |
4658 | } |
4659 | } | |
211c17f5 | 4660 | wait_on_page_writeback(page); |
e6dcd2dc | 4661 | |
d0082371 | 4662 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
4663 | set_page_extent_mapped(page); |
4664 | ||
4665 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
4666 | if (ordered) { | |
2ac55d41 JB |
4667 | unlock_extent_cached(io_tree, page_start, page_end, |
4668 | &cached_state, GFP_NOFS); | |
e6dcd2dc CM |
4669 | unlock_page(page); |
4670 | page_cache_release(page); | |
eb84ae03 | 4671 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4672 | btrfs_put_ordered_extent(ordered); |
4673 | goto again; | |
4674 | } | |
4675 | ||
2ac55d41 | 4676 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
4677 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4678 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4679 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4680 | |
2ac55d41 JB |
4681 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
4682 | &cached_state); | |
9ed74f2d | 4683 | if (ret) { |
2ac55d41 JB |
4684 | unlock_extent_cached(io_tree, page_start, page_end, |
4685 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
4686 | goto out_unlock; |
4687 | } | |
4688 | ||
e6dcd2dc | 4689 | if (offset != PAGE_CACHE_SIZE) { |
2aaa6655 JB |
4690 | if (!len) |
4691 | len = PAGE_CACHE_SIZE - offset; | |
e6dcd2dc | 4692 | kaddr = kmap(page); |
2aaa6655 JB |
4693 | if (front) |
4694 | memset(kaddr, 0, offset); | |
4695 | else | |
4696 | memset(kaddr + offset, 0, len); | |
e6dcd2dc CM |
4697 | flush_dcache_page(page); |
4698 | kunmap(page); | |
4699 | } | |
247e743c | 4700 | ClearPageChecked(page); |
e6dcd2dc | 4701 | set_page_dirty(page); |
2ac55d41 JB |
4702 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
4703 | GFP_NOFS); | |
39279cc3 | 4704 | |
89642229 | 4705 | out_unlock: |
5d5e103a | 4706 | if (ret) |
7cf5b976 QW |
4707 | btrfs_delalloc_release_space(inode, page_start, |
4708 | PAGE_CACHE_SIZE); | |
39279cc3 CM |
4709 | unlock_page(page); |
4710 | page_cache_release(page); | |
4711 | out: | |
4712 | return ret; | |
4713 | } | |
4714 | ||
16e7549f JB |
4715 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4716 | u64 offset, u64 len) | |
4717 | { | |
4718 | struct btrfs_trans_handle *trans; | |
4719 | int ret; | |
4720 | ||
4721 | /* | |
4722 | * Still need to make sure the inode looks like it's been updated so | |
4723 | * that any holes get logged if we fsync. | |
4724 | */ | |
4725 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { | |
4726 | BTRFS_I(inode)->last_trans = root->fs_info->generation; | |
4727 | BTRFS_I(inode)->last_sub_trans = root->log_transid; | |
4728 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4729 | return 0; | |
4730 | } | |
4731 | ||
4732 | /* | |
4733 | * 1 - for the one we're dropping | |
4734 | * 1 - for the one we're adding | |
4735 | * 1 - for updating the inode. | |
4736 | */ | |
4737 | trans = btrfs_start_transaction(root, 3); | |
4738 | if (IS_ERR(trans)) | |
4739 | return PTR_ERR(trans); | |
4740 | ||
4741 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4742 | if (ret) { | |
4743 | btrfs_abort_transaction(trans, root, ret); | |
4744 | btrfs_end_transaction(trans, root); | |
4745 | return ret; | |
4746 | } | |
4747 | ||
4748 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, | |
4749 | 0, 0, len, 0, len, 0, 0, 0); | |
4750 | if (ret) | |
4751 | btrfs_abort_transaction(trans, root, ret); | |
4752 | else | |
4753 | btrfs_update_inode(trans, root, inode); | |
4754 | btrfs_end_transaction(trans, root); | |
4755 | return ret; | |
4756 | } | |
4757 | ||
695a0d0d JB |
4758 | /* |
4759 | * This function puts in dummy file extents for the area we're creating a hole | |
4760 | * for. So if we are truncating this file to a larger size we need to insert | |
4761 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4762 | * the range between oldsize and size | |
4763 | */ | |
a41ad394 | 4764 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4765 | { |
9036c102 YZ |
4766 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4767 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4768 | struct extent_map *em = NULL; |
2ac55d41 | 4769 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4770 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
fda2832f QW |
4771 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
4772 | u64 block_end = ALIGN(size, root->sectorsize); | |
9036c102 YZ |
4773 | u64 last_byte; |
4774 | u64 cur_offset; | |
4775 | u64 hole_size; | |
9ed74f2d | 4776 | int err = 0; |
39279cc3 | 4777 | |
a71754fc JB |
4778 | /* |
4779 | * If our size started in the middle of a page we need to zero out the | |
4780 | * rest of the page before we expand the i_size, otherwise we could | |
4781 | * expose stale data. | |
4782 | */ | |
4783 | err = btrfs_truncate_page(inode, oldsize, 0, 0); | |
4784 | if (err) | |
4785 | return err; | |
4786 | ||
9036c102 YZ |
4787 | if (size <= hole_start) |
4788 | return 0; | |
4789 | ||
9036c102 YZ |
4790 | while (1) { |
4791 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4792 | |
2ac55d41 | 4793 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
d0082371 | 4794 | &cached_state); |
fa7c1494 MX |
4795 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
4796 | block_end - hole_start); | |
9036c102 YZ |
4797 | if (!ordered) |
4798 | break; | |
2ac55d41 JB |
4799 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4800 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4801 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4802 | btrfs_put_ordered_extent(ordered); |
4803 | } | |
39279cc3 | 4804 | |
9036c102 YZ |
4805 | cur_offset = hole_start; |
4806 | while (1) { | |
4807 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
4808 | block_end - cur_offset, 0); | |
79787eaa JM |
4809 | if (IS_ERR(em)) { |
4810 | err = PTR_ERR(em); | |
f2767956 | 4811 | em = NULL; |
79787eaa JM |
4812 | break; |
4813 | } | |
9036c102 | 4814 | last_byte = min(extent_map_end(em), block_end); |
fda2832f | 4815 | last_byte = ALIGN(last_byte , root->sectorsize); |
8082510e | 4816 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4817 | struct extent_map *hole_em; |
9036c102 | 4818 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4819 | |
16e7549f JB |
4820 | err = maybe_insert_hole(root, inode, cur_offset, |
4821 | hole_size); | |
4822 | if (err) | |
3893e33b | 4823 | break; |
5dc562c5 JB |
4824 | btrfs_drop_extent_cache(inode, cur_offset, |
4825 | cur_offset + hole_size - 1, 0); | |
4826 | hole_em = alloc_extent_map(); | |
4827 | if (!hole_em) { | |
4828 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4829 | &BTRFS_I(inode)->runtime_flags); | |
4830 | goto next; | |
4831 | } | |
4832 | hole_em->start = cur_offset; | |
4833 | hole_em->len = hole_size; | |
4834 | hole_em->orig_start = cur_offset; | |
8082510e | 4835 | |
5dc562c5 JB |
4836 | hole_em->block_start = EXTENT_MAP_HOLE; |
4837 | hole_em->block_len = 0; | |
b4939680 | 4838 | hole_em->orig_block_len = 0; |
cc95bef6 | 4839 | hole_em->ram_bytes = hole_size; |
5dc562c5 JB |
4840 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
4841 | hole_em->compress_type = BTRFS_COMPRESS_NONE; | |
16e7549f | 4842 | hole_em->generation = root->fs_info->generation; |
8082510e | 4843 | |
5dc562c5 JB |
4844 | while (1) { |
4845 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4846 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4847 | write_unlock(&em_tree->lock); |
4848 | if (err != -EEXIST) | |
4849 | break; | |
4850 | btrfs_drop_extent_cache(inode, cur_offset, | |
4851 | cur_offset + | |
4852 | hole_size - 1, 0); | |
4853 | } | |
4854 | free_extent_map(hole_em); | |
9036c102 | 4855 | } |
16e7549f | 4856 | next: |
9036c102 | 4857 | free_extent_map(em); |
a22285a6 | 4858 | em = NULL; |
9036c102 | 4859 | cur_offset = last_byte; |
8082510e | 4860 | if (cur_offset >= block_end) |
9036c102 YZ |
4861 | break; |
4862 | } | |
a22285a6 | 4863 | free_extent_map(em); |
2ac55d41 JB |
4864 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
4865 | GFP_NOFS); | |
9036c102 YZ |
4866 | return err; |
4867 | } | |
39279cc3 | 4868 | |
9ea24bbe FM |
4869 | static int wait_snapshoting_atomic_t(atomic_t *a) |
4870 | { | |
4871 | schedule(); | |
4872 | return 0; | |
4873 | } | |
4874 | ||
4875 | static void wait_for_snapshot_creation(struct btrfs_root *root) | |
4876 | { | |
4877 | while (true) { | |
4878 | int ret; | |
4879 | ||
4880 | ret = btrfs_start_write_no_snapshoting(root); | |
4881 | if (ret) | |
4882 | break; | |
4883 | wait_on_atomic_t(&root->will_be_snapshoted, | |
4884 | wait_snapshoting_atomic_t, | |
4885 | TASK_UNINTERRUPTIBLE); | |
4886 | } | |
4887 | } | |
4888 | ||
3972f260 | 4889 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4890 | { |
f4a2f4c5 MX |
4891 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4892 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4893 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4894 | loff_t newsize = attr->ia_size; |
4895 | int mask = attr->ia_valid; | |
8082510e YZ |
4896 | int ret; |
4897 | ||
3972f260 ES |
4898 | /* |
4899 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4900 | * special case where we need to update the times despite not having | |
4901 | * these flags set. For all other operations the VFS set these flags | |
4902 | * explicitly if it wants a timestamp update. | |
4903 | */ | |
dff6efc3 CH |
4904 | if (newsize != oldsize) { |
4905 | inode_inc_iversion(inode); | |
4906 | if (!(mask & (ATTR_CTIME | ATTR_MTIME))) | |
4907 | inode->i_ctime = inode->i_mtime = | |
4908 | current_fs_time(inode->i_sb); | |
4909 | } | |
3972f260 | 4910 | |
a41ad394 | 4911 | if (newsize > oldsize) { |
7caef267 | 4912 | truncate_pagecache(inode, newsize); |
9ea24bbe FM |
4913 | /* |
4914 | * Don't do an expanding truncate while snapshoting is ongoing. | |
4915 | * This is to ensure the snapshot captures a fully consistent | |
4916 | * state of this file - if the snapshot captures this expanding | |
4917 | * truncation, it must capture all writes that happened before | |
4918 | * this truncation. | |
4919 | */ | |
4920 | wait_for_snapshot_creation(root); | |
a41ad394 | 4921 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
9ea24bbe FM |
4922 | if (ret) { |
4923 | btrfs_end_write_no_snapshoting(root); | |
8082510e | 4924 | return ret; |
9ea24bbe | 4925 | } |
8082510e | 4926 | |
f4a2f4c5 | 4927 | trans = btrfs_start_transaction(root, 1); |
9ea24bbe FM |
4928 | if (IS_ERR(trans)) { |
4929 | btrfs_end_write_no_snapshoting(root); | |
f4a2f4c5 | 4930 | return PTR_ERR(trans); |
9ea24bbe | 4931 | } |
f4a2f4c5 MX |
4932 | |
4933 | i_size_write(inode, newsize); | |
4934 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
4935 | ret = btrfs_update_inode(trans, root, inode); | |
9ea24bbe | 4936 | btrfs_end_write_no_snapshoting(root); |
7ad85bb7 | 4937 | btrfs_end_transaction(trans, root); |
a41ad394 | 4938 | } else { |
8082510e | 4939 | |
a41ad394 JB |
4940 | /* |
4941 | * We're truncating a file that used to have good data down to | |
4942 | * zero. Make sure it gets into the ordered flush list so that | |
4943 | * any new writes get down to disk quickly. | |
4944 | */ | |
4945 | if (newsize == 0) | |
72ac3c0d JB |
4946 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
4947 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 4948 | |
f3fe820c JB |
4949 | /* |
4950 | * 1 for the orphan item we're going to add | |
4951 | * 1 for the orphan item deletion. | |
4952 | */ | |
4953 | trans = btrfs_start_transaction(root, 2); | |
4954 | if (IS_ERR(trans)) | |
4955 | return PTR_ERR(trans); | |
4956 | ||
4957 | /* | |
4958 | * We need to do this in case we fail at _any_ point during the | |
4959 | * actual truncate. Once we do the truncate_setsize we could | |
4960 | * invalidate pages which forces any outstanding ordered io to | |
4961 | * be instantly completed which will give us extents that need | |
4962 | * to be truncated. If we fail to get an orphan inode down we | |
4963 | * could have left over extents that were never meant to live, | |
4964 | * so we need to garuntee from this point on that everything | |
4965 | * will be consistent. | |
4966 | */ | |
4967 | ret = btrfs_orphan_add(trans, inode); | |
4968 | btrfs_end_transaction(trans, root); | |
4969 | if (ret) | |
4970 | return ret; | |
4971 | ||
a41ad394 JB |
4972 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
4973 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
4974 | |
4975 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
4976 | btrfs_inode_block_unlocked_dio(inode); | |
4977 | inode_dio_wait(inode); | |
4978 | btrfs_inode_resume_unlocked_dio(inode); | |
4979 | ||
a41ad394 | 4980 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
4981 | if (ret && inode->i_nlink) { |
4982 | int err; | |
4983 | ||
4984 | /* | |
4985 | * failed to truncate, disk_i_size is only adjusted down | |
4986 | * as we remove extents, so it should represent the true | |
4987 | * size of the inode, so reset the in memory size and | |
4988 | * delete our orphan entry. | |
4989 | */ | |
4990 | trans = btrfs_join_transaction(root); | |
4991 | if (IS_ERR(trans)) { | |
4992 | btrfs_orphan_del(NULL, inode); | |
4993 | return ret; | |
4994 | } | |
4995 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
4996 | err = btrfs_orphan_del(trans, inode); | |
4997 | if (err) | |
4998 | btrfs_abort_transaction(trans, root, err); | |
4999 | btrfs_end_transaction(trans, root); | |
5000 | } | |
8082510e YZ |
5001 | } |
5002 | ||
a41ad394 | 5003 | return ret; |
8082510e YZ |
5004 | } |
5005 | ||
9036c102 YZ |
5006 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
5007 | { | |
2b0143b5 | 5008 | struct inode *inode = d_inode(dentry); |
b83cc969 | 5009 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 5010 | int err; |
39279cc3 | 5011 | |
b83cc969 LZ |
5012 | if (btrfs_root_readonly(root)) |
5013 | return -EROFS; | |
5014 | ||
9036c102 YZ |
5015 | err = inode_change_ok(inode, attr); |
5016 | if (err) | |
5017 | return err; | |
2bf5a725 | 5018 | |
5a3f23d5 | 5019 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 5020 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
5021 | if (err) |
5022 | return err; | |
39279cc3 | 5023 | } |
9036c102 | 5024 | |
1025774c CH |
5025 | if (attr->ia_valid) { |
5026 | setattr_copy(inode, attr); | |
0c4d2d95 | 5027 | inode_inc_iversion(inode); |
22c44fe6 | 5028 | err = btrfs_dirty_inode(inode); |
1025774c | 5029 | |
22c44fe6 | 5030 | if (!err && attr->ia_valid & ATTR_MODE) |
996a710d | 5031 | err = posix_acl_chmod(inode, inode->i_mode); |
1025774c | 5032 | } |
33268eaf | 5033 | |
39279cc3 CM |
5034 | return err; |
5035 | } | |
61295eb8 | 5036 | |
131e404a FDBM |
5037 | /* |
5038 | * While truncating the inode pages during eviction, we get the VFS calling | |
5039 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
5040 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
5041 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
5042 | * extent_state structures over and over, wasting lots of time. | |
5043 | * | |
5044 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
5045 | * those expensive operations on a per page basis and do only the ordered io | |
5046 | * finishing, while we release here the extent_map and extent_state structures, | |
5047 | * without the excessive merging and splitting. | |
5048 | */ | |
5049 | static void evict_inode_truncate_pages(struct inode *inode) | |
5050 | { | |
5051 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
5052 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
5053 | struct rb_node *node; | |
5054 | ||
5055 | ASSERT(inode->i_state & I_FREEING); | |
91b0abe3 | 5056 | truncate_inode_pages_final(&inode->i_data); |
131e404a FDBM |
5057 | |
5058 | write_lock(&map_tree->lock); | |
5059 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
5060 | struct extent_map *em; | |
5061 | ||
5062 | node = rb_first(&map_tree->map); | |
5063 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
5064 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
5065 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
5066 | remove_extent_mapping(map_tree, em); |
5067 | free_extent_map(em); | |
7064dd5c FM |
5068 | if (need_resched()) { |
5069 | write_unlock(&map_tree->lock); | |
5070 | cond_resched(); | |
5071 | write_lock(&map_tree->lock); | |
5072 | } | |
131e404a FDBM |
5073 | } |
5074 | write_unlock(&map_tree->lock); | |
5075 | ||
6ca07097 FM |
5076 | /* |
5077 | * Keep looping until we have no more ranges in the io tree. | |
5078 | * We can have ongoing bios started by readpages (called from readahead) | |
9c6429d9 FM |
5079 | * that have their endio callback (extent_io.c:end_bio_extent_readpage) |
5080 | * still in progress (unlocked the pages in the bio but did not yet | |
5081 | * unlocked the ranges in the io tree). Therefore this means some | |
6ca07097 FM |
5082 | * ranges can still be locked and eviction started because before |
5083 | * submitting those bios, which are executed by a separate task (work | |
5084 | * queue kthread), inode references (inode->i_count) were not taken | |
5085 | * (which would be dropped in the end io callback of each bio). | |
5086 | * Therefore here we effectively end up waiting for those bios and | |
5087 | * anyone else holding locked ranges without having bumped the inode's | |
5088 | * reference count - if we don't do it, when they access the inode's | |
5089 | * io_tree to unlock a range it may be too late, leading to an | |
5090 | * use-after-free issue. | |
5091 | */ | |
131e404a FDBM |
5092 | spin_lock(&io_tree->lock); |
5093 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
5094 | struct extent_state *state; | |
5095 | struct extent_state *cached_state = NULL; | |
6ca07097 FM |
5096 | u64 start; |
5097 | u64 end; | |
131e404a FDBM |
5098 | |
5099 | node = rb_first(&io_tree->state); | |
5100 | state = rb_entry(node, struct extent_state, rb_node); | |
6ca07097 FM |
5101 | start = state->start; |
5102 | end = state->end; | |
131e404a FDBM |
5103 | spin_unlock(&io_tree->lock); |
5104 | ||
6ca07097 | 5105 | lock_extent_bits(io_tree, start, end, 0, &cached_state); |
b9d0b389 QW |
5106 | |
5107 | /* | |
5108 | * If still has DELALLOC flag, the extent didn't reach disk, | |
5109 | * and its reserved space won't be freed by delayed_ref. | |
5110 | * So we need to free its reserved space here. | |
5111 | * (Refer to comment in btrfs_invalidatepage, case 2) | |
5112 | * | |
5113 | * Note, end is the bytenr of last byte, so we need + 1 here. | |
5114 | */ | |
5115 | if (state->state & EXTENT_DELALLOC) | |
5116 | btrfs_qgroup_free_data(inode, start, end - start + 1); | |
5117 | ||
6ca07097 | 5118 | clear_extent_bit(io_tree, start, end, |
131e404a FDBM |
5119 | EXTENT_LOCKED | EXTENT_DIRTY | |
5120 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
5121 | EXTENT_DEFRAG, 1, 1, | |
5122 | &cached_state, GFP_NOFS); | |
131e404a | 5123 | |
7064dd5c | 5124 | cond_resched(); |
131e404a FDBM |
5125 | spin_lock(&io_tree->lock); |
5126 | } | |
5127 | spin_unlock(&io_tree->lock); | |
5128 | } | |
5129 | ||
bd555975 | 5130 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 CM |
5131 | { |
5132 | struct btrfs_trans_handle *trans; | |
5133 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 5134 | struct btrfs_block_rsv *rsv, *global_rsv; |
3bce876f | 5135 | int steal_from_global = 0; |
07127184 | 5136 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 CM |
5137 | int ret; |
5138 | ||
1abe9b8a | 5139 | trace_btrfs_inode_evict(inode); |
5140 | ||
131e404a FDBM |
5141 | evict_inode_truncate_pages(inode); |
5142 | ||
69e9c6c6 SB |
5143 | if (inode->i_nlink && |
5144 | ((btrfs_root_refs(&root->root_item) != 0 && | |
5145 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
5146 | btrfs_is_free_space_inode(inode))) | |
bd555975 AV |
5147 | goto no_delete; |
5148 | ||
39279cc3 | 5149 | if (is_bad_inode(inode)) { |
7b128766 | 5150 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
5151 | goto no_delete; |
5152 | } | |
bd555975 | 5153 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
a30e577c JM |
5154 | if (!special_file(inode->i_mode)) |
5155 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
5f39d397 | 5156 | |
f612496b MX |
5157 | btrfs_free_io_failure_record(inode, 0, (u64)-1); |
5158 | ||
c71bf099 | 5159 | if (root->fs_info->log_root_recovering) { |
6bf02314 | 5160 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 5161 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
5162 | goto no_delete; |
5163 | } | |
5164 | ||
76dda93c | 5165 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
5166 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
5167 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
5168 | goto no_delete; |
5169 | } | |
5170 | ||
0e8c36a9 MX |
5171 | ret = btrfs_commit_inode_delayed_inode(inode); |
5172 | if (ret) { | |
5173 | btrfs_orphan_del(NULL, inode); | |
5174 | goto no_delete; | |
5175 | } | |
5176 | ||
66d8f3dd | 5177 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
4289a667 JB |
5178 | if (!rsv) { |
5179 | btrfs_orphan_del(NULL, inode); | |
5180 | goto no_delete; | |
5181 | } | |
4a338542 | 5182 | rsv->size = min_size; |
ca7e70f5 | 5183 | rsv->failfast = 1; |
726c35fa | 5184 | global_rsv = &root->fs_info->global_block_rsv; |
4289a667 | 5185 | |
dbe674a9 | 5186 | btrfs_i_size_write(inode, 0); |
5f39d397 | 5187 | |
4289a667 | 5188 | /* |
8407aa46 MX |
5189 | * This is a bit simpler than btrfs_truncate since we've already |
5190 | * reserved our space for our orphan item in the unlink, so we just | |
5191 | * need to reserve some slack space in case we add bytes and update | |
5192 | * inode item when doing the truncate. | |
4289a667 | 5193 | */ |
8082510e | 5194 | while (1) { |
08e007d2 MX |
5195 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
5196 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
5197 | |
5198 | /* | |
5199 | * Try and steal from the global reserve since we will | |
5200 | * likely not use this space anyway, we want to try as | |
5201 | * hard as possible to get this to work. | |
5202 | */ | |
5203 | if (ret) | |
3bce876f JB |
5204 | steal_from_global++; |
5205 | else | |
5206 | steal_from_global = 0; | |
5207 | ret = 0; | |
d68fc57b | 5208 | |
3bce876f JB |
5209 | /* |
5210 | * steal_from_global == 0: we reserved stuff, hooray! | |
5211 | * steal_from_global == 1: we didn't reserve stuff, boo! | |
5212 | * steal_from_global == 2: we've committed, still not a lot of | |
5213 | * room but maybe we'll have room in the global reserve this | |
5214 | * time. | |
5215 | * steal_from_global == 3: abandon all hope! | |
5216 | */ | |
5217 | if (steal_from_global > 2) { | |
c2cf52eb SK |
5218 | btrfs_warn(root->fs_info, |
5219 | "Could not get space for a delete, will truncate on mount %d", | |
5220 | ret); | |
4289a667 JB |
5221 | btrfs_orphan_del(NULL, inode); |
5222 | btrfs_free_block_rsv(root, rsv); | |
5223 | goto no_delete; | |
d68fc57b | 5224 | } |
7b128766 | 5225 | |
0e8c36a9 | 5226 | trans = btrfs_join_transaction(root); |
4289a667 JB |
5227 | if (IS_ERR(trans)) { |
5228 | btrfs_orphan_del(NULL, inode); | |
5229 | btrfs_free_block_rsv(root, rsv); | |
5230 | goto no_delete; | |
d68fc57b | 5231 | } |
7b128766 | 5232 | |
3bce876f JB |
5233 | /* |
5234 | * We can't just steal from the global reserve, we need tomake | |
5235 | * sure there is room to do it, if not we need to commit and try | |
5236 | * again. | |
5237 | */ | |
5238 | if (steal_from_global) { | |
5239 | if (!btrfs_check_space_for_delayed_refs(trans, root)) | |
5240 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, | |
5241 | min_size); | |
5242 | else | |
5243 | ret = -ENOSPC; | |
5244 | } | |
5245 | ||
5246 | /* | |
5247 | * Couldn't steal from the global reserve, we have too much | |
5248 | * pending stuff built up, commit the transaction and try it | |
5249 | * again. | |
5250 | */ | |
5251 | if (ret) { | |
5252 | ret = btrfs_commit_transaction(trans, root); | |
5253 | if (ret) { | |
5254 | btrfs_orphan_del(NULL, inode); | |
5255 | btrfs_free_block_rsv(root, rsv); | |
5256 | goto no_delete; | |
5257 | } | |
5258 | continue; | |
5259 | } else { | |
5260 | steal_from_global = 0; | |
5261 | } | |
5262 | ||
4289a667 JB |
5263 | trans->block_rsv = rsv; |
5264 | ||
d68fc57b | 5265 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
28ed1345 | 5266 | if (ret != -ENOSPC && ret != -EAGAIN) |
8082510e | 5267 | break; |
85e21bac | 5268 | |
8407aa46 | 5269 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e YZ |
5270 | btrfs_end_transaction(trans, root); |
5271 | trans = NULL; | |
b53d3f5d | 5272 | btrfs_btree_balance_dirty(root); |
8082510e | 5273 | } |
5f39d397 | 5274 | |
4289a667 JB |
5275 | btrfs_free_block_rsv(root, rsv); |
5276 | ||
4ef31a45 JB |
5277 | /* |
5278 | * Errors here aren't a big deal, it just means we leave orphan items | |
5279 | * in the tree. They will be cleaned up on the next mount. | |
5280 | */ | |
8082510e | 5281 | if (ret == 0) { |
4289a667 | 5282 | trans->block_rsv = root->orphan_block_rsv; |
4ef31a45 JB |
5283 | btrfs_orphan_del(trans, inode); |
5284 | } else { | |
5285 | btrfs_orphan_del(NULL, inode); | |
8082510e | 5286 | } |
54aa1f4d | 5287 | |
4289a667 | 5288 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
581bb050 LZ |
5289 | if (!(root == root->fs_info->tree_root || |
5290 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) | |
33345d01 | 5291 | btrfs_return_ino(root, btrfs_ino(inode)); |
581bb050 | 5292 | |
54aa1f4d | 5293 | btrfs_end_transaction(trans, root); |
b53d3f5d | 5294 | btrfs_btree_balance_dirty(root); |
39279cc3 | 5295 | no_delete: |
89042e5a | 5296 | btrfs_remove_delayed_node(inode); |
dbd5768f | 5297 | clear_inode(inode); |
8082510e | 5298 | return; |
39279cc3 CM |
5299 | } |
5300 | ||
5301 | /* | |
5302 | * this returns the key found in the dir entry in the location pointer. | |
5303 | * If no dir entries were found, location->objectid is 0. | |
5304 | */ | |
5305 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
5306 | struct btrfs_key *location) | |
5307 | { | |
5308 | const char *name = dentry->d_name.name; | |
5309 | int namelen = dentry->d_name.len; | |
5310 | struct btrfs_dir_item *di; | |
5311 | struct btrfs_path *path; | |
5312 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 5313 | int ret = 0; |
39279cc3 CM |
5314 | |
5315 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
5316 | if (!path) |
5317 | return -ENOMEM; | |
3954401f | 5318 | |
33345d01 | 5319 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
39279cc3 | 5320 | namelen, 0); |
0d9f7f3e Y |
5321 | if (IS_ERR(di)) |
5322 | ret = PTR_ERR(di); | |
d397712b | 5323 | |
c704005d | 5324 | if (IS_ERR_OR_NULL(di)) |
3954401f | 5325 | goto out_err; |
d397712b | 5326 | |
5f39d397 | 5327 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 5328 | out: |
39279cc3 CM |
5329 | btrfs_free_path(path); |
5330 | return ret; | |
3954401f CM |
5331 | out_err: |
5332 | location->objectid = 0; | |
5333 | goto out; | |
39279cc3 CM |
5334 | } |
5335 | ||
5336 | /* | |
5337 | * when we hit a tree root in a directory, the btrfs part of the inode | |
5338 | * needs to be changed to reflect the root directory of the tree root. This | |
5339 | * is kind of like crossing a mount point. | |
5340 | */ | |
5341 | static int fixup_tree_root_location(struct btrfs_root *root, | |
4df27c4d YZ |
5342 | struct inode *dir, |
5343 | struct dentry *dentry, | |
5344 | struct btrfs_key *location, | |
5345 | struct btrfs_root **sub_root) | |
39279cc3 | 5346 | { |
4df27c4d YZ |
5347 | struct btrfs_path *path; |
5348 | struct btrfs_root *new_root; | |
5349 | struct btrfs_root_ref *ref; | |
5350 | struct extent_buffer *leaf; | |
1d4c08e0 | 5351 | struct btrfs_key key; |
4df27c4d YZ |
5352 | int ret; |
5353 | int err = 0; | |
39279cc3 | 5354 | |
4df27c4d YZ |
5355 | path = btrfs_alloc_path(); |
5356 | if (!path) { | |
5357 | err = -ENOMEM; | |
5358 | goto out; | |
5359 | } | |
39279cc3 | 5360 | |
4df27c4d | 5361 | err = -ENOENT; |
1d4c08e0 DS |
5362 | key.objectid = BTRFS_I(dir)->root->root_key.objectid; |
5363 | key.type = BTRFS_ROOT_REF_KEY; | |
5364 | key.offset = location->objectid; | |
5365 | ||
5366 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, | |
5367 | 0, 0); | |
4df27c4d YZ |
5368 | if (ret) { |
5369 | if (ret < 0) | |
5370 | err = ret; | |
5371 | goto out; | |
5372 | } | |
39279cc3 | 5373 | |
4df27c4d YZ |
5374 | leaf = path->nodes[0]; |
5375 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
33345d01 | 5376 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
4df27c4d YZ |
5377 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
5378 | goto out; | |
39279cc3 | 5379 | |
4df27c4d YZ |
5380 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
5381 | (unsigned long)(ref + 1), | |
5382 | dentry->d_name.len); | |
5383 | if (ret) | |
5384 | goto out; | |
5385 | ||
b3b4aa74 | 5386 | btrfs_release_path(path); |
4df27c4d YZ |
5387 | |
5388 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); | |
5389 | if (IS_ERR(new_root)) { | |
5390 | err = PTR_ERR(new_root); | |
5391 | goto out; | |
5392 | } | |
5393 | ||
4df27c4d YZ |
5394 | *sub_root = new_root; |
5395 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
5396 | location->type = BTRFS_INODE_ITEM_KEY; | |
5397 | location->offset = 0; | |
5398 | err = 0; | |
5399 | out: | |
5400 | btrfs_free_path(path); | |
5401 | return err; | |
39279cc3 CM |
5402 | } |
5403 | ||
5d4f98a2 YZ |
5404 | static void inode_tree_add(struct inode *inode) |
5405 | { | |
5406 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5407 | struct btrfs_inode *entry; | |
03e860bd FNP |
5408 | struct rb_node **p; |
5409 | struct rb_node *parent; | |
cef21937 | 5410 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
33345d01 | 5411 | u64 ino = btrfs_ino(inode); |
5d4f98a2 | 5412 | |
1d3382cb | 5413 | if (inode_unhashed(inode)) |
76dda93c | 5414 | return; |
e1409cef | 5415 | parent = NULL; |
5d4f98a2 | 5416 | spin_lock(&root->inode_lock); |
e1409cef | 5417 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
5418 | while (*p) { |
5419 | parent = *p; | |
5420 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
5421 | ||
33345d01 | 5422 | if (ino < btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5423 | p = &parent->rb_left; |
33345d01 | 5424 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
03e860bd | 5425 | p = &parent->rb_right; |
5d4f98a2 YZ |
5426 | else { |
5427 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 5428 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 5429 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
5430 | RB_CLEAR_NODE(parent); |
5431 | spin_unlock(&root->inode_lock); | |
cef21937 | 5432 | return; |
5d4f98a2 YZ |
5433 | } |
5434 | } | |
cef21937 FDBM |
5435 | rb_link_node(new, parent, p); |
5436 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
5437 | spin_unlock(&root->inode_lock); |
5438 | } | |
5439 | ||
5440 | static void inode_tree_del(struct inode *inode) | |
5441 | { | |
5442 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
76dda93c | 5443 | int empty = 0; |
5d4f98a2 | 5444 | |
03e860bd | 5445 | spin_lock(&root->inode_lock); |
5d4f98a2 | 5446 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 5447 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 5448 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 5449 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 5450 | } |
03e860bd | 5451 | spin_unlock(&root->inode_lock); |
76dda93c | 5452 | |
69e9c6c6 | 5453 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
5454 | synchronize_srcu(&root->fs_info->subvol_srcu); |
5455 | spin_lock(&root->inode_lock); | |
5456 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
5457 | spin_unlock(&root->inode_lock); | |
5458 | if (empty) | |
5459 | btrfs_add_dead_root(root); | |
5460 | } | |
5461 | } | |
5462 | ||
143bede5 | 5463 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c YZ |
5464 | { |
5465 | struct rb_node *node; | |
5466 | struct rb_node *prev; | |
5467 | struct btrfs_inode *entry; | |
5468 | struct inode *inode; | |
5469 | u64 objectid = 0; | |
5470 | ||
7813b3db LB |
5471 | if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
5472 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
76dda93c YZ |
5473 | |
5474 | spin_lock(&root->inode_lock); | |
5475 | again: | |
5476 | node = root->inode_tree.rb_node; | |
5477 | prev = NULL; | |
5478 | while (node) { | |
5479 | prev = node; | |
5480 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
5481 | ||
33345d01 | 5482 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
76dda93c | 5483 | node = node->rb_left; |
33345d01 | 5484 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
76dda93c YZ |
5485 | node = node->rb_right; |
5486 | else | |
5487 | break; | |
5488 | } | |
5489 | if (!node) { | |
5490 | while (prev) { | |
5491 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
33345d01 | 5492 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
76dda93c YZ |
5493 | node = prev; |
5494 | break; | |
5495 | } | |
5496 | prev = rb_next(prev); | |
5497 | } | |
5498 | } | |
5499 | while (node) { | |
5500 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
33345d01 | 5501 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
76dda93c YZ |
5502 | inode = igrab(&entry->vfs_inode); |
5503 | if (inode) { | |
5504 | spin_unlock(&root->inode_lock); | |
5505 | if (atomic_read(&inode->i_count) > 1) | |
5506 | d_prune_aliases(inode); | |
5507 | /* | |
45321ac5 | 5508 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
5509 | * the inode cache when its usage count |
5510 | * hits zero. | |
5511 | */ | |
5512 | iput(inode); | |
5513 | cond_resched(); | |
5514 | spin_lock(&root->inode_lock); | |
5515 | goto again; | |
5516 | } | |
5517 | ||
5518 | if (cond_resched_lock(&root->inode_lock)) | |
5519 | goto again; | |
5520 | ||
5521 | node = rb_next(node); | |
5522 | } | |
5523 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
5524 | } |
5525 | ||
e02119d5 CM |
5526 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
5527 | { | |
5528 | struct btrfs_iget_args *args = p; | |
90d3e592 CM |
5529 | inode->i_ino = args->location->objectid; |
5530 | memcpy(&BTRFS_I(inode)->location, args->location, | |
5531 | sizeof(*args->location)); | |
e02119d5 | 5532 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
5533 | return 0; |
5534 | } | |
5535 | ||
5536 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
5537 | { | |
5538 | struct btrfs_iget_args *args = opaque; | |
90d3e592 | 5539 | return args->location->objectid == BTRFS_I(inode)->location.objectid && |
d397712b | 5540 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
5541 | } |
5542 | ||
5d4f98a2 | 5543 | static struct inode *btrfs_iget_locked(struct super_block *s, |
90d3e592 | 5544 | struct btrfs_key *location, |
5d4f98a2 | 5545 | struct btrfs_root *root) |
39279cc3 CM |
5546 | { |
5547 | struct inode *inode; | |
5548 | struct btrfs_iget_args args; | |
90d3e592 | 5549 | unsigned long hashval = btrfs_inode_hash(location->objectid, root); |
778ba82b | 5550 | |
90d3e592 | 5551 | args.location = location; |
39279cc3 CM |
5552 | args.root = root; |
5553 | ||
778ba82b | 5554 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
5555 | btrfs_init_locked_inode, |
5556 | (void *)&args); | |
5557 | return inode; | |
5558 | } | |
5559 | ||
1a54ef8c BR |
5560 | /* Get an inode object given its location and corresponding root. |
5561 | * Returns in *is_new if the inode was read from disk | |
5562 | */ | |
5563 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 5564 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
5565 | { |
5566 | struct inode *inode; | |
5567 | ||
90d3e592 | 5568 | inode = btrfs_iget_locked(s, location, root); |
1a54ef8c | 5569 | if (!inode) |
5d4f98a2 | 5570 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
5571 | |
5572 | if (inode->i_state & I_NEW) { | |
1a54ef8c | 5573 | btrfs_read_locked_inode(inode); |
1748f843 MF |
5574 | if (!is_bad_inode(inode)) { |
5575 | inode_tree_add(inode); | |
5576 | unlock_new_inode(inode); | |
5577 | if (new) | |
5578 | *new = 1; | |
5579 | } else { | |
e0b6d65b ST |
5580 | unlock_new_inode(inode); |
5581 | iput(inode); | |
5582 | inode = ERR_PTR(-ESTALE); | |
1748f843 MF |
5583 | } |
5584 | } | |
5585 | ||
1a54ef8c BR |
5586 | return inode; |
5587 | } | |
5588 | ||
4df27c4d YZ |
5589 | static struct inode *new_simple_dir(struct super_block *s, |
5590 | struct btrfs_key *key, | |
5591 | struct btrfs_root *root) | |
5592 | { | |
5593 | struct inode *inode = new_inode(s); | |
5594 | ||
5595 | if (!inode) | |
5596 | return ERR_PTR(-ENOMEM); | |
5597 | ||
4df27c4d YZ |
5598 | BTRFS_I(inode)->root = root; |
5599 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5600 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5601 | |
5602 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5603 | inode->i_op = &btrfs_dir_ro_inode_operations; |
4df27c4d YZ |
5604 | inode->i_fop = &simple_dir_operations; |
5605 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
9cc97d64 | 5606 | inode->i_mtime = CURRENT_TIME; |
5607 | inode->i_atime = inode->i_mtime; | |
5608 | inode->i_ctime = inode->i_mtime; | |
5609 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
4df27c4d YZ |
5610 | |
5611 | return inode; | |
5612 | } | |
5613 | ||
3de4586c | 5614 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5615 | { |
d397712b | 5616 | struct inode *inode; |
4df27c4d | 5617 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5618 | struct btrfs_root *sub_root = root; |
5619 | struct btrfs_key location; | |
76dda93c | 5620 | int index; |
b4aff1f8 | 5621 | int ret = 0; |
39279cc3 CM |
5622 | |
5623 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5624 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5625 | |
39e3c955 | 5626 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5627 | if (ret < 0) |
5628 | return ERR_PTR(ret); | |
5f39d397 | 5629 | |
4df27c4d | 5630 | if (location.objectid == 0) |
5662344b | 5631 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5632 | |
5633 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5634 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5635 | return inode; |
5636 | } | |
5637 | ||
5638 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5639 | ||
76dda93c | 5640 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
4df27c4d YZ |
5641 | ret = fixup_tree_root_location(root, dir, dentry, |
5642 | &location, &sub_root); | |
5643 | if (ret < 0) { | |
5644 | if (ret != -ENOENT) | |
5645 | inode = ERR_PTR(ret); | |
5646 | else | |
5647 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5648 | } else { | |
73f73415 | 5649 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5650 | } |
76dda93c YZ |
5651 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
5652 | ||
34d19bad | 5653 | if (!IS_ERR(inode) && root != sub_root) { |
c71bf099 YZ |
5654 | down_read(&root->fs_info->cleanup_work_sem); |
5655 | if (!(inode->i_sb->s_flags & MS_RDONLY)) | |
66b4ffd1 | 5656 | ret = btrfs_orphan_cleanup(sub_root); |
c71bf099 | 5657 | up_read(&root->fs_info->cleanup_work_sem); |
01cd3367 JB |
5658 | if (ret) { |
5659 | iput(inode); | |
66b4ffd1 | 5660 | inode = ERR_PTR(ret); |
01cd3367 | 5661 | } |
c71bf099 YZ |
5662 | } |
5663 | ||
3de4586c CM |
5664 | return inode; |
5665 | } | |
5666 | ||
fe15ce44 | 5667 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5668 | { |
5669 | struct btrfs_root *root; | |
2b0143b5 | 5670 | struct inode *inode = d_inode(dentry); |
76dda93c | 5671 | |
848cce0d | 5672 | if (!inode && !IS_ROOT(dentry)) |
2b0143b5 | 5673 | inode = d_inode(dentry->d_parent); |
76dda93c | 5674 | |
848cce0d LZ |
5675 | if (inode) { |
5676 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5677 | if (btrfs_root_refs(&root->root_item) == 0) |
5678 | return 1; | |
848cce0d LZ |
5679 | |
5680 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) | |
5681 | return 1; | |
efefb143 | 5682 | } |
76dda93c YZ |
5683 | return 0; |
5684 | } | |
5685 | ||
b4aff1f8 JB |
5686 | static void btrfs_dentry_release(struct dentry *dentry) |
5687 | { | |
944a4515 | 5688 | kfree(dentry->d_fsdata); |
b4aff1f8 JB |
5689 | } |
5690 | ||
3de4586c | 5691 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5692 | unsigned int flags) |
3de4586c | 5693 | { |
5662344b | 5694 | struct inode *inode; |
a66e7cc6 | 5695 | |
5662344b TI |
5696 | inode = btrfs_lookup_dentry(dir, dentry); |
5697 | if (IS_ERR(inode)) { | |
5698 | if (PTR_ERR(inode) == -ENOENT) | |
5699 | inode = NULL; | |
5700 | else | |
5701 | return ERR_CAST(inode); | |
5702 | } | |
5703 | ||
41d28bca | 5704 | return d_splice_alias(inode, dentry); |
39279cc3 CM |
5705 | } |
5706 | ||
16cdcec7 | 5707 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5708 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5709 | }; | |
5710 | ||
9cdda8d3 | 5711 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5712 | { |
9cdda8d3 | 5713 | struct inode *inode = file_inode(file); |
39279cc3 CM |
5714 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5715 | struct btrfs_item *item; | |
5716 | struct btrfs_dir_item *di; | |
5717 | struct btrfs_key key; | |
5f39d397 | 5718 | struct btrfs_key found_key; |
39279cc3 | 5719 | struct btrfs_path *path; |
16cdcec7 MX |
5720 | struct list_head ins_list; |
5721 | struct list_head del_list; | |
39279cc3 | 5722 | int ret; |
5f39d397 | 5723 | struct extent_buffer *leaf; |
39279cc3 | 5724 | int slot; |
39279cc3 CM |
5725 | unsigned char d_type; |
5726 | int over = 0; | |
5727 | u32 di_cur; | |
5728 | u32 di_total; | |
5729 | u32 di_len; | |
5730 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
5731 | char tmp_name[32]; |
5732 | char *name_ptr; | |
5733 | int name_len; | |
9cdda8d3 | 5734 | int is_curr = 0; /* ctx->pos points to the current index? */ |
39279cc3 CM |
5735 | |
5736 | /* FIXME, use a real flag for deciding about the key type */ | |
5737 | if (root->fs_info->tree_root == root) | |
5738 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 5739 | |
9cdda8d3 AV |
5740 | if (!dir_emit_dots(file, ctx)) |
5741 | return 0; | |
5742 | ||
49593bfa | 5743 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5744 | if (!path) |
5745 | return -ENOMEM; | |
ff5714cc | 5746 | |
026fd317 | 5747 | path->reada = 1; |
49593bfa | 5748 | |
16cdcec7 MX |
5749 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5750 | INIT_LIST_HEAD(&ins_list); | |
5751 | INIT_LIST_HEAD(&del_list); | |
5752 | btrfs_get_delayed_items(inode, &ins_list, &del_list); | |
5753 | } | |
5754 | ||
962a298f | 5755 | key.type = key_type; |
9cdda8d3 | 5756 | key.offset = ctx->pos; |
33345d01 | 5757 | key.objectid = btrfs_ino(inode); |
5f39d397 | 5758 | |
39279cc3 CM |
5759 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5760 | if (ret < 0) | |
5761 | goto err; | |
49593bfa DW |
5762 | |
5763 | while (1) { | |
5f39d397 | 5764 | leaf = path->nodes[0]; |
39279cc3 | 5765 | slot = path->slots[0]; |
b9e03af0 LZ |
5766 | if (slot >= btrfs_header_nritems(leaf)) { |
5767 | ret = btrfs_next_leaf(root, path); | |
5768 | if (ret < 0) | |
5769 | goto err; | |
5770 | else if (ret > 0) | |
5771 | break; | |
5772 | continue; | |
39279cc3 | 5773 | } |
3de4586c | 5774 | |
dd3cc16b | 5775 | item = btrfs_item_nr(slot); |
5f39d397 CM |
5776 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5777 | ||
5778 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5779 | break; |
962a298f | 5780 | if (found_key.type != key_type) |
39279cc3 | 5781 | break; |
9cdda8d3 | 5782 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5783 | goto next; |
16cdcec7 MX |
5784 | if (key_type == BTRFS_DIR_INDEX_KEY && |
5785 | btrfs_should_delete_dir_index(&del_list, | |
5786 | found_key.offset)) | |
5787 | goto next; | |
5f39d397 | 5788 | |
9cdda8d3 | 5789 | ctx->pos = found_key.offset; |
16cdcec7 | 5790 | is_curr = 1; |
49593bfa | 5791 | |
39279cc3 CM |
5792 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
5793 | di_cur = 0; | |
5f39d397 | 5794 | di_total = btrfs_item_size(leaf, item); |
49593bfa DW |
5795 | |
5796 | while (di_cur < di_total) { | |
5f39d397 CM |
5797 | struct btrfs_key location; |
5798 | ||
22a94d44 JB |
5799 | if (verify_dir_item(root, leaf, di)) |
5800 | break; | |
5801 | ||
5f39d397 | 5802 | name_len = btrfs_dir_name_len(leaf, di); |
49593bfa | 5803 | if (name_len <= sizeof(tmp_name)) { |
5f39d397 CM |
5804 | name_ptr = tmp_name; |
5805 | } else { | |
5806 | name_ptr = kmalloc(name_len, GFP_NOFS); | |
49593bfa DW |
5807 | if (!name_ptr) { |
5808 | ret = -ENOMEM; | |
5809 | goto err; | |
5810 | } | |
5f39d397 CM |
5811 | } |
5812 | read_extent_buffer(leaf, name_ptr, | |
5813 | (unsigned long)(di + 1), name_len); | |
5814 | ||
5815 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
5816 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
3de4586c | 5817 | |
fede766f | 5818 | |
3de4586c | 5819 | /* is this a reference to our own snapshot? If so |
8c9c2bf7 AJ |
5820 | * skip it. |
5821 | * | |
5822 | * In contrast to old kernels, we insert the snapshot's | |
5823 | * dir item and dir index after it has been created, so | |
5824 | * we won't find a reference to our own snapshot. We | |
5825 | * still keep the following code for backward | |
5826 | * compatibility. | |
3de4586c CM |
5827 | */ |
5828 | if (location.type == BTRFS_ROOT_ITEM_KEY && | |
5829 | location.objectid == root->root_key.objectid) { | |
5830 | over = 0; | |
5831 | goto skip; | |
5832 | } | |
9cdda8d3 AV |
5833 | over = !dir_emit(ctx, name_ptr, name_len, |
5834 | location.objectid, d_type); | |
5f39d397 | 5835 | |
3de4586c | 5836 | skip: |
5f39d397 CM |
5837 | if (name_ptr != tmp_name) |
5838 | kfree(name_ptr); | |
5839 | ||
39279cc3 CM |
5840 | if (over) |
5841 | goto nopos; | |
5103e947 | 5842 | di_len = btrfs_dir_name_len(leaf, di) + |
49593bfa | 5843 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
39279cc3 CM |
5844 | di_cur += di_len; |
5845 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
5846 | } | |
b9e03af0 LZ |
5847 | next: |
5848 | path->slots[0]++; | |
39279cc3 | 5849 | } |
49593bfa | 5850 | |
16cdcec7 MX |
5851 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5852 | if (is_curr) | |
9cdda8d3 AV |
5853 | ctx->pos++; |
5854 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); | |
16cdcec7 MX |
5855 | if (ret) |
5856 | goto nopos; | |
5857 | } | |
5858 | ||
49593bfa | 5859 | /* Reached end of directory/root. Bump pos past the last item. */ |
db62efbb ZB |
5860 | ctx->pos++; |
5861 | ||
5862 | /* | |
5863 | * Stop new entries from being returned after we return the last | |
5864 | * entry. | |
5865 | * | |
5866 | * New directory entries are assigned a strictly increasing | |
5867 | * offset. This means that new entries created during readdir | |
5868 | * are *guaranteed* to be seen in the future by that readdir. | |
5869 | * This has broken buggy programs which operate on names as | |
5870 | * they're returned by readdir. Until we re-use freed offsets | |
5871 | * we have this hack to stop new entries from being returned | |
5872 | * under the assumption that they'll never reach this huge | |
5873 | * offset. | |
5874 | * | |
5875 | * This is being careful not to overflow 32bit loff_t unless the | |
5876 | * last entry requires it because doing so has broken 32bit apps | |
5877 | * in the past. | |
5878 | */ | |
5879 | if (key_type == BTRFS_DIR_INDEX_KEY) { | |
5880 | if (ctx->pos >= INT_MAX) | |
5881 | ctx->pos = LLONG_MAX; | |
5882 | else | |
5883 | ctx->pos = INT_MAX; | |
5884 | } | |
39279cc3 CM |
5885 | nopos: |
5886 | ret = 0; | |
5887 | err: | |
16cdcec7 MX |
5888 | if (key_type == BTRFS_DIR_INDEX_KEY) |
5889 | btrfs_put_delayed_items(&ins_list, &del_list); | |
39279cc3 | 5890 | btrfs_free_path(path); |
39279cc3 CM |
5891 | return ret; |
5892 | } | |
5893 | ||
a9185b41 | 5894 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
5895 | { |
5896 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5897 | struct btrfs_trans_handle *trans; | |
5898 | int ret = 0; | |
0af3d00b | 5899 | bool nolock = false; |
39279cc3 | 5900 | |
72ac3c0d | 5901 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
5902 | return 0; |
5903 | ||
83eea1f1 | 5904 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
82d5902d | 5905 | nolock = true; |
0af3d00b | 5906 | |
a9185b41 | 5907 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 5908 | if (nolock) |
7a7eaa40 | 5909 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 5910 | else |
7a7eaa40 | 5911 | trans = btrfs_join_transaction(root); |
3612b495 TI |
5912 | if (IS_ERR(trans)) |
5913 | return PTR_ERR(trans); | |
a698d075 | 5914 | ret = btrfs_commit_transaction(trans, root); |
39279cc3 CM |
5915 | } |
5916 | return ret; | |
5917 | } | |
5918 | ||
5919 | /* | |
54aa1f4d | 5920 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5921 | * inode changes. But, it is most likely to find the inode in cache. |
5922 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5923 | * to keep or drop this code. | |
5924 | */ | |
48a3b636 | 5925 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 CM |
5926 | { |
5927 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5928 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5929 | int ret; |
5930 | ||
72ac3c0d | 5931 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5932 | return 0; |
39279cc3 | 5933 | |
7a7eaa40 | 5934 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5935 | if (IS_ERR(trans)) |
5936 | return PTR_ERR(trans); | |
8929ecfa YZ |
5937 | |
5938 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
5939 | if (ret && ret == -ENOSPC) { |
5940 | /* whoops, lets try again with the full transaction */ | |
5941 | btrfs_end_transaction(trans, root); | |
5942 | trans = btrfs_start_transaction(root, 1); | |
22c44fe6 JB |
5943 | if (IS_ERR(trans)) |
5944 | return PTR_ERR(trans); | |
8929ecfa | 5945 | |
94b60442 | 5946 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 5947 | } |
39279cc3 | 5948 | btrfs_end_transaction(trans, root); |
16cdcec7 MX |
5949 | if (BTRFS_I(inode)->delayed_node) |
5950 | btrfs_balance_delayed_items(root); | |
22c44fe6 JB |
5951 | |
5952 | return ret; | |
5953 | } | |
5954 | ||
5955 | /* | |
5956 | * This is a copy of file_update_time. We need this so we can return error on | |
5957 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
5958 | */ | |
e41f941a JB |
5959 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
5960 | int flags) | |
22c44fe6 | 5961 | { |
2bc55652 AB |
5962 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5963 | ||
5964 | if (btrfs_root_readonly(root)) | |
5965 | return -EROFS; | |
5966 | ||
e41f941a | 5967 | if (flags & S_VERSION) |
22c44fe6 | 5968 | inode_inc_iversion(inode); |
e41f941a JB |
5969 | if (flags & S_CTIME) |
5970 | inode->i_ctime = *now; | |
5971 | if (flags & S_MTIME) | |
5972 | inode->i_mtime = *now; | |
5973 | if (flags & S_ATIME) | |
5974 | inode->i_atime = *now; | |
5975 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
5976 | } |
5977 | ||
d352ac68 CM |
5978 | /* |
5979 | * find the highest existing sequence number in a directory | |
5980 | * and then set the in-memory index_cnt variable to reflect | |
5981 | * free sequence numbers | |
5982 | */ | |
aec7477b JB |
5983 | static int btrfs_set_inode_index_count(struct inode *inode) |
5984 | { | |
5985 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5986 | struct btrfs_key key, found_key; | |
5987 | struct btrfs_path *path; | |
5988 | struct extent_buffer *leaf; | |
5989 | int ret; | |
5990 | ||
33345d01 | 5991 | key.objectid = btrfs_ino(inode); |
962a298f | 5992 | key.type = BTRFS_DIR_INDEX_KEY; |
aec7477b JB |
5993 | key.offset = (u64)-1; |
5994 | ||
5995 | path = btrfs_alloc_path(); | |
5996 | if (!path) | |
5997 | return -ENOMEM; | |
5998 | ||
5999 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
6000 | if (ret < 0) | |
6001 | goto out; | |
6002 | /* FIXME: we should be able to handle this */ | |
6003 | if (ret == 0) | |
6004 | goto out; | |
6005 | ret = 0; | |
6006 | ||
6007 | /* | |
6008 | * MAGIC NUMBER EXPLANATION: | |
6009 | * since we search a directory based on f_pos we have to start at 2 | |
6010 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
6011 | * else has to start at 2 | |
6012 | */ | |
6013 | if (path->slots[0] == 0) { | |
6014 | BTRFS_I(inode)->index_cnt = 2; | |
6015 | goto out; | |
6016 | } | |
6017 | ||
6018 | path->slots[0]--; | |
6019 | ||
6020 | leaf = path->nodes[0]; | |
6021 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
6022 | ||
33345d01 | 6023 | if (found_key.objectid != btrfs_ino(inode) || |
962a298f | 6024 | found_key.type != BTRFS_DIR_INDEX_KEY) { |
aec7477b JB |
6025 | BTRFS_I(inode)->index_cnt = 2; |
6026 | goto out; | |
6027 | } | |
6028 | ||
6029 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
6030 | out: | |
6031 | btrfs_free_path(path); | |
6032 | return ret; | |
6033 | } | |
6034 | ||
d352ac68 CM |
6035 | /* |
6036 | * helper to find a free sequence number in a given directory. This current | |
6037 | * code is very simple, later versions will do smarter things in the btree | |
6038 | */ | |
3de4586c | 6039 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
aec7477b JB |
6040 | { |
6041 | int ret = 0; | |
6042 | ||
6043 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
16cdcec7 MX |
6044 | ret = btrfs_inode_delayed_dir_index_count(dir); |
6045 | if (ret) { | |
6046 | ret = btrfs_set_inode_index_count(dir); | |
6047 | if (ret) | |
6048 | return ret; | |
6049 | } | |
aec7477b JB |
6050 | } |
6051 | ||
00e4e6b3 | 6052 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
6053 | BTRFS_I(dir)->index_cnt++; |
6054 | ||
6055 | return ret; | |
6056 | } | |
6057 | ||
b0d5d10f CM |
6058 | static int btrfs_insert_inode_locked(struct inode *inode) |
6059 | { | |
6060 | struct btrfs_iget_args args; | |
6061 | args.location = &BTRFS_I(inode)->location; | |
6062 | args.root = BTRFS_I(inode)->root; | |
6063 | ||
6064 | return insert_inode_locked4(inode, | |
6065 | btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root), | |
6066 | btrfs_find_actor, &args); | |
6067 | } | |
6068 | ||
39279cc3 CM |
6069 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
6070 | struct btrfs_root *root, | |
aec7477b | 6071 | struct inode *dir, |
9c58309d | 6072 | const char *name, int name_len, |
175a4eb7 AV |
6073 | u64 ref_objectid, u64 objectid, |
6074 | umode_t mode, u64 *index) | |
39279cc3 CM |
6075 | { |
6076 | struct inode *inode; | |
5f39d397 | 6077 | struct btrfs_inode_item *inode_item; |
39279cc3 | 6078 | struct btrfs_key *location; |
5f39d397 | 6079 | struct btrfs_path *path; |
9c58309d CM |
6080 | struct btrfs_inode_ref *ref; |
6081 | struct btrfs_key key[2]; | |
6082 | u32 sizes[2]; | |
ef3b9af5 | 6083 | int nitems = name ? 2 : 1; |
9c58309d | 6084 | unsigned long ptr; |
39279cc3 | 6085 | int ret; |
39279cc3 | 6086 | |
5f39d397 | 6087 | path = btrfs_alloc_path(); |
d8926bb3 MF |
6088 | if (!path) |
6089 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 6090 | |
39279cc3 | 6091 | inode = new_inode(root->fs_info->sb); |
8fb27640 YS |
6092 | if (!inode) { |
6093 | btrfs_free_path(path); | |
39279cc3 | 6094 | return ERR_PTR(-ENOMEM); |
8fb27640 | 6095 | } |
39279cc3 | 6096 | |
5762b5c9 FM |
6097 | /* |
6098 | * O_TMPFILE, set link count to 0, so that after this point, | |
6099 | * we fill in an inode item with the correct link count. | |
6100 | */ | |
6101 | if (!name) | |
6102 | set_nlink(inode, 0); | |
6103 | ||
581bb050 LZ |
6104 | /* |
6105 | * we have to initialize this early, so we can reclaim the inode | |
6106 | * number if we fail afterwards in this function. | |
6107 | */ | |
6108 | inode->i_ino = objectid; | |
6109 | ||
ef3b9af5 | 6110 | if (dir && name) { |
1abe9b8a | 6111 | trace_btrfs_inode_request(dir); |
6112 | ||
3de4586c | 6113 | ret = btrfs_set_inode_index(dir, index); |
09771430 | 6114 | if (ret) { |
8fb27640 | 6115 | btrfs_free_path(path); |
09771430 | 6116 | iput(inode); |
aec7477b | 6117 | return ERR_PTR(ret); |
09771430 | 6118 | } |
ef3b9af5 FM |
6119 | } else if (dir) { |
6120 | *index = 0; | |
aec7477b JB |
6121 | } |
6122 | /* | |
6123 | * index_cnt is ignored for everything but a dir, | |
6124 | * btrfs_get_inode_index_count has an explanation for the magic | |
6125 | * number | |
6126 | */ | |
6127 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 6128 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 6129 | BTRFS_I(inode)->root = root; |
e02119d5 | 6130 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 6131 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 6132 | |
5dc562c5 JB |
6133 | /* |
6134 | * We could have gotten an inode number from somebody who was fsynced | |
6135 | * and then removed in this same transaction, so let's just set full | |
6136 | * sync since it will be a full sync anyway and this will blow away the | |
6137 | * old info in the log. | |
6138 | */ | |
6139 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
6140 | ||
9c58309d | 6141 | key[0].objectid = objectid; |
962a298f | 6142 | key[0].type = BTRFS_INODE_ITEM_KEY; |
9c58309d CM |
6143 | key[0].offset = 0; |
6144 | ||
9c58309d | 6145 | sizes[0] = sizeof(struct btrfs_inode_item); |
ef3b9af5 FM |
6146 | |
6147 | if (name) { | |
6148 | /* | |
6149 | * Start new inodes with an inode_ref. This is slightly more | |
6150 | * efficient for small numbers of hard links since they will | |
6151 | * be packed into one item. Extended refs will kick in if we | |
6152 | * add more hard links than can fit in the ref item. | |
6153 | */ | |
6154 | key[1].objectid = objectid; | |
962a298f | 6155 | key[1].type = BTRFS_INODE_REF_KEY; |
ef3b9af5 FM |
6156 | key[1].offset = ref_objectid; |
6157 | ||
6158 | sizes[1] = name_len + sizeof(*ref); | |
6159 | } | |
9c58309d | 6160 | |
b0d5d10f CM |
6161 | location = &BTRFS_I(inode)->location; |
6162 | location->objectid = objectid; | |
6163 | location->offset = 0; | |
962a298f | 6164 | location->type = BTRFS_INODE_ITEM_KEY; |
b0d5d10f CM |
6165 | |
6166 | ret = btrfs_insert_inode_locked(inode); | |
6167 | if (ret < 0) | |
6168 | goto fail; | |
6169 | ||
b9473439 | 6170 | path->leave_spinning = 1; |
ef3b9af5 | 6171 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems); |
9c58309d | 6172 | if (ret != 0) |
b0d5d10f | 6173 | goto fail_unlock; |
5f39d397 | 6174 | |
ecc11fab | 6175 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 6176 | inode_set_bytes(inode, 0); |
9cc97d64 | 6177 | |
6178 | inode->i_mtime = CURRENT_TIME; | |
6179 | inode->i_atime = inode->i_mtime; | |
6180 | inode->i_ctime = inode->i_mtime; | |
6181 | BTRFS_I(inode)->i_otime = inode->i_mtime; | |
6182 | ||
5f39d397 CM |
6183 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
6184 | struct btrfs_inode_item); | |
293f7e07 LZ |
6185 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
6186 | sizeof(*inode_item)); | |
e02119d5 | 6187 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d | 6188 | |
ef3b9af5 FM |
6189 | if (name) { |
6190 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
6191 | struct btrfs_inode_ref); | |
6192 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
6193 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); | |
6194 | ptr = (unsigned long)(ref + 1); | |
6195 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
6196 | } | |
9c58309d | 6197 | |
5f39d397 CM |
6198 | btrfs_mark_buffer_dirty(path->nodes[0]); |
6199 | btrfs_free_path(path); | |
6200 | ||
6cbff00f CH |
6201 | btrfs_inherit_iflags(inode, dir); |
6202 | ||
569254b0 | 6203 | if (S_ISREG(mode)) { |
94272164 CM |
6204 | if (btrfs_test_opt(root, NODATASUM)) |
6205 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
213490b3 | 6206 | if (btrfs_test_opt(root, NODATACOW)) |
f2bdf9a8 JB |
6207 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
6208 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
6209 | } |
6210 | ||
5d4f98a2 | 6211 | inode_tree_add(inode); |
1abe9b8a | 6212 | |
6213 | trace_btrfs_inode_new(inode); | |
1973f0fa | 6214 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 6215 | |
8ea05e3a AB |
6216 | btrfs_update_root_times(trans, root); |
6217 | ||
63541927 FDBM |
6218 | ret = btrfs_inode_inherit_props(trans, inode, dir); |
6219 | if (ret) | |
6220 | btrfs_err(root->fs_info, | |
6221 | "error inheriting props for ino %llu (root %llu): %d", | |
6222 | btrfs_ino(inode), root->root_key.objectid, ret); | |
6223 | ||
39279cc3 | 6224 | return inode; |
b0d5d10f CM |
6225 | |
6226 | fail_unlock: | |
6227 | unlock_new_inode(inode); | |
5f39d397 | 6228 | fail: |
ef3b9af5 | 6229 | if (dir && name) |
aec7477b | 6230 | BTRFS_I(dir)->index_cnt--; |
5f39d397 | 6231 | btrfs_free_path(path); |
09771430 | 6232 | iput(inode); |
5f39d397 | 6233 | return ERR_PTR(ret); |
39279cc3 CM |
6234 | } |
6235 | ||
6236 | static inline u8 btrfs_inode_type(struct inode *inode) | |
6237 | { | |
6238 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
6239 | } | |
6240 | ||
d352ac68 CM |
6241 | /* |
6242 | * utility function to add 'inode' into 'parent_inode' with | |
6243 | * a give name and a given sequence number. | |
6244 | * if 'add_backref' is true, also insert a backref from the | |
6245 | * inode to the parent directory. | |
6246 | */ | |
e02119d5 CM |
6247 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
6248 | struct inode *parent_inode, struct inode *inode, | |
6249 | const char *name, int name_len, int add_backref, u64 index) | |
39279cc3 | 6250 | { |
4df27c4d | 6251 | int ret = 0; |
39279cc3 | 6252 | struct btrfs_key key; |
e02119d5 | 6253 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
33345d01 LZ |
6254 | u64 ino = btrfs_ino(inode); |
6255 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 6256 | |
33345d01 | 6257 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6258 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
6259 | } else { | |
33345d01 | 6260 | key.objectid = ino; |
962a298f | 6261 | key.type = BTRFS_INODE_ITEM_KEY; |
4df27c4d YZ |
6262 | key.offset = 0; |
6263 | } | |
6264 | ||
33345d01 | 6265 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
6266 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
6267 | key.objectid, root->root_key.objectid, | |
33345d01 | 6268 | parent_ino, index, name, name_len); |
4df27c4d | 6269 | } else if (add_backref) { |
33345d01 LZ |
6270 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
6271 | parent_ino, index); | |
4df27c4d | 6272 | } |
39279cc3 | 6273 | |
79787eaa JM |
6274 | /* Nothing to clean up yet */ |
6275 | if (ret) | |
6276 | return ret; | |
4df27c4d | 6277 | |
79787eaa JM |
6278 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
6279 | parent_inode, &key, | |
6280 | btrfs_inode_type(inode), index); | |
9c52057c | 6281 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
6282 | goto fail_dir_item; |
6283 | else if (ret) { | |
6284 | btrfs_abort_transaction(trans, root, ret); | |
6285 | return ret; | |
39279cc3 | 6286 | } |
79787eaa JM |
6287 | |
6288 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | |
6289 | name_len * 2); | |
0c4d2d95 | 6290 | inode_inc_iversion(parent_inode); |
79787eaa JM |
6291 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
6292 | ret = btrfs_update_inode(trans, root, parent_inode); | |
6293 | if (ret) | |
6294 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 6295 | return ret; |
fe66a05a CM |
6296 | |
6297 | fail_dir_item: | |
6298 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
6299 | u64 local_index; | |
6300 | int err; | |
6301 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
6302 | key.objectid, root->root_key.objectid, | |
6303 | parent_ino, &local_index, name, name_len); | |
6304 | ||
6305 | } else if (add_backref) { | |
6306 | u64 local_index; | |
6307 | int err; | |
6308 | ||
6309 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
6310 | ino, parent_ino, &local_index); | |
6311 | } | |
6312 | return ret; | |
39279cc3 CM |
6313 | } |
6314 | ||
6315 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
a1b075d2 JB |
6316 | struct inode *dir, struct dentry *dentry, |
6317 | struct inode *inode, int backref, u64 index) | |
39279cc3 | 6318 | { |
a1b075d2 JB |
6319 | int err = btrfs_add_link(trans, dir, inode, |
6320 | dentry->d_name.name, dentry->d_name.len, | |
6321 | backref, index); | |
39279cc3 CM |
6322 | if (err > 0) |
6323 | err = -EEXIST; | |
6324 | return err; | |
6325 | } | |
6326 | ||
618e21d5 | 6327 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 6328 | umode_t mode, dev_t rdev) |
618e21d5 JB |
6329 | { |
6330 | struct btrfs_trans_handle *trans; | |
6331 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6332 | struct inode *inode = NULL; |
618e21d5 JB |
6333 | int err; |
6334 | int drop_inode = 0; | |
6335 | u64 objectid; | |
00e4e6b3 | 6336 | u64 index = 0; |
618e21d5 | 6337 | |
9ed74f2d JB |
6338 | /* |
6339 | * 2 for inode item and ref | |
6340 | * 2 for dir items | |
6341 | * 1 for xattr if selinux is on | |
6342 | */ | |
a22285a6 YZ |
6343 | trans = btrfs_start_transaction(root, 5); |
6344 | if (IS_ERR(trans)) | |
6345 | return PTR_ERR(trans); | |
1832a6d5 | 6346 | |
581bb050 LZ |
6347 | err = btrfs_find_free_ino(root, &objectid); |
6348 | if (err) | |
6349 | goto out_unlock; | |
6350 | ||
aec7477b | 6351 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6352 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6353 | mode, &index); |
7cf96da3 TI |
6354 | if (IS_ERR(inode)) { |
6355 | err = PTR_ERR(inode); | |
618e21d5 | 6356 | goto out_unlock; |
7cf96da3 | 6357 | } |
618e21d5 | 6358 | |
ad19db71 CS |
6359 | /* |
6360 | * If the active LSM wants to access the inode during | |
6361 | * d_instantiate it needs these. Smack checks to see | |
6362 | * if the filesystem supports xattrs by looking at the | |
6363 | * ops vector. | |
6364 | */ | |
ad19db71 | 6365 | inode->i_op = &btrfs_special_inode_operations; |
b0d5d10f CM |
6366 | init_special_inode(inode, inode->i_mode, rdev); |
6367 | ||
6368 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
618e21d5 | 6369 | if (err) |
b0d5d10f CM |
6370 | goto out_unlock_inode; |
6371 | ||
6372 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); | |
6373 | if (err) { | |
6374 | goto out_unlock_inode; | |
6375 | } else { | |
1b4ab1bb | 6376 | btrfs_update_inode(trans, root, inode); |
b0d5d10f | 6377 | unlock_new_inode(inode); |
08c422c2 | 6378 | d_instantiate(dentry, inode); |
618e21d5 | 6379 | } |
b0d5d10f | 6380 | |
618e21d5 | 6381 | out_unlock: |
7ad85bb7 | 6382 | btrfs_end_transaction(trans, root); |
c581afc8 | 6383 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6384 | btrfs_btree_balance_dirty(root); |
618e21d5 JB |
6385 | if (drop_inode) { |
6386 | inode_dec_link_count(inode); | |
6387 | iput(inode); | |
6388 | } | |
618e21d5 | 6389 | return err; |
b0d5d10f CM |
6390 | |
6391 | out_unlock_inode: | |
6392 | drop_inode = 1; | |
6393 | unlock_new_inode(inode); | |
6394 | goto out_unlock; | |
6395 | ||
618e21d5 JB |
6396 | } |
6397 | ||
39279cc3 | 6398 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 6399 | umode_t mode, bool excl) |
39279cc3 CM |
6400 | { |
6401 | struct btrfs_trans_handle *trans; | |
6402 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 6403 | struct inode *inode = NULL; |
43baa579 | 6404 | int drop_inode_on_err = 0; |
a22285a6 | 6405 | int err; |
39279cc3 | 6406 | u64 objectid; |
00e4e6b3 | 6407 | u64 index = 0; |
39279cc3 | 6408 | |
9ed74f2d JB |
6409 | /* |
6410 | * 2 for inode item and ref | |
6411 | * 2 for dir items | |
6412 | * 1 for xattr if selinux is on | |
6413 | */ | |
a22285a6 YZ |
6414 | trans = btrfs_start_transaction(root, 5); |
6415 | if (IS_ERR(trans)) | |
6416 | return PTR_ERR(trans); | |
9ed74f2d | 6417 | |
581bb050 LZ |
6418 | err = btrfs_find_free_ino(root, &objectid); |
6419 | if (err) | |
6420 | goto out_unlock; | |
6421 | ||
aec7477b | 6422 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6423 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6424 | mode, &index); |
7cf96da3 TI |
6425 | if (IS_ERR(inode)) { |
6426 | err = PTR_ERR(inode); | |
39279cc3 | 6427 | goto out_unlock; |
7cf96da3 | 6428 | } |
43baa579 | 6429 | drop_inode_on_err = 1; |
ad19db71 CS |
6430 | /* |
6431 | * If the active LSM wants to access the inode during | |
6432 | * d_instantiate it needs these. Smack checks to see | |
6433 | * if the filesystem supports xattrs by looking at the | |
6434 | * ops vector. | |
6435 | */ | |
6436 | inode->i_fop = &btrfs_file_operations; | |
6437 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 6438 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
6439 | |
6440 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
6441 | if (err) | |
6442 | goto out_unlock_inode; | |
6443 | ||
6444 | err = btrfs_update_inode(trans, root, inode); | |
6445 | if (err) | |
6446 | goto out_unlock_inode; | |
ad19db71 | 6447 | |
a1b075d2 | 6448 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 6449 | if (err) |
b0d5d10f | 6450 | goto out_unlock_inode; |
43baa579 | 6451 | |
43baa579 | 6452 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
b0d5d10f | 6453 | unlock_new_inode(inode); |
43baa579 FB |
6454 | d_instantiate(dentry, inode); |
6455 | ||
39279cc3 | 6456 | out_unlock: |
7ad85bb7 | 6457 | btrfs_end_transaction(trans, root); |
43baa579 | 6458 | if (err && drop_inode_on_err) { |
39279cc3 CM |
6459 | inode_dec_link_count(inode); |
6460 | iput(inode); | |
6461 | } | |
c581afc8 | 6462 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6463 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6464 | return err; |
b0d5d10f CM |
6465 | |
6466 | out_unlock_inode: | |
6467 | unlock_new_inode(inode); | |
6468 | goto out_unlock; | |
6469 | ||
39279cc3 CM |
6470 | } |
6471 | ||
6472 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
6473 | struct dentry *dentry) | |
6474 | { | |
6475 | struct btrfs_trans_handle *trans; | |
6476 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2b0143b5 | 6477 | struct inode *inode = d_inode(old_dentry); |
00e4e6b3 | 6478 | u64 index; |
39279cc3 CM |
6479 | int err; |
6480 | int drop_inode = 0; | |
6481 | ||
4a8be425 TH |
6482 | /* do not allow sys_link's with other subvols of the same device */ |
6483 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 6484 | return -EXDEV; |
4a8be425 | 6485 | |
f186373f | 6486 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 6487 | return -EMLINK; |
4a8be425 | 6488 | |
3de4586c | 6489 | err = btrfs_set_inode_index(dir, &index); |
aec7477b JB |
6490 | if (err) |
6491 | goto fail; | |
6492 | ||
a22285a6 | 6493 | /* |
7e6b6465 | 6494 | * 2 items for inode and inode ref |
a22285a6 | 6495 | * 2 items for dir items |
7e6b6465 | 6496 | * 1 item for parent inode |
a22285a6 | 6497 | */ |
7e6b6465 | 6498 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
6499 | if (IS_ERR(trans)) { |
6500 | err = PTR_ERR(trans); | |
6501 | goto fail; | |
6502 | } | |
5f39d397 | 6503 | |
67de1176 MX |
6504 | /* There are several dir indexes for this inode, clear the cache. */ |
6505 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 6506 | inc_nlink(inode); |
0c4d2d95 | 6507 | inode_inc_iversion(inode); |
3153495d | 6508 | inode->i_ctime = CURRENT_TIME; |
7de9c6ee | 6509 | ihold(inode); |
e9976151 | 6510 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 6511 | |
a1b075d2 | 6512 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
5f39d397 | 6513 | |
a5719521 | 6514 | if (err) { |
54aa1f4d | 6515 | drop_inode = 1; |
a5719521 | 6516 | } else { |
10d9f309 | 6517 | struct dentry *parent = dentry->d_parent; |
a5719521 | 6518 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
6519 | if (err) |
6520 | goto fail; | |
ef3b9af5 FM |
6521 | if (inode->i_nlink == 1) { |
6522 | /* | |
6523 | * If new hard link count is 1, it's a file created | |
6524 | * with open(2) O_TMPFILE flag. | |
6525 | */ | |
6526 | err = btrfs_orphan_del(trans, inode); | |
6527 | if (err) | |
6528 | goto fail; | |
6529 | } | |
08c422c2 | 6530 | d_instantiate(dentry, inode); |
6a912213 | 6531 | btrfs_log_new_name(trans, inode, NULL, parent); |
a5719521 | 6532 | } |
39279cc3 | 6533 | |
7ad85bb7 | 6534 | btrfs_end_transaction(trans, root); |
c581afc8 | 6535 | btrfs_balance_delayed_items(root); |
1832a6d5 | 6536 | fail: |
39279cc3 CM |
6537 | if (drop_inode) { |
6538 | inode_dec_link_count(inode); | |
6539 | iput(inode); | |
6540 | } | |
b53d3f5d | 6541 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
6542 | return err; |
6543 | } | |
6544 | ||
18bb1db3 | 6545 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 6546 | { |
b9d86667 | 6547 | struct inode *inode = NULL; |
39279cc3 CM |
6548 | struct btrfs_trans_handle *trans; |
6549 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
6550 | int err = 0; | |
6551 | int drop_on_err = 0; | |
b9d86667 | 6552 | u64 objectid = 0; |
00e4e6b3 | 6553 | u64 index = 0; |
39279cc3 | 6554 | |
9ed74f2d JB |
6555 | /* |
6556 | * 2 items for inode and ref | |
6557 | * 2 items for dir items | |
6558 | * 1 for xattr if selinux is on | |
6559 | */ | |
a22285a6 YZ |
6560 | trans = btrfs_start_transaction(root, 5); |
6561 | if (IS_ERR(trans)) | |
6562 | return PTR_ERR(trans); | |
39279cc3 | 6563 | |
581bb050 LZ |
6564 | err = btrfs_find_free_ino(root, &objectid); |
6565 | if (err) | |
6566 | goto out_fail; | |
6567 | ||
aec7477b | 6568 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 6569 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 6570 | S_IFDIR | mode, &index); |
39279cc3 CM |
6571 | if (IS_ERR(inode)) { |
6572 | err = PTR_ERR(inode); | |
6573 | goto out_fail; | |
6574 | } | |
5f39d397 | 6575 | |
39279cc3 | 6576 | drop_on_err = 1; |
b0d5d10f CM |
6577 | /* these must be set before we unlock the inode */ |
6578 | inode->i_op = &btrfs_dir_inode_operations; | |
6579 | inode->i_fop = &btrfs_dir_file_operations; | |
33268eaf | 6580 | |
2a7dba39 | 6581 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf | 6582 | if (err) |
b0d5d10f | 6583 | goto out_fail_inode; |
39279cc3 | 6584 | |
dbe674a9 | 6585 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
6586 | err = btrfs_update_inode(trans, root, inode); |
6587 | if (err) | |
b0d5d10f | 6588 | goto out_fail_inode; |
5f39d397 | 6589 | |
a1b075d2 JB |
6590 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
6591 | dentry->d_name.len, 0, index); | |
39279cc3 | 6592 | if (err) |
b0d5d10f | 6593 | goto out_fail_inode; |
5f39d397 | 6594 | |
39279cc3 | 6595 | d_instantiate(dentry, inode); |
b0d5d10f CM |
6596 | /* |
6597 | * mkdir is special. We're unlocking after we call d_instantiate | |
6598 | * to avoid a race with nfsd calling d_instantiate. | |
6599 | */ | |
6600 | unlock_new_inode(inode); | |
39279cc3 | 6601 | drop_on_err = 0; |
39279cc3 CM |
6602 | |
6603 | out_fail: | |
7ad85bb7 | 6604 | btrfs_end_transaction(trans, root); |
c7cfb8a5 WS |
6605 | if (drop_on_err) { |
6606 | inode_dec_link_count(inode); | |
39279cc3 | 6607 | iput(inode); |
c7cfb8a5 | 6608 | } |
c581afc8 | 6609 | btrfs_balance_delayed_items(root); |
b53d3f5d | 6610 | btrfs_btree_balance_dirty(root); |
39279cc3 | 6611 | return err; |
b0d5d10f CM |
6612 | |
6613 | out_fail_inode: | |
6614 | unlock_new_inode(inode); | |
6615 | goto out_fail; | |
39279cc3 CM |
6616 | } |
6617 | ||
e6c4efd8 QW |
6618 | /* Find next extent map of a given extent map, caller needs to ensure locks */ |
6619 | static struct extent_map *next_extent_map(struct extent_map *em) | |
6620 | { | |
6621 | struct rb_node *next; | |
6622 | ||
6623 | next = rb_next(&em->rb_node); | |
6624 | if (!next) | |
6625 | return NULL; | |
6626 | return container_of(next, struct extent_map, rb_node); | |
6627 | } | |
6628 | ||
6629 | static struct extent_map *prev_extent_map(struct extent_map *em) | |
6630 | { | |
6631 | struct rb_node *prev; | |
6632 | ||
6633 | prev = rb_prev(&em->rb_node); | |
6634 | if (!prev) | |
6635 | return NULL; | |
6636 | return container_of(prev, struct extent_map, rb_node); | |
6637 | } | |
6638 | ||
d352ac68 | 6639 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
e6c4efd8 | 6640 | * the existing extent is the nearest extent to map_start, |
d352ac68 | 6641 | * and an extent that you want to insert, deal with overlap and insert |
e6c4efd8 | 6642 | * the best fitted new extent into the tree. |
d352ac68 | 6643 | */ |
3b951516 CM |
6644 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
6645 | struct extent_map *existing, | |
e6dcd2dc | 6646 | struct extent_map *em, |
51f395ad | 6647 | u64 map_start) |
3b951516 | 6648 | { |
e6c4efd8 QW |
6649 | struct extent_map *prev; |
6650 | struct extent_map *next; | |
6651 | u64 start; | |
6652 | u64 end; | |
3b951516 | 6653 | u64 start_diff; |
3b951516 | 6654 | |
e6dcd2dc | 6655 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
e6c4efd8 QW |
6656 | |
6657 | if (existing->start > map_start) { | |
6658 | next = existing; | |
6659 | prev = prev_extent_map(next); | |
6660 | } else { | |
6661 | prev = existing; | |
6662 | next = next_extent_map(prev); | |
6663 | } | |
6664 | ||
6665 | start = prev ? extent_map_end(prev) : em->start; | |
6666 | start = max_t(u64, start, em->start); | |
6667 | end = next ? next->start : extent_map_end(em); | |
6668 | end = min_t(u64, end, extent_map_end(em)); | |
6669 | start_diff = start - em->start; | |
6670 | em->start = start; | |
6671 | em->len = end - start; | |
c8b97818 CM |
6672 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
6673 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 6674 | em->block_start += start_diff; |
c8b97818 CM |
6675 | em->block_len -= start_diff; |
6676 | } | |
09a2a8f9 | 6677 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6678 | } |
6679 | ||
c8b97818 CM |
6680 | static noinline int uncompress_inline(struct btrfs_path *path, |
6681 | struct inode *inode, struct page *page, | |
6682 | size_t pg_offset, u64 extent_offset, | |
6683 | struct btrfs_file_extent_item *item) | |
6684 | { | |
6685 | int ret; | |
6686 | struct extent_buffer *leaf = path->nodes[0]; | |
6687 | char *tmp; | |
6688 | size_t max_size; | |
6689 | unsigned long inline_size; | |
6690 | unsigned long ptr; | |
261507a0 | 6691 | int compress_type; |
c8b97818 CM |
6692 | |
6693 | WARN_ON(pg_offset != 0); | |
261507a0 | 6694 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6695 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6696 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6697 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6698 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6699 | if (!tmp) |
6700 | return -ENOMEM; | |
c8b97818 CM |
6701 | ptr = btrfs_file_extent_inline_start(item); |
6702 | ||
6703 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6704 | ||
5b050f04 | 6705 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
261507a0 LZ |
6706 | ret = btrfs_decompress(compress_type, tmp, page, |
6707 | extent_offset, inline_size, max_size); | |
c8b97818 | 6708 | kfree(tmp); |
166ae5a4 | 6709 | return ret; |
c8b97818 CM |
6710 | } |
6711 | ||
d352ac68 CM |
6712 | /* |
6713 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6714 | * the ugly parts come from merging extents from the disk with the in-ram |
6715 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6716 | * where the in-ram extents might be locked pending data=ordered completion. |
6717 | * | |
6718 | * This also copies inline extents directly into the page. | |
6719 | */ | |
d397712b | 6720 | |
a52d9a80 | 6721 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 6722 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
6723 | int create) |
6724 | { | |
6725 | int ret; | |
6726 | int err = 0; | |
a52d9a80 CM |
6727 | u64 extent_start = 0; |
6728 | u64 extent_end = 0; | |
33345d01 | 6729 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6730 | u32 found_type; |
f421950f | 6731 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
6732 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6733 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
6734 | struct extent_buffer *leaf; |
6735 | struct btrfs_key found_key; | |
a52d9a80 CM |
6736 | struct extent_map *em = NULL; |
6737 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 6738 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 | 6739 | struct btrfs_trans_handle *trans = NULL; |
7ffbb598 | 6740 | const bool new_inline = !page || create; |
a52d9a80 | 6741 | |
a52d9a80 | 6742 | again: |
890871be | 6743 | read_lock(&em_tree->lock); |
d1310b2e | 6744 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d CM |
6745 | if (em) |
6746 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
890871be | 6747 | read_unlock(&em_tree->lock); |
d1310b2e | 6748 | |
a52d9a80 | 6749 | if (em) { |
e1c4b745 CM |
6750 | if (em->start > start || em->start + em->len <= start) |
6751 | free_extent_map(em); | |
6752 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6753 | free_extent_map(em); |
6754 | else | |
6755 | goto out; | |
a52d9a80 | 6756 | } |
172ddd60 | 6757 | em = alloc_extent_map(); |
a52d9a80 | 6758 | if (!em) { |
d1310b2e CM |
6759 | err = -ENOMEM; |
6760 | goto out; | |
a52d9a80 | 6761 | } |
e6dcd2dc | 6762 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
d1310b2e | 6763 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6764 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6765 | em->len = (u64)-1; |
c8b97818 | 6766 | em->block_len = (u64)-1; |
f421950f CM |
6767 | |
6768 | if (!path) { | |
6769 | path = btrfs_alloc_path(); | |
026fd317 JB |
6770 | if (!path) { |
6771 | err = -ENOMEM; | |
6772 | goto out; | |
6773 | } | |
6774 | /* | |
6775 | * Chances are we'll be called again, so go ahead and do | |
6776 | * readahead | |
6777 | */ | |
6778 | path->reada = 1; | |
f421950f CM |
6779 | } |
6780 | ||
179e29e4 CM |
6781 | ret = btrfs_lookup_file_extent(trans, root, path, |
6782 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6783 | if (ret < 0) { |
6784 | err = ret; | |
6785 | goto out; | |
6786 | } | |
6787 | ||
6788 | if (ret != 0) { | |
6789 | if (path->slots[0] == 0) | |
6790 | goto not_found; | |
6791 | path->slots[0]--; | |
6792 | } | |
6793 | ||
5f39d397 CM |
6794 | leaf = path->nodes[0]; |
6795 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6796 | struct btrfs_file_extent_item); |
a52d9a80 | 6797 | /* are we inside the extent that was found? */ |
5f39d397 | 6798 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
962a298f | 6799 | found_type = found_key.type; |
5f39d397 | 6800 | if (found_key.objectid != objectid || |
a52d9a80 | 6801 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6802 | /* |
6803 | * If we backup past the first extent we want to move forward | |
6804 | * and see if there is an extent in front of us, otherwise we'll | |
6805 | * say there is a hole for our whole search range which can | |
6806 | * cause problems. | |
6807 | */ | |
6808 | extent_end = start; | |
6809 | goto next; | |
a52d9a80 CM |
6810 | } |
6811 | ||
5f39d397 CM |
6812 | found_type = btrfs_file_extent_type(leaf, item); |
6813 | extent_start = found_key.offset; | |
d899e052 YZ |
6814 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6815 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6816 | extent_end = extent_start + |
db94535d | 6817 | btrfs_file_extent_num_bytes(leaf, item); |
9036c102 YZ |
6818 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6819 | size_t size; | |
514ac8ad | 6820 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
fda2832f | 6821 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
9036c102 | 6822 | } |
25a50341 | 6823 | next: |
9036c102 YZ |
6824 | if (start >= extent_end) { |
6825 | path->slots[0]++; | |
6826 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6827 | ret = btrfs_next_leaf(root, path); | |
6828 | if (ret < 0) { | |
6829 | err = ret; | |
6830 | goto out; | |
a52d9a80 | 6831 | } |
9036c102 YZ |
6832 | if (ret > 0) |
6833 | goto not_found; | |
6834 | leaf = path->nodes[0]; | |
a52d9a80 | 6835 | } |
9036c102 YZ |
6836 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6837 | if (found_key.objectid != objectid || | |
6838 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6839 | goto not_found; | |
6840 | if (start + len <= found_key.offset) | |
6841 | goto not_found; | |
e2eca69d WS |
6842 | if (start > found_key.offset) |
6843 | goto next; | |
9036c102 | 6844 | em->start = start; |
70c8a91c | 6845 | em->orig_start = start; |
9036c102 YZ |
6846 | em->len = found_key.offset - start; |
6847 | goto not_found_em; | |
6848 | } | |
6849 | ||
7ffbb598 FM |
6850 | btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em); |
6851 | ||
d899e052 YZ |
6852 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6853 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 CM |
6854 | goto insert; |
6855 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 6856 | unsigned long ptr; |
a52d9a80 | 6857 | char *map; |
3326d1b0 CM |
6858 | size_t size; |
6859 | size_t extent_offset; | |
6860 | size_t copy_size; | |
a52d9a80 | 6861 | |
7ffbb598 | 6862 | if (new_inline) |
689f9346 | 6863 | goto out; |
5f39d397 | 6864 | |
514ac8ad | 6865 | size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); |
9036c102 | 6866 | extent_offset = page_offset(page) + pg_offset - extent_start; |
70dec807 | 6867 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
3326d1b0 | 6868 | size - extent_offset); |
3326d1b0 | 6869 | em->start = extent_start + extent_offset; |
fda2832f | 6870 | em->len = ALIGN(copy_size, root->sectorsize); |
b4939680 | 6871 | em->orig_block_len = em->len; |
70c8a91c | 6872 | em->orig_start = em->start; |
689f9346 | 6873 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 6874 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
6875 | if (btrfs_file_extent_compression(leaf, item) != |
6876 | BTRFS_COMPRESS_NONE) { | |
c8b97818 CM |
6877 | ret = uncompress_inline(path, inode, page, |
6878 | pg_offset, | |
6879 | extent_offset, item); | |
166ae5a4 ZB |
6880 | if (ret) { |
6881 | err = ret; | |
6882 | goto out; | |
6883 | } | |
c8b97818 CM |
6884 | } else { |
6885 | map = kmap(page); | |
6886 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6887 | copy_size); | |
93c82d57 CM |
6888 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
6889 | memset(map + pg_offset + copy_size, 0, | |
6890 | PAGE_CACHE_SIZE - pg_offset - | |
6891 | copy_size); | |
6892 | } | |
c8b97818 CM |
6893 | kunmap(page); |
6894 | } | |
179e29e4 CM |
6895 | flush_dcache_page(page); |
6896 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 6897 | BUG(); |
179e29e4 CM |
6898 | if (!trans) { |
6899 | kunmap(page); | |
6900 | free_extent_map(em); | |
6901 | em = NULL; | |
ff5714cc | 6902 | |
b3b4aa74 | 6903 | btrfs_release_path(path); |
7a7eaa40 | 6904 | trans = btrfs_join_transaction(root); |
ff5714cc | 6905 | |
3612b495 TI |
6906 | if (IS_ERR(trans)) |
6907 | return ERR_CAST(trans); | |
179e29e4 CM |
6908 | goto again; |
6909 | } | |
c8b97818 | 6910 | map = kmap(page); |
70dec807 | 6911 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 6912 | copy_size); |
c8b97818 | 6913 | kunmap(page); |
179e29e4 | 6914 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 6915 | } |
d1310b2e | 6916 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6917 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 | 6918 | goto insert; |
a52d9a80 CM |
6919 | } |
6920 | not_found: | |
6921 | em->start = start; | |
70c8a91c | 6922 | em->orig_start = start; |
d1310b2e | 6923 | em->len = len; |
a52d9a80 | 6924 | not_found_em: |
5f39d397 | 6925 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 6926 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 6927 | insert: |
b3b4aa74 | 6928 | btrfs_release_path(path); |
d1310b2e | 6929 | if (em->start > start || extent_map_end(em) <= start) { |
c2cf52eb | 6930 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
c1c9ff7c | 6931 | em->start, em->len, start, len); |
a52d9a80 CM |
6932 | err = -EIO; |
6933 | goto out; | |
6934 | } | |
d1310b2e CM |
6935 | |
6936 | err = 0; | |
890871be | 6937 | write_lock(&em_tree->lock); |
09a2a8f9 | 6938 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6939 | /* it is possible that someone inserted the extent into the tree |
6940 | * while we had the lock dropped. It is also possible that | |
6941 | * an overlapping map exists in the tree | |
6942 | */ | |
a52d9a80 | 6943 | if (ret == -EEXIST) { |
3b951516 | 6944 | struct extent_map *existing; |
e6dcd2dc CM |
6945 | |
6946 | ret = 0; | |
6947 | ||
e6c4efd8 QW |
6948 | existing = search_extent_mapping(em_tree, start, len); |
6949 | /* | |
6950 | * existing will always be non-NULL, since there must be | |
6951 | * extent causing the -EEXIST. | |
6952 | */ | |
6953 | if (start >= extent_map_end(existing) || | |
32be3a1a | 6954 | start <= existing->start) { |
e6c4efd8 QW |
6955 | /* |
6956 | * The existing extent map is the one nearest to | |
6957 | * the [start, start + len) range which overlaps | |
6958 | */ | |
6959 | err = merge_extent_mapping(em_tree, existing, | |
6960 | em, start); | |
e1c4b745 | 6961 | free_extent_map(existing); |
e6c4efd8 | 6962 | if (err) { |
3b951516 CM |
6963 | free_extent_map(em); |
6964 | em = NULL; | |
6965 | } | |
6966 | } else { | |
6967 | free_extent_map(em); | |
6968 | em = existing; | |
e6dcd2dc | 6969 | err = 0; |
a52d9a80 | 6970 | } |
a52d9a80 | 6971 | } |
890871be | 6972 | write_unlock(&em_tree->lock); |
a52d9a80 | 6973 | out: |
1abe9b8a | 6974 | |
4cd8587c | 6975 | trace_btrfs_get_extent(root, em); |
1abe9b8a | 6976 | |
527afb44 | 6977 | btrfs_free_path(path); |
a52d9a80 CM |
6978 | if (trans) { |
6979 | ret = btrfs_end_transaction(trans, root); | |
d397712b | 6980 | if (!err) |
a52d9a80 CM |
6981 | err = ret; |
6982 | } | |
a52d9a80 CM |
6983 | if (err) { |
6984 | free_extent_map(em); | |
a52d9a80 CM |
6985 | return ERR_PTR(err); |
6986 | } | |
79787eaa | 6987 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
6988 | return em; |
6989 | } | |
6990 | ||
ec29ed5b CM |
6991 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
6992 | size_t pg_offset, u64 start, u64 len, | |
6993 | int create) | |
6994 | { | |
6995 | struct extent_map *em; | |
6996 | struct extent_map *hole_em = NULL; | |
6997 | u64 range_start = start; | |
6998 | u64 end; | |
6999 | u64 found; | |
7000 | u64 found_end; | |
7001 | int err = 0; | |
7002 | ||
7003 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
7004 | if (IS_ERR(em)) | |
7005 | return em; | |
7006 | if (em) { | |
7007 | /* | |
f9e4fb53 LB |
7008 | * if our em maps to |
7009 | * - a hole or | |
7010 | * - a pre-alloc extent, | |
7011 | * there might actually be delalloc bytes behind it. | |
ec29ed5b | 7012 | */ |
f9e4fb53 LB |
7013 | if (em->block_start != EXTENT_MAP_HOLE && |
7014 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
ec29ed5b CM |
7015 | return em; |
7016 | else | |
7017 | hole_em = em; | |
7018 | } | |
7019 | ||
7020 | /* check to see if we've wrapped (len == -1 or similar) */ | |
7021 | end = start + len; | |
7022 | if (end < start) | |
7023 | end = (u64)-1; | |
7024 | else | |
7025 | end -= 1; | |
7026 | ||
7027 | em = NULL; | |
7028 | ||
7029 | /* ok, we didn't find anything, lets look for delalloc */ | |
7030 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, | |
7031 | end, len, EXTENT_DELALLOC, 1); | |
7032 | found_end = range_start + found; | |
7033 | if (found_end < range_start) | |
7034 | found_end = (u64)-1; | |
7035 | ||
7036 | /* | |
7037 | * we didn't find anything useful, return | |
7038 | * the original results from get_extent() | |
7039 | */ | |
7040 | if (range_start > end || found_end <= start) { | |
7041 | em = hole_em; | |
7042 | hole_em = NULL; | |
7043 | goto out; | |
7044 | } | |
7045 | ||
7046 | /* adjust the range_start to make sure it doesn't | |
7047 | * go backwards from the start they passed in | |
7048 | */ | |
67871254 | 7049 | range_start = max(start, range_start); |
ec29ed5b CM |
7050 | found = found_end - range_start; |
7051 | ||
7052 | if (found > 0) { | |
7053 | u64 hole_start = start; | |
7054 | u64 hole_len = len; | |
7055 | ||
172ddd60 | 7056 | em = alloc_extent_map(); |
ec29ed5b CM |
7057 | if (!em) { |
7058 | err = -ENOMEM; | |
7059 | goto out; | |
7060 | } | |
7061 | /* | |
7062 | * when btrfs_get_extent can't find anything it | |
7063 | * returns one huge hole | |
7064 | * | |
7065 | * make sure what it found really fits our range, and | |
7066 | * adjust to make sure it is based on the start from | |
7067 | * the caller | |
7068 | */ | |
7069 | if (hole_em) { | |
7070 | u64 calc_end = extent_map_end(hole_em); | |
7071 | ||
7072 | if (calc_end <= start || (hole_em->start > end)) { | |
7073 | free_extent_map(hole_em); | |
7074 | hole_em = NULL; | |
7075 | } else { | |
7076 | hole_start = max(hole_em->start, start); | |
7077 | hole_len = calc_end - hole_start; | |
7078 | } | |
7079 | } | |
7080 | em->bdev = NULL; | |
7081 | if (hole_em && range_start > hole_start) { | |
7082 | /* our hole starts before our delalloc, so we | |
7083 | * have to return just the parts of the hole | |
7084 | * that go until the delalloc starts | |
7085 | */ | |
7086 | em->len = min(hole_len, | |
7087 | range_start - hole_start); | |
7088 | em->start = hole_start; | |
7089 | em->orig_start = hole_start; | |
7090 | /* | |
7091 | * don't adjust block start at all, | |
7092 | * it is fixed at EXTENT_MAP_HOLE | |
7093 | */ | |
7094 | em->block_start = hole_em->block_start; | |
7095 | em->block_len = hole_len; | |
f9e4fb53 LB |
7096 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
7097 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
7098 | } else { |
7099 | em->start = range_start; | |
7100 | em->len = found; | |
7101 | em->orig_start = range_start; | |
7102 | em->block_start = EXTENT_MAP_DELALLOC; | |
7103 | em->block_len = found; | |
7104 | } | |
7105 | } else if (hole_em) { | |
7106 | return hole_em; | |
7107 | } | |
7108 | out: | |
7109 | ||
7110 | free_extent_map(hole_em); | |
7111 | if (err) { | |
7112 | free_extent_map(em); | |
7113 | return ERR_PTR(err); | |
7114 | } | |
7115 | return em; | |
7116 | } | |
7117 | ||
4b46fce2 JB |
7118 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
7119 | u64 start, u64 len) | |
7120 | { | |
7121 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
70c8a91c | 7122 | struct extent_map *em; |
4b46fce2 JB |
7123 | struct btrfs_key ins; |
7124 | u64 alloc_hint; | |
7125 | int ret; | |
4b46fce2 | 7126 | |
4b46fce2 | 7127 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
00361589 | 7128 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
e570fd27 | 7129 | alloc_hint, &ins, 1, 1); |
00361589 JB |
7130 | if (ret) |
7131 | return ERR_PTR(ret); | |
4b46fce2 | 7132 | |
70c8a91c | 7133 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
cc95bef6 | 7134 | ins.offset, ins.offset, ins.offset, 0); |
00361589 | 7135 | if (IS_ERR(em)) { |
e570fd27 | 7136 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
00361589 JB |
7137 | return em; |
7138 | } | |
4b46fce2 JB |
7139 | |
7140 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, | |
7141 | ins.offset, ins.offset, 0); | |
7142 | if (ret) { | |
e570fd27 | 7143 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); |
00361589 JB |
7144 | free_extent_map(em); |
7145 | return ERR_PTR(ret); | |
4b46fce2 | 7146 | } |
00361589 | 7147 | |
4b46fce2 JB |
7148 | return em; |
7149 | } | |
7150 | ||
46bfbb5c CM |
7151 | /* |
7152 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
7153 | * block must be cow'd | |
7154 | */ | |
00361589 | 7155 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
7156 | u64 *orig_start, u64 *orig_block_len, |
7157 | u64 *ram_bytes) | |
46bfbb5c | 7158 | { |
00361589 | 7159 | struct btrfs_trans_handle *trans; |
46bfbb5c CM |
7160 | struct btrfs_path *path; |
7161 | int ret; | |
7162 | struct extent_buffer *leaf; | |
7163 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7b2b7085 | 7164 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
46bfbb5c CM |
7165 | struct btrfs_file_extent_item *fi; |
7166 | struct btrfs_key key; | |
7167 | u64 disk_bytenr; | |
7168 | u64 backref_offset; | |
7169 | u64 extent_end; | |
7170 | u64 num_bytes; | |
7171 | int slot; | |
7172 | int found_type; | |
7ee9e440 | 7173 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 7174 | |
46bfbb5c CM |
7175 | path = btrfs_alloc_path(); |
7176 | if (!path) | |
7177 | return -ENOMEM; | |
7178 | ||
00361589 | 7179 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
46bfbb5c CM |
7180 | offset, 0); |
7181 | if (ret < 0) | |
7182 | goto out; | |
7183 | ||
7184 | slot = path->slots[0]; | |
7185 | if (ret == 1) { | |
7186 | if (slot == 0) { | |
7187 | /* can't find the item, must cow */ | |
7188 | ret = 0; | |
7189 | goto out; | |
7190 | } | |
7191 | slot--; | |
7192 | } | |
7193 | ret = 0; | |
7194 | leaf = path->nodes[0]; | |
7195 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
33345d01 | 7196 | if (key.objectid != btrfs_ino(inode) || |
46bfbb5c CM |
7197 | key.type != BTRFS_EXTENT_DATA_KEY) { |
7198 | /* not our file or wrong item type, must cow */ | |
7199 | goto out; | |
7200 | } | |
7201 | ||
7202 | if (key.offset > offset) { | |
7203 | /* Wrong offset, must cow */ | |
7204 | goto out; | |
7205 | } | |
7206 | ||
7207 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
7208 | found_type = btrfs_file_extent_type(leaf, fi); | |
7209 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
7210 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
7211 | /* not a regular extent, must cow */ | |
7212 | goto out; | |
7213 | } | |
7ee9e440 JB |
7214 | |
7215 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
7216 | goto out; | |
7217 | ||
e77751aa MX |
7218 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
7219 | if (extent_end <= offset) | |
7220 | goto out; | |
7221 | ||
46bfbb5c | 7222 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
7223 | if (disk_bytenr == 0) |
7224 | goto out; | |
7225 | ||
7226 | if (btrfs_file_extent_compression(leaf, fi) || | |
7227 | btrfs_file_extent_encryption(leaf, fi) || | |
7228 | btrfs_file_extent_other_encoding(leaf, fi)) | |
7229 | goto out; | |
7230 | ||
46bfbb5c CM |
7231 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
7232 | ||
7ee9e440 JB |
7233 | if (orig_start) { |
7234 | *orig_start = key.offset - backref_offset; | |
7235 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
7236 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
7237 | } | |
eb384b55 | 7238 | |
46bfbb5c CM |
7239 | if (btrfs_extent_readonly(root, disk_bytenr)) |
7240 | goto out; | |
7b2b7085 MX |
7241 | |
7242 | num_bytes = min(offset + *len, extent_end) - offset; | |
7243 | if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
7244 | u64 range_end; | |
7245 | ||
7246 | range_end = round_up(offset + num_bytes, root->sectorsize) - 1; | |
7247 | ret = test_range_bit(io_tree, offset, range_end, | |
7248 | EXTENT_DELALLOC, 0, NULL); | |
7249 | if (ret) { | |
7250 | ret = -EAGAIN; | |
7251 | goto out; | |
7252 | } | |
7253 | } | |
7254 | ||
1bda19eb | 7255 | btrfs_release_path(path); |
46bfbb5c CM |
7256 | |
7257 | /* | |
7258 | * look for other files referencing this extent, if we | |
7259 | * find any we must cow | |
7260 | */ | |
00361589 JB |
7261 | trans = btrfs_join_transaction(root); |
7262 | if (IS_ERR(trans)) { | |
7263 | ret = 0; | |
46bfbb5c | 7264 | goto out; |
00361589 JB |
7265 | } |
7266 | ||
7267 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), | |
7268 | key.offset - backref_offset, disk_bytenr); | |
7269 | btrfs_end_transaction(trans, root); | |
7270 | if (ret) { | |
7271 | ret = 0; | |
7272 | goto out; | |
7273 | } | |
46bfbb5c CM |
7274 | |
7275 | /* | |
7276 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
7277 | * in this extent we are about to write. If there | |
7278 | * are any csums in that range we have to cow in order | |
7279 | * to keep the csums correct | |
7280 | */ | |
7281 | disk_bytenr += backref_offset; | |
7282 | disk_bytenr += offset - key.offset; | |
46bfbb5c CM |
7283 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
7284 | goto out; | |
7285 | /* | |
7286 | * all of the above have passed, it is safe to overwrite this extent | |
7287 | * without cow | |
7288 | */ | |
eb384b55 | 7289 | *len = num_bytes; |
46bfbb5c CM |
7290 | ret = 1; |
7291 | out: | |
7292 | btrfs_free_path(path); | |
7293 | return ret; | |
7294 | } | |
7295 | ||
fc4adbff AG |
7296 | bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end) |
7297 | { | |
7298 | struct radix_tree_root *root = &inode->i_mapping->page_tree; | |
7299 | int found = false; | |
7300 | void **pagep = NULL; | |
7301 | struct page *page = NULL; | |
7302 | int start_idx; | |
7303 | int end_idx; | |
7304 | ||
7305 | start_idx = start >> PAGE_CACHE_SHIFT; | |
7306 | ||
7307 | /* | |
7308 | * end is the last byte in the last page. end == start is legal | |
7309 | */ | |
7310 | end_idx = end >> PAGE_CACHE_SHIFT; | |
7311 | ||
7312 | rcu_read_lock(); | |
7313 | ||
7314 | /* Most of the code in this while loop is lifted from | |
7315 | * find_get_page. It's been modified to begin searching from a | |
7316 | * page and return just the first page found in that range. If the | |
7317 | * found idx is less than or equal to the end idx then we know that | |
7318 | * a page exists. If no pages are found or if those pages are | |
7319 | * outside of the range then we're fine (yay!) */ | |
7320 | while (page == NULL && | |
7321 | radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) { | |
7322 | page = radix_tree_deref_slot(pagep); | |
7323 | if (unlikely(!page)) | |
7324 | break; | |
7325 | ||
7326 | if (radix_tree_exception(page)) { | |
809f9016 FM |
7327 | if (radix_tree_deref_retry(page)) { |
7328 | page = NULL; | |
fc4adbff | 7329 | continue; |
809f9016 | 7330 | } |
fc4adbff AG |
7331 | /* |
7332 | * Otherwise, shmem/tmpfs must be storing a swap entry | |
7333 | * here as an exceptional entry: so return it without | |
7334 | * attempting to raise page count. | |
7335 | */ | |
6fdef6d4 | 7336 | page = NULL; |
fc4adbff AG |
7337 | break; /* TODO: Is this relevant for this use case? */ |
7338 | } | |
7339 | ||
91405151 FM |
7340 | if (!page_cache_get_speculative(page)) { |
7341 | page = NULL; | |
fc4adbff | 7342 | continue; |
91405151 | 7343 | } |
fc4adbff AG |
7344 | |
7345 | /* | |
7346 | * Has the page moved? | |
7347 | * This is part of the lockless pagecache protocol. See | |
7348 | * include/linux/pagemap.h for details. | |
7349 | */ | |
7350 | if (unlikely(page != *pagep)) { | |
7351 | page_cache_release(page); | |
7352 | page = NULL; | |
7353 | } | |
7354 | } | |
7355 | ||
7356 | if (page) { | |
7357 | if (page->index <= end_idx) | |
7358 | found = true; | |
7359 | page_cache_release(page); | |
7360 | } | |
7361 | ||
7362 | rcu_read_unlock(); | |
7363 | return found; | |
7364 | } | |
7365 | ||
eb838e73 JB |
7366 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
7367 | struct extent_state **cached_state, int writing) | |
7368 | { | |
7369 | struct btrfs_ordered_extent *ordered; | |
7370 | int ret = 0; | |
7371 | ||
7372 | while (1) { | |
7373 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7374 | 0, cached_state); | |
7375 | /* | |
7376 | * We're concerned with the entire range that we're going to be | |
7377 | * doing DIO to, so we need to make sure theres no ordered | |
7378 | * extents in this range. | |
7379 | */ | |
7380 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | |
7381 | lockend - lockstart + 1); | |
7382 | ||
7383 | /* | |
7384 | * We need to make sure there are no buffered pages in this | |
7385 | * range either, we could have raced between the invalidate in | |
7386 | * generic_file_direct_write and locking the extent. The | |
7387 | * invalidate needs to happen so that reads after a write do not | |
7388 | * get stale data. | |
7389 | */ | |
fc4adbff AG |
7390 | if (!ordered && |
7391 | (!writing || | |
7392 | !btrfs_page_exists_in_range(inode, lockstart, lockend))) | |
eb838e73 JB |
7393 | break; |
7394 | ||
7395 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
7396 | cached_state, GFP_NOFS); | |
7397 | ||
7398 | if (ordered) { | |
7399 | btrfs_start_ordered_extent(inode, ordered, 1); | |
7400 | btrfs_put_ordered_extent(ordered); | |
7401 | } else { | |
7402 | /* Screw you mmap */ | |
728404da | 7403 | ret = btrfs_fdatawrite_range(inode, lockstart, lockend); |
075bdbdb FM |
7404 | if (ret) |
7405 | break; | |
7406 | ret = filemap_fdatawait_range(inode->i_mapping, | |
7407 | lockstart, | |
7408 | lockend); | |
eb838e73 JB |
7409 | if (ret) |
7410 | break; | |
7411 | ||
7412 | /* | |
7413 | * If we found a page that couldn't be invalidated just | |
7414 | * fall back to buffered. | |
7415 | */ | |
7416 | ret = invalidate_inode_pages2_range(inode->i_mapping, | |
7417 | lockstart >> PAGE_CACHE_SHIFT, | |
7418 | lockend >> PAGE_CACHE_SHIFT); | |
7419 | if (ret) | |
7420 | break; | |
7421 | } | |
7422 | ||
7423 | cond_resched(); | |
7424 | } | |
7425 | ||
7426 | return ret; | |
7427 | } | |
7428 | ||
69ffb543 JB |
7429 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
7430 | u64 len, u64 orig_start, | |
7431 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
7432 | u64 orig_block_len, u64 ram_bytes, |
7433 | int type) | |
69ffb543 JB |
7434 | { |
7435 | struct extent_map_tree *em_tree; | |
7436 | struct extent_map *em; | |
7437 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7438 | int ret; | |
7439 | ||
7440 | em_tree = &BTRFS_I(inode)->extent_tree; | |
7441 | em = alloc_extent_map(); | |
7442 | if (!em) | |
7443 | return ERR_PTR(-ENOMEM); | |
7444 | ||
7445 | em->start = start; | |
7446 | em->orig_start = orig_start; | |
2ab28f32 JB |
7447 | em->mod_start = start; |
7448 | em->mod_len = len; | |
69ffb543 JB |
7449 | em->len = len; |
7450 | em->block_len = block_len; | |
7451 | em->block_start = block_start; | |
7452 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 7453 | em->orig_block_len = orig_block_len; |
cc95bef6 | 7454 | em->ram_bytes = ram_bytes; |
70c8a91c | 7455 | em->generation = -1; |
69ffb543 JB |
7456 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
7457 | if (type == BTRFS_ORDERED_PREALLOC) | |
b11e234d | 7458 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
69ffb543 JB |
7459 | |
7460 | do { | |
7461 | btrfs_drop_extent_cache(inode, em->start, | |
7462 | em->start + em->len - 1, 0); | |
7463 | write_lock(&em_tree->lock); | |
09a2a8f9 | 7464 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 JB |
7465 | write_unlock(&em_tree->lock); |
7466 | } while (ret == -EEXIST); | |
7467 | ||
7468 | if (ret) { | |
7469 | free_extent_map(em); | |
7470 | return ERR_PTR(ret); | |
7471 | } | |
7472 | ||
7473 | return em; | |
7474 | } | |
7475 | ||
50745b0a | 7476 | struct btrfs_dio_data { |
7477 | u64 outstanding_extents; | |
7478 | u64 reserve; | |
7479 | }; | |
69ffb543 | 7480 | |
9c9464cc FM |
7481 | static void adjust_dio_outstanding_extents(struct inode *inode, |
7482 | struct btrfs_dio_data *dio_data, | |
7483 | const u64 len) | |
7484 | { | |
7485 | unsigned num_extents; | |
7486 | ||
7487 | num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1, | |
7488 | BTRFS_MAX_EXTENT_SIZE); | |
7489 | /* | |
7490 | * If we have an outstanding_extents count still set then we're | |
7491 | * within our reservation, otherwise we need to adjust our inode | |
7492 | * counter appropriately. | |
7493 | */ | |
7494 | if (dio_data->outstanding_extents) { | |
7495 | dio_data->outstanding_extents -= num_extents; | |
7496 | } else { | |
7497 | spin_lock(&BTRFS_I(inode)->lock); | |
7498 | BTRFS_I(inode)->outstanding_extents += num_extents; | |
7499 | spin_unlock(&BTRFS_I(inode)->lock); | |
7500 | } | |
7501 | } | |
7502 | ||
4b46fce2 JB |
7503 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
7504 | struct buffer_head *bh_result, int create) | |
7505 | { | |
7506 | struct extent_map *em; | |
7507 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
eb838e73 | 7508 | struct extent_state *cached_state = NULL; |
50745b0a | 7509 | struct btrfs_dio_data *dio_data = NULL; |
4b46fce2 | 7510 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 7511 | u64 lockstart, lockend; |
4b46fce2 | 7512 | u64 len = bh_result->b_size; |
eb838e73 | 7513 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 7514 | int ret = 0; |
eb838e73 | 7515 | |
172a5049 | 7516 | if (create) |
3266789f | 7517 | unlock_bits |= EXTENT_DIRTY; |
172a5049 | 7518 | else |
c329861d | 7519 | len = min_t(u64, len, root->sectorsize); |
eb838e73 | 7520 | |
c329861d JB |
7521 | lockstart = start; |
7522 | lockend = start + len - 1; | |
7523 | ||
e1cbbfa5 JB |
7524 | if (current->journal_info) { |
7525 | /* | |
7526 | * Need to pull our outstanding extents and set journal_info to NULL so | |
7527 | * that anything that needs to check if there's a transction doesn't get | |
7528 | * confused. | |
7529 | */ | |
50745b0a | 7530 | dio_data = current->journal_info; |
e1cbbfa5 JB |
7531 | current->journal_info = NULL; |
7532 | } | |
7533 | ||
eb838e73 JB |
7534 | /* |
7535 | * If this errors out it's because we couldn't invalidate pagecache for | |
7536 | * this range and we need to fallback to buffered. | |
7537 | */ | |
9c9464cc FM |
7538 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, |
7539 | create)) { | |
7540 | ret = -ENOTBLK; | |
7541 | goto err; | |
7542 | } | |
eb838e73 | 7543 | |
4b46fce2 | 7544 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
eb838e73 JB |
7545 | if (IS_ERR(em)) { |
7546 | ret = PTR_ERR(em); | |
7547 | goto unlock_err; | |
7548 | } | |
4b46fce2 JB |
7549 | |
7550 | /* | |
7551 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
7552 | * io. INLINE is special, and we could probably kludge it in here, but | |
7553 | * it's still buffered so for safety lets just fall back to the generic | |
7554 | * buffered path. | |
7555 | * | |
7556 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
7557 | * decompress it, so there will be buffering required no matter what we | |
7558 | * do, so go ahead and fallback to buffered. | |
7559 | * | |
7560 | * We return -ENOTBLK because thats what makes DIO go ahead and go back | |
7561 | * to buffered IO. Don't blame me, this is the price we pay for using | |
7562 | * the generic code. | |
7563 | */ | |
7564 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
7565 | em->block_start == EXTENT_MAP_INLINE) { | |
7566 | free_extent_map(em); | |
eb838e73 JB |
7567 | ret = -ENOTBLK; |
7568 | goto unlock_err; | |
4b46fce2 JB |
7569 | } |
7570 | ||
7571 | /* Just a good old fashioned hole, return */ | |
7572 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
7573 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
7574 | free_extent_map(em); | |
eb838e73 | 7575 | goto unlock_err; |
4b46fce2 JB |
7576 | } |
7577 | ||
7578 | /* | |
7579 | * We don't allocate a new extent in the following cases | |
7580 | * | |
7581 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
7582 | * existing extent. | |
7583 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
7584 | * just use the extent. | |
7585 | * | |
7586 | */ | |
46bfbb5c | 7587 | if (!create) { |
eb838e73 JB |
7588 | len = min(len, em->len - (start - em->start)); |
7589 | lockstart = start + len; | |
7590 | goto unlock; | |
46bfbb5c | 7591 | } |
4b46fce2 JB |
7592 | |
7593 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
7594 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
7595 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 | 7596 | int type; |
eb384b55 | 7597 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
7598 | |
7599 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7600 | type = BTRFS_ORDERED_PREALLOC; | |
7601 | else | |
7602 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 7603 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 7604 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 7605 | |
00361589 | 7606 | if (can_nocow_extent(inode, start, &len, &orig_start, |
7ee9e440 | 7607 | &orig_block_len, &ram_bytes) == 1) { |
69ffb543 JB |
7608 | if (type == BTRFS_ORDERED_PREALLOC) { |
7609 | free_extent_map(em); | |
7610 | em = create_pinned_em(inode, start, len, | |
7611 | orig_start, | |
b4939680 | 7612 | block_start, len, |
cc95bef6 JB |
7613 | orig_block_len, |
7614 | ram_bytes, type); | |
555e1286 FM |
7615 | if (IS_ERR(em)) { |
7616 | ret = PTR_ERR(em); | |
69ffb543 | 7617 | goto unlock_err; |
555e1286 | 7618 | } |
69ffb543 JB |
7619 | } |
7620 | ||
46bfbb5c CM |
7621 | ret = btrfs_add_ordered_extent_dio(inode, start, |
7622 | block_start, len, len, type); | |
46bfbb5c CM |
7623 | if (ret) { |
7624 | free_extent_map(em); | |
eb838e73 | 7625 | goto unlock_err; |
46bfbb5c CM |
7626 | } |
7627 | goto unlock; | |
4b46fce2 | 7628 | } |
4b46fce2 | 7629 | } |
00361589 | 7630 | |
46bfbb5c CM |
7631 | /* |
7632 | * this will cow the extent, reset the len in case we changed | |
7633 | * it above | |
7634 | */ | |
7635 | len = bh_result->b_size; | |
70c8a91c JB |
7636 | free_extent_map(em); |
7637 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
7638 | if (IS_ERR(em)) { |
7639 | ret = PTR_ERR(em); | |
7640 | goto unlock_err; | |
7641 | } | |
46bfbb5c CM |
7642 | len = min(len, em->len - (start - em->start)); |
7643 | unlock: | |
4b46fce2 JB |
7644 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
7645 | inode->i_blkbits; | |
46bfbb5c | 7646 | bh_result->b_size = len; |
4b46fce2 JB |
7647 | bh_result->b_bdev = em->bdev; |
7648 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
7649 | if (create) { |
7650 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
7651 | set_buffer_new(bh_result); | |
7652 | ||
7653 | /* | |
7654 | * Need to update the i_size under the extent lock so buffered | |
7655 | * readers will get the updated i_size when we unlock. | |
7656 | */ | |
7657 | if (start + len > i_size_read(inode)) | |
7658 | i_size_write(inode, start + len); | |
0934856d | 7659 | |
9c9464cc | 7660 | adjust_dio_outstanding_extents(inode, dio_data, len); |
7cf5b976 | 7661 | btrfs_free_reserved_data_space(inode, start, len); |
50745b0a | 7662 | WARN_ON(dio_data->reserve < len); |
7663 | dio_data->reserve -= len; | |
7664 | current->journal_info = dio_data; | |
c3473e83 | 7665 | } |
4b46fce2 | 7666 | |
eb838e73 JB |
7667 | /* |
7668 | * In the case of write we need to clear and unlock the entire range, | |
7669 | * in the case of read we need to unlock only the end area that we | |
7670 | * aren't using if there is any left over space. | |
7671 | */ | |
24c03fa5 | 7672 | if (lockstart < lockend) { |
0934856d MX |
7673 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
7674 | lockend, unlock_bits, 1, 0, | |
7675 | &cached_state, GFP_NOFS); | |
24c03fa5 | 7676 | } else { |
eb838e73 | 7677 | free_extent_state(cached_state); |
24c03fa5 | 7678 | } |
eb838e73 | 7679 | |
4b46fce2 JB |
7680 | free_extent_map(em); |
7681 | ||
7682 | return 0; | |
eb838e73 JB |
7683 | |
7684 | unlock_err: | |
eb838e73 JB |
7685 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
7686 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
9c9464cc | 7687 | err: |
50745b0a | 7688 | if (dio_data) |
7689 | current->journal_info = dio_data; | |
9c9464cc FM |
7690 | /* |
7691 | * Compensate the delalloc release we do in btrfs_direct_IO() when we | |
7692 | * write less data then expected, so that we don't underflow our inode's | |
7693 | * outstanding extents counter. | |
7694 | */ | |
7695 | if (create && dio_data) | |
7696 | adjust_dio_outstanding_extents(inode, dio_data, len); | |
7697 | ||
eb838e73 | 7698 | return ret; |
4b46fce2 JB |
7699 | } |
7700 | ||
8b110e39 MX |
7701 | static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, |
7702 | int rw, int mirror_num) | |
7703 | { | |
7704 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7705 | int ret; | |
7706 | ||
7707 | BUG_ON(rw & REQ_WRITE); | |
7708 | ||
7709 | bio_get(bio); | |
7710 | ||
7711 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, | |
7712 | BTRFS_WQ_ENDIO_DIO_REPAIR); | |
7713 | if (ret) | |
7714 | goto err; | |
7715 | ||
7716 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); | |
7717 | err: | |
7718 | bio_put(bio); | |
7719 | return ret; | |
7720 | } | |
7721 | ||
7722 | static int btrfs_check_dio_repairable(struct inode *inode, | |
7723 | struct bio *failed_bio, | |
7724 | struct io_failure_record *failrec, | |
7725 | int failed_mirror) | |
7726 | { | |
7727 | int num_copies; | |
7728 | ||
7729 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, | |
7730 | failrec->logical, failrec->len); | |
7731 | if (num_copies == 1) { | |
7732 | /* | |
7733 | * we only have a single copy of the data, so don't bother with | |
7734 | * all the retry and error correction code that follows. no | |
7735 | * matter what the error is, it is very likely to persist. | |
7736 | */ | |
7737 | pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7738 | num_copies, failrec->this_mirror, failed_mirror); | |
7739 | return 0; | |
7740 | } | |
7741 | ||
7742 | failrec->failed_mirror = failed_mirror; | |
7743 | failrec->this_mirror++; | |
7744 | if (failrec->this_mirror == failed_mirror) | |
7745 | failrec->this_mirror++; | |
7746 | ||
7747 | if (failrec->this_mirror > num_copies) { | |
7748 | pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n", | |
7749 | num_copies, failrec->this_mirror, failed_mirror); | |
7750 | return 0; | |
7751 | } | |
7752 | ||
7753 | return 1; | |
7754 | } | |
7755 | ||
7756 | static int dio_read_error(struct inode *inode, struct bio *failed_bio, | |
7757 | struct page *page, u64 start, u64 end, | |
7758 | int failed_mirror, bio_end_io_t *repair_endio, | |
7759 | void *repair_arg) | |
7760 | { | |
7761 | struct io_failure_record *failrec; | |
7762 | struct bio *bio; | |
7763 | int isector; | |
7764 | int read_mode; | |
7765 | int ret; | |
7766 | ||
7767 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); | |
7768 | ||
7769 | ret = btrfs_get_io_failure_record(inode, start, end, &failrec); | |
7770 | if (ret) | |
7771 | return ret; | |
7772 | ||
7773 | ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, | |
7774 | failed_mirror); | |
7775 | if (!ret) { | |
7776 | free_io_failure(inode, failrec); | |
7777 | return -EIO; | |
7778 | } | |
7779 | ||
7780 | if (failed_bio->bi_vcnt > 1) | |
7781 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; | |
7782 | else | |
7783 | read_mode = READ_SYNC; | |
7784 | ||
7785 | isector = start - btrfs_io_bio(failed_bio)->logical; | |
7786 | isector >>= inode->i_sb->s_blocksize_bits; | |
7787 | bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page, | |
7788 | 0, isector, repair_endio, repair_arg); | |
7789 | if (!bio) { | |
7790 | free_io_failure(inode, failrec); | |
7791 | return -EIO; | |
7792 | } | |
7793 | ||
7794 | btrfs_debug(BTRFS_I(inode)->root->fs_info, | |
7795 | "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", | |
7796 | read_mode, failrec->this_mirror, failrec->in_validation); | |
7797 | ||
7798 | ret = submit_dio_repair_bio(inode, bio, read_mode, | |
7799 | failrec->this_mirror); | |
7800 | if (ret) { | |
7801 | free_io_failure(inode, failrec); | |
7802 | bio_put(bio); | |
7803 | } | |
7804 | ||
7805 | return ret; | |
7806 | } | |
7807 | ||
7808 | struct btrfs_retry_complete { | |
7809 | struct completion done; | |
7810 | struct inode *inode; | |
7811 | u64 start; | |
7812 | int uptodate; | |
7813 | }; | |
7814 | ||
4246a0b6 | 7815 | static void btrfs_retry_endio_nocsum(struct bio *bio) |
8b110e39 MX |
7816 | { |
7817 | struct btrfs_retry_complete *done = bio->bi_private; | |
7818 | struct bio_vec *bvec; | |
7819 | int i; | |
7820 | ||
4246a0b6 | 7821 | if (bio->bi_error) |
8b110e39 MX |
7822 | goto end; |
7823 | ||
7824 | done->uptodate = 1; | |
7825 | bio_for_each_segment_all(bvec, bio, i) | |
7826 | clean_io_failure(done->inode, done->start, bvec->bv_page, 0); | |
7827 | end: | |
7828 | complete(&done->done); | |
7829 | bio_put(bio); | |
7830 | } | |
7831 | ||
7832 | static int __btrfs_correct_data_nocsum(struct inode *inode, | |
7833 | struct btrfs_io_bio *io_bio) | |
4b46fce2 | 7834 | { |
2c30c71b | 7835 | struct bio_vec *bvec; |
8b110e39 | 7836 | struct btrfs_retry_complete done; |
4b46fce2 | 7837 | u64 start; |
2c30c71b | 7838 | int i; |
c1dc0896 | 7839 | int ret; |
4b46fce2 | 7840 | |
8b110e39 MX |
7841 | start = io_bio->logical; |
7842 | done.inode = inode; | |
7843 | ||
7844 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { | |
7845 | try_again: | |
7846 | done.uptodate = 0; | |
7847 | done.start = start; | |
7848 | init_completion(&done.done); | |
7849 | ||
7850 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, | |
7851 | start + bvec->bv_len - 1, | |
7852 | io_bio->mirror_num, | |
7853 | btrfs_retry_endio_nocsum, &done); | |
7854 | if (ret) | |
7855 | return ret; | |
7856 | ||
7857 | wait_for_completion(&done.done); | |
7858 | ||
7859 | if (!done.uptodate) { | |
7860 | /* We might have another mirror, so try again */ | |
7861 | goto try_again; | |
7862 | } | |
7863 | ||
7864 | start += bvec->bv_len; | |
7865 | } | |
7866 | ||
7867 | return 0; | |
7868 | } | |
7869 | ||
4246a0b6 | 7870 | static void btrfs_retry_endio(struct bio *bio) |
8b110e39 MX |
7871 | { |
7872 | struct btrfs_retry_complete *done = bio->bi_private; | |
7873 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
7874 | struct bio_vec *bvec; | |
7875 | int uptodate; | |
7876 | int ret; | |
7877 | int i; | |
7878 | ||
4246a0b6 | 7879 | if (bio->bi_error) |
8b110e39 MX |
7880 | goto end; |
7881 | ||
7882 | uptodate = 1; | |
7883 | bio_for_each_segment_all(bvec, bio, i) { | |
7884 | ret = __readpage_endio_check(done->inode, io_bio, i, | |
7885 | bvec->bv_page, 0, | |
7886 | done->start, bvec->bv_len); | |
7887 | if (!ret) | |
7888 | clean_io_failure(done->inode, done->start, | |
7889 | bvec->bv_page, 0); | |
7890 | else | |
7891 | uptodate = 0; | |
7892 | } | |
7893 | ||
7894 | done->uptodate = uptodate; | |
7895 | end: | |
7896 | complete(&done->done); | |
7897 | bio_put(bio); | |
7898 | } | |
7899 | ||
7900 | static int __btrfs_subio_endio_read(struct inode *inode, | |
7901 | struct btrfs_io_bio *io_bio, int err) | |
7902 | { | |
7903 | struct bio_vec *bvec; | |
7904 | struct btrfs_retry_complete done; | |
7905 | u64 start; | |
7906 | u64 offset = 0; | |
7907 | int i; | |
7908 | int ret; | |
dc380aea | 7909 | |
8b110e39 | 7910 | err = 0; |
c1dc0896 | 7911 | start = io_bio->logical; |
8b110e39 MX |
7912 | done.inode = inode; |
7913 | ||
c1dc0896 | 7914 | bio_for_each_segment_all(bvec, &io_bio->bio, i) { |
dc380aea MX |
7915 | ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page, |
7916 | 0, start, bvec->bv_len); | |
8b110e39 MX |
7917 | if (likely(!ret)) |
7918 | goto next; | |
7919 | try_again: | |
7920 | done.uptodate = 0; | |
7921 | done.start = start; | |
7922 | init_completion(&done.done); | |
7923 | ||
7924 | ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page, start, | |
7925 | start + bvec->bv_len - 1, | |
7926 | io_bio->mirror_num, | |
7927 | btrfs_retry_endio, &done); | |
7928 | if (ret) { | |
7929 | err = ret; | |
7930 | goto next; | |
7931 | } | |
7932 | ||
7933 | wait_for_completion(&done.done); | |
7934 | ||
7935 | if (!done.uptodate) { | |
7936 | /* We might have another mirror, so try again */ | |
7937 | goto try_again; | |
7938 | } | |
7939 | next: | |
7940 | offset += bvec->bv_len; | |
4b46fce2 | 7941 | start += bvec->bv_len; |
2c30c71b | 7942 | } |
c1dc0896 MX |
7943 | |
7944 | return err; | |
7945 | } | |
7946 | ||
8b110e39 MX |
7947 | static int btrfs_subio_endio_read(struct inode *inode, |
7948 | struct btrfs_io_bio *io_bio, int err) | |
7949 | { | |
7950 | bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
7951 | ||
7952 | if (skip_csum) { | |
7953 | if (unlikely(err)) | |
7954 | return __btrfs_correct_data_nocsum(inode, io_bio); | |
7955 | else | |
7956 | return 0; | |
7957 | } else { | |
7958 | return __btrfs_subio_endio_read(inode, io_bio, err); | |
7959 | } | |
7960 | } | |
7961 | ||
4246a0b6 | 7962 | static void btrfs_endio_direct_read(struct bio *bio) |
c1dc0896 MX |
7963 | { |
7964 | struct btrfs_dio_private *dip = bio->bi_private; | |
7965 | struct inode *inode = dip->inode; | |
7966 | struct bio *dio_bio; | |
7967 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
4246a0b6 | 7968 | int err = bio->bi_error; |
c1dc0896 | 7969 | |
8b110e39 MX |
7970 | if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) |
7971 | err = btrfs_subio_endio_read(inode, io_bio, err); | |
c1dc0896 | 7972 | |
4b46fce2 | 7973 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, |
d0082371 | 7974 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 7975 | dio_bio = dip->dio_bio; |
4b46fce2 | 7976 | |
4b46fce2 | 7977 | kfree(dip); |
c0da7aa1 | 7978 | |
4246a0b6 | 7979 | dio_end_io(dio_bio, bio->bi_error); |
23ea8e5a MX |
7980 | |
7981 | if (io_bio->end_io) | |
7982 | io_bio->end_io(io_bio, err); | |
9be3395b | 7983 | bio_put(bio); |
4b46fce2 JB |
7984 | } |
7985 | ||
4246a0b6 | 7986 | static void btrfs_endio_direct_write(struct bio *bio) |
4b46fce2 JB |
7987 | { |
7988 | struct btrfs_dio_private *dip = bio->bi_private; | |
7989 | struct inode *inode = dip->inode; | |
7990 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b46fce2 | 7991 | struct btrfs_ordered_extent *ordered = NULL; |
163cf09c CM |
7992 | u64 ordered_offset = dip->logical_offset; |
7993 | u64 ordered_bytes = dip->bytes; | |
9be3395b | 7994 | struct bio *dio_bio; |
4b46fce2 JB |
7995 | int ret; |
7996 | ||
163cf09c CM |
7997 | again: |
7998 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
7999 | &ordered_offset, | |
4246a0b6 CH |
8000 | ordered_bytes, |
8001 | !bio->bi_error); | |
4b46fce2 | 8002 | if (!ret) |
163cf09c | 8003 | goto out_test; |
4b46fce2 | 8004 | |
9e0af237 LB |
8005 | btrfs_init_work(&ordered->work, btrfs_endio_write_helper, |
8006 | finish_ordered_fn, NULL, NULL); | |
fccb5d86 QW |
8007 | btrfs_queue_work(root->fs_info->endio_write_workers, |
8008 | &ordered->work); | |
163cf09c CM |
8009 | out_test: |
8010 | /* | |
8011 | * our bio might span multiple ordered extents. If we haven't | |
8012 | * completed the accounting for the whole dio, go back and try again | |
8013 | */ | |
8014 | if (ordered_offset < dip->logical_offset + dip->bytes) { | |
8015 | ordered_bytes = dip->logical_offset + dip->bytes - | |
8016 | ordered_offset; | |
5fd02043 | 8017 | ordered = NULL; |
163cf09c CM |
8018 | goto again; |
8019 | } | |
9be3395b | 8020 | dio_bio = dip->dio_bio; |
4b46fce2 | 8021 | |
4b46fce2 | 8022 | kfree(dip); |
c0da7aa1 | 8023 | |
4246a0b6 | 8024 | dio_end_io(dio_bio, bio->bi_error); |
9be3395b | 8025 | bio_put(bio); |
4b46fce2 JB |
8026 | } |
8027 | ||
eaf25d93 CM |
8028 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
8029 | struct bio *bio, int mirror_num, | |
8030 | unsigned long bio_flags, u64 offset) | |
8031 | { | |
8032 | int ret; | |
8033 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
8034 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); | |
79787eaa | 8035 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
8036 | return 0; |
8037 | } | |
8038 | ||
4246a0b6 | 8039 | static void btrfs_end_dio_bio(struct bio *bio) |
e65e1535 MX |
8040 | { |
8041 | struct btrfs_dio_private *dip = bio->bi_private; | |
4246a0b6 | 8042 | int err = bio->bi_error; |
e65e1535 | 8043 | |
8b110e39 MX |
8044 | if (err) |
8045 | btrfs_warn(BTRFS_I(dip->inode)->root->fs_info, | |
8046 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", | |
8047 | btrfs_ino(dip->inode), bio->bi_rw, | |
8048 | (unsigned long long)bio->bi_iter.bi_sector, | |
8049 | bio->bi_iter.bi_size, err); | |
8050 | ||
8051 | if (dip->subio_endio) | |
8052 | err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err); | |
c1dc0896 MX |
8053 | |
8054 | if (err) { | |
e65e1535 MX |
8055 | dip->errors = 1; |
8056 | ||
8057 | /* | |
8058 | * before atomic variable goto zero, we must make sure | |
8059 | * dip->errors is perceived to be set. | |
8060 | */ | |
4e857c58 | 8061 | smp_mb__before_atomic(); |
e65e1535 MX |
8062 | } |
8063 | ||
8064 | /* if there are more bios still pending for this dio, just exit */ | |
8065 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
8066 | goto out; | |
8067 | ||
9be3395b | 8068 | if (dip->errors) { |
e65e1535 | 8069 | bio_io_error(dip->orig_bio); |
9be3395b | 8070 | } else { |
4246a0b6 CH |
8071 | dip->dio_bio->bi_error = 0; |
8072 | bio_endio(dip->orig_bio); | |
e65e1535 MX |
8073 | } |
8074 | out: | |
8075 | bio_put(bio); | |
8076 | } | |
8077 | ||
8078 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, | |
8079 | u64 first_sector, gfp_t gfp_flags) | |
8080 | { | |
da2f0f74 | 8081 | struct bio *bio; |
22365979 | 8082 | bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags); |
da2f0f74 CM |
8083 | if (bio) |
8084 | bio_associate_current(bio); | |
8085 | return bio; | |
e65e1535 MX |
8086 | } |
8087 | ||
c1dc0896 MX |
8088 | static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, |
8089 | struct inode *inode, | |
8090 | struct btrfs_dio_private *dip, | |
8091 | struct bio *bio, | |
8092 | u64 file_offset) | |
8093 | { | |
8094 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); | |
8095 | struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio); | |
8096 | int ret; | |
8097 | ||
8098 | /* | |
8099 | * We load all the csum data we need when we submit | |
8100 | * the first bio to reduce the csum tree search and | |
8101 | * contention. | |
8102 | */ | |
8103 | if (dip->logical_offset == file_offset) { | |
8104 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, | |
8105 | file_offset); | |
8106 | if (ret) | |
8107 | return ret; | |
8108 | } | |
8109 | ||
8110 | if (bio == dip->orig_bio) | |
8111 | return 0; | |
8112 | ||
8113 | file_offset -= dip->logical_offset; | |
8114 | file_offset >>= inode->i_sb->s_blocksize_bits; | |
8115 | io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset); | |
8116 | ||
8117 | return 0; | |
8118 | } | |
8119 | ||
e65e1535 MX |
8120 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
8121 | int rw, u64 file_offset, int skip_sum, | |
c329861d | 8122 | int async_submit) |
e65e1535 | 8123 | { |
facc8a22 | 8124 | struct btrfs_dio_private *dip = bio->bi_private; |
e65e1535 MX |
8125 | int write = rw & REQ_WRITE; |
8126 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
8127 | int ret; | |
8128 | ||
b812ce28 JB |
8129 | if (async_submit) |
8130 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
8131 | ||
e65e1535 | 8132 | bio_get(bio); |
5fd02043 JB |
8133 | |
8134 | if (!write) { | |
bfebd8b5 DS |
8135 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, |
8136 | BTRFS_WQ_ENDIO_DATA); | |
5fd02043 JB |
8137 | if (ret) |
8138 | goto err; | |
8139 | } | |
e65e1535 | 8140 | |
1ae39938 JB |
8141 | if (skip_sum) |
8142 | goto map; | |
8143 | ||
8144 | if (write && async_submit) { | |
e65e1535 MX |
8145 | ret = btrfs_wq_submit_bio(root->fs_info, |
8146 | inode, rw, bio, 0, 0, | |
8147 | file_offset, | |
8148 | __btrfs_submit_bio_start_direct_io, | |
8149 | __btrfs_submit_bio_done); | |
8150 | goto err; | |
1ae39938 JB |
8151 | } else if (write) { |
8152 | /* | |
8153 | * If we aren't doing async submit, calculate the csum of the | |
8154 | * bio now. | |
8155 | */ | |
8156 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); | |
8157 | if (ret) | |
8158 | goto err; | |
23ea8e5a | 8159 | } else { |
c1dc0896 MX |
8160 | ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, |
8161 | file_offset); | |
c2db1073 TI |
8162 | if (ret) |
8163 | goto err; | |
8164 | } | |
1ae39938 JB |
8165 | map: |
8166 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); | |
e65e1535 MX |
8167 | err: |
8168 | bio_put(bio); | |
8169 | return ret; | |
8170 | } | |
8171 | ||
8172 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, | |
8173 | int skip_sum) | |
8174 | { | |
8175 | struct inode *inode = dip->inode; | |
8176 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
e65e1535 MX |
8177 | struct bio *bio; |
8178 | struct bio *orig_bio = dip->orig_bio; | |
8179 | struct bio_vec *bvec = orig_bio->bi_io_vec; | |
4f024f37 | 8180 | u64 start_sector = orig_bio->bi_iter.bi_sector; |
e65e1535 MX |
8181 | u64 file_offset = dip->logical_offset; |
8182 | u64 submit_len = 0; | |
8183 | u64 map_length; | |
8184 | int nr_pages = 0; | |
23ea8e5a | 8185 | int ret; |
1ae39938 | 8186 | int async_submit = 0; |
e65e1535 | 8187 | |
4f024f37 | 8188 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8189 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
e65e1535 | 8190 | &map_length, NULL, 0); |
7a5c3c9b | 8191 | if (ret) |
e65e1535 | 8192 | return -EIO; |
facc8a22 | 8193 | |
4f024f37 | 8194 | if (map_length >= orig_bio->bi_iter.bi_size) { |
02f57c7a | 8195 | bio = orig_bio; |
c1dc0896 | 8196 | dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED; |
02f57c7a JB |
8197 | goto submit; |
8198 | } | |
8199 | ||
53b381b3 | 8200 | /* async crcs make it difficult to collect full stripe writes. */ |
ffe2d203 | 8201 | if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK) |
53b381b3 DW |
8202 | async_submit = 0; |
8203 | else | |
8204 | async_submit = 1; | |
8205 | ||
02f57c7a JB |
8206 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
8207 | if (!bio) | |
8208 | return -ENOMEM; | |
7a5c3c9b | 8209 | |
02f57c7a JB |
8210 | bio->bi_private = dip; |
8211 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8212 | btrfs_io_bio(bio)->logical = file_offset; |
02f57c7a JB |
8213 | atomic_inc(&dip->pending_bios); |
8214 | ||
e65e1535 | 8215 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
ee39b432 | 8216 | if (map_length < submit_len + bvec->bv_len || |
e65e1535 | 8217 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
ee39b432 | 8218 | bvec->bv_offset) < bvec->bv_len) { |
e65e1535 MX |
8219 | /* |
8220 | * inc the count before we submit the bio so | |
8221 | * we know the end IO handler won't happen before | |
8222 | * we inc the count. Otherwise, the dip might get freed | |
8223 | * before we're done setting it up | |
8224 | */ | |
8225 | atomic_inc(&dip->pending_bios); | |
8226 | ret = __btrfs_submit_dio_bio(bio, inode, rw, | |
8227 | file_offset, skip_sum, | |
c329861d | 8228 | async_submit); |
e65e1535 MX |
8229 | if (ret) { |
8230 | bio_put(bio); | |
8231 | atomic_dec(&dip->pending_bios); | |
8232 | goto out_err; | |
8233 | } | |
8234 | ||
e65e1535 MX |
8235 | start_sector += submit_len >> 9; |
8236 | file_offset += submit_len; | |
8237 | ||
8238 | submit_len = 0; | |
8239 | nr_pages = 0; | |
8240 | ||
8241 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, | |
8242 | start_sector, GFP_NOFS); | |
8243 | if (!bio) | |
8244 | goto out_err; | |
8245 | bio->bi_private = dip; | |
8246 | bio->bi_end_io = btrfs_end_dio_bio; | |
c1dc0896 | 8247 | btrfs_io_bio(bio)->logical = file_offset; |
e65e1535 | 8248 | |
4f024f37 | 8249 | map_length = orig_bio->bi_iter.bi_size; |
53b381b3 | 8250 | ret = btrfs_map_block(root->fs_info, rw, |
3ec706c8 | 8251 | start_sector << 9, |
e65e1535 MX |
8252 | &map_length, NULL, 0); |
8253 | if (ret) { | |
8254 | bio_put(bio); | |
8255 | goto out_err; | |
8256 | } | |
8257 | } else { | |
8258 | submit_len += bvec->bv_len; | |
67871254 | 8259 | nr_pages++; |
e65e1535 MX |
8260 | bvec++; |
8261 | } | |
8262 | } | |
8263 | ||
02f57c7a | 8264 | submit: |
e65e1535 | 8265 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
c329861d | 8266 | async_submit); |
e65e1535 MX |
8267 | if (!ret) |
8268 | return 0; | |
8269 | ||
8270 | bio_put(bio); | |
8271 | out_err: | |
8272 | dip->errors = 1; | |
8273 | /* | |
8274 | * before atomic variable goto zero, we must | |
8275 | * make sure dip->errors is perceived to be set. | |
8276 | */ | |
4e857c58 | 8277 | smp_mb__before_atomic(); |
e65e1535 MX |
8278 | if (atomic_dec_and_test(&dip->pending_bios)) |
8279 | bio_io_error(dip->orig_bio); | |
8280 | ||
8281 | /* bio_end_io() will handle error, so we needn't return it */ | |
8282 | return 0; | |
8283 | } | |
8284 | ||
9be3395b CM |
8285 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
8286 | struct inode *inode, loff_t file_offset) | |
4b46fce2 | 8287 | { |
61de718f FM |
8288 | struct btrfs_dio_private *dip = NULL; |
8289 | struct bio *io_bio = NULL; | |
23ea8e5a | 8290 | struct btrfs_io_bio *btrfs_bio; |
4b46fce2 | 8291 | int skip_sum; |
7b6d91da | 8292 | int write = rw & REQ_WRITE; |
4b46fce2 JB |
8293 | int ret = 0; |
8294 | ||
8295 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
8296 | ||
9be3395b | 8297 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
9be3395b CM |
8298 | if (!io_bio) { |
8299 | ret = -ENOMEM; | |
8300 | goto free_ordered; | |
8301 | } | |
8302 | ||
c1dc0896 | 8303 | dip = kzalloc(sizeof(*dip), GFP_NOFS); |
4b46fce2 JB |
8304 | if (!dip) { |
8305 | ret = -ENOMEM; | |
61de718f | 8306 | goto free_ordered; |
4b46fce2 | 8307 | } |
4b46fce2 | 8308 | |
9be3395b | 8309 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
8310 | dip->inode = inode; |
8311 | dip->logical_offset = file_offset; | |
4f024f37 KO |
8312 | dip->bytes = dio_bio->bi_iter.bi_size; |
8313 | dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9; | |
9be3395b | 8314 | io_bio->bi_private = dip; |
9be3395b CM |
8315 | dip->orig_bio = io_bio; |
8316 | dip->dio_bio = dio_bio; | |
e65e1535 | 8317 | atomic_set(&dip->pending_bios, 0); |
c1dc0896 MX |
8318 | btrfs_bio = btrfs_io_bio(io_bio); |
8319 | btrfs_bio->logical = file_offset; | |
4b46fce2 | 8320 | |
c1dc0896 | 8321 | if (write) { |
9be3395b | 8322 | io_bio->bi_end_io = btrfs_endio_direct_write; |
c1dc0896 | 8323 | } else { |
9be3395b | 8324 | io_bio->bi_end_io = btrfs_endio_direct_read; |
c1dc0896 MX |
8325 | dip->subio_endio = btrfs_subio_endio_read; |
8326 | } | |
4b46fce2 | 8327 | |
e65e1535 MX |
8328 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
8329 | if (!ret) | |
eaf25d93 | 8330 | return; |
9be3395b | 8331 | |
23ea8e5a MX |
8332 | if (btrfs_bio->end_io) |
8333 | btrfs_bio->end_io(btrfs_bio, ret); | |
9be3395b | 8334 | |
4b46fce2 JB |
8335 | free_ordered: |
8336 | /* | |
61de718f FM |
8337 | * If we arrived here it means either we failed to submit the dip |
8338 | * or we either failed to clone the dio_bio or failed to allocate the | |
8339 | * dip. If we cloned the dio_bio and allocated the dip, we can just | |
8340 | * call bio_endio against our io_bio so that we get proper resource | |
8341 | * cleanup if we fail to submit the dip, otherwise, we must do the | |
8342 | * same as btrfs_endio_direct_[write|read] because we can't call these | |
8343 | * callbacks - they require an allocated dip and a clone of dio_bio. | |
4b46fce2 | 8344 | */ |
61de718f | 8345 | if (io_bio && dip) { |
4246a0b6 CH |
8346 | io_bio->bi_error = -EIO; |
8347 | bio_endio(io_bio); | |
61de718f FM |
8348 | /* |
8349 | * The end io callbacks free our dip, do the final put on io_bio | |
8350 | * and all the cleanup and final put for dio_bio (through | |
8351 | * dio_end_io()). | |
8352 | */ | |
8353 | dip = NULL; | |
8354 | io_bio = NULL; | |
8355 | } else { | |
8356 | if (write) { | |
8357 | struct btrfs_ordered_extent *ordered; | |
8358 | ||
8359 | ordered = btrfs_lookup_ordered_extent(inode, | |
8360 | file_offset); | |
8361 | set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); | |
8362 | /* | |
8363 | * Decrements our ref on the ordered extent and removes | |
8364 | * the ordered extent from the inode's ordered tree, | |
8365 | * doing all the proper resource cleanup such as for the | |
8366 | * reserved space and waking up any waiters for this | |
8367 | * ordered extent (through btrfs_remove_ordered_extent). | |
8368 | */ | |
8369 | btrfs_finish_ordered_io(ordered); | |
8370 | } else { | |
8371 | unlock_extent(&BTRFS_I(inode)->io_tree, file_offset, | |
8372 | file_offset + dio_bio->bi_iter.bi_size - 1); | |
8373 | } | |
4246a0b6 | 8374 | dio_bio->bi_error = -EIO; |
61de718f FM |
8375 | /* |
8376 | * Releases and cleans up our dio_bio, no need to bio_put() | |
8377 | * nor bio_endio()/bio_io_error() against dio_bio. | |
8378 | */ | |
8379 | dio_end_io(dio_bio, ret); | |
4b46fce2 | 8380 | } |
61de718f FM |
8381 | if (io_bio) |
8382 | bio_put(io_bio); | |
8383 | kfree(dip); | |
4b46fce2 JB |
8384 | } |
8385 | ||
6f673763 | 8386 | static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb, |
28060d5d | 8387 | const struct iov_iter *iter, loff_t offset) |
5a5f79b5 CM |
8388 | { |
8389 | int seg; | |
a1b75f7d | 8390 | int i; |
5a5f79b5 CM |
8391 | unsigned blocksize_mask = root->sectorsize - 1; |
8392 | ssize_t retval = -EINVAL; | |
5a5f79b5 CM |
8393 | |
8394 | if (offset & blocksize_mask) | |
8395 | goto out; | |
8396 | ||
28060d5d AV |
8397 | if (iov_iter_alignment(iter) & blocksize_mask) |
8398 | goto out; | |
a1b75f7d | 8399 | |
28060d5d | 8400 | /* If this is a write we don't need to check anymore */ |
6f673763 | 8401 | if (iov_iter_rw(iter) == WRITE) |
28060d5d AV |
8402 | return 0; |
8403 | /* | |
8404 | * Check to make sure we don't have duplicate iov_base's in this | |
8405 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
8406 | * when reading back. | |
8407 | */ | |
8408 | for (seg = 0; seg < iter->nr_segs; seg++) { | |
8409 | for (i = seg + 1; i < iter->nr_segs; i++) { | |
8410 | if (iter->iov[seg].iov_base == iter->iov[i].iov_base) | |
a1b75f7d JB |
8411 | goto out; |
8412 | } | |
5a5f79b5 CM |
8413 | } |
8414 | retval = 0; | |
8415 | out: | |
8416 | return retval; | |
8417 | } | |
eb838e73 | 8418 | |
22c6186e OS |
8419 | static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, |
8420 | loff_t offset) | |
16432985 | 8421 | { |
4b46fce2 JB |
8422 | struct file *file = iocb->ki_filp; |
8423 | struct inode *inode = file->f_mapping->host; | |
50745b0a | 8424 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8425 | struct btrfs_dio_data dio_data = { 0 }; | |
0934856d | 8426 | size_t count = 0; |
2e60a51e | 8427 | int flags = 0; |
38851cc1 MX |
8428 | bool wakeup = true; |
8429 | bool relock = false; | |
0934856d | 8430 | ssize_t ret; |
4b46fce2 | 8431 | |
6f673763 | 8432 | if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset)) |
5a5f79b5 | 8433 | return 0; |
3f7c579c | 8434 | |
fe0f07d0 | 8435 | inode_dio_begin(inode); |
4e857c58 | 8436 | smp_mb__after_atomic(); |
38851cc1 | 8437 | |
0e267c44 | 8438 | /* |
41bd9ca4 MX |
8439 | * The generic stuff only does filemap_write_and_wait_range, which |
8440 | * isn't enough if we've written compressed pages to this area, so | |
8441 | * we need to flush the dirty pages again to make absolutely sure | |
8442 | * that any outstanding dirty pages are on disk. | |
0e267c44 | 8443 | */ |
a6cbcd4a | 8444 | count = iov_iter_count(iter); |
41bd9ca4 MX |
8445 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
8446 | &BTRFS_I(inode)->runtime_flags)) | |
9a025a08 WS |
8447 | filemap_fdatawrite_range(inode->i_mapping, offset, |
8448 | offset + count - 1); | |
0e267c44 | 8449 | |
6f673763 | 8450 | if (iov_iter_rw(iter) == WRITE) { |
38851cc1 MX |
8451 | /* |
8452 | * If the write DIO is beyond the EOF, we need update | |
8453 | * the isize, but it is protected by i_mutex. So we can | |
8454 | * not unlock the i_mutex at this case. | |
8455 | */ | |
8456 | if (offset + count <= inode->i_size) { | |
8457 | mutex_unlock(&inode->i_mutex); | |
8458 | relock = true; | |
8459 | } | |
7cf5b976 | 8460 | ret = btrfs_delalloc_reserve_space(inode, offset, count); |
0934856d | 8461 | if (ret) |
38851cc1 | 8462 | goto out; |
50745b0a | 8463 | dio_data.outstanding_extents = div64_u64(count + |
e1cbbfa5 JB |
8464 | BTRFS_MAX_EXTENT_SIZE - 1, |
8465 | BTRFS_MAX_EXTENT_SIZE); | |
8466 | ||
8467 | /* | |
8468 | * We need to know how many extents we reserved so that we can | |
8469 | * do the accounting properly if we go over the number we | |
8470 | * originally calculated. Abuse current->journal_info for this. | |
8471 | */ | |
50745b0a | 8472 | dio_data.reserve = round_up(count, root->sectorsize); |
8473 | current->journal_info = &dio_data; | |
ee39b432 DS |
8474 | } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK, |
8475 | &BTRFS_I(inode)->runtime_flags)) { | |
fe0f07d0 | 8476 | inode_dio_end(inode); |
38851cc1 MX |
8477 | flags = DIO_LOCKING | DIO_SKIP_HOLES; |
8478 | wakeup = false; | |
0934856d MX |
8479 | } |
8480 | ||
17f8c842 OS |
8481 | ret = __blockdev_direct_IO(iocb, inode, |
8482 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, | |
8483 | iter, offset, btrfs_get_blocks_direct, NULL, | |
8484 | btrfs_submit_direct, flags); | |
6f673763 | 8485 | if (iov_iter_rw(iter) == WRITE) { |
e1cbbfa5 | 8486 | current->journal_info = NULL; |
ddba1bfc | 8487 | if (ret < 0 && ret != -EIOCBQUEUED) { |
50745b0a | 8488 | if (dio_data.reserve) |
7cf5b976 QW |
8489 | btrfs_delalloc_release_space(inode, offset, |
8490 | dio_data.reserve); | |
ddba1bfc | 8491 | } else if (ret >= 0 && (size_t)ret < count) |
7cf5b976 QW |
8492 | btrfs_delalloc_release_space(inode, offset, |
8493 | count - (size_t)ret); | |
0934856d | 8494 | } |
38851cc1 | 8495 | out: |
2e60a51e | 8496 | if (wakeup) |
fe0f07d0 | 8497 | inode_dio_end(inode); |
38851cc1 MX |
8498 | if (relock) |
8499 | mutex_lock(&inode->i_mutex); | |
0934856d MX |
8500 | |
8501 | return ret; | |
16432985 CM |
8502 | } |
8503 | ||
05dadc09 TI |
8504 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
8505 | ||
1506fcc8 YS |
8506 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
8507 | __u64 start, __u64 len) | |
8508 | { | |
05dadc09 TI |
8509 | int ret; |
8510 | ||
8511 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
8512 | if (ret) | |
8513 | return ret; | |
8514 | ||
ec29ed5b | 8515 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
8516 | } |
8517 | ||
a52d9a80 | 8518 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 8519 | { |
d1310b2e CM |
8520 | struct extent_io_tree *tree; |
8521 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 8522 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 8523 | } |
1832a6d5 | 8524 | |
a52d9a80 | 8525 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 8526 | { |
d1310b2e | 8527 | struct extent_io_tree *tree; |
be7bd730 JB |
8528 | struct inode *inode = page->mapping->host; |
8529 | int ret; | |
b888db2b CM |
8530 | |
8531 | if (current->flags & PF_MEMALLOC) { | |
8532 | redirty_page_for_writepage(wbc, page); | |
8533 | unlock_page(page); | |
8534 | return 0; | |
8535 | } | |
be7bd730 JB |
8536 | |
8537 | /* | |
8538 | * If we are under memory pressure we will call this directly from the | |
8539 | * VM, we need to make sure we have the inode referenced for the ordered | |
8540 | * extent. If not just return like we didn't do anything. | |
8541 | */ | |
8542 | if (!igrab(inode)) { | |
8543 | redirty_page_for_writepage(wbc, page); | |
8544 | return AOP_WRITEPAGE_ACTIVATE; | |
8545 | } | |
d1310b2e | 8546 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
be7bd730 JB |
8547 | ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
8548 | btrfs_add_delayed_iput(inode); | |
8549 | return ret; | |
9ebefb18 CM |
8550 | } |
8551 | ||
48a3b636 ES |
8552 | static int btrfs_writepages(struct address_space *mapping, |
8553 | struct writeback_control *wbc) | |
b293f02e | 8554 | { |
d1310b2e | 8555 | struct extent_io_tree *tree; |
771ed689 | 8556 | |
d1310b2e | 8557 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
8558 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
8559 | } | |
8560 | ||
3ab2fb5a CM |
8561 | static int |
8562 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
8563 | struct list_head *pages, unsigned nr_pages) | |
8564 | { | |
d1310b2e CM |
8565 | struct extent_io_tree *tree; |
8566 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
8567 | return extent_readpages(tree, mapping, pages, nr_pages, |
8568 | btrfs_get_extent); | |
8569 | } | |
e6dcd2dc | 8570 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 8571 | { |
d1310b2e CM |
8572 | struct extent_io_tree *tree; |
8573 | struct extent_map_tree *map; | |
a52d9a80 | 8574 | int ret; |
8c2383c3 | 8575 | |
d1310b2e CM |
8576 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
8577 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 8578 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
8579 | if (ret == 1) { |
8580 | ClearPagePrivate(page); | |
8581 | set_page_private(page, 0); | |
8582 | page_cache_release(page); | |
39279cc3 | 8583 | } |
a52d9a80 | 8584 | return ret; |
39279cc3 CM |
8585 | } |
8586 | ||
e6dcd2dc CM |
8587 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
8588 | { | |
98509cfc CM |
8589 | if (PageWriteback(page) || PageDirty(page)) |
8590 | return 0; | |
b335b003 | 8591 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
e6dcd2dc CM |
8592 | } |
8593 | ||
d47992f8 LC |
8594 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
8595 | unsigned int length) | |
39279cc3 | 8596 | { |
5fd02043 | 8597 | struct inode *inode = page->mapping->host; |
d1310b2e | 8598 | struct extent_io_tree *tree; |
e6dcd2dc | 8599 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 8600 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8601 | u64 page_start = page_offset(page); |
8602 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; | |
131e404a | 8603 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 8604 | |
8b62b72b CM |
8605 | /* |
8606 | * we have the page locked, so new writeback can't start, | |
8607 | * and the dirty bit won't be cleared while we are here. | |
8608 | * | |
8609 | * Wait for IO on this page so that we can safely clear | |
8610 | * the PagePrivate2 bit and do ordered accounting | |
8611 | */ | |
e6dcd2dc | 8612 | wait_on_page_writeback(page); |
8b62b72b | 8613 | |
5fd02043 | 8614 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
8615 | if (offset) { |
8616 | btrfs_releasepage(page, GFP_NOFS); | |
8617 | return; | |
8618 | } | |
131e404a FDBM |
8619 | |
8620 | if (!inode_evicting) | |
8621 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); | |
8622 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
e6dcd2dc | 8623 | if (ordered) { |
eb84ae03 CM |
8624 | /* |
8625 | * IO on this page will never be started, so we need | |
8626 | * to account for any ordered extents now | |
8627 | */ | |
131e404a FDBM |
8628 | if (!inode_evicting) |
8629 | clear_extent_bit(tree, page_start, page_end, | |
8630 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
8631 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | | |
8632 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
8633 | GFP_NOFS); | |
8b62b72b CM |
8634 | /* |
8635 | * whoever cleared the private bit is responsible | |
8636 | * for the finish_ordered_io | |
8637 | */ | |
77cef2ec JB |
8638 | if (TestClearPagePrivate2(page)) { |
8639 | struct btrfs_ordered_inode_tree *tree; | |
8640 | u64 new_len; | |
8641 | ||
8642 | tree = &BTRFS_I(inode)->ordered_tree; | |
8643 | ||
8644 | spin_lock_irq(&tree->lock); | |
8645 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
8646 | new_len = page_start - ordered->file_offset; | |
8647 | if (new_len < ordered->truncated_len) | |
8648 | ordered->truncated_len = new_len; | |
8649 | spin_unlock_irq(&tree->lock); | |
8650 | ||
8651 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
8652 | page_start, | |
8653 | PAGE_CACHE_SIZE, 1)) | |
8654 | btrfs_finish_ordered_io(ordered); | |
8b62b72b | 8655 | } |
e6dcd2dc | 8656 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
8657 | if (!inode_evicting) { |
8658 | cached_state = NULL; | |
8659 | lock_extent_bits(tree, page_start, page_end, 0, | |
8660 | &cached_state); | |
8661 | } | |
8662 | } | |
8663 | ||
b9d0b389 QW |
8664 | /* |
8665 | * Qgroup reserved space handler | |
8666 | * Page here will be either | |
8667 | * 1) Already written to disk | |
8668 | * In this case, its reserved space is released from data rsv map | |
8669 | * and will be freed by delayed_ref handler finally. | |
8670 | * So even we call qgroup_free_data(), it won't decrease reserved | |
8671 | * space. | |
8672 | * 2) Not written to disk | |
8673 | * This means the reserved space should be freed here. | |
8674 | */ | |
8675 | btrfs_qgroup_free_data(inode, page_start, PAGE_CACHE_SIZE); | |
131e404a FDBM |
8676 | if (!inode_evicting) { |
8677 | clear_extent_bit(tree, page_start, page_end, | |
8678 | EXTENT_LOCKED | EXTENT_DIRTY | | |
8679 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
8680 | EXTENT_DEFRAG, 1, 1, | |
8681 | &cached_state, GFP_NOFS); | |
8682 | ||
8683 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 8684 | } |
e6dcd2dc | 8685 | |
4a096752 | 8686 | ClearPageChecked(page); |
9ad6b7bc | 8687 | if (PagePrivate(page)) { |
9ad6b7bc CM |
8688 | ClearPagePrivate(page); |
8689 | set_page_private(page, 0); | |
8690 | page_cache_release(page); | |
8691 | } | |
39279cc3 CM |
8692 | } |
8693 | ||
9ebefb18 CM |
8694 | /* |
8695 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
8696 | * called from a page fault handler when a page is first dirtied. Hence we must | |
8697 | * be careful to check for EOF conditions here. We set the page up correctly | |
8698 | * for a written page which means we get ENOSPC checking when writing into | |
8699 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
8700 | * support these features. | |
8701 | * | |
8702 | * We are not allowed to take the i_mutex here so we have to play games to | |
8703 | * protect against truncate races as the page could now be beyond EOF. Because | |
8704 | * vmtruncate() writes the inode size before removing pages, once we have the | |
8705 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
8706 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
8707 | * unlock the page. | |
8708 | */ | |
c2ec175c | 8709 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
9ebefb18 | 8710 | { |
c2ec175c | 8711 | struct page *page = vmf->page; |
496ad9aa | 8712 | struct inode *inode = file_inode(vma->vm_file); |
1832a6d5 | 8713 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
8714 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
8715 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 8716 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
8717 | char *kaddr; |
8718 | unsigned long zero_start; | |
9ebefb18 | 8719 | loff_t size; |
1832a6d5 | 8720 | int ret; |
9998eb70 | 8721 | int reserved = 0; |
a52d9a80 | 8722 | u64 page_start; |
e6dcd2dc | 8723 | u64 page_end; |
9ebefb18 | 8724 | |
b2b5ef5c | 8725 | sb_start_pagefault(inode->i_sb); |
df480633 QW |
8726 | page_start = page_offset(page); |
8727 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
8728 | ||
7cf5b976 QW |
8729 | ret = btrfs_delalloc_reserve_space(inode, page_start, |
8730 | PAGE_CACHE_SIZE); | |
9998eb70 | 8731 | if (!ret) { |
e41f941a | 8732 | ret = file_update_time(vma->vm_file); |
9998eb70 CM |
8733 | reserved = 1; |
8734 | } | |
56a76f82 NP |
8735 | if (ret) { |
8736 | if (ret == -ENOMEM) | |
8737 | ret = VM_FAULT_OOM; | |
8738 | else /* -ENOSPC, -EIO, etc */ | |
8739 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
8740 | if (reserved) |
8741 | goto out; | |
8742 | goto out_noreserve; | |
56a76f82 | 8743 | } |
1832a6d5 | 8744 | |
56a76f82 | 8745 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 8746 | again: |
9ebefb18 | 8747 | lock_page(page); |
9ebefb18 | 8748 | size = i_size_read(inode); |
a52d9a80 | 8749 | |
9ebefb18 | 8750 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 8751 | (page_start >= size)) { |
9ebefb18 CM |
8752 | /* page got truncated out from underneath us */ |
8753 | goto out_unlock; | |
8754 | } | |
e6dcd2dc CM |
8755 | wait_on_page_writeback(page); |
8756 | ||
d0082371 | 8757 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
8758 | set_page_extent_mapped(page); |
8759 | ||
eb84ae03 CM |
8760 | /* |
8761 | * we can't set the delalloc bits if there are pending ordered | |
8762 | * extents. Drop our locks and wait for them to finish | |
8763 | */ | |
e6dcd2dc CM |
8764 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
8765 | if (ordered) { | |
2ac55d41 JB |
8766 | unlock_extent_cached(io_tree, page_start, page_end, |
8767 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 8768 | unlock_page(page); |
eb84ae03 | 8769 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
8770 | btrfs_put_ordered_extent(ordered); |
8771 | goto again; | |
8772 | } | |
8773 | ||
fbf19087 JB |
8774 | /* |
8775 | * XXX - page_mkwrite gets called every time the page is dirtied, even | |
8776 | * if it was already dirty, so for space accounting reasons we need to | |
8777 | * clear any delalloc bits for the range we are fixing to save. There | |
8778 | * is probably a better way to do this, but for now keep consistent with | |
8779 | * prepare_pages in the normal write path. | |
8780 | */ | |
2ac55d41 | 8781 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
8782 | EXTENT_DIRTY | EXTENT_DELALLOC | |
8783 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 8784 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 8785 | |
2ac55d41 JB |
8786 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
8787 | &cached_state); | |
9ed74f2d | 8788 | if (ret) { |
2ac55d41 JB |
8789 | unlock_extent_cached(io_tree, page_start, page_end, |
8790 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
8791 | ret = VM_FAULT_SIGBUS; |
8792 | goto out_unlock; | |
8793 | } | |
e6dcd2dc | 8794 | ret = 0; |
9ebefb18 CM |
8795 | |
8796 | /* page is wholly or partially inside EOF */ | |
a52d9a80 | 8797 | if (page_start + PAGE_CACHE_SIZE > size) |
e6dcd2dc | 8798 | zero_start = size & ~PAGE_CACHE_MASK; |
9ebefb18 | 8799 | else |
e6dcd2dc | 8800 | zero_start = PAGE_CACHE_SIZE; |
9ebefb18 | 8801 | |
e6dcd2dc CM |
8802 | if (zero_start != PAGE_CACHE_SIZE) { |
8803 | kaddr = kmap(page); | |
8804 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); | |
8805 | flush_dcache_page(page); | |
8806 | kunmap(page); | |
8807 | } | |
247e743c | 8808 | ClearPageChecked(page); |
e6dcd2dc | 8809 | set_page_dirty(page); |
50a9b214 | 8810 | SetPageUptodate(page); |
5a3f23d5 | 8811 | |
257c62e1 CM |
8812 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
8813 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | |
46d8bc34 | 8814 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 8815 | |
2ac55d41 | 8816 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
8817 | |
8818 | out_unlock: | |
b2b5ef5c JK |
8819 | if (!ret) { |
8820 | sb_end_pagefault(inode->i_sb); | |
50a9b214 | 8821 | return VM_FAULT_LOCKED; |
b2b5ef5c | 8822 | } |
9ebefb18 | 8823 | unlock_page(page); |
1832a6d5 | 8824 | out: |
7cf5b976 | 8825 | btrfs_delalloc_release_space(inode, page_start, PAGE_CACHE_SIZE); |
9998eb70 | 8826 | out_noreserve: |
b2b5ef5c | 8827 | sb_end_pagefault(inode->i_sb); |
9ebefb18 CM |
8828 | return ret; |
8829 | } | |
8830 | ||
a41ad394 | 8831 | static int btrfs_truncate(struct inode *inode) |
39279cc3 CM |
8832 | { |
8833 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
fcb80c2a | 8834 | struct btrfs_block_rsv *rsv; |
a71754fc | 8835 | int ret = 0; |
3893e33b | 8836 | int err = 0; |
39279cc3 | 8837 | struct btrfs_trans_handle *trans; |
dbe674a9 | 8838 | u64 mask = root->sectorsize - 1; |
07127184 | 8839 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 | 8840 | |
0ef8b726 JB |
8841 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
8842 | (u64)-1); | |
8843 | if (ret) | |
8844 | return ret; | |
39279cc3 | 8845 | |
fcb80c2a JB |
8846 | /* |
8847 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have | |
8848 | * 3 things going on here | |
8849 | * | |
8850 | * 1) We need to reserve space for our orphan item and the space to | |
8851 | * delete our orphan item. Lord knows we don't want to have a dangling | |
8852 | * orphan item because we didn't reserve space to remove it. | |
8853 | * | |
8854 | * 2) We need to reserve space to update our inode. | |
8855 | * | |
8856 | * 3) We need to have something to cache all the space that is going to | |
8857 | * be free'd up by the truncate operation, but also have some slack | |
8858 | * space reserved in case it uses space during the truncate (thank you | |
8859 | * very much snapshotting). | |
8860 | * | |
8861 | * And we need these to all be seperate. The fact is we can use alot of | |
8862 | * space doing the truncate, and we have no earthly idea how much space | |
8863 | * we will use, so we need the truncate reservation to be seperate so it | |
8864 | * doesn't end up using space reserved for updating the inode or | |
8865 | * removing the orphan item. We also need to be able to stop the | |
8866 | * transaction and start a new one, which means we need to be able to | |
8867 | * update the inode several times, and we have no idea of knowing how | |
8868 | * many times that will be, so we can't just reserve 1 item for the | |
8869 | * entirety of the opration, so that has to be done seperately as well. | |
8870 | * Then there is the orphan item, which does indeed need to be held on | |
8871 | * to for the whole operation, and we need nobody to touch this reserved | |
8872 | * space except the orphan code. | |
8873 | * | |
8874 | * So that leaves us with | |
8875 | * | |
8876 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
8877 | * 2) rsv - for the truncate reservation, which we will steal from the | |
8878 | * transaction reservation. | |
8879 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
8880 | * updating the inode. | |
8881 | */ | |
66d8f3dd | 8882 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
8883 | if (!rsv) |
8884 | return -ENOMEM; | |
4a338542 | 8885 | rsv->size = min_size; |
ca7e70f5 | 8886 | rsv->failfast = 1; |
f0cd846e | 8887 | |
907cbceb | 8888 | /* |
07127184 | 8889 | * 1 for the truncate slack space |
907cbceb JB |
8890 | * 1 for updating the inode. |
8891 | */ | |
f3fe820c | 8892 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
8893 | if (IS_ERR(trans)) { |
8894 | err = PTR_ERR(trans); | |
8895 | goto out; | |
8896 | } | |
f0cd846e | 8897 | |
907cbceb JB |
8898 | /* Migrate the slack space for the truncate to our reserve */ |
8899 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, | |
8900 | min_size); | |
fcb80c2a | 8901 | BUG_ON(ret); |
f0cd846e | 8902 | |
5dc562c5 JB |
8903 | /* |
8904 | * So if we truncate and then write and fsync we normally would just | |
8905 | * write the extents that changed, which is a problem if we need to | |
8906 | * first truncate that entire inode. So set this flag so we write out | |
8907 | * all of the extents in the inode to the sync log so we're completely | |
8908 | * safe. | |
8909 | */ | |
8910 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 8911 | trans->block_rsv = rsv; |
907cbceb | 8912 | |
8082510e YZ |
8913 | while (1) { |
8914 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
8915 | inode->i_size, | |
8916 | BTRFS_EXTENT_DATA_KEY); | |
28ed1345 | 8917 | if (ret != -ENOSPC && ret != -EAGAIN) { |
3893e33b | 8918 | err = ret; |
8082510e | 8919 | break; |
3893e33b | 8920 | } |
39279cc3 | 8921 | |
fcb80c2a | 8922 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e | 8923 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
8924 | if (ret) { |
8925 | err = ret; | |
8926 | break; | |
8927 | } | |
ca7e70f5 | 8928 | |
8082510e | 8929 | btrfs_end_transaction(trans, root); |
b53d3f5d | 8930 | btrfs_btree_balance_dirty(root); |
ca7e70f5 JB |
8931 | |
8932 | trans = btrfs_start_transaction(root, 2); | |
8933 | if (IS_ERR(trans)) { | |
8934 | ret = err = PTR_ERR(trans); | |
8935 | trans = NULL; | |
8936 | break; | |
8937 | } | |
8938 | ||
8939 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, | |
8940 | rsv, min_size); | |
8941 | BUG_ON(ret); /* shouldn't happen */ | |
8942 | trans->block_rsv = rsv; | |
8082510e YZ |
8943 | } |
8944 | ||
8945 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 8946 | trans->block_rsv = root->orphan_block_rsv; |
8082510e | 8947 | ret = btrfs_orphan_del(trans, inode); |
3893e33b JB |
8948 | if (ret) |
8949 | err = ret; | |
8082510e YZ |
8950 | } |
8951 | ||
917c16b2 CM |
8952 | if (trans) { |
8953 | trans->block_rsv = &root->fs_info->trans_block_rsv; | |
8954 | ret = btrfs_update_inode(trans, root, inode); | |
8955 | if (ret && !err) | |
8956 | err = ret; | |
7b128766 | 8957 | |
7ad85bb7 | 8958 | ret = btrfs_end_transaction(trans, root); |
b53d3f5d | 8959 | btrfs_btree_balance_dirty(root); |
917c16b2 | 8960 | } |
fcb80c2a JB |
8961 | |
8962 | out: | |
8963 | btrfs_free_block_rsv(root, rsv); | |
8964 | ||
3893e33b JB |
8965 | if (ret && !err) |
8966 | err = ret; | |
a41ad394 | 8967 | |
3893e33b | 8968 | return err; |
39279cc3 CM |
8969 | } |
8970 | ||
d352ac68 CM |
8971 | /* |
8972 | * create a new subvolume directory/inode (helper for the ioctl). | |
8973 | */ | |
d2fb3437 | 8974 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
63541927 FDBM |
8975 | struct btrfs_root *new_root, |
8976 | struct btrfs_root *parent_root, | |
8977 | u64 new_dirid) | |
39279cc3 | 8978 | { |
39279cc3 | 8979 | struct inode *inode; |
76dda93c | 8980 | int err; |
00e4e6b3 | 8981 | u64 index = 0; |
39279cc3 | 8982 | |
12fc9d09 FA |
8983 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
8984 | new_dirid, new_dirid, | |
8985 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
8986 | &index); | |
54aa1f4d | 8987 | if (IS_ERR(inode)) |
f46b5a66 | 8988 | return PTR_ERR(inode); |
39279cc3 CM |
8989 | inode->i_op = &btrfs_dir_inode_operations; |
8990 | inode->i_fop = &btrfs_dir_file_operations; | |
8991 | ||
bfe86848 | 8992 | set_nlink(inode, 1); |
dbe674a9 | 8993 | btrfs_i_size_write(inode, 0); |
b0d5d10f | 8994 | unlock_new_inode(inode); |
3b96362c | 8995 | |
63541927 FDBM |
8996 | err = btrfs_subvol_inherit_props(trans, new_root, parent_root); |
8997 | if (err) | |
8998 | btrfs_err(new_root->fs_info, | |
351fd353 | 8999 | "error inheriting subvolume %llu properties: %d", |
63541927 FDBM |
9000 | new_root->root_key.objectid, err); |
9001 | ||
76dda93c | 9002 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 9003 | |
76dda93c | 9004 | iput(inode); |
ce598979 | 9005 | return err; |
39279cc3 CM |
9006 | } |
9007 | ||
39279cc3 CM |
9008 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
9009 | { | |
9010 | struct btrfs_inode *ei; | |
2ead6ae7 | 9011 | struct inode *inode; |
39279cc3 CM |
9012 | |
9013 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
9014 | if (!ei) | |
9015 | return NULL; | |
2ead6ae7 YZ |
9016 | |
9017 | ei->root = NULL; | |
2ead6ae7 | 9018 | ei->generation = 0; |
15ee9bc7 | 9019 | ei->last_trans = 0; |
257c62e1 | 9020 | ei->last_sub_trans = 0; |
e02119d5 | 9021 | ei->logged_trans = 0; |
2ead6ae7 | 9022 | ei->delalloc_bytes = 0; |
47059d93 | 9023 | ei->defrag_bytes = 0; |
2ead6ae7 YZ |
9024 | ei->disk_i_size = 0; |
9025 | ei->flags = 0; | |
7709cde3 | 9026 | ei->csum_bytes = 0; |
2ead6ae7 | 9027 | ei->index_cnt = (u64)-1; |
67de1176 | 9028 | ei->dir_index = 0; |
2ead6ae7 | 9029 | ei->last_unlink_trans = 0; |
46d8bc34 | 9030 | ei->last_log_commit = 0; |
8089fe62 | 9031 | ei->delayed_iput_count = 0; |
2ead6ae7 | 9032 | |
9e0baf60 JB |
9033 | spin_lock_init(&ei->lock); |
9034 | ei->outstanding_extents = 0; | |
9035 | ei->reserved_extents = 0; | |
2ead6ae7 | 9036 | |
72ac3c0d | 9037 | ei->runtime_flags = 0; |
261507a0 | 9038 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 9039 | |
16cdcec7 MX |
9040 | ei->delayed_node = NULL; |
9041 | ||
9cc97d64 | 9042 | ei->i_otime.tv_sec = 0; |
9043 | ei->i_otime.tv_nsec = 0; | |
9044 | ||
2ead6ae7 | 9045 | inode = &ei->vfs_inode; |
a8067e02 | 9046 | extent_map_tree_init(&ei->extent_tree); |
f993c883 DS |
9047 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
9048 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); | |
0b32f4bb JB |
9049 | ei->io_tree.track_uptodate = 1; |
9050 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 9051 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 9052 | mutex_init(&ei->log_mutex); |
f248679e | 9053 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 9054 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 9055 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
8089fe62 | 9056 | INIT_LIST_HEAD(&ei->delayed_iput); |
2ead6ae7 YZ |
9057 | RB_CLEAR_NODE(&ei->rb_node); |
9058 | ||
9059 | return inode; | |
39279cc3 CM |
9060 | } |
9061 | ||
aaedb55b JB |
9062 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
9063 | void btrfs_test_destroy_inode(struct inode *inode) | |
9064 | { | |
9065 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
9066 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
9067 | } | |
9068 | #endif | |
9069 | ||
fa0d7e3d NP |
9070 | static void btrfs_i_callback(struct rcu_head *head) |
9071 | { | |
9072 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
9073 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
9074 | } | |
9075 | ||
39279cc3 CM |
9076 | void btrfs_destroy_inode(struct inode *inode) |
9077 | { | |
e6dcd2dc | 9078 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
9079 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9080 | ||
b3d9b7a3 | 9081 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 9082 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
9083 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
9084 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 JB |
9085 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
9086 | WARN_ON(BTRFS_I(inode)->csum_bytes); | |
47059d93 | 9087 | WARN_ON(BTRFS_I(inode)->defrag_bytes); |
39279cc3 | 9088 | |
a6dbd429 JB |
9089 | /* |
9090 | * This can happen where we create an inode, but somebody else also | |
9091 | * created the same inode and we need to destroy the one we already | |
9092 | * created. | |
9093 | */ | |
9094 | if (!root) | |
9095 | goto free; | |
9096 | ||
8a35d95f JB |
9097 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
9098 | &BTRFS_I(inode)->runtime_flags)) { | |
c2cf52eb | 9099 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
c1c9ff7c | 9100 | btrfs_ino(inode)); |
8a35d95f | 9101 | atomic_dec(&root->orphan_inodes); |
7b128766 | 9102 | } |
7b128766 | 9103 | |
d397712b | 9104 | while (1) { |
e6dcd2dc CM |
9105 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
9106 | if (!ordered) | |
9107 | break; | |
9108 | else { | |
c2cf52eb | 9109 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
c1c9ff7c | 9110 | ordered->file_offset, ordered->len); |
e6dcd2dc CM |
9111 | btrfs_remove_ordered_extent(inode, ordered); |
9112 | btrfs_put_ordered_extent(ordered); | |
9113 | btrfs_put_ordered_extent(ordered); | |
9114 | } | |
9115 | } | |
56fa9d07 | 9116 | btrfs_qgroup_check_reserved_leak(inode); |
5d4f98a2 | 9117 | inode_tree_del(inode); |
5b21f2ed | 9118 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
a6dbd429 | 9119 | free: |
fa0d7e3d | 9120 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
9121 | } |
9122 | ||
45321ac5 | 9123 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
9124 | { |
9125 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 9126 | |
6379ef9f NA |
9127 | if (root == NULL) |
9128 | return 1; | |
9129 | ||
fa6ac876 | 9130 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 9131 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 9132 | return 1; |
76dda93c | 9133 | else |
45321ac5 | 9134 | return generic_drop_inode(inode); |
76dda93c YZ |
9135 | } |
9136 | ||
0ee0fda0 | 9137 | static void init_once(void *foo) |
39279cc3 CM |
9138 | { |
9139 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
9140 | ||
9141 | inode_init_once(&ei->vfs_inode); | |
9142 | } | |
9143 | ||
9144 | void btrfs_destroy_cachep(void) | |
9145 | { | |
8c0a8537 KS |
9146 | /* |
9147 | * Make sure all delayed rcu free inodes are flushed before we | |
9148 | * destroy cache. | |
9149 | */ | |
9150 | rcu_barrier(); | |
39279cc3 CM |
9151 | if (btrfs_inode_cachep) |
9152 | kmem_cache_destroy(btrfs_inode_cachep); | |
9153 | if (btrfs_trans_handle_cachep) | |
9154 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
9155 | if (btrfs_transaction_cachep) | |
9156 | kmem_cache_destroy(btrfs_transaction_cachep); | |
39279cc3 CM |
9157 | if (btrfs_path_cachep) |
9158 | kmem_cache_destroy(btrfs_path_cachep); | |
dc89e982 JB |
9159 | if (btrfs_free_space_cachep) |
9160 | kmem_cache_destroy(btrfs_free_space_cachep); | |
39279cc3 CM |
9161 | } |
9162 | ||
9163 | int btrfs_init_cachep(void) | |
9164 | { | |
837e1972 | 9165 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 CH |
9166 | sizeof(struct btrfs_inode), 0, |
9167 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); | |
39279cc3 CM |
9168 | if (!btrfs_inode_cachep) |
9169 | goto fail; | |
9601e3f6 | 9170 | |
837e1972 | 9171 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 CH |
9172 | sizeof(struct btrfs_trans_handle), 0, |
9173 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
9174 | if (!btrfs_trans_handle_cachep) |
9175 | goto fail; | |
9601e3f6 | 9176 | |
837e1972 | 9177 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
9601e3f6 CH |
9178 | sizeof(struct btrfs_transaction), 0, |
9179 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
9180 | if (!btrfs_transaction_cachep) |
9181 | goto fail; | |
9601e3f6 | 9182 | |
837e1972 | 9183 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 CH |
9184 | sizeof(struct btrfs_path), 0, |
9185 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
9186 | if (!btrfs_path_cachep) |
9187 | goto fail; | |
9601e3f6 | 9188 | |
837e1972 | 9189 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 JB |
9190 | sizeof(struct btrfs_free_space), 0, |
9191 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
9192 | if (!btrfs_free_space_cachep) | |
9193 | goto fail; | |
9194 | ||
39279cc3 CM |
9195 | return 0; |
9196 | fail: | |
9197 | btrfs_destroy_cachep(); | |
9198 | return -ENOMEM; | |
9199 | } | |
9200 | ||
9201 | static int btrfs_getattr(struct vfsmount *mnt, | |
9202 | struct dentry *dentry, struct kstat *stat) | |
9203 | { | |
df0af1a5 | 9204 | u64 delalloc_bytes; |
2b0143b5 | 9205 | struct inode *inode = d_inode(dentry); |
fadc0d8b DS |
9206 | u32 blocksize = inode->i_sb->s_blocksize; |
9207 | ||
39279cc3 | 9208 | generic_fillattr(inode, stat); |
0ee5dc67 | 9209 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
d6667462 | 9210 | stat->blksize = PAGE_CACHE_SIZE; |
df0af1a5 MX |
9211 | |
9212 | spin_lock(&BTRFS_I(inode)->lock); | |
9213 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; | |
9214 | spin_unlock(&BTRFS_I(inode)->lock); | |
fadc0d8b | 9215 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 9216 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
9217 | return 0; |
9218 | } | |
9219 | ||
d397712b CM |
9220 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
9221 | struct inode *new_dir, struct dentry *new_dentry) | |
39279cc3 CM |
9222 | { |
9223 | struct btrfs_trans_handle *trans; | |
9224 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
4df27c4d | 9225 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
2b0143b5 DH |
9226 | struct inode *new_inode = d_inode(new_dentry); |
9227 | struct inode *old_inode = d_inode(old_dentry); | |
39279cc3 | 9228 | struct timespec ctime = CURRENT_TIME; |
00e4e6b3 | 9229 | u64 index = 0; |
4df27c4d | 9230 | u64 root_objectid; |
39279cc3 | 9231 | int ret; |
33345d01 | 9232 | u64 old_ino = btrfs_ino(old_inode); |
39279cc3 | 9233 | |
33345d01 | 9234 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
9235 | return -EPERM; |
9236 | ||
4df27c4d | 9237 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 9238 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
9239 | return -EXDEV; |
9240 | ||
33345d01 LZ |
9241 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
9242 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) | |
39279cc3 | 9243 | return -ENOTEMPTY; |
5f39d397 | 9244 | |
4df27c4d YZ |
9245 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
9246 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
9247 | return -ENOTEMPTY; | |
9c52057c CM |
9248 | |
9249 | ||
9250 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 9251 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
9252 | new_dentry->d_name.name, |
9253 | new_dentry->d_name.len); | |
9254 | ||
9255 | if (ret) { | |
9256 | if (ret == -EEXIST) { | |
9257 | /* we shouldn't get | |
9258 | * eexist without a new_inode */ | |
fae7f21c | 9259 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
9260 | return ret; |
9261 | } | |
9262 | } else { | |
9263 | /* maybe -EOVERFLOW */ | |
9264 | return ret; | |
9265 | } | |
9266 | } | |
9267 | ret = 0; | |
9268 | ||
5a3f23d5 | 9269 | /* |
8d875f95 CM |
9270 | * we're using rename to replace one file with another. Start IO on it |
9271 | * now so we don't add too much work to the end of the transaction | |
5a3f23d5 | 9272 | */ |
8d875f95 | 9273 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size) |
5a3f23d5 CM |
9274 | filemap_flush(old_inode->i_mapping); |
9275 | ||
76dda93c | 9276 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 9277 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9278 | down_read(&root->fs_info->subvol_sem); |
a22285a6 YZ |
9279 | /* |
9280 | * We want to reserve the absolute worst case amount of items. So if | |
9281 | * both inodes are subvols and we need to unlink them then that would | |
9282 | * require 4 item modifications, but if they are both normal inodes it | |
9283 | * would require 5 item modifications, so we'll assume their normal | |
9284 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items | |
9285 | * should cover the worst case number of items we'll modify. | |
9286 | */ | |
6e137ed3 | 9287 | trans = btrfs_start_transaction(root, 11); |
b44c59a8 JL |
9288 | if (IS_ERR(trans)) { |
9289 | ret = PTR_ERR(trans); | |
9290 | goto out_notrans; | |
9291 | } | |
76dda93c | 9292 | |
4df27c4d YZ |
9293 | if (dest != root) |
9294 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 9295 | |
a5719521 YZ |
9296 | ret = btrfs_set_inode_index(new_dir, &index); |
9297 | if (ret) | |
9298 | goto out_fail; | |
5a3f23d5 | 9299 | |
67de1176 | 9300 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 9301 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d | 9302 | /* force full log commit if subvolume involved. */ |
995946dd | 9303 | btrfs_set_log_full_commit(root->fs_info, trans); |
4df27c4d | 9304 | } else { |
a5719521 YZ |
9305 | ret = btrfs_insert_inode_ref(trans, dest, |
9306 | new_dentry->d_name.name, | |
9307 | new_dentry->d_name.len, | |
33345d01 LZ |
9308 | old_ino, |
9309 | btrfs_ino(new_dir), index); | |
a5719521 YZ |
9310 | if (ret) |
9311 | goto out_fail; | |
4df27c4d YZ |
9312 | /* |
9313 | * this is an ugly little race, but the rename is required | |
9314 | * to make sure that if we crash, the inode is either at the | |
9315 | * old name or the new one. pinning the log transaction lets | |
9316 | * us make sure we don't allow a log commit to come in after | |
9317 | * we unlink the name but before we add the new name back in. | |
9318 | */ | |
9319 | btrfs_pin_log_trans(root); | |
9320 | } | |
5a3f23d5 | 9321 | |
0c4d2d95 JB |
9322 | inode_inc_iversion(old_dir); |
9323 | inode_inc_iversion(new_dir); | |
9324 | inode_inc_iversion(old_inode); | |
39279cc3 CM |
9325 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
9326 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
9327 | old_inode->i_ctime = ctime; | |
5f39d397 | 9328 | |
12fcfd22 CM |
9329 | if (old_dentry->d_parent != new_dentry->d_parent) |
9330 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | |
9331 | ||
33345d01 | 9332 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
9333 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
9334 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
9335 | old_dentry->d_name.name, | |
9336 | old_dentry->d_name.len); | |
9337 | } else { | |
92986796 | 9338 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
2b0143b5 | 9339 | d_inode(old_dentry), |
92986796 AV |
9340 | old_dentry->d_name.name, |
9341 | old_dentry->d_name.len); | |
9342 | if (!ret) | |
9343 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 9344 | } |
79787eaa JM |
9345 | if (ret) { |
9346 | btrfs_abort_transaction(trans, root, ret); | |
9347 | goto out_fail; | |
9348 | } | |
39279cc3 CM |
9349 | |
9350 | if (new_inode) { | |
0c4d2d95 | 9351 | inode_inc_iversion(new_inode); |
39279cc3 | 9352 | new_inode->i_ctime = CURRENT_TIME; |
33345d01 | 9353 | if (unlikely(btrfs_ino(new_inode) == |
4df27c4d YZ |
9354 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
9355 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
9356 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
9357 | root_objectid, | |
9358 | new_dentry->d_name.name, | |
9359 | new_dentry->d_name.len); | |
9360 | BUG_ON(new_inode->i_nlink == 0); | |
9361 | } else { | |
9362 | ret = btrfs_unlink_inode(trans, dest, new_dir, | |
2b0143b5 | 9363 | d_inode(new_dentry), |
4df27c4d YZ |
9364 | new_dentry->d_name.name, |
9365 | new_dentry->d_name.len); | |
9366 | } | |
4ef31a45 | 9367 | if (!ret && new_inode->i_nlink == 0) |
2b0143b5 | 9368 | ret = btrfs_orphan_add(trans, d_inode(new_dentry)); |
79787eaa JM |
9369 | if (ret) { |
9370 | btrfs_abort_transaction(trans, root, ret); | |
9371 | goto out_fail; | |
9372 | } | |
39279cc3 | 9373 | } |
aec7477b | 9374 | |
4df27c4d YZ |
9375 | ret = btrfs_add_link(trans, new_dir, old_inode, |
9376 | new_dentry->d_name.name, | |
a5719521 | 9377 | new_dentry->d_name.len, 0, index); |
79787eaa JM |
9378 | if (ret) { |
9379 | btrfs_abort_transaction(trans, root, ret); | |
9380 | goto out_fail; | |
9381 | } | |
39279cc3 | 9382 | |
67de1176 MX |
9383 | if (old_inode->i_nlink == 1) |
9384 | BTRFS_I(old_inode)->dir_index = index; | |
9385 | ||
33345d01 | 9386 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
10d9f309 | 9387 | struct dentry *parent = new_dentry->d_parent; |
6a912213 | 9388 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
4df27c4d YZ |
9389 | btrfs_end_log_trans(root); |
9390 | } | |
39279cc3 | 9391 | out_fail: |
7ad85bb7 | 9392 | btrfs_end_transaction(trans, root); |
b44c59a8 | 9393 | out_notrans: |
33345d01 | 9394 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 9395 | up_read(&root->fs_info->subvol_sem); |
9ed74f2d | 9396 | |
39279cc3 CM |
9397 | return ret; |
9398 | } | |
9399 | ||
80ace85c MS |
9400 | static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
9401 | struct inode *new_dir, struct dentry *new_dentry, | |
9402 | unsigned int flags) | |
9403 | { | |
9404 | if (flags & ~RENAME_NOREPLACE) | |
9405 | return -EINVAL; | |
9406 | ||
9407 | return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry); | |
9408 | } | |
9409 | ||
8ccf6f19 MX |
9410 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
9411 | { | |
9412 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 9413 | struct inode *inode; |
8ccf6f19 MX |
9414 | |
9415 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
9416 | work); | |
9f23e289 JB |
9417 | inode = delalloc_work->inode; |
9418 | if (delalloc_work->wait) { | |
9419 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
9420 | } else { | |
9421 | filemap_flush(inode->i_mapping); | |
9422 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
9423 | &BTRFS_I(inode)->runtime_flags)) | |
9424 | filemap_flush(inode->i_mapping); | |
9425 | } | |
8ccf6f19 MX |
9426 | |
9427 | if (delalloc_work->delay_iput) | |
9f23e289 | 9428 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 9429 | else |
9f23e289 | 9430 | iput(inode); |
8ccf6f19 MX |
9431 | complete(&delalloc_work->completion); |
9432 | } | |
9433 | ||
9434 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
9435 | int wait, int delay_iput) | |
9436 | { | |
9437 | struct btrfs_delalloc_work *work; | |
9438 | ||
100d5702 | 9439 | work = kmalloc(sizeof(*work), GFP_NOFS); |
8ccf6f19 MX |
9440 | if (!work) |
9441 | return NULL; | |
9442 | ||
9443 | init_completion(&work->completion); | |
9444 | INIT_LIST_HEAD(&work->list); | |
9445 | work->inode = inode; | |
9446 | work->wait = wait; | |
9447 | work->delay_iput = delay_iput; | |
9e0af237 LB |
9448 | WARN_ON_ONCE(!inode); |
9449 | btrfs_init_work(&work->work, btrfs_flush_delalloc_helper, | |
9450 | btrfs_run_delalloc_work, NULL, NULL); | |
8ccf6f19 MX |
9451 | |
9452 | return work; | |
9453 | } | |
9454 | ||
9455 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
9456 | { | |
9457 | wait_for_completion(&work->completion); | |
100d5702 | 9458 | kfree(work); |
8ccf6f19 MX |
9459 | } |
9460 | ||
d352ac68 CM |
9461 | /* |
9462 | * some fairly slow code that needs optimization. This walks the list | |
9463 | * of all the inodes with pending delalloc and forces them to disk. | |
9464 | */ | |
6c255e67 MX |
9465 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput, |
9466 | int nr) | |
ea8c2819 | 9467 | { |
ea8c2819 | 9468 | struct btrfs_inode *binode; |
5b21f2ed | 9469 | struct inode *inode; |
8ccf6f19 MX |
9470 | struct btrfs_delalloc_work *work, *next; |
9471 | struct list_head works; | |
1eafa6c7 | 9472 | struct list_head splice; |
8ccf6f19 | 9473 | int ret = 0; |
ea8c2819 | 9474 | |
8ccf6f19 | 9475 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 9476 | INIT_LIST_HEAD(&splice); |
63607cc8 | 9477 | |
573bfb72 | 9478 | mutex_lock(&root->delalloc_mutex); |
eb73c1b7 MX |
9479 | spin_lock(&root->delalloc_lock); |
9480 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
9481 | while (!list_empty(&splice)) { |
9482 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 9483 | delalloc_inodes); |
1eafa6c7 | 9484 | |
eb73c1b7 MX |
9485 | list_move_tail(&binode->delalloc_inodes, |
9486 | &root->delalloc_inodes); | |
5b21f2ed | 9487 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 9488 | if (!inode) { |
eb73c1b7 | 9489 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 9490 | continue; |
df0af1a5 | 9491 | } |
eb73c1b7 | 9492 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 MX |
9493 | |
9494 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); | |
5d99a998 | 9495 | if (!work) { |
f4ab9ea7 JB |
9496 | if (delay_iput) |
9497 | btrfs_add_delayed_iput(inode); | |
9498 | else | |
9499 | iput(inode); | |
1eafa6c7 | 9500 | ret = -ENOMEM; |
a1ecaabb | 9501 | goto out; |
5b21f2ed | 9502 | } |
1eafa6c7 | 9503 | list_add_tail(&work->list, &works); |
a44903ab QW |
9504 | btrfs_queue_work(root->fs_info->flush_workers, |
9505 | &work->work); | |
6c255e67 MX |
9506 | ret++; |
9507 | if (nr != -1 && ret >= nr) | |
a1ecaabb | 9508 | goto out; |
5b21f2ed | 9509 | cond_resched(); |
eb73c1b7 | 9510 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 9511 | } |
eb73c1b7 | 9512 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 9513 | |
a1ecaabb | 9514 | out: |
eb73c1b7 MX |
9515 | list_for_each_entry_safe(work, next, &works, list) { |
9516 | list_del_init(&work->list); | |
9517 | btrfs_wait_and_free_delalloc_work(work); | |
9518 | } | |
9519 | ||
9520 | if (!list_empty_careful(&splice)) { | |
9521 | spin_lock(&root->delalloc_lock); | |
9522 | list_splice_tail(&splice, &root->delalloc_inodes); | |
9523 | spin_unlock(&root->delalloc_lock); | |
9524 | } | |
573bfb72 | 9525 | mutex_unlock(&root->delalloc_mutex); |
eb73c1b7 MX |
9526 | return ret; |
9527 | } | |
1eafa6c7 | 9528 | |
eb73c1b7 MX |
9529 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
9530 | { | |
9531 | int ret; | |
1eafa6c7 | 9532 | |
2c21b4d7 | 9533 | if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) |
eb73c1b7 MX |
9534 | return -EROFS; |
9535 | ||
6c255e67 MX |
9536 | ret = __start_delalloc_inodes(root, delay_iput, -1); |
9537 | if (ret > 0) | |
9538 | ret = 0; | |
eb73c1b7 MX |
9539 | /* |
9540 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
9541 | * we have to make sure the IO is actually started and that |
9542 | * ordered extents get created before we return | |
9543 | */ | |
9544 | atomic_inc(&root->fs_info->async_submit_draining); | |
d397712b | 9545 | while (atomic_read(&root->fs_info->nr_async_submits) || |
771ed689 | 9546 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
8c8bee1d | 9547 | wait_event(root->fs_info->async_submit_wait, |
771ed689 CM |
9548 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
9549 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | |
8c8bee1d CM |
9550 | } |
9551 | atomic_dec(&root->fs_info->async_submit_draining); | |
eb73c1b7 MX |
9552 | return ret; |
9553 | } | |
9554 | ||
6c255e67 MX |
9555 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput, |
9556 | int nr) | |
eb73c1b7 MX |
9557 | { |
9558 | struct btrfs_root *root; | |
9559 | struct list_head splice; | |
9560 | int ret; | |
9561 | ||
2c21b4d7 | 9562 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
eb73c1b7 MX |
9563 | return -EROFS; |
9564 | ||
9565 | INIT_LIST_HEAD(&splice); | |
9566 | ||
573bfb72 | 9567 | mutex_lock(&fs_info->delalloc_root_mutex); |
eb73c1b7 MX |
9568 | spin_lock(&fs_info->delalloc_root_lock); |
9569 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
6c255e67 | 9570 | while (!list_empty(&splice) && nr) { |
eb73c1b7 MX |
9571 | root = list_first_entry(&splice, struct btrfs_root, |
9572 | delalloc_root); | |
9573 | root = btrfs_grab_fs_root(root); | |
9574 | BUG_ON(!root); | |
9575 | list_move_tail(&root->delalloc_root, | |
9576 | &fs_info->delalloc_roots); | |
9577 | spin_unlock(&fs_info->delalloc_root_lock); | |
9578 | ||
6c255e67 | 9579 | ret = __start_delalloc_inodes(root, delay_iput, nr); |
eb73c1b7 | 9580 | btrfs_put_fs_root(root); |
6c255e67 | 9581 | if (ret < 0) |
eb73c1b7 MX |
9582 | goto out; |
9583 | ||
6c255e67 MX |
9584 | if (nr != -1) { |
9585 | nr -= ret; | |
9586 | WARN_ON(nr < 0); | |
9587 | } | |
eb73c1b7 | 9588 | spin_lock(&fs_info->delalloc_root_lock); |
8ccf6f19 | 9589 | } |
eb73c1b7 | 9590 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 9591 | |
6c255e67 | 9592 | ret = 0; |
eb73c1b7 MX |
9593 | atomic_inc(&fs_info->async_submit_draining); |
9594 | while (atomic_read(&fs_info->nr_async_submits) || | |
9595 | atomic_read(&fs_info->async_delalloc_pages)) { | |
9596 | wait_event(fs_info->async_submit_wait, | |
9597 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
9598 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
9599 | } | |
9600 | atomic_dec(&fs_info->async_submit_draining); | |
eb73c1b7 | 9601 | out: |
1eafa6c7 | 9602 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
9603 | spin_lock(&fs_info->delalloc_root_lock); |
9604 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
9605 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 9606 | } |
573bfb72 | 9607 | mutex_unlock(&fs_info->delalloc_root_mutex); |
8ccf6f19 | 9608 | return ret; |
ea8c2819 CM |
9609 | } |
9610 | ||
39279cc3 CM |
9611 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
9612 | const char *symname) | |
9613 | { | |
9614 | struct btrfs_trans_handle *trans; | |
9615 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9616 | struct btrfs_path *path; | |
9617 | struct btrfs_key key; | |
1832a6d5 | 9618 | struct inode *inode = NULL; |
39279cc3 CM |
9619 | int err; |
9620 | int drop_inode = 0; | |
9621 | u64 objectid; | |
67871254 | 9622 | u64 index = 0; |
39279cc3 CM |
9623 | int name_len; |
9624 | int datasize; | |
5f39d397 | 9625 | unsigned long ptr; |
39279cc3 | 9626 | struct btrfs_file_extent_item *ei; |
5f39d397 | 9627 | struct extent_buffer *leaf; |
39279cc3 | 9628 | |
f06becc4 | 9629 | name_len = strlen(symname); |
39279cc3 CM |
9630 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
9631 | return -ENAMETOOLONG; | |
1832a6d5 | 9632 | |
9ed74f2d JB |
9633 | /* |
9634 | * 2 items for inode item and ref | |
9635 | * 2 items for dir items | |
9636 | * 1 item for xattr if selinux is on | |
9637 | */ | |
a22285a6 YZ |
9638 | trans = btrfs_start_transaction(root, 5); |
9639 | if (IS_ERR(trans)) | |
9640 | return PTR_ERR(trans); | |
1832a6d5 | 9641 | |
581bb050 LZ |
9642 | err = btrfs_find_free_ino(root, &objectid); |
9643 | if (err) | |
9644 | goto out_unlock; | |
9645 | ||
aec7477b | 9646 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 9647 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 9648 | S_IFLNK|S_IRWXUGO, &index); |
7cf96da3 TI |
9649 | if (IS_ERR(inode)) { |
9650 | err = PTR_ERR(inode); | |
39279cc3 | 9651 | goto out_unlock; |
7cf96da3 | 9652 | } |
39279cc3 | 9653 | |
ad19db71 CS |
9654 | /* |
9655 | * If the active LSM wants to access the inode during | |
9656 | * d_instantiate it needs these. Smack checks to see | |
9657 | * if the filesystem supports xattrs by looking at the | |
9658 | * ops vector. | |
9659 | */ | |
9660 | inode->i_fop = &btrfs_file_operations; | |
9661 | inode->i_op = &btrfs_file_inode_operations; | |
b0d5d10f | 9662 | inode->i_mapping->a_ops = &btrfs_aops; |
b0d5d10f CM |
9663 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9664 | ||
9665 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); | |
9666 | if (err) | |
9667 | goto out_unlock_inode; | |
ad19db71 | 9668 | |
a1b075d2 | 9669 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 9670 | if (err) |
b0d5d10f | 9671 | goto out_unlock_inode; |
39279cc3 CM |
9672 | |
9673 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
9674 | if (!path) { |
9675 | err = -ENOMEM; | |
b0d5d10f | 9676 | goto out_unlock_inode; |
d8926bb3 | 9677 | } |
33345d01 | 9678 | key.objectid = btrfs_ino(inode); |
39279cc3 | 9679 | key.offset = 0; |
962a298f | 9680 | key.type = BTRFS_EXTENT_DATA_KEY; |
39279cc3 CM |
9681 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
9682 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
9683 | datasize); | |
54aa1f4d | 9684 | if (err) { |
b0839166 | 9685 | btrfs_free_path(path); |
b0d5d10f | 9686 | goto out_unlock_inode; |
54aa1f4d | 9687 | } |
5f39d397 CM |
9688 | leaf = path->nodes[0]; |
9689 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
9690 | struct btrfs_file_extent_item); | |
9691 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
9692 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 9693 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
9694 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
9695 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
9696 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
9697 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
9698 | ||
39279cc3 | 9699 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
9700 | write_extent_buffer(leaf, symname, ptr, name_len); |
9701 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 9702 | btrfs_free_path(path); |
5f39d397 | 9703 | |
39279cc3 CM |
9704 | inode->i_op = &btrfs_symlink_inode_operations; |
9705 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
d899e052 | 9706 | inode_set_bytes(inode, name_len); |
f06becc4 | 9707 | btrfs_i_size_write(inode, name_len); |
54aa1f4d | 9708 | err = btrfs_update_inode(trans, root, inode); |
b0d5d10f | 9709 | if (err) { |
54aa1f4d | 9710 | drop_inode = 1; |
b0d5d10f CM |
9711 | goto out_unlock_inode; |
9712 | } | |
9713 | ||
9714 | unlock_new_inode(inode); | |
9715 | d_instantiate(dentry, inode); | |
39279cc3 CM |
9716 | |
9717 | out_unlock: | |
7ad85bb7 | 9718 | btrfs_end_transaction(trans, root); |
39279cc3 CM |
9719 | if (drop_inode) { |
9720 | inode_dec_link_count(inode); | |
9721 | iput(inode); | |
9722 | } | |
b53d3f5d | 9723 | btrfs_btree_balance_dirty(root); |
39279cc3 | 9724 | return err; |
b0d5d10f CM |
9725 | |
9726 | out_unlock_inode: | |
9727 | drop_inode = 1; | |
9728 | unlock_new_inode(inode); | |
9729 | goto out_unlock; | |
39279cc3 | 9730 | } |
16432985 | 9731 | |
0af3d00b JB |
9732 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
9733 | u64 start, u64 num_bytes, u64 min_size, | |
9734 | loff_t actual_len, u64 *alloc_hint, | |
9735 | struct btrfs_trans_handle *trans) | |
d899e052 | 9736 | { |
5dc562c5 JB |
9737 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
9738 | struct extent_map *em; | |
d899e052 YZ |
9739 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9740 | struct btrfs_key ins; | |
d899e052 | 9741 | u64 cur_offset = start; |
55a61d1d | 9742 | u64 i_size; |
154ea289 | 9743 | u64 cur_bytes; |
0b670dc4 | 9744 | u64 last_alloc = (u64)-1; |
d899e052 | 9745 | int ret = 0; |
0af3d00b | 9746 | bool own_trans = true; |
d899e052 | 9747 | |
0af3d00b JB |
9748 | if (trans) |
9749 | own_trans = false; | |
d899e052 | 9750 | while (num_bytes > 0) { |
0af3d00b JB |
9751 | if (own_trans) { |
9752 | trans = btrfs_start_transaction(root, 3); | |
9753 | if (IS_ERR(trans)) { | |
9754 | ret = PTR_ERR(trans); | |
9755 | break; | |
9756 | } | |
5a303d5d YZ |
9757 | } |
9758 | ||
154ea289 CM |
9759 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
9760 | cur_bytes = max(cur_bytes, min_size); | |
0b670dc4 JB |
9761 | /* |
9762 | * If we are severely fragmented we could end up with really | |
9763 | * small allocations, so if the allocator is returning small | |
9764 | * chunks lets make its job easier by only searching for those | |
9765 | * sized chunks. | |
9766 | */ | |
9767 | cur_bytes = min(cur_bytes, last_alloc); | |
00361589 | 9768 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
e570fd27 | 9769 | *alloc_hint, &ins, 1, 0); |
5a303d5d | 9770 | if (ret) { |
0af3d00b JB |
9771 | if (own_trans) |
9772 | btrfs_end_transaction(trans, root); | |
a22285a6 | 9773 | break; |
d899e052 | 9774 | } |
5a303d5d | 9775 | |
0b670dc4 | 9776 | last_alloc = ins.offset; |
d899e052 YZ |
9777 | ret = insert_reserved_file_extent(trans, inode, |
9778 | cur_offset, ins.objectid, | |
9779 | ins.offset, ins.offset, | |
920bbbfb | 9780 | ins.offset, 0, 0, 0, |
d899e052 | 9781 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 9782 | if (ret) { |
857cc2fc | 9783 | btrfs_free_reserved_extent(root, ins.objectid, |
e570fd27 | 9784 | ins.offset, 0); |
79787eaa JM |
9785 | btrfs_abort_transaction(trans, root, ret); |
9786 | if (own_trans) | |
9787 | btrfs_end_transaction(trans, root); | |
9788 | break; | |
9789 | } | |
31193213 | 9790 | |
a1ed835e CM |
9791 | btrfs_drop_extent_cache(inode, cur_offset, |
9792 | cur_offset + ins.offset -1, 0); | |
5a303d5d | 9793 | |
5dc562c5 JB |
9794 | em = alloc_extent_map(); |
9795 | if (!em) { | |
9796 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
9797 | &BTRFS_I(inode)->runtime_flags); | |
9798 | goto next; | |
9799 | } | |
9800 | ||
9801 | em->start = cur_offset; | |
9802 | em->orig_start = cur_offset; | |
9803 | em->len = ins.offset; | |
9804 | em->block_start = ins.objectid; | |
9805 | em->block_len = ins.offset; | |
b4939680 | 9806 | em->orig_block_len = ins.offset; |
cc95bef6 | 9807 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
9808 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
9809 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
9810 | em->generation = trans->transid; | |
9811 | ||
9812 | while (1) { | |
9813 | write_lock(&em_tree->lock); | |
09a2a8f9 | 9814 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
9815 | write_unlock(&em_tree->lock); |
9816 | if (ret != -EEXIST) | |
9817 | break; | |
9818 | btrfs_drop_extent_cache(inode, cur_offset, | |
9819 | cur_offset + ins.offset - 1, | |
9820 | 0); | |
9821 | } | |
9822 | free_extent_map(em); | |
9823 | next: | |
d899e052 YZ |
9824 | num_bytes -= ins.offset; |
9825 | cur_offset += ins.offset; | |
efa56464 | 9826 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 9827 | |
0c4d2d95 | 9828 | inode_inc_iversion(inode); |
d899e052 | 9829 | inode->i_ctime = CURRENT_TIME; |
6cbff00f | 9830 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 9831 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
9832 | (actual_len > inode->i_size) && |
9833 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 9834 | if (cur_offset > actual_len) |
55a61d1d | 9835 | i_size = actual_len; |
d1ea6a61 | 9836 | else |
55a61d1d JB |
9837 | i_size = cur_offset; |
9838 | i_size_write(inode, i_size); | |
9839 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
9840 | } |
9841 | ||
d899e052 | 9842 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
9843 | |
9844 | if (ret) { | |
9845 | btrfs_abort_transaction(trans, root, ret); | |
9846 | if (own_trans) | |
9847 | btrfs_end_transaction(trans, root); | |
9848 | break; | |
9849 | } | |
d899e052 | 9850 | |
0af3d00b JB |
9851 | if (own_trans) |
9852 | btrfs_end_transaction(trans, root); | |
5a303d5d | 9853 | } |
d899e052 YZ |
9854 | return ret; |
9855 | } | |
9856 | ||
0af3d00b JB |
9857 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
9858 | u64 start, u64 num_bytes, u64 min_size, | |
9859 | loff_t actual_len, u64 *alloc_hint) | |
9860 | { | |
9861 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9862 | min_size, actual_len, alloc_hint, | |
9863 | NULL); | |
9864 | } | |
9865 | ||
9866 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
9867 | struct btrfs_trans_handle *trans, int mode, | |
9868 | u64 start, u64 num_bytes, u64 min_size, | |
9869 | loff_t actual_len, u64 *alloc_hint) | |
9870 | { | |
9871 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
9872 | min_size, actual_len, alloc_hint, trans); | |
9873 | } | |
9874 | ||
e6dcd2dc CM |
9875 | static int btrfs_set_page_dirty(struct page *page) |
9876 | { | |
e6dcd2dc CM |
9877 | return __set_page_dirty_nobuffers(page); |
9878 | } | |
9879 | ||
10556cb2 | 9880 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 9881 | { |
b83cc969 | 9882 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 9883 | umode_t mode = inode->i_mode; |
b83cc969 | 9884 | |
cb6db4e5 JM |
9885 | if (mask & MAY_WRITE && |
9886 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
9887 | if (btrfs_root_readonly(root)) | |
9888 | return -EROFS; | |
9889 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
9890 | return -EACCES; | |
9891 | } | |
2830ba7f | 9892 | return generic_permission(inode, mask); |
fdebe2bd | 9893 | } |
39279cc3 | 9894 | |
ef3b9af5 FM |
9895 | static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
9896 | { | |
9897 | struct btrfs_trans_handle *trans; | |
9898 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
9899 | struct inode *inode = NULL; | |
9900 | u64 objectid; | |
9901 | u64 index; | |
9902 | int ret = 0; | |
9903 | ||
9904 | /* | |
9905 | * 5 units required for adding orphan entry | |
9906 | */ | |
9907 | trans = btrfs_start_transaction(root, 5); | |
9908 | if (IS_ERR(trans)) | |
9909 | return PTR_ERR(trans); | |
9910 | ||
9911 | ret = btrfs_find_free_ino(root, &objectid); | |
9912 | if (ret) | |
9913 | goto out; | |
9914 | ||
9915 | inode = btrfs_new_inode(trans, root, dir, NULL, 0, | |
9916 | btrfs_ino(dir), objectid, mode, &index); | |
9917 | if (IS_ERR(inode)) { | |
9918 | ret = PTR_ERR(inode); | |
9919 | inode = NULL; | |
9920 | goto out; | |
9921 | } | |
9922 | ||
ef3b9af5 FM |
9923 | inode->i_fop = &btrfs_file_operations; |
9924 | inode->i_op = &btrfs_file_inode_operations; | |
9925 | ||
9926 | inode->i_mapping->a_ops = &btrfs_aops; | |
ef3b9af5 FM |
9927 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
9928 | ||
b0d5d10f CM |
9929 | ret = btrfs_init_inode_security(trans, inode, dir, NULL); |
9930 | if (ret) | |
9931 | goto out_inode; | |
9932 | ||
9933 | ret = btrfs_update_inode(trans, root, inode); | |
9934 | if (ret) | |
9935 | goto out_inode; | |
ef3b9af5 FM |
9936 | ret = btrfs_orphan_add(trans, inode); |
9937 | if (ret) | |
b0d5d10f | 9938 | goto out_inode; |
ef3b9af5 | 9939 | |
5762b5c9 FM |
9940 | /* |
9941 | * We set number of links to 0 in btrfs_new_inode(), and here we set | |
9942 | * it to 1 because d_tmpfile() will issue a warning if the count is 0, | |
9943 | * through: | |
9944 | * | |
9945 | * d_tmpfile() -> inode_dec_link_count() -> drop_nlink() | |
9946 | */ | |
9947 | set_nlink(inode, 1); | |
b0d5d10f | 9948 | unlock_new_inode(inode); |
ef3b9af5 FM |
9949 | d_tmpfile(dentry, inode); |
9950 | mark_inode_dirty(inode); | |
9951 | ||
9952 | out: | |
9953 | btrfs_end_transaction(trans, root); | |
9954 | if (ret) | |
9955 | iput(inode); | |
9956 | btrfs_balance_delayed_items(root); | |
9957 | btrfs_btree_balance_dirty(root); | |
ef3b9af5 | 9958 | return ret; |
b0d5d10f CM |
9959 | |
9960 | out_inode: | |
9961 | unlock_new_inode(inode); | |
9962 | goto out; | |
9963 | ||
ef3b9af5 FM |
9964 | } |
9965 | ||
b38ef71c FM |
9966 | /* Inspired by filemap_check_errors() */ |
9967 | int btrfs_inode_check_errors(struct inode *inode) | |
9968 | { | |
9969 | int ret = 0; | |
9970 | ||
9971 | if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) && | |
9972 | test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags)) | |
9973 | ret = -ENOSPC; | |
9974 | if (test_bit(AS_EIO, &inode->i_mapping->flags) && | |
9975 | test_and_clear_bit(AS_EIO, &inode->i_mapping->flags)) | |
9976 | ret = -EIO; | |
9977 | ||
9978 | return ret; | |
9979 | } | |
9980 | ||
6e1d5dcc | 9981 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 9982 | .getattr = btrfs_getattr, |
39279cc3 CM |
9983 | .lookup = btrfs_lookup, |
9984 | .create = btrfs_create, | |
9985 | .unlink = btrfs_unlink, | |
9986 | .link = btrfs_link, | |
9987 | .mkdir = btrfs_mkdir, | |
9988 | .rmdir = btrfs_rmdir, | |
80ace85c | 9989 | .rename2 = btrfs_rename2, |
39279cc3 CM |
9990 | .symlink = btrfs_symlink, |
9991 | .setattr = btrfs_setattr, | |
618e21d5 | 9992 | .mknod = btrfs_mknod, |
95819c05 CH |
9993 | .setxattr = btrfs_setxattr, |
9994 | .getxattr = btrfs_getxattr, | |
5103e947 | 9995 | .listxattr = btrfs_listxattr, |
95819c05 | 9996 | .removexattr = btrfs_removexattr, |
fdebe2bd | 9997 | .permission = btrfs_permission, |
4e34e719 | 9998 | .get_acl = btrfs_get_acl, |
996a710d | 9999 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10000 | .update_time = btrfs_update_time, |
ef3b9af5 | 10001 | .tmpfile = btrfs_tmpfile, |
39279cc3 | 10002 | }; |
6e1d5dcc | 10003 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 10004 | .lookup = btrfs_lookup, |
fdebe2bd | 10005 | .permission = btrfs_permission, |
4e34e719 | 10006 | .get_acl = btrfs_get_acl, |
996a710d | 10007 | .set_acl = btrfs_set_acl, |
93fd63c2 | 10008 | .update_time = btrfs_update_time, |
39279cc3 | 10009 | }; |
76dda93c | 10010 | |
828c0950 | 10011 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
10012 | .llseek = generic_file_llseek, |
10013 | .read = generic_read_dir, | |
9cdda8d3 | 10014 | .iterate = btrfs_real_readdir, |
34287aa3 | 10015 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 10016 | #ifdef CONFIG_COMPAT |
34287aa3 | 10017 | .compat_ioctl = btrfs_ioctl, |
39279cc3 | 10018 | #endif |
6bf13c0c | 10019 | .release = btrfs_release_file, |
e02119d5 | 10020 | .fsync = btrfs_sync_file, |
39279cc3 CM |
10021 | }; |
10022 | ||
20e5506b | 10023 | static const struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 10024 | .fill_delalloc = run_delalloc_range, |
065631f6 | 10025 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 10026 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 10027 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 10028 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 10029 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
10030 | .set_bit_hook = btrfs_set_bit_hook, |
10031 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
10032 | .merge_extent_hook = btrfs_merge_extent_hook, |
10033 | .split_extent_hook = btrfs_split_extent_hook, | |
07157aac CM |
10034 | }; |
10035 | ||
35054394 CM |
10036 | /* |
10037 | * btrfs doesn't support the bmap operation because swapfiles | |
10038 | * use bmap to make a mapping of extents in the file. They assume | |
10039 | * these extents won't change over the life of the file and they | |
10040 | * use the bmap result to do IO directly to the drive. | |
10041 | * | |
10042 | * the btrfs bmap call would return logical addresses that aren't | |
10043 | * suitable for IO and they also will change frequently as COW | |
10044 | * operations happen. So, swapfile + btrfs == corruption. | |
10045 | * | |
10046 | * For now we're avoiding this by dropping bmap. | |
10047 | */ | |
7f09410b | 10048 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
10049 | .readpage = btrfs_readpage, |
10050 | .writepage = btrfs_writepage, | |
b293f02e | 10051 | .writepages = btrfs_writepages, |
3ab2fb5a | 10052 | .readpages = btrfs_readpages, |
16432985 | 10053 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
10054 | .invalidatepage = btrfs_invalidatepage, |
10055 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 10056 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 10057 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
10058 | }; |
10059 | ||
7f09410b | 10060 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
10061 | .readpage = btrfs_readpage, |
10062 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
10063 | .invalidatepage = btrfs_invalidatepage, |
10064 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
10065 | }; |
10066 | ||
6e1d5dcc | 10067 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
10068 | .getattr = btrfs_getattr, |
10069 | .setattr = btrfs_setattr, | |
95819c05 CH |
10070 | .setxattr = btrfs_setxattr, |
10071 | .getxattr = btrfs_getxattr, | |
5103e947 | 10072 | .listxattr = btrfs_listxattr, |
95819c05 | 10073 | .removexattr = btrfs_removexattr, |
fdebe2bd | 10074 | .permission = btrfs_permission, |
1506fcc8 | 10075 | .fiemap = btrfs_fiemap, |
4e34e719 | 10076 | .get_acl = btrfs_get_acl, |
996a710d | 10077 | .set_acl = btrfs_set_acl, |
e41f941a | 10078 | .update_time = btrfs_update_time, |
39279cc3 | 10079 | }; |
6e1d5dcc | 10080 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
10081 | .getattr = btrfs_getattr, |
10082 | .setattr = btrfs_setattr, | |
fdebe2bd | 10083 | .permission = btrfs_permission, |
95819c05 CH |
10084 | .setxattr = btrfs_setxattr, |
10085 | .getxattr = btrfs_getxattr, | |
33268eaf | 10086 | .listxattr = btrfs_listxattr, |
95819c05 | 10087 | .removexattr = btrfs_removexattr, |
4e34e719 | 10088 | .get_acl = btrfs_get_acl, |
996a710d | 10089 | .set_acl = btrfs_set_acl, |
e41f941a | 10090 | .update_time = btrfs_update_time, |
618e21d5 | 10091 | }; |
6e1d5dcc | 10092 | static const struct inode_operations btrfs_symlink_inode_operations = { |
39279cc3 CM |
10093 | .readlink = generic_readlink, |
10094 | .follow_link = page_follow_link_light, | |
10095 | .put_link = page_put_link, | |
f209561a | 10096 | .getattr = btrfs_getattr, |
22c44fe6 | 10097 | .setattr = btrfs_setattr, |
fdebe2bd | 10098 | .permission = btrfs_permission, |
0279b4cd JO |
10099 | .setxattr = btrfs_setxattr, |
10100 | .getxattr = btrfs_getxattr, | |
10101 | .listxattr = btrfs_listxattr, | |
10102 | .removexattr = btrfs_removexattr, | |
e41f941a | 10103 | .update_time = btrfs_update_time, |
39279cc3 | 10104 | }; |
76dda93c | 10105 | |
82d339d9 | 10106 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 10107 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 10108 | .d_release = btrfs_dentry_release, |
76dda93c | 10109 | }; |