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