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