Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
8f18cf13 | 19 | #include <linux/kernel.h> |
065631f6 | 20 | #include <linux/bio.h> |
39279cc3 | 21 | #include <linux/buffer_head.h> |
f2eb0a24 | 22 | #include <linux/file.h> |
39279cc3 CM |
23 | #include <linux/fs.h> |
24 | #include <linux/pagemap.h> | |
25 | #include <linux/highmem.h> | |
26 | #include <linux/time.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/string.h> | |
39279cc3 CM |
29 | #include <linux/backing-dev.h> |
30 | #include <linux/mpage.h> | |
31 | #include <linux/swap.h> | |
32 | #include <linux/writeback.h> | |
33 | #include <linux/statfs.h> | |
34 | #include <linux/compat.h> | |
a27bb332 | 35 | #include <linux/aio.h> |
9ebefb18 | 36 | #include <linux/bit_spinlock.h> |
5103e947 | 37 | #include <linux/xattr.h> |
33268eaf | 38 | #include <linux/posix_acl.h> |
d899e052 | 39 | #include <linux/falloc.h> |
5a0e3ad6 | 40 | #include <linux/slab.h> |
7a36ddec | 41 | #include <linux/ratelimit.h> |
22c44fe6 | 42 | #include <linux/mount.h> |
55e301fd | 43 | #include <linux/btrfs.h> |
53b381b3 | 44 | #include <linux/blkdev.h> |
f23b5a59 | 45 | #include <linux/posix_acl_xattr.h> |
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" |
39279cc3 CM |
61 | |
62 | struct btrfs_iget_args { | |
63 | u64 ino; | |
64 | struct btrfs_root *root; | |
65 | }; | |
66 | ||
6e1d5dcc AD |
67 | static const struct inode_operations btrfs_dir_inode_operations; |
68 | static const struct inode_operations btrfs_symlink_inode_operations; | |
69 | static const struct inode_operations btrfs_dir_ro_inode_operations; | |
70 | static const struct inode_operations btrfs_special_inode_operations; | |
71 | static const struct inode_operations btrfs_file_inode_operations; | |
7f09410b AD |
72 | static const struct address_space_operations btrfs_aops; |
73 | static const struct address_space_operations btrfs_symlink_aops; | |
828c0950 | 74 | static const struct file_operations btrfs_dir_file_operations; |
d1310b2e | 75 | static struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
76 | |
77 | static struct kmem_cache *btrfs_inode_cachep; | |
8ccf6f19 | 78 | static struct kmem_cache *btrfs_delalloc_work_cachep; |
39279cc3 CM |
79 | struct kmem_cache *btrfs_trans_handle_cachep; |
80 | struct kmem_cache *btrfs_transaction_cachep; | |
39279cc3 | 81 | struct kmem_cache *btrfs_path_cachep; |
dc89e982 | 82 | struct kmem_cache *btrfs_free_space_cachep; |
39279cc3 CM |
83 | |
84 | #define S_SHIFT 12 | |
85 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { | |
86 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, | |
87 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
88 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
89 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
90 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
91 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
92 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
93 | }; | |
94 | ||
3972f260 | 95 | static int btrfs_setsize(struct inode *inode, struct iattr *attr); |
a41ad394 | 96 | static int btrfs_truncate(struct inode *inode); |
5fd02043 | 97 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); |
771ed689 CM |
98 | static noinline int cow_file_range(struct inode *inode, |
99 | struct page *locked_page, | |
100 | u64 start, u64 end, int *page_started, | |
101 | unsigned long *nr_written, int unlock); | |
70c8a91c JB |
102 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
103 | u64 len, u64 orig_start, | |
104 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
105 | u64 orig_block_len, u64 ram_bytes, |
106 | int type); | |
7b128766 | 107 | |
48a3b636 | 108 | static int btrfs_dirty_inode(struct inode *inode); |
7b128766 | 109 | |
f34f57a3 | 110 | static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, |
2a7dba39 EP |
111 | struct inode *inode, struct inode *dir, |
112 | const struct qstr *qstr) | |
0279b4cd JO |
113 | { |
114 | int err; | |
115 | ||
f34f57a3 | 116 | err = btrfs_init_acl(trans, inode, dir); |
0279b4cd | 117 | if (!err) |
2a7dba39 | 118 | err = btrfs_xattr_security_init(trans, inode, dir, qstr); |
0279b4cd JO |
119 | return err; |
120 | } | |
121 | ||
c8b97818 CM |
122 | /* |
123 | * this does all the hard work for inserting an inline extent into | |
124 | * the btree. The caller should have done a btrfs_drop_extents so that | |
125 | * no overlapping inline items exist in the btree | |
126 | */ | |
d397712b | 127 | static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, |
1acae57b | 128 | struct btrfs_path *path, int extent_inserted, |
c8b97818 CM |
129 | struct btrfs_root *root, struct inode *inode, |
130 | u64 start, size_t size, size_t compressed_size, | |
fe3f566c | 131 | int compress_type, |
c8b97818 CM |
132 | struct page **compressed_pages) |
133 | { | |
c8b97818 CM |
134 | struct extent_buffer *leaf; |
135 | struct page *page = NULL; | |
136 | char *kaddr; | |
137 | unsigned long ptr; | |
138 | struct btrfs_file_extent_item *ei; | |
139 | int err = 0; | |
140 | int ret; | |
141 | size_t cur_size = size; | |
c8b97818 | 142 | unsigned long offset; |
c8b97818 | 143 | |
fe3f566c | 144 | if (compressed_size && compressed_pages) |
c8b97818 | 145 | cur_size = compressed_size; |
c8b97818 | 146 | |
1acae57b | 147 | inode_add_bytes(inode, size); |
c8b97818 | 148 | |
1acae57b FDBM |
149 | if (!extent_inserted) { |
150 | struct btrfs_key key; | |
151 | size_t datasize; | |
c8b97818 | 152 | |
1acae57b FDBM |
153 | key.objectid = btrfs_ino(inode); |
154 | key.offset = start; | |
155 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); | |
c8b97818 | 156 | |
1acae57b FDBM |
157 | datasize = btrfs_file_extent_calc_inline_size(cur_size); |
158 | path->leave_spinning = 1; | |
159 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
160 | datasize); | |
161 | if (ret) { | |
162 | err = ret; | |
163 | goto fail; | |
164 | } | |
c8b97818 CM |
165 | } |
166 | leaf = path->nodes[0]; | |
167 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
168 | struct btrfs_file_extent_item); | |
169 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
170 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
171 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
172 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
173 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
174 | ptr = btrfs_file_extent_inline_start(ei); | |
175 | ||
261507a0 | 176 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 CM |
177 | struct page *cpage; |
178 | int i = 0; | |
d397712b | 179 | while (compressed_size > 0) { |
c8b97818 | 180 | cpage = compressed_pages[i]; |
5b050f04 | 181 | cur_size = min_t(unsigned long, compressed_size, |
c8b97818 CM |
182 | PAGE_CACHE_SIZE); |
183 | ||
7ac687d9 | 184 | kaddr = kmap_atomic(cpage); |
c8b97818 | 185 | write_extent_buffer(leaf, kaddr, ptr, cur_size); |
7ac687d9 | 186 | kunmap_atomic(kaddr); |
c8b97818 CM |
187 | |
188 | i++; | |
189 | ptr += cur_size; | |
190 | compressed_size -= cur_size; | |
191 | } | |
192 | btrfs_set_file_extent_compression(leaf, ei, | |
261507a0 | 193 | compress_type); |
c8b97818 CM |
194 | } else { |
195 | page = find_get_page(inode->i_mapping, | |
196 | start >> PAGE_CACHE_SHIFT); | |
197 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
7ac687d9 | 198 | kaddr = kmap_atomic(page); |
c8b97818 CM |
199 | offset = start & (PAGE_CACHE_SIZE - 1); |
200 | write_extent_buffer(leaf, kaddr + offset, ptr, size); | |
7ac687d9 | 201 | kunmap_atomic(kaddr); |
c8b97818 CM |
202 | page_cache_release(page); |
203 | } | |
204 | btrfs_mark_buffer_dirty(leaf); | |
1acae57b | 205 | btrfs_release_path(path); |
c8b97818 | 206 | |
c2167754 YZ |
207 | /* |
208 | * we're an inline extent, so nobody can | |
209 | * extend the file past i_size without locking | |
210 | * a page we already have locked. | |
211 | * | |
212 | * We must do any isize and inode updates | |
213 | * before we unlock the pages. Otherwise we | |
214 | * could end up racing with unlink. | |
215 | */ | |
c8b97818 | 216 | BTRFS_I(inode)->disk_i_size = inode->i_size; |
79787eaa | 217 | ret = btrfs_update_inode(trans, root, inode); |
c2167754 | 218 | |
79787eaa | 219 | return ret; |
c8b97818 | 220 | fail: |
c8b97818 CM |
221 | return err; |
222 | } | |
223 | ||
224 | ||
225 | /* | |
226 | * conditionally insert an inline extent into the file. This | |
227 | * does the checks required to make sure the data is small enough | |
228 | * to fit as an inline extent. | |
229 | */ | |
00361589 JB |
230 | static noinline int cow_file_range_inline(struct btrfs_root *root, |
231 | struct inode *inode, u64 start, | |
232 | u64 end, size_t compressed_size, | |
233 | int compress_type, | |
234 | struct page **compressed_pages) | |
c8b97818 | 235 | { |
00361589 | 236 | struct btrfs_trans_handle *trans; |
c8b97818 CM |
237 | u64 isize = i_size_read(inode); |
238 | u64 actual_end = min(end + 1, isize); | |
239 | u64 inline_len = actual_end - start; | |
fda2832f | 240 | u64 aligned_end = ALIGN(end, root->sectorsize); |
c8b97818 CM |
241 | u64 data_len = inline_len; |
242 | int ret; | |
1acae57b FDBM |
243 | struct btrfs_path *path; |
244 | int extent_inserted = 0; | |
245 | u32 extent_item_size; | |
c8b97818 CM |
246 | |
247 | if (compressed_size) | |
248 | data_len = compressed_size; | |
249 | ||
250 | if (start > 0 || | |
70b99e69 | 251 | actual_end >= PAGE_CACHE_SIZE || |
c8b97818 CM |
252 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
253 | (!compressed_size && | |
254 | (actual_end & (root->sectorsize - 1)) == 0) || | |
255 | end + 1 < isize || | |
256 | data_len > root->fs_info->max_inline) { | |
257 | return 1; | |
258 | } | |
259 | ||
1acae57b FDBM |
260 | path = btrfs_alloc_path(); |
261 | if (!path) | |
262 | return -ENOMEM; | |
263 | ||
00361589 | 264 | trans = btrfs_join_transaction(root); |
1acae57b FDBM |
265 | if (IS_ERR(trans)) { |
266 | btrfs_free_path(path); | |
00361589 | 267 | return PTR_ERR(trans); |
1acae57b | 268 | } |
00361589 JB |
269 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
270 | ||
1acae57b FDBM |
271 | if (compressed_size && compressed_pages) |
272 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
273 | compressed_size); | |
274 | else | |
275 | extent_item_size = btrfs_file_extent_calc_inline_size( | |
276 | inline_len); | |
277 | ||
278 | ret = __btrfs_drop_extents(trans, root, inode, path, | |
279 | start, aligned_end, NULL, | |
280 | 1, 1, extent_item_size, &extent_inserted); | |
00361589 JB |
281 | if (ret) { |
282 | btrfs_abort_transaction(trans, root, ret); | |
283 | goto out; | |
284 | } | |
c8b97818 CM |
285 | |
286 | if (isize > actual_end) | |
287 | inline_len = min_t(u64, isize, actual_end); | |
1acae57b FDBM |
288 | ret = insert_inline_extent(trans, path, extent_inserted, |
289 | root, inode, start, | |
c8b97818 | 290 | inline_len, compressed_size, |
fe3f566c | 291 | compress_type, compressed_pages); |
2adcac1a | 292 | if (ret && ret != -ENOSPC) { |
79787eaa | 293 | btrfs_abort_transaction(trans, root, ret); |
00361589 | 294 | goto out; |
2adcac1a | 295 | } else if (ret == -ENOSPC) { |
00361589 JB |
296 | ret = 1; |
297 | goto out; | |
79787eaa | 298 | } |
2adcac1a | 299 | |
bdc20e67 | 300 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); |
0ca1f7ce | 301 | btrfs_delalloc_release_metadata(inode, end + 1 - start); |
a1ed835e | 302 | btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); |
00361589 | 303 | out: |
1acae57b | 304 | btrfs_free_path(path); |
00361589 JB |
305 | btrfs_end_transaction(trans, root); |
306 | return ret; | |
c8b97818 CM |
307 | } |
308 | ||
771ed689 CM |
309 | struct async_extent { |
310 | u64 start; | |
311 | u64 ram_size; | |
312 | u64 compressed_size; | |
313 | struct page **pages; | |
314 | unsigned long nr_pages; | |
261507a0 | 315 | int compress_type; |
771ed689 CM |
316 | struct list_head list; |
317 | }; | |
318 | ||
319 | struct async_cow { | |
320 | struct inode *inode; | |
321 | struct btrfs_root *root; | |
322 | struct page *locked_page; | |
323 | u64 start; | |
324 | u64 end; | |
325 | struct list_head extents; | |
326 | struct btrfs_work work; | |
327 | }; | |
328 | ||
329 | static noinline int add_async_extent(struct async_cow *cow, | |
330 | u64 start, u64 ram_size, | |
331 | u64 compressed_size, | |
332 | struct page **pages, | |
261507a0 LZ |
333 | unsigned long nr_pages, |
334 | int compress_type) | |
771ed689 CM |
335 | { |
336 | struct async_extent *async_extent; | |
337 | ||
338 | async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); | |
79787eaa | 339 | BUG_ON(!async_extent); /* -ENOMEM */ |
771ed689 CM |
340 | async_extent->start = start; |
341 | async_extent->ram_size = ram_size; | |
342 | async_extent->compressed_size = compressed_size; | |
343 | async_extent->pages = pages; | |
344 | async_extent->nr_pages = nr_pages; | |
261507a0 | 345 | async_extent->compress_type = compress_type; |
771ed689 CM |
346 | list_add_tail(&async_extent->list, &cow->extents); |
347 | return 0; | |
348 | } | |
349 | ||
d352ac68 | 350 | /* |
771ed689 CM |
351 | * we create compressed extents in two phases. The first |
352 | * phase compresses a range of pages that have already been | |
353 | * locked (both pages and state bits are locked). | |
c8b97818 | 354 | * |
771ed689 CM |
355 | * This is done inside an ordered work queue, and the compression |
356 | * is spread across many cpus. The actual IO submission is step | |
357 | * two, and the ordered work queue takes care of making sure that | |
358 | * happens in the same order things were put onto the queue by | |
359 | * writepages and friends. | |
c8b97818 | 360 | * |
771ed689 CM |
361 | * If this code finds it can't get good compression, it puts an |
362 | * entry onto the work queue to write the uncompressed bytes. This | |
363 | * makes sure that both compressed inodes and uncompressed inodes | |
b2570314 AB |
364 | * are written in the same order that the flusher thread sent them |
365 | * down. | |
d352ac68 | 366 | */ |
771ed689 CM |
367 | static noinline int compress_file_range(struct inode *inode, |
368 | struct page *locked_page, | |
369 | u64 start, u64 end, | |
370 | struct async_cow *async_cow, | |
371 | int *num_added) | |
b888db2b CM |
372 | { |
373 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
db94535d | 374 | u64 num_bytes; |
db94535d | 375 | u64 blocksize = root->sectorsize; |
c8b97818 | 376 | u64 actual_end; |
42dc7bab | 377 | u64 isize = i_size_read(inode); |
e6dcd2dc | 378 | int ret = 0; |
c8b97818 CM |
379 | struct page **pages = NULL; |
380 | unsigned long nr_pages; | |
381 | unsigned long nr_pages_ret = 0; | |
382 | unsigned long total_compressed = 0; | |
383 | unsigned long total_in = 0; | |
384 | unsigned long max_compressed = 128 * 1024; | |
771ed689 | 385 | unsigned long max_uncompressed = 128 * 1024; |
c8b97818 CM |
386 | int i; |
387 | int will_compress; | |
261507a0 | 388 | int compress_type = root->fs_info->compress_type; |
4adaa611 | 389 | int redirty = 0; |
b888db2b | 390 | |
4cb13e5d LB |
391 | /* if this is a small write inside eof, kick off a defrag */ |
392 | if ((end - start + 1) < 16 * 1024 && | |
393 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
4cb5300b CM |
394 | btrfs_add_inode_defrag(NULL, inode); |
395 | ||
42dc7bab | 396 | actual_end = min_t(u64, isize, end + 1); |
c8b97818 CM |
397 | again: |
398 | will_compress = 0; | |
399 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; | |
400 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); | |
be20aa9d | 401 | |
f03d9301 CM |
402 | /* |
403 | * we don't want to send crud past the end of i_size through | |
404 | * compression, that's just a waste of CPU time. So, if the | |
405 | * end of the file is before the start of our current | |
406 | * requested range of bytes, we bail out to the uncompressed | |
407 | * cleanup code that can deal with all of this. | |
408 | * | |
409 | * It isn't really the fastest way to fix things, but this is a | |
410 | * very uncommon corner. | |
411 | */ | |
412 | if (actual_end <= start) | |
413 | goto cleanup_and_bail_uncompressed; | |
414 | ||
c8b97818 CM |
415 | total_compressed = actual_end - start; |
416 | ||
417 | /* we want to make sure that amount of ram required to uncompress | |
418 | * an extent is reasonable, so we limit the total size in ram | |
771ed689 CM |
419 | * of a compressed extent to 128k. This is a crucial number |
420 | * because it also controls how easily we can spread reads across | |
421 | * cpus for decompression. | |
422 | * | |
423 | * We also want to make sure the amount of IO required to do | |
424 | * a random read is reasonably small, so we limit the size of | |
425 | * a compressed extent to 128k. | |
c8b97818 CM |
426 | */ |
427 | total_compressed = min(total_compressed, max_uncompressed); | |
fda2832f | 428 | num_bytes = ALIGN(end - start + 1, blocksize); |
be20aa9d | 429 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
430 | total_in = 0; |
431 | ret = 0; | |
db94535d | 432 | |
771ed689 CM |
433 | /* |
434 | * we do compression for mount -o compress and when the | |
435 | * inode has not been flagged as nocompress. This flag can | |
436 | * change at any time if we discover bad compression ratios. | |
c8b97818 | 437 | */ |
6cbff00f | 438 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && |
1e701a32 | 439 | (btrfs_test_opt(root, COMPRESS) || |
75e7cb7f LB |
440 | (BTRFS_I(inode)->force_compress) || |
441 | (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { | |
c8b97818 | 442 | WARN_ON(pages); |
cfbc246e | 443 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
560f7d75 LZ |
444 | if (!pages) { |
445 | /* just bail out to the uncompressed code */ | |
446 | goto cont; | |
447 | } | |
c8b97818 | 448 | |
261507a0 LZ |
449 | if (BTRFS_I(inode)->force_compress) |
450 | compress_type = BTRFS_I(inode)->force_compress; | |
451 | ||
4adaa611 CM |
452 | /* |
453 | * we need to call clear_page_dirty_for_io on each | |
454 | * page in the range. Otherwise applications with the file | |
455 | * mmap'd can wander in and change the page contents while | |
456 | * we are compressing them. | |
457 | * | |
458 | * If the compression fails for any reason, we set the pages | |
459 | * dirty again later on. | |
460 | */ | |
461 | extent_range_clear_dirty_for_io(inode, start, end); | |
462 | redirty = 1; | |
261507a0 LZ |
463 | ret = btrfs_compress_pages(compress_type, |
464 | inode->i_mapping, start, | |
465 | total_compressed, pages, | |
466 | nr_pages, &nr_pages_ret, | |
467 | &total_in, | |
468 | &total_compressed, | |
469 | max_compressed); | |
c8b97818 CM |
470 | |
471 | if (!ret) { | |
472 | unsigned long offset = total_compressed & | |
473 | (PAGE_CACHE_SIZE - 1); | |
474 | struct page *page = pages[nr_pages_ret - 1]; | |
475 | char *kaddr; | |
476 | ||
477 | /* zero the tail end of the last page, we might be | |
478 | * sending it down to disk | |
479 | */ | |
480 | if (offset) { | |
7ac687d9 | 481 | kaddr = kmap_atomic(page); |
c8b97818 CM |
482 | memset(kaddr + offset, 0, |
483 | PAGE_CACHE_SIZE - offset); | |
7ac687d9 | 484 | kunmap_atomic(kaddr); |
c8b97818 CM |
485 | } |
486 | will_compress = 1; | |
487 | } | |
488 | } | |
560f7d75 | 489 | cont: |
c8b97818 CM |
490 | if (start == 0) { |
491 | /* lets try to make an inline extent */ | |
771ed689 | 492 | if (ret || total_in < (actual_end - start)) { |
c8b97818 | 493 | /* we didn't compress the entire range, try |
771ed689 | 494 | * to make an uncompressed inline extent. |
c8b97818 | 495 | */ |
00361589 JB |
496 | ret = cow_file_range_inline(root, inode, start, end, |
497 | 0, 0, NULL); | |
c8b97818 | 498 | } else { |
771ed689 | 499 | /* try making a compressed inline extent */ |
00361589 | 500 | ret = cow_file_range_inline(root, inode, start, end, |
fe3f566c LZ |
501 | total_compressed, |
502 | compress_type, pages); | |
c8b97818 | 503 | } |
79787eaa | 504 | if (ret <= 0) { |
151a41bc JB |
505 | unsigned long clear_flags = EXTENT_DELALLOC | |
506 | EXTENT_DEFRAG; | |
507 | clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0; | |
508 | ||
771ed689 | 509 | /* |
79787eaa JM |
510 | * inline extent creation worked or returned error, |
511 | * we don't need to create any more async work items. | |
512 | * Unlock and free up our temp pages. | |
771ed689 | 513 | */ |
c2790a2e | 514 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
151a41bc | 515 | clear_flags, PAGE_UNLOCK | |
c2790a2e JB |
516 | PAGE_CLEAR_DIRTY | |
517 | PAGE_SET_WRITEBACK | | |
518 | PAGE_END_WRITEBACK); | |
c8b97818 CM |
519 | goto free_pages_out; |
520 | } | |
521 | } | |
522 | ||
523 | if (will_compress) { | |
524 | /* | |
525 | * we aren't doing an inline extent round the compressed size | |
526 | * up to a block size boundary so the allocator does sane | |
527 | * things | |
528 | */ | |
fda2832f | 529 | total_compressed = ALIGN(total_compressed, blocksize); |
c8b97818 CM |
530 | |
531 | /* | |
532 | * one last check to make sure the compression is really a | |
533 | * win, compare the page count read with the blocks on disk | |
534 | */ | |
fda2832f | 535 | total_in = ALIGN(total_in, PAGE_CACHE_SIZE); |
c8b97818 CM |
536 | if (total_compressed >= total_in) { |
537 | will_compress = 0; | |
538 | } else { | |
c8b97818 CM |
539 | num_bytes = total_in; |
540 | } | |
541 | } | |
542 | if (!will_compress && pages) { | |
543 | /* | |
544 | * the compression code ran but failed to make things smaller, | |
545 | * free any pages it allocated and our page pointer array | |
546 | */ | |
547 | for (i = 0; i < nr_pages_ret; i++) { | |
70b99e69 | 548 | WARN_ON(pages[i]->mapping); |
c8b97818 CM |
549 | page_cache_release(pages[i]); |
550 | } | |
551 | kfree(pages); | |
552 | pages = NULL; | |
553 | total_compressed = 0; | |
554 | nr_pages_ret = 0; | |
555 | ||
556 | /* flag the file so we don't compress in the future */ | |
1e701a32 CM |
557 | if (!btrfs_test_opt(root, FORCE_COMPRESS) && |
558 | !(BTRFS_I(inode)->force_compress)) { | |
a555f810 | 559 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
1e701a32 | 560 | } |
c8b97818 | 561 | } |
771ed689 CM |
562 | if (will_compress) { |
563 | *num_added += 1; | |
c8b97818 | 564 | |
771ed689 CM |
565 | /* the async work queues will take care of doing actual |
566 | * allocation on disk for these compressed pages, | |
567 | * and will submit them to the elevator. | |
568 | */ | |
569 | add_async_extent(async_cow, start, num_bytes, | |
261507a0 LZ |
570 | total_compressed, pages, nr_pages_ret, |
571 | compress_type); | |
179e29e4 | 572 | |
24ae6365 | 573 | if (start + num_bytes < end) { |
771ed689 CM |
574 | start += num_bytes; |
575 | pages = NULL; | |
576 | cond_resched(); | |
577 | goto again; | |
578 | } | |
579 | } else { | |
f03d9301 | 580 | cleanup_and_bail_uncompressed: |
771ed689 CM |
581 | /* |
582 | * No compression, but we still need to write the pages in | |
583 | * the file we've been given so far. redirty the locked | |
584 | * page if it corresponds to our extent and set things up | |
585 | * for the async work queue to run cow_file_range to do | |
586 | * the normal delalloc dance | |
587 | */ | |
588 | if (page_offset(locked_page) >= start && | |
589 | page_offset(locked_page) <= end) { | |
590 | __set_page_dirty_nobuffers(locked_page); | |
591 | /* unlocked later on in the async handlers */ | |
592 | } | |
4adaa611 CM |
593 | if (redirty) |
594 | extent_range_redirty_for_io(inode, start, end); | |
261507a0 LZ |
595 | add_async_extent(async_cow, start, end - start + 1, |
596 | 0, NULL, 0, BTRFS_COMPRESS_NONE); | |
771ed689 CM |
597 | *num_added += 1; |
598 | } | |
3b951516 | 599 | |
771ed689 | 600 | out: |
79787eaa | 601 | return ret; |
771ed689 CM |
602 | |
603 | free_pages_out: | |
604 | for (i = 0; i < nr_pages_ret; i++) { | |
605 | WARN_ON(pages[i]->mapping); | |
606 | page_cache_release(pages[i]); | |
607 | } | |
d397712b | 608 | kfree(pages); |
771ed689 CM |
609 | |
610 | goto out; | |
611 | } | |
612 | ||
613 | /* | |
614 | * phase two of compressed writeback. This is the ordered portion | |
615 | * of the code, which only gets called in the order the work was | |
616 | * queued. We walk all the async extents created by compress_file_range | |
617 | * and send them down to the disk. | |
618 | */ | |
619 | static noinline int submit_compressed_extents(struct inode *inode, | |
620 | struct async_cow *async_cow) | |
621 | { | |
622 | struct async_extent *async_extent; | |
623 | u64 alloc_hint = 0; | |
771ed689 CM |
624 | struct btrfs_key ins; |
625 | struct extent_map *em; | |
626 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
627 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
628 | struct extent_io_tree *io_tree; | |
f5a84ee3 | 629 | int ret = 0; |
771ed689 CM |
630 | |
631 | if (list_empty(&async_cow->extents)) | |
632 | return 0; | |
633 | ||
3e04e7f1 | 634 | again: |
d397712b | 635 | while (!list_empty(&async_cow->extents)) { |
771ed689 CM |
636 | async_extent = list_entry(async_cow->extents.next, |
637 | struct async_extent, list); | |
638 | list_del(&async_extent->list); | |
c8b97818 | 639 | |
771ed689 CM |
640 | io_tree = &BTRFS_I(inode)->io_tree; |
641 | ||
f5a84ee3 | 642 | retry: |
771ed689 CM |
643 | /* did the compression code fall back to uncompressed IO? */ |
644 | if (!async_extent->pages) { | |
645 | int page_started = 0; | |
646 | unsigned long nr_written = 0; | |
647 | ||
648 | lock_extent(io_tree, async_extent->start, | |
2ac55d41 | 649 | async_extent->start + |
d0082371 | 650 | async_extent->ram_size - 1); |
771ed689 CM |
651 | |
652 | /* allocate blocks */ | |
f5a84ee3 JB |
653 | ret = cow_file_range(inode, async_cow->locked_page, |
654 | async_extent->start, | |
655 | async_extent->start + | |
656 | async_extent->ram_size - 1, | |
657 | &page_started, &nr_written, 0); | |
771ed689 | 658 | |
79787eaa JM |
659 | /* JDM XXX */ |
660 | ||
771ed689 CM |
661 | /* |
662 | * if page_started, cow_file_range inserted an | |
663 | * inline extent and took care of all the unlocking | |
664 | * and IO for us. Otherwise, we need to submit | |
665 | * all those pages down to the drive. | |
666 | */ | |
f5a84ee3 | 667 | if (!page_started && !ret) |
771ed689 CM |
668 | extent_write_locked_range(io_tree, |
669 | inode, async_extent->start, | |
d397712b | 670 | async_extent->start + |
771ed689 CM |
671 | async_extent->ram_size - 1, |
672 | btrfs_get_extent, | |
673 | WB_SYNC_ALL); | |
3e04e7f1 JB |
674 | else if (ret) |
675 | unlock_page(async_cow->locked_page); | |
771ed689 CM |
676 | kfree(async_extent); |
677 | cond_resched(); | |
678 | continue; | |
679 | } | |
680 | ||
681 | lock_extent(io_tree, async_extent->start, | |
d0082371 | 682 | async_extent->start + async_extent->ram_size - 1); |
771ed689 | 683 | |
00361589 | 684 | ret = btrfs_reserve_extent(root, |
771ed689 CM |
685 | async_extent->compressed_size, |
686 | async_extent->compressed_size, | |
81c9ad23 | 687 | 0, alloc_hint, &ins, 1); |
f5a84ee3 JB |
688 | if (ret) { |
689 | int i; | |
3e04e7f1 | 690 | |
f5a84ee3 JB |
691 | for (i = 0; i < async_extent->nr_pages; i++) { |
692 | WARN_ON(async_extent->pages[i]->mapping); | |
693 | page_cache_release(async_extent->pages[i]); | |
694 | } | |
695 | kfree(async_extent->pages); | |
696 | async_extent->nr_pages = 0; | |
697 | async_extent->pages = NULL; | |
3e04e7f1 | 698 | |
fdf8e2ea JB |
699 | if (ret == -ENOSPC) { |
700 | unlock_extent(io_tree, async_extent->start, | |
701 | async_extent->start + | |
702 | async_extent->ram_size - 1); | |
79787eaa | 703 | goto retry; |
fdf8e2ea | 704 | } |
3e04e7f1 | 705 | goto out_free; |
f5a84ee3 JB |
706 | } |
707 | ||
c2167754 YZ |
708 | /* |
709 | * here we're doing allocation and writeback of the | |
710 | * compressed pages | |
711 | */ | |
712 | btrfs_drop_extent_cache(inode, async_extent->start, | |
713 | async_extent->start + | |
714 | async_extent->ram_size - 1, 0); | |
715 | ||
172ddd60 | 716 | em = alloc_extent_map(); |
b9aa55be LB |
717 | if (!em) { |
718 | ret = -ENOMEM; | |
3e04e7f1 | 719 | goto out_free_reserve; |
b9aa55be | 720 | } |
771ed689 CM |
721 | em->start = async_extent->start; |
722 | em->len = async_extent->ram_size; | |
445a6944 | 723 | em->orig_start = em->start; |
2ab28f32 JB |
724 | em->mod_start = em->start; |
725 | em->mod_len = em->len; | |
c8b97818 | 726 | |
771ed689 CM |
727 | em->block_start = ins.objectid; |
728 | em->block_len = ins.offset; | |
b4939680 | 729 | em->orig_block_len = ins.offset; |
cc95bef6 | 730 | em->ram_bytes = async_extent->ram_size; |
771ed689 | 731 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
261507a0 | 732 | em->compress_type = async_extent->compress_type; |
771ed689 CM |
733 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
734 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
70c8a91c | 735 | em->generation = -1; |
771ed689 | 736 | |
d397712b | 737 | while (1) { |
890871be | 738 | write_lock(&em_tree->lock); |
09a2a8f9 | 739 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 740 | write_unlock(&em_tree->lock); |
771ed689 CM |
741 | if (ret != -EEXIST) { |
742 | free_extent_map(em); | |
743 | break; | |
744 | } | |
745 | btrfs_drop_extent_cache(inode, async_extent->start, | |
746 | async_extent->start + | |
747 | async_extent->ram_size - 1, 0); | |
748 | } | |
749 | ||
3e04e7f1 JB |
750 | if (ret) |
751 | goto out_free_reserve; | |
752 | ||
261507a0 LZ |
753 | ret = btrfs_add_ordered_extent_compress(inode, |
754 | async_extent->start, | |
755 | ins.objectid, | |
756 | async_extent->ram_size, | |
757 | ins.offset, | |
758 | BTRFS_ORDERED_COMPRESSED, | |
759 | async_extent->compress_type); | |
3e04e7f1 JB |
760 | if (ret) |
761 | goto out_free_reserve; | |
771ed689 | 762 | |
771ed689 CM |
763 | /* |
764 | * clear dirty, set writeback and unlock the pages. | |
765 | */ | |
c2790a2e | 766 | extent_clear_unlock_delalloc(inode, async_extent->start, |
a791e35e CM |
767 | async_extent->start + |
768 | async_extent->ram_size - 1, | |
151a41bc JB |
769 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC, |
770 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
c2790a2e | 771 | PAGE_SET_WRITEBACK); |
771ed689 | 772 | ret = btrfs_submit_compressed_write(inode, |
d397712b CM |
773 | async_extent->start, |
774 | async_extent->ram_size, | |
775 | ins.objectid, | |
776 | ins.offset, async_extent->pages, | |
777 | async_extent->nr_pages); | |
771ed689 CM |
778 | alloc_hint = ins.objectid + ins.offset; |
779 | kfree(async_extent); | |
3e04e7f1 JB |
780 | if (ret) |
781 | goto out; | |
771ed689 CM |
782 | cond_resched(); |
783 | } | |
79787eaa JM |
784 | ret = 0; |
785 | out: | |
786 | return ret; | |
3e04e7f1 JB |
787 | out_free_reserve: |
788 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); | |
79787eaa | 789 | out_free: |
c2790a2e | 790 | extent_clear_unlock_delalloc(inode, async_extent->start, |
3e04e7f1 JB |
791 | async_extent->start + |
792 | async_extent->ram_size - 1, | |
c2790a2e | 793 | NULL, EXTENT_LOCKED | EXTENT_DELALLOC | |
151a41bc JB |
794 | EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING, |
795 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
796 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); | |
79787eaa | 797 | kfree(async_extent); |
3e04e7f1 | 798 | goto again; |
771ed689 CM |
799 | } |
800 | ||
4b46fce2 JB |
801 | static u64 get_extent_allocation_hint(struct inode *inode, u64 start, |
802 | u64 num_bytes) | |
803 | { | |
804 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
805 | struct extent_map *em; | |
806 | u64 alloc_hint = 0; | |
807 | ||
808 | read_lock(&em_tree->lock); | |
809 | em = search_extent_mapping(em_tree, start, num_bytes); | |
810 | if (em) { | |
811 | /* | |
812 | * if block start isn't an actual block number then find the | |
813 | * first block in this inode and use that as a hint. If that | |
814 | * block is also bogus then just don't worry about it. | |
815 | */ | |
816 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | |
817 | free_extent_map(em); | |
818 | em = search_extent_mapping(em_tree, 0, 0); | |
819 | if (em && em->block_start < EXTENT_MAP_LAST_BYTE) | |
820 | alloc_hint = em->block_start; | |
821 | if (em) | |
822 | free_extent_map(em); | |
823 | } else { | |
824 | alloc_hint = em->block_start; | |
825 | free_extent_map(em); | |
826 | } | |
827 | } | |
828 | read_unlock(&em_tree->lock); | |
829 | ||
830 | return alloc_hint; | |
831 | } | |
832 | ||
771ed689 CM |
833 | /* |
834 | * when extent_io.c finds a delayed allocation range in the file, | |
835 | * the call backs end up in this code. The basic idea is to | |
836 | * allocate extents on disk for the range, and create ordered data structs | |
837 | * in ram to track those extents. | |
838 | * | |
839 | * locked_page is the page that writepage had locked already. We use | |
840 | * it to make sure we don't do extra locks or unlocks. | |
841 | * | |
842 | * *page_started is set to one if we unlock locked_page and do everything | |
843 | * required to start IO on it. It may be clean and already done with | |
844 | * IO when we return. | |
845 | */ | |
00361589 JB |
846 | static noinline int cow_file_range(struct inode *inode, |
847 | struct page *locked_page, | |
848 | u64 start, u64 end, int *page_started, | |
849 | unsigned long *nr_written, | |
850 | int unlock) | |
771ed689 | 851 | { |
00361589 | 852 | struct btrfs_root *root = BTRFS_I(inode)->root; |
771ed689 CM |
853 | u64 alloc_hint = 0; |
854 | u64 num_bytes; | |
855 | unsigned long ram_size; | |
856 | u64 disk_num_bytes; | |
857 | u64 cur_alloc_size; | |
858 | u64 blocksize = root->sectorsize; | |
771ed689 CM |
859 | struct btrfs_key ins; |
860 | struct extent_map *em; | |
861 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
862 | int ret = 0; | |
863 | ||
02ecd2c2 JB |
864 | if (btrfs_is_free_space_inode(inode)) { |
865 | WARN_ON_ONCE(1); | |
866 | return -EINVAL; | |
867 | } | |
771ed689 | 868 | |
fda2832f | 869 | num_bytes = ALIGN(end - start + 1, blocksize); |
771ed689 CM |
870 | num_bytes = max(blocksize, num_bytes); |
871 | disk_num_bytes = num_bytes; | |
771ed689 | 872 | |
4cb5300b | 873 | /* if this is a small write inside eof, kick off defrag */ |
4cb13e5d LB |
874 | if (num_bytes < 64 * 1024 && |
875 | (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size)) | |
00361589 | 876 | btrfs_add_inode_defrag(NULL, inode); |
4cb5300b | 877 | |
771ed689 CM |
878 | if (start == 0) { |
879 | /* lets try to make an inline extent */ | |
00361589 JB |
880 | ret = cow_file_range_inline(root, inode, start, end, 0, 0, |
881 | NULL); | |
771ed689 | 882 | if (ret == 0) { |
c2790a2e JB |
883 | extent_clear_unlock_delalloc(inode, start, end, NULL, |
884 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc | 885 | EXTENT_DEFRAG, PAGE_UNLOCK | |
c2790a2e JB |
886 | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK | |
887 | PAGE_END_WRITEBACK); | |
c2167754 | 888 | |
771ed689 CM |
889 | *nr_written = *nr_written + |
890 | (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; | |
891 | *page_started = 1; | |
771ed689 | 892 | goto out; |
79787eaa | 893 | } else if (ret < 0) { |
79787eaa | 894 | goto out_unlock; |
771ed689 CM |
895 | } |
896 | } | |
897 | ||
898 | BUG_ON(disk_num_bytes > | |
6c41761f | 899 | btrfs_super_total_bytes(root->fs_info->super_copy)); |
771ed689 | 900 | |
4b46fce2 | 901 | alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); |
771ed689 CM |
902 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
903 | ||
d397712b | 904 | while (disk_num_bytes > 0) { |
a791e35e CM |
905 | unsigned long op; |
906 | ||
287a0ab9 | 907 | cur_alloc_size = disk_num_bytes; |
00361589 | 908 | ret = btrfs_reserve_extent(root, cur_alloc_size, |
771ed689 | 909 | root->sectorsize, 0, alloc_hint, |
81c9ad23 | 910 | &ins, 1); |
00361589 | 911 | if (ret < 0) |
79787eaa | 912 | goto out_unlock; |
d397712b | 913 | |
172ddd60 | 914 | em = alloc_extent_map(); |
b9aa55be LB |
915 | if (!em) { |
916 | ret = -ENOMEM; | |
ace68bac | 917 | goto out_reserve; |
b9aa55be | 918 | } |
e6dcd2dc | 919 | em->start = start; |
445a6944 | 920 | em->orig_start = em->start; |
771ed689 CM |
921 | ram_size = ins.offset; |
922 | em->len = ins.offset; | |
2ab28f32 JB |
923 | em->mod_start = em->start; |
924 | em->mod_len = em->len; | |
c8b97818 | 925 | |
e6dcd2dc | 926 | em->block_start = ins.objectid; |
c8b97818 | 927 | em->block_len = ins.offset; |
b4939680 | 928 | em->orig_block_len = ins.offset; |
cc95bef6 | 929 | em->ram_bytes = ram_size; |
e6dcd2dc | 930 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
7f3c74fb | 931 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
70c8a91c | 932 | em->generation = -1; |
c8b97818 | 933 | |
d397712b | 934 | while (1) { |
890871be | 935 | write_lock(&em_tree->lock); |
09a2a8f9 | 936 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 937 | write_unlock(&em_tree->lock); |
e6dcd2dc CM |
938 | if (ret != -EEXIST) { |
939 | free_extent_map(em); | |
940 | break; | |
941 | } | |
942 | btrfs_drop_extent_cache(inode, start, | |
c8b97818 | 943 | start + ram_size - 1, 0); |
e6dcd2dc | 944 | } |
ace68bac LB |
945 | if (ret) |
946 | goto out_reserve; | |
e6dcd2dc | 947 | |
98d20f67 | 948 | cur_alloc_size = ins.offset; |
e6dcd2dc | 949 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
771ed689 | 950 | ram_size, cur_alloc_size, 0); |
ace68bac LB |
951 | if (ret) |
952 | goto out_reserve; | |
c8b97818 | 953 | |
17d217fe YZ |
954 | if (root->root_key.objectid == |
955 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
956 | ret = btrfs_reloc_clone_csums(inode, start, | |
957 | cur_alloc_size); | |
00361589 | 958 | if (ret) |
ace68bac | 959 | goto out_reserve; |
17d217fe YZ |
960 | } |
961 | ||
d397712b | 962 | if (disk_num_bytes < cur_alloc_size) |
3b951516 | 963 | break; |
d397712b | 964 | |
c8b97818 CM |
965 | /* we're not doing compressed IO, don't unlock the first |
966 | * page (which the caller expects to stay locked), don't | |
967 | * clear any dirty bits and don't set any writeback bits | |
8b62b72b CM |
968 | * |
969 | * Do set the Private2 bit so we know this page was properly | |
970 | * setup for writepage | |
c8b97818 | 971 | */ |
c2790a2e JB |
972 | op = unlock ? PAGE_UNLOCK : 0; |
973 | op |= PAGE_SET_PRIVATE2; | |
a791e35e | 974 | |
c2790a2e JB |
975 | extent_clear_unlock_delalloc(inode, start, |
976 | start + ram_size - 1, locked_page, | |
977 | EXTENT_LOCKED | EXTENT_DELALLOC, | |
978 | op); | |
c8b97818 | 979 | disk_num_bytes -= cur_alloc_size; |
c59f8951 CM |
980 | num_bytes -= cur_alloc_size; |
981 | alloc_hint = ins.objectid + ins.offset; | |
982 | start += cur_alloc_size; | |
b888db2b | 983 | } |
79787eaa | 984 | out: |
be20aa9d | 985 | return ret; |
b7d5b0a8 | 986 | |
ace68bac LB |
987 | out_reserve: |
988 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); | |
79787eaa | 989 | out_unlock: |
c2790a2e | 990 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
151a41bc JB |
991 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | |
992 | EXTENT_DELALLOC | EXTENT_DEFRAG, | |
993 | PAGE_UNLOCK | PAGE_CLEAR_DIRTY | | |
994 | PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK); | |
79787eaa | 995 | goto out; |
771ed689 | 996 | } |
c8b97818 | 997 | |
771ed689 CM |
998 | /* |
999 | * work queue call back to started compression on a file and pages | |
1000 | */ | |
1001 | static noinline void async_cow_start(struct btrfs_work *work) | |
1002 | { | |
1003 | struct async_cow *async_cow; | |
1004 | int num_added = 0; | |
1005 | async_cow = container_of(work, struct async_cow, work); | |
1006 | ||
1007 | compress_file_range(async_cow->inode, async_cow->locked_page, | |
1008 | async_cow->start, async_cow->end, async_cow, | |
1009 | &num_added); | |
8180ef88 | 1010 | if (num_added == 0) { |
cb77fcd8 | 1011 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 | 1012 | async_cow->inode = NULL; |
8180ef88 | 1013 | } |
771ed689 CM |
1014 | } |
1015 | ||
1016 | /* | |
1017 | * work queue call back to submit previously compressed pages | |
1018 | */ | |
1019 | static noinline void async_cow_submit(struct btrfs_work *work) | |
1020 | { | |
1021 | struct async_cow *async_cow; | |
1022 | struct btrfs_root *root; | |
1023 | unsigned long nr_pages; | |
1024 | ||
1025 | async_cow = container_of(work, struct async_cow, work); | |
1026 | ||
1027 | root = async_cow->root; | |
1028 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> | |
1029 | PAGE_CACHE_SHIFT; | |
1030 | ||
66657b31 | 1031 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
287082b0 | 1032 | 5 * 1024 * 1024 && |
771ed689 CM |
1033 | waitqueue_active(&root->fs_info->async_submit_wait)) |
1034 | wake_up(&root->fs_info->async_submit_wait); | |
1035 | ||
d397712b | 1036 | if (async_cow->inode) |
771ed689 | 1037 | submit_compressed_extents(async_cow->inode, async_cow); |
771ed689 | 1038 | } |
c8b97818 | 1039 | |
771ed689 CM |
1040 | static noinline void async_cow_free(struct btrfs_work *work) |
1041 | { | |
1042 | struct async_cow *async_cow; | |
1043 | async_cow = container_of(work, struct async_cow, work); | |
8180ef88 | 1044 | if (async_cow->inode) |
cb77fcd8 | 1045 | btrfs_add_delayed_iput(async_cow->inode); |
771ed689 CM |
1046 | kfree(async_cow); |
1047 | } | |
1048 | ||
1049 | static int cow_file_range_async(struct inode *inode, struct page *locked_page, | |
1050 | u64 start, u64 end, int *page_started, | |
1051 | unsigned long *nr_written) | |
1052 | { | |
1053 | struct async_cow *async_cow; | |
1054 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1055 | unsigned long nr_pages; | |
1056 | u64 cur_end; | |
287082b0 | 1057 | int limit = 10 * 1024 * 1024; |
771ed689 | 1058 | |
a3429ab7 CM |
1059 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED, |
1060 | 1, 0, NULL, GFP_NOFS); | |
d397712b | 1061 | while (start < end) { |
771ed689 | 1062 | async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); |
79787eaa | 1063 | BUG_ON(!async_cow); /* -ENOMEM */ |
8180ef88 | 1064 | async_cow->inode = igrab(inode); |
771ed689 CM |
1065 | async_cow->root = root; |
1066 | async_cow->locked_page = locked_page; | |
1067 | async_cow->start = start; | |
1068 | ||
6cbff00f | 1069 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) |
771ed689 CM |
1070 | cur_end = end; |
1071 | else | |
1072 | cur_end = min(end, start + 512 * 1024 - 1); | |
1073 | ||
1074 | async_cow->end = cur_end; | |
1075 | INIT_LIST_HEAD(&async_cow->extents); | |
1076 | ||
1077 | async_cow->work.func = async_cow_start; | |
1078 | async_cow->work.ordered_func = async_cow_submit; | |
1079 | async_cow->work.ordered_free = async_cow_free; | |
1080 | async_cow->work.flags = 0; | |
1081 | ||
771ed689 CM |
1082 | nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >> |
1083 | PAGE_CACHE_SHIFT; | |
1084 | atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); | |
1085 | ||
1086 | btrfs_queue_worker(&root->fs_info->delalloc_workers, | |
1087 | &async_cow->work); | |
1088 | ||
1089 | if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { | |
1090 | wait_event(root->fs_info->async_submit_wait, | |
1091 | (atomic_read(&root->fs_info->async_delalloc_pages) < | |
1092 | limit)); | |
1093 | } | |
1094 | ||
d397712b | 1095 | while (atomic_read(&root->fs_info->async_submit_draining) && |
771ed689 CM |
1096 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
1097 | wait_event(root->fs_info->async_submit_wait, | |
1098 | (atomic_read(&root->fs_info->async_delalloc_pages) == | |
1099 | 0)); | |
1100 | } | |
1101 | ||
1102 | *nr_written += nr_pages; | |
1103 | start = cur_end + 1; | |
1104 | } | |
1105 | *page_started = 1; | |
1106 | return 0; | |
be20aa9d CM |
1107 | } |
1108 | ||
d397712b | 1109 | static noinline int csum_exist_in_range(struct btrfs_root *root, |
17d217fe YZ |
1110 | u64 bytenr, u64 num_bytes) |
1111 | { | |
1112 | int ret; | |
1113 | struct btrfs_ordered_sum *sums; | |
1114 | LIST_HEAD(list); | |
1115 | ||
07d400a6 | 1116 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, |
a2de733c | 1117 | bytenr + num_bytes - 1, &list, 0); |
17d217fe YZ |
1118 | if (ret == 0 && list_empty(&list)) |
1119 | return 0; | |
1120 | ||
1121 | while (!list_empty(&list)) { | |
1122 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
1123 | list_del(&sums->list); | |
1124 | kfree(sums); | |
1125 | } | |
1126 | return 1; | |
1127 | } | |
1128 | ||
d352ac68 CM |
1129 | /* |
1130 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
1131 | * of the extents that exist in the file, and COWs the file as required. | |
1132 | * | |
1133 | * If no cow copies or snapshots exist, we write directly to the existing | |
1134 | * blocks on disk | |
1135 | */ | |
7f366cfe CM |
1136 | static noinline int run_delalloc_nocow(struct inode *inode, |
1137 | struct page *locked_page, | |
771ed689 CM |
1138 | u64 start, u64 end, int *page_started, int force, |
1139 | unsigned long *nr_written) | |
be20aa9d | 1140 | { |
be20aa9d | 1141 | struct btrfs_root *root = BTRFS_I(inode)->root; |
7ea394f1 | 1142 | struct btrfs_trans_handle *trans; |
be20aa9d | 1143 | struct extent_buffer *leaf; |
be20aa9d | 1144 | struct btrfs_path *path; |
80ff3856 | 1145 | struct btrfs_file_extent_item *fi; |
be20aa9d | 1146 | struct btrfs_key found_key; |
80ff3856 YZ |
1147 | u64 cow_start; |
1148 | u64 cur_offset; | |
1149 | u64 extent_end; | |
5d4f98a2 | 1150 | u64 extent_offset; |
80ff3856 YZ |
1151 | u64 disk_bytenr; |
1152 | u64 num_bytes; | |
b4939680 | 1153 | u64 disk_num_bytes; |
cc95bef6 | 1154 | u64 ram_bytes; |
80ff3856 | 1155 | int extent_type; |
79787eaa | 1156 | int ret, err; |
d899e052 | 1157 | int type; |
80ff3856 YZ |
1158 | int nocow; |
1159 | int check_prev = 1; | |
82d5902d | 1160 | bool nolock; |
33345d01 | 1161 | u64 ino = btrfs_ino(inode); |
be20aa9d CM |
1162 | |
1163 | path = btrfs_alloc_path(); | |
17ca04af | 1164 | if (!path) { |
c2790a2e JB |
1165 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1166 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1167 | EXTENT_DO_ACCOUNTING | |
1168 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1169 | PAGE_CLEAR_DIRTY | |
1170 | PAGE_SET_WRITEBACK | | |
1171 | PAGE_END_WRITEBACK); | |
d8926bb3 | 1172 | return -ENOMEM; |
17ca04af | 1173 | } |
82d5902d | 1174 | |
83eea1f1 | 1175 | nolock = btrfs_is_free_space_inode(inode); |
82d5902d LZ |
1176 | |
1177 | if (nolock) | |
7a7eaa40 | 1178 | trans = btrfs_join_transaction_nolock(root); |
82d5902d | 1179 | else |
7a7eaa40 | 1180 | trans = btrfs_join_transaction(root); |
ff5714cc | 1181 | |
79787eaa | 1182 | if (IS_ERR(trans)) { |
c2790a2e JB |
1183 | extent_clear_unlock_delalloc(inode, start, end, locked_page, |
1184 | EXTENT_LOCKED | EXTENT_DELALLOC | | |
151a41bc JB |
1185 | EXTENT_DO_ACCOUNTING | |
1186 | EXTENT_DEFRAG, PAGE_UNLOCK | | |
c2790a2e JB |
1187 | PAGE_CLEAR_DIRTY | |
1188 | PAGE_SET_WRITEBACK | | |
1189 | PAGE_END_WRITEBACK); | |
79787eaa JM |
1190 | btrfs_free_path(path); |
1191 | return PTR_ERR(trans); | |
1192 | } | |
1193 | ||
74b21075 | 1194 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
be20aa9d | 1195 | |
80ff3856 YZ |
1196 | cow_start = (u64)-1; |
1197 | cur_offset = start; | |
1198 | while (1) { | |
33345d01 | 1199 | ret = btrfs_lookup_file_extent(trans, root, path, ino, |
80ff3856 | 1200 | cur_offset, 0); |
d788a349 | 1201 | if (ret < 0) |
79787eaa | 1202 | goto error; |
80ff3856 YZ |
1203 | if (ret > 0 && path->slots[0] > 0 && check_prev) { |
1204 | leaf = path->nodes[0]; | |
1205 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1206 | path->slots[0] - 1); | |
33345d01 | 1207 | if (found_key.objectid == ino && |
80ff3856 YZ |
1208 | found_key.type == BTRFS_EXTENT_DATA_KEY) |
1209 | path->slots[0]--; | |
1210 | } | |
1211 | check_prev = 0; | |
1212 | next_slot: | |
1213 | leaf = path->nodes[0]; | |
1214 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1215 | ret = btrfs_next_leaf(root, path); | |
d788a349 | 1216 | if (ret < 0) |
79787eaa | 1217 | goto error; |
80ff3856 YZ |
1218 | if (ret > 0) |
1219 | break; | |
1220 | leaf = path->nodes[0]; | |
1221 | } | |
be20aa9d | 1222 | |
80ff3856 YZ |
1223 | nocow = 0; |
1224 | disk_bytenr = 0; | |
17d217fe | 1225 | num_bytes = 0; |
80ff3856 YZ |
1226 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
1227 | ||
33345d01 | 1228 | if (found_key.objectid > ino || |
80ff3856 YZ |
1229 | found_key.type > BTRFS_EXTENT_DATA_KEY || |
1230 | found_key.offset > end) | |
1231 | break; | |
1232 | ||
1233 | if (found_key.offset > cur_offset) { | |
1234 | extent_end = found_key.offset; | |
e9061e21 | 1235 | extent_type = 0; |
80ff3856 YZ |
1236 | goto out_check; |
1237 | } | |
1238 | ||
1239 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1240 | struct btrfs_file_extent_item); | |
1241 | extent_type = btrfs_file_extent_type(leaf, fi); | |
1242 | ||
cc95bef6 | 1243 | ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
d899e052 YZ |
1244 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
1245 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 | 1246 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
5d4f98a2 | 1247 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
80ff3856 YZ |
1248 | extent_end = found_key.offset + |
1249 | btrfs_file_extent_num_bytes(leaf, fi); | |
b4939680 JB |
1250 | disk_num_bytes = |
1251 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
80ff3856 YZ |
1252 | if (extent_end <= start) { |
1253 | path->slots[0]++; | |
1254 | goto next_slot; | |
1255 | } | |
17d217fe YZ |
1256 | if (disk_bytenr == 0) |
1257 | goto out_check; | |
80ff3856 YZ |
1258 | if (btrfs_file_extent_compression(leaf, fi) || |
1259 | btrfs_file_extent_encryption(leaf, fi) || | |
1260 | btrfs_file_extent_other_encoding(leaf, fi)) | |
1261 | goto out_check; | |
d899e052 YZ |
1262 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
1263 | goto out_check; | |
d2fb3437 | 1264 | if (btrfs_extent_readonly(root, disk_bytenr)) |
80ff3856 | 1265 | goto out_check; |
33345d01 | 1266 | if (btrfs_cross_ref_exist(trans, root, ino, |
5d4f98a2 YZ |
1267 | found_key.offset - |
1268 | extent_offset, disk_bytenr)) | |
17d217fe | 1269 | goto out_check; |
5d4f98a2 | 1270 | disk_bytenr += extent_offset; |
17d217fe YZ |
1271 | disk_bytenr += cur_offset - found_key.offset; |
1272 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
1273 | /* | |
1274 | * force cow if csum exists in the range. | |
1275 | * this ensure that csum for a given extent are | |
1276 | * either valid or do not exist. | |
1277 | */ | |
1278 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) | |
1279 | goto out_check; | |
80ff3856 YZ |
1280 | nocow = 1; |
1281 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
1282 | extent_end = found_key.offset + | |
1283 | btrfs_file_extent_inline_len(leaf, fi); | |
1284 | extent_end = ALIGN(extent_end, root->sectorsize); | |
1285 | } else { | |
1286 | BUG_ON(1); | |
1287 | } | |
1288 | out_check: | |
1289 | if (extent_end <= start) { | |
1290 | path->slots[0]++; | |
1291 | goto next_slot; | |
1292 | } | |
1293 | if (!nocow) { | |
1294 | if (cow_start == (u64)-1) | |
1295 | cow_start = cur_offset; | |
1296 | cur_offset = extent_end; | |
1297 | if (cur_offset > end) | |
1298 | break; | |
1299 | path->slots[0]++; | |
1300 | goto next_slot; | |
7ea394f1 YZ |
1301 | } |
1302 | ||
b3b4aa74 | 1303 | btrfs_release_path(path); |
80ff3856 | 1304 | if (cow_start != (u64)-1) { |
00361589 JB |
1305 | ret = cow_file_range(inode, locked_page, |
1306 | cow_start, found_key.offset - 1, | |
1307 | page_started, nr_written, 1); | |
d788a349 | 1308 | if (ret) |
79787eaa | 1309 | goto error; |
80ff3856 | 1310 | cow_start = (u64)-1; |
7ea394f1 | 1311 | } |
80ff3856 | 1312 | |
d899e052 YZ |
1313 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
1314 | struct extent_map *em; | |
1315 | struct extent_map_tree *em_tree; | |
1316 | em_tree = &BTRFS_I(inode)->extent_tree; | |
172ddd60 | 1317 | em = alloc_extent_map(); |
79787eaa | 1318 | BUG_ON(!em); /* -ENOMEM */ |
d899e052 | 1319 | em->start = cur_offset; |
70c8a91c | 1320 | em->orig_start = found_key.offset - extent_offset; |
d899e052 YZ |
1321 | em->len = num_bytes; |
1322 | em->block_len = num_bytes; | |
1323 | em->block_start = disk_bytenr; | |
b4939680 | 1324 | em->orig_block_len = disk_num_bytes; |
cc95bef6 | 1325 | em->ram_bytes = ram_bytes; |
d899e052 | 1326 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
2ab28f32 JB |
1327 | em->mod_start = em->start; |
1328 | em->mod_len = em->len; | |
d899e052 | 1329 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
b11e234d | 1330 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
70c8a91c | 1331 | em->generation = -1; |
d899e052 | 1332 | while (1) { |
890871be | 1333 | write_lock(&em_tree->lock); |
09a2a8f9 | 1334 | ret = add_extent_mapping(em_tree, em, 1); |
890871be | 1335 | write_unlock(&em_tree->lock); |
d899e052 YZ |
1336 | if (ret != -EEXIST) { |
1337 | free_extent_map(em); | |
1338 | break; | |
1339 | } | |
1340 | btrfs_drop_extent_cache(inode, em->start, | |
1341 | em->start + em->len - 1, 0); | |
1342 | } | |
1343 | type = BTRFS_ORDERED_PREALLOC; | |
1344 | } else { | |
1345 | type = BTRFS_ORDERED_NOCOW; | |
1346 | } | |
80ff3856 YZ |
1347 | |
1348 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 | 1349 | num_bytes, num_bytes, type); |
79787eaa | 1350 | BUG_ON(ret); /* -ENOMEM */ |
771ed689 | 1351 | |
efa56464 YZ |
1352 | if (root->root_key.objectid == |
1353 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
1354 | ret = btrfs_reloc_clone_csums(inode, cur_offset, | |
1355 | num_bytes); | |
d788a349 | 1356 | if (ret) |
79787eaa | 1357 | goto error; |
efa56464 YZ |
1358 | } |
1359 | ||
c2790a2e JB |
1360 | extent_clear_unlock_delalloc(inode, cur_offset, |
1361 | cur_offset + num_bytes - 1, | |
1362 | locked_page, EXTENT_LOCKED | | |
1363 | EXTENT_DELALLOC, PAGE_UNLOCK | | |
1364 | PAGE_SET_PRIVATE2); | |
80ff3856 YZ |
1365 | cur_offset = extent_end; |
1366 | if (cur_offset > end) | |
1367 | break; | |
be20aa9d | 1368 | } |
b3b4aa74 | 1369 | btrfs_release_path(path); |
80ff3856 | 1370 | |
17ca04af | 1371 | if (cur_offset <= end && cow_start == (u64)-1) { |
80ff3856 | 1372 | cow_start = cur_offset; |
17ca04af JB |
1373 | cur_offset = end; |
1374 | } | |
1375 | ||
80ff3856 | 1376 | if (cow_start != (u64)-1) { |
00361589 JB |
1377 | ret = cow_file_range(inode, locked_page, cow_start, end, |
1378 | page_started, nr_written, 1); | |
d788a349 | 1379 | if (ret) |
79787eaa | 1380 | goto error; |
80ff3856 YZ |
1381 | } |
1382 | ||
79787eaa | 1383 | error: |
a698d075 | 1384 | err = btrfs_end_transaction(trans, root); |
79787eaa JM |
1385 | if (!ret) |
1386 | ret = err; | |
1387 | ||
17ca04af | 1388 | if (ret && cur_offset < end) |
c2790a2e JB |
1389 | extent_clear_unlock_delalloc(inode, cur_offset, end, |
1390 | locked_page, EXTENT_LOCKED | | |
151a41bc JB |
1391 | EXTENT_DELALLOC | EXTENT_DEFRAG | |
1392 | EXTENT_DO_ACCOUNTING, PAGE_UNLOCK | | |
1393 | PAGE_CLEAR_DIRTY | | |
c2790a2e JB |
1394 | PAGE_SET_WRITEBACK | |
1395 | PAGE_END_WRITEBACK); | |
7ea394f1 | 1396 | btrfs_free_path(path); |
79787eaa | 1397 | return ret; |
be20aa9d CM |
1398 | } |
1399 | ||
d352ac68 CM |
1400 | /* |
1401 | * extent_io.c call back to do delayed allocation processing | |
1402 | */ | |
c8b97818 | 1403 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
771ed689 CM |
1404 | u64 start, u64 end, int *page_started, |
1405 | unsigned long *nr_written) | |
be20aa9d | 1406 | { |
be20aa9d | 1407 | int ret; |
7f366cfe | 1408 | struct btrfs_root *root = BTRFS_I(inode)->root; |
a2135011 | 1409 | |
7ddf5a42 | 1410 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) { |
c8b97818 | 1411 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1412 | page_started, 1, nr_written); |
7ddf5a42 | 1413 | } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) { |
d899e052 | 1414 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d397712b | 1415 | page_started, 0, nr_written); |
7ddf5a42 JB |
1416 | } else if (!btrfs_test_opt(root, COMPRESS) && |
1417 | !(BTRFS_I(inode)->force_compress) && | |
1418 | !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) { | |
7f366cfe CM |
1419 | ret = cow_file_range(inode, locked_page, start, end, |
1420 | page_started, nr_written, 1); | |
7ddf5a42 JB |
1421 | } else { |
1422 | set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
1423 | &BTRFS_I(inode)->runtime_flags); | |
771ed689 | 1424 | ret = cow_file_range_async(inode, locked_page, start, end, |
d397712b | 1425 | page_started, nr_written); |
7ddf5a42 | 1426 | } |
b888db2b CM |
1427 | return ret; |
1428 | } | |
1429 | ||
1bf85046 JM |
1430 | static void btrfs_split_extent_hook(struct inode *inode, |
1431 | struct extent_state *orig, u64 split) | |
9ed74f2d | 1432 | { |
0ca1f7ce | 1433 | /* not delalloc, ignore it */ |
9ed74f2d | 1434 | if (!(orig->state & EXTENT_DELALLOC)) |
1bf85046 | 1435 | return; |
9ed74f2d | 1436 | |
9e0baf60 JB |
1437 | spin_lock(&BTRFS_I(inode)->lock); |
1438 | BTRFS_I(inode)->outstanding_extents++; | |
1439 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1440 | } |
1441 | ||
1442 | /* | |
1443 | * extent_io.c merge_extent_hook, used to track merged delayed allocation | |
1444 | * extents so we can keep track of new extents that are just merged onto old | |
1445 | * extents, such as when we are doing sequential writes, so we can properly | |
1446 | * account for the metadata space we'll need. | |
1447 | */ | |
1bf85046 JM |
1448 | static void btrfs_merge_extent_hook(struct inode *inode, |
1449 | struct extent_state *new, | |
1450 | struct extent_state *other) | |
9ed74f2d | 1451 | { |
9ed74f2d JB |
1452 | /* not delalloc, ignore it */ |
1453 | if (!(other->state & EXTENT_DELALLOC)) | |
1bf85046 | 1454 | return; |
9ed74f2d | 1455 | |
9e0baf60 JB |
1456 | spin_lock(&BTRFS_I(inode)->lock); |
1457 | BTRFS_I(inode)->outstanding_extents--; | |
1458 | spin_unlock(&BTRFS_I(inode)->lock); | |
9ed74f2d JB |
1459 | } |
1460 | ||
eb73c1b7 MX |
1461 | static void btrfs_add_delalloc_inodes(struct btrfs_root *root, |
1462 | struct inode *inode) | |
1463 | { | |
1464 | spin_lock(&root->delalloc_lock); | |
1465 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1466 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
1467 | &root->delalloc_inodes); | |
1468 | set_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1469 | &BTRFS_I(inode)->runtime_flags); | |
1470 | root->nr_delalloc_inodes++; | |
1471 | if (root->nr_delalloc_inodes == 1) { | |
1472 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1473 | BUG_ON(!list_empty(&root->delalloc_root)); | |
1474 | list_add_tail(&root->delalloc_root, | |
1475 | &root->fs_info->delalloc_roots); | |
1476 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1477 | } | |
1478 | } | |
1479 | spin_unlock(&root->delalloc_lock); | |
1480 | } | |
1481 | ||
1482 | static void btrfs_del_delalloc_inode(struct btrfs_root *root, | |
1483 | struct inode *inode) | |
1484 | { | |
1485 | spin_lock(&root->delalloc_lock); | |
1486 | if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
1487 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); | |
1488 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, | |
1489 | &BTRFS_I(inode)->runtime_flags); | |
1490 | root->nr_delalloc_inodes--; | |
1491 | if (!root->nr_delalloc_inodes) { | |
1492 | spin_lock(&root->fs_info->delalloc_root_lock); | |
1493 | BUG_ON(list_empty(&root->delalloc_root)); | |
1494 | list_del_init(&root->delalloc_root); | |
1495 | spin_unlock(&root->fs_info->delalloc_root_lock); | |
1496 | } | |
1497 | } | |
1498 | spin_unlock(&root->delalloc_lock); | |
1499 | } | |
1500 | ||
d352ac68 CM |
1501 | /* |
1502 | * extent_io.c set_bit_hook, used to track delayed allocation | |
1503 | * bytes in this file, and to maintain the list of inodes that | |
1504 | * have pending delalloc work to be done. | |
1505 | */ | |
1bf85046 | 1506 | static void btrfs_set_bit_hook(struct inode *inode, |
41074888 | 1507 | struct extent_state *state, unsigned long *bits) |
291d673e | 1508 | { |
9ed74f2d | 1509 | |
75eff68e CM |
1510 | /* |
1511 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1512 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1513 | * bit, which is only set or cleared with irqs on |
1514 | */ | |
0ca1f7ce | 1515 | if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1516 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1517 | u64 len = state->end + 1 - state->start; |
83eea1f1 | 1518 | bool do_list = !btrfs_is_free_space_inode(inode); |
9ed74f2d | 1519 | |
9e0baf60 | 1520 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1521 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1522 | } else { |
1523 | spin_lock(&BTRFS_I(inode)->lock); | |
1524 | BTRFS_I(inode)->outstanding_extents++; | |
1525 | spin_unlock(&BTRFS_I(inode)->lock); | |
1526 | } | |
287a0ab9 | 1527 | |
963d678b MX |
1528 | __percpu_counter_add(&root->fs_info->delalloc_bytes, len, |
1529 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1530 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1531 | BTRFS_I(inode)->delalloc_bytes += len; |
df0af1a5 | 1532 | if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1533 | &BTRFS_I(inode)->runtime_flags)) |
1534 | btrfs_add_delalloc_inodes(root, inode); | |
df0af1a5 | 1535 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1536 | } |
291d673e CM |
1537 | } |
1538 | ||
d352ac68 CM |
1539 | /* |
1540 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
1541 | */ | |
1bf85046 | 1542 | static void btrfs_clear_bit_hook(struct inode *inode, |
41074888 DS |
1543 | struct extent_state *state, |
1544 | unsigned long *bits) | |
291d673e | 1545 | { |
75eff68e CM |
1546 | /* |
1547 | * set_bit and clear bit hooks normally require _irqsave/restore | |
27160b6b | 1548 | * but in this case, we are only testing for the DELALLOC |
75eff68e CM |
1549 | * bit, which is only set or cleared with irqs on |
1550 | */ | |
0ca1f7ce | 1551 | if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { |
291d673e | 1552 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 1553 | u64 len = state->end + 1 - state->start; |
83eea1f1 | 1554 | bool do_list = !btrfs_is_free_space_inode(inode); |
bcbfce8a | 1555 | |
9e0baf60 | 1556 | if (*bits & EXTENT_FIRST_DELALLOC) { |
0ca1f7ce | 1557 | *bits &= ~EXTENT_FIRST_DELALLOC; |
9e0baf60 JB |
1558 | } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { |
1559 | spin_lock(&BTRFS_I(inode)->lock); | |
1560 | BTRFS_I(inode)->outstanding_extents--; | |
1561 | spin_unlock(&BTRFS_I(inode)->lock); | |
1562 | } | |
0ca1f7ce | 1563 | |
b6d08f06 JB |
1564 | /* |
1565 | * We don't reserve metadata space for space cache inodes so we | |
1566 | * don't need to call dellalloc_release_metadata if there is an | |
1567 | * error. | |
1568 | */ | |
1569 | if (*bits & EXTENT_DO_ACCOUNTING && | |
1570 | root != root->fs_info->tree_root) | |
0ca1f7ce YZ |
1571 | btrfs_delalloc_release_metadata(inode, len); |
1572 | ||
0cb59c99 | 1573 | if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID |
7ee9e440 | 1574 | && do_list && !(state->state & EXTENT_NORESERVE)) |
0ca1f7ce | 1575 | btrfs_free_reserved_data_space(inode, len); |
9ed74f2d | 1576 | |
963d678b MX |
1577 | __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, |
1578 | root->fs_info->delalloc_batch); | |
df0af1a5 | 1579 | spin_lock(&BTRFS_I(inode)->lock); |
0ca1f7ce | 1580 | BTRFS_I(inode)->delalloc_bytes -= len; |
0cb59c99 | 1581 | if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && |
df0af1a5 | 1582 | test_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
eb73c1b7 MX |
1583 | &BTRFS_I(inode)->runtime_flags)) |
1584 | btrfs_del_delalloc_inode(root, inode); | |
df0af1a5 | 1585 | spin_unlock(&BTRFS_I(inode)->lock); |
291d673e | 1586 | } |
291d673e CM |
1587 | } |
1588 | ||
d352ac68 CM |
1589 | /* |
1590 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
1591 | * we don't create bios that span stripes or chunks | |
1592 | */ | |
64a16701 | 1593 | int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset, |
c8b97818 CM |
1594 | size_t size, struct bio *bio, |
1595 | unsigned long bio_flags) | |
239b14b3 CM |
1596 | { |
1597 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
a62b9401 | 1598 | u64 logical = (u64)bio->bi_sector << 9; |
239b14b3 CM |
1599 | u64 length = 0; |
1600 | u64 map_length; | |
239b14b3 CM |
1601 | int ret; |
1602 | ||
771ed689 CM |
1603 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
1604 | return 0; | |
1605 | ||
f2d8d74d | 1606 | length = bio->bi_size; |
239b14b3 | 1607 | map_length = length; |
64a16701 | 1608 | ret = btrfs_map_block(root->fs_info, rw, logical, |
f188591e | 1609 | &map_length, NULL, 0); |
3ec706c8 | 1610 | /* Will always return 0 with map_multi == NULL */ |
3444a972 | 1611 | BUG_ON(ret < 0); |
d397712b | 1612 | if (map_length < length + size) |
239b14b3 | 1613 | return 1; |
3444a972 | 1614 | return 0; |
239b14b3 CM |
1615 | } |
1616 | ||
d352ac68 CM |
1617 | /* |
1618 | * in order to insert checksums into the metadata in large chunks, | |
1619 | * we wait until bio submission time. All the pages in the bio are | |
1620 | * checksummed and sums are attached onto the ordered extent record. | |
1621 | * | |
1622 | * At IO completion time the cums attached on the ordered extent record | |
1623 | * are inserted into the btree | |
1624 | */ | |
d397712b CM |
1625 | static int __btrfs_submit_bio_start(struct inode *inode, int rw, |
1626 | struct bio *bio, int mirror_num, | |
eaf25d93 CM |
1627 | unsigned long bio_flags, |
1628 | u64 bio_offset) | |
065631f6 | 1629 | { |
065631f6 | 1630 | struct btrfs_root *root = BTRFS_I(inode)->root; |
065631f6 | 1631 | int ret = 0; |
e015640f | 1632 | |
d20f7043 | 1633 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); |
79787eaa | 1634 | BUG_ON(ret); /* -ENOMEM */ |
4a69a410 CM |
1635 | return 0; |
1636 | } | |
e015640f | 1637 | |
4a69a410 CM |
1638 | /* |
1639 | * in order to insert checksums into the metadata in large chunks, | |
1640 | * we wait until bio submission time. All the pages in the bio are | |
1641 | * checksummed and sums are attached onto the ordered extent record. | |
1642 | * | |
1643 | * At IO completion time the cums attached on the ordered extent record | |
1644 | * are inserted into the btree | |
1645 | */ | |
b2950863 | 1646 | static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1647 | int mirror_num, unsigned long bio_flags, |
1648 | u64 bio_offset) | |
4a69a410 CM |
1649 | { |
1650 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
61891923 SB |
1651 | int ret; |
1652 | ||
1653 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 1); | |
1654 | if (ret) | |
1655 | bio_endio(bio, ret); | |
1656 | return ret; | |
44b8bd7e CM |
1657 | } |
1658 | ||
d352ac68 | 1659 | /* |
cad321ad CM |
1660 | * extent_io.c submission hook. This does the right thing for csum calculation |
1661 | * on write, or reading the csums from the tree before a read | |
d352ac68 | 1662 | */ |
b2950863 | 1663 | static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
eaf25d93 CM |
1664 | int mirror_num, unsigned long bio_flags, |
1665 | u64 bio_offset) | |
44b8bd7e CM |
1666 | { |
1667 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1668 | int ret = 0; | |
19b9bdb0 | 1669 | int skip_sum; |
0417341e | 1670 | int metadata = 0; |
b812ce28 | 1671 | int async = !atomic_read(&BTRFS_I(inode)->sync_writers); |
44b8bd7e | 1672 | |
6cbff00f | 1673 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
cad321ad | 1674 | |
83eea1f1 | 1675 | if (btrfs_is_free_space_inode(inode)) |
0417341e JM |
1676 | metadata = 2; |
1677 | ||
7b6d91da | 1678 | if (!(rw & REQ_WRITE)) { |
5fd02043 JB |
1679 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); |
1680 | if (ret) | |
61891923 | 1681 | goto out; |
5fd02043 | 1682 | |
d20f7043 | 1683 | if (bio_flags & EXTENT_BIO_COMPRESSED) { |
61891923 SB |
1684 | ret = btrfs_submit_compressed_read(inode, bio, |
1685 | mirror_num, | |
1686 | bio_flags); | |
1687 | goto out; | |
c2db1073 TI |
1688 | } else if (!skip_sum) { |
1689 | ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); | |
1690 | if (ret) | |
61891923 | 1691 | goto out; |
c2db1073 | 1692 | } |
4d1b5fb4 | 1693 | goto mapit; |
b812ce28 | 1694 | } else if (async && !skip_sum) { |
17d217fe YZ |
1695 | /* csum items have already been cloned */ |
1696 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) | |
1697 | goto mapit; | |
19b9bdb0 | 1698 | /* we're doing a write, do the async checksumming */ |
61891923 | 1699 | ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
44b8bd7e | 1700 | inode, rw, bio, mirror_num, |
eaf25d93 CM |
1701 | bio_flags, bio_offset, |
1702 | __btrfs_submit_bio_start, | |
4a69a410 | 1703 | __btrfs_submit_bio_done); |
61891923 | 1704 | goto out; |
b812ce28 JB |
1705 | } else if (!skip_sum) { |
1706 | ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); | |
1707 | if (ret) | |
1708 | goto out; | |
19b9bdb0 CM |
1709 | } |
1710 | ||
0b86a832 | 1711 | mapit: |
61891923 SB |
1712 | ret = btrfs_map_bio(root, rw, bio, mirror_num, 0); |
1713 | ||
1714 | out: | |
1715 | if (ret < 0) | |
1716 | bio_endio(bio, ret); | |
1717 | return ret; | |
065631f6 | 1718 | } |
6885f308 | 1719 | |
d352ac68 CM |
1720 | /* |
1721 | * given a list of ordered sums record them in the inode. This happens | |
1722 | * at IO completion time based on sums calculated at bio submission time. | |
1723 | */ | |
ba1da2f4 | 1724 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
1725 | struct inode *inode, u64 file_offset, |
1726 | struct list_head *list) | |
1727 | { | |
e6dcd2dc CM |
1728 | struct btrfs_ordered_sum *sum; |
1729 | ||
c6e30871 | 1730 | list_for_each_entry(sum, list, list) { |
39847c4d | 1731 | trans->adding_csums = 1; |
d20f7043 CM |
1732 | btrfs_csum_file_blocks(trans, |
1733 | BTRFS_I(inode)->root->fs_info->csum_root, sum); | |
39847c4d | 1734 | trans->adding_csums = 0; |
e6dcd2dc CM |
1735 | } |
1736 | return 0; | |
1737 | } | |
1738 | ||
2ac55d41 JB |
1739 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, |
1740 | struct extent_state **cached_state) | |
ea8c2819 | 1741 | { |
6c1500f2 | 1742 | WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0); |
ea8c2819 | 1743 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, |
2ac55d41 | 1744 | cached_state, GFP_NOFS); |
ea8c2819 CM |
1745 | } |
1746 | ||
d352ac68 | 1747 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
1748 | struct btrfs_writepage_fixup { |
1749 | struct page *page; | |
1750 | struct btrfs_work work; | |
1751 | }; | |
1752 | ||
b2950863 | 1753 | static void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
247e743c CM |
1754 | { |
1755 | struct btrfs_writepage_fixup *fixup; | |
1756 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 1757 | struct extent_state *cached_state = NULL; |
247e743c CM |
1758 | struct page *page; |
1759 | struct inode *inode; | |
1760 | u64 page_start; | |
1761 | u64 page_end; | |
87826df0 | 1762 | int ret; |
247e743c CM |
1763 | |
1764 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
1765 | page = fixup->page; | |
4a096752 | 1766 | again: |
247e743c CM |
1767 | lock_page(page); |
1768 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
1769 | ClearPageChecked(page); | |
1770 | goto out_page; | |
1771 | } | |
1772 | ||
1773 | inode = page->mapping->host; | |
1774 | page_start = page_offset(page); | |
1775 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; | |
1776 | ||
2ac55d41 | 1777 | lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, |
d0082371 | 1778 | &cached_state); |
4a096752 CM |
1779 | |
1780 | /* already ordered? We're done */ | |
8b62b72b | 1781 | if (PagePrivate2(page)) |
247e743c | 1782 | goto out; |
4a096752 CM |
1783 | |
1784 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
1785 | if (ordered) { | |
2ac55d41 JB |
1786 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, |
1787 | page_end, &cached_state, GFP_NOFS); | |
4a096752 CM |
1788 | unlock_page(page); |
1789 | btrfs_start_ordered_extent(inode, ordered, 1); | |
87826df0 | 1790 | btrfs_put_ordered_extent(ordered); |
4a096752 CM |
1791 | goto again; |
1792 | } | |
247e743c | 1793 | |
87826df0 JM |
1794 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
1795 | if (ret) { | |
1796 | mapping_set_error(page->mapping, ret); | |
1797 | end_extent_writepage(page, ret, page_start, page_end); | |
1798 | ClearPageChecked(page); | |
1799 | goto out; | |
1800 | } | |
1801 | ||
2ac55d41 | 1802 | btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); |
247e743c | 1803 | ClearPageChecked(page); |
87826df0 | 1804 | set_page_dirty(page); |
247e743c | 1805 | out: |
2ac55d41 JB |
1806 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, |
1807 | &cached_state, GFP_NOFS); | |
247e743c CM |
1808 | out_page: |
1809 | unlock_page(page); | |
1810 | page_cache_release(page); | |
b897abec | 1811 | kfree(fixup); |
247e743c CM |
1812 | } |
1813 | ||
1814 | /* | |
1815 | * There are a few paths in the higher layers of the kernel that directly | |
1816 | * set the page dirty bit without asking the filesystem if it is a | |
1817 | * good idea. This causes problems because we want to make sure COW | |
1818 | * properly happens and the data=ordered rules are followed. | |
1819 | * | |
c8b97818 | 1820 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
1821 | * hasn't been properly setup for IO. We kick off an async process |
1822 | * to fix it up. The async helper will wait for ordered extents, set | |
1823 | * the delalloc bit and make it safe to write the page. | |
1824 | */ | |
b2950863 | 1825 | static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) |
247e743c CM |
1826 | { |
1827 | struct inode *inode = page->mapping->host; | |
1828 | struct btrfs_writepage_fixup *fixup; | |
1829 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
247e743c | 1830 | |
8b62b72b CM |
1831 | /* this page is properly in the ordered list */ |
1832 | if (TestClearPagePrivate2(page)) | |
247e743c CM |
1833 | return 0; |
1834 | ||
1835 | if (PageChecked(page)) | |
1836 | return -EAGAIN; | |
1837 | ||
1838 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
1839 | if (!fixup) | |
1840 | return -EAGAIN; | |
f421950f | 1841 | |
247e743c CM |
1842 | SetPageChecked(page); |
1843 | page_cache_get(page); | |
1844 | fixup->work.func = btrfs_writepage_fixup_worker; | |
1845 | fixup->page = page; | |
1846 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); | |
87826df0 | 1847 | return -EBUSY; |
247e743c CM |
1848 | } |
1849 | ||
d899e052 YZ |
1850 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
1851 | struct inode *inode, u64 file_pos, | |
1852 | u64 disk_bytenr, u64 disk_num_bytes, | |
1853 | u64 num_bytes, u64 ram_bytes, | |
1854 | u8 compression, u8 encryption, | |
1855 | u16 other_encoding, int extent_type) | |
1856 | { | |
1857 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1858 | struct btrfs_file_extent_item *fi; | |
1859 | struct btrfs_path *path; | |
1860 | struct extent_buffer *leaf; | |
1861 | struct btrfs_key ins; | |
1acae57b | 1862 | int extent_inserted = 0; |
d899e052 YZ |
1863 | int ret; |
1864 | ||
1865 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
1866 | if (!path) |
1867 | return -ENOMEM; | |
d899e052 | 1868 | |
a1ed835e CM |
1869 | /* |
1870 | * we may be replacing one extent in the tree with another. | |
1871 | * The new extent is pinned in the extent map, and we don't want | |
1872 | * to drop it from the cache until it is completely in the btree. | |
1873 | * | |
1874 | * So, tell btrfs_drop_extents to leave this extent in the cache. | |
1875 | * the caller is expected to unpin it and allow it to be merged | |
1876 | * with the others. | |
1877 | */ | |
1acae57b FDBM |
1878 | ret = __btrfs_drop_extents(trans, root, inode, path, file_pos, |
1879 | file_pos + num_bytes, NULL, 0, | |
1880 | 1, sizeof(*fi), &extent_inserted); | |
79787eaa JM |
1881 | if (ret) |
1882 | goto out; | |
d899e052 | 1883 | |
1acae57b FDBM |
1884 | if (!extent_inserted) { |
1885 | ins.objectid = btrfs_ino(inode); | |
1886 | ins.offset = file_pos; | |
1887 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
1888 | ||
1889 | path->leave_spinning = 1; | |
1890 | ret = btrfs_insert_empty_item(trans, root, path, &ins, | |
1891 | sizeof(*fi)); | |
1892 | if (ret) | |
1893 | goto out; | |
1894 | } | |
d899e052 YZ |
1895 | leaf = path->nodes[0]; |
1896 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1897 | struct btrfs_file_extent_item); | |
1898 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
1899 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
1900 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
1901 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
1902 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
1903 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
1904 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
1905 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
1906 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
1907 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
b9473439 | 1908 | |
d899e052 | 1909 | btrfs_mark_buffer_dirty(leaf); |
ce195332 | 1910 | btrfs_release_path(path); |
d899e052 YZ |
1911 | |
1912 | inode_add_bytes(inode, num_bytes); | |
d899e052 YZ |
1913 | |
1914 | ins.objectid = disk_bytenr; | |
1915 | ins.offset = disk_num_bytes; | |
1916 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
5d4f98a2 YZ |
1917 | ret = btrfs_alloc_reserved_file_extent(trans, root, |
1918 | root->root_key.objectid, | |
33345d01 | 1919 | btrfs_ino(inode), file_pos, &ins); |
79787eaa | 1920 | out: |
d899e052 | 1921 | btrfs_free_path(path); |
b9473439 | 1922 | |
79787eaa | 1923 | return ret; |
d899e052 YZ |
1924 | } |
1925 | ||
38c227d8 LB |
1926 | /* snapshot-aware defrag */ |
1927 | struct sa_defrag_extent_backref { | |
1928 | struct rb_node node; | |
1929 | struct old_sa_defrag_extent *old; | |
1930 | u64 root_id; | |
1931 | u64 inum; | |
1932 | u64 file_pos; | |
1933 | u64 extent_offset; | |
1934 | u64 num_bytes; | |
1935 | u64 generation; | |
1936 | }; | |
1937 | ||
1938 | struct old_sa_defrag_extent { | |
1939 | struct list_head list; | |
1940 | struct new_sa_defrag_extent *new; | |
1941 | ||
1942 | u64 extent_offset; | |
1943 | u64 bytenr; | |
1944 | u64 offset; | |
1945 | u64 len; | |
1946 | int count; | |
1947 | }; | |
1948 | ||
1949 | struct new_sa_defrag_extent { | |
1950 | struct rb_root root; | |
1951 | struct list_head head; | |
1952 | struct btrfs_path *path; | |
1953 | struct inode *inode; | |
1954 | u64 file_pos; | |
1955 | u64 len; | |
1956 | u64 bytenr; | |
1957 | u64 disk_len; | |
1958 | u8 compress_type; | |
1959 | }; | |
1960 | ||
1961 | static int backref_comp(struct sa_defrag_extent_backref *b1, | |
1962 | struct sa_defrag_extent_backref *b2) | |
1963 | { | |
1964 | if (b1->root_id < b2->root_id) | |
1965 | return -1; | |
1966 | else if (b1->root_id > b2->root_id) | |
1967 | return 1; | |
1968 | ||
1969 | if (b1->inum < b2->inum) | |
1970 | return -1; | |
1971 | else if (b1->inum > b2->inum) | |
1972 | return 1; | |
1973 | ||
1974 | if (b1->file_pos < b2->file_pos) | |
1975 | return -1; | |
1976 | else if (b1->file_pos > b2->file_pos) | |
1977 | return 1; | |
1978 | ||
1979 | /* | |
1980 | * [------------------------------] ===> (a range of space) | |
1981 | * |<--->| |<---->| =============> (fs/file tree A) | |
1982 | * |<---------------------------->| ===> (fs/file tree B) | |
1983 | * | |
1984 | * A range of space can refer to two file extents in one tree while | |
1985 | * refer to only one file extent in another tree. | |
1986 | * | |
1987 | * So we may process a disk offset more than one time(two extents in A) | |
1988 | * and locate at the same extent(one extent in B), then insert two same | |
1989 | * backrefs(both refer to the extent in B). | |
1990 | */ | |
1991 | return 0; | |
1992 | } | |
1993 | ||
1994 | static void backref_insert(struct rb_root *root, | |
1995 | struct sa_defrag_extent_backref *backref) | |
1996 | { | |
1997 | struct rb_node **p = &root->rb_node; | |
1998 | struct rb_node *parent = NULL; | |
1999 | struct sa_defrag_extent_backref *entry; | |
2000 | int ret; | |
2001 | ||
2002 | while (*p) { | |
2003 | parent = *p; | |
2004 | entry = rb_entry(parent, struct sa_defrag_extent_backref, node); | |
2005 | ||
2006 | ret = backref_comp(backref, entry); | |
2007 | if (ret < 0) | |
2008 | p = &(*p)->rb_left; | |
2009 | else | |
2010 | p = &(*p)->rb_right; | |
2011 | } | |
2012 | ||
2013 | rb_link_node(&backref->node, parent, p); | |
2014 | rb_insert_color(&backref->node, root); | |
2015 | } | |
2016 | ||
2017 | /* | |
2018 | * Note the backref might has changed, and in this case we just return 0. | |
2019 | */ | |
2020 | static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, | |
2021 | void *ctx) | |
2022 | { | |
2023 | struct btrfs_file_extent_item *extent; | |
2024 | struct btrfs_fs_info *fs_info; | |
2025 | struct old_sa_defrag_extent *old = ctx; | |
2026 | struct new_sa_defrag_extent *new = old->new; | |
2027 | struct btrfs_path *path = new->path; | |
2028 | struct btrfs_key key; | |
2029 | struct btrfs_root *root; | |
2030 | struct sa_defrag_extent_backref *backref; | |
2031 | struct extent_buffer *leaf; | |
2032 | struct inode *inode = new->inode; | |
2033 | int slot; | |
2034 | int ret; | |
2035 | u64 extent_offset; | |
2036 | u64 num_bytes; | |
2037 | ||
2038 | if (BTRFS_I(inode)->root->root_key.objectid == root_id && | |
2039 | inum == btrfs_ino(inode)) | |
2040 | return 0; | |
2041 | ||
2042 | key.objectid = root_id; | |
2043 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2044 | key.offset = (u64)-1; | |
2045 | ||
2046 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2047 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2048 | if (IS_ERR(root)) { | |
2049 | if (PTR_ERR(root) == -ENOENT) | |
2050 | return 0; | |
2051 | WARN_ON(1); | |
2052 | pr_debug("inum=%llu, offset=%llu, root_id=%llu\n", | |
2053 | inum, offset, root_id); | |
2054 | return PTR_ERR(root); | |
2055 | } | |
2056 | ||
2057 | key.objectid = inum; | |
2058 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2059 | if (offset > (u64)-1 << 32) | |
2060 | key.offset = 0; | |
2061 | else | |
2062 | key.offset = offset; | |
2063 | ||
2064 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
fae7f21c | 2065 | if (WARN_ON(ret < 0)) |
38c227d8 | 2066 | return ret; |
50f1319c | 2067 | ret = 0; |
38c227d8 LB |
2068 | |
2069 | while (1) { | |
2070 | cond_resched(); | |
2071 | ||
2072 | leaf = path->nodes[0]; | |
2073 | slot = path->slots[0]; | |
2074 | ||
2075 | if (slot >= btrfs_header_nritems(leaf)) { | |
2076 | ret = btrfs_next_leaf(root, path); | |
2077 | if (ret < 0) { | |
2078 | goto out; | |
2079 | } else if (ret > 0) { | |
2080 | ret = 0; | |
2081 | goto out; | |
2082 | } | |
2083 | continue; | |
2084 | } | |
2085 | ||
2086 | path->slots[0]++; | |
2087 | ||
2088 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2089 | ||
2090 | if (key.objectid > inum) | |
2091 | goto out; | |
2092 | ||
2093 | if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) | |
2094 | continue; | |
2095 | ||
2096 | extent = btrfs_item_ptr(leaf, slot, | |
2097 | struct btrfs_file_extent_item); | |
2098 | ||
2099 | if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) | |
2100 | continue; | |
2101 | ||
e68afa49 LB |
2102 | /* |
2103 | * 'offset' refers to the exact key.offset, | |
2104 | * NOT the 'offset' field in btrfs_extent_data_ref, ie. | |
2105 | * (key.offset - extent_offset). | |
2106 | */ | |
2107 | if (key.offset != offset) | |
38c227d8 LB |
2108 | continue; |
2109 | ||
e68afa49 | 2110 | extent_offset = btrfs_file_extent_offset(leaf, extent); |
38c227d8 | 2111 | num_bytes = btrfs_file_extent_num_bytes(leaf, extent); |
e68afa49 | 2112 | |
38c227d8 LB |
2113 | if (extent_offset >= old->extent_offset + old->offset + |
2114 | old->len || extent_offset + num_bytes <= | |
2115 | old->extent_offset + old->offset) | |
2116 | continue; | |
38c227d8 LB |
2117 | break; |
2118 | } | |
2119 | ||
2120 | backref = kmalloc(sizeof(*backref), GFP_NOFS); | |
2121 | if (!backref) { | |
2122 | ret = -ENOENT; | |
2123 | goto out; | |
2124 | } | |
2125 | ||
2126 | backref->root_id = root_id; | |
2127 | backref->inum = inum; | |
e68afa49 | 2128 | backref->file_pos = offset; |
38c227d8 LB |
2129 | backref->num_bytes = num_bytes; |
2130 | backref->extent_offset = extent_offset; | |
2131 | backref->generation = btrfs_file_extent_generation(leaf, extent); | |
2132 | backref->old = old; | |
2133 | backref_insert(&new->root, backref); | |
2134 | old->count++; | |
2135 | out: | |
2136 | btrfs_release_path(path); | |
2137 | WARN_ON(ret); | |
2138 | return ret; | |
2139 | } | |
2140 | ||
2141 | static noinline bool record_extent_backrefs(struct btrfs_path *path, | |
2142 | struct new_sa_defrag_extent *new) | |
2143 | { | |
2144 | struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; | |
2145 | struct old_sa_defrag_extent *old, *tmp; | |
2146 | int ret; | |
2147 | ||
2148 | new->path = path; | |
2149 | ||
2150 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
e68afa49 LB |
2151 | ret = iterate_inodes_from_logical(old->bytenr + |
2152 | old->extent_offset, fs_info, | |
38c227d8 LB |
2153 | path, record_one_backref, |
2154 | old); | |
4724b106 JB |
2155 | if (ret < 0 && ret != -ENOENT) |
2156 | return false; | |
38c227d8 LB |
2157 | |
2158 | /* no backref to be processed for this extent */ | |
2159 | if (!old->count) { | |
2160 | list_del(&old->list); | |
2161 | kfree(old); | |
2162 | } | |
2163 | } | |
2164 | ||
2165 | if (list_empty(&new->head)) | |
2166 | return false; | |
2167 | ||
2168 | return true; | |
2169 | } | |
2170 | ||
2171 | static int relink_is_mergable(struct extent_buffer *leaf, | |
2172 | struct btrfs_file_extent_item *fi, | |
116e0024 | 2173 | struct new_sa_defrag_extent *new) |
38c227d8 | 2174 | { |
116e0024 | 2175 | if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) |
38c227d8 LB |
2176 | return 0; |
2177 | ||
2178 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) | |
2179 | return 0; | |
2180 | ||
116e0024 LB |
2181 | if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) |
2182 | return 0; | |
2183 | ||
2184 | if (btrfs_file_extent_encryption(leaf, fi) || | |
38c227d8 LB |
2185 | btrfs_file_extent_other_encoding(leaf, fi)) |
2186 | return 0; | |
2187 | ||
2188 | return 1; | |
2189 | } | |
2190 | ||
2191 | /* | |
2192 | * Note the backref might has changed, and in this case we just return 0. | |
2193 | */ | |
2194 | static noinline int relink_extent_backref(struct btrfs_path *path, | |
2195 | struct sa_defrag_extent_backref *prev, | |
2196 | struct sa_defrag_extent_backref *backref) | |
2197 | { | |
2198 | struct btrfs_file_extent_item *extent; | |
2199 | struct btrfs_file_extent_item *item; | |
2200 | struct btrfs_ordered_extent *ordered; | |
2201 | struct btrfs_trans_handle *trans; | |
2202 | struct btrfs_fs_info *fs_info; | |
2203 | struct btrfs_root *root; | |
2204 | struct btrfs_key key; | |
2205 | struct extent_buffer *leaf; | |
2206 | struct old_sa_defrag_extent *old = backref->old; | |
2207 | struct new_sa_defrag_extent *new = old->new; | |
2208 | struct inode *src_inode = new->inode; | |
2209 | struct inode *inode; | |
2210 | struct extent_state *cached = NULL; | |
2211 | int ret = 0; | |
2212 | u64 start; | |
2213 | u64 len; | |
2214 | u64 lock_start; | |
2215 | u64 lock_end; | |
2216 | bool merge = false; | |
2217 | int index; | |
2218 | ||
2219 | if (prev && prev->root_id == backref->root_id && | |
2220 | prev->inum == backref->inum && | |
2221 | prev->file_pos + prev->num_bytes == backref->file_pos) | |
2222 | merge = true; | |
2223 | ||
2224 | /* step 1: get root */ | |
2225 | key.objectid = backref->root_id; | |
2226 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2227 | key.offset = (u64)-1; | |
2228 | ||
2229 | fs_info = BTRFS_I(src_inode)->root->fs_info; | |
2230 | index = srcu_read_lock(&fs_info->subvol_srcu); | |
2231 | ||
2232 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2233 | if (IS_ERR(root)) { | |
2234 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2235 | if (PTR_ERR(root) == -ENOENT) | |
2236 | return 0; | |
2237 | return PTR_ERR(root); | |
2238 | } | |
38c227d8 LB |
2239 | |
2240 | /* step 2: get inode */ | |
2241 | key.objectid = backref->inum; | |
2242 | key.type = BTRFS_INODE_ITEM_KEY; | |
2243 | key.offset = 0; | |
2244 | ||
2245 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); | |
2246 | if (IS_ERR(inode)) { | |
2247 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2248 | return 0; | |
2249 | } | |
2250 | ||
2251 | srcu_read_unlock(&fs_info->subvol_srcu, index); | |
2252 | ||
2253 | /* step 3: relink backref */ | |
2254 | lock_start = backref->file_pos; | |
2255 | lock_end = backref->file_pos + backref->num_bytes - 1; | |
2256 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2257 | 0, &cached); | |
2258 | ||
2259 | ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); | |
2260 | if (ordered) { | |
2261 | btrfs_put_ordered_extent(ordered); | |
2262 | goto out_unlock; | |
2263 | } | |
2264 | ||
2265 | trans = btrfs_join_transaction(root); | |
2266 | if (IS_ERR(trans)) { | |
2267 | ret = PTR_ERR(trans); | |
2268 | goto out_unlock; | |
2269 | } | |
2270 | ||
2271 | key.objectid = backref->inum; | |
2272 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2273 | key.offset = backref->file_pos; | |
2274 | ||
2275 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2276 | if (ret < 0) { | |
2277 | goto out_free_path; | |
2278 | } else if (ret > 0) { | |
2279 | ret = 0; | |
2280 | goto out_free_path; | |
2281 | } | |
2282 | ||
2283 | extent = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
2284 | struct btrfs_file_extent_item); | |
2285 | ||
2286 | if (btrfs_file_extent_generation(path->nodes[0], extent) != | |
2287 | backref->generation) | |
2288 | goto out_free_path; | |
2289 | ||
2290 | btrfs_release_path(path); | |
2291 | ||
2292 | start = backref->file_pos; | |
2293 | if (backref->extent_offset < old->extent_offset + old->offset) | |
2294 | start += old->extent_offset + old->offset - | |
2295 | backref->extent_offset; | |
2296 | ||
2297 | len = min(backref->extent_offset + backref->num_bytes, | |
2298 | old->extent_offset + old->offset + old->len); | |
2299 | len -= max(backref->extent_offset, old->extent_offset + old->offset); | |
2300 | ||
2301 | ret = btrfs_drop_extents(trans, root, inode, start, | |
2302 | start + len, 1); | |
2303 | if (ret) | |
2304 | goto out_free_path; | |
2305 | again: | |
2306 | key.objectid = btrfs_ino(inode); | |
2307 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2308 | key.offset = start; | |
2309 | ||
a09a0a70 | 2310 | path->leave_spinning = 1; |
38c227d8 LB |
2311 | if (merge) { |
2312 | struct btrfs_file_extent_item *fi; | |
2313 | u64 extent_len; | |
2314 | struct btrfs_key found_key; | |
2315 | ||
2316 | ret = btrfs_search_slot(trans, root, &key, path, 1, 1); | |
2317 | if (ret < 0) | |
2318 | goto out_free_path; | |
2319 | ||
2320 | path->slots[0]--; | |
2321 | leaf = path->nodes[0]; | |
2322 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2323 | ||
2324 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
2325 | struct btrfs_file_extent_item); | |
2326 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); | |
2327 | ||
116e0024 LB |
2328 | if (extent_len + found_key.offset == start && |
2329 | relink_is_mergable(leaf, fi, new)) { | |
38c227d8 LB |
2330 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2331 | extent_len + len); | |
2332 | btrfs_mark_buffer_dirty(leaf); | |
2333 | inode_add_bytes(inode, len); | |
2334 | ||
2335 | ret = 1; | |
2336 | goto out_free_path; | |
2337 | } else { | |
2338 | merge = false; | |
2339 | btrfs_release_path(path); | |
2340 | goto again; | |
2341 | } | |
2342 | } | |
2343 | ||
2344 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
2345 | sizeof(*extent)); | |
2346 | if (ret) { | |
2347 | btrfs_abort_transaction(trans, root, ret); | |
2348 | goto out_free_path; | |
2349 | } | |
2350 | ||
2351 | leaf = path->nodes[0]; | |
2352 | item = btrfs_item_ptr(leaf, path->slots[0], | |
2353 | struct btrfs_file_extent_item); | |
2354 | btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); | |
2355 | btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); | |
2356 | btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); | |
2357 | btrfs_set_file_extent_num_bytes(leaf, item, len); | |
2358 | btrfs_set_file_extent_ram_bytes(leaf, item, new->len); | |
2359 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | |
2360 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
2361 | btrfs_set_file_extent_compression(leaf, item, new->compress_type); | |
2362 | btrfs_set_file_extent_encryption(leaf, item, 0); | |
2363 | btrfs_set_file_extent_other_encoding(leaf, item, 0); | |
2364 | ||
2365 | btrfs_mark_buffer_dirty(leaf); | |
2366 | inode_add_bytes(inode, len); | |
a09a0a70 | 2367 | btrfs_release_path(path); |
38c227d8 LB |
2368 | |
2369 | ret = btrfs_inc_extent_ref(trans, root, new->bytenr, | |
2370 | new->disk_len, 0, | |
2371 | backref->root_id, backref->inum, | |
2372 | new->file_pos, 0); /* start - extent_offset */ | |
2373 | if (ret) { | |
2374 | btrfs_abort_transaction(trans, root, ret); | |
2375 | goto out_free_path; | |
2376 | } | |
2377 | ||
2378 | ret = 1; | |
2379 | out_free_path: | |
2380 | btrfs_release_path(path); | |
a09a0a70 | 2381 | path->leave_spinning = 0; |
38c227d8 LB |
2382 | btrfs_end_transaction(trans, root); |
2383 | out_unlock: | |
2384 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, | |
2385 | &cached, GFP_NOFS); | |
2386 | iput(inode); | |
2387 | return ret; | |
2388 | } | |
2389 | ||
6f519564 LB |
2390 | static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) |
2391 | { | |
2392 | struct old_sa_defrag_extent *old, *tmp; | |
2393 | ||
2394 | if (!new) | |
2395 | return; | |
2396 | ||
2397 | list_for_each_entry_safe(old, tmp, &new->head, list) { | |
2398 | list_del(&old->list); | |
2399 | kfree(old); | |
2400 | } | |
2401 | kfree(new); | |
2402 | } | |
2403 | ||
38c227d8 LB |
2404 | static void relink_file_extents(struct new_sa_defrag_extent *new) |
2405 | { | |
2406 | struct btrfs_path *path; | |
38c227d8 LB |
2407 | struct sa_defrag_extent_backref *backref; |
2408 | struct sa_defrag_extent_backref *prev = NULL; | |
2409 | struct inode *inode; | |
2410 | struct btrfs_root *root; | |
2411 | struct rb_node *node; | |
2412 | int ret; | |
2413 | ||
2414 | inode = new->inode; | |
2415 | root = BTRFS_I(inode)->root; | |
2416 | ||
2417 | path = btrfs_alloc_path(); | |
2418 | if (!path) | |
2419 | return; | |
2420 | ||
2421 | if (!record_extent_backrefs(path, new)) { | |
2422 | btrfs_free_path(path); | |
2423 | goto out; | |
2424 | } | |
2425 | btrfs_release_path(path); | |
2426 | ||
2427 | while (1) { | |
2428 | node = rb_first(&new->root); | |
2429 | if (!node) | |
2430 | break; | |
2431 | rb_erase(node, &new->root); | |
2432 | ||
2433 | backref = rb_entry(node, struct sa_defrag_extent_backref, node); | |
2434 | ||
2435 | ret = relink_extent_backref(path, prev, backref); | |
2436 | WARN_ON(ret < 0); | |
2437 | ||
2438 | kfree(prev); | |
2439 | ||
2440 | if (ret == 1) | |
2441 | prev = backref; | |
2442 | else | |
2443 | prev = NULL; | |
2444 | cond_resched(); | |
2445 | } | |
2446 | kfree(prev); | |
2447 | ||
2448 | btrfs_free_path(path); | |
38c227d8 | 2449 | out: |
6f519564 LB |
2450 | free_sa_defrag_extent(new); |
2451 | ||
38c227d8 LB |
2452 | atomic_dec(&root->fs_info->defrag_running); |
2453 | wake_up(&root->fs_info->transaction_wait); | |
38c227d8 LB |
2454 | } |
2455 | ||
2456 | static struct new_sa_defrag_extent * | |
2457 | record_old_file_extents(struct inode *inode, | |
2458 | struct btrfs_ordered_extent *ordered) | |
2459 | { | |
2460 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2461 | struct btrfs_path *path; | |
2462 | struct btrfs_key key; | |
6f519564 | 2463 | struct old_sa_defrag_extent *old; |
38c227d8 LB |
2464 | struct new_sa_defrag_extent *new; |
2465 | int ret; | |
2466 | ||
2467 | new = kmalloc(sizeof(*new), GFP_NOFS); | |
2468 | if (!new) | |
2469 | return NULL; | |
2470 | ||
2471 | new->inode = inode; | |
2472 | new->file_pos = ordered->file_offset; | |
2473 | new->len = ordered->len; | |
2474 | new->bytenr = ordered->start; | |
2475 | new->disk_len = ordered->disk_len; | |
2476 | new->compress_type = ordered->compress_type; | |
2477 | new->root = RB_ROOT; | |
2478 | INIT_LIST_HEAD(&new->head); | |
2479 | ||
2480 | path = btrfs_alloc_path(); | |
2481 | if (!path) | |
2482 | goto out_kfree; | |
2483 | ||
2484 | key.objectid = btrfs_ino(inode); | |
2485 | key.type = BTRFS_EXTENT_DATA_KEY; | |
2486 | key.offset = new->file_pos; | |
2487 | ||
2488 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2489 | if (ret < 0) | |
2490 | goto out_free_path; | |
2491 | if (ret > 0 && path->slots[0] > 0) | |
2492 | path->slots[0]--; | |
2493 | ||
2494 | /* find out all the old extents for the file range */ | |
2495 | while (1) { | |
2496 | struct btrfs_file_extent_item *extent; | |
2497 | struct extent_buffer *l; | |
2498 | int slot; | |
2499 | u64 num_bytes; | |
2500 | u64 offset; | |
2501 | u64 end; | |
2502 | u64 disk_bytenr; | |
2503 | u64 extent_offset; | |
2504 | ||
2505 | l = path->nodes[0]; | |
2506 | slot = path->slots[0]; | |
2507 | ||
2508 | if (slot >= btrfs_header_nritems(l)) { | |
2509 | ret = btrfs_next_leaf(root, path); | |
2510 | if (ret < 0) | |
6f519564 | 2511 | goto out_free_path; |
38c227d8 LB |
2512 | else if (ret > 0) |
2513 | break; | |
2514 | continue; | |
2515 | } | |
2516 | ||
2517 | btrfs_item_key_to_cpu(l, &key, slot); | |
2518 | ||
2519 | if (key.objectid != btrfs_ino(inode)) | |
2520 | break; | |
2521 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
2522 | break; | |
2523 | if (key.offset >= new->file_pos + new->len) | |
2524 | break; | |
2525 | ||
2526 | extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); | |
2527 | ||
2528 | num_bytes = btrfs_file_extent_num_bytes(l, extent); | |
2529 | if (key.offset + num_bytes < new->file_pos) | |
2530 | goto next; | |
2531 | ||
2532 | disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); | |
2533 | if (!disk_bytenr) | |
2534 | goto next; | |
2535 | ||
2536 | extent_offset = btrfs_file_extent_offset(l, extent); | |
2537 | ||
2538 | old = kmalloc(sizeof(*old), GFP_NOFS); | |
2539 | if (!old) | |
6f519564 | 2540 | goto out_free_path; |
38c227d8 LB |
2541 | |
2542 | offset = max(new->file_pos, key.offset); | |
2543 | end = min(new->file_pos + new->len, key.offset + num_bytes); | |
2544 | ||
2545 | old->bytenr = disk_bytenr; | |
2546 | old->extent_offset = extent_offset; | |
2547 | old->offset = offset - key.offset; | |
2548 | old->len = end - offset; | |
2549 | old->new = new; | |
2550 | old->count = 0; | |
2551 | list_add_tail(&old->list, &new->head); | |
2552 | next: | |
2553 | path->slots[0]++; | |
2554 | cond_resched(); | |
2555 | } | |
2556 | ||
2557 | btrfs_free_path(path); | |
2558 | atomic_inc(&root->fs_info->defrag_running); | |
2559 | ||
2560 | return new; | |
2561 | ||
38c227d8 LB |
2562 | out_free_path: |
2563 | btrfs_free_path(path); | |
2564 | out_kfree: | |
6f519564 | 2565 | free_sa_defrag_extent(new); |
38c227d8 LB |
2566 | return NULL; |
2567 | } | |
2568 | ||
d352ac68 CM |
2569 | /* as ordered data IO finishes, this gets called so we can finish |
2570 | * an ordered extent if the range of bytes in the file it covers are | |
2571 | * fully written. | |
2572 | */ | |
5fd02043 | 2573 | static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) |
e6dcd2dc | 2574 | { |
5fd02043 | 2575 | struct inode *inode = ordered_extent->inode; |
e6dcd2dc | 2576 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0ca1f7ce | 2577 | struct btrfs_trans_handle *trans = NULL; |
e6dcd2dc | 2578 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2ac55d41 | 2579 | struct extent_state *cached_state = NULL; |
38c227d8 | 2580 | struct new_sa_defrag_extent *new = NULL; |
261507a0 | 2581 | int compress_type = 0; |
77cef2ec JB |
2582 | int ret = 0; |
2583 | u64 logical_len = ordered_extent->len; | |
82d5902d | 2584 | bool nolock; |
77cef2ec | 2585 | bool truncated = false; |
e6dcd2dc | 2586 | |
83eea1f1 | 2587 | nolock = btrfs_is_free_space_inode(inode); |
0cb59c99 | 2588 | |
5fd02043 JB |
2589 | if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) { |
2590 | ret = -EIO; | |
2591 | goto out; | |
2592 | } | |
2593 | ||
77cef2ec JB |
2594 | if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) { |
2595 | truncated = true; | |
2596 | logical_len = ordered_extent->truncated_len; | |
2597 | /* Truncated the entire extent, don't bother adding */ | |
2598 | if (!logical_len) | |
2599 | goto out; | |
2600 | } | |
2601 | ||
c2167754 | 2602 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { |
79787eaa | 2603 | BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ |
6c760c07 JB |
2604 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2605 | if (nolock) | |
2606 | trans = btrfs_join_transaction_nolock(root); | |
2607 | else | |
2608 | trans = btrfs_join_transaction(root); | |
2609 | if (IS_ERR(trans)) { | |
2610 | ret = PTR_ERR(trans); | |
2611 | trans = NULL; | |
2612 | goto out; | |
c2167754 | 2613 | } |
6c760c07 JB |
2614 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
2615 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2616 | if (ret) /* -ENOMEM or corruption */ | |
2617 | btrfs_abort_transaction(trans, root, ret); | |
c2167754 YZ |
2618 | goto out; |
2619 | } | |
e6dcd2dc | 2620 | |
2ac55d41 JB |
2621 | lock_extent_bits(io_tree, ordered_extent->file_offset, |
2622 | ordered_extent->file_offset + ordered_extent->len - 1, | |
d0082371 | 2623 | 0, &cached_state); |
e6dcd2dc | 2624 | |
38c227d8 LB |
2625 | ret = test_range_bit(io_tree, ordered_extent->file_offset, |
2626 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2627 | EXTENT_DEFRAG, 1, cached_state); | |
2628 | if (ret) { | |
2629 | u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); | |
2630 | if (last_snapshot >= BTRFS_I(inode)->generation) | |
2631 | /* the inode is shared */ | |
2632 | new = record_old_file_extents(inode, ordered_extent); | |
2633 | ||
2634 | clear_extent_bit(io_tree, ordered_extent->file_offset, | |
2635 | ordered_extent->file_offset + ordered_extent->len - 1, | |
2636 | EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS); | |
2637 | } | |
2638 | ||
0cb59c99 | 2639 | if (nolock) |
7a7eaa40 | 2640 | trans = btrfs_join_transaction_nolock(root); |
0cb59c99 | 2641 | else |
7a7eaa40 | 2642 | trans = btrfs_join_transaction(root); |
79787eaa JM |
2643 | if (IS_ERR(trans)) { |
2644 | ret = PTR_ERR(trans); | |
2645 | trans = NULL; | |
2646 | goto out_unlock; | |
2647 | } | |
0ca1f7ce | 2648 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
c2167754 | 2649 | |
c8b97818 | 2650 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
261507a0 | 2651 | compress_type = ordered_extent->compress_type; |
d899e052 | 2652 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { |
261507a0 | 2653 | BUG_ON(compress_type); |
920bbbfb | 2654 | ret = btrfs_mark_extent_written(trans, inode, |
d899e052 YZ |
2655 | ordered_extent->file_offset, |
2656 | ordered_extent->file_offset + | |
77cef2ec | 2657 | logical_len); |
d899e052 | 2658 | } else { |
0af3d00b | 2659 | BUG_ON(root == root->fs_info->tree_root); |
d899e052 YZ |
2660 | ret = insert_reserved_file_extent(trans, inode, |
2661 | ordered_extent->file_offset, | |
2662 | ordered_extent->start, | |
2663 | ordered_extent->disk_len, | |
77cef2ec | 2664 | logical_len, logical_len, |
261507a0 | 2665 | compress_type, 0, 0, |
d899e052 | 2666 | BTRFS_FILE_EXTENT_REG); |
d899e052 | 2667 | } |
5dc562c5 JB |
2668 | unpin_extent_cache(&BTRFS_I(inode)->extent_tree, |
2669 | ordered_extent->file_offset, ordered_extent->len, | |
2670 | trans->transid); | |
79787eaa JM |
2671 | if (ret < 0) { |
2672 | btrfs_abort_transaction(trans, root, ret); | |
5fd02043 | 2673 | goto out_unlock; |
79787eaa | 2674 | } |
2ac55d41 | 2675 | |
e6dcd2dc CM |
2676 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
2677 | &ordered_extent->list); | |
2678 | ||
6c760c07 JB |
2679 | btrfs_ordered_update_i_size(inode, 0, ordered_extent); |
2680 | ret = btrfs_update_inode_fallback(trans, root, inode); | |
2681 | if (ret) { /* -ENOMEM or corruption */ | |
2682 | btrfs_abort_transaction(trans, root, ret); | |
2683 | goto out_unlock; | |
1ef30be1 JB |
2684 | } |
2685 | ret = 0; | |
5fd02043 JB |
2686 | out_unlock: |
2687 | unlock_extent_cached(io_tree, ordered_extent->file_offset, | |
2688 | ordered_extent->file_offset + | |
2689 | ordered_extent->len - 1, &cached_state, GFP_NOFS); | |
c2167754 | 2690 | out: |
5b0e95bf | 2691 | if (root != root->fs_info->tree_root) |
0cb59c99 | 2692 | btrfs_delalloc_release_metadata(inode, ordered_extent->len); |
a698d075 MX |
2693 | if (trans) |
2694 | btrfs_end_transaction(trans, root); | |
0cb59c99 | 2695 | |
77cef2ec JB |
2696 | if (ret || truncated) { |
2697 | u64 start, end; | |
2698 | ||
2699 | if (truncated) | |
2700 | start = ordered_extent->file_offset + logical_len; | |
2701 | else | |
2702 | start = ordered_extent->file_offset; | |
2703 | end = ordered_extent->file_offset + ordered_extent->len - 1; | |
2704 | clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS); | |
2705 | ||
2706 | /* Drop the cache for the part of the extent we didn't write. */ | |
2707 | btrfs_drop_extent_cache(inode, start, end, 0); | |
5fd02043 | 2708 | |
0bec9ef5 JB |
2709 | /* |
2710 | * If the ordered extent had an IOERR or something else went | |
2711 | * wrong we need to return the space for this ordered extent | |
77cef2ec JB |
2712 | * back to the allocator. We only free the extent in the |
2713 | * truncated case if we didn't write out the extent at all. | |
0bec9ef5 | 2714 | */ |
77cef2ec JB |
2715 | if ((ret || !logical_len) && |
2716 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && | |
0bec9ef5 JB |
2717 | !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) |
2718 | btrfs_free_reserved_extent(root, ordered_extent->start, | |
2719 | ordered_extent->disk_len); | |
2720 | } | |
2721 | ||
2722 | ||
5fd02043 | 2723 | /* |
8bad3c02 LB |
2724 | * This needs to be done to make sure anybody waiting knows we are done |
2725 | * updating everything for this ordered extent. | |
5fd02043 JB |
2726 | */ |
2727 | btrfs_remove_ordered_extent(inode, ordered_extent); | |
2728 | ||
38c227d8 | 2729 | /* for snapshot-aware defrag */ |
6f519564 LB |
2730 | if (new) { |
2731 | if (ret) { | |
2732 | free_sa_defrag_extent(new); | |
2733 | atomic_dec(&root->fs_info->defrag_running); | |
2734 | } else { | |
2735 | relink_file_extents(new); | |
2736 | } | |
2737 | } | |
38c227d8 | 2738 | |
e6dcd2dc CM |
2739 | /* once for us */ |
2740 | btrfs_put_ordered_extent(ordered_extent); | |
2741 | /* once for the tree */ | |
2742 | btrfs_put_ordered_extent(ordered_extent); | |
2743 | ||
5fd02043 JB |
2744 | return ret; |
2745 | } | |
2746 | ||
2747 | static void finish_ordered_fn(struct btrfs_work *work) | |
2748 | { | |
2749 | struct btrfs_ordered_extent *ordered_extent; | |
2750 | ordered_extent = container_of(work, struct btrfs_ordered_extent, work); | |
2751 | btrfs_finish_ordered_io(ordered_extent); | |
e6dcd2dc CM |
2752 | } |
2753 | ||
b2950863 | 2754 | static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
211f90e6 CM |
2755 | struct extent_state *state, int uptodate) |
2756 | { | |
5fd02043 JB |
2757 | struct inode *inode = page->mapping->host; |
2758 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2759 | struct btrfs_ordered_extent *ordered_extent = NULL; | |
2760 | struct btrfs_workers *workers; | |
2761 | ||
1abe9b8a | 2762 | trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); |
2763 | ||
8b62b72b | 2764 | ClearPagePrivate2(page); |
5fd02043 JB |
2765 | if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, |
2766 | end - start + 1, uptodate)) | |
2767 | return 0; | |
2768 | ||
2769 | ordered_extent->work.func = finish_ordered_fn; | |
2770 | ordered_extent->work.flags = 0; | |
2771 | ||
83eea1f1 | 2772 | if (btrfs_is_free_space_inode(inode)) |
5fd02043 JB |
2773 | workers = &root->fs_info->endio_freespace_worker; |
2774 | else | |
2775 | workers = &root->fs_info->endio_write_workers; | |
2776 | btrfs_queue_worker(workers, &ordered_extent->work); | |
2777 | ||
2778 | return 0; | |
211f90e6 CM |
2779 | } |
2780 | ||
d352ac68 CM |
2781 | /* |
2782 | * when reads are done, we need to check csums to verify the data is correct | |
4a54c8c1 JS |
2783 | * if there's a match, we allow the bio to finish. If not, the code in |
2784 | * extent_io.c will try to find good copies for us. | |
d352ac68 | 2785 | */ |
facc8a22 MX |
2786 | static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio, |
2787 | u64 phy_offset, struct page *page, | |
2788 | u64 start, u64 end, int mirror) | |
07157aac | 2789 | { |
4eee4fa4 | 2790 | size_t offset = start - page_offset(page); |
07157aac | 2791 | struct inode *inode = page->mapping->host; |
d1310b2e | 2792 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
07157aac | 2793 | char *kaddr; |
ff79f819 | 2794 | struct btrfs_root *root = BTRFS_I(inode)->root; |
facc8a22 | 2795 | u32 csum_expected; |
ff79f819 | 2796 | u32 csum = ~(u32)0; |
c2cf52eb SK |
2797 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
2798 | DEFAULT_RATELIMIT_BURST); | |
d1310b2e | 2799 | |
d20f7043 CM |
2800 | if (PageChecked(page)) { |
2801 | ClearPageChecked(page); | |
2802 | goto good; | |
2803 | } | |
6cbff00f CH |
2804 | |
2805 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) | |
08d2f347 | 2806 | goto good; |
17d217fe YZ |
2807 | |
2808 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && | |
9655d298 | 2809 | test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) { |
17d217fe YZ |
2810 | clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, |
2811 | GFP_NOFS); | |
b6cda9bc | 2812 | return 0; |
17d217fe | 2813 | } |
d20f7043 | 2814 | |
facc8a22 MX |
2815 | phy_offset >>= inode->i_sb->s_blocksize_bits; |
2816 | csum_expected = *(((u32 *)io_bio->csum) + phy_offset); | |
d397712b | 2817 | |
facc8a22 | 2818 | kaddr = kmap_atomic(page); |
b0496686 | 2819 | csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1); |
ff79f819 | 2820 | btrfs_csum_final(csum, (char *)&csum); |
facc8a22 | 2821 | if (csum != csum_expected) |
07157aac | 2822 | goto zeroit; |
d397712b | 2823 | |
7ac687d9 | 2824 | kunmap_atomic(kaddr); |
d20f7043 | 2825 | good: |
07157aac CM |
2826 | return 0; |
2827 | ||
2828 | zeroit: | |
c2cf52eb | 2829 | if (__ratelimit(&_rs)) |
facc8a22 | 2830 | btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", |
c1c9ff7c | 2831 | btrfs_ino(page->mapping->host), start, csum, csum_expected); |
db94535d CM |
2832 | memset(kaddr + offset, 1, end - start + 1); |
2833 | flush_dcache_page(page); | |
7ac687d9 | 2834 | kunmap_atomic(kaddr); |
facc8a22 | 2835 | if (csum_expected == 0) |
3b951516 | 2836 | return 0; |
7e38326f | 2837 | return -EIO; |
07157aac | 2838 | } |
b888db2b | 2839 | |
24bbcf04 YZ |
2840 | struct delayed_iput { |
2841 | struct list_head list; | |
2842 | struct inode *inode; | |
2843 | }; | |
2844 | ||
79787eaa JM |
2845 | /* JDM: If this is fs-wide, why can't we add a pointer to |
2846 | * btrfs_inode instead and avoid the allocation? */ | |
24bbcf04 YZ |
2847 | void btrfs_add_delayed_iput(struct inode *inode) |
2848 | { | |
2849 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
2850 | struct delayed_iput *delayed; | |
2851 | ||
2852 | if (atomic_add_unless(&inode->i_count, -1, 1)) | |
2853 | return; | |
2854 | ||
2855 | delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL); | |
2856 | delayed->inode = inode; | |
2857 | ||
2858 | spin_lock(&fs_info->delayed_iput_lock); | |
2859 | list_add_tail(&delayed->list, &fs_info->delayed_iputs); | |
2860 | spin_unlock(&fs_info->delayed_iput_lock); | |
2861 | } | |
2862 | ||
2863 | void btrfs_run_delayed_iputs(struct btrfs_root *root) | |
2864 | { | |
2865 | LIST_HEAD(list); | |
2866 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2867 | struct delayed_iput *delayed; | |
2868 | int empty; | |
2869 | ||
2870 | spin_lock(&fs_info->delayed_iput_lock); | |
2871 | empty = list_empty(&fs_info->delayed_iputs); | |
2872 | spin_unlock(&fs_info->delayed_iput_lock); | |
2873 | if (empty) | |
2874 | return; | |
2875 | ||
24bbcf04 YZ |
2876 | spin_lock(&fs_info->delayed_iput_lock); |
2877 | list_splice_init(&fs_info->delayed_iputs, &list); | |
2878 | spin_unlock(&fs_info->delayed_iput_lock); | |
2879 | ||
2880 | while (!list_empty(&list)) { | |
2881 | delayed = list_entry(list.next, struct delayed_iput, list); | |
2882 | list_del(&delayed->list); | |
2883 | iput(delayed->inode); | |
2884 | kfree(delayed); | |
2885 | } | |
24bbcf04 YZ |
2886 | } |
2887 | ||
d68fc57b | 2888 | /* |
42b2aa86 | 2889 | * This is called in transaction commit time. If there are no orphan |
d68fc57b YZ |
2890 | * files in the subvolume, it removes orphan item and frees block_rsv |
2891 | * structure. | |
2892 | */ | |
2893 | void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, | |
2894 | struct btrfs_root *root) | |
2895 | { | |
90290e19 | 2896 | struct btrfs_block_rsv *block_rsv; |
d68fc57b YZ |
2897 | int ret; |
2898 | ||
8a35d95f | 2899 | if (atomic_read(&root->orphan_inodes) || |
d68fc57b YZ |
2900 | root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) |
2901 | return; | |
2902 | ||
90290e19 | 2903 | spin_lock(&root->orphan_lock); |
8a35d95f | 2904 | if (atomic_read(&root->orphan_inodes)) { |
90290e19 JB |
2905 | spin_unlock(&root->orphan_lock); |
2906 | return; | |
2907 | } | |
2908 | ||
2909 | if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) { | |
2910 | spin_unlock(&root->orphan_lock); | |
2911 | return; | |
2912 | } | |
2913 | ||
2914 | block_rsv = root->orphan_block_rsv; | |
2915 | root->orphan_block_rsv = NULL; | |
2916 | spin_unlock(&root->orphan_lock); | |
2917 | ||
d68fc57b YZ |
2918 | if (root->orphan_item_inserted && |
2919 | btrfs_root_refs(&root->root_item) > 0) { | |
2920 | ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, | |
2921 | root->root_key.objectid); | |
4ef31a45 JB |
2922 | if (ret) |
2923 | btrfs_abort_transaction(trans, root, ret); | |
2924 | else | |
2925 | root->orphan_item_inserted = 0; | |
d68fc57b YZ |
2926 | } |
2927 | ||
90290e19 JB |
2928 | if (block_rsv) { |
2929 | WARN_ON(block_rsv->size > 0); | |
2930 | btrfs_free_block_rsv(root, block_rsv); | |
d68fc57b YZ |
2931 | } |
2932 | } | |
2933 | ||
7b128766 JB |
2934 | /* |
2935 | * This creates an orphan entry for the given inode in case something goes | |
2936 | * wrong in the middle of an unlink/truncate. | |
d68fc57b YZ |
2937 | * |
2938 | * NOTE: caller of this function should reserve 5 units of metadata for | |
2939 | * this function. | |
7b128766 JB |
2940 | */ |
2941 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
2942 | { | |
2943 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
2944 | struct btrfs_block_rsv *block_rsv = NULL; |
2945 | int reserve = 0; | |
2946 | int insert = 0; | |
2947 | int ret; | |
7b128766 | 2948 | |
d68fc57b | 2949 | if (!root->orphan_block_rsv) { |
66d8f3dd | 2950 | block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
b532402e TI |
2951 | if (!block_rsv) |
2952 | return -ENOMEM; | |
d68fc57b | 2953 | } |
7b128766 | 2954 | |
d68fc57b YZ |
2955 | spin_lock(&root->orphan_lock); |
2956 | if (!root->orphan_block_rsv) { | |
2957 | root->orphan_block_rsv = block_rsv; | |
2958 | } else if (block_rsv) { | |
2959 | btrfs_free_block_rsv(root, block_rsv); | |
2960 | block_rsv = NULL; | |
7b128766 | 2961 | } |
7b128766 | 2962 | |
8a35d95f JB |
2963 | if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
2964 | &BTRFS_I(inode)->runtime_flags)) { | |
d68fc57b YZ |
2965 | #if 0 |
2966 | /* | |
2967 | * For proper ENOSPC handling, we should do orphan | |
2968 | * cleanup when mounting. But this introduces backward | |
2969 | * compatibility issue. | |
2970 | */ | |
2971 | if (!xchg(&root->orphan_item_inserted, 1)) | |
2972 | insert = 2; | |
2973 | else | |
2974 | insert = 1; | |
2975 | #endif | |
2976 | insert = 1; | |
321f0e70 | 2977 | atomic_inc(&root->orphan_inodes); |
7b128766 JB |
2978 | } |
2979 | ||
72ac3c0d JB |
2980 | if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
2981 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 2982 | reserve = 1; |
d68fc57b | 2983 | spin_unlock(&root->orphan_lock); |
7b128766 | 2984 | |
d68fc57b YZ |
2985 | /* grab metadata reservation from transaction handle */ |
2986 | if (reserve) { | |
2987 | ret = btrfs_orphan_reserve_metadata(trans, inode); | |
79787eaa | 2988 | BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */ |
d68fc57b | 2989 | } |
7b128766 | 2990 | |
d68fc57b YZ |
2991 | /* insert an orphan item to track this unlinked/truncated file */ |
2992 | if (insert >= 1) { | |
33345d01 | 2993 | ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode)); |
4ef31a45 | 2994 | if (ret) { |
703c88e0 | 2995 | atomic_dec(&root->orphan_inodes); |
4ef31a45 JB |
2996 | if (reserve) { |
2997 | clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, | |
2998 | &BTRFS_I(inode)->runtime_flags); | |
2999 | btrfs_orphan_release_metadata(inode); | |
3000 | } | |
3001 | if (ret != -EEXIST) { | |
e8e7cff6 JB |
3002 | clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3003 | &BTRFS_I(inode)->runtime_flags); | |
4ef31a45 JB |
3004 | btrfs_abort_transaction(trans, root, ret); |
3005 | return ret; | |
3006 | } | |
79787eaa JM |
3007 | } |
3008 | ret = 0; | |
d68fc57b YZ |
3009 | } |
3010 | ||
3011 | /* insert an orphan item to track subvolume contains orphan files */ | |
3012 | if (insert >= 2) { | |
3013 | ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, | |
3014 | root->root_key.objectid); | |
79787eaa JM |
3015 | if (ret && ret != -EEXIST) { |
3016 | btrfs_abort_transaction(trans, root, ret); | |
3017 | return ret; | |
3018 | } | |
d68fc57b YZ |
3019 | } |
3020 | return 0; | |
7b128766 JB |
3021 | } |
3022 | ||
3023 | /* | |
3024 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
3025 | * item for this particular inode. | |
3026 | */ | |
48a3b636 ES |
3027 | static int btrfs_orphan_del(struct btrfs_trans_handle *trans, |
3028 | struct inode *inode) | |
7b128766 JB |
3029 | { |
3030 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d68fc57b YZ |
3031 | int delete_item = 0; |
3032 | int release_rsv = 0; | |
7b128766 JB |
3033 | int ret = 0; |
3034 | ||
d68fc57b | 3035 | spin_lock(&root->orphan_lock); |
8a35d95f JB |
3036 | if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3037 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3038 | delete_item = 1; |
7b128766 | 3039 | |
72ac3c0d JB |
3040 | if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED, |
3041 | &BTRFS_I(inode)->runtime_flags)) | |
d68fc57b | 3042 | release_rsv = 1; |
d68fc57b | 3043 | spin_unlock(&root->orphan_lock); |
7b128766 | 3044 | |
703c88e0 | 3045 | if (delete_item) { |
8a35d95f | 3046 | atomic_dec(&root->orphan_inodes); |
703c88e0 FDBM |
3047 | if (trans) |
3048 | ret = btrfs_del_orphan_item(trans, root, | |
3049 | btrfs_ino(inode)); | |
8a35d95f | 3050 | } |
7b128766 | 3051 | |
703c88e0 FDBM |
3052 | if (release_rsv) |
3053 | btrfs_orphan_release_metadata(inode); | |
3054 | ||
4ef31a45 | 3055 | return ret; |
7b128766 JB |
3056 | } |
3057 | ||
3058 | /* | |
3059 | * this cleans up any orphans that may be left on the list from the last use | |
3060 | * of this root. | |
3061 | */ | |
66b4ffd1 | 3062 | int btrfs_orphan_cleanup(struct btrfs_root *root) |
7b128766 JB |
3063 | { |
3064 | struct btrfs_path *path; | |
3065 | struct extent_buffer *leaf; | |
7b128766 JB |
3066 | struct btrfs_key key, found_key; |
3067 | struct btrfs_trans_handle *trans; | |
3068 | struct inode *inode; | |
8f6d7f4f | 3069 | u64 last_objectid = 0; |
7b128766 JB |
3070 | int ret = 0, nr_unlink = 0, nr_truncate = 0; |
3071 | ||
d68fc57b | 3072 | if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) |
66b4ffd1 | 3073 | return 0; |
c71bf099 YZ |
3074 | |
3075 | path = btrfs_alloc_path(); | |
66b4ffd1 JB |
3076 | if (!path) { |
3077 | ret = -ENOMEM; | |
3078 | goto out; | |
3079 | } | |
7b128766 JB |
3080 | path->reada = -1; |
3081 | ||
3082 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
3083 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); | |
3084 | key.offset = (u64)-1; | |
3085 | ||
7b128766 JB |
3086 | while (1) { |
3087 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
66b4ffd1 JB |
3088 | if (ret < 0) |
3089 | goto out; | |
7b128766 JB |
3090 | |
3091 | /* | |
3092 | * if ret == 0 means we found what we were searching for, which | |
25985edc | 3093 | * is weird, but possible, so only screw with path if we didn't |
7b128766 JB |
3094 | * find the key and see if we have stuff that matches |
3095 | */ | |
3096 | if (ret > 0) { | |
66b4ffd1 | 3097 | ret = 0; |
7b128766 JB |
3098 | if (path->slots[0] == 0) |
3099 | break; | |
3100 | path->slots[0]--; | |
3101 | } | |
3102 | ||
3103 | /* pull out the item */ | |
3104 | leaf = path->nodes[0]; | |
7b128766 JB |
3105 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3106 | ||
3107 | /* make sure the item matches what we want */ | |
3108 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
3109 | break; | |
3110 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) | |
3111 | break; | |
3112 | ||
3113 | /* release the path since we're done with it */ | |
b3b4aa74 | 3114 | btrfs_release_path(path); |
7b128766 JB |
3115 | |
3116 | /* | |
3117 | * this is where we are basically btrfs_lookup, without the | |
3118 | * crossing root thing. we store the inode number in the | |
3119 | * offset of the orphan item. | |
3120 | */ | |
8f6d7f4f JB |
3121 | |
3122 | if (found_key.offset == last_objectid) { | |
c2cf52eb SK |
3123 | btrfs_err(root->fs_info, |
3124 | "Error removing orphan entry, stopping orphan cleanup"); | |
8f6d7f4f JB |
3125 | ret = -EINVAL; |
3126 | goto out; | |
3127 | } | |
3128 | ||
3129 | last_objectid = found_key.offset; | |
3130 | ||
5d4f98a2 YZ |
3131 | found_key.objectid = found_key.offset; |
3132 | found_key.type = BTRFS_INODE_ITEM_KEY; | |
3133 | found_key.offset = 0; | |
73f73415 | 3134 | inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); |
8c6ffba0 | 3135 | ret = PTR_ERR_OR_ZERO(inode); |
a8c9e576 | 3136 | if (ret && ret != -ESTALE) |
66b4ffd1 | 3137 | goto out; |
7b128766 | 3138 | |
f8e9e0b0 AJ |
3139 | if (ret == -ESTALE && root == root->fs_info->tree_root) { |
3140 | struct btrfs_root *dead_root; | |
3141 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3142 | int is_dead_root = 0; | |
3143 | ||
3144 | /* | |
3145 | * this is an orphan in the tree root. Currently these | |
3146 | * could come from 2 sources: | |
3147 | * a) a snapshot deletion in progress | |
3148 | * b) a free space cache inode | |
3149 | * We need to distinguish those two, as the snapshot | |
3150 | * orphan must not get deleted. | |
3151 | * find_dead_roots already ran before us, so if this | |
3152 | * is a snapshot deletion, we should find the root | |
3153 | * in the dead_roots list | |
3154 | */ | |
3155 | spin_lock(&fs_info->trans_lock); | |
3156 | list_for_each_entry(dead_root, &fs_info->dead_roots, | |
3157 | root_list) { | |
3158 | if (dead_root->root_key.objectid == | |
3159 | found_key.objectid) { | |
3160 | is_dead_root = 1; | |
3161 | break; | |
3162 | } | |
3163 | } | |
3164 | spin_unlock(&fs_info->trans_lock); | |
3165 | if (is_dead_root) { | |
3166 | /* prevent this orphan from being found again */ | |
3167 | key.offset = found_key.objectid - 1; | |
3168 | continue; | |
3169 | } | |
3170 | } | |
7b128766 | 3171 | /* |
a8c9e576 JB |
3172 | * Inode is already gone but the orphan item is still there, |
3173 | * kill the orphan item. | |
7b128766 | 3174 | */ |
a8c9e576 JB |
3175 | if (ret == -ESTALE) { |
3176 | trans = btrfs_start_transaction(root, 1); | |
66b4ffd1 JB |
3177 | if (IS_ERR(trans)) { |
3178 | ret = PTR_ERR(trans); | |
3179 | goto out; | |
3180 | } | |
c2cf52eb SK |
3181 | btrfs_debug(root->fs_info, "auto deleting %Lu", |
3182 | found_key.objectid); | |
a8c9e576 JB |
3183 | ret = btrfs_del_orphan_item(trans, root, |
3184 | found_key.objectid); | |
5b21f2ed | 3185 | btrfs_end_transaction(trans, root); |
4ef31a45 JB |
3186 | if (ret) |
3187 | goto out; | |
7b128766 JB |
3188 | continue; |
3189 | } | |
3190 | ||
a8c9e576 JB |
3191 | /* |
3192 | * add this inode to the orphan list so btrfs_orphan_del does | |
3193 | * the proper thing when we hit it | |
3194 | */ | |
8a35d95f JB |
3195 | set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
3196 | &BTRFS_I(inode)->runtime_flags); | |
925396ec | 3197 | atomic_inc(&root->orphan_inodes); |
a8c9e576 | 3198 | |
7b128766 JB |
3199 | /* if we have links, this was a truncate, lets do that */ |
3200 | if (inode->i_nlink) { | |
fae7f21c | 3201 | if (WARN_ON(!S_ISREG(inode->i_mode))) { |
a41ad394 JB |
3202 | iput(inode); |
3203 | continue; | |
3204 | } | |
7b128766 | 3205 | nr_truncate++; |
f3fe820c JB |
3206 | |
3207 | /* 1 for the orphan item deletion. */ | |
3208 | trans = btrfs_start_transaction(root, 1); | |
3209 | if (IS_ERR(trans)) { | |
c69b26b0 | 3210 | iput(inode); |
f3fe820c JB |
3211 | ret = PTR_ERR(trans); |
3212 | goto out; | |
3213 | } | |
3214 | ret = btrfs_orphan_add(trans, inode); | |
3215 | btrfs_end_transaction(trans, root); | |
c69b26b0 JB |
3216 | if (ret) { |
3217 | iput(inode); | |
f3fe820c | 3218 | goto out; |
c69b26b0 | 3219 | } |
f3fe820c | 3220 | |
66b4ffd1 | 3221 | ret = btrfs_truncate(inode); |
4a7d0f68 JB |
3222 | if (ret) |
3223 | btrfs_orphan_del(NULL, inode); | |
7b128766 JB |
3224 | } else { |
3225 | nr_unlink++; | |
3226 | } | |
3227 | ||
3228 | /* this will do delete_inode and everything for us */ | |
3229 | iput(inode); | |
66b4ffd1 JB |
3230 | if (ret) |
3231 | goto out; | |
7b128766 | 3232 | } |
3254c876 MX |
3233 | /* release the path since we're done with it */ |
3234 | btrfs_release_path(path); | |
3235 | ||
d68fc57b YZ |
3236 | root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; |
3237 | ||
3238 | if (root->orphan_block_rsv) | |
3239 | btrfs_block_rsv_release(root, root->orphan_block_rsv, | |
3240 | (u64)-1); | |
3241 | ||
3242 | if (root->orphan_block_rsv || root->orphan_item_inserted) { | |
7a7eaa40 | 3243 | trans = btrfs_join_transaction(root); |
66b4ffd1 JB |
3244 | if (!IS_ERR(trans)) |
3245 | btrfs_end_transaction(trans, root); | |
d68fc57b | 3246 | } |
7b128766 JB |
3247 | |
3248 | if (nr_unlink) | |
4884b476 | 3249 | btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); |
7b128766 | 3250 | if (nr_truncate) |
4884b476 | 3251 | btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); |
66b4ffd1 JB |
3252 | |
3253 | out: | |
3254 | if (ret) | |
c2cf52eb SK |
3255 | btrfs_crit(root->fs_info, |
3256 | "could not do orphan cleanup %d", ret); | |
66b4ffd1 JB |
3257 | btrfs_free_path(path); |
3258 | return ret; | |
7b128766 JB |
3259 | } |
3260 | ||
46a53cca CM |
3261 | /* |
3262 | * very simple check to peek ahead in the leaf looking for xattrs. If we | |
3263 | * don't find any xattrs, we know there can't be any acls. | |
3264 | * | |
3265 | * slot is the slot the inode is in, objectid is the objectid of the inode | |
3266 | */ | |
3267 | static noinline int acls_after_inode_item(struct extent_buffer *leaf, | |
3268 | int slot, u64 objectid) | |
3269 | { | |
3270 | u32 nritems = btrfs_header_nritems(leaf); | |
3271 | struct btrfs_key found_key; | |
f23b5a59 JB |
3272 | static u64 xattr_access = 0; |
3273 | static u64 xattr_default = 0; | |
46a53cca CM |
3274 | int scanned = 0; |
3275 | ||
f23b5a59 JB |
3276 | if (!xattr_access) { |
3277 | xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS, | |
3278 | strlen(POSIX_ACL_XATTR_ACCESS)); | |
3279 | xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT, | |
3280 | strlen(POSIX_ACL_XATTR_DEFAULT)); | |
3281 | } | |
3282 | ||
46a53cca CM |
3283 | slot++; |
3284 | while (slot < nritems) { | |
3285 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
3286 | ||
3287 | /* we found a different objectid, there must not be acls */ | |
3288 | if (found_key.objectid != objectid) | |
3289 | return 0; | |
3290 | ||
3291 | /* we found an xattr, assume we've got an acl */ | |
f23b5a59 JB |
3292 | if (found_key.type == BTRFS_XATTR_ITEM_KEY) { |
3293 | if (found_key.offset == xattr_access || | |
3294 | found_key.offset == xattr_default) | |
3295 | return 1; | |
3296 | } | |
46a53cca CM |
3297 | |
3298 | /* | |
3299 | * we found a key greater than an xattr key, there can't | |
3300 | * be any acls later on | |
3301 | */ | |
3302 | if (found_key.type > BTRFS_XATTR_ITEM_KEY) | |
3303 | return 0; | |
3304 | ||
3305 | slot++; | |
3306 | scanned++; | |
3307 | ||
3308 | /* | |
3309 | * it goes inode, inode backrefs, xattrs, extents, | |
3310 | * so if there are a ton of hard links to an inode there can | |
3311 | * be a lot of backrefs. Don't waste time searching too hard, | |
3312 | * this is just an optimization | |
3313 | */ | |
3314 | if (scanned >= 8) | |
3315 | break; | |
3316 | } | |
3317 | /* we hit the end of the leaf before we found an xattr or | |
3318 | * something larger than an xattr. We have to assume the inode | |
3319 | * has acls | |
3320 | */ | |
3321 | return 1; | |
3322 | } | |
3323 | ||
d352ac68 CM |
3324 | /* |
3325 | * read an inode from the btree into the in-memory inode | |
3326 | */ | |
5d4f98a2 | 3327 | static void btrfs_read_locked_inode(struct inode *inode) |
39279cc3 CM |
3328 | { |
3329 | struct btrfs_path *path; | |
5f39d397 | 3330 | struct extent_buffer *leaf; |
39279cc3 | 3331 | struct btrfs_inode_item *inode_item; |
0b86a832 | 3332 | struct btrfs_timespec *tspec; |
39279cc3 CM |
3333 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3334 | struct btrfs_key location; | |
67de1176 | 3335 | unsigned long ptr; |
46a53cca | 3336 | int maybe_acls; |
618e21d5 | 3337 | u32 rdev; |
39279cc3 | 3338 | int ret; |
2f7e33d4 MX |
3339 | bool filled = false; |
3340 | ||
3341 | ret = btrfs_fill_inode(inode, &rdev); | |
3342 | if (!ret) | |
3343 | filled = true; | |
39279cc3 CM |
3344 | |
3345 | path = btrfs_alloc_path(); | |
1748f843 MF |
3346 | if (!path) |
3347 | goto make_bad; | |
3348 | ||
d90c7321 | 3349 | path->leave_spinning = 1; |
39279cc3 | 3350 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 3351 | |
39279cc3 | 3352 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
5f39d397 | 3353 | if (ret) |
39279cc3 | 3354 | goto make_bad; |
39279cc3 | 3355 | |
5f39d397 | 3356 | leaf = path->nodes[0]; |
2f7e33d4 MX |
3357 | |
3358 | if (filled) | |
67de1176 | 3359 | goto cache_index; |
2f7e33d4 | 3360 | |
5f39d397 CM |
3361 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
3362 | struct btrfs_inode_item); | |
5f39d397 | 3363 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); |
bfe86848 | 3364 | set_nlink(inode, btrfs_inode_nlink(leaf, inode_item)); |
2f2f43d3 EB |
3365 | i_uid_write(inode, btrfs_inode_uid(leaf, inode_item)); |
3366 | i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); | |
dbe674a9 | 3367 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 CM |
3368 | |
3369 | tspec = btrfs_inode_atime(inode_item); | |
3370 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
3371 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
3372 | ||
3373 | tspec = btrfs_inode_mtime(inode_item); | |
3374 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
3375 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
3376 | ||
3377 | tspec = btrfs_inode_ctime(inode_item); | |
3378 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
3379 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
3380 | ||
a76a3cd4 | 3381 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 | 3382 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
5dc562c5 JB |
3383 | BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item); |
3384 | ||
3385 | /* | |
3386 | * If we were modified in the current generation and evicted from memory | |
3387 | * and then re-read we need to do a full sync since we don't have any | |
3388 | * idea about which extents were modified before we were evicted from | |
3389 | * cache. | |
3390 | */ | |
3391 | if (BTRFS_I(inode)->last_trans == root->fs_info->generation) | |
3392 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
3393 | &BTRFS_I(inode)->runtime_flags); | |
3394 | ||
0c4d2d95 | 3395 | inode->i_version = btrfs_inode_sequence(leaf, inode_item); |
e02119d5 | 3396 | inode->i_generation = BTRFS_I(inode)->generation; |
618e21d5 | 3397 | inode->i_rdev = 0; |
5f39d397 CM |
3398 | rdev = btrfs_inode_rdev(leaf, inode_item); |
3399 | ||
aec7477b | 3400 | BTRFS_I(inode)->index_cnt = (u64)-1; |
d2fb3437 | 3401 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
67de1176 MX |
3402 | |
3403 | cache_index: | |
3404 | path->slots[0]++; | |
3405 | if (inode->i_nlink != 1 || | |
3406 | path->slots[0] >= btrfs_header_nritems(leaf)) | |
3407 | goto cache_acl; | |
3408 | ||
3409 | btrfs_item_key_to_cpu(leaf, &location, path->slots[0]); | |
3410 | if (location.objectid != btrfs_ino(inode)) | |
3411 | goto cache_acl; | |
3412 | ||
3413 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3414 | if (location.type == BTRFS_INODE_REF_KEY) { | |
3415 | struct btrfs_inode_ref *ref; | |
3416 | ||
3417 | ref = (struct btrfs_inode_ref *)ptr; | |
3418 | BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref); | |
3419 | } else if (location.type == BTRFS_INODE_EXTREF_KEY) { | |
3420 | struct btrfs_inode_extref *extref; | |
3421 | ||
3422 | extref = (struct btrfs_inode_extref *)ptr; | |
3423 | BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf, | |
3424 | extref); | |
3425 | } | |
2f7e33d4 | 3426 | cache_acl: |
46a53cca CM |
3427 | /* |
3428 | * try to precache a NULL acl entry for files that don't have | |
3429 | * any xattrs or acls | |
3430 | */ | |
33345d01 LZ |
3431 | maybe_acls = acls_after_inode_item(leaf, path->slots[0], |
3432 | btrfs_ino(inode)); | |
72c04902 AV |
3433 | if (!maybe_acls) |
3434 | cache_no_acl(inode); | |
46a53cca | 3435 | |
39279cc3 | 3436 | btrfs_free_path(path); |
39279cc3 | 3437 | |
39279cc3 | 3438 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
3439 | case S_IFREG: |
3440 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 3441 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
d1310b2e | 3442 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3443 | inode->i_fop = &btrfs_file_operations; |
3444 | inode->i_op = &btrfs_file_inode_operations; | |
3445 | break; | |
3446 | case S_IFDIR: | |
3447 | inode->i_fop = &btrfs_dir_file_operations; | |
3448 | if (root == root->fs_info->tree_root) | |
3449 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
3450 | else | |
3451 | inode->i_op = &btrfs_dir_inode_operations; | |
3452 | break; | |
3453 | case S_IFLNK: | |
3454 | inode->i_op = &btrfs_symlink_inode_operations; | |
3455 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
04160088 | 3456 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 | 3457 | break; |
618e21d5 | 3458 | default: |
0279b4cd | 3459 | inode->i_op = &btrfs_special_inode_operations; |
618e21d5 JB |
3460 | init_special_inode(inode, inode->i_mode, rdev); |
3461 | break; | |
39279cc3 | 3462 | } |
6cbff00f CH |
3463 | |
3464 | btrfs_update_iflags(inode); | |
39279cc3 CM |
3465 | return; |
3466 | ||
3467 | make_bad: | |
39279cc3 | 3468 | btrfs_free_path(path); |
39279cc3 CM |
3469 | make_bad_inode(inode); |
3470 | } | |
3471 | ||
d352ac68 CM |
3472 | /* |
3473 | * given a leaf and an inode, copy the inode fields into the leaf | |
3474 | */ | |
e02119d5 CM |
3475 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
3476 | struct extent_buffer *leaf, | |
5f39d397 | 3477 | struct btrfs_inode_item *item, |
39279cc3 CM |
3478 | struct inode *inode) |
3479 | { | |
51fab693 LB |
3480 | struct btrfs_map_token token; |
3481 | ||
3482 | btrfs_init_map_token(&token); | |
5f39d397 | 3483 | |
51fab693 LB |
3484 | btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token); |
3485 | btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token); | |
3486 | btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size, | |
3487 | &token); | |
3488 | btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); | |
3489 | btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); | |
5f39d397 | 3490 | |
51fab693 LB |
3491 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), |
3492 | inode->i_atime.tv_sec, &token); | |
3493 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), | |
3494 | inode->i_atime.tv_nsec, &token); | |
5f39d397 | 3495 | |
51fab693 LB |
3496 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), |
3497 | inode->i_mtime.tv_sec, &token); | |
3498 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), | |
3499 | inode->i_mtime.tv_nsec, &token); | |
5f39d397 | 3500 | |
51fab693 LB |
3501 | btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), |
3502 | inode->i_ctime.tv_sec, &token); | |
3503 | btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), | |
3504 | inode->i_ctime.tv_nsec, &token); | |
5f39d397 | 3505 | |
51fab693 LB |
3506 | btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), |
3507 | &token); | |
3508 | btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, | |
3509 | &token); | |
3510 | btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token); | |
3511 | btrfs_set_token_inode_transid(leaf, item, trans->transid, &token); | |
3512 | btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token); | |
3513 | btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token); | |
3514 | btrfs_set_token_inode_block_group(leaf, item, 0, &token); | |
39279cc3 CM |
3515 | } |
3516 | ||
d352ac68 CM |
3517 | /* |
3518 | * copy everything in the in-memory inode into the btree. | |
3519 | */ | |
2115133f | 3520 | static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans, |
d397712b | 3521 | struct btrfs_root *root, struct inode *inode) |
39279cc3 CM |
3522 | { |
3523 | struct btrfs_inode_item *inode_item; | |
3524 | struct btrfs_path *path; | |
5f39d397 | 3525 | struct extent_buffer *leaf; |
39279cc3 CM |
3526 | int ret; |
3527 | ||
3528 | path = btrfs_alloc_path(); | |
16cdcec7 MX |
3529 | if (!path) |
3530 | return -ENOMEM; | |
3531 | ||
b9473439 | 3532 | path->leave_spinning = 1; |
16cdcec7 MX |
3533 | ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location, |
3534 | 1); | |
39279cc3 CM |
3535 | if (ret) { |
3536 | if (ret > 0) | |
3537 | ret = -ENOENT; | |
3538 | goto failed; | |
3539 | } | |
3540 | ||
5f39d397 CM |
3541 | leaf = path->nodes[0]; |
3542 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
16cdcec7 | 3543 | struct btrfs_inode_item); |
39279cc3 | 3544 | |
e02119d5 | 3545 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 3546 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 3547 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
3548 | ret = 0; |
3549 | failed: | |
39279cc3 CM |
3550 | btrfs_free_path(path); |
3551 | return ret; | |
3552 | } | |
3553 | ||
2115133f CM |
3554 | /* |
3555 | * copy everything in the in-memory inode into the btree. | |
3556 | */ | |
3557 | noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, | |
3558 | struct btrfs_root *root, struct inode *inode) | |
3559 | { | |
3560 | int ret; | |
3561 | ||
3562 | /* | |
3563 | * If the inode is a free space inode, we can deadlock during commit | |
3564 | * if we put it into the delayed code. | |
3565 | * | |
3566 | * The data relocation inode should also be directly updated | |
3567 | * without delay | |
3568 | */ | |
83eea1f1 | 3569 | if (!btrfs_is_free_space_inode(inode) |
2115133f | 3570 | && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) { |
8ea05e3a AB |
3571 | btrfs_update_root_times(trans, root); |
3572 | ||
2115133f CM |
3573 | ret = btrfs_delayed_update_inode(trans, root, inode); |
3574 | if (!ret) | |
3575 | btrfs_set_inode_last_trans(trans, inode); | |
3576 | return ret; | |
3577 | } | |
3578 | ||
3579 | return btrfs_update_inode_item(trans, root, inode); | |
3580 | } | |
3581 | ||
be6aef60 JB |
3582 | noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans, |
3583 | struct btrfs_root *root, | |
3584 | struct inode *inode) | |
2115133f CM |
3585 | { |
3586 | int ret; | |
3587 | ||
3588 | ret = btrfs_update_inode(trans, root, inode); | |
3589 | if (ret == -ENOSPC) | |
3590 | return btrfs_update_inode_item(trans, root, inode); | |
3591 | return ret; | |
3592 | } | |
3593 | ||
d352ac68 CM |
3594 | /* |
3595 | * unlink helper that gets used here in inode.c and in the tree logging | |
3596 | * recovery code. It remove a link in a directory with a given name, and | |
3597 | * also drops the back refs in the inode to the directory | |
3598 | */ | |
92986796 AV |
3599 | static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3600 | struct btrfs_root *root, | |
3601 | struct inode *dir, struct inode *inode, | |
3602 | const char *name, int name_len) | |
39279cc3 CM |
3603 | { |
3604 | struct btrfs_path *path; | |
39279cc3 | 3605 | int ret = 0; |
5f39d397 | 3606 | struct extent_buffer *leaf; |
39279cc3 | 3607 | struct btrfs_dir_item *di; |
5f39d397 | 3608 | struct btrfs_key key; |
aec7477b | 3609 | u64 index; |
33345d01 LZ |
3610 | u64 ino = btrfs_ino(inode); |
3611 | u64 dir_ino = btrfs_ino(dir); | |
39279cc3 CM |
3612 | |
3613 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
3614 | if (!path) { |
3615 | ret = -ENOMEM; | |
554233a6 | 3616 | goto out; |
54aa1f4d CM |
3617 | } |
3618 | ||
b9473439 | 3619 | path->leave_spinning = 1; |
33345d01 | 3620 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
39279cc3 CM |
3621 | name, name_len, -1); |
3622 | if (IS_ERR(di)) { | |
3623 | ret = PTR_ERR(di); | |
3624 | goto err; | |
3625 | } | |
3626 | if (!di) { | |
3627 | ret = -ENOENT; | |
3628 | goto err; | |
3629 | } | |
5f39d397 CM |
3630 | leaf = path->nodes[0]; |
3631 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 3632 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
3633 | if (ret) |
3634 | goto err; | |
b3b4aa74 | 3635 | btrfs_release_path(path); |
39279cc3 | 3636 | |
67de1176 MX |
3637 | /* |
3638 | * If we don't have dir index, we have to get it by looking up | |
3639 | * the inode ref, since we get the inode ref, remove it directly, | |
3640 | * it is unnecessary to do delayed deletion. | |
3641 | * | |
3642 | * But if we have dir index, needn't search inode ref to get it. | |
3643 | * Since the inode ref is close to the inode item, it is better | |
3644 | * that we delay to delete it, and just do this deletion when | |
3645 | * we update the inode item. | |
3646 | */ | |
3647 | if (BTRFS_I(inode)->dir_index) { | |
3648 | ret = btrfs_delayed_delete_inode_ref(inode); | |
3649 | if (!ret) { | |
3650 | index = BTRFS_I(inode)->dir_index; | |
3651 | goto skip_backref; | |
3652 | } | |
3653 | } | |
3654 | ||
33345d01 LZ |
3655 | ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, |
3656 | dir_ino, &index); | |
aec7477b | 3657 | if (ret) { |
c2cf52eb SK |
3658 | btrfs_info(root->fs_info, |
3659 | "failed to delete reference to %.*s, inode %llu parent %llu", | |
c1c9ff7c | 3660 | name_len, name, ino, dir_ino); |
79787eaa | 3661 | btrfs_abort_transaction(trans, root, ret); |
aec7477b JB |
3662 | goto err; |
3663 | } | |
67de1176 | 3664 | skip_backref: |
16cdcec7 | 3665 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
3666 | if (ret) { |
3667 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 3668 | goto err; |
79787eaa | 3669 | } |
39279cc3 | 3670 | |
e02119d5 | 3671 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
33345d01 | 3672 | inode, dir_ino); |
79787eaa JM |
3673 | if (ret != 0 && ret != -ENOENT) { |
3674 | btrfs_abort_transaction(trans, root, ret); | |
3675 | goto err; | |
3676 | } | |
e02119d5 CM |
3677 | |
3678 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | |
3679 | dir, index); | |
6418c961 CM |
3680 | if (ret == -ENOENT) |
3681 | ret = 0; | |
d4e3991b ZB |
3682 | else if (ret) |
3683 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 CM |
3684 | err: |
3685 | btrfs_free_path(path); | |
e02119d5 CM |
3686 | if (ret) |
3687 | goto out; | |
3688 | ||
3689 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 JB |
3690 | inode_inc_iversion(inode); |
3691 | inode_inc_iversion(dir); | |
e02119d5 | 3692 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
b9959295 | 3693 | ret = btrfs_update_inode(trans, root, dir); |
e02119d5 | 3694 | out: |
39279cc3 CM |
3695 | return ret; |
3696 | } | |
3697 | ||
92986796 AV |
3698 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
3699 | struct btrfs_root *root, | |
3700 | struct inode *dir, struct inode *inode, | |
3701 | const char *name, int name_len) | |
3702 | { | |
3703 | int ret; | |
3704 | ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); | |
3705 | if (!ret) { | |
8b558c5f | 3706 | drop_nlink(inode); |
92986796 AV |
3707 | ret = btrfs_update_inode(trans, root, inode); |
3708 | } | |
3709 | return ret; | |
3710 | } | |
39279cc3 | 3711 | |
a22285a6 YZ |
3712 | /* |
3713 | * helper to start transaction for unlink and rmdir. | |
3714 | * | |
d52be818 JB |
3715 | * unlink and rmdir are special in btrfs, they do not always free space, so |
3716 | * if we cannot make our reservations the normal way try and see if there is | |
3717 | * plenty of slack room in the global reserve to migrate, otherwise we cannot | |
3718 | * allow the unlink to occur. | |
a22285a6 | 3719 | */ |
d52be818 | 3720 | static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir) |
4df27c4d | 3721 | { |
39279cc3 | 3722 | struct btrfs_trans_handle *trans; |
a22285a6 | 3723 | struct btrfs_root *root = BTRFS_I(dir)->root; |
4df27c4d YZ |
3724 | int ret; |
3725 | ||
e70bea5f JB |
3726 | /* |
3727 | * 1 for the possible orphan item | |
3728 | * 1 for the dir item | |
3729 | * 1 for the dir index | |
3730 | * 1 for the inode ref | |
e70bea5f JB |
3731 | * 1 for the inode |
3732 | */ | |
6e137ed3 | 3733 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
3734 | if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) |
3735 | return trans; | |
4df27c4d | 3736 | |
d52be818 JB |
3737 | if (PTR_ERR(trans) == -ENOSPC) { |
3738 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); | |
4df27c4d | 3739 | |
d52be818 JB |
3740 | trans = btrfs_start_transaction(root, 0); |
3741 | if (IS_ERR(trans)) | |
3742 | return trans; | |
3743 | ret = btrfs_cond_migrate_bytes(root->fs_info, | |
3744 | &root->fs_info->trans_block_rsv, | |
3745 | num_bytes, 5); | |
3746 | if (ret) { | |
3747 | btrfs_end_transaction(trans, root); | |
3748 | return ERR_PTR(ret); | |
a22285a6 | 3749 | } |
5a77d76c | 3750 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
d52be818 | 3751 | trans->bytes_reserved = num_bytes; |
a22285a6 | 3752 | } |
d52be818 | 3753 | return trans; |
a22285a6 YZ |
3754 | } |
3755 | ||
3756 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
3757 | { | |
3758 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
3759 | struct btrfs_trans_handle *trans; | |
3760 | struct inode *inode = dentry->d_inode; | |
3761 | int ret; | |
a22285a6 | 3762 | |
d52be818 | 3763 | trans = __unlink_start_trans(dir); |
a22285a6 YZ |
3764 | if (IS_ERR(trans)) |
3765 | return PTR_ERR(trans); | |
5f39d397 | 3766 | |
12fcfd22 CM |
3767 | btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); |
3768 | ||
e02119d5 CM |
3769 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
3770 | dentry->d_name.name, dentry->d_name.len); | |
b532402e TI |
3771 | if (ret) |
3772 | goto out; | |
7b128766 | 3773 | |
a22285a6 | 3774 | if (inode->i_nlink == 0) { |
7b128766 | 3775 | ret = btrfs_orphan_add(trans, inode); |
b532402e TI |
3776 | if (ret) |
3777 | goto out; | |
a22285a6 | 3778 | } |
7b128766 | 3779 | |
b532402e | 3780 | out: |
d52be818 | 3781 | btrfs_end_transaction(trans, root); |
b53d3f5d | 3782 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
3783 | return ret; |
3784 | } | |
3785 | ||
4df27c4d YZ |
3786 | int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, |
3787 | struct btrfs_root *root, | |
3788 | struct inode *dir, u64 objectid, | |
3789 | const char *name, int name_len) | |
3790 | { | |
3791 | struct btrfs_path *path; | |
3792 | struct extent_buffer *leaf; | |
3793 | struct btrfs_dir_item *di; | |
3794 | struct btrfs_key key; | |
3795 | u64 index; | |
3796 | int ret; | |
33345d01 | 3797 | u64 dir_ino = btrfs_ino(dir); |
4df27c4d YZ |
3798 | |
3799 | path = btrfs_alloc_path(); | |
3800 | if (!path) | |
3801 | return -ENOMEM; | |
3802 | ||
33345d01 | 3803 | di = btrfs_lookup_dir_item(trans, root, path, dir_ino, |
4df27c4d | 3804 | name, name_len, -1); |
79787eaa JM |
3805 | if (IS_ERR_OR_NULL(di)) { |
3806 | if (!di) | |
3807 | ret = -ENOENT; | |
3808 | else | |
3809 | ret = PTR_ERR(di); | |
3810 | goto out; | |
3811 | } | |
4df27c4d YZ |
3812 | |
3813 | leaf = path->nodes[0]; | |
3814 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
3815 | WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); | |
3816 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
79787eaa JM |
3817 | if (ret) { |
3818 | btrfs_abort_transaction(trans, root, ret); | |
3819 | goto out; | |
3820 | } | |
b3b4aa74 | 3821 | btrfs_release_path(path); |
4df27c4d YZ |
3822 | |
3823 | ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
3824 | objectid, root->root_key.objectid, | |
33345d01 | 3825 | dir_ino, &index, name, name_len); |
4df27c4d | 3826 | if (ret < 0) { |
79787eaa JM |
3827 | if (ret != -ENOENT) { |
3828 | btrfs_abort_transaction(trans, root, ret); | |
3829 | goto out; | |
3830 | } | |
33345d01 | 3831 | di = btrfs_search_dir_index_item(root, path, dir_ino, |
4df27c4d | 3832 | name, name_len); |
79787eaa JM |
3833 | if (IS_ERR_OR_NULL(di)) { |
3834 | if (!di) | |
3835 | ret = -ENOENT; | |
3836 | else | |
3837 | ret = PTR_ERR(di); | |
3838 | btrfs_abort_transaction(trans, root, ret); | |
3839 | goto out; | |
3840 | } | |
4df27c4d YZ |
3841 | |
3842 | leaf = path->nodes[0]; | |
3843 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
b3b4aa74 | 3844 | btrfs_release_path(path); |
4df27c4d YZ |
3845 | index = key.offset; |
3846 | } | |
945d8962 | 3847 | btrfs_release_path(path); |
4df27c4d | 3848 | |
16cdcec7 | 3849 | ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); |
79787eaa JM |
3850 | if (ret) { |
3851 | btrfs_abort_transaction(trans, root, ret); | |
3852 | goto out; | |
3853 | } | |
4df27c4d YZ |
3854 | |
3855 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
0c4d2d95 | 3856 | inode_inc_iversion(dir); |
4df27c4d | 3857 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
5a24e84c | 3858 | ret = btrfs_update_inode_fallback(trans, root, dir); |
79787eaa JM |
3859 | if (ret) |
3860 | btrfs_abort_transaction(trans, root, ret); | |
3861 | out: | |
71d7aed0 | 3862 | btrfs_free_path(path); |
79787eaa | 3863 | return ret; |
4df27c4d YZ |
3864 | } |
3865 | ||
39279cc3 CM |
3866 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
3867 | { | |
3868 | struct inode *inode = dentry->d_inode; | |
1832a6d5 | 3869 | int err = 0; |
39279cc3 | 3870 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 | 3871 | struct btrfs_trans_handle *trans; |
39279cc3 | 3872 | |
b3ae244e | 3873 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
134d4512 | 3874 | return -ENOTEMPTY; |
b3ae244e DS |
3875 | if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) |
3876 | return -EPERM; | |
134d4512 | 3877 | |
d52be818 | 3878 | trans = __unlink_start_trans(dir); |
a22285a6 | 3879 | if (IS_ERR(trans)) |
5df6a9f6 | 3880 | return PTR_ERR(trans); |
5df6a9f6 | 3881 | |
33345d01 | 3882 | if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
4df27c4d YZ |
3883 | err = btrfs_unlink_subvol(trans, root, dir, |
3884 | BTRFS_I(inode)->location.objectid, | |
3885 | dentry->d_name.name, | |
3886 | dentry->d_name.len); | |
3887 | goto out; | |
3888 | } | |
3889 | ||
7b128766 JB |
3890 | err = btrfs_orphan_add(trans, inode); |
3891 | if (err) | |
4df27c4d | 3892 | goto out; |
7b128766 | 3893 | |
39279cc3 | 3894 | /* now the directory is empty */ |
e02119d5 CM |
3895 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
3896 | dentry->d_name.name, dentry->d_name.len); | |
d397712b | 3897 | if (!err) |
dbe674a9 | 3898 | btrfs_i_size_write(inode, 0); |
4df27c4d | 3899 | out: |
d52be818 | 3900 | btrfs_end_transaction(trans, root); |
b53d3f5d | 3901 | btrfs_btree_balance_dirty(root); |
3954401f | 3902 | |
39279cc3 CM |
3903 | return err; |
3904 | } | |
3905 | ||
39279cc3 CM |
3906 | /* |
3907 | * this can truncate away extent items, csum items and directory items. | |
3908 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 3909 | * any higher than new_size |
39279cc3 CM |
3910 | * |
3911 | * csum items that cross the new i_size are truncated to the new size | |
3912 | * as well. | |
7b128766 JB |
3913 | * |
3914 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
3915 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 3916 | */ |
8082510e YZ |
3917 | int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
3918 | struct btrfs_root *root, | |
3919 | struct inode *inode, | |
3920 | u64 new_size, u32 min_type) | |
39279cc3 | 3921 | { |
39279cc3 | 3922 | struct btrfs_path *path; |
5f39d397 | 3923 | struct extent_buffer *leaf; |
39279cc3 | 3924 | struct btrfs_file_extent_item *fi; |
8082510e YZ |
3925 | struct btrfs_key key; |
3926 | struct btrfs_key found_key; | |
39279cc3 | 3927 | u64 extent_start = 0; |
db94535d | 3928 | u64 extent_num_bytes = 0; |
5d4f98a2 | 3929 | u64 extent_offset = 0; |
39279cc3 | 3930 | u64 item_end = 0; |
7f4f6e0a | 3931 | u64 last_size = (u64)-1; |
8082510e | 3932 | u32 found_type = (u8)-1; |
39279cc3 CM |
3933 | int found_extent; |
3934 | int del_item; | |
85e21bac CM |
3935 | int pending_del_nr = 0; |
3936 | int pending_del_slot = 0; | |
179e29e4 | 3937 | int extent_type = -1; |
8082510e YZ |
3938 | int ret; |
3939 | int err = 0; | |
33345d01 | 3940 | u64 ino = btrfs_ino(inode); |
8082510e YZ |
3941 | |
3942 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 3943 | |
0eb0e19c MF |
3944 | path = btrfs_alloc_path(); |
3945 | if (!path) | |
3946 | return -ENOMEM; | |
3947 | path->reada = -1; | |
3948 | ||
5dc562c5 JB |
3949 | /* |
3950 | * We want to drop from the next block forward in case this new size is | |
3951 | * not block aligned since we will be keeping the last block of the | |
3952 | * extent just the way it is. | |
3953 | */ | |
0af3d00b | 3954 | if (root->ref_cows || root == root->fs_info->tree_root) |
fda2832f QW |
3955 | btrfs_drop_extent_cache(inode, ALIGN(new_size, |
3956 | root->sectorsize), (u64)-1, 0); | |
8082510e | 3957 | |
16cdcec7 MX |
3958 | /* |
3959 | * This function is also used to drop the items in the log tree before | |
3960 | * we relog the inode, so if root != BTRFS_I(inode)->root, it means | |
3961 | * it is used to drop the loged items. So we shouldn't kill the delayed | |
3962 | * items. | |
3963 | */ | |
3964 | if (min_type == 0 && root == BTRFS_I(inode)->root) | |
3965 | btrfs_kill_delayed_inode_items(inode); | |
3966 | ||
33345d01 | 3967 | key.objectid = ino; |
39279cc3 | 3968 | key.offset = (u64)-1; |
5f39d397 CM |
3969 | key.type = (u8)-1; |
3970 | ||
85e21bac | 3971 | search_again: |
b9473439 | 3972 | path->leave_spinning = 1; |
85e21bac | 3973 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
8082510e YZ |
3974 | if (ret < 0) { |
3975 | err = ret; | |
3976 | goto out; | |
3977 | } | |
d397712b | 3978 | |
85e21bac | 3979 | if (ret > 0) { |
e02119d5 CM |
3980 | /* there are no items in the tree for us to truncate, we're |
3981 | * done | |
3982 | */ | |
8082510e YZ |
3983 | if (path->slots[0] == 0) |
3984 | goto out; | |
85e21bac CM |
3985 | path->slots[0]--; |
3986 | } | |
3987 | ||
d397712b | 3988 | while (1) { |
39279cc3 | 3989 | fi = NULL; |
5f39d397 CM |
3990 | leaf = path->nodes[0]; |
3991 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
3992 | found_type = btrfs_key_type(&found_key); | |
39279cc3 | 3993 | |
33345d01 | 3994 | if (found_key.objectid != ino) |
39279cc3 | 3995 | break; |
5f39d397 | 3996 | |
85e21bac | 3997 | if (found_type < min_type) |
39279cc3 CM |
3998 | break; |
3999 | ||
5f39d397 | 4000 | item_end = found_key.offset; |
39279cc3 | 4001 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 4002 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 4003 | struct btrfs_file_extent_item); |
179e29e4 CM |
4004 | extent_type = btrfs_file_extent_type(leaf, fi); |
4005 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 4006 | item_end += |
db94535d | 4007 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 | 4008 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 4009 | item_end += btrfs_file_extent_inline_len(leaf, |
c8b97818 | 4010 | fi); |
39279cc3 | 4011 | } |
008630c1 | 4012 | item_end--; |
39279cc3 | 4013 | } |
8082510e YZ |
4014 | if (found_type > min_type) { |
4015 | del_item = 1; | |
4016 | } else { | |
4017 | if (item_end < new_size) | |
b888db2b | 4018 | break; |
8082510e YZ |
4019 | if (found_key.offset >= new_size) |
4020 | del_item = 1; | |
4021 | else | |
4022 | del_item = 0; | |
39279cc3 | 4023 | } |
39279cc3 | 4024 | found_extent = 0; |
39279cc3 | 4025 | /* FIXME, shrink the extent if the ref count is only 1 */ |
179e29e4 CM |
4026 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
4027 | goto delete; | |
4028 | ||
7f4f6e0a JB |
4029 | if (del_item) |
4030 | last_size = found_key.offset; | |
4031 | else | |
4032 | last_size = new_size; | |
4033 | ||
179e29e4 | 4034 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { |
39279cc3 | 4035 | u64 num_dec; |
db94535d | 4036 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
f70a9a6b | 4037 | if (!del_item) { |
db94535d CM |
4038 | u64 orig_num_bytes = |
4039 | btrfs_file_extent_num_bytes(leaf, fi); | |
fda2832f QW |
4040 | extent_num_bytes = ALIGN(new_size - |
4041 | found_key.offset, | |
4042 | root->sectorsize); | |
db94535d CM |
4043 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4044 | extent_num_bytes); | |
4045 | num_dec = (orig_num_bytes - | |
9069218d | 4046 | extent_num_bytes); |
e02119d5 | 4047 | if (root->ref_cows && extent_start != 0) |
a76a3cd4 | 4048 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 4049 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 4050 | } else { |
db94535d CM |
4051 | extent_num_bytes = |
4052 | btrfs_file_extent_disk_num_bytes(leaf, | |
4053 | fi); | |
5d4f98a2 YZ |
4054 | extent_offset = found_key.offset - |
4055 | btrfs_file_extent_offset(leaf, fi); | |
4056 | ||
39279cc3 | 4057 | /* FIXME blocksize != 4096 */ |
9069218d | 4058 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
4059 | if (extent_start != 0) { |
4060 | found_extent = 1; | |
e02119d5 | 4061 | if (root->ref_cows) |
a76a3cd4 | 4062 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 4063 | } |
39279cc3 | 4064 | } |
9069218d | 4065 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
4066 | /* |
4067 | * we can't truncate inline items that have had | |
4068 | * special encodings | |
4069 | */ | |
4070 | if (!del_item && | |
4071 | btrfs_file_extent_compression(leaf, fi) == 0 && | |
4072 | btrfs_file_extent_encryption(leaf, fi) == 0 && | |
4073 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
e02119d5 CM |
4074 | u32 size = new_size - found_key.offset; |
4075 | ||
4076 | if (root->ref_cows) { | |
a76a3cd4 YZ |
4077 | inode_sub_bytes(inode, item_end + 1 - |
4078 | new_size); | |
e02119d5 CM |
4079 | } |
4080 | size = | |
4081 | btrfs_file_extent_calc_inline_size(size); | |
afe5fea7 | 4082 | btrfs_truncate_item(root, path, size, 1); |
e02119d5 | 4083 | } else if (root->ref_cows) { |
a76a3cd4 YZ |
4084 | inode_sub_bytes(inode, item_end + 1 - |
4085 | found_key.offset); | |
9069218d | 4086 | } |
39279cc3 | 4087 | } |
179e29e4 | 4088 | delete: |
39279cc3 | 4089 | if (del_item) { |
85e21bac CM |
4090 | if (!pending_del_nr) { |
4091 | /* no pending yet, add ourselves */ | |
4092 | pending_del_slot = path->slots[0]; | |
4093 | pending_del_nr = 1; | |
4094 | } else if (pending_del_nr && | |
4095 | path->slots[0] + 1 == pending_del_slot) { | |
4096 | /* hop on the pending chunk */ | |
4097 | pending_del_nr++; | |
4098 | pending_del_slot = path->slots[0]; | |
4099 | } else { | |
d397712b | 4100 | BUG(); |
85e21bac | 4101 | } |
39279cc3 CM |
4102 | } else { |
4103 | break; | |
4104 | } | |
0af3d00b JB |
4105 | if (found_extent && (root->ref_cows || |
4106 | root == root->fs_info->tree_root)) { | |
b9473439 | 4107 | btrfs_set_path_blocking(path); |
39279cc3 | 4108 | ret = btrfs_free_extent(trans, root, extent_start, |
5d4f98a2 YZ |
4109 | extent_num_bytes, 0, |
4110 | btrfs_header_owner(leaf), | |
66d7e7f0 | 4111 | ino, extent_offset, 0); |
39279cc3 CM |
4112 | BUG_ON(ret); |
4113 | } | |
85e21bac | 4114 | |
8082510e YZ |
4115 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4116 | break; | |
4117 | ||
4118 | if (path->slots[0] == 0 || | |
4119 | path->slots[0] != pending_del_slot) { | |
8082510e YZ |
4120 | if (pending_del_nr) { |
4121 | ret = btrfs_del_items(trans, root, path, | |
4122 | pending_del_slot, | |
4123 | pending_del_nr); | |
79787eaa JM |
4124 | if (ret) { |
4125 | btrfs_abort_transaction(trans, | |
4126 | root, ret); | |
4127 | goto error; | |
4128 | } | |
8082510e YZ |
4129 | pending_del_nr = 0; |
4130 | } | |
b3b4aa74 | 4131 | btrfs_release_path(path); |
85e21bac | 4132 | goto search_again; |
8082510e YZ |
4133 | } else { |
4134 | path->slots[0]--; | |
85e21bac | 4135 | } |
39279cc3 | 4136 | } |
8082510e | 4137 | out: |
85e21bac CM |
4138 | if (pending_del_nr) { |
4139 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
4140 | pending_del_nr); | |
79787eaa JM |
4141 | if (ret) |
4142 | btrfs_abort_transaction(trans, root, ret); | |
85e21bac | 4143 | } |
79787eaa | 4144 | error: |
7f4f6e0a JB |
4145 | if (last_size != (u64)-1) |
4146 | btrfs_ordered_update_i_size(inode, last_size, NULL); | |
39279cc3 | 4147 | btrfs_free_path(path); |
8082510e | 4148 | return err; |
39279cc3 CM |
4149 | } |
4150 | ||
4151 | /* | |
2aaa6655 JB |
4152 | * btrfs_truncate_page - read, zero a chunk and write a page |
4153 | * @inode - inode that we're zeroing | |
4154 | * @from - the offset to start zeroing | |
4155 | * @len - the length to zero, 0 to zero the entire range respective to the | |
4156 | * offset | |
4157 | * @front - zero up to the offset instead of from the offset on | |
4158 | * | |
4159 | * This will find the page for the "from" offset and cow the page and zero the | |
4160 | * part we want to zero. This is used with truncate and hole punching. | |
39279cc3 | 4161 | */ |
2aaa6655 JB |
4162 | int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len, |
4163 | int front) | |
39279cc3 | 4164 | { |
2aaa6655 | 4165 | struct address_space *mapping = inode->i_mapping; |
db94535d | 4166 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
4167 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4168 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 4169 | struct extent_state *cached_state = NULL; |
e6dcd2dc | 4170 | char *kaddr; |
db94535d | 4171 | u32 blocksize = root->sectorsize; |
39279cc3 CM |
4172 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
4173 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
4174 | struct page *page; | |
3b16a4e3 | 4175 | gfp_t mask = btrfs_alloc_write_mask(mapping); |
39279cc3 | 4176 | int ret = 0; |
a52d9a80 | 4177 | u64 page_start; |
e6dcd2dc | 4178 | u64 page_end; |
39279cc3 | 4179 | |
2aaa6655 JB |
4180 | if ((offset & (blocksize - 1)) == 0 && |
4181 | (!len || ((len & (blocksize - 1)) == 0))) | |
39279cc3 | 4182 | goto out; |
0ca1f7ce | 4183 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
5d5e103a JB |
4184 | if (ret) |
4185 | goto out; | |
39279cc3 | 4186 | |
211c17f5 | 4187 | again: |
3b16a4e3 | 4188 | page = find_or_create_page(mapping, index, mask); |
5d5e103a | 4189 | if (!page) { |
0ca1f7ce | 4190 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
ac6a2b36 | 4191 | ret = -ENOMEM; |
39279cc3 | 4192 | goto out; |
5d5e103a | 4193 | } |
e6dcd2dc CM |
4194 | |
4195 | page_start = page_offset(page); | |
4196 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
4197 | ||
39279cc3 | 4198 | if (!PageUptodate(page)) { |
9ebefb18 | 4199 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 4200 | lock_page(page); |
211c17f5 CM |
4201 | if (page->mapping != mapping) { |
4202 | unlock_page(page); | |
4203 | page_cache_release(page); | |
4204 | goto again; | |
4205 | } | |
39279cc3 CM |
4206 | if (!PageUptodate(page)) { |
4207 | ret = -EIO; | |
89642229 | 4208 | goto out_unlock; |
39279cc3 CM |
4209 | } |
4210 | } | |
211c17f5 | 4211 | wait_on_page_writeback(page); |
e6dcd2dc | 4212 | |
d0082371 | 4213 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
4214 | set_page_extent_mapped(page); |
4215 | ||
4216 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
4217 | if (ordered) { | |
2ac55d41 JB |
4218 | unlock_extent_cached(io_tree, page_start, page_end, |
4219 | &cached_state, GFP_NOFS); | |
e6dcd2dc CM |
4220 | unlock_page(page); |
4221 | page_cache_release(page); | |
eb84ae03 | 4222 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
4223 | btrfs_put_ordered_extent(ordered); |
4224 | goto again; | |
4225 | } | |
4226 | ||
2ac55d41 | 4227 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
4228 | EXTENT_DIRTY | EXTENT_DELALLOC | |
4229 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 4230 | 0, 0, &cached_state, GFP_NOFS); |
5d5e103a | 4231 | |
2ac55d41 JB |
4232 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
4233 | &cached_state); | |
9ed74f2d | 4234 | if (ret) { |
2ac55d41 JB |
4235 | unlock_extent_cached(io_tree, page_start, page_end, |
4236 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
4237 | goto out_unlock; |
4238 | } | |
4239 | ||
e6dcd2dc | 4240 | if (offset != PAGE_CACHE_SIZE) { |
2aaa6655 JB |
4241 | if (!len) |
4242 | len = PAGE_CACHE_SIZE - offset; | |
e6dcd2dc | 4243 | kaddr = kmap(page); |
2aaa6655 JB |
4244 | if (front) |
4245 | memset(kaddr, 0, offset); | |
4246 | else | |
4247 | memset(kaddr + offset, 0, len); | |
e6dcd2dc CM |
4248 | flush_dcache_page(page); |
4249 | kunmap(page); | |
4250 | } | |
247e743c | 4251 | ClearPageChecked(page); |
e6dcd2dc | 4252 | set_page_dirty(page); |
2ac55d41 JB |
4253 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, |
4254 | GFP_NOFS); | |
39279cc3 | 4255 | |
89642229 | 4256 | out_unlock: |
5d5e103a | 4257 | if (ret) |
0ca1f7ce | 4258 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
39279cc3 CM |
4259 | unlock_page(page); |
4260 | page_cache_release(page); | |
4261 | out: | |
4262 | return ret; | |
4263 | } | |
4264 | ||
16e7549f JB |
4265 | static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, |
4266 | u64 offset, u64 len) | |
4267 | { | |
4268 | struct btrfs_trans_handle *trans; | |
4269 | int ret; | |
4270 | ||
4271 | /* | |
4272 | * Still need to make sure the inode looks like it's been updated so | |
4273 | * that any holes get logged if we fsync. | |
4274 | */ | |
4275 | if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { | |
4276 | BTRFS_I(inode)->last_trans = root->fs_info->generation; | |
4277 | BTRFS_I(inode)->last_sub_trans = root->log_transid; | |
4278 | BTRFS_I(inode)->last_log_commit = root->last_log_commit; | |
4279 | return 0; | |
4280 | } | |
4281 | ||
4282 | /* | |
4283 | * 1 - for the one we're dropping | |
4284 | * 1 - for the one we're adding | |
4285 | * 1 - for updating the inode. | |
4286 | */ | |
4287 | trans = btrfs_start_transaction(root, 3); | |
4288 | if (IS_ERR(trans)) | |
4289 | return PTR_ERR(trans); | |
4290 | ||
4291 | ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1); | |
4292 | if (ret) { | |
4293 | btrfs_abort_transaction(trans, root, ret); | |
4294 | btrfs_end_transaction(trans, root); | |
4295 | return ret; | |
4296 | } | |
4297 | ||
4298 | ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset, | |
4299 | 0, 0, len, 0, len, 0, 0, 0); | |
4300 | if (ret) | |
4301 | btrfs_abort_transaction(trans, root, ret); | |
4302 | else | |
4303 | btrfs_update_inode(trans, root, inode); | |
4304 | btrfs_end_transaction(trans, root); | |
4305 | return ret; | |
4306 | } | |
4307 | ||
695a0d0d JB |
4308 | /* |
4309 | * This function puts in dummy file extents for the area we're creating a hole | |
4310 | * for. So if we are truncating this file to a larger size we need to insert | |
4311 | * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for | |
4312 | * the range between oldsize and size | |
4313 | */ | |
a41ad394 | 4314 | int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) |
39279cc3 | 4315 | { |
9036c102 YZ |
4316 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4317 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
a22285a6 | 4318 | struct extent_map *em = NULL; |
2ac55d41 | 4319 | struct extent_state *cached_state = NULL; |
5dc562c5 | 4320 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
fda2832f QW |
4321 | u64 hole_start = ALIGN(oldsize, root->sectorsize); |
4322 | u64 block_end = ALIGN(size, root->sectorsize); | |
9036c102 YZ |
4323 | u64 last_byte; |
4324 | u64 cur_offset; | |
4325 | u64 hole_size; | |
9ed74f2d | 4326 | int err = 0; |
39279cc3 | 4327 | |
a71754fc JB |
4328 | /* |
4329 | * If our size started in the middle of a page we need to zero out the | |
4330 | * rest of the page before we expand the i_size, otherwise we could | |
4331 | * expose stale data. | |
4332 | */ | |
4333 | err = btrfs_truncate_page(inode, oldsize, 0, 0); | |
4334 | if (err) | |
4335 | return err; | |
4336 | ||
9036c102 YZ |
4337 | if (size <= hole_start) |
4338 | return 0; | |
4339 | ||
9036c102 YZ |
4340 | while (1) { |
4341 | struct btrfs_ordered_extent *ordered; | |
fa7c1494 | 4342 | |
2ac55d41 | 4343 | lock_extent_bits(io_tree, hole_start, block_end - 1, 0, |
d0082371 | 4344 | &cached_state); |
fa7c1494 MX |
4345 | ordered = btrfs_lookup_ordered_range(inode, hole_start, |
4346 | block_end - hole_start); | |
9036c102 YZ |
4347 | if (!ordered) |
4348 | break; | |
2ac55d41 JB |
4349 | unlock_extent_cached(io_tree, hole_start, block_end - 1, |
4350 | &cached_state, GFP_NOFS); | |
fa7c1494 | 4351 | btrfs_start_ordered_extent(inode, ordered, 1); |
9036c102 YZ |
4352 | btrfs_put_ordered_extent(ordered); |
4353 | } | |
39279cc3 | 4354 | |
9036c102 YZ |
4355 | cur_offset = hole_start; |
4356 | while (1) { | |
4357 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
4358 | block_end - cur_offset, 0); | |
79787eaa JM |
4359 | if (IS_ERR(em)) { |
4360 | err = PTR_ERR(em); | |
f2767956 | 4361 | em = NULL; |
79787eaa JM |
4362 | break; |
4363 | } | |
9036c102 | 4364 | last_byte = min(extent_map_end(em), block_end); |
fda2832f | 4365 | last_byte = ALIGN(last_byte , root->sectorsize); |
8082510e | 4366 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { |
5dc562c5 | 4367 | struct extent_map *hole_em; |
9036c102 | 4368 | hole_size = last_byte - cur_offset; |
9ed74f2d | 4369 | |
16e7549f JB |
4370 | err = maybe_insert_hole(root, inode, cur_offset, |
4371 | hole_size); | |
4372 | if (err) | |
3893e33b | 4373 | break; |
5dc562c5 JB |
4374 | btrfs_drop_extent_cache(inode, cur_offset, |
4375 | cur_offset + hole_size - 1, 0); | |
4376 | hole_em = alloc_extent_map(); | |
4377 | if (!hole_em) { | |
4378 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
4379 | &BTRFS_I(inode)->runtime_flags); | |
4380 | goto next; | |
4381 | } | |
4382 | hole_em->start = cur_offset; | |
4383 | hole_em->len = hole_size; | |
4384 | hole_em->orig_start = cur_offset; | |
8082510e | 4385 | |
5dc562c5 JB |
4386 | hole_em->block_start = EXTENT_MAP_HOLE; |
4387 | hole_em->block_len = 0; | |
b4939680 | 4388 | hole_em->orig_block_len = 0; |
cc95bef6 | 4389 | hole_em->ram_bytes = hole_size; |
5dc562c5 JB |
4390 | hole_em->bdev = root->fs_info->fs_devices->latest_bdev; |
4391 | hole_em->compress_type = BTRFS_COMPRESS_NONE; | |
16e7549f | 4392 | hole_em->generation = root->fs_info->generation; |
8082510e | 4393 | |
5dc562c5 JB |
4394 | while (1) { |
4395 | write_lock(&em_tree->lock); | |
09a2a8f9 | 4396 | err = add_extent_mapping(em_tree, hole_em, 1); |
5dc562c5 JB |
4397 | write_unlock(&em_tree->lock); |
4398 | if (err != -EEXIST) | |
4399 | break; | |
4400 | btrfs_drop_extent_cache(inode, cur_offset, | |
4401 | cur_offset + | |
4402 | hole_size - 1, 0); | |
4403 | } | |
4404 | free_extent_map(hole_em); | |
9036c102 | 4405 | } |
16e7549f | 4406 | next: |
9036c102 | 4407 | free_extent_map(em); |
a22285a6 | 4408 | em = NULL; |
9036c102 | 4409 | cur_offset = last_byte; |
8082510e | 4410 | if (cur_offset >= block_end) |
9036c102 YZ |
4411 | break; |
4412 | } | |
a22285a6 | 4413 | free_extent_map(em); |
2ac55d41 JB |
4414 | unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, |
4415 | GFP_NOFS); | |
9036c102 YZ |
4416 | return err; |
4417 | } | |
39279cc3 | 4418 | |
3972f260 | 4419 | static int btrfs_setsize(struct inode *inode, struct iattr *attr) |
8082510e | 4420 | { |
f4a2f4c5 MX |
4421 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4422 | struct btrfs_trans_handle *trans; | |
a41ad394 | 4423 | loff_t oldsize = i_size_read(inode); |
3972f260 ES |
4424 | loff_t newsize = attr->ia_size; |
4425 | int mask = attr->ia_valid; | |
8082510e YZ |
4426 | int ret; |
4427 | ||
3972f260 ES |
4428 | /* |
4429 | * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a | |
4430 | * special case where we need to update the times despite not having | |
4431 | * these flags set. For all other operations the VFS set these flags | |
4432 | * explicitly if it wants a timestamp update. | |
4433 | */ | |
4434 | if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) | |
4435 | inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); | |
4436 | ||
a41ad394 | 4437 | if (newsize > oldsize) { |
7caef267 | 4438 | truncate_pagecache(inode, newsize); |
a41ad394 | 4439 | ret = btrfs_cont_expand(inode, oldsize, newsize); |
f4a2f4c5 | 4440 | if (ret) |
8082510e | 4441 | return ret; |
8082510e | 4442 | |
f4a2f4c5 MX |
4443 | trans = btrfs_start_transaction(root, 1); |
4444 | if (IS_ERR(trans)) | |
4445 | return PTR_ERR(trans); | |
4446 | ||
4447 | i_size_write(inode, newsize); | |
4448 | btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); | |
4449 | ret = btrfs_update_inode(trans, root, inode); | |
7ad85bb7 | 4450 | btrfs_end_transaction(trans, root); |
a41ad394 | 4451 | } else { |
8082510e | 4452 | |
a41ad394 JB |
4453 | /* |
4454 | * We're truncating a file that used to have good data down to | |
4455 | * zero. Make sure it gets into the ordered flush list so that | |
4456 | * any new writes get down to disk quickly. | |
4457 | */ | |
4458 | if (newsize == 0) | |
72ac3c0d JB |
4459 | set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
4460 | &BTRFS_I(inode)->runtime_flags); | |
8082510e | 4461 | |
f3fe820c JB |
4462 | /* |
4463 | * 1 for the orphan item we're going to add | |
4464 | * 1 for the orphan item deletion. | |
4465 | */ | |
4466 | trans = btrfs_start_transaction(root, 2); | |
4467 | if (IS_ERR(trans)) | |
4468 | return PTR_ERR(trans); | |
4469 | ||
4470 | /* | |
4471 | * We need to do this in case we fail at _any_ point during the | |
4472 | * actual truncate. Once we do the truncate_setsize we could | |
4473 | * invalidate pages which forces any outstanding ordered io to | |
4474 | * be instantly completed which will give us extents that need | |
4475 | * to be truncated. If we fail to get an orphan inode down we | |
4476 | * could have left over extents that were never meant to live, | |
4477 | * so we need to garuntee from this point on that everything | |
4478 | * will be consistent. | |
4479 | */ | |
4480 | ret = btrfs_orphan_add(trans, inode); | |
4481 | btrfs_end_transaction(trans, root); | |
4482 | if (ret) | |
4483 | return ret; | |
4484 | ||
a41ad394 JB |
4485 | /* we don't support swapfiles, so vmtruncate shouldn't fail */ |
4486 | truncate_setsize(inode, newsize); | |
2e60a51e MX |
4487 | |
4488 | /* Disable nonlocked read DIO to avoid the end less truncate */ | |
4489 | btrfs_inode_block_unlocked_dio(inode); | |
4490 | inode_dio_wait(inode); | |
4491 | btrfs_inode_resume_unlocked_dio(inode); | |
4492 | ||
a41ad394 | 4493 | ret = btrfs_truncate(inode); |
7f4f6e0a JB |
4494 | if (ret && inode->i_nlink) { |
4495 | int err; | |
4496 | ||
4497 | /* | |
4498 | * failed to truncate, disk_i_size is only adjusted down | |
4499 | * as we remove extents, so it should represent the true | |
4500 | * size of the inode, so reset the in memory size and | |
4501 | * delete our orphan entry. | |
4502 | */ | |
4503 | trans = btrfs_join_transaction(root); | |
4504 | if (IS_ERR(trans)) { | |
4505 | btrfs_orphan_del(NULL, inode); | |
4506 | return ret; | |
4507 | } | |
4508 | i_size_write(inode, BTRFS_I(inode)->disk_i_size); | |
4509 | err = btrfs_orphan_del(trans, inode); | |
4510 | if (err) | |
4511 | btrfs_abort_transaction(trans, root, err); | |
4512 | btrfs_end_transaction(trans, root); | |
4513 | } | |
8082510e YZ |
4514 | } |
4515 | ||
a41ad394 | 4516 | return ret; |
8082510e YZ |
4517 | } |
4518 | ||
9036c102 YZ |
4519 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
4520 | { | |
4521 | struct inode *inode = dentry->d_inode; | |
b83cc969 | 4522 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9036c102 | 4523 | int err; |
39279cc3 | 4524 | |
b83cc969 LZ |
4525 | if (btrfs_root_readonly(root)) |
4526 | return -EROFS; | |
4527 | ||
9036c102 YZ |
4528 | err = inode_change_ok(inode, attr); |
4529 | if (err) | |
4530 | return err; | |
2bf5a725 | 4531 | |
5a3f23d5 | 4532 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
3972f260 | 4533 | err = btrfs_setsize(inode, attr); |
8082510e YZ |
4534 | if (err) |
4535 | return err; | |
39279cc3 | 4536 | } |
9036c102 | 4537 | |
1025774c CH |
4538 | if (attr->ia_valid) { |
4539 | setattr_copy(inode, attr); | |
0c4d2d95 | 4540 | inode_inc_iversion(inode); |
22c44fe6 | 4541 | err = btrfs_dirty_inode(inode); |
1025774c | 4542 | |
22c44fe6 | 4543 | if (!err && attr->ia_valid & ATTR_MODE) |
1025774c CH |
4544 | err = btrfs_acl_chmod(inode); |
4545 | } | |
33268eaf | 4546 | |
39279cc3 CM |
4547 | return err; |
4548 | } | |
61295eb8 | 4549 | |
131e404a FDBM |
4550 | /* |
4551 | * While truncating the inode pages during eviction, we get the VFS calling | |
4552 | * btrfs_invalidatepage() against each page of the inode. This is slow because | |
4553 | * the calls to btrfs_invalidatepage() result in a huge amount of calls to | |
4554 | * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting | |
4555 | * extent_state structures over and over, wasting lots of time. | |
4556 | * | |
4557 | * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all | |
4558 | * those expensive operations on a per page basis and do only the ordered io | |
4559 | * finishing, while we release here the extent_map and extent_state structures, | |
4560 | * without the excessive merging and splitting. | |
4561 | */ | |
4562 | static void evict_inode_truncate_pages(struct inode *inode) | |
4563 | { | |
4564 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
4565 | struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree; | |
4566 | struct rb_node *node; | |
4567 | ||
4568 | ASSERT(inode->i_state & I_FREEING); | |
4569 | truncate_inode_pages(&inode->i_data, 0); | |
4570 | ||
4571 | write_lock(&map_tree->lock); | |
4572 | while (!RB_EMPTY_ROOT(&map_tree->map)) { | |
4573 | struct extent_map *em; | |
4574 | ||
4575 | node = rb_first(&map_tree->map); | |
4576 | em = rb_entry(node, struct extent_map, rb_node); | |
180589ef WS |
4577 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
4578 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); | |
131e404a FDBM |
4579 | remove_extent_mapping(map_tree, em); |
4580 | free_extent_map(em); | |
4581 | } | |
4582 | write_unlock(&map_tree->lock); | |
4583 | ||
4584 | spin_lock(&io_tree->lock); | |
4585 | while (!RB_EMPTY_ROOT(&io_tree->state)) { | |
4586 | struct extent_state *state; | |
4587 | struct extent_state *cached_state = NULL; | |
4588 | ||
4589 | node = rb_first(&io_tree->state); | |
4590 | state = rb_entry(node, struct extent_state, rb_node); | |
4591 | atomic_inc(&state->refs); | |
4592 | spin_unlock(&io_tree->lock); | |
4593 | ||
4594 | lock_extent_bits(io_tree, state->start, state->end, | |
4595 | 0, &cached_state); | |
4596 | clear_extent_bit(io_tree, state->start, state->end, | |
4597 | EXTENT_LOCKED | EXTENT_DIRTY | | |
4598 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
4599 | EXTENT_DEFRAG, 1, 1, | |
4600 | &cached_state, GFP_NOFS); | |
4601 | free_extent_state(state); | |
4602 | ||
4603 | spin_lock(&io_tree->lock); | |
4604 | } | |
4605 | spin_unlock(&io_tree->lock); | |
4606 | } | |
4607 | ||
bd555975 | 4608 | void btrfs_evict_inode(struct inode *inode) |
39279cc3 CM |
4609 | { |
4610 | struct btrfs_trans_handle *trans; | |
4611 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
726c35fa | 4612 | struct btrfs_block_rsv *rsv, *global_rsv; |
07127184 | 4613 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 CM |
4614 | int ret; |
4615 | ||
1abe9b8a | 4616 | trace_btrfs_inode_evict(inode); |
4617 | ||
131e404a FDBM |
4618 | evict_inode_truncate_pages(inode); |
4619 | ||
69e9c6c6 SB |
4620 | if (inode->i_nlink && |
4621 | ((btrfs_root_refs(&root->root_item) != 0 && | |
4622 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || | |
4623 | btrfs_is_free_space_inode(inode))) | |
bd555975 AV |
4624 | goto no_delete; |
4625 | ||
39279cc3 | 4626 | if (is_bad_inode(inode)) { |
7b128766 | 4627 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
4628 | goto no_delete; |
4629 | } | |
bd555975 | 4630 | /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */ |
4a096752 | 4631 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
5f39d397 | 4632 | |
c71bf099 | 4633 | if (root->fs_info->log_root_recovering) { |
6bf02314 | 4634 | BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8a35d95f | 4635 | &BTRFS_I(inode)->runtime_flags)); |
c71bf099 YZ |
4636 | goto no_delete; |
4637 | } | |
4638 | ||
76dda93c | 4639 | if (inode->i_nlink > 0) { |
69e9c6c6 SB |
4640 | BUG_ON(btrfs_root_refs(&root->root_item) != 0 && |
4641 | root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID); | |
76dda93c YZ |
4642 | goto no_delete; |
4643 | } | |
4644 | ||
0e8c36a9 MX |
4645 | ret = btrfs_commit_inode_delayed_inode(inode); |
4646 | if (ret) { | |
4647 | btrfs_orphan_del(NULL, inode); | |
4648 | goto no_delete; | |
4649 | } | |
4650 | ||
66d8f3dd | 4651 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
4289a667 JB |
4652 | if (!rsv) { |
4653 | btrfs_orphan_del(NULL, inode); | |
4654 | goto no_delete; | |
4655 | } | |
4a338542 | 4656 | rsv->size = min_size; |
ca7e70f5 | 4657 | rsv->failfast = 1; |
726c35fa | 4658 | global_rsv = &root->fs_info->global_block_rsv; |
4289a667 | 4659 | |
dbe674a9 | 4660 | btrfs_i_size_write(inode, 0); |
5f39d397 | 4661 | |
4289a667 | 4662 | /* |
8407aa46 MX |
4663 | * This is a bit simpler than btrfs_truncate since we've already |
4664 | * reserved our space for our orphan item in the unlink, so we just | |
4665 | * need to reserve some slack space in case we add bytes and update | |
4666 | * inode item when doing the truncate. | |
4289a667 | 4667 | */ |
8082510e | 4668 | while (1) { |
08e007d2 MX |
4669 | ret = btrfs_block_rsv_refill(root, rsv, min_size, |
4670 | BTRFS_RESERVE_FLUSH_LIMIT); | |
726c35fa JB |
4671 | |
4672 | /* | |
4673 | * Try and steal from the global reserve since we will | |
4674 | * likely not use this space anyway, we want to try as | |
4675 | * hard as possible to get this to work. | |
4676 | */ | |
4677 | if (ret) | |
4678 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); | |
d68fc57b | 4679 | |
d68fc57b | 4680 | if (ret) { |
c2cf52eb SK |
4681 | btrfs_warn(root->fs_info, |
4682 | "Could not get space for a delete, will truncate on mount %d", | |
4683 | ret); | |
4289a667 JB |
4684 | btrfs_orphan_del(NULL, inode); |
4685 | btrfs_free_block_rsv(root, rsv); | |
4686 | goto no_delete; | |
d68fc57b | 4687 | } |
7b128766 | 4688 | |
0e8c36a9 | 4689 | trans = btrfs_join_transaction(root); |
4289a667 JB |
4690 | if (IS_ERR(trans)) { |
4691 | btrfs_orphan_del(NULL, inode); | |
4692 | btrfs_free_block_rsv(root, rsv); | |
4693 | goto no_delete; | |
d68fc57b | 4694 | } |
7b128766 | 4695 | |
4289a667 JB |
4696 | trans->block_rsv = rsv; |
4697 | ||
d68fc57b | 4698 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
ca7e70f5 | 4699 | if (ret != -ENOSPC) |
8082510e | 4700 | break; |
85e21bac | 4701 | |
8407aa46 | 4702 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e YZ |
4703 | btrfs_end_transaction(trans, root); |
4704 | trans = NULL; | |
b53d3f5d | 4705 | btrfs_btree_balance_dirty(root); |
8082510e | 4706 | } |
5f39d397 | 4707 | |
4289a667 JB |
4708 | btrfs_free_block_rsv(root, rsv); |
4709 | ||
4ef31a45 JB |
4710 | /* |
4711 | * Errors here aren't a big deal, it just means we leave orphan items | |
4712 | * in the tree. They will be cleaned up on the next mount. | |
4713 | */ | |
8082510e | 4714 | if (ret == 0) { |
4289a667 | 4715 | trans->block_rsv = root->orphan_block_rsv; |
4ef31a45 JB |
4716 | btrfs_orphan_del(trans, inode); |
4717 | } else { | |
4718 | btrfs_orphan_del(NULL, inode); | |
8082510e | 4719 | } |
54aa1f4d | 4720 | |
4289a667 | 4721 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
581bb050 LZ |
4722 | if (!(root == root->fs_info->tree_root || |
4723 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) | |
33345d01 | 4724 | btrfs_return_ino(root, btrfs_ino(inode)); |
581bb050 | 4725 | |
54aa1f4d | 4726 | btrfs_end_transaction(trans, root); |
b53d3f5d | 4727 | btrfs_btree_balance_dirty(root); |
39279cc3 | 4728 | no_delete: |
89042e5a | 4729 | btrfs_remove_delayed_node(inode); |
dbd5768f | 4730 | clear_inode(inode); |
8082510e | 4731 | return; |
39279cc3 CM |
4732 | } |
4733 | ||
4734 | /* | |
4735 | * this returns the key found in the dir entry in the location pointer. | |
4736 | * If no dir entries were found, location->objectid is 0. | |
4737 | */ | |
4738 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
4739 | struct btrfs_key *location) | |
4740 | { | |
4741 | const char *name = dentry->d_name.name; | |
4742 | int namelen = dentry->d_name.len; | |
4743 | struct btrfs_dir_item *di; | |
4744 | struct btrfs_path *path; | |
4745 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 4746 | int ret = 0; |
39279cc3 CM |
4747 | |
4748 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
4749 | if (!path) |
4750 | return -ENOMEM; | |
3954401f | 4751 | |
33345d01 | 4752 | di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name, |
39279cc3 | 4753 | namelen, 0); |
0d9f7f3e Y |
4754 | if (IS_ERR(di)) |
4755 | ret = PTR_ERR(di); | |
d397712b | 4756 | |
c704005d | 4757 | if (IS_ERR_OR_NULL(di)) |
3954401f | 4758 | goto out_err; |
d397712b | 4759 | |
5f39d397 | 4760 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 4761 | out: |
39279cc3 CM |
4762 | btrfs_free_path(path); |
4763 | return ret; | |
3954401f CM |
4764 | out_err: |
4765 | location->objectid = 0; | |
4766 | goto out; | |
39279cc3 CM |
4767 | } |
4768 | ||
4769 | /* | |
4770 | * when we hit a tree root in a directory, the btrfs part of the inode | |
4771 | * needs to be changed to reflect the root directory of the tree root. This | |
4772 | * is kind of like crossing a mount point. | |
4773 | */ | |
4774 | static int fixup_tree_root_location(struct btrfs_root *root, | |
4df27c4d YZ |
4775 | struct inode *dir, |
4776 | struct dentry *dentry, | |
4777 | struct btrfs_key *location, | |
4778 | struct btrfs_root **sub_root) | |
39279cc3 | 4779 | { |
4df27c4d YZ |
4780 | struct btrfs_path *path; |
4781 | struct btrfs_root *new_root; | |
4782 | struct btrfs_root_ref *ref; | |
4783 | struct extent_buffer *leaf; | |
4784 | int ret; | |
4785 | int err = 0; | |
39279cc3 | 4786 | |
4df27c4d YZ |
4787 | path = btrfs_alloc_path(); |
4788 | if (!path) { | |
4789 | err = -ENOMEM; | |
4790 | goto out; | |
4791 | } | |
39279cc3 | 4792 | |
4df27c4d | 4793 | err = -ENOENT; |
75ac2dd9 KN |
4794 | ret = btrfs_find_item(root->fs_info->tree_root, path, |
4795 | BTRFS_I(dir)->root->root_key.objectid, | |
4796 | location->objectid, BTRFS_ROOT_REF_KEY, NULL); | |
4df27c4d YZ |
4797 | if (ret) { |
4798 | if (ret < 0) | |
4799 | err = ret; | |
4800 | goto out; | |
4801 | } | |
39279cc3 | 4802 | |
4df27c4d YZ |
4803 | leaf = path->nodes[0]; |
4804 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | |
33345d01 | 4805 | if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) || |
4df27c4d YZ |
4806 | btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) |
4807 | goto out; | |
39279cc3 | 4808 | |
4df27c4d YZ |
4809 | ret = memcmp_extent_buffer(leaf, dentry->d_name.name, |
4810 | (unsigned long)(ref + 1), | |
4811 | dentry->d_name.len); | |
4812 | if (ret) | |
4813 | goto out; | |
4814 | ||
b3b4aa74 | 4815 | btrfs_release_path(path); |
4df27c4d YZ |
4816 | |
4817 | new_root = btrfs_read_fs_root_no_name(root->fs_info, location); | |
4818 | if (IS_ERR(new_root)) { | |
4819 | err = PTR_ERR(new_root); | |
4820 | goto out; | |
4821 | } | |
4822 | ||
4df27c4d YZ |
4823 | *sub_root = new_root; |
4824 | location->objectid = btrfs_root_dirid(&new_root->root_item); | |
4825 | location->type = BTRFS_INODE_ITEM_KEY; | |
4826 | location->offset = 0; | |
4827 | err = 0; | |
4828 | out: | |
4829 | btrfs_free_path(path); | |
4830 | return err; | |
39279cc3 CM |
4831 | } |
4832 | ||
5d4f98a2 YZ |
4833 | static void inode_tree_add(struct inode *inode) |
4834 | { | |
4835 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4836 | struct btrfs_inode *entry; | |
03e860bd FNP |
4837 | struct rb_node **p; |
4838 | struct rb_node *parent; | |
cef21937 | 4839 | struct rb_node *new = &BTRFS_I(inode)->rb_node; |
33345d01 | 4840 | u64 ino = btrfs_ino(inode); |
5d4f98a2 | 4841 | |
1d3382cb | 4842 | if (inode_unhashed(inode)) |
76dda93c | 4843 | return; |
e1409cef | 4844 | parent = NULL; |
5d4f98a2 | 4845 | spin_lock(&root->inode_lock); |
e1409cef | 4846 | p = &root->inode_tree.rb_node; |
5d4f98a2 YZ |
4847 | while (*p) { |
4848 | parent = *p; | |
4849 | entry = rb_entry(parent, struct btrfs_inode, rb_node); | |
4850 | ||
33345d01 | 4851 | if (ino < btrfs_ino(&entry->vfs_inode)) |
03e860bd | 4852 | p = &parent->rb_left; |
33345d01 | 4853 | else if (ino > btrfs_ino(&entry->vfs_inode)) |
03e860bd | 4854 | p = &parent->rb_right; |
5d4f98a2 YZ |
4855 | else { |
4856 | WARN_ON(!(entry->vfs_inode.i_state & | |
a4ffdde6 | 4857 | (I_WILL_FREE | I_FREEING))); |
cef21937 | 4858 | rb_replace_node(parent, new, &root->inode_tree); |
03e860bd FNP |
4859 | RB_CLEAR_NODE(parent); |
4860 | spin_unlock(&root->inode_lock); | |
cef21937 | 4861 | return; |
5d4f98a2 YZ |
4862 | } |
4863 | } | |
cef21937 FDBM |
4864 | rb_link_node(new, parent, p); |
4865 | rb_insert_color(new, &root->inode_tree); | |
5d4f98a2 YZ |
4866 | spin_unlock(&root->inode_lock); |
4867 | } | |
4868 | ||
4869 | static void inode_tree_del(struct inode *inode) | |
4870 | { | |
4871 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
76dda93c | 4872 | int empty = 0; |
5d4f98a2 | 4873 | |
03e860bd | 4874 | spin_lock(&root->inode_lock); |
5d4f98a2 | 4875 | if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { |
5d4f98a2 | 4876 | rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); |
5d4f98a2 | 4877 | RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); |
76dda93c | 4878 | empty = RB_EMPTY_ROOT(&root->inode_tree); |
5d4f98a2 | 4879 | } |
03e860bd | 4880 | spin_unlock(&root->inode_lock); |
76dda93c | 4881 | |
69e9c6c6 | 4882 | if (empty && btrfs_root_refs(&root->root_item) == 0) { |
76dda93c YZ |
4883 | synchronize_srcu(&root->fs_info->subvol_srcu); |
4884 | spin_lock(&root->inode_lock); | |
4885 | empty = RB_EMPTY_ROOT(&root->inode_tree); | |
4886 | spin_unlock(&root->inode_lock); | |
4887 | if (empty) | |
4888 | btrfs_add_dead_root(root); | |
4889 | } | |
4890 | } | |
4891 | ||
143bede5 | 4892 | void btrfs_invalidate_inodes(struct btrfs_root *root) |
76dda93c YZ |
4893 | { |
4894 | struct rb_node *node; | |
4895 | struct rb_node *prev; | |
4896 | struct btrfs_inode *entry; | |
4897 | struct inode *inode; | |
4898 | u64 objectid = 0; | |
4899 | ||
4900 | WARN_ON(btrfs_root_refs(&root->root_item) != 0); | |
4901 | ||
4902 | spin_lock(&root->inode_lock); | |
4903 | again: | |
4904 | node = root->inode_tree.rb_node; | |
4905 | prev = NULL; | |
4906 | while (node) { | |
4907 | prev = node; | |
4908 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
4909 | ||
33345d01 | 4910 | if (objectid < btrfs_ino(&entry->vfs_inode)) |
76dda93c | 4911 | node = node->rb_left; |
33345d01 | 4912 | else if (objectid > btrfs_ino(&entry->vfs_inode)) |
76dda93c YZ |
4913 | node = node->rb_right; |
4914 | else | |
4915 | break; | |
4916 | } | |
4917 | if (!node) { | |
4918 | while (prev) { | |
4919 | entry = rb_entry(prev, struct btrfs_inode, rb_node); | |
33345d01 | 4920 | if (objectid <= btrfs_ino(&entry->vfs_inode)) { |
76dda93c YZ |
4921 | node = prev; |
4922 | break; | |
4923 | } | |
4924 | prev = rb_next(prev); | |
4925 | } | |
4926 | } | |
4927 | while (node) { | |
4928 | entry = rb_entry(node, struct btrfs_inode, rb_node); | |
33345d01 | 4929 | objectid = btrfs_ino(&entry->vfs_inode) + 1; |
76dda93c YZ |
4930 | inode = igrab(&entry->vfs_inode); |
4931 | if (inode) { | |
4932 | spin_unlock(&root->inode_lock); | |
4933 | if (atomic_read(&inode->i_count) > 1) | |
4934 | d_prune_aliases(inode); | |
4935 | /* | |
45321ac5 | 4936 | * btrfs_drop_inode will have it removed from |
76dda93c YZ |
4937 | * the inode cache when its usage count |
4938 | * hits zero. | |
4939 | */ | |
4940 | iput(inode); | |
4941 | cond_resched(); | |
4942 | spin_lock(&root->inode_lock); | |
4943 | goto again; | |
4944 | } | |
4945 | ||
4946 | if (cond_resched_lock(&root->inode_lock)) | |
4947 | goto again; | |
4948 | ||
4949 | node = rb_next(node); | |
4950 | } | |
4951 | spin_unlock(&root->inode_lock); | |
5d4f98a2 YZ |
4952 | } |
4953 | ||
e02119d5 CM |
4954 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
4955 | { | |
4956 | struct btrfs_iget_args *args = p; | |
4957 | inode->i_ino = args->ino; | |
e02119d5 | 4958 | BTRFS_I(inode)->root = args->root; |
39279cc3 CM |
4959 | return 0; |
4960 | } | |
4961 | ||
4962 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
4963 | { | |
4964 | struct btrfs_iget_args *args = opaque; | |
33345d01 | 4965 | return args->ino == btrfs_ino(inode) && |
d397712b | 4966 | args->root == BTRFS_I(inode)->root; |
39279cc3 CM |
4967 | } |
4968 | ||
5d4f98a2 YZ |
4969 | static struct inode *btrfs_iget_locked(struct super_block *s, |
4970 | u64 objectid, | |
4971 | struct btrfs_root *root) | |
39279cc3 CM |
4972 | { |
4973 | struct inode *inode; | |
4974 | struct btrfs_iget_args args; | |
778ba82b FDBM |
4975 | unsigned long hashval = btrfs_inode_hash(objectid, root); |
4976 | ||
39279cc3 CM |
4977 | args.ino = objectid; |
4978 | args.root = root; | |
4979 | ||
778ba82b | 4980 | inode = iget5_locked(s, hashval, btrfs_find_actor, |
39279cc3 CM |
4981 | btrfs_init_locked_inode, |
4982 | (void *)&args); | |
4983 | return inode; | |
4984 | } | |
4985 | ||
1a54ef8c BR |
4986 | /* Get an inode object given its location and corresponding root. |
4987 | * Returns in *is_new if the inode was read from disk | |
4988 | */ | |
4989 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
73f73415 | 4990 | struct btrfs_root *root, int *new) |
1a54ef8c BR |
4991 | { |
4992 | struct inode *inode; | |
4993 | ||
4994 | inode = btrfs_iget_locked(s, location->objectid, root); | |
4995 | if (!inode) | |
5d4f98a2 | 4996 | return ERR_PTR(-ENOMEM); |
1a54ef8c BR |
4997 | |
4998 | if (inode->i_state & I_NEW) { | |
4999 | BTRFS_I(inode)->root = root; | |
5000 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); | |
5001 | btrfs_read_locked_inode(inode); | |
1748f843 MF |
5002 | if (!is_bad_inode(inode)) { |
5003 | inode_tree_add(inode); | |
5004 | unlock_new_inode(inode); | |
5005 | if (new) | |
5006 | *new = 1; | |
5007 | } else { | |
e0b6d65b ST |
5008 | unlock_new_inode(inode); |
5009 | iput(inode); | |
5010 | inode = ERR_PTR(-ESTALE); | |
1748f843 MF |
5011 | } |
5012 | } | |
5013 | ||
1a54ef8c BR |
5014 | return inode; |
5015 | } | |
5016 | ||
4df27c4d YZ |
5017 | static struct inode *new_simple_dir(struct super_block *s, |
5018 | struct btrfs_key *key, | |
5019 | struct btrfs_root *root) | |
5020 | { | |
5021 | struct inode *inode = new_inode(s); | |
5022 | ||
5023 | if (!inode) | |
5024 | return ERR_PTR(-ENOMEM); | |
5025 | ||
4df27c4d YZ |
5026 | BTRFS_I(inode)->root = root; |
5027 | memcpy(&BTRFS_I(inode)->location, key, sizeof(*key)); | |
72ac3c0d | 5028 | set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags); |
4df27c4d YZ |
5029 | |
5030 | inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID; | |
848cce0d | 5031 | inode->i_op = &btrfs_dir_ro_inode_operations; |
4df27c4d YZ |
5032 | inode->i_fop = &simple_dir_operations; |
5033 | inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; | |
5034 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
5035 | ||
5036 | return inode; | |
5037 | } | |
5038 | ||
3de4586c | 5039 | struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) |
39279cc3 | 5040 | { |
d397712b | 5041 | struct inode *inode; |
4df27c4d | 5042 | struct btrfs_root *root = BTRFS_I(dir)->root; |
39279cc3 CM |
5043 | struct btrfs_root *sub_root = root; |
5044 | struct btrfs_key location; | |
76dda93c | 5045 | int index; |
b4aff1f8 | 5046 | int ret = 0; |
39279cc3 CM |
5047 | |
5048 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
5049 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 5050 | |
39e3c955 | 5051 | ret = btrfs_inode_by_name(dir, dentry, &location); |
39279cc3 CM |
5052 | if (ret < 0) |
5053 | return ERR_PTR(ret); | |
5f39d397 | 5054 | |
4df27c4d | 5055 | if (location.objectid == 0) |
5662344b | 5056 | return ERR_PTR(-ENOENT); |
4df27c4d YZ |
5057 | |
5058 | if (location.type == BTRFS_INODE_ITEM_KEY) { | |
73f73415 | 5059 | inode = btrfs_iget(dir->i_sb, &location, root, NULL); |
4df27c4d YZ |
5060 | return inode; |
5061 | } | |
5062 | ||
5063 | BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); | |
5064 | ||
76dda93c | 5065 | index = srcu_read_lock(&root->fs_info->subvol_srcu); |
4df27c4d YZ |
5066 | ret = fixup_tree_root_location(root, dir, dentry, |
5067 | &location, &sub_root); | |
5068 | if (ret < 0) { | |
5069 | if (ret != -ENOENT) | |
5070 | inode = ERR_PTR(ret); | |
5071 | else | |
5072 | inode = new_simple_dir(dir->i_sb, &location, sub_root); | |
5073 | } else { | |
73f73415 | 5074 | inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); |
39279cc3 | 5075 | } |
76dda93c YZ |
5076 | srcu_read_unlock(&root->fs_info->subvol_srcu, index); |
5077 | ||
34d19bad | 5078 | if (!IS_ERR(inode) && root != sub_root) { |
c71bf099 YZ |
5079 | down_read(&root->fs_info->cleanup_work_sem); |
5080 | if (!(inode->i_sb->s_flags & MS_RDONLY)) | |
66b4ffd1 | 5081 | ret = btrfs_orphan_cleanup(sub_root); |
c71bf099 | 5082 | up_read(&root->fs_info->cleanup_work_sem); |
01cd3367 JB |
5083 | if (ret) { |
5084 | iput(inode); | |
66b4ffd1 | 5085 | inode = ERR_PTR(ret); |
01cd3367 | 5086 | } |
c71bf099 YZ |
5087 | } |
5088 | ||
3de4586c CM |
5089 | return inode; |
5090 | } | |
5091 | ||
fe15ce44 | 5092 | static int btrfs_dentry_delete(const struct dentry *dentry) |
76dda93c YZ |
5093 | { |
5094 | struct btrfs_root *root; | |
848cce0d | 5095 | struct inode *inode = dentry->d_inode; |
76dda93c | 5096 | |
848cce0d LZ |
5097 | if (!inode && !IS_ROOT(dentry)) |
5098 | inode = dentry->d_parent->d_inode; | |
76dda93c | 5099 | |
848cce0d LZ |
5100 | if (inode) { |
5101 | root = BTRFS_I(inode)->root; | |
efefb143 YZ |
5102 | if (btrfs_root_refs(&root->root_item) == 0) |
5103 | return 1; | |
848cce0d LZ |
5104 | |
5105 | if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) | |
5106 | return 1; | |
efefb143 | 5107 | } |
76dda93c YZ |
5108 | return 0; |
5109 | } | |
5110 | ||
b4aff1f8 JB |
5111 | static void btrfs_dentry_release(struct dentry *dentry) |
5112 | { | |
5113 | if (dentry->d_fsdata) | |
5114 | kfree(dentry->d_fsdata); | |
5115 | } | |
5116 | ||
3de4586c | 5117 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 5118 | unsigned int flags) |
3de4586c | 5119 | { |
5662344b | 5120 | struct inode *inode; |
a66e7cc6 | 5121 | |
5662344b TI |
5122 | inode = btrfs_lookup_dentry(dir, dentry); |
5123 | if (IS_ERR(inode)) { | |
5124 | if (PTR_ERR(inode) == -ENOENT) | |
5125 | inode = NULL; | |
5126 | else | |
5127 | return ERR_CAST(inode); | |
5128 | } | |
5129 | ||
5130 | return d_splice_alias(inode, dentry); | |
39279cc3 CM |
5131 | } |
5132 | ||
16cdcec7 | 5133 | unsigned char btrfs_filetype_table[] = { |
39279cc3 CM |
5134 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
5135 | }; | |
5136 | ||
9cdda8d3 | 5137 | static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) |
39279cc3 | 5138 | { |
9cdda8d3 | 5139 | struct inode *inode = file_inode(file); |
39279cc3 CM |
5140 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5141 | struct btrfs_item *item; | |
5142 | struct btrfs_dir_item *di; | |
5143 | struct btrfs_key key; | |
5f39d397 | 5144 | struct btrfs_key found_key; |
39279cc3 | 5145 | struct btrfs_path *path; |
16cdcec7 MX |
5146 | struct list_head ins_list; |
5147 | struct list_head del_list; | |
39279cc3 | 5148 | int ret; |
5f39d397 | 5149 | struct extent_buffer *leaf; |
39279cc3 | 5150 | int slot; |
39279cc3 CM |
5151 | unsigned char d_type; |
5152 | int over = 0; | |
5153 | u32 di_cur; | |
5154 | u32 di_total; | |
5155 | u32 di_len; | |
5156 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
5157 | char tmp_name[32]; |
5158 | char *name_ptr; | |
5159 | int name_len; | |
9cdda8d3 | 5160 | int is_curr = 0; /* ctx->pos points to the current index? */ |
39279cc3 CM |
5161 | |
5162 | /* FIXME, use a real flag for deciding about the key type */ | |
5163 | if (root->fs_info->tree_root == root) | |
5164 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 5165 | |
9cdda8d3 AV |
5166 | if (!dir_emit_dots(file, ctx)) |
5167 | return 0; | |
5168 | ||
49593bfa | 5169 | path = btrfs_alloc_path(); |
16cdcec7 MX |
5170 | if (!path) |
5171 | return -ENOMEM; | |
ff5714cc | 5172 | |
026fd317 | 5173 | path->reada = 1; |
49593bfa | 5174 | |
16cdcec7 MX |
5175 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5176 | INIT_LIST_HEAD(&ins_list); | |
5177 | INIT_LIST_HEAD(&del_list); | |
5178 | btrfs_get_delayed_items(inode, &ins_list, &del_list); | |
5179 | } | |
5180 | ||
39279cc3 | 5181 | btrfs_set_key_type(&key, key_type); |
9cdda8d3 | 5182 | key.offset = ctx->pos; |
33345d01 | 5183 | key.objectid = btrfs_ino(inode); |
5f39d397 | 5184 | |
39279cc3 CM |
5185 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5186 | if (ret < 0) | |
5187 | goto err; | |
49593bfa DW |
5188 | |
5189 | while (1) { | |
5f39d397 | 5190 | leaf = path->nodes[0]; |
39279cc3 | 5191 | slot = path->slots[0]; |
b9e03af0 LZ |
5192 | if (slot >= btrfs_header_nritems(leaf)) { |
5193 | ret = btrfs_next_leaf(root, path); | |
5194 | if (ret < 0) | |
5195 | goto err; | |
5196 | else if (ret > 0) | |
5197 | break; | |
5198 | continue; | |
39279cc3 | 5199 | } |
3de4586c | 5200 | |
dd3cc16b | 5201 | item = btrfs_item_nr(slot); |
5f39d397 CM |
5202 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
5203 | ||
5204 | if (found_key.objectid != key.objectid) | |
39279cc3 | 5205 | break; |
5f39d397 | 5206 | if (btrfs_key_type(&found_key) != key_type) |
39279cc3 | 5207 | break; |
9cdda8d3 | 5208 | if (found_key.offset < ctx->pos) |
b9e03af0 | 5209 | goto next; |
16cdcec7 MX |
5210 | if (key_type == BTRFS_DIR_INDEX_KEY && |
5211 | btrfs_should_delete_dir_index(&del_list, | |
5212 | found_key.offset)) | |
5213 | goto next; | |
5f39d397 | 5214 | |
9cdda8d3 | 5215 | ctx->pos = found_key.offset; |
16cdcec7 | 5216 | is_curr = 1; |
49593bfa | 5217 | |
39279cc3 CM |
5218 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
5219 | di_cur = 0; | |
5f39d397 | 5220 | di_total = btrfs_item_size(leaf, item); |
49593bfa DW |
5221 | |
5222 | while (di_cur < di_total) { | |
5f39d397 CM |
5223 | struct btrfs_key location; |
5224 | ||
22a94d44 JB |
5225 | if (verify_dir_item(root, leaf, di)) |
5226 | break; | |
5227 | ||
5f39d397 | 5228 | name_len = btrfs_dir_name_len(leaf, di); |
49593bfa | 5229 | if (name_len <= sizeof(tmp_name)) { |
5f39d397 CM |
5230 | name_ptr = tmp_name; |
5231 | } else { | |
5232 | name_ptr = kmalloc(name_len, GFP_NOFS); | |
49593bfa DW |
5233 | if (!name_ptr) { |
5234 | ret = -ENOMEM; | |
5235 | goto err; | |
5236 | } | |
5f39d397 CM |
5237 | } |
5238 | read_extent_buffer(leaf, name_ptr, | |
5239 | (unsigned long)(di + 1), name_len); | |
5240 | ||
5241 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
5242 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
3de4586c | 5243 | |
fede766f | 5244 | |
3de4586c | 5245 | /* is this a reference to our own snapshot? If so |
8c9c2bf7 AJ |
5246 | * skip it. |
5247 | * | |
5248 | * In contrast to old kernels, we insert the snapshot's | |
5249 | * dir item and dir index after it has been created, so | |
5250 | * we won't find a reference to our own snapshot. We | |
5251 | * still keep the following code for backward | |
5252 | * compatibility. | |
3de4586c CM |
5253 | */ |
5254 | if (location.type == BTRFS_ROOT_ITEM_KEY && | |
5255 | location.objectid == root->root_key.objectid) { | |
5256 | over = 0; | |
5257 | goto skip; | |
5258 | } | |
9cdda8d3 AV |
5259 | over = !dir_emit(ctx, name_ptr, name_len, |
5260 | location.objectid, d_type); | |
5f39d397 | 5261 | |
3de4586c | 5262 | skip: |
5f39d397 CM |
5263 | if (name_ptr != tmp_name) |
5264 | kfree(name_ptr); | |
5265 | ||
39279cc3 CM |
5266 | if (over) |
5267 | goto nopos; | |
5103e947 | 5268 | di_len = btrfs_dir_name_len(leaf, di) + |
49593bfa | 5269 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
39279cc3 CM |
5270 | di_cur += di_len; |
5271 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
5272 | } | |
b9e03af0 LZ |
5273 | next: |
5274 | path->slots[0]++; | |
39279cc3 | 5275 | } |
49593bfa | 5276 | |
16cdcec7 MX |
5277 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
5278 | if (is_curr) | |
9cdda8d3 AV |
5279 | ctx->pos++; |
5280 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); | |
16cdcec7 MX |
5281 | if (ret) |
5282 | goto nopos; | |
5283 | } | |
5284 | ||
49593bfa | 5285 | /* Reached end of directory/root. Bump pos past the last item. */ |
db62efbb ZB |
5286 | ctx->pos++; |
5287 | ||
5288 | /* | |
5289 | * Stop new entries from being returned after we return the last | |
5290 | * entry. | |
5291 | * | |
5292 | * New directory entries are assigned a strictly increasing | |
5293 | * offset. This means that new entries created during readdir | |
5294 | * are *guaranteed* to be seen in the future by that readdir. | |
5295 | * This has broken buggy programs which operate on names as | |
5296 | * they're returned by readdir. Until we re-use freed offsets | |
5297 | * we have this hack to stop new entries from being returned | |
5298 | * under the assumption that they'll never reach this huge | |
5299 | * offset. | |
5300 | * | |
5301 | * This is being careful not to overflow 32bit loff_t unless the | |
5302 | * last entry requires it because doing so has broken 32bit apps | |
5303 | * in the past. | |
5304 | */ | |
5305 | if (key_type == BTRFS_DIR_INDEX_KEY) { | |
5306 | if (ctx->pos >= INT_MAX) | |
5307 | ctx->pos = LLONG_MAX; | |
5308 | else | |
5309 | ctx->pos = INT_MAX; | |
5310 | } | |
39279cc3 CM |
5311 | nopos: |
5312 | ret = 0; | |
5313 | err: | |
16cdcec7 MX |
5314 | if (key_type == BTRFS_DIR_INDEX_KEY) |
5315 | btrfs_put_delayed_items(&ins_list, &del_list); | |
39279cc3 | 5316 | btrfs_free_path(path); |
39279cc3 CM |
5317 | return ret; |
5318 | } | |
5319 | ||
a9185b41 | 5320 | int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
39279cc3 CM |
5321 | { |
5322 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5323 | struct btrfs_trans_handle *trans; | |
5324 | int ret = 0; | |
0af3d00b | 5325 | bool nolock = false; |
39279cc3 | 5326 | |
72ac3c0d | 5327 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
4ca8b41e CM |
5328 | return 0; |
5329 | ||
83eea1f1 | 5330 | if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode)) |
82d5902d | 5331 | nolock = true; |
0af3d00b | 5332 | |
a9185b41 | 5333 | if (wbc->sync_mode == WB_SYNC_ALL) { |
0af3d00b | 5334 | if (nolock) |
7a7eaa40 | 5335 | trans = btrfs_join_transaction_nolock(root); |
0af3d00b | 5336 | else |
7a7eaa40 | 5337 | trans = btrfs_join_transaction(root); |
3612b495 TI |
5338 | if (IS_ERR(trans)) |
5339 | return PTR_ERR(trans); | |
a698d075 | 5340 | ret = btrfs_commit_transaction(trans, root); |
39279cc3 CM |
5341 | } |
5342 | return ret; | |
5343 | } | |
5344 | ||
5345 | /* | |
54aa1f4d | 5346 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
5347 | * inode changes. But, it is most likely to find the inode in cache. |
5348 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
5349 | * to keep or drop this code. | |
5350 | */ | |
48a3b636 | 5351 | static int btrfs_dirty_inode(struct inode *inode) |
39279cc3 CM |
5352 | { |
5353 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5354 | struct btrfs_trans_handle *trans; | |
8929ecfa YZ |
5355 | int ret; |
5356 | ||
72ac3c0d | 5357 | if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags)) |
22c44fe6 | 5358 | return 0; |
39279cc3 | 5359 | |
7a7eaa40 | 5360 | trans = btrfs_join_transaction(root); |
22c44fe6 JB |
5361 | if (IS_ERR(trans)) |
5362 | return PTR_ERR(trans); | |
8929ecfa YZ |
5363 | |
5364 | ret = btrfs_update_inode(trans, root, inode); | |
94b60442 CM |
5365 | if (ret && ret == -ENOSPC) { |
5366 | /* whoops, lets try again with the full transaction */ | |
5367 | btrfs_end_transaction(trans, root); | |
5368 | trans = btrfs_start_transaction(root, 1); | |
22c44fe6 JB |
5369 | if (IS_ERR(trans)) |
5370 | return PTR_ERR(trans); | |
8929ecfa | 5371 | |
94b60442 | 5372 | ret = btrfs_update_inode(trans, root, inode); |
94b60442 | 5373 | } |
39279cc3 | 5374 | btrfs_end_transaction(trans, root); |
16cdcec7 MX |
5375 | if (BTRFS_I(inode)->delayed_node) |
5376 | btrfs_balance_delayed_items(root); | |
22c44fe6 JB |
5377 | |
5378 | return ret; | |
5379 | } | |
5380 | ||
5381 | /* | |
5382 | * This is a copy of file_update_time. We need this so we can return error on | |
5383 | * ENOSPC for updating the inode in the case of file write and mmap writes. | |
5384 | */ | |
e41f941a JB |
5385 | static int btrfs_update_time(struct inode *inode, struct timespec *now, |
5386 | int flags) | |
22c44fe6 | 5387 | { |
2bc55652 AB |
5388 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5389 | ||
5390 | if (btrfs_root_readonly(root)) | |
5391 | return -EROFS; | |
5392 | ||
e41f941a | 5393 | if (flags & S_VERSION) |
22c44fe6 | 5394 | inode_inc_iversion(inode); |
e41f941a JB |
5395 | if (flags & S_CTIME) |
5396 | inode->i_ctime = *now; | |
5397 | if (flags & S_MTIME) | |
5398 | inode->i_mtime = *now; | |
5399 | if (flags & S_ATIME) | |
5400 | inode->i_atime = *now; | |
5401 | return btrfs_dirty_inode(inode); | |
39279cc3 CM |
5402 | } |
5403 | ||
d352ac68 CM |
5404 | /* |
5405 | * find the highest existing sequence number in a directory | |
5406 | * and then set the in-memory index_cnt variable to reflect | |
5407 | * free sequence numbers | |
5408 | */ | |
aec7477b JB |
5409 | static int btrfs_set_inode_index_count(struct inode *inode) |
5410 | { | |
5411 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5412 | struct btrfs_key key, found_key; | |
5413 | struct btrfs_path *path; | |
5414 | struct extent_buffer *leaf; | |
5415 | int ret; | |
5416 | ||
33345d01 | 5417 | key.objectid = btrfs_ino(inode); |
aec7477b JB |
5418 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
5419 | key.offset = (u64)-1; | |
5420 | ||
5421 | path = btrfs_alloc_path(); | |
5422 | if (!path) | |
5423 | return -ENOMEM; | |
5424 | ||
5425 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
5426 | if (ret < 0) | |
5427 | goto out; | |
5428 | /* FIXME: we should be able to handle this */ | |
5429 | if (ret == 0) | |
5430 | goto out; | |
5431 | ret = 0; | |
5432 | ||
5433 | /* | |
5434 | * MAGIC NUMBER EXPLANATION: | |
5435 | * since we search a directory based on f_pos we have to start at 2 | |
5436 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
5437 | * else has to start at 2 | |
5438 | */ | |
5439 | if (path->slots[0] == 0) { | |
5440 | BTRFS_I(inode)->index_cnt = 2; | |
5441 | goto out; | |
5442 | } | |
5443 | ||
5444 | path->slots[0]--; | |
5445 | ||
5446 | leaf = path->nodes[0]; | |
5447 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
5448 | ||
33345d01 | 5449 | if (found_key.objectid != btrfs_ino(inode) || |
aec7477b JB |
5450 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { |
5451 | BTRFS_I(inode)->index_cnt = 2; | |
5452 | goto out; | |
5453 | } | |
5454 | ||
5455 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
5456 | out: | |
5457 | btrfs_free_path(path); | |
5458 | return ret; | |
5459 | } | |
5460 | ||
d352ac68 CM |
5461 | /* |
5462 | * helper to find a free sequence number in a given directory. This current | |
5463 | * code is very simple, later versions will do smarter things in the btree | |
5464 | */ | |
3de4586c | 5465 | int btrfs_set_inode_index(struct inode *dir, u64 *index) |
aec7477b JB |
5466 | { |
5467 | int ret = 0; | |
5468 | ||
5469 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
16cdcec7 MX |
5470 | ret = btrfs_inode_delayed_dir_index_count(dir); |
5471 | if (ret) { | |
5472 | ret = btrfs_set_inode_index_count(dir); | |
5473 | if (ret) | |
5474 | return ret; | |
5475 | } | |
aec7477b JB |
5476 | } |
5477 | ||
00e4e6b3 | 5478 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
5479 | BTRFS_I(dir)->index_cnt++; |
5480 | ||
5481 | return ret; | |
5482 | } | |
5483 | ||
39279cc3 CM |
5484 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
5485 | struct btrfs_root *root, | |
aec7477b | 5486 | struct inode *dir, |
9c58309d | 5487 | const char *name, int name_len, |
175a4eb7 AV |
5488 | u64 ref_objectid, u64 objectid, |
5489 | umode_t mode, u64 *index) | |
39279cc3 CM |
5490 | { |
5491 | struct inode *inode; | |
5f39d397 | 5492 | struct btrfs_inode_item *inode_item; |
39279cc3 | 5493 | struct btrfs_key *location; |
5f39d397 | 5494 | struct btrfs_path *path; |
9c58309d CM |
5495 | struct btrfs_inode_ref *ref; |
5496 | struct btrfs_key key[2]; | |
5497 | u32 sizes[2]; | |
5498 | unsigned long ptr; | |
39279cc3 | 5499 | int ret; |
39279cc3 | 5500 | |
5f39d397 | 5501 | path = btrfs_alloc_path(); |
d8926bb3 MF |
5502 | if (!path) |
5503 | return ERR_PTR(-ENOMEM); | |
5f39d397 | 5504 | |
39279cc3 | 5505 | inode = new_inode(root->fs_info->sb); |
8fb27640 YS |
5506 | if (!inode) { |
5507 | btrfs_free_path(path); | |
39279cc3 | 5508 | return ERR_PTR(-ENOMEM); |
8fb27640 | 5509 | } |
39279cc3 | 5510 | |
581bb050 LZ |
5511 | /* |
5512 | * we have to initialize this early, so we can reclaim the inode | |
5513 | * number if we fail afterwards in this function. | |
5514 | */ | |
5515 | inode->i_ino = objectid; | |
5516 | ||
aec7477b | 5517 | if (dir) { |
1abe9b8a | 5518 | trace_btrfs_inode_request(dir); |
5519 | ||
3de4586c | 5520 | ret = btrfs_set_inode_index(dir, index); |
09771430 | 5521 | if (ret) { |
8fb27640 | 5522 | btrfs_free_path(path); |
09771430 | 5523 | iput(inode); |
aec7477b | 5524 | return ERR_PTR(ret); |
09771430 | 5525 | } |
aec7477b JB |
5526 | } |
5527 | /* | |
5528 | * index_cnt is ignored for everything but a dir, | |
5529 | * btrfs_get_inode_index_count has an explanation for the magic | |
5530 | * number | |
5531 | */ | |
5532 | BTRFS_I(inode)->index_cnt = 2; | |
67de1176 | 5533 | BTRFS_I(inode)->dir_index = *index; |
39279cc3 | 5534 | BTRFS_I(inode)->root = root; |
e02119d5 | 5535 | BTRFS_I(inode)->generation = trans->transid; |
76195853 | 5536 | inode->i_generation = BTRFS_I(inode)->generation; |
b888db2b | 5537 | |
5dc562c5 JB |
5538 | /* |
5539 | * We could have gotten an inode number from somebody who was fsynced | |
5540 | * and then removed in this same transaction, so let's just set full | |
5541 | * sync since it will be a full sync anyway and this will blow away the | |
5542 | * old info in the log. | |
5543 | */ | |
5544 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
5545 | ||
9c58309d CM |
5546 | key[0].objectid = objectid; |
5547 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); | |
5548 | key[0].offset = 0; | |
5549 | ||
f186373f MF |
5550 | /* |
5551 | * Start new inodes with an inode_ref. This is slightly more | |
5552 | * efficient for small numbers of hard links since they will | |
5553 | * be packed into one item. Extended refs will kick in if we | |
5554 | * add more hard links than can fit in the ref item. | |
5555 | */ | |
9c58309d CM |
5556 | key[1].objectid = objectid; |
5557 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); | |
5558 | key[1].offset = ref_objectid; | |
5559 | ||
5560 | sizes[0] = sizeof(struct btrfs_inode_item); | |
5561 | sizes[1] = name_len + sizeof(*ref); | |
5562 | ||
b9473439 | 5563 | path->leave_spinning = 1; |
9c58309d CM |
5564 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); |
5565 | if (ret != 0) | |
5f39d397 CM |
5566 | goto fail; |
5567 | ||
ecc11fab | 5568 | inode_init_owner(inode, dir, mode); |
a76a3cd4 | 5569 | inode_set_bytes(inode, 0); |
39279cc3 | 5570 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
5f39d397 CM |
5571 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
5572 | struct btrfs_inode_item); | |
293f7e07 LZ |
5573 | memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, |
5574 | sizeof(*inode_item)); | |
e02119d5 | 5575 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d CM |
5576 | |
5577 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
5578 | struct btrfs_inode_ref); | |
5579 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
00e4e6b3 | 5580 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
9c58309d CM |
5581 | ptr = (unsigned long)(ref + 1); |
5582 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
5583 | ||
5f39d397 CM |
5584 | btrfs_mark_buffer_dirty(path->nodes[0]); |
5585 | btrfs_free_path(path); | |
5586 | ||
39279cc3 CM |
5587 | location = &BTRFS_I(inode)->location; |
5588 | location->objectid = objectid; | |
39279cc3 CM |
5589 | location->offset = 0; |
5590 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | |
5591 | ||
6cbff00f CH |
5592 | btrfs_inherit_iflags(inode, dir); |
5593 | ||
569254b0 | 5594 | if (S_ISREG(mode)) { |
94272164 CM |
5595 | if (btrfs_test_opt(root, NODATASUM)) |
5596 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
213490b3 | 5597 | if (btrfs_test_opt(root, NODATACOW)) |
f2bdf9a8 JB |
5598 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | |
5599 | BTRFS_INODE_NODATASUM; | |
94272164 CM |
5600 | } |
5601 | ||
778ba82b | 5602 | btrfs_insert_inode_hash(inode); |
5d4f98a2 | 5603 | inode_tree_add(inode); |
1abe9b8a | 5604 | |
5605 | trace_btrfs_inode_new(inode); | |
1973f0fa | 5606 | btrfs_set_inode_last_trans(trans, inode); |
1abe9b8a | 5607 | |
8ea05e3a AB |
5608 | btrfs_update_root_times(trans, root); |
5609 | ||
39279cc3 | 5610 | return inode; |
5f39d397 | 5611 | fail: |
aec7477b JB |
5612 | if (dir) |
5613 | BTRFS_I(dir)->index_cnt--; | |
5f39d397 | 5614 | btrfs_free_path(path); |
09771430 | 5615 | iput(inode); |
5f39d397 | 5616 | return ERR_PTR(ret); |
39279cc3 CM |
5617 | } |
5618 | ||
5619 | static inline u8 btrfs_inode_type(struct inode *inode) | |
5620 | { | |
5621 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
5622 | } | |
5623 | ||
d352ac68 CM |
5624 | /* |
5625 | * utility function to add 'inode' into 'parent_inode' with | |
5626 | * a give name and a given sequence number. | |
5627 | * if 'add_backref' is true, also insert a backref from the | |
5628 | * inode to the parent directory. | |
5629 | */ | |
e02119d5 CM |
5630 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
5631 | struct inode *parent_inode, struct inode *inode, | |
5632 | const char *name, int name_len, int add_backref, u64 index) | |
39279cc3 | 5633 | { |
4df27c4d | 5634 | int ret = 0; |
39279cc3 | 5635 | struct btrfs_key key; |
e02119d5 | 5636 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
33345d01 LZ |
5637 | u64 ino = btrfs_ino(inode); |
5638 | u64 parent_ino = btrfs_ino(parent_inode); | |
5f39d397 | 5639 | |
33345d01 | 5640 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
5641 | memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); |
5642 | } else { | |
33345d01 | 5643 | key.objectid = ino; |
4df27c4d YZ |
5644 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
5645 | key.offset = 0; | |
5646 | } | |
5647 | ||
33345d01 | 5648 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
5649 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
5650 | key.objectid, root->root_key.objectid, | |
33345d01 | 5651 | parent_ino, index, name, name_len); |
4df27c4d | 5652 | } else if (add_backref) { |
33345d01 LZ |
5653 | ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, |
5654 | parent_ino, index); | |
4df27c4d | 5655 | } |
39279cc3 | 5656 | |
79787eaa JM |
5657 | /* Nothing to clean up yet */ |
5658 | if (ret) | |
5659 | return ret; | |
4df27c4d | 5660 | |
79787eaa JM |
5661 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
5662 | parent_inode, &key, | |
5663 | btrfs_inode_type(inode), index); | |
9c52057c | 5664 | if (ret == -EEXIST || ret == -EOVERFLOW) |
79787eaa JM |
5665 | goto fail_dir_item; |
5666 | else if (ret) { | |
5667 | btrfs_abort_transaction(trans, root, ret); | |
5668 | return ret; | |
39279cc3 | 5669 | } |
79787eaa JM |
5670 | |
5671 | btrfs_i_size_write(parent_inode, parent_inode->i_size + | |
5672 | name_len * 2); | |
0c4d2d95 | 5673 | inode_inc_iversion(parent_inode); |
79787eaa JM |
5674 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
5675 | ret = btrfs_update_inode(trans, root, parent_inode); | |
5676 | if (ret) | |
5677 | btrfs_abort_transaction(trans, root, ret); | |
39279cc3 | 5678 | return ret; |
fe66a05a CM |
5679 | |
5680 | fail_dir_item: | |
5681 | if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { | |
5682 | u64 local_index; | |
5683 | int err; | |
5684 | err = btrfs_del_root_ref(trans, root->fs_info->tree_root, | |
5685 | key.objectid, root->root_key.objectid, | |
5686 | parent_ino, &local_index, name, name_len); | |
5687 | ||
5688 | } else if (add_backref) { | |
5689 | u64 local_index; | |
5690 | int err; | |
5691 | ||
5692 | err = btrfs_del_inode_ref(trans, root, name, name_len, | |
5693 | ino, parent_ino, &local_index); | |
5694 | } | |
5695 | return ret; | |
39279cc3 CM |
5696 | } |
5697 | ||
5698 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
a1b075d2 JB |
5699 | struct inode *dir, struct dentry *dentry, |
5700 | struct inode *inode, int backref, u64 index) | |
39279cc3 | 5701 | { |
a1b075d2 JB |
5702 | int err = btrfs_add_link(trans, dir, inode, |
5703 | dentry->d_name.name, dentry->d_name.len, | |
5704 | backref, index); | |
39279cc3 CM |
5705 | if (err > 0) |
5706 | err = -EEXIST; | |
5707 | return err; | |
5708 | } | |
5709 | ||
618e21d5 | 5710 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1a67aafb | 5711 | umode_t mode, dev_t rdev) |
618e21d5 JB |
5712 | { |
5713 | struct btrfs_trans_handle *trans; | |
5714 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 5715 | struct inode *inode = NULL; |
618e21d5 JB |
5716 | int err; |
5717 | int drop_inode = 0; | |
5718 | u64 objectid; | |
00e4e6b3 | 5719 | u64 index = 0; |
618e21d5 JB |
5720 | |
5721 | if (!new_valid_dev(rdev)) | |
5722 | return -EINVAL; | |
5723 | ||
9ed74f2d JB |
5724 | /* |
5725 | * 2 for inode item and ref | |
5726 | * 2 for dir items | |
5727 | * 1 for xattr if selinux is on | |
5728 | */ | |
a22285a6 YZ |
5729 | trans = btrfs_start_transaction(root, 5); |
5730 | if (IS_ERR(trans)) | |
5731 | return PTR_ERR(trans); | |
1832a6d5 | 5732 | |
581bb050 LZ |
5733 | err = btrfs_find_free_ino(root, &objectid); |
5734 | if (err) | |
5735 | goto out_unlock; | |
5736 | ||
aec7477b | 5737 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 5738 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 5739 | mode, &index); |
7cf96da3 TI |
5740 | if (IS_ERR(inode)) { |
5741 | err = PTR_ERR(inode); | |
618e21d5 | 5742 | goto out_unlock; |
7cf96da3 | 5743 | } |
618e21d5 | 5744 | |
2a7dba39 | 5745 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf JB |
5746 | if (err) { |
5747 | drop_inode = 1; | |
5748 | goto out_unlock; | |
5749 | } | |
5750 | ||
ad19db71 CS |
5751 | /* |
5752 | * If the active LSM wants to access the inode during | |
5753 | * d_instantiate it needs these. Smack checks to see | |
5754 | * if the filesystem supports xattrs by looking at the | |
5755 | * ops vector. | |
5756 | */ | |
5757 | ||
5758 | inode->i_op = &btrfs_special_inode_operations; | |
a1b075d2 | 5759 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
618e21d5 JB |
5760 | if (err) |
5761 | drop_inode = 1; | |
5762 | else { | |
618e21d5 | 5763 | init_special_inode(inode, inode->i_mode, rdev); |
1b4ab1bb | 5764 | btrfs_update_inode(trans, root, inode); |
08c422c2 | 5765 | d_instantiate(dentry, inode); |
618e21d5 | 5766 | } |
618e21d5 | 5767 | out_unlock: |
7ad85bb7 | 5768 | btrfs_end_transaction(trans, root); |
b53d3f5d | 5769 | btrfs_btree_balance_dirty(root); |
618e21d5 JB |
5770 | if (drop_inode) { |
5771 | inode_dec_link_count(inode); | |
5772 | iput(inode); | |
5773 | } | |
618e21d5 JB |
5774 | return err; |
5775 | } | |
5776 | ||
39279cc3 | 5777 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 5778 | umode_t mode, bool excl) |
39279cc3 CM |
5779 | { |
5780 | struct btrfs_trans_handle *trans; | |
5781 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 5782 | struct inode *inode = NULL; |
43baa579 | 5783 | int drop_inode_on_err = 0; |
a22285a6 | 5784 | int err; |
39279cc3 | 5785 | u64 objectid; |
00e4e6b3 | 5786 | u64 index = 0; |
39279cc3 | 5787 | |
9ed74f2d JB |
5788 | /* |
5789 | * 2 for inode item and ref | |
5790 | * 2 for dir items | |
5791 | * 1 for xattr if selinux is on | |
5792 | */ | |
a22285a6 YZ |
5793 | trans = btrfs_start_transaction(root, 5); |
5794 | if (IS_ERR(trans)) | |
5795 | return PTR_ERR(trans); | |
9ed74f2d | 5796 | |
581bb050 LZ |
5797 | err = btrfs_find_free_ino(root, &objectid); |
5798 | if (err) | |
5799 | goto out_unlock; | |
5800 | ||
aec7477b | 5801 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 5802 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 5803 | mode, &index); |
7cf96da3 TI |
5804 | if (IS_ERR(inode)) { |
5805 | err = PTR_ERR(inode); | |
39279cc3 | 5806 | goto out_unlock; |
7cf96da3 | 5807 | } |
43baa579 | 5808 | drop_inode_on_err = 1; |
39279cc3 | 5809 | |
2a7dba39 | 5810 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
43baa579 | 5811 | if (err) |
33268eaf | 5812 | goto out_unlock; |
33268eaf | 5813 | |
9185aa58 FB |
5814 | err = btrfs_update_inode(trans, root, inode); |
5815 | if (err) | |
5816 | goto out_unlock; | |
5817 | ||
ad19db71 CS |
5818 | /* |
5819 | * If the active LSM wants to access the inode during | |
5820 | * d_instantiate it needs these. Smack checks to see | |
5821 | * if the filesystem supports xattrs by looking at the | |
5822 | * ops vector. | |
5823 | */ | |
5824 | inode->i_fop = &btrfs_file_operations; | |
5825 | inode->i_op = &btrfs_file_inode_operations; | |
5826 | ||
a1b075d2 | 5827 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 | 5828 | if (err) |
43baa579 FB |
5829 | goto out_unlock; |
5830 | ||
5831 | inode->i_mapping->a_ops = &btrfs_aops; | |
5832 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; | |
5833 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; | |
5834 | d_instantiate(dentry, inode); | |
5835 | ||
39279cc3 | 5836 | out_unlock: |
7ad85bb7 | 5837 | btrfs_end_transaction(trans, root); |
43baa579 | 5838 | if (err && drop_inode_on_err) { |
39279cc3 CM |
5839 | inode_dec_link_count(inode); |
5840 | iput(inode); | |
5841 | } | |
b53d3f5d | 5842 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
5843 | return err; |
5844 | } | |
5845 | ||
5846 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
5847 | struct dentry *dentry) | |
5848 | { | |
5849 | struct btrfs_trans_handle *trans; | |
5850 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
5851 | struct inode *inode = old_dentry->d_inode; | |
00e4e6b3 | 5852 | u64 index; |
39279cc3 CM |
5853 | int err; |
5854 | int drop_inode = 0; | |
5855 | ||
4a8be425 TH |
5856 | /* do not allow sys_link's with other subvols of the same device */ |
5857 | if (root->objectid != BTRFS_I(inode)->root->objectid) | |
3ab3564f | 5858 | return -EXDEV; |
4a8be425 | 5859 | |
f186373f | 5860 | if (inode->i_nlink >= BTRFS_LINK_MAX) |
c055e99e | 5861 | return -EMLINK; |
4a8be425 | 5862 | |
3de4586c | 5863 | err = btrfs_set_inode_index(dir, &index); |
aec7477b JB |
5864 | if (err) |
5865 | goto fail; | |
5866 | ||
a22285a6 | 5867 | /* |
7e6b6465 | 5868 | * 2 items for inode and inode ref |
a22285a6 | 5869 | * 2 items for dir items |
7e6b6465 | 5870 | * 1 item for parent inode |
a22285a6 | 5871 | */ |
7e6b6465 | 5872 | trans = btrfs_start_transaction(root, 5); |
a22285a6 YZ |
5873 | if (IS_ERR(trans)) { |
5874 | err = PTR_ERR(trans); | |
5875 | goto fail; | |
5876 | } | |
5f39d397 | 5877 | |
67de1176 MX |
5878 | /* There are several dir indexes for this inode, clear the cache. */ |
5879 | BTRFS_I(inode)->dir_index = 0ULL; | |
8b558c5f | 5880 | inc_nlink(inode); |
0c4d2d95 | 5881 | inode_inc_iversion(inode); |
3153495d | 5882 | inode->i_ctime = CURRENT_TIME; |
7de9c6ee | 5883 | ihold(inode); |
e9976151 | 5884 | set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); |
aec7477b | 5885 | |
a1b075d2 | 5886 | err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); |
5f39d397 | 5887 | |
a5719521 | 5888 | if (err) { |
54aa1f4d | 5889 | drop_inode = 1; |
a5719521 | 5890 | } else { |
10d9f309 | 5891 | struct dentry *parent = dentry->d_parent; |
a5719521 | 5892 | err = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
5893 | if (err) |
5894 | goto fail; | |
08c422c2 | 5895 | d_instantiate(dentry, inode); |
6a912213 | 5896 | btrfs_log_new_name(trans, inode, NULL, parent); |
a5719521 | 5897 | } |
39279cc3 | 5898 | |
7ad85bb7 | 5899 | btrfs_end_transaction(trans, root); |
1832a6d5 | 5900 | fail: |
39279cc3 CM |
5901 | if (drop_inode) { |
5902 | inode_dec_link_count(inode); | |
5903 | iput(inode); | |
5904 | } | |
b53d3f5d | 5905 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
5906 | return err; |
5907 | } | |
5908 | ||
18bb1db3 | 5909 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
39279cc3 | 5910 | { |
b9d86667 | 5911 | struct inode *inode = NULL; |
39279cc3 CM |
5912 | struct btrfs_trans_handle *trans; |
5913 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
5914 | int err = 0; | |
5915 | int drop_on_err = 0; | |
b9d86667 | 5916 | u64 objectid = 0; |
00e4e6b3 | 5917 | u64 index = 0; |
39279cc3 | 5918 | |
9ed74f2d JB |
5919 | /* |
5920 | * 2 items for inode and ref | |
5921 | * 2 items for dir items | |
5922 | * 1 for xattr if selinux is on | |
5923 | */ | |
a22285a6 YZ |
5924 | trans = btrfs_start_transaction(root, 5); |
5925 | if (IS_ERR(trans)) | |
5926 | return PTR_ERR(trans); | |
39279cc3 | 5927 | |
581bb050 LZ |
5928 | err = btrfs_find_free_ino(root, &objectid); |
5929 | if (err) | |
5930 | goto out_fail; | |
5931 | ||
aec7477b | 5932 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 5933 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 5934 | S_IFDIR | mode, &index); |
39279cc3 CM |
5935 | if (IS_ERR(inode)) { |
5936 | err = PTR_ERR(inode); | |
5937 | goto out_fail; | |
5938 | } | |
5f39d397 | 5939 | |
39279cc3 | 5940 | drop_on_err = 1; |
33268eaf | 5941 | |
2a7dba39 | 5942 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf JB |
5943 | if (err) |
5944 | goto out_fail; | |
5945 | ||
39279cc3 CM |
5946 | inode->i_op = &btrfs_dir_inode_operations; |
5947 | inode->i_fop = &btrfs_dir_file_operations; | |
39279cc3 | 5948 | |
dbe674a9 | 5949 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
5950 | err = btrfs_update_inode(trans, root, inode); |
5951 | if (err) | |
5952 | goto out_fail; | |
5f39d397 | 5953 | |
a1b075d2 JB |
5954 | err = btrfs_add_link(trans, dir, inode, dentry->d_name.name, |
5955 | dentry->d_name.len, 0, index); | |
39279cc3 CM |
5956 | if (err) |
5957 | goto out_fail; | |
5f39d397 | 5958 | |
39279cc3 CM |
5959 | d_instantiate(dentry, inode); |
5960 | drop_on_err = 0; | |
39279cc3 CM |
5961 | |
5962 | out_fail: | |
7ad85bb7 | 5963 | btrfs_end_transaction(trans, root); |
39279cc3 CM |
5964 | if (drop_on_err) |
5965 | iput(inode); | |
b53d3f5d | 5966 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
5967 | return err; |
5968 | } | |
5969 | ||
d352ac68 CM |
5970 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
5971 | * and an extent that you want to insert, deal with overlap and insert | |
5972 | * the new extent into the tree. | |
5973 | */ | |
3b951516 CM |
5974 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
5975 | struct extent_map *existing, | |
e6dcd2dc CM |
5976 | struct extent_map *em, |
5977 | u64 map_start, u64 map_len) | |
3b951516 CM |
5978 | { |
5979 | u64 start_diff; | |
3b951516 | 5980 | |
e6dcd2dc CM |
5981 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
5982 | start_diff = map_start - em->start; | |
5983 | em->start = map_start; | |
5984 | em->len = map_len; | |
c8b97818 CM |
5985 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
5986 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 5987 | em->block_start += start_diff; |
c8b97818 CM |
5988 | em->block_len -= start_diff; |
5989 | } | |
09a2a8f9 | 5990 | return add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
5991 | } |
5992 | ||
c8b97818 CM |
5993 | static noinline int uncompress_inline(struct btrfs_path *path, |
5994 | struct inode *inode, struct page *page, | |
5995 | size_t pg_offset, u64 extent_offset, | |
5996 | struct btrfs_file_extent_item *item) | |
5997 | { | |
5998 | int ret; | |
5999 | struct extent_buffer *leaf = path->nodes[0]; | |
6000 | char *tmp; | |
6001 | size_t max_size; | |
6002 | unsigned long inline_size; | |
6003 | unsigned long ptr; | |
261507a0 | 6004 | int compress_type; |
c8b97818 CM |
6005 | |
6006 | WARN_ON(pg_offset != 0); | |
261507a0 | 6007 | compress_type = btrfs_file_extent_compression(leaf, item); |
c8b97818 CM |
6008 | max_size = btrfs_file_extent_ram_bytes(leaf, item); |
6009 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
dd3cc16b | 6010 | btrfs_item_nr(path->slots[0])); |
c8b97818 | 6011 | tmp = kmalloc(inline_size, GFP_NOFS); |
8d413713 TI |
6012 | if (!tmp) |
6013 | return -ENOMEM; | |
c8b97818 CM |
6014 | ptr = btrfs_file_extent_inline_start(item); |
6015 | ||
6016 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
6017 | ||
5b050f04 | 6018 | max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size); |
261507a0 LZ |
6019 | ret = btrfs_decompress(compress_type, tmp, page, |
6020 | extent_offset, inline_size, max_size); | |
c8b97818 | 6021 | if (ret) { |
7ac687d9 | 6022 | char *kaddr = kmap_atomic(page); |
c8b97818 CM |
6023 | unsigned long copy_size = min_t(u64, |
6024 | PAGE_CACHE_SIZE - pg_offset, | |
6025 | max_size - extent_offset); | |
6026 | memset(kaddr + pg_offset, 0, copy_size); | |
7ac687d9 | 6027 | kunmap_atomic(kaddr); |
c8b97818 CM |
6028 | } |
6029 | kfree(tmp); | |
6030 | return 0; | |
6031 | } | |
6032 | ||
d352ac68 CM |
6033 | /* |
6034 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
d397712b CM |
6035 | * the ugly parts come from merging extents from the disk with the in-ram |
6036 | * representation. This gets more complex because of the data=ordered code, | |
d352ac68 CM |
6037 | * where the in-ram extents might be locked pending data=ordered completion. |
6038 | * | |
6039 | * This also copies inline extents directly into the page. | |
6040 | */ | |
d397712b | 6041 | |
a52d9a80 | 6042 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 6043 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
6044 | int create) |
6045 | { | |
6046 | int ret; | |
6047 | int err = 0; | |
db94535d | 6048 | u64 bytenr; |
a52d9a80 CM |
6049 | u64 extent_start = 0; |
6050 | u64 extent_end = 0; | |
33345d01 | 6051 | u64 objectid = btrfs_ino(inode); |
a52d9a80 | 6052 | u32 found_type; |
f421950f | 6053 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
6054 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6055 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
6056 | struct extent_buffer *leaf; |
6057 | struct btrfs_key found_key; | |
a52d9a80 CM |
6058 | struct extent_map *em = NULL; |
6059 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 6060 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 | 6061 | struct btrfs_trans_handle *trans = NULL; |
261507a0 | 6062 | int compress_type; |
a52d9a80 | 6063 | |
a52d9a80 | 6064 | again: |
890871be | 6065 | read_lock(&em_tree->lock); |
d1310b2e | 6066 | em = lookup_extent_mapping(em_tree, start, len); |
a061fc8d CM |
6067 | if (em) |
6068 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
890871be | 6069 | read_unlock(&em_tree->lock); |
d1310b2e | 6070 | |
a52d9a80 | 6071 | if (em) { |
e1c4b745 CM |
6072 | if (em->start > start || em->start + em->len <= start) |
6073 | free_extent_map(em); | |
6074 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
6075 | free_extent_map(em); |
6076 | else | |
6077 | goto out; | |
a52d9a80 | 6078 | } |
172ddd60 | 6079 | em = alloc_extent_map(); |
a52d9a80 | 6080 | if (!em) { |
d1310b2e CM |
6081 | err = -ENOMEM; |
6082 | goto out; | |
a52d9a80 | 6083 | } |
e6dcd2dc | 6084 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
d1310b2e | 6085 | em->start = EXTENT_MAP_HOLE; |
445a6944 | 6086 | em->orig_start = EXTENT_MAP_HOLE; |
d1310b2e | 6087 | em->len = (u64)-1; |
c8b97818 | 6088 | em->block_len = (u64)-1; |
f421950f CM |
6089 | |
6090 | if (!path) { | |
6091 | path = btrfs_alloc_path(); | |
026fd317 JB |
6092 | if (!path) { |
6093 | err = -ENOMEM; | |
6094 | goto out; | |
6095 | } | |
6096 | /* | |
6097 | * Chances are we'll be called again, so go ahead and do | |
6098 | * readahead | |
6099 | */ | |
6100 | path->reada = 1; | |
f421950f CM |
6101 | } |
6102 | ||
179e29e4 CM |
6103 | ret = btrfs_lookup_file_extent(trans, root, path, |
6104 | objectid, start, trans != NULL); | |
a52d9a80 CM |
6105 | if (ret < 0) { |
6106 | err = ret; | |
6107 | goto out; | |
6108 | } | |
6109 | ||
6110 | if (ret != 0) { | |
6111 | if (path->slots[0] == 0) | |
6112 | goto not_found; | |
6113 | path->slots[0]--; | |
6114 | } | |
6115 | ||
5f39d397 CM |
6116 | leaf = path->nodes[0]; |
6117 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 6118 | struct btrfs_file_extent_item); |
a52d9a80 | 6119 | /* are we inside the extent that was found? */ |
5f39d397 CM |
6120 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6121 | found_type = btrfs_key_type(&found_key); | |
6122 | if (found_key.objectid != objectid || | |
a52d9a80 | 6123 | found_type != BTRFS_EXTENT_DATA_KEY) { |
25a50341 JB |
6124 | /* |
6125 | * If we backup past the first extent we want to move forward | |
6126 | * and see if there is an extent in front of us, otherwise we'll | |
6127 | * say there is a hole for our whole search range which can | |
6128 | * cause problems. | |
6129 | */ | |
6130 | extent_end = start; | |
6131 | goto next; | |
a52d9a80 CM |
6132 | } |
6133 | ||
5f39d397 CM |
6134 | found_type = btrfs_file_extent_type(leaf, item); |
6135 | extent_start = found_key.offset; | |
261507a0 | 6136 | compress_type = btrfs_file_extent_compression(leaf, item); |
d899e052 YZ |
6137 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6138 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 6139 | extent_end = extent_start + |
db94535d | 6140 | btrfs_file_extent_num_bytes(leaf, item); |
9036c102 YZ |
6141 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
6142 | size_t size; | |
6143 | size = btrfs_file_extent_inline_len(leaf, item); | |
fda2832f | 6144 | extent_end = ALIGN(extent_start + size, root->sectorsize); |
9036c102 | 6145 | } |
25a50341 | 6146 | next: |
9036c102 YZ |
6147 | if (start >= extent_end) { |
6148 | path->slots[0]++; | |
6149 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
6150 | ret = btrfs_next_leaf(root, path); | |
6151 | if (ret < 0) { | |
6152 | err = ret; | |
6153 | goto out; | |
a52d9a80 | 6154 | } |
9036c102 YZ |
6155 | if (ret > 0) |
6156 | goto not_found; | |
6157 | leaf = path->nodes[0]; | |
a52d9a80 | 6158 | } |
9036c102 YZ |
6159 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
6160 | if (found_key.objectid != objectid || | |
6161 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
6162 | goto not_found; | |
6163 | if (start + len <= found_key.offset) | |
6164 | goto not_found; | |
6165 | em->start = start; | |
70c8a91c | 6166 | em->orig_start = start; |
9036c102 YZ |
6167 | em->len = found_key.offset - start; |
6168 | goto not_found_em; | |
6169 | } | |
6170 | ||
cc95bef6 | 6171 | em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item); |
d899e052 YZ |
6172 | if (found_type == BTRFS_FILE_EXTENT_REG || |
6173 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
9036c102 YZ |
6174 | em->start = extent_start; |
6175 | em->len = extent_end - extent_start; | |
ff5b7ee3 YZ |
6176 | em->orig_start = extent_start - |
6177 | btrfs_file_extent_offset(leaf, item); | |
b4939680 JB |
6178 | em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, |
6179 | item); | |
db94535d CM |
6180 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
6181 | if (bytenr == 0) { | |
5f39d397 | 6182 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 CM |
6183 | goto insert; |
6184 | } | |
261507a0 | 6185 | if (compress_type != BTRFS_COMPRESS_NONE) { |
c8b97818 | 6186 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
261507a0 | 6187 | em->compress_type = compress_type; |
c8b97818 | 6188 | em->block_start = bytenr; |
b4939680 | 6189 | em->block_len = em->orig_block_len; |
c8b97818 CM |
6190 | } else { |
6191 | bytenr += btrfs_file_extent_offset(leaf, item); | |
6192 | em->block_start = bytenr; | |
6193 | em->block_len = em->len; | |
d899e052 YZ |
6194 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
6195 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
c8b97818 | 6196 | } |
a52d9a80 CM |
6197 | goto insert; |
6198 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 6199 | unsigned long ptr; |
a52d9a80 | 6200 | char *map; |
3326d1b0 CM |
6201 | size_t size; |
6202 | size_t extent_offset; | |
6203 | size_t copy_size; | |
a52d9a80 | 6204 | |
689f9346 | 6205 | em->block_start = EXTENT_MAP_INLINE; |
c8b97818 | 6206 | if (!page || create) { |
689f9346 | 6207 | em->start = extent_start; |
9036c102 | 6208 | em->len = extent_end - extent_start; |
689f9346 Y |
6209 | goto out; |
6210 | } | |
5f39d397 | 6211 | |
9036c102 YZ |
6212 | size = btrfs_file_extent_inline_len(leaf, item); |
6213 | extent_offset = page_offset(page) + pg_offset - extent_start; | |
70dec807 | 6214 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
3326d1b0 | 6215 | size - extent_offset); |
3326d1b0 | 6216 | em->start = extent_start + extent_offset; |
fda2832f | 6217 | em->len = ALIGN(copy_size, root->sectorsize); |
b4939680 | 6218 | em->orig_block_len = em->len; |
70c8a91c | 6219 | em->orig_start = em->start; |
261507a0 | 6220 | if (compress_type) { |
c8b97818 | 6221 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
261507a0 LZ |
6222 | em->compress_type = compress_type; |
6223 | } | |
689f9346 | 6224 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 6225 | if (create == 0 && !PageUptodate(page)) { |
261507a0 LZ |
6226 | if (btrfs_file_extent_compression(leaf, item) != |
6227 | BTRFS_COMPRESS_NONE) { | |
c8b97818 CM |
6228 | ret = uncompress_inline(path, inode, page, |
6229 | pg_offset, | |
6230 | extent_offset, item); | |
79787eaa | 6231 | BUG_ON(ret); /* -ENOMEM */ |
c8b97818 CM |
6232 | } else { |
6233 | map = kmap(page); | |
6234 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
6235 | copy_size); | |
93c82d57 CM |
6236 | if (pg_offset + copy_size < PAGE_CACHE_SIZE) { |
6237 | memset(map + pg_offset + copy_size, 0, | |
6238 | PAGE_CACHE_SIZE - pg_offset - | |
6239 | copy_size); | |
6240 | } | |
c8b97818 CM |
6241 | kunmap(page); |
6242 | } | |
179e29e4 CM |
6243 | flush_dcache_page(page); |
6244 | } else if (create && PageUptodate(page)) { | |
6bf7e080 | 6245 | BUG(); |
179e29e4 CM |
6246 | if (!trans) { |
6247 | kunmap(page); | |
6248 | free_extent_map(em); | |
6249 | em = NULL; | |
ff5714cc | 6250 | |
b3b4aa74 | 6251 | btrfs_release_path(path); |
7a7eaa40 | 6252 | trans = btrfs_join_transaction(root); |
ff5714cc | 6253 | |
3612b495 TI |
6254 | if (IS_ERR(trans)) |
6255 | return ERR_CAST(trans); | |
179e29e4 CM |
6256 | goto again; |
6257 | } | |
c8b97818 | 6258 | map = kmap(page); |
70dec807 | 6259 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 6260 | copy_size); |
c8b97818 | 6261 | kunmap(page); |
179e29e4 | 6262 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 6263 | } |
d1310b2e | 6264 | set_extent_uptodate(io_tree, em->start, |
507903b8 | 6265 | extent_map_end(em) - 1, NULL, GFP_NOFS); |
a52d9a80 CM |
6266 | goto insert; |
6267 | } else { | |
31b1a2bd | 6268 | WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type); |
a52d9a80 CM |
6269 | } |
6270 | not_found: | |
6271 | em->start = start; | |
70c8a91c | 6272 | em->orig_start = start; |
d1310b2e | 6273 | em->len = len; |
a52d9a80 | 6274 | not_found_em: |
5f39d397 | 6275 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 6276 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 | 6277 | insert: |
b3b4aa74 | 6278 | btrfs_release_path(path); |
d1310b2e | 6279 | if (em->start > start || extent_map_end(em) <= start) { |
c2cf52eb | 6280 | btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]", |
c1c9ff7c | 6281 | em->start, em->len, start, len); |
a52d9a80 CM |
6282 | err = -EIO; |
6283 | goto out; | |
6284 | } | |
d1310b2e CM |
6285 | |
6286 | err = 0; | |
890871be | 6287 | write_lock(&em_tree->lock); |
09a2a8f9 | 6288 | ret = add_extent_mapping(em_tree, em, 0); |
3b951516 CM |
6289 | /* it is possible that someone inserted the extent into the tree |
6290 | * while we had the lock dropped. It is also possible that | |
6291 | * an overlapping map exists in the tree | |
6292 | */ | |
a52d9a80 | 6293 | if (ret == -EEXIST) { |
3b951516 | 6294 | struct extent_map *existing; |
e6dcd2dc CM |
6295 | |
6296 | ret = 0; | |
6297 | ||
3b951516 | 6298 | existing = lookup_extent_mapping(em_tree, start, len); |
e1c4b745 CM |
6299 | if (existing && (existing->start > start || |
6300 | existing->start + existing->len <= start)) { | |
6301 | free_extent_map(existing); | |
6302 | existing = NULL; | |
6303 | } | |
3b951516 CM |
6304 | if (!existing) { |
6305 | existing = lookup_extent_mapping(em_tree, em->start, | |
6306 | em->len); | |
6307 | if (existing) { | |
6308 | err = merge_extent_mapping(em_tree, existing, | |
e6dcd2dc CM |
6309 | em, start, |
6310 | root->sectorsize); | |
3b951516 CM |
6311 | free_extent_map(existing); |
6312 | if (err) { | |
6313 | free_extent_map(em); | |
6314 | em = NULL; | |
6315 | } | |
6316 | } else { | |
6317 | err = -EIO; | |
3b951516 CM |
6318 | free_extent_map(em); |
6319 | em = NULL; | |
6320 | } | |
6321 | } else { | |
6322 | free_extent_map(em); | |
6323 | em = existing; | |
e6dcd2dc | 6324 | err = 0; |
a52d9a80 | 6325 | } |
a52d9a80 | 6326 | } |
890871be | 6327 | write_unlock(&em_tree->lock); |
a52d9a80 | 6328 | out: |
1abe9b8a | 6329 | |
4cd8587c | 6330 | trace_btrfs_get_extent(root, em); |
1abe9b8a | 6331 | |
f421950f CM |
6332 | if (path) |
6333 | btrfs_free_path(path); | |
a52d9a80 CM |
6334 | if (trans) { |
6335 | ret = btrfs_end_transaction(trans, root); | |
d397712b | 6336 | if (!err) |
a52d9a80 CM |
6337 | err = ret; |
6338 | } | |
a52d9a80 CM |
6339 | if (err) { |
6340 | free_extent_map(em); | |
a52d9a80 CM |
6341 | return ERR_PTR(err); |
6342 | } | |
79787eaa | 6343 | BUG_ON(!em); /* Error is always set */ |
a52d9a80 CM |
6344 | return em; |
6345 | } | |
6346 | ||
ec29ed5b CM |
6347 | struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page, |
6348 | size_t pg_offset, u64 start, u64 len, | |
6349 | int create) | |
6350 | { | |
6351 | struct extent_map *em; | |
6352 | struct extent_map *hole_em = NULL; | |
6353 | u64 range_start = start; | |
6354 | u64 end; | |
6355 | u64 found; | |
6356 | u64 found_end; | |
6357 | int err = 0; | |
6358 | ||
6359 | em = btrfs_get_extent(inode, page, pg_offset, start, len, create); | |
6360 | if (IS_ERR(em)) | |
6361 | return em; | |
6362 | if (em) { | |
6363 | /* | |
f9e4fb53 LB |
6364 | * if our em maps to |
6365 | * - a hole or | |
6366 | * - a pre-alloc extent, | |
6367 | * there might actually be delalloc bytes behind it. | |
ec29ed5b | 6368 | */ |
f9e4fb53 LB |
6369 | if (em->block_start != EXTENT_MAP_HOLE && |
6370 | !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
ec29ed5b CM |
6371 | return em; |
6372 | else | |
6373 | hole_em = em; | |
6374 | } | |
6375 | ||
6376 | /* check to see if we've wrapped (len == -1 or similar) */ | |
6377 | end = start + len; | |
6378 | if (end < start) | |
6379 | end = (u64)-1; | |
6380 | else | |
6381 | end -= 1; | |
6382 | ||
6383 | em = NULL; | |
6384 | ||
6385 | /* ok, we didn't find anything, lets look for delalloc */ | |
6386 | found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start, | |
6387 | end, len, EXTENT_DELALLOC, 1); | |
6388 | found_end = range_start + found; | |
6389 | if (found_end < range_start) | |
6390 | found_end = (u64)-1; | |
6391 | ||
6392 | /* | |
6393 | * we didn't find anything useful, return | |
6394 | * the original results from get_extent() | |
6395 | */ | |
6396 | if (range_start > end || found_end <= start) { | |
6397 | em = hole_em; | |
6398 | hole_em = NULL; | |
6399 | goto out; | |
6400 | } | |
6401 | ||
6402 | /* adjust the range_start to make sure it doesn't | |
6403 | * go backwards from the start they passed in | |
6404 | */ | |
67871254 | 6405 | range_start = max(start, range_start); |
ec29ed5b CM |
6406 | found = found_end - range_start; |
6407 | ||
6408 | if (found > 0) { | |
6409 | u64 hole_start = start; | |
6410 | u64 hole_len = len; | |
6411 | ||
172ddd60 | 6412 | em = alloc_extent_map(); |
ec29ed5b CM |
6413 | if (!em) { |
6414 | err = -ENOMEM; | |
6415 | goto out; | |
6416 | } | |
6417 | /* | |
6418 | * when btrfs_get_extent can't find anything it | |
6419 | * returns one huge hole | |
6420 | * | |
6421 | * make sure what it found really fits our range, and | |
6422 | * adjust to make sure it is based on the start from | |
6423 | * the caller | |
6424 | */ | |
6425 | if (hole_em) { | |
6426 | u64 calc_end = extent_map_end(hole_em); | |
6427 | ||
6428 | if (calc_end <= start || (hole_em->start > end)) { | |
6429 | free_extent_map(hole_em); | |
6430 | hole_em = NULL; | |
6431 | } else { | |
6432 | hole_start = max(hole_em->start, start); | |
6433 | hole_len = calc_end - hole_start; | |
6434 | } | |
6435 | } | |
6436 | em->bdev = NULL; | |
6437 | if (hole_em && range_start > hole_start) { | |
6438 | /* our hole starts before our delalloc, so we | |
6439 | * have to return just the parts of the hole | |
6440 | * that go until the delalloc starts | |
6441 | */ | |
6442 | em->len = min(hole_len, | |
6443 | range_start - hole_start); | |
6444 | em->start = hole_start; | |
6445 | em->orig_start = hole_start; | |
6446 | /* | |
6447 | * don't adjust block start at all, | |
6448 | * it is fixed at EXTENT_MAP_HOLE | |
6449 | */ | |
6450 | em->block_start = hole_em->block_start; | |
6451 | em->block_len = hole_len; | |
f9e4fb53 LB |
6452 | if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags)) |
6453 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
ec29ed5b CM |
6454 | } else { |
6455 | em->start = range_start; | |
6456 | em->len = found; | |
6457 | em->orig_start = range_start; | |
6458 | em->block_start = EXTENT_MAP_DELALLOC; | |
6459 | em->block_len = found; | |
6460 | } | |
6461 | } else if (hole_em) { | |
6462 | return hole_em; | |
6463 | } | |
6464 | out: | |
6465 | ||
6466 | free_extent_map(hole_em); | |
6467 | if (err) { | |
6468 | free_extent_map(em); | |
6469 | return ERR_PTR(err); | |
6470 | } | |
6471 | return em; | |
6472 | } | |
6473 | ||
4b46fce2 JB |
6474 | static struct extent_map *btrfs_new_extent_direct(struct inode *inode, |
6475 | u64 start, u64 len) | |
6476 | { | |
6477 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
70c8a91c | 6478 | struct extent_map *em; |
4b46fce2 JB |
6479 | struct btrfs_key ins; |
6480 | u64 alloc_hint; | |
6481 | int ret; | |
4b46fce2 | 6482 | |
4b46fce2 | 6483 | alloc_hint = get_extent_allocation_hint(inode, start, len); |
00361589 | 6484 | ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, |
81c9ad23 | 6485 | alloc_hint, &ins, 1); |
00361589 JB |
6486 | if (ret) |
6487 | return ERR_PTR(ret); | |
4b46fce2 | 6488 | |
70c8a91c | 6489 | em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, |
cc95bef6 | 6490 | ins.offset, ins.offset, ins.offset, 0); |
00361589 JB |
6491 | if (IS_ERR(em)) { |
6492 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); | |
6493 | return em; | |
6494 | } | |
4b46fce2 JB |
6495 | |
6496 | ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, | |
6497 | ins.offset, ins.offset, 0); | |
6498 | if (ret) { | |
6499 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset); | |
00361589 JB |
6500 | free_extent_map(em); |
6501 | return ERR_PTR(ret); | |
4b46fce2 | 6502 | } |
00361589 | 6503 | |
4b46fce2 JB |
6504 | return em; |
6505 | } | |
6506 | ||
46bfbb5c CM |
6507 | /* |
6508 | * returns 1 when the nocow is safe, < 1 on error, 0 if the | |
6509 | * block must be cow'd | |
6510 | */ | |
00361589 | 6511 | noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len, |
7ee9e440 JB |
6512 | u64 *orig_start, u64 *orig_block_len, |
6513 | u64 *ram_bytes) | |
46bfbb5c | 6514 | { |
00361589 | 6515 | struct btrfs_trans_handle *trans; |
46bfbb5c CM |
6516 | struct btrfs_path *path; |
6517 | int ret; | |
6518 | struct extent_buffer *leaf; | |
6519 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6520 | struct btrfs_file_extent_item *fi; | |
6521 | struct btrfs_key key; | |
6522 | u64 disk_bytenr; | |
6523 | u64 backref_offset; | |
6524 | u64 extent_end; | |
6525 | u64 num_bytes; | |
6526 | int slot; | |
6527 | int found_type; | |
7ee9e440 | 6528 | bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); |
e77751aa | 6529 | |
46bfbb5c CM |
6530 | path = btrfs_alloc_path(); |
6531 | if (!path) | |
6532 | return -ENOMEM; | |
6533 | ||
00361589 | 6534 | ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode), |
46bfbb5c CM |
6535 | offset, 0); |
6536 | if (ret < 0) | |
6537 | goto out; | |
6538 | ||
6539 | slot = path->slots[0]; | |
6540 | if (ret == 1) { | |
6541 | if (slot == 0) { | |
6542 | /* can't find the item, must cow */ | |
6543 | ret = 0; | |
6544 | goto out; | |
6545 | } | |
6546 | slot--; | |
6547 | } | |
6548 | ret = 0; | |
6549 | leaf = path->nodes[0]; | |
6550 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
33345d01 | 6551 | if (key.objectid != btrfs_ino(inode) || |
46bfbb5c CM |
6552 | key.type != BTRFS_EXTENT_DATA_KEY) { |
6553 | /* not our file or wrong item type, must cow */ | |
6554 | goto out; | |
6555 | } | |
6556 | ||
6557 | if (key.offset > offset) { | |
6558 | /* Wrong offset, must cow */ | |
6559 | goto out; | |
6560 | } | |
6561 | ||
6562 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
6563 | found_type = btrfs_file_extent_type(leaf, fi); | |
6564 | if (found_type != BTRFS_FILE_EXTENT_REG && | |
6565 | found_type != BTRFS_FILE_EXTENT_PREALLOC) { | |
6566 | /* not a regular extent, must cow */ | |
6567 | goto out; | |
6568 | } | |
7ee9e440 JB |
6569 | |
6570 | if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) | |
6571 | goto out; | |
6572 | ||
e77751aa MX |
6573 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
6574 | if (extent_end <= offset) | |
6575 | goto out; | |
6576 | ||
46bfbb5c | 6577 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
7ee9e440 JB |
6578 | if (disk_bytenr == 0) |
6579 | goto out; | |
6580 | ||
6581 | if (btrfs_file_extent_compression(leaf, fi) || | |
6582 | btrfs_file_extent_encryption(leaf, fi) || | |
6583 | btrfs_file_extent_other_encoding(leaf, fi)) | |
6584 | goto out; | |
6585 | ||
46bfbb5c CM |
6586 | backref_offset = btrfs_file_extent_offset(leaf, fi); |
6587 | ||
7ee9e440 JB |
6588 | if (orig_start) { |
6589 | *orig_start = key.offset - backref_offset; | |
6590 | *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
6591 | *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | |
6592 | } | |
eb384b55 | 6593 | |
46bfbb5c CM |
6594 | if (btrfs_extent_readonly(root, disk_bytenr)) |
6595 | goto out; | |
1bda19eb | 6596 | btrfs_release_path(path); |
46bfbb5c CM |
6597 | |
6598 | /* | |
6599 | * look for other files referencing this extent, if we | |
6600 | * find any we must cow | |
6601 | */ | |
00361589 JB |
6602 | trans = btrfs_join_transaction(root); |
6603 | if (IS_ERR(trans)) { | |
6604 | ret = 0; | |
46bfbb5c | 6605 | goto out; |
00361589 JB |
6606 | } |
6607 | ||
6608 | ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode), | |
6609 | key.offset - backref_offset, disk_bytenr); | |
6610 | btrfs_end_transaction(trans, root); | |
6611 | if (ret) { | |
6612 | ret = 0; | |
6613 | goto out; | |
6614 | } | |
46bfbb5c CM |
6615 | |
6616 | /* | |
6617 | * adjust disk_bytenr and num_bytes to cover just the bytes | |
6618 | * in this extent we are about to write. If there | |
6619 | * are any csums in that range we have to cow in order | |
6620 | * to keep the csums correct | |
6621 | */ | |
6622 | disk_bytenr += backref_offset; | |
6623 | disk_bytenr += offset - key.offset; | |
eb384b55 | 6624 | num_bytes = min(offset + *len, extent_end) - offset; |
46bfbb5c CM |
6625 | if (csum_exist_in_range(root, disk_bytenr, num_bytes)) |
6626 | goto out; | |
6627 | /* | |
6628 | * all of the above have passed, it is safe to overwrite this extent | |
6629 | * without cow | |
6630 | */ | |
eb384b55 | 6631 | *len = num_bytes; |
46bfbb5c CM |
6632 | ret = 1; |
6633 | out: | |
6634 | btrfs_free_path(path); | |
6635 | return ret; | |
6636 | } | |
6637 | ||
eb838e73 JB |
6638 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, |
6639 | struct extent_state **cached_state, int writing) | |
6640 | { | |
6641 | struct btrfs_ordered_extent *ordered; | |
6642 | int ret = 0; | |
6643 | ||
6644 | while (1) { | |
6645 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
6646 | 0, cached_state); | |
6647 | /* | |
6648 | * We're concerned with the entire range that we're going to be | |
6649 | * doing DIO to, so we need to make sure theres no ordered | |
6650 | * extents in this range. | |
6651 | */ | |
6652 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | |
6653 | lockend - lockstart + 1); | |
6654 | ||
6655 | /* | |
6656 | * We need to make sure there are no buffered pages in this | |
6657 | * range either, we could have raced between the invalidate in | |
6658 | * generic_file_direct_write and locking the extent. The | |
6659 | * invalidate needs to happen so that reads after a write do not | |
6660 | * get stale data. | |
6661 | */ | |
6662 | if (!ordered && (!writing || | |
6663 | !test_range_bit(&BTRFS_I(inode)->io_tree, | |
6664 | lockstart, lockend, EXTENT_UPTODATE, 0, | |
6665 | *cached_state))) | |
6666 | break; | |
6667 | ||
6668 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | |
6669 | cached_state, GFP_NOFS); | |
6670 | ||
6671 | if (ordered) { | |
6672 | btrfs_start_ordered_extent(inode, ordered, 1); | |
6673 | btrfs_put_ordered_extent(ordered); | |
6674 | } else { | |
6675 | /* Screw you mmap */ | |
6676 | ret = filemap_write_and_wait_range(inode->i_mapping, | |
6677 | lockstart, | |
6678 | lockend); | |
6679 | if (ret) | |
6680 | break; | |
6681 | ||
6682 | /* | |
6683 | * If we found a page that couldn't be invalidated just | |
6684 | * fall back to buffered. | |
6685 | */ | |
6686 | ret = invalidate_inode_pages2_range(inode->i_mapping, | |
6687 | lockstart >> PAGE_CACHE_SHIFT, | |
6688 | lockend >> PAGE_CACHE_SHIFT); | |
6689 | if (ret) | |
6690 | break; | |
6691 | } | |
6692 | ||
6693 | cond_resched(); | |
6694 | } | |
6695 | ||
6696 | return ret; | |
6697 | } | |
6698 | ||
69ffb543 JB |
6699 | static struct extent_map *create_pinned_em(struct inode *inode, u64 start, |
6700 | u64 len, u64 orig_start, | |
6701 | u64 block_start, u64 block_len, | |
cc95bef6 JB |
6702 | u64 orig_block_len, u64 ram_bytes, |
6703 | int type) | |
69ffb543 JB |
6704 | { |
6705 | struct extent_map_tree *em_tree; | |
6706 | struct extent_map *em; | |
6707 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
6708 | int ret; | |
6709 | ||
6710 | em_tree = &BTRFS_I(inode)->extent_tree; | |
6711 | em = alloc_extent_map(); | |
6712 | if (!em) | |
6713 | return ERR_PTR(-ENOMEM); | |
6714 | ||
6715 | em->start = start; | |
6716 | em->orig_start = orig_start; | |
2ab28f32 JB |
6717 | em->mod_start = start; |
6718 | em->mod_len = len; | |
69ffb543 JB |
6719 | em->len = len; |
6720 | em->block_len = block_len; | |
6721 | em->block_start = block_start; | |
6722 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
b4939680 | 6723 | em->orig_block_len = orig_block_len; |
cc95bef6 | 6724 | em->ram_bytes = ram_bytes; |
70c8a91c | 6725 | em->generation = -1; |
69ffb543 JB |
6726 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
6727 | if (type == BTRFS_ORDERED_PREALLOC) | |
b11e234d | 6728 | set_bit(EXTENT_FLAG_FILLING, &em->flags); |
69ffb543 JB |
6729 | |
6730 | do { | |
6731 | btrfs_drop_extent_cache(inode, em->start, | |
6732 | em->start + em->len - 1, 0); | |
6733 | write_lock(&em_tree->lock); | |
09a2a8f9 | 6734 | ret = add_extent_mapping(em_tree, em, 1); |
69ffb543 JB |
6735 | write_unlock(&em_tree->lock); |
6736 | } while (ret == -EEXIST); | |
6737 | ||
6738 | if (ret) { | |
6739 | free_extent_map(em); | |
6740 | return ERR_PTR(ret); | |
6741 | } | |
6742 | ||
6743 | return em; | |
6744 | } | |
6745 | ||
6746 | ||
4b46fce2 JB |
6747 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
6748 | struct buffer_head *bh_result, int create) | |
6749 | { | |
6750 | struct extent_map *em; | |
6751 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
eb838e73 | 6752 | struct extent_state *cached_state = NULL; |
4b46fce2 | 6753 | u64 start = iblock << inode->i_blkbits; |
eb838e73 | 6754 | u64 lockstart, lockend; |
4b46fce2 | 6755 | u64 len = bh_result->b_size; |
eb838e73 | 6756 | int unlock_bits = EXTENT_LOCKED; |
0934856d | 6757 | int ret = 0; |
eb838e73 | 6758 | |
172a5049 | 6759 | if (create) |
eb838e73 | 6760 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; |
172a5049 | 6761 | else |
c329861d | 6762 | len = min_t(u64, len, root->sectorsize); |
eb838e73 | 6763 | |
c329861d JB |
6764 | lockstart = start; |
6765 | lockend = start + len - 1; | |
6766 | ||
eb838e73 JB |
6767 | /* |
6768 | * If this errors out it's because we couldn't invalidate pagecache for | |
6769 | * this range and we need to fallback to buffered. | |
6770 | */ | |
6771 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) | |
6772 | return -ENOTBLK; | |
6773 | ||
4b46fce2 | 6774 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
eb838e73 JB |
6775 | if (IS_ERR(em)) { |
6776 | ret = PTR_ERR(em); | |
6777 | goto unlock_err; | |
6778 | } | |
4b46fce2 JB |
6779 | |
6780 | /* | |
6781 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | |
6782 | * io. INLINE is special, and we could probably kludge it in here, but | |
6783 | * it's still buffered so for safety lets just fall back to the generic | |
6784 | * buffered path. | |
6785 | * | |
6786 | * For COMPRESSED we _have_ to read the entire extent in so we can | |
6787 | * decompress it, so there will be buffering required no matter what we | |
6788 | * do, so go ahead and fallback to buffered. | |
6789 | * | |
6790 | * We return -ENOTBLK because thats what makes DIO go ahead and go back | |
6791 | * to buffered IO. Don't blame me, this is the price we pay for using | |
6792 | * the generic code. | |
6793 | */ | |
6794 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | |
6795 | em->block_start == EXTENT_MAP_INLINE) { | |
6796 | free_extent_map(em); | |
eb838e73 JB |
6797 | ret = -ENOTBLK; |
6798 | goto unlock_err; | |
4b46fce2 JB |
6799 | } |
6800 | ||
6801 | /* Just a good old fashioned hole, return */ | |
6802 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | |
6803 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | |
6804 | free_extent_map(em); | |
eb838e73 | 6805 | goto unlock_err; |
4b46fce2 JB |
6806 | } |
6807 | ||
6808 | /* | |
6809 | * We don't allocate a new extent in the following cases | |
6810 | * | |
6811 | * 1) The inode is marked as NODATACOW. In this case we'll just use the | |
6812 | * existing extent. | |
6813 | * 2) The extent is marked as PREALLOC. We're good to go here and can | |
6814 | * just use the extent. | |
6815 | * | |
6816 | */ | |
46bfbb5c | 6817 | if (!create) { |
eb838e73 JB |
6818 | len = min(len, em->len - (start - em->start)); |
6819 | lockstart = start + len; | |
6820 | goto unlock; | |
46bfbb5c | 6821 | } |
4b46fce2 JB |
6822 | |
6823 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | |
6824 | ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) && | |
6825 | em->block_start != EXTENT_MAP_HOLE)) { | |
4b46fce2 JB |
6826 | int type; |
6827 | int ret; | |
eb384b55 | 6828 | u64 block_start, orig_start, orig_block_len, ram_bytes; |
4b46fce2 JB |
6829 | |
6830 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
6831 | type = BTRFS_ORDERED_PREALLOC; | |
6832 | else | |
6833 | type = BTRFS_ORDERED_NOCOW; | |
46bfbb5c | 6834 | len = min(len, em->len - (start - em->start)); |
4b46fce2 | 6835 | block_start = em->block_start + (start - em->start); |
46bfbb5c | 6836 | |
00361589 | 6837 | if (can_nocow_extent(inode, start, &len, &orig_start, |
7ee9e440 | 6838 | &orig_block_len, &ram_bytes) == 1) { |
69ffb543 JB |
6839 | if (type == BTRFS_ORDERED_PREALLOC) { |
6840 | free_extent_map(em); | |
6841 | em = create_pinned_em(inode, start, len, | |
6842 | orig_start, | |
b4939680 | 6843 | block_start, len, |
cc95bef6 JB |
6844 | orig_block_len, |
6845 | ram_bytes, type); | |
00361589 | 6846 | if (IS_ERR(em)) |
69ffb543 | 6847 | goto unlock_err; |
69ffb543 JB |
6848 | } |
6849 | ||
46bfbb5c CM |
6850 | ret = btrfs_add_ordered_extent_dio(inode, start, |
6851 | block_start, len, len, type); | |
46bfbb5c CM |
6852 | if (ret) { |
6853 | free_extent_map(em); | |
eb838e73 | 6854 | goto unlock_err; |
46bfbb5c CM |
6855 | } |
6856 | goto unlock; | |
4b46fce2 | 6857 | } |
4b46fce2 | 6858 | } |
00361589 | 6859 | |
46bfbb5c CM |
6860 | /* |
6861 | * this will cow the extent, reset the len in case we changed | |
6862 | * it above | |
6863 | */ | |
6864 | len = bh_result->b_size; | |
70c8a91c JB |
6865 | free_extent_map(em); |
6866 | em = btrfs_new_extent_direct(inode, start, len); | |
eb838e73 JB |
6867 | if (IS_ERR(em)) { |
6868 | ret = PTR_ERR(em); | |
6869 | goto unlock_err; | |
6870 | } | |
46bfbb5c CM |
6871 | len = min(len, em->len - (start - em->start)); |
6872 | unlock: | |
4b46fce2 JB |
6873 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
6874 | inode->i_blkbits; | |
46bfbb5c | 6875 | bh_result->b_size = len; |
4b46fce2 JB |
6876 | bh_result->b_bdev = em->bdev; |
6877 | set_buffer_mapped(bh_result); | |
c3473e83 JB |
6878 | if (create) { |
6879 | if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) | |
6880 | set_buffer_new(bh_result); | |
6881 | ||
6882 | /* | |
6883 | * Need to update the i_size under the extent lock so buffered | |
6884 | * readers will get the updated i_size when we unlock. | |
6885 | */ | |
6886 | if (start + len > i_size_read(inode)) | |
6887 | i_size_write(inode, start + len); | |
0934856d | 6888 | |
172a5049 MX |
6889 | spin_lock(&BTRFS_I(inode)->lock); |
6890 | BTRFS_I(inode)->outstanding_extents++; | |
6891 | spin_unlock(&BTRFS_I(inode)->lock); | |
6892 | ||
0934856d MX |
6893 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
6894 | lockstart + len - 1, EXTENT_DELALLOC, NULL, | |
6895 | &cached_state, GFP_NOFS); | |
6896 | BUG_ON(ret); | |
c3473e83 | 6897 | } |
4b46fce2 | 6898 | |
eb838e73 JB |
6899 | /* |
6900 | * In the case of write we need to clear and unlock the entire range, | |
6901 | * in the case of read we need to unlock only the end area that we | |
6902 | * aren't using if there is any left over space. | |
6903 | */ | |
24c03fa5 | 6904 | if (lockstart < lockend) { |
0934856d MX |
6905 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, |
6906 | lockend, unlock_bits, 1, 0, | |
6907 | &cached_state, GFP_NOFS); | |
24c03fa5 | 6908 | } else { |
eb838e73 | 6909 | free_extent_state(cached_state); |
24c03fa5 | 6910 | } |
eb838e73 | 6911 | |
4b46fce2 JB |
6912 | free_extent_map(em); |
6913 | ||
6914 | return 0; | |
eb838e73 JB |
6915 | |
6916 | unlock_err: | |
eb838e73 JB |
6917 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, |
6918 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | |
6919 | return ret; | |
4b46fce2 JB |
6920 | } |
6921 | ||
4b46fce2 JB |
6922 | static void btrfs_endio_direct_read(struct bio *bio, int err) |
6923 | { | |
e65e1535 | 6924 | struct btrfs_dio_private *dip = bio->bi_private; |
4b46fce2 JB |
6925 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
6926 | struct bio_vec *bvec = bio->bi_io_vec; | |
4b46fce2 JB |
6927 | struct inode *inode = dip->inode; |
6928 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
9be3395b | 6929 | struct bio *dio_bio; |
facc8a22 MX |
6930 | u32 *csums = (u32 *)dip->csum; |
6931 | int index = 0; | |
4b46fce2 | 6932 | u64 start; |
4b46fce2 JB |
6933 | |
6934 | start = dip->logical_offset; | |
6935 | do { | |
6936 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
6937 | struct page *page = bvec->bv_page; | |
6938 | char *kaddr; | |
6939 | u32 csum = ~(u32)0; | |
6940 | unsigned long flags; | |
6941 | ||
6942 | local_irq_save(flags); | |
7ac687d9 | 6943 | kaddr = kmap_atomic(page); |
b0496686 | 6944 | csum = btrfs_csum_data(kaddr + bvec->bv_offset, |
4b46fce2 JB |
6945 | csum, bvec->bv_len); |
6946 | btrfs_csum_final(csum, (char *)&csum); | |
7ac687d9 | 6947 | kunmap_atomic(kaddr); |
4b46fce2 JB |
6948 | local_irq_restore(flags); |
6949 | ||
6950 | flush_dcache_page(bvec->bv_page); | |
facc8a22 MX |
6951 | if (csum != csums[index]) { |
6952 | btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", | |
c1c9ff7c GU |
6953 | btrfs_ino(inode), start, csum, |
6954 | csums[index]); | |
4b46fce2 JB |
6955 | err = -EIO; |
6956 | } | |
6957 | } | |
6958 | ||
6959 | start += bvec->bv_len; | |
4b46fce2 | 6960 | bvec++; |
facc8a22 | 6961 | index++; |
4b46fce2 JB |
6962 | } while (bvec <= bvec_end); |
6963 | ||
6964 | unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, | |
d0082371 | 6965 | dip->logical_offset + dip->bytes - 1); |
9be3395b | 6966 | dio_bio = dip->dio_bio; |
4b46fce2 | 6967 | |
4b46fce2 | 6968 | kfree(dip); |
c0da7aa1 JB |
6969 | |
6970 | /* If we had a csum failure make sure to clear the uptodate flag */ | |
6971 | if (err) | |
9be3395b CM |
6972 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
6973 | dio_end_io(dio_bio, err); | |
6974 | bio_put(bio); | |
4b46fce2 JB |
6975 | } |
6976 | ||
6977 | static void btrfs_endio_direct_write(struct bio *bio, int err) | |
6978 | { | |
6979 | struct btrfs_dio_private *dip = bio->bi_private; | |
6980 | struct inode *inode = dip->inode; | |
6981 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4b46fce2 | 6982 | struct btrfs_ordered_extent *ordered = NULL; |
163cf09c CM |
6983 | u64 ordered_offset = dip->logical_offset; |
6984 | u64 ordered_bytes = dip->bytes; | |
9be3395b | 6985 | struct bio *dio_bio; |
4b46fce2 JB |
6986 | int ret; |
6987 | ||
6988 | if (err) | |
6989 | goto out_done; | |
163cf09c CM |
6990 | again: |
6991 | ret = btrfs_dec_test_first_ordered_pending(inode, &ordered, | |
6992 | &ordered_offset, | |
5fd02043 | 6993 | ordered_bytes, !err); |
4b46fce2 | 6994 | if (!ret) |
163cf09c | 6995 | goto out_test; |
4b46fce2 | 6996 | |
5fd02043 JB |
6997 | ordered->work.func = finish_ordered_fn; |
6998 | ordered->work.flags = 0; | |
6999 | btrfs_queue_worker(&root->fs_info->endio_write_workers, | |
7000 | &ordered->work); | |
163cf09c CM |
7001 | out_test: |
7002 | /* | |
7003 | * our bio might span multiple ordered extents. If we haven't | |
7004 | * completed the accounting for the whole dio, go back and try again | |
7005 | */ | |
7006 | if (ordered_offset < dip->logical_offset + dip->bytes) { | |
7007 | ordered_bytes = dip->logical_offset + dip->bytes - | |
7008 | ordered_offset; | |
5fd02043 | 7009 | ordered = NULL; |
163cf09c CM |
7010 | goto again; |
7011 | } | |
4b46fce2 | 7012 | out_done: |
9be3395b | 7013 | dio_bio = dip->dio_bio; |
4b46fce2 | 7014 | |
4b46fce2 | 7015 | kfree(dip); |
c0da7aa1 JB |
7016 | |
7017 | /* If we had an error make sure to clear the uptodate flag */ | |
7018 | if (err) | |
9be3395b CM |
7019 | clear_bit(BIO_UPTODATE, &dio_bio->bi_flags); |
7020 | dio_end_io(dio_bio, err); | |
7021 | bio_put(bio); | |
4b46fce2 JB |
7022 | } |
7023 | ||
eaf25d93 CM |
7024 | static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw, |
7025 | struct bio *bio, int mirror_num, | |
7026 | unsigned long bio_flags, u64 offset) | |
7027 | { | |
7028 | int ret; | |
7029 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7030 | ret = btrfs_csum_one_bio(root, inode, bio, offset, 1); | |
79787eaa | 7031 | BUG_ON(ret); /* -ENOMEM */ |
eaf25d93 CM |
7032 | return 0; |
7033 | } | |
7034 | ||
e65e1535 MX |
7035 | static void btrfs_end_dio_bio(struct bio *bio, int err) |
7036 | { | |
7037 | struct btrfs_dio_private *dip = bio->bi_private; | |
7038 | ||
7039 | if (err) { | |
efe120a0 FH |
7040 | btrfs_err(BTRFS_I(dip->inode)->root->fs_info, |
7041 | "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d", | |
c1c9ff7c | 7042 | btrfs_ino(dip->inode), bio->bi_rw, |
3dd1462e | 7043 | (unsigned long long)bio->bi_sector, bio->bi_size, err); |
e65e1535 MX |
7044 | dip->errors = 1; |
7045 | ||
7046 | /* | |
7047 | * before atomic variable goto zero, we must make sure | |
7048 | * dip->errors is perceived to be set. | |
7049 | */ | |
7050 | smp_mb__before_atomic_dec(); | |
7051 | } | |
7052 | ||
7053 | /* if there are more bios still pending for this dio, just exit */ | |
7054 | if (!atomic_dec_and_test(&dip->pending_bios)) | |
7055 | goto out; | |
7056 | ||
9be3395b | 7057 | if (dip->errors) { |
e65e1535 | 7058 | bio_io_error(dip->orig_bio); |
9be3395b CM |
7059 | } else { |
7060 | set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags); | |
e65e1535 MX |
7061 | bio_endio(dip->orig_bio, 0); |
7062 | } | |
7063 | out: | |
7064 | bio_put(bio); | |
7065 | } | |
7066 | ||
7067 | static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, | |
7068 | u64 first_sector, gfp_t gfp_flags) | |
7069 | { | |
7070 | int nr_vecs = bio_get_nr_vecs(bdev); | |
7071 | return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags); | |
7072 | } | |
7073 | ||
7074 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, | |
7075 | int rw, u64 file_offset, int skip_sum, | |
c329861d | 7076 | int async_submit) |
e65e1535 | 7077 | { |
facc8a22 | 7078 | struct btrfs_dio_private *dip = bio->bi_private; |
e65e1535 MX |
7079 | int write = rw & REQ_WRITE; |
7080 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7081 | int ret; | |
7082 | ||
b812ce28 JB |
7083 | if (async_submit) |
7084 | async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers); | |
7085 | ||
e65e1535 | 7086 | bio_get(bio); |
5fd02043 JB |
7087 | |
7088 | if (!write) { | |
7089 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); | |
7090 | if (ret) | |
7091 | goto err; | |
7092 | } | |
e65e1535 | 7093 | |
1ae39938 JB |
7094 | if (skip_sum) |
7095 | goto map; | |
7096 | ||
7097 | if (write && async_submit) { | |
e65e1535 MX |
7098 | ret = btrfs_wq_submit_bio(root->fs_info, |
7099 | inode, rw, bio, 0, 0, | |
7100 | file_offset, | |
7101 | __btrfs_submit_bio_start_direct_io, | |
7102 | __btrfs_submit_bio_done); | |
7103 | goto err; | |
1ae39938 JB |
7104 | } else if (write) { |
7105 | /* | |
7106 | * If we aren't doing async submit, calculate the csum of the | |
7107 | * bio now. | |
7108 | */ | |
7109 | ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); | |
7110 | if (ret) | |
7111 | goto err; | |
c2db1073 | 7112 | } else if (!skip_sum) { |
facc8a22 MX |
7113 | ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio, |
7114 | file_offset); | |
c2db1073 TI |
7115 | if (ret) |
7116 | goto err; | |
7117 | } | |
e65e1535 | 7118 | |
1ae39938 JB |
7119 | map: |
7120 | ret = btrfs_map_bio(root, rw, bio, 0, async_submit); | |
e65e1535 MX |
7121 | err: |
7122 | bio_put(bio); | |
7123 | return ret; | |
7124 | } | |
7125 | ||
7126 | static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, | |
7127 | int skip_sum) | |
7128 | { | |
7129 | struct inode *inode = dip->inode; | |
7130 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
e65e1535 MX |
7131 | struct bio *bio; |
7132 | struct bio *orig_bio = dip->orig_bio; | |
7133 | struct bio_vec *bvec = orig_bio->bi_io_vec; | |
7134 | u64 start_sector = orig_bio->bi_sector; | |
7135 | u64 file_offset = dip->logical_offset; | |
7136 | u64 submit_len = 0; | |
7137 | u64 map_length; | |
7138 | int nr_pages = 0; | |
e65e1535 | 7139 | int ret = 0; |
1ae39938 | 7140 | int async_submit = 0; |
e65e1535 | 7141 | |
e65e1535 | 7142 | map_length = orig_bio->bi_size; |
53b381b3 | 7143 | ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, |
e65e1535 MX |
7144 | &map_length, NULL, 0); |
7145 | if (ret) { | |
64728bbb | 7146 | bio_put(orig_bio); |
e65e1535 MX |
7147 | return -EIO; |
7148 | } | |
facc8a22 | 7149 | |
02f57c7a JB |
7150 | if (map_length >= orig_bio->bi_size) { |
7151 | bio = orig_bio; | |
7152 | goto submit; | |
7153 | } | |
7154 | ||
53b381b3 DW |
7155 | /* async crcs make it difficult to collect full stripe writes. */ |
7156 | if (btrfs_get_alloc_profile(root, 1) & | |
7157 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) | |
7158 | async_submit = 0; | |
7159 | else | |
7160 | async_submit = 1; | |
7161 | ||
02f57c7a JB |
7162 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); |
7163 | if (!bio) | |
7164 | return -ENOMEM; | |
7165 | bio->bi_private = dip; | |
7166 | bio->bi_end_io = btrfs_end_dio_bio; | |
7167 | atomic_inc(&dip->pending_bios); | |
7168 | ||
e65e1535 MX |
7169 | while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) { |
7170 | if (unlikely(map_length < submit_len + bvec->bv_len || | |
7171 | bio_add_page(bio, bvec->bv_page, bvec->bv_len, | |
7172 | bvec->bv_offset) < bvec->bv_len)) { | |
7173 | /* | |
7174 | * inc the count before we submit the bio so | |
7175 | * we know the end IO handler won't happen before | |
7176 | * we inc the count. Otherwise, the dip might get freed | |
7177 | * before we're done setting it up | |
7178 | */ | |
7179 | atomic_inc(&dip->pending_bios); | |
7180 | ret = __btrfs_submit_dio_bio(bio, inode, rw, | |
7181 | file_offset, skip_sum, | |
c329861d | 7182 | async_submit); |
e65e1535 MX |
7183 | if (ret) { |
7184 | bio_put(bio); | |
7185 | atomic_dec(&dip->pending_bios); | |
7186 | goto out_err; | |
7187 | } | |
7188 | ||
e65e1535 MX |
7189 | start_sector += submit_len >> 9; |
7190 | file_offset += submit_len; | |
7191 | ||
7192 | submit_len = 0; | |
7193 | nr_pages = 0; | |
7194 | ||
7195 | bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, | |
7196 | start_sector, GFP_NOFS); | |
7197 | if (!bio) | |
7198 | goto out_err; | |
7199 | bio->bi_private = dip; | |
7200 | bio->bi_end_io = btrfs_end_dio_bio; | |
7201 | ||
7202 | map_length = orig_bio->bi_size; | |
53b381b3 | 7203 | ret = btrfs_map_block(root->fs_info, rw, |
3ec706c8 | 7204 | start_sector << 9, |
e65e1535 MX |
7205 | &map_length, NULL, 0); |
7206 | if (ret) { | |
7207 | bio_put(bio); | |
7208 | goto out_err; | |
7209 | } | |
7210 | } else { | |
7211 | submit_len += bvec->bv_len; | |
67871254 | 7212 | nr_pages++; |
e65e1535 MX |
7213 | bvec++; |
7214 | } | |
7215 | } | |
7216 | ||
02f57c7a | 7217 | submit: |
e65e1535 | 7218 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
c329861d | 7219 | async_submit); |
e65e1535 MX |
7220 | if (!ret) |
7221 | return 0; | |
7222 | ||
7223 | bio_put(bio); | |
7224 | out_err: | |
7225 | dip->errors = 1; | |
7226 | /* | |
7227 | * before atomic variable goto zero, we must | |
7228 | * make sure dip->errors is perceived to be set. | |
7229 | */ | |
7230 | smp_mb__before_atomic_dec(); | |
7231 | if (atomic_dec_and_test(&dip->pending_bios)) | |
7232 | bio_io_error(dip->orig_bio); | |
7233 | ||
7234 | /* bio_end_io() will handle error, so we needn't return it */ | |
7235 | return 0; | |
7236 | } | |
7237 | ||
9be3395b CM |
7238 | static void btrfs_submit_direct(int rw, struct bio *dio_bio, |
7239 | struct inode *inode, loff_t file_offset) | |
4b46fce2 JB |
7240 | { |
7241 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
7242 | struct btrfs_dio_private *dip; | |
9be3395b | 7243 | struct bio *io_bio; |
4b46fce2 | 7244 | int skip_sum; |
facc8a22 | 7245 | int sum_len; |
7b6d91da | 7246 | int write = rw & REQ_WRITE; |
4b46fce2 | 7247 | int ret = 0; |
facc8a22 | 7248 | u16 csum_size; |
4b46fce2 JB |
7249 | |
7250 | skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; | |
7251 | ||
9be3395b | 7252 | io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS); |
9be3395b CM |
7253 | if (!io_bio) { |
7254 | ret = -ENOMEM; | |
7255 | goto free_ordered; | |
7256 | } | |
7257 | ||
facc8a22 MX |
7258 | if (!skip_sum && !write) { |
7259 | csum_size = btrfs_super_csum_size(root->fs_info->super_copy); | |
7260 | sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits; | |
7261 | sum_len *= csum_size; | |
7262 | } else { | |
7263 | sum_len = 0; | |
7264 | } | |
7265 | ||
7266 | dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS); | |
4b46fce2 JB |
7267 | if (!dip) { |
7268 | ret = -ENOMEM; | |
9be3395b | 7269 | goto free_io_bio; |
4b46fce2 | 7270 | } |
4b46fce2 | 7271 | |
9be3395b | 7272 | dip->private = dio_bio->bi_private; |
4b46fce2 JB |
7273 | dip->inode = inode; |
7274 | dip->logical_offset = file_offset; | |
e6da5d2e | 7275 | dip->bytes = dio_bio->bi_size; |
9be3395b CM |
7276 | dip->disk_bytenr = (u64)dio_bio->bi_sector << 9; |
7277 | io_bio->bi_private = dip; | |
e65e1535 | 7278 | dip->errors = 0; |
9be3395b CM |
7279 | dip->orig_bio = io_bio; |
7280 | dip->dio_bio = dio_bio; | |
e65e1535 | 7281 | atomic_set(&dip->pending_bios, 0); |
4b46fce2 JB |
7282 | |
7283 | if (write) | |
9be3395b | 7284 | io_bio->bi_end_io = btrfs_endio_direct_write; |
4b46fce2 | 7285 | else |
9be3395b | 7286 | io_bio->bi_end_io = btrfs_endio_direct_read; |
4b46fce2 | 7287 | |
e65e1535 MX |
7288 | ret = btrfs_submit_direct_hook(rw, dip, skip_sum); |
7289 | if (!ret) | |
eaf25d93 | 7290 | return; |
9be3395b CM |
7291 | |
7292 | free_io_bio: | |
7293 | bio_put(io_bio); | |
7294 | ||
4b46fce2 JB |
7295 | free_ordered: |
7296 | /* | |
7297 | * If this is a write, we need to clean up the reserved space and kill | |
7298 | * the ordered extent. | |
7299 | */ | |
7300 | if (write) { | |
7301 | struct btrfs_ordered_extent *ordered; | |
955256f2 | 7302 | ordered = btrfs_lookup_ordered_extent(inode, file_offset); |
4b46fce2 JB |
7303 | if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && |
7304 | !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) | |
7305 | btrfs_free_reserved_extent(root, ordered->start, | |
7306 | ordered->disk_len); | |
7307 | btrfs_put_ordered_extent(ordered); | |
7308 | btrfs_put_ordered_extent(ordered); | |
7309 | } | |
9be3395b | 7310 | bio_endio(dio_bio, ret); |
4b46fce2 JB |
7311 | } |
7312 | ||
5a5f79b5 CM |
7313 | static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb, |
7314 | const struct iovec *iov, loff_t offset, | |
7315 | unsigned long nr_segs) | |
7316 | { | |
7317 | int seg; | |
a1b75f7d | 7318 | int i; |
5a5f79b5 CM |
7319 | size_t size; |
7320 | unsigned long addr; | |
7321 | unsigned blocksize_mask = root->sectorsize - 1; | |
7322 | ssize_t retval = -EINVAL; | |
7323 | loff_t end = offset; | |
7324 | ||
7325 | if (offset & blocksize_mask) | |
7326 | goto out; | |
7327 | ||
7328 | /* Check the memory alignment. Blocks cannot straddle pages */ | |
7329 | for (seg = 0; seg < nr_segs; seg++) { | |
7330 | addr = (unsigned long)iov[seg].iov_base; | |
7331 | size = iov[seg].iov_len; | |
7332 | end += size; | |
a1b75f7d | 7333 | if ((addr & blocksize_mask) || (size & blocksize_mask)) |
5a5f79b5 | 7334 | goto out; |
a1b75f7d JB |
7335 | |
7336 | /* If this is a write we don't need to check anymore */ | |
7337 | if (rw & WRITE) | |
7338 | continue; | |
7339 | ||
7340 | /* | |
7341 | * Check to make sure we don't have duplicate iov_base's in this | |
7342 | * iovec, if so return EINVAL, otherwise we'll get csum errors | |
7343 | * when reading back. | |
7344 | */ | |
7345 | for (i = seg + 1; i < nr_segs; i++) { | |
7346 | if (iov[seg].iov_base == iov[i].iov_base) | |
7347 | goto out; | |
7348 | } | |
5a5f79b5 CM |
7349 | } |
7350 | retval = 0; | |
7351 | out: | |
7352 | return retval; | |
7353 | } | |
eb838e73 | 7354 | |
16432985 CM |
7355 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
7356 | const struct iovec *iov, loff_t offset, | |
7357 | unsigned long nr_segs) | |
7358 | { | |
4b46fce2 JB |
7359 | struct file *file = iocb->ki_filp; |
7360 | struct inode *inode = file->f_mapping->host; | |
0934856d | 7361 | size_t count = 0; |
2e60a51e | 7362 | int flags = 0; |
38851cc1 MX |
7363 | bool wakeup = true; |
7364 | bool relock = false; | |
0934856d | 7365 | ssize_t ret; |
4b46fce2 | 7366 | |
5a5f79b5 | 7367 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
eb838e73 | 7368 | offset, nr_segs)) |
5a5f79b5 | 7369 | return 0; |
3f7c579c | 7370 | |
38851cc1 MX |
7371 | atomic_inc(&inode->i_dio_count); |
7372 | smp_mb__after_atomic_inc(); | |
7373 | ||
0e267c44 JB |
7374 | /* |
7375 | * The generic stuff only does filemap_write_and_wait_range, which isn't | |
7376 | * enough if we've written compressed pages to this area, so we need to | |
7377 | * call btrfs_wait_ordered_range to make absolutely sure that any | |
7378 | * outstanding dirty pages are on disk. | |
7379 | */ | |
7380 | count = iov_length(iov, nr_segs); | |
0ef8b726 JB |
7381 | ret = btrfs_wait_ordered_range(inode, offset, count); |
7382 | if (ret) | |
7383 | return ret; | |
0e267c44 | 7384 | |
0934856d | 7385 | if (rw & WRITE) { |
38851cc1 MX |
7386 | /* |
7387 | * If the write DIO is beyond the EOF, we need update | |
7388 | * the isize, but it is protected by i_mutex. So we can | |
7389 | * not unlock the i_mutex at this case. | |
7390 | */ | |
7391 | if (offset + count <= inode->i_size) { | |
7392 | mutex_unlock(&inode->i_mutex); | |
7393 | relock = true; | |
7394 | } | |
0934856d MX |
7395 | ret = btrfs_delalloc_reserve_space(inode, count); |
7396 | if (ret) | |
38851cc1 MX |
7397 | goto out; |
7398 | } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK, | |
7399 | &BTRFS_I(inode)->runtime_flags))) { | |
7400 | inode_dio_done(inode); | |
7401 | flags = DIO_LOCKING | DIO_SKIP_HOLES; | |
7402 | wakeup = false; | |
0934856d MX |
7403 | } |
7404 | ||
7405 | ret = __blockdev_direct_IO(rw, iocb, inode, | |
7406 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, | |
7407 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, | |
2e60a51e | 7408 | btrfs_submit_direct, flags); |
0934856d MX |
7409 | if (rw & WRITE) { |
7410 | if (ret < 0 && ret != -EIOCBQUEUED) | |
7411 | btrfs_delalloc_release_space(inode, count); | |
172a5049 | 7412 | else if (ret >= 0 && (size_t)ret < count) |
0934856d MX |
7413 | btrfs_delalloc_release_space(inode, |
7414 | count - (size_t)ret); | |
172a5049 MX |
7415 | else |
7416 | btrfs_delalloc_release_metadata(inode, 0); | |
0934856d | 7417 | } |
38851cc1 | 7418 | out: |
2e60a51e MX |
7419 | if (wakeup) |
7420 | inode_dio_done(inode); | |
38851cc1 MX |
7421 | if (relock) |
7422 | mutex_lock(&inode->i_mutex); | |
0934856d MX |
7423 | |
7424 | return ret; | |
16432985 CM |
7425 | } |
7426 | ||
05dadc09 TI |
7427 | #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC) |
7428 | ||
1506fcc8 YS |
7429 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
7430 | __u64 start, __u64 len) | |
7431 | { | |
05dadc09 TI |
7432 | int ret; |
7433 | ||
7434 | ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS); | |
7435 | if (ret) | |
7436 | return ret; | |
7437 | ||
ec29ed5b | 7438 | return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); |
1506fcc8 YS |
7439 | } |
7440 | ||
a52d9a80 | 7441 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 7442 | { |
d1310b2e CM |
7443 | struct extent_io_tree *tree; |
7444 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
8ddc7d9c | 7445 | return extent_read_full_page(tree, page, btrfs_get_extent, 0); |
9ebefb18 | 7446 | } |
1832a6d5 | 7447 | |
a52d9a80 | 7448 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 7449 | { |
d1310b2e | 7450 | struct extent_io_tree *tree; |
b888db2b CM |
7451 | |
7452 | ||
7453 | if (current->flags & PF_MEMALLOC) { | |
7454 | redirty_page_for_writepage(wbc, page); | |
7455 | unlock_page(page); | |
7456 | return 0; | |
7457 | } | |
d1310b2e | 7458 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
a52d9a80 | 7459 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
9ebefb18 CM |
7460 | } |
7461 | ||
48a3b636 ES |
7462 | static int btrfs_writepages(struct address_space *mapping, |
7463 | struct writeback_control *wbc) | |
b293f02e | 7464 | { |
d1310b2e | 7465 | struct extent_io_tree *tree; |
771ed689 | 7466 | |
d1310b2e | 7467 | tree = &BTRFS_I(mapping->host)->io_tree; |
b293f02e CM |
7468 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
7469 | } | |
7470 | ||
3ab2fb5a CM |
7471 | static int |
7472 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
7473 | struct list_head *pages, unsigned nr_pages) | |
7474 | { | |
d1310b2e CM |
7475 | struct extent_io_tree *tree; |
7476 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
7477 | return extent_readpages(tree, mapping, pages, nr_pages, |
7478 | btrfs_get_extent); | |
7479 | } | |
e6dcd2dc | 7480 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 7481 | { |
d1310b2e CM |
7482 | struct extent_io_tree *tree; |
7483 | struct extent_map_tree *map; | |
a52d9a80 | 7484 | int ret; |
8c2383c3 | 7485 | |
d1310b2e CM |
7486 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
7487 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 7488 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
7489 | if (ret == 1) { |
7490 | ClearPagePrivate(page); | |
7491 | set_page_private(page, 0); | |
7492 | page_cache_release(page); | |
39279cc3 | 7493 | } |
a52d9a80 | 7494 | return ret; |
39279cc3 CM |
7495 | } |
7496 | ||
e6dcd2dc CM |
7497 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
7498 | { | |
98509cfc CM |
7499 | if (PageWriteback(page) || PageDirty(page)) |
7500 | return 0; | |
b335b003 | 7501 | return __btrfs_releasepage(page, gfp_flags & GFP_NOFS); |
e6dcd2dc CM |
7502 | } |
7503 | ||
d47992f8 LC |
7504 | static void btrfs_invalidatepage(struct page *page, unsigned int offset, |
7505 | unsigned int length) | |
39279cc3 | 7506 | { |
5fd02043 | 7507 | struct inode *inode = page->mapping->host; |
d1310b2e | 7508 | struct extent_io_tree *tree; |
e6dcd2dc | 7509 | struct btrfs_ordered_extent *ordered; |
2ac55d41 | 7510 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
7511 | u64 page_start = page_offset(page); |
7512 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; | |
131e404a | 7513 | int inode_evicting = inode->i_state & I_FREEING; |
39279cc3 | 7514 | |
8b62b72b CM |
7515 | /* |
7516 | * we have the page locked, so new writeback can't start, | |
7517 | * and the dirty bit won't be cleared while we are here. | |
7518 | * | |
7519 | * Wait for IO on this page so that we can safely clear | |
7520 | * the PagePrivate2 bit and do ordered accounting | |
7521 | */ | |
e6dcd2dc | 7522 | wait_on_page_writeback(page); |
8b62b72b | 7523 | |
5fd02043 | 7524 | tree = &BTRFS_I(inode)->io_tree; |
e6dcd2dc CM |
7525 | if (offset) { |
7526 | btrfs_releasepage(page, GFP_NOFS); | |
7527 | return; | |
7528 | } | |
131e404a FDBM |
7529 | |
7530 | if (!inode_evicting) | |
7531 | lock_extent_bits(tree, page_start, page_end, 0, &cached_state); | |
7532 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
e6dcd2dc | 7533 | if (ordered) { |
eb84ae03 CM |
7534 | /* |
7535 | * IO on this page will never be started, so we need | |
7536 | * to account for any ordered extents now | |
7537 | */ | |
131e404a FDBM |
7538 | if (!inode_evicting) |
7539 | clear_extent_bit(tree, page_start, page_end, | |
7540 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
7541 | EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | | |
7542 | EXTENT_DEFRAG, 1, 0, &cached_state, | |
7543 | GFP_NOFS); | |
8b62b72b CM |
7544 | /* |
7545 | * whoever cleared the private bit is responsible | |
7546 | * for the finish_ordered_io | |
7547 | */ | |
77cef2ec JB |
7548 | if (TestClearPagePrivate2(page)) { |
7549 | struct btrfs_ordered_inode_tree *tree; | |
7550 | u64 new_len; | |
7551 | ||
7552 | tree = &BTRFS_I(inode)->ordered_tree; | |
7553 | ||
7554 | spin_lock_irq(&tree->lock); | |
7555 | set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); | |
7556 | new_len = page_start - ordered->file_offset; | |
7557 | if (new_len < ordered->truncated_len) | |
7558 | ordered->truncated_len = new_len; | |
7559 | spin_unlock_irq(&tree->lock); | |
7560 | ||
7561 | if (btrfs_dec_test_ordered_pending(inode, &ordered, | |
7562 | page_start, | |
7563 | PAGE_CACHE_SIZE, 1)) | |
7564 | btrfs_finish_ordered_io(ordered); | |
8b62b72b | 7565 | } |
e6dcd2dc | 7566 | btrfs_put_ordered_extent(ordered); |
131e404a FDBM |
7567 | if (!inode_evicting) { |
7568 | cached_state = NULL; | |
7569 | lock_extent_bits(tree, page_start, page_end, 0, | |
7570 | &cached_state); | |
7571 | } | |
7572 | } | |
7573 | ||
7574 | if (!inode_evicting) { | |
7575 | clear_extent_bit(tree, page_start, page_end, | |
7576 | EXTENT_LOCKED | EXTENT_DIRTY | | |
7577 | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | | |
7578 | EXTENT_DEFRAG, 1, 1, | |
7579 | &cached_state, GFP_NOFS); | |
7580 | ||
7581 | __btrfs_releasepage(page, GFP_NOFS); | |
e6dcd2dc | 7582 | } |
e6dcd2dc | 7583 | |
4a096752 | 7584 | ClearPageChecked(page); |
9ad6b7bc | 7585 | if (PagePrivate(page)) { |
9ad6b7bc CM |
7586 | ClearPagePrivate(page); |
7587 | set_page_private(page, 0); | |
7588 | page_cache_release(page); | |
7589 | } | |
39279cc3 CM |
7590 | } |
7591 | ||
9ebefb18 CM |
7592 | /* |
7593 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
7594 | * called from a page fault handler when a page is first dirtied. Hence we must | |
7595 | * be careful to check for EOF conditions here. We set the page up correctly | |
7596 | * for a written page which means we get ENOSPC checking when writing into | |
7597 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
7598 | * support these features. | |
7599 | * | |
7600 | * We are not allowed to take the i_mutex here so we have to play games to | |
7601 | * protect against truncate races as the page could now be beyond EOF. Because | |
7602 | * vmtruncate() writes the inode size before removing pages, once we have the | |
7603 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
7604 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
7605 | * unlock the page. | |
7606 | */ | |
c2ec175c | 7607 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
9ebefb18 | 7608 | { |
c2ec175c | 7609 | struct page *page = vmf->page; |
496ad9aa | 7610 | struct inode *inode = file_inode(vma->vm_file); |
1832a6d5 | 7611 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
7612 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
7613 | struct btrfs_ordered_extent *ordered; | |
2ac55d41 | 7614 | struct extent_state *cached_state = NULL; |
e6dcd2dc CM |
7615 | char *kaddr; |
7616 | unsigned long zero_start; | |
9ebefb18 | 7617 | loff_t size; |
1832a6d5 | 7618 | int ret; |
9998eb70 | 7619 | int reserved = 0; |
a52d9a80 | 7620 | u64 page_start; |
e6dcd2dc | 7621 | u64 page_end; |
9ebefb18 | 7622 | |
b2b5ef5c | 7623 | sb_start_pagefault(inode->i_sb); |
0ca1f7ce | 7624 | ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE); |
9998eb70 | 7625 | if (!ret) { |
e41f941a | 7626 | ret = file_update_time(vma->vm_file); |
9998eb70 CM |
7627 | reserved = 1; |
7628 | } | |
56a76f82 NP |
7629 | if (ret) { |
7630 | if (ret == -ENOMEM) | |
7631 | ret = VM_FAULT_OOM; | |
7632 | else /* -ENOSPC, -EIO, etc */ | |
7633 | ret = VM_FAULT_SIGBUS; | |
9998eb70 CM |
7634 | if (reserved) |
7635 | goto out; | |
7636 | goto out_noreserve; | |
56a76f82 | 7637 | } |
1832a6d5 | 7638 | |
56a76f82 | 7639 | ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ |
e6dcd2dc | 7640 | again: |
9ebefb18 | 7641 | lock_page(page); |
9ebefb18 | 7642 | size = i_size_read(inode); |
e6dcd2dc CM |
7643 | page_start = page_offset(page); |
7644 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
a52d9a80 | 7645 | |
9ebefb18 | 7646 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 7647 | (page_start >= size)) { |
9ebefb18 CM |
7648 | /* page got truncated out from underneath us */ |
7649 | goto out_unlock; | |
7650 | } | |
e6dcd2dc CM |
7651 | wait_on_page_writeback(page); |
7652 | ||
d0082371 | 7653 | lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state); |
e6dcd2dc CM |
7654 | set_page_extent_mapped(page); |
7655 | ||
eb84ae03 CM |
7656 | /* |
7657 | * we can't set the delalloc bits if there are pending ordered | |
7658 | * extents. Drop our locks and wait for them to finish | |
7659 | */ | |
e6dcd2dc CM |
7660 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
7661 | if (ordered) { | |
2ac55d41 JB |
7662 | unlock_extent_cached(io_tree, page_start, page_end, |
7663 | &cached_state, GFP_NOFS); | |
e6dcd2dc | 7664 | unlock_page(page); |
eb84ae03 | 7665 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
7666 | btrfs_put_ordered_extent(ordered); |
7667 | goto again; | |
7668 | } | |
7669 | ||
fbf19087 JB |
7670 | /* |
7671 | * XXX - page_mkwrite gets called every time the page is dirtied, even | |
7672 | * if it was already dirty, so for space accounting reasons we need to | |
7673 | * clear any delalloc bits for the range we are fixing to save. There | |
7674 | * is probably a better way to do this, but for now keep consistent with | |
7675 | * prepare_pages in the normal write path. | |
7676 | */ | |
2ac55d41 | 7677 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, |
9e8a4a8b LB |
7678 | EXTENT_DIRTY | EXTENT_DELALLOC | |
7679 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, | |
2ac55d41 | 7680 | 0, 0, &cached_state, GFP_NOFS); |
fbf19087 | 7681 | |
2ac55d41 JB |
7682 | ret = btrfs_set_extent_delalloc(inode, page_start, page_end, |
7683 | &cached_state); | |
9ed74f2d | 7684 | if (ret) { |
2ac55d41 JB |
7685 | unlock_extent_cached(io_tree, page_start, page_end, |
7686 | &cached_state, GFP_NOFS); | |
9ed74f2d JB |
7687 | ret = VM_FAULT_SIGBUS; |
7688 | goto out_unlock; | |
7689 | } | |
e6dcd2dc | 7690 | ret = 0; |
9ebefb18 CM |
7691 | |
7692 | /* page is wholly or partially inside EOF */ | |
a52d9a80 | 7693 | if (page_start + PAGE_CACHE_SIZE > size) |
e6dcd2dc | 7694 | zero_start = size & ~PAGE_CACHE_MASK; |
9ebefb18 | 7695 | else |
e6dcd2dc | 7696 | zero_start = PAGE_CACHE_SIZE; |
9ebefb18 | 7697 | |
e6dcd2dc CM |
7698 | if (zero_start != PAGE_CACHE_SIZE) { |
7699 | kaddr = kmap(page); | |
7700 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); | |
7701 | flush_dcache_page(page); | |
7702 | kunmap(page); | |
7703 | } | |
247e743c | 7704 | ClearPageChecked(page); |
e6dcd2dc | 7705 | set_page_dirty(page); |
50a9b214 | 7706 | SetPageUptodate(page); |
5a3f23d5 | 7707 | |
257c62e1 CM |
7708 | BTRFS_I(inode)->last_trans = root->fs_info->generation; |
7709 | BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; | |
46d8bc34 | 7710 | BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; |
257c62e1 | 7711 | |
2ac55d41 | 7712 | unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); |
9ebefb18 CM |
7713 | |
7714 | out_unlock: | |
b2b5ef5c JK |
7715 | if (!ret) { |
7716 | sb_end_pagefault(inode->i_sb); | |
50a9b214 | 7717 | return VM_FAULT_LOCKED; |
b2b5ef5c | 7718 | } |
9ebefb18 | 7719 | unlock_page(page); |
1832a6d5 | 7720 | out: |
ec39e180 | 7721 | btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); |
9998eb70 | 7722 | out_noreserve: |
b2b5ef5c | 7723 | sb_end_pagefault(inode->i_sb); |
9ebefb18 CM |
7724 | return ret; |
7725 | } | |
7726 | ||
a41ad394 | 7727 | static int btrfs_truncate(struct inode *inode) |
39279cc3 CM |
7728 | { |
7729 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
fcb80c2a | 7730 | struct btrfs_block_rsv *rsv; |
a71754fc | 7731 | int ret = 0; |
3893e33b | 7732 | int err = 0; |
39279cc3 | 7733 | struct btrfs_trans_handle *trans; |
dbe674a9 | 7734 | u64 mask = root->sectorsize - 1; |
07127184 | 7735 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
39279cc3 | 7736 | |
0ef8b726 JB |
7737 | ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), |
7738 | (u64)-1); | |
7739 | if (ret) | |
7740 | return ret; | |
39279cc3 | 7741 | |
fcb80c2a JB |
7742 | /* |
7743 | * Yes ladies and gentelment, this is indeed ugly. The fact is we have | |
7744 | * 3 things going on here | |
7745 | * | |
7746 | * 1) We need to reserve space for our orphan item and the space to | |
7747 | * delete our orphan item. Lord knows we don't want to have a dangling | |
7748 | * orphan item because we didn't reserve space to remove it. | |
7749 | * | |
7750 | * 2) We need to reserve space to update our inode. | |
7751 | * | |
7752 | * 3) We need to have something to cache all the space that is going to | |
7753 | * be free'd up by the truncate operation, but also have some slack | |
7754 | * space reserved in case it uses space during the truncate (thank you | |
7755 | * very much snapshotting). | |
7756 | * | |
7757 | * And we need these to all be seperate. The fact is we can use alot of | |
7758 | * space doing the truncate, and we have no earthly idea how much space | |
7759 | * we will use, so we need the truncate reservation to be seperate so it | |
7760 | * doesn't end up using space reserved for updating the inode or | |
7761 | * removing the orphan item. We also need to be able to stop the | |
7762 | * transaction and start a new one, which means we need to be able to | |
7763 | * update the inode several times, and we have no idea of knowing how | |
7764 | * many times that will be, so we can't just reserve 1 item for the | |
7765 | * entirety of the opration, so that has to be done seperately as well. | |
7766 | * Then there is the orphan item, which does indeed need to be held on | |
7767 | * to for the whole operation, and we need nobody to touch this reserved | |
7768 | * space except the orphan code. | |
7769 | * | |
7770 | * So that leaves us with | |
7771 | * | |
7772 | * 1) root->orphan_block_rsv - for the orphan deletion. | |
7773 | * 2) rsv - for the truncate reservation, which we will steal from the | |
7774 | * transaction reservation. | |
7775 | * 3) fs_info->trans_block_rsv - this will have 1 items worth left for | |
7776 | * updating the inode. | |
7777 | */ | |
66d8f3dd | 7778 | rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); |
fcb80c2a JB |
7779 | if (!rsv) |
7780 | return -ENOMEM; | |
4a338542 | 7781 | rsv->size = min_size; |
ca7e70f5 | 7782 | rsv->failfast = 1; |
f0cd846e | 7783 | |
907cbceb | 7784 | /* |
07127184 | 7785 | * 1 for the truncate slack space |
907cbceb JB |
7786 | * 1 for updating the inode. |
7787 | */ | |
f3fe820c | 7788 | trans = btrfs_start_transaction(root, 2); |
fcb80c2a JB |
7789 | if (IS_ERR(trans)) { |
7790 | err = PTR_ERR(trans); | |
7791 | goto out; | |
7792 | } | |
f0cd846e | 7793 | |
907cbceb JB |
7794 | /* Migrate the slack space for the truncate to our reserve */ |
7795 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, | |
7796 | min_size); | |
fcb80c2a | 7797 | BUG_ON(ret); |
f0cd846e | 7798 | |
5a3f23d5 CM |
7799 | /* |
7800 | * setattr is responsible for setting the ordered_data_close flag, | |
7801 | * but that is only tested during the last file release. That | |
7802 | * could happen well after the next commit, leaving a great big | |
7803 | * window where new writes may get lost if someone chooses to write | |
7804 | * to this file after truncating to zero | |
7805 | * | |
7806 | * The inode doesn't have any dirty data here, and so if we commit | |
7807 | * this is a noop. If someone immediately starts writing to the inode | |
7808 | * it is very likely we'll catch some of their writes in this | |
7809 | * transaction, and the commit will find this file on the ordered | |
7810 | * data list with good things to send down. | |
7811 | * | |
7812 | * This is a best effort solution, there is still a window where | |
7813 | * using truncate to replace the contents of the file will | |
7814 | * end up with a zero length file after a crash. | |
7815 | */ | |
72ac3c0d JB |
7816 | if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, |
7817 | &BTRFS_I(inode)->runtime_flags)) | |
5a3f23d5 CM |
7818 | btrfs_add_ordered_operation(trans, root, inode); |
7819 | ||
5dc562c5 JB |
7820 | /* |
7821 | * So if we truncate and then write and fsync we normally would just | |
7822 | * write the extents that changed, which is a problem if we need to | |
7823 | * first truncate that entire inode. So set this flag so we write out | |
7824 | * all of the extents in the inode to the sync log so we're completely | |
7825 | * safe. | |
7826 | */ | |
7827 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); | |
ca7e70f5 | 7828 | trans->block_rsv = rsv; |
907cbceb | 7829 | |
8082510e YZ |
7830 | while (1) { |
7831 | ret = btrfs_truncate_inode_items(trans, root, inode, | |
7832 | inode->i_size, | |
7833 | BTRFS_EXTENT_DATA_KEY); | |
ca7e70f5 | 7834 | if (ret != -ENOSPC) { |
3893e33b | 7835 | err = ret; |
8082510e | 7836 | break; |
3893e33b | 7837 | } |
39279cc3 | 7838 | |
fcb80c2a | 7839 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
8082510e | 7840 | ret = btrfs_update_inode(trans, root, inode); |
3893e33b JB |
7841 | if (ret) { |
7842 | err = ret; | |
7843 | break; | |
7844 | } | |
ca7e70f5 | 7845 | |
8082510e | 7846 | btrfs_end_transaction(trans, root); |
b53d3f5d | 7847 | btrfs_btree_balance_dirty(root); |
ca7e70f5 JB |
7848 | |
7849 | trans = btrfs_start_transaction(root, 2); | |
7850 | if (IS_ERR(trans)) { | |
7851 | ret = err = PTR_ERR(trans); | |
7852 | trans = NULL; | |
7853 | break; | |
7854 | } | |
7855 | ||
7856 | ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, | |
7857 | rsv, min_size); | |
7858 | BUG_ON(ret); /* shouldn't happen */ | |
7859 | trans->block_rsv = rsv; | |
8082510e YZ |
7860 | } |
7861 | ||
7862 | if (ret == 0 && inode->i_nlink > 0) { | |
fcb80c2a | 7863 | trans->block_rsv = root->orphan_block_rsv; |
8082510e | 7864 | ret = btrfs_orphan_del(trans, inode); |
3893e33b JB |
7865 | if (ret) |
7866 | err = ret; | |
8082510e YZ |
7867 | } |
7868 | ||
917c16b2 CM |
7869 | if (trans) { |
7870 | trans->block_rsv = &root->fs_info->trans_block_rsv; | |
7871 | ret = btrfs_update_inode(trans, root, inode); | |
7872 | if (ret && !err) | |
7873 | err = ret; | |
7b128766 | 7874 | |
7ad85bb7 | 7875 | ret = btrfs_end_transaction(trans, root); |
b53d3f5d | 7876 | btrfs_btree_balance_dirty(root); |
917c16b2 | 7877 | } |
fcb80c2a JB |
7878 | |
7879 | out: | |
7880 | btrfs_free_block_rsv(root, rsv); | |
7881 | ||
3893e33b JB |
7882 | if (ret && !err) |
7883 | err = ret; | |
a41ad394 | 7884 | |
3893e33b | 7885 | return err; |
39279cc3 CM |
7886 | } |
7887 | ||
d352ac68 CM |
7888 | /* |
7889 | * create a new subvolume directory/inode (helper for the ioctl). | |
7890 | */ | |
d2fb3437 | 7891 | int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, |
d82a6f1d | 7892 | struct btrfs_root *new_root, u64 new_dirid) |
39279cc3 | 7893 | { |
39279cc3 | 7894 | struct inode *inode; |
76dda93c | 7895 | int err; |
00e4e6b3 | 7896 | u64 index = 0; |
39279cc3 | 7897 | |
12fc9d09 FA |
7898 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, |
7899 | new_dirid, new_dirid, | |
7900 | S_IFDIR | (~current_umask() & S_IRWXUGO), | |
7901 | &index); | |
54aa1f4d | 7902 | if (IS_ERR(inode)) |
f46b5a66 | 7903 | return PTR_ERR(inode); |
39279cc3 CM |
7904 | inode->i_op = &btrfs_dir_inode_operations; |
7905 | inode->i_fop = &btrfs_dir_file_operations; | |
7906 | ||
bfe86848 | 7907 | set_nlink(inode, 1); |
dbe674a9 | 7908 | btrfs_i_size_write(inode, 0); |
3b96362c | 7909 | |
76dda93c | 7910 | err = btrfs_update_inode(trans, new_root, inode); |
cb8e7090 | 7911 | |
76dda93c | 7912 | iput(inode); |
ce598979 | 7913 | return err; |
39279cc3 CM |
7914 | } |
7915 | ||
39279cc3 CM |
7916 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
7917 | { | |
7918 | struct btrfs_inode *ei; | |
2ead6ae7 | 7919 | struct inode *inode; |
39279cc3 CM |
7920 | |
7921 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
7922 | if (!ei) | |
7923 | return NULL; | |
2ead6ae7 YZ |
7924 | |
7925 | ei->root = NULL; | |
2ead6ae7 | 7926 | ei->generation = 0; |
15ee9bc7 | 7927 | ei->last_trans = 0; |
257c62e1 | 7928 | ei->last_sub_trans = 0; |
e02119d5 | 7929 | ei->logged_trans = 0; |
2ead6ae7 | 7930 | ei->delalloc_bytes = 0; |
2ead6ae7 YZ |
7931 | ei->disk_i_size = 0; |
7932 | ei->flags = 0; | |
7709cde3 | 7933 | ei->csum_bytes = 0; |
2ead6ae7 | 7934 | ei->index_cnt = (u64)-1; |
67de1176 | 7935 | ei->dir_index = 0; |
2ead6ae7 | 7936 | ei->last_unlink_trans = 0; |
46d8bc34 | 7937 | ei->last_log_commit = 0; |
2ead6ae7 | 7938 | |
9e0baf60 JB |
7939 | spin_lock_init(&ei->lock); |
7940 | ei->outstanding_extents = 0; | |
7941 | ei->reserved_extents = 0; | |
2ead6ae7 | 7942 | |
72ac3c0d | 7943 | ei->runtime_flags = 0; |
261507a0 | 7944 | ei->force_compress = BTRFS_COMPRESS_NONE; |
2ead6ae7 | 7945 | |
16cdcec7 MX |
7946 | ei->delayed_node = NULL; |
7947 | ||
2ead6ae7 | 7948 | inode = &ei->vfs_inode; |
a8067e02 | 7949 | extent_map_tree_init(&ei->extent_tree); |
f993c883 DS |
7950 | extent_io_tree_init(&ei->io_tree, &inode->i_data); |
7951 | extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); | |
0b32f4bb JB |
7952 | ei->io_tree.track_uptodate = 1; |
7953 | ei->io_failure_tree.track_uptodate = 1; | |
b812ce28 | 7954 | atomic_set(&ei->sync_writers, 0); |
2ead6ae7 | 7955 | mutex_init(&ei->log_mutex); |
f248679e | 7956 | mutex_init(&ei->delalloc_mutex); |
e6dcd2dc | 7957 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
2ead6ae7 | 7958 | INIT_LIST_HEAD(&ei->delalloc_inodes); |
5a3f23d5 | 7959 | INIT_LIST_HEAD(&ei->ordered_operations); |
2ead6ae7 YZ |
7960 | RB_CLEAR_NODE(&ei->rb_node); |
7961 | ||
7962 | return inode; | |
39279cc3 CM |
7963 | } |
7964 | ||
aaedb55b JB |
7965 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
7966 | void btrfs_test_destroy_inode(struct inode *inode) | |
7967 | { | |
7968 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); | |
7969 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); | |
7970 | } | |
7971 | #endif | |
7972 | ||
fa0d7e3d NP |
7973 | static void btrfs_i_callback(struct rcu_head *head) |
7974 | { | |
7975 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
7976 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
7977 | } | |
7978 | ||
39279cc3 CM |
7979 | void btrfs_destroy_inode(struct inode *inode) |
7980 | { | |
e6dcd2dc | 7981 | struct btrfs_ordered_extent *ordered; |
5a3f23d5 CM |
7982 | struct btrfs_root *root = BTRFS_I(inode)->root; |
7983 | ||
b3d9b7a3 | 7984 | WARN_ON(!hlist_empty(&inode->i_dentry)); |
39279cc3 | 7985 | WARN_ON(inode->i_data.nrpages); |
9e0baf60 JB |
7986 | WARN_ON(BTRFS_I(inode)->outstanding_extents); |
7987 | WARN_ON(BTRFS_I(inode)->reserved_extents); | |
7709cde3 JB |
7988 | WARN_ON(BTRFS_I(inode)->delalloc_bytes); |
7989 | WARN_ON(BTRFS_I(inode)->csum_bytes); | |
39279cc3 | 7990 | |
a6dbd429 JB |
7991 | /* |
7992 | * This can happen where we create an inode, but somebody else also | |
7993 | * created the same inode and we need to destroy the one we already | |
7994 | * created. | |
7995 | */ | |
7996 | if (!root) | |
7997 | goto free; | |
7998 | ||
5a3f23d5 CM |
7999 | /* |
8000 | * Make sure we're properly removed from the ordered operation | |
8001 | * lists. | |
8002 | */ | |
8003 | smp_mb(); | |
8004 | if (!list_empty(&BTRFS_I(inode)->ordered_operations)) { | |
199c2a9c | 8005 | spin_lock(&root->fs_info->ordered_root_lock); |
5a3f23d5 | 8006 | list_del_init(&BTRFS_I(inode)->ordered_operations); |
199c2a9c | 8007 | spin_unlock(&root->fs_info->ordered_root_lock); |
5a3f23d5 CM |
8008 | } |
8009 | ||
8a35d95f JB |
8010 | if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, |
8011 | &BTRFS_I(inode)->runtime_flags)) { | |
c2cf52eb | 8012 | btrfs_info(root->fs_info, "inode %llu still on the orphan list", |
c1c9ff7c | 8013 | btrfs_ino(inode)); |
8a35d95f | 8014 | atomic_dec(&root->orphan_inodes); |
7b128766 | 8015 | } |
7b128766 | 8016 | |
d397712b | 8017 | while (1) { |
e6dcd2dc CM |
8018 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); |
8019 | if (!ordered) | |
8020 | break; | |
8021 | else { | |
c2cf52eb | 8022 | btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup", |
c1c9ff7c | 8023 | ordered->file_offset, ordered->len); |
e6dcd2dc CM |
8024 | btrfs_remove_ordered_extent(inode, ordered); |
8025 | btrfs_put_ordered_extent(ordered); | |
8026 | btrfs_put_ordered_extent(ordered); | |
8027 | } | |
8028 | } | |
5d4f98a2 | 8029 | inode_tree_del(inode); |
5b21f2ed | 8030 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
a6dbd429 | 8031 | free: |
fa0d7e3d | 8032 | call_rcu(&inode->i_rcu, btrfs_i_callback); |
39279cc3 CM |
8033 | } |
8034 | ||
45321ac5 | 8035 | int btrfs_drop_inode(struct inode *inode) |
76dda93c YZ |
8036 | { |
8037 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
45321ac5 | 8038 | |
6379ef9f NA |
8039 | if (root == NULL) |
8040 | return 1; | |
8041 | ||
fa6ac876 | 8042 | /* the snap/subvol tree is on deleting */ |
69e9c6c6 | 8043 | if (btrfs_root_refs(&root->root_item) == 0) |
45321ac5 | 8044 | return 1; |
76dda93c | 8045 | else |
45321ac5 | 8046 | return generic_drop_inode(inode); |
76dda93c YZ |
8047 | } |
8048 | ||
0ee0fda0 | 8049 | static void init_once(void *foo) |
39279cc3 CM |
8050 | { |
8051 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
8052 | ||
8053 | inode_init_once(&ei->vfs_inode); | |
8054 | } | |
8055 | ||
8056 | void btrfs_destroy_cachep(void) | |
8057 | { | |
8c0a8537 KS |
8058 | /* |
8059 | * Make sure all delayed rcu free inodes are flushed before we | |
8060 | * destroy cache. | |
8061 | */ | |
8062 | rcu_barrier(); | |
39279cc3 CM |
8063 | if (btrfs_inode_cachep) |
8064 | kmem_cache_destroy(btrfs_inode_cachep); | |
8065 | if (btrfs_trans_handle_cachep) | |
8066 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
8067 | if (btrfs_transaction_cachep) | |
8068 | kmem_cache_destroy(btrfs_transaction_cachep); | |
39279cc3 CM |
8069 | if (btrfs_path_cachep) |
8070 | kmem_cache_destroy(btrfs_path_cachep); | |
dc89e982 JB |
8071 | if (btrfs_free_space_cachep) |
8072 | kmem_cache_destroy(btrfs_free_space_cachep); | |
8ccf6f19 MX |
8073 | if (btrfs_delalloc_work_cachep) |
8074 | kmem_cache_destroy(btrfs_delalloc_work_cachep); | |
39279cc3 CM |
8075 | } |
8076 | ||
8077 | int btrfs_init_cachep(void) | |
8078 | { | |
837e1972 | 8079 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode", |
9601e3f6 CH |
8080 | sizeof(struct btrfs_inode), 0, |
8081 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once); | |
39279cc3 CM |
8082 | if (!btrfs_inode_cachep) |
8083 | goto fail; | |
9601e3f6 | 8084 | |
837e1972 | 8085 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle", |
9601e3f6 CH |
8086 | sizeof(struct btrfs_trans_handle), 0, |
8087 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
8088 | if (!btrfs_trans_handle_cachep) |
8089 | goto fail; | |
9601e3f6 | 8090 | |
837e1972 | 8091 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction", |
9601e3f6 CH |
8092 | sizeof(struct btrfs_transaction), 0, |
8093 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
8094 | if (!btrfs_transaction_cachep) |
8095 | goto fail; | |
9601e3f6 | 8096 | |
837e1972 | 8097 | btrfs_path_cachep = kmem_cache_create("btrfs_path", |
9601e3f6 CH |
8098 | sizeof(struct btrfs_path), 0, |
8099 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
39279cc3 CM |
8100 | if (!btrfs_path_cachep) |
8101 | goto fail; | |
9601e3f6 | 8102 | |
837e1972 | 8103 | btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space", |
dc89e982 JB |
8104 | sizeof(struct btrfs_free_space), 0, |
8105 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
8106 | if (!btrfs_free_space_cachep) | |
8107 | goto fail; | |
8108 | ||
8ccf6f19 MX |
8109 | btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work", |
8110 | sizeof(struct btrfs_delalloc_work), 0, | |
8111 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, | |
8112 | NULL); | |
8113 | if (!btrfs_delalloc_work_cachep) | |
8114 | goto fail; | |
8115 | ||
39279cc3 CM |
8116 | return 0; |
8117 | fail: | |
8118 | btrfs_destroy_cachep(); | |
8119 | return -ENOMEM; | |
8120 | } | |
8121 | ||
8122 | static int btrfs_getattr(struct vfsmount *mnt, | |
8123 | struct dentry *dentry, struct kstat *stat) | |
8124 | { | |
df0af1a5 | 8125 | u64 delalloc_bytes; |
39279cc3 | 8126 | struct inode *inode = dentry->d_inode; |
fadc0d8b DS |
8127 | u32 blocksize = inode->i_sb->s_blocksize; |
8128 | ||
39279cc3 | 8129 | generic_fillattr(inode, stat); |
0ee5dc67 | 8130 | stat->dev = BTRFS_I(inode)->root->anon_dev; |
d6667462 | 8131 | stat->blksize = PAGE_CACHE_SIZE; |
df0af1a5 MX |
8132 | |
8133 | spin_lock(&BTRFS_I(inode)->lock); | |
8134 | delalloc_bytes = BTRFS_I(inode)->delalloc_bytes; | |
8135 | spin_unlock(&BTRFS_I(inode)->lock); | |
fadc0d8b | 8136 | stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) + |
df0af1a5 | 8137 | ALIGN(delalloc_bytes, blocksize)) >> 9; |
39279cc3 CM |
8138 | return 0; |
8139 | } | |
8140 | ||
d397712b CM |
8141 | static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
8142 | struct inode *new_dir, struct dentry *new_dentry) | |
39279cc3 CM |
8143 | { |
8144 | struct btrfs_trans_handle *trans; | |
8145 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
4df27c4d | 8146 | struct btrfs_root *dest = BTRFS_I(new_dir)->root; |
39279cc3 CM |
8147 | struct inode *new_inode = new_dentry->d_inode; |
8148 | struct inode *old_inode = old_dentry->d_inode; | |
8149 | struct timespec ctime = CURRENT_TIME; | |
00e4e6b3 | 8150 | u64 index = 0; |
4df27c4d | 8151 | u64 root_objectid; |
39279cc3 | 8152 | int ret; |
33345d01 | 8153 | u64 old_ino = btrfs_ino(old_inode); |
39279cc3 | 8154 | |
33345d01 | 8155 | if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) |
f679a840 YZ |
8156 | return -EPERM; |
8157 | ||
4df27c4d | 8158 | /* we only allow rename subvolume link between subvolumes */ |
33345d01 | 8159 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) |
3394e160 CM |
8160 | return -EXDEV; |
8161 | ||
33345d01 LZ |
8162 | if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || |
8163 | (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID)) | |
39279cc3 | 8164 | return -ENOTEMPTY; |
5f39d397 | 8165 | |
4df27c4d YZ |
8166 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
8167 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) | |
8168 | return -ENOTEMPTY; | |
9c52057c CM |
8169 | |
8170 | ||
8171 | /* check for collisions, even if the name isn't there */ | |
4871c158 | 8172 | ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino, |
9c52057c CM |
8173 | new_dentry->d_name.name, |
8174 | new_dentry->d_name.len); | |
8175 | ||
8176 | if (ret) { | |
8177 | if (ret == -EEXIST) { | |
8178 | /* we shouldn't get | |
8179 | * eexist without a new_inode */ | |
fae7f21c | 8180 | if (WARN_ON(!new_inode)) { |
9c52057c CM |
8181 | return ret; |
8182 | } | |
8183 | } else { | |
8184 | /* maybe -EOVERFLOW */ | |
8185 | return ret; | |
8186 | } | |
8187 | } | |
8188 | ret = 0; | |
8189 | ||
5a3f23d5 CM |
8190 | /* |
8191 | * we're using rename to replace one file with another. | |
8192 | * and the replacement file is large. Start IO on it now so | |
8193 | * we don't add too much work to the end of the transaction | |
8194 | */ | |
4baf8c92 | 8195 | if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size && |
5a3f23d5 CM |
8196 | old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
8197 | filemap_flush(old_inode->i_mapping); | |
8198 | ||
76dda93c | 8199 | /* close the racy window with snapshot create/destroy ioctl */ |
33345d01 | 8200 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 8201 | down_read(&root->fs_info->subvol_sem); |
a22285a6 YZ |
8202 | /* |
8203 | * We want to reserve the absolute worst case amount of items. So if | |
8204 | * both inodes are subvols and we need to unlink them then that would | |
8205 | * require 4 item modifications, but if they are both normal inodes it | |
8206 | * would require 5 item modifications, so we'll assume their normal | |
8207 | * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items | |
8208 | * should cover the worst case number of items we'll modify. | |
8209 | */ | |
6e137ed3 | 8210 | trans = btrfs_start_transaction(root, 11); |
b44c59a8 JL |
8211 | if (IS_ERR(trans)) { |
8212 | ret = PTR_ERR(trans); | |
8213 | goto out_notrans; | |
8214 | } | |
76dda93c | 8215 | |
4df27c4d YZ |
8216 | if (dest != root) |
8217 | btrfs_record_root_in_trans(trans, dest); | |
5f39d397 | 8218 | |
a5719521 YZ |
8219 | ret = btrfs_set_inode_index(new_dir, &index); |
8220 | if (ret) | |
8221 | goto out_fail; | |
5a3f23d5 | 8222 | |
67de1176 | 8223 | BTRFS_I(old_inode)->dir_index = 0ULL; |
33345d01 | 8224 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
8225 | /* force full log commit if subvolume involved. */ |
8226 | root->fs_info->last_trans_log_full_commit = trans->transid; | |
8227 | } else { | |
a5719521 YZ |
8228 | ret = btrfs_insert_inode_ref(trans, dest, |
8229 | new_dentry->d_name.name, | |
8230 | new_dentry->d_name.len, | |
33345d01 LZ |
8231 | old_ino, |
8232 | btrfs_ino(new_dir), index); | |
a5719521 YZ |
8233 | if (ret) |
8234 | goto out_fail; | |
4df27c4d YZ |
8235 | /* |
8236 | * this is an ugly little race, but the rename is required | |
8237 | * to make sure that if we crash, the inode is either at the | |
8238 | * old name or the new one. pinning the log transaction lets | |
8239 | * us make sure we don't allow a log commit to come in after | |
8240 | * we unlink the name but before we add the new name back in. | |
8241 | */ | |
8242 | btrfs_pin_log_trans(root); | |
8243 | } | |
5a3f23d5 CM |
8244 | /* |
8245 | * make sure the inode gets flushed if it is replacing | |
8246 | * something. | |
8247 | */ | |
33345d01 | 8248 | if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode)) |
5a3f23d5 | 8249 | btrfs_add_ordered_operation(trans, root, old_inode); |
5a3f23d5 | 8250 | |
0c4d2d95 JB |
8251 | inode_inc_iversion(old_dir); |
8252 | inode_inc_iversion(new_dir); | |
8253 | inode_inc_iversion(old_inode); | |
39279cc3 CM |
8254 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
8255 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
8256 | old_inode->i_ctime = ctime; | |
5f39d397 | 8257 | |
12fcfd22 CM |
8258 | if (old_dentry->d_parent != new_dentry->d_parent) |
8259 | btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); | |
8260 | ||
33345d01 | 8261 | if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { |
4df27c4d YZ |
8262 | root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; |
8263 | ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, | |
8264 | old_dentry->d_name.name, | |
8265 | old_dentry->d_name.len); | |
8266 | } else { | |
92986796 AV |
8267 | ret = __btrfs_unlink_inode(trans, root, old_dir, |
8268 | old_dentry->d_inode, | |
8269 | old_dentry->d_name.name, | |
8270 | old_dentry->d_name.len); | |
8271 | if (!ret) | |
8272 | ret = btrfs_update_inode(trans, root, old_inode); | |
4df27c4d | 8273 | } |
79787eaa JM |
8274 | if (ret) { |
8275 | btrfs_abort_transaction(trans, root, ret); | |
8276 | goto out_fail; | |
8277 | } | |
39279cc3 CM |
8278 | |
8279 | if (new_inode) { | |
0c4d2d95 | 8280 | inode_inc_iversion(new_inode); |
39279cc3 | 8281 | new_inode->i_ctime = CURRENT_TIME; |
33345d01 | 8282 | if (unlikely(btrfs_ino(new_inode) == |
4df27c4d YZ |
8283 | BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { |
8284 | root_objectid = BTRFS_I(new_inode)->location.objectid; | |
8285 | ret = btrfs_unlink_subvol(trans, dest, new_dir, | |
8286 | root_objectid, | |
8287 | new_dentry->d_name.name, | |
8288 | new_dentry->d_name.len); | |
8289 | BUG_ON(new_inode->i_nlink == 0); | |
8290 | } else { | |
8291 | ret = btrfs_unlink_inode(trans, dest, new_dir, | |
8292 | new_dentry->d_inode, | |
8293 | new_dentry->d_name.name, | |
8294 | new_dentry->d_name.len); | |
8295 | } | |
4ef31a45 | 8296 | if (!ret && new_inode->i_nlink == 0) |
e02119d5 | 8297 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
79787eaa JM |
8298 | if (ret) { |
8299 | btrfs_abort_transaction(trans, root, ret); | |
8300 | goto out_fail; | |
8301 | } | |
39279cc3 | 8302 | } |
aec7477b | 8303 | |
4df27c4d YZ |
8304 | ret = btrfs_add_link(trans, new_dir, old_inode, |
8305 | new_dentry->d_name.name, | |
a5719521 | 8306 | new_dentry->d_name.len, 0, index); |
79787eaa JM |
8307 | if (ret) { |
8308 | btrfs_abort_transaction(trans, root, ret); | |
8309 | goto out_fail; | |
8310 | } | |
39279cc3 | 8311 | |
67de1176 MX |
8312 | if (old_inode->i_nlink == 1) |
8313 | BTRFS_I(old_inode)->dir_index = index; | |
8314 | ||
33345d01 | 8315 | if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { |
10d9f309 | 8316 | struct dentry *parent = new_dentry->d_parent; |
6a912213 | 8317 | btrfs_log_new_name(trans, old_inode, old_dir, parent); |
4df27c4d YZ |
8318 | btrfs_end_log_trans(root); |
8319 | } | |
39279cc3 | 8320 | out_fail: |
7ad85bb7 | 8321 | btrfs_end_transaction(trans, root); |
b44c59a8 | 8322 | out_notrans: |
33345d01 | 8323 | if (old_ino == BTRFS_FIRST_FREE_OBJECTID) |
76dda93c | 8324 | up_read(&root->fs_info->subvol_sem); |
9ed74f2d | 8325 | |
39279cc3 CM |
8326 | return ret; |
8327 | } | |
8328 | ||
8ccf6f19 MX |
8329 | static void btrfs_run_delalloc_work(struct btrfs_work *work) |
8330 | { | |
8331 | struct btrfs_delalloc_work *delalloc_work; | |
9f23e289 | 8332 | struct inode *inode; |
8ccf6f19 MX |
8333 | |
8334 | delalloc_work = container_of(work, struct btrfs_delalloc_work, | |
8335 | work); | |
9f23e289 JB |
8336 | inode = delalloc_work->inode; |
8337 | if (delalloc_work->wait) { | |
8338 | btrfs_wait_ordered_range(inode, 0, (u64)-1); | |
8339 | } else { | |
8340 | filemap_flush(inode->i_mapping); | |
8341 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, | |
8342 | &BTRFS_I(inode)->runtime_flags)) | |
8343 | filemap_flush(inode->i_mapping); | |
8344 | } | |
8ccf6f19 MX |
8345 | |
8346 | if (delalloc_work->delay_iput) | |
9f23e289 | 8347 | btrfs_add_delayed_iput(inode); |
8ccf6f19 | 8348 | else |
9f23e289 | 8349 | iput(inode); |
8ccf6f19 MX |
8350 | complete(&delalloc_work->completion); |
8351 | } | |
8352 | ||
8353 | struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode, | |
8354 | int wait, int delay_iput) | |
8355 | { | |
8356 | struct btrfs_delalloc_work *work; | |
8357 | ||
8358 | work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS); | |
8359 | if (!work) | |
8360 | return NULL; | |
8361 | ||
8362 | init_completion(&work->completion); | |
8363 | INIT_LIST_HEAD(&work->list); | |
8364 | work->inode = inode; | |
8365 | work->wait = wait; | |
8366 | work->delay_iput = delay_iput; | |
8367 | work->work.func = btrfs_run_delalloc_work; | |
8368 | ||
8369 | return work; | |
8370 | } | |
8371 | ||
8372 | void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work) | |
8373 | { | |
8374 | wait_for_completion(&work->completion); | |
8375 | kmem_cache_free(btrfs_delalloc_work_cachep, work); | |
8376 | } | |
8377 | ||
d352ac68 CM |
8378 | /* |
8379 | * some fairly slow code that needs optimization. This walks the list | |
8380 | * of all the inodes with pending delalloc and forces them to disk. | |
8381 | */ | |
eb73c1b7 | 8382 | static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
ea8c2819 | 8383 | { |
ea8c2819 | 8384 | struct btrfs_inode *binode; |
5b21f2ed | 8385 | struct inode *inode; |
8ccf6f19 MX |
8386 | struct btrfs_delalloc_work *work, *next; |
8387 | struct list_head works; | |
1eafa6c7 | 8388 | struct list_head splice; |
8ccf6f19 | 8389 | int ret = 0; |
ea8c2819 | 8390 | |
8ccf6f19 | 8391 | INIT_LIST_HEAD(&works); |
1eafa6c7 | 8392 | INIT_LIST_HEAD(&splice); |
63607cc8 | 8393 | |
eb73c1b7 MX |
8394 | spin_lock(&root->delalloc_lock); |
8395 | list_splice_init(&root->delalloc_inodes, &splice); | |
1eafa6c7 MX |
8396 | while (!list_empty(&splice)) { |
8397 | binode = list_entry(splice.next, struct btrfs_inode, | |
ea8c2819 | 8398 | delalloc_inodes); |
1eafa6c7 | 8399 | |
eb73c1b7 MX |
8400 | list_move_tail(&binode->delalloc_inodes, |
8401 | &root->delalloc_inodes); | |
5b21f2ed | 8402 | inode = igrab(&binode->vfs_inode); |
df0af1a5 | 8403 | if (!inode) { |
eb73c1b7 | 8404 | cond_resched_lock(&root->delalloc_lock); |
1eafa6c7 | 8405 | continue; |
df0af1a5 | 8406 | } |
eb73c1b7 | 8407 | spin_unlock(&root->delalloc_lock); |
1eafa6c7 MX |
8408 | |
8409 | work = btrfs_alloc_delalloc_work(inode, 0, delay_iput); | |
8410 | if (unlikely(!work)) { | |
f4ab9ea7 JB |
8411 | if (delay_iput) |
8412 | btrfs_add_delayed_iput(inode); | |
8413 | else | |
8414 | iput(inode); | |
1eafa6c7 MX |
8415 | ret = -ENOMEM; |
8416 | goto out; | |
5b21f2ed | 8417 | } |
1eafa6c7 MX |
8418 | list_add_tail(&work->list, &works); |
8419 | btrfs_queue_worker(&root->fs_info->flush_workers, | |
8420 | &work->work); | |
8421 | ||
5b21f2ed | 8422 | cond_resched(); |
eb73c1b7 | 8423 | spin_lock(&root->delalloc_lock); |
ea8c2819 | 8424 | } |
eb73c1b7 | 8425 | spin_unlock(&root->delalloc_lock); |
8c8bee1d | 8426 | |
1eafa6c7 MX |
8427 | list_for_each_entry_safe(work, next, &works, list) { |
8428 | list_del_init(&work->list); | |
8429 | btrfs_wait_and_free_delalloc_work(work); | |
8430 | } | |
eb73c1b7 MX |
8431 | return 0; |
8432 | out: | |
8433 | list_for_each_entry_safe(work, next, &works, list) { | |
8434 | list_del_init(&work->list); | |
8435 | btrfs_wait_and_free_delalloc_work(work); | |
8436 | } | |
8437 | ||
8438 | if (!list_empty_careful(&splice)) { | |
8439 | spin_lock(&root->delalloc_lock); | |
8440 | list_splice_tail(&splice, &root->delalloc_inodes); | |
8441 | spin_unlock(&root->delalloc_lock); | |
8442 | } | |
8443 | return ret; | |
8444 | } | |
1eafa6c7 | 8445 | |
eb73c1b7 MX |
8446 | int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) |
8447 | { | |
8448 | int ret; | |
1eafa6c7 | 8449 | |
eb73c1b7 MX |
8450 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
8451 | return -EROFS; | |
8452 | ||
8453 | ret = __start_delalloc_inodes(root, delay_iput); | |
8454 | /* | |
8455 | * the filemap_flush will queue IO into the worker threads, but | |
8c8bee1d CM |
8456 | * we have to make sure the IO is actually started and that |
8457 | * ordered extents get created before we return | |
8458 | */ | |
8459 | atomic_inc(&root->fs_info->async_submit_draining); | |
d397712b | 8460 | while (atomic_read(&root->fs_info->nr_async_submits) || |
771ed689 | 8461 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
8c8bee1d | 8462 | wait_event(root->fs_info->async_submit_wait, |
771ed689 CM |
8463 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
8464 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | |
8c8bee1d CM |
8465 | } |
8466 | atomic_dec(&root->fs_info->async_submit_draining); | |
eb73c1b7 MX |
8467 | return ret; |
8468 | } | |
8469 | ||
91aef86f | 8470 | int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput) |
eb73c1b7 MX |
8471 | { |
8472 | struct btrfs_root *root; | |
8473 | struct list_head splice; | |
8474 | int ret; | |
8475 | ||
8476 | if (fs_info->sb->s_flags & MS_RDONLY) | |
8477 | return -EROFS; | |
8478 | ||
8479 | INIT_LIST_HEAD(&splice); | |
8480 | ||
8481 | spin_lock(&fs_info->delalloc_root_lock); | |
8482 | list_splice_init(&fs_info->delalloc_roots, &splice); | |
8483 | while (!list_empty(&splice)) { | |
8484 | root = list_first_entry(&splice, struct btrfs_root, | |
8485 | delalloc_root); | |
8486 | root = btrfs_grab_fs_root(root); | |
8487 | BUG_ON(!root); | |
8488 | list_move_tail(&root->delalloc_root, | |
8489 | &fs_info->delalloc_roots); | |
8490 | spin_unlock(&fs_info->delalloc_root_lock); | |
8491 | ||
8492 | ret = __start_delalloc_inodes(root, delay_iput); | |
8493 | btrfs_put_fs_root(root); | |
8494 | if (ret) | |
8495 | goto out; | |
8496 | ||
8497 | spin_lock(&fs_info->delalloc_root_lock); | |
8ccf6f19 | 8498 | } |
eb73c1b7 | 8499 | spin_unlock(&fs_info->delalloc_root_lock); |
1eafa6c7 | 8500 | |
eb73c1b7 MX |
8501 | atomic_inc(&fs_info->async_submit_draining); |
8502 | while (atomic_read(&fs_info->nr_async_submits) || | |
8503 | atomic_read(&fs_info->async_delalloc_pages)) { | |
8504 | wait_event(fs_info->async_submit_wait, | |
8505 | (atomic_read(&fs_info->nr_async_submits) == 0 && | |
8506 | atomic_read(&fs_info->async_delalloc_pages) == 0)); | |
8507 | } | |
8508 | atomic_dec(&fs_info->async_submit_draining); | |
8509 | return 0; | |
8510 | out: | |
1eafa6c7 | 8511 | if (!list_empty_careful(&splice)) { |
eb73c1b7 MX |
8512 | spin_lock(&fs_info->delalloc_root_lock); |
8513 | list_splice_tail(&splice, &fs_info->delalloc_roots); | |
8514 | spin_unlock(&fs_info->delalloc_root_lock); | |
1eafa6c7 | 8515 | } |
8ccf6f19 | 8516 | return ret; |
ea8c2819 CM |
8517 | } |
8518 | ||
39279cc3 CM |
8519 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
8520 | const char *symname) | |
8521 | { | |
8522 | struct btrfs_trans_handle *trans; | |
8523 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
8524 | struct btrfs_path *path; | |
8525 | struct btrfs_key key; | |
1832a6d5 | 8526 | struct inode *inode = NULL; |
39279cc3 CM |
8527 | int err; |
8528 | int drop_inode = 0; | |
8529 | u64 objectid; | |
67871254 | 8530 | u64 index = 0; |
39279cc3 CM |
8531 | int name_len; |
8532 | int datasize; | |
5f39d397 | 8533 | unsigned long ptr; |
39279cc3 | 8534 | struct btrfs_file_extent_item *ei; |
5f39d397 | 8535 | struct extent_buffer *leaf; |
39279cc3 | 8536 | |
f06becc4 | 8537 | name_len = strlen(symname); |
39279cc3 CM |
8538 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
8539 | return -ENAMETOOLONG; | |
1832a6d5 | 8540 | |
9ed74f2d JB |
8541 | /* |
8542 | * 2 items for inode item and ref | |
8543 | * 2 items for dir items | |
8544 | * 1 item for xattr if selinux is on | |
8545 | */ | |
a22285a6 YZ |
8546 | trans = btrfs_start_transaction(root, 5); |
8547 | if (IS_ERR(trans)) | |
8548 | return PTR_ERR(trans); | |
1832a6d5 | 8549 | |
581bb050 LZ |
8550 | err = btrfs_find_free_ino(root, &objectid); |
8551 | if (err) | |
8552 | goto out_unlock; | |
8553 | ||
aec7477b | 8554 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
33345d01 | 8555 | dentry->d_name.len, btrfs_ino(dir), objectid, |
d82a6f1d | 8556 | S_IFLNK|S_IRWXUGO, &index); |
7cf96da3 TI |
8557 | if (IS_ERR(inode)) { |
8558 | err = PTR_ERR(inode); | |
39279cc3 | 8559 | goto out_unlock; |
7cf96da3 | 8560 | } |
39279cc3 | 8561 | |
2a7dba39 | 8562 | err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); |
33268eaf JB |
8563 | if (err) { |
8564 | drop_inode = 1; | |
8565 | goto out_unlock; | |
8566 | } | |
8567 | ||
ad19db71 CS |
8568 | /* |
8569 | * If the active LSM wants to access the inode during | |
8570 | * d_instantiate it needs these. Smack checks to see | |
8571 | * if the filesystem supports xattrs by looking at the | |
8572 | * ops vector. | |
8573 | */ | |
8574 | inode->i_fop = &btrfs_file_operations; | |
8575 | inode->i_op = &btrfs_file_inode_operations; | |
8576 | ||
a1b075d2 | 8577 | err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); |
39279cc3 CM |
8578 | if (err) |
8579 | drop_inode = 1; | |
8580 | else { | |
8581 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 8582 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
d1310b2e | 8583 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 | 8584 | } |
39279cc3 CM |
8585 | if (drop_inode) |
8586 | goto out_unlock; | |
8587 | ||
8588 | path = btrfs_alloc_path(); | |
d8926bb3 MF |
8589 | if (!path) { |
8590 | err = -ENOMEM; | |
8591 | drop_inode = 1; | |
8592 | goto out_unlock; | |
8593 | } | |
33345d01 | 8594 | key.objectid = btrfs_ino(inode); |
39279cc3 | 8595 | key.offset = 0; |
39279cc3 CM |
8596 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
8597 | datasize = btrfs_file_extent_calc_inline_size(name_len); | |
8598 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
8599 | datasize); | |
54aa1f4d CM |
8600 | if (err) { |
8601 | drop_inode = 1; | |
b0839166 | 8602 | btrfs_free_path(path); |
54aa1f4d CM |
8603 | goto out_unlock; |
8604 | } | |
5f39d397 CM |
8605 | leaf = path->nodes[0]; |
8606 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
8607 | struct btrfs_file_extent_item); | |
8608 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
8609 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 8610 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
8611 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
8612 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
8613 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
8614 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
8615 | ||
39279cc3 | 8616 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
8617 | write_extent_buffer(leaf, symname, ptr, name_len); |
8618 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 8619 | btrfs_free_path(path); |
5f39d397 | 8620 | |
39279cc3 CM |
8621 | inode->i_op = &btrfs_symlink_inode_operations; |
8622 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
04160088 | 8623 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
d899e052 | 8624 | inode_set_bytes(inode, name_len); |
f06becc4 | 8625 | btrfs_i_size_write(inode, name_len); |
54aa1f4d CM |
8626 | err = btrfs_update_inode(trans, root, inode); |
8627 | if (err) | |
8628 | drop_inode = 1; | |
39279cc3 CM |
8629 | |
8630 | out_unlock: | |
08c422c2 AV |
8631 | if (!err) |
8632 | d_instantiate(dentry, inode); | |
7ad85bb7 | 8633 | btrfs_end_transaction(trans, root); |
39279cc3 CM |
8634 | if (drop_inode) { |
8635 | inode_dec_link_count(inode); | |
8636 | iput(inode); | |
8637 | } | |
b53d3f5d | 8638 | btrfs_btree_balance_dirty(root); |
39279cc3 CM |
8639 | return err; |
8640 | } | |
16432985 | 8641 | |
0af3d00b JB |
8642 | static int __btrfs_prealloc_file_range(struct inode *inode, int mode, |
8643 | u64 start, u64 num_bytes, u64 min_size, | |
8644 | loff_t actual_len, u64 *alloc_hint, | |
8645 | struct btrfs_trans_handle *trans) | |
d899e052 | 8646 | { |
5dc562c5 JB |
8647 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
8648 | struct extent_map *em; | |
d899e052 YZ |
8649 | struct btrfs_root *root = BTRFS_I(inode)->root; |
8650 | struct btrfs_key ins; | |
d899e052 | 8651 | u64 cur_offset = start; |
55a61d1d | 8652 | u64 i_size; |
154ea289 | 8653 | u64 cur_bytes; |
d899e052 | 8654 | int ret = 0; |
0af3d00b | 8655 | bool own_trans = true; |
d899e052 | 8656 | |
0af3d00b JB |
8657 | if (trans) |
8658 | own_trans = false; | |
d899e052 | 8659 | while (num_bytes > 0) { |
0af3d00b JB |
8660 | if (own_trans) { |
8661 | trans = btrfs_start_transaction(root, 3); | |
8662 | if (IS_ERR(trans)) { | |
8663 | ret = PTR_ERR(trans); | |
8664 | break; | |
8665 | } | |
5a303d5d YZ |
8666 | } |
8667 | ||
154ea289 CM |
8668 | cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); |
8669 | cur_bytes = max(cur_bytes, min_size); | |
00361589 JB |
8670 | ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, |
8671 | *alloc_hint, &ins, 1); | |
5a303d5d | 8672 | if (ret) { |
0af3d00b JB |
8673 | if (own_trans) |
8674 | btrfs_end_transaction(trans, root); | |
a22285a6 | 8675 | break; |
d899e052 | 8676 | } |
5a303d5d | 8677 | |
d899e052 YZ |
8678 | ret = insert_reserved_file_extent(trans, inode, |
8679 | cur_offset, ins.objectid, | |
8680 | ins.offset, ins.offset, | |
920bbbfb | 8681 | ins.offset, 0, 0, 0, |
d899e052 | 8682 | BTRFS_FILE_EXTENT_PREALLOC); |
79787eaa | 8683 | if (ret) { |
857cc2fc JB |
8684 | btrfs_free_reserved_extent(root, ins.objectid, |
8685 | ins.offset); | |
79787eaa JM |
8686 | btrfs_abort_transaction(trans, root, ret); |
8687 | if (own_trans) | |
8688 | btrfs_end_transaction(trans, root); | |
8689 | break; | |
8690 | } | |
a1ed835e CM |
8691 | btrfs_drop_extent_cache(inode, cur_offset, |
8692 | cur_offset + ins.offset -1, 0); | |
5a303d5d | 8693 | |
5dc562c5 JB |
8694 | em = alloc_extent_map(); |
8695 | if (!em) { | |
8696 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
8697 | &BTRFS_I(inode)->runtime_flags); | |
8698 | goto next; | |
8699 | } | |
8700 | ||
8701 | em->start = cur_offset; | |
8702 | em->orig_start = cur_offset; | |
8703 | em->len = ins.offset; | |
8704 | em->block_start = ins.objectid; | |
8705 | em->block_len = ins.offset; | |
b4939680 | 8706 | em->orig_block_len = ins.offset; |
cc95bef6 | 8707 | em->ram_bytes = ins.offset; |
5dc562c5 JB |
8708 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
8709 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
8710 | em->generation = trans->transid; | |
8711 | ||
8712 | while (1) { | |
8713 | write_lock(&em_tree->lock); | |
09a2a8f9 | 8714 | ret = add_extent_mapping(em_tree, em, 1); |
5dc562c5 JB |
8715 | write_unlock(&em_tree->lock); |
8716 | if (ret != -EEXIST) | |
8717 | break; | |
8718 | btrfs_drop_extent_cache(inode, cur_offset, | |
8719 | cur_offset + ins.offset - 1, | |
8720 | 0); | |
8721 | } | |
8722 | free_extent_map(em); | |
8723 | next: | |
d899e052 YZ |
8724 | num_bytes -= ins.offset; |
8725 | cur_offset += ins.offset; | |
efa56464 | 8726 | *alloc_hint = ins.objectid + ins.offset; |
5a303d5d | 8727 | |
0c4d2d95 | 8728 | inode_inc_iversion(inode); |
d899e052 | 8729 | inode->i_ctime = CURRENT_TIME; |
6cbff00f | 8730 | BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; |
d899e052 | 8731 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
efa56464 YZ |
8732 | (actual_len > inode->i_size) && |
8733 | (cur_offset > inode->i_size)) { | |
d1ea6a61 | 8734 | if (cur_offset > actual_len) |
55a61d1d | 8735 | i_size = actual_len; |
d1ea6a61 | 8736 | else |
55a61d1d JB |
8737 | i_size = cur_offset; |
8738 | i_size_write(inode, i_size); | |
8739 | btrfs_ordered_update_i_size(inode, i_size, NULL); | |
5a303d5d YZ |
8740 | } |
8741 | ||
d899e052 | 8742 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
8743 | |
8744 | if (ret) { | |
8745 | btrfs_abort_transaction(trans, root, ret); | |
8746 | if (own_trans) | |
8747 | btrfs_end_transaction(trans, root); | |
8748 | break; | |
8749 | } | |
d899e052 | 8750 | |
0af3d00b JB |
8751 | if (own_trans) |
8752 | btrfs_end_transaction(trans, root); | |
5a303d5d | 8753 | } |
d899e052 YZ |
8754 | return ret; |
8755 | } | |
8756 | ||
0af3d00b JB |
8757 | int btrfs_prealloc_file_range(struct inode *inode, int mode, |
8758 | u64 start, u64 num_bytes, u64 min_size, | |
8759 | loff_t actual_len, u64 *alloc_hint) | |
8760 | { | |
8761 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
8762 | min_size, actual_len, alloc_hint, | |
8763 | NULL); | |
8764 | } | |
8765 | ||
8766 | int btrfs_prealloc_file_range_trans(struct inode *inode, | |
8767 | struct btrfs_trans_handle *trans, int mode, | |
8768 | u64 start, u64 num_bytes, u64 min_size, | |
8769 | loff_t actual_len, u64 *alloc_hint) | |
8770 | { | |
8771 | return __btrfs_prealloc_file_range(inode, mode, start, num_bytes, | |
8772 | min_size, actual_len, alloc_hint, trans); | |
8773 | } | |
8774 | ||
e6dcd2dc CM |
8775 | static int btrfs_set_page_dirty(struct page *page) |
8776 | { | |
e6dcd2dc CM |
8777 | return __set_page_dirty_nobuffers(page); |
8778 | } | |
8779 | ||
10556cb2 | 8780 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd | 8781 | { |
b83cc969 | 8782 | struct btrfs_root *root = BTRFS_I(inode)->root; |
cb6db4e5 | 8783 | umode_t mode = inode->i_mode; |
b83cc969 | 8784 | |
cb6db4e5 JM |
8785 | if (mask & MAY_WRITE && |
8786 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) { | |
8787 | if (btrfs_root_readonly(root)) | |
8788 | return -EROFS; | |
8789 | if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) | |
8790 | return -EACCES; | |
8791 | } | |
2830ba7f | 8792 | return generic_permission(inode, mask); |
fdebe2bd | 8793 | } |
39279cc3 | 8794 | |
6e1d5dcc | 8795 | static const struct inode_operations btrfs_dir_inode_operations = { |
3394e160 | 8796 | .getattr = btrfs_getattr, |
39279cc3 CM |
8797 | .lookup = btrfs_lookup, |
8798 | .create = btrfs_create, | |
8799 | .unlink = btrfs_unlink, | |
8800 | .link = btrfs_link, | |
8801 | .mkdir = btrfs_mkdir, | |
8802 | .rmdir = btrfs_rmdir, | |
8803 | .rename = btrfs_rename, | |
8804 | .symlink = btrfs_symlink, | |
8805 | .setattr = btrfs_setattr, | |
618e21d5 | 8806 | .mknod = btrfs_mknod, |
95819c05 CH |
8807 | .setxattr = btrfs_setxattr, |
8808 | .getxattr = btrfs_getxattr, | |
5103e947 | 8809 | .listxattr = btrfs_listxattr, |
95819c05 | 8810 | .removexattr = btrfs_removexattr, |
fdebe2bd | 8811 | .permission = btrfs_permission, |
4e34e719 | 8812 | .get_acl = btrfs_get_acl, |
93fd63c2 | 8813 | .update_time = btrfs_update_time, |
39279cc3 | 8814 | }; |
6e1d5dcc | 8815 | static const struct inode_operations btrfs_dir_ro_inode_operations = { |
39279cc3 | 8816 | .lookup = btrfs_lookup, |
fdebe2bd | 8817 | .permission = btrfs_permission, |
4e34e719 | 8818 | .get_acl = btrfs_get_acl, |
93fd63c2 | 8819 | .update_time = btrfs_update_time, |
39279cc3 | 8820 | }; |
76dda93c | 8821 | |
828c0950 | 8822 | static const struct file_operations btrfs_dir_file_operations = { |
39279cc3 CM |
8823 | .llseek = generic_file_llseek, |
8824 | .read = generic_read_dir, | |
9cdda8d3 | 8825 | .iterate = btrfs_real_readdir, |
34287aa3 | 8826 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 8827 | #ifdef CONFIG_COMPAT |
34287aa3 | 8828 | .compat_ioctl = btrfs_ioctl, |
39279cc3 | 8829 | #endif |
6bf13c0c | 8830 | .release = btrfs_release_file, |
e02119d5 | 8831 | .fsync = btrfs_sync_file, |
39279cc3 CM |
8832 | }; |
8833 | ||
d1310b2e | 8834 | static struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 8835 | .fill_delalloc = run_delalloc_range, |
065631f6 | 8836 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 8837 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 8838 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 8839 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 8840 | .writepage_start_hook = btrfs_writepage_start_hook, |
b0c68f8b CM |
8841 | .set_bit_hook = btrfs_set_bit_hook, |
8842 | .clear_bit_hook = btrfs_clear_bit_hook, | |
9ed74f2d JB |
8843 | .merge_extent_hook = btrfs_merge_extent_hook, |
8844 | .split_extent_hook = btrfs_split_extent_hook, | |
07157aac CM |
8845 | }; |
8846 | ||
35054394 CM |
8847 | /* |
8848 | * btrfs doesn't support the bmap operation because swapfiles | |
8849 | * use bmap to make a mapping of extents in the file. They assume | |
8850 | * these extents won't change over the life of the file and they | |
8851 | * use the bmap result to do IO directly to the drive. | |
8852 | * | |
8853 | * the btrfs bmap call would return logical addresses that aren't | |
8854 | * suitable for IO and they also will change frequently as COW | |
8855 | * operations happen. So, swapfile + btrfs == corruption. | |
8856 | * | |
8857 | * For now we're avoiding this by dropping bmap. | |
8858 | */ | |
7f09410b | 8859 | static const struct address_space_operations btrfs_aops = { |
39279cc3 CM |
8860 | .readpage = btrfs_readpage, |
8861 | .writepage = btrfs_writepage, | |
b293f02e | 8862 | .writepages = btrfs_writepages, |
3ab2fb5a | 8863 | .readpages = btrfs_readpages, |
16432985 | 8864 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
8865 | .invalidatepage = btrfs_invalidatepage, |
8866 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 8867 | .set_page_dirty = btrfs_set_page_dirty, |
465fdd97 | 8868 | .error_remove_page = generic_error_remove_page, |
39279cc3 CM |
8869 | }; |
8870 | ||
7f09410b | 8871 | static const struct address_space_operations btrfs_symlink_aops = { |
39279cc3 CM |
8872 | .readpage = btrfs_readpage, |
8873 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
8874 | .invalidatepage = btrfs_invalidatepage, |
8875 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
8876 | }; |
8877 | ||
6e1d5dcc | 8878 | static const struct inode_operations btrfs_file_inode_operations = { |
39279cc3 CM |
8879 | .getattr = btrfs_getattr, |
8880 | .setattr = btrfs_setattr, | |
95819c05 CH |
8881 | .setxattr = btrfs_setxattr, |
8882 | .getxattr = btrfs_getxattr, | |
5103e947 | 8883 | .listxattr = btrfs_listxattr, |
95819c05 | 8884 | .removexattr = btrfs_removexattr, |
fdebe2bd | 8885 | .permission = btrfs_permission, |
1506fcc8 | 8886 | .fiemap = btrfs_fiemap, |
4e34e719 | 8887 | .get_acl = btrfs_get_acl, |
e41f941a | 8888 | .update_time = btrfs_update_time, |
39279cc3 | 8889 | }; |
6e1d5dcc | 8890 | static const struct inode_operations btrfs_special_inode_operations = { |
618e21d5 JB |
8891 | .getattr = btrfs_getattr, |
8892 | .setattr = btrfs_setattr, | |
fdebe2bd | 8893 | .permission = btrfs_permission, |
95819c05 CH |
8894 | .setxattr = btrfs_setxattr, |
8895 | .getxattr = btrfs_getxattr, | |
33268eaf | 8896 | .listxattr = btrfs_listxattr, |
95819c05 | 8897 | .removexattr = btrfs_removexattr, |
4e34e719 | 8898 | .get_acl = btrfs_get_acl, |
e41f941a | 8899 | .update_time = btrfs_update_time, |
618e21d5 | 8900 | }; |
6e1d5dcc | 8901 | static const struct inode_operations btrfs_symlink_inode_operations = { |
39279cc3 CM |
8902 | .readlink = generic_readlink, |
8903 | .follow_link = page_follow_link_light, | |
8904 | .put_link = page_put_link, | |
f209561a | 8905 | .getattr = btrfs_getattr, |
22c44fe6 | 8906 | .setattr = btrfs_setattr, |
fdebe2bd | 8907 | .permission = btrfs_permission, |
0279b4cd JO |
8908 | .setxattr = btrfs_setxattr, |
8909 | .getxattr = btrfs_getxattr, | |
8910 | .listxattr = btrfs_listxattr, | |
8911 | .removexattr = btrfs_removexattr, | |
4e34e719 | 8912 | .get_acl = btrfs_get_acl, |
e41f941a | 8913 | .update_time = btrfs_update_time, |
39279cc3 | 8914 | }; |
76dda93c | 8915 | |
82d339d9 | 8916 | const struct dentry_operations btrfs_dentry_operations = { |
76dda93c | 8917 | .d_delete = btrfs_dentry_delete, |
b4aff1f8 | 8918 | .d_release = btrfs_dentry_release, |
76dda93c | 8919 | }; |