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