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