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