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