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