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