]>
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> | |
29 | #include <linux/smp_lock.h> | |
30 | #include <linux/backing-dev.h> | |
31 | #include <linux/mpage.h> | |
32 | #include <linux/swap.h> | |
33 | #include <linux/writeback.h> | |
34 | #include <linux/statfs.h> | |
35 | #include <linux/compat.h> | |
9ebefb18 | 36 | #include <linux/bit_spinlock.h> |
92fee66d | 37 | #include <linux/version.h> |
5103e947 | 38 | #include <linux/xattr.h> |
33268eaf | 39 | #include <linux/posix_acl.h> |
d899e052 | 40 | #include <linux/falloc.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 CM |
50 | #include "compat.h" |
51 | #include "tree-log.h" | |
5b84e8d6 | 52 | #include "ref-cache.h" |
c8b97818 | 53 | #include "compression.h" |
39279cc3 CM |
54 | |
55 | struct btrfs_iget_args { | |
56 | u64 ino; | |
57 | struct btrfs_root *root; | |
58 | }; | |
59 | ||
60 | static struct inode_operations btrfs_dir_inode_operations; | |
61 | static struct inode_operations btrfs_symlink_inode_operations; | |
62 | static struct inode_operations btrfs_dir_ro_inode_operations; | |
618e21d5 | 63 | static struct inode_operations btrfs_special_inode_operations; |
39279cc3 CM |
64 | static struct inode_operations btrfs_file_inode_operations; |
65 | static struct address_space_operations btrfs_aops; | |
66 | static struct address_space_operations btrfs_symlink_aops; | |
67 | static struct file_operations btrfs_dir_file_operations; | |
d1310b2e | 68 | static struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
69 | |
70 | static struct kmem_cache *btrfs_inode_cachep; | |
71 | struct kmem_cache *btrfs_trans_handle_cachep; | |
72 | struct kmem_cache *btrfs_transaction_cachep; | |
73 | struct kmem_cache *btrfs_bit_radix_cachep; | |
74 | struct kmem_cache *btrfs_path_cachep; | |
75 | ||
76 | #define S_SHIFT 12 | |
77 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { | |
78 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, | |
79 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
80 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
81 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
82 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
83 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
84 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
85 | }; | |
86 | ||
7b128766 | 87 | static void btrfs_truncate(struct inode *inode); |
c8b97818 | 88 | static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end); |
7b128766 | 89 | |
d352ac68 CM |
90 | /* |
91 | * a very lame attempt at stopping writes when the FS is 85% full. There | |
92 | * are countless ways this is incorrect, but it is better than nothing. | |
93 | */ | |
1832a6d5 CM |
94 | int btrfs_check_free_space(struct btrfs_root *root, u64 num_required, |
95 | int for_del) | |
96 | { | |
a2135011 CM |
97 | u64 total; |
98 | u64 used; | |
1832a6d5 | 99 | u64 thresh; |
bcbfce8a | 100 | unsigned long flags; |
1832a6d5 CM |
101 | int ret = 0; |
102 | ||
a2135011 CM |
103 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
104 | total = btrfs_super_total_bytes(&root->fs_info->super_copy); | |
105 | used = btrfs_super_bytes_used(&root->fs_info->super_copy); | |
1832a6d5 | 106 | if (for_del) |
f9ef6604 | 107 | thresh = total * 90; |
1832a6d5 | 108 | else |
f9ef6604 CM |
109 | thresh = total * 85; |
110 | ||
111 | do_div(thresh, 100); | |
1832a6d5 | 112 | |
1832a6d5 CM |
113 | if (used + root->fs_info->delalloc_bytes + num_required > thresh) |
114 | ret = -ENOSPC; | |
bcbfce8a | 115 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
1832a6d5 CM |
116 | return ret; |
117 | } | |
118 | ||
c8b97818 CM |
119 | /* |
120 | * this does all the hard work for inserting an inline extent into | |
121 | * the btree. The caller should have done a btrfs_drop_extents so that | |
122 | * no overlapping inline items exist in the btree | |
123 | */ | |
124 | static int noinline insert_inline_extent(struct btrfs_trans_handle *trans, | |
125 | struct btrfs_root *root, struct inode *inode, | |
126 | u64 start, size_t size, size_t compressed_size, | |
127 | struct page **compressed_pages) | |
128 | { | |
129 | struct btrfs_key key; | |
130 | struct btrfs_path *path; | |
131 | struct extent_buffer *leaf; | |
132 | struct page *page = NULL; | |
133 | char *kaddr; | |
134 | unsigned long ptr; | |
135 | struct btrfs_file_extent_item *ei; | |
136 | int err = 0; | |
137 | int ret; | |
138 | size_t cur_size = size; | |
139 | size_t datasize; | |
140 | unsigned long offset; | |
141 | int use_compress = 0; | |
142 | ||
143 | if (compressed_size && compressed_pages) { | |
144 | use_compress = 1; | |
145 | cur_size = compressed_size; | |
146 | } | |
147 | ||
148 | path = btrfs_alloc_path(); if (!path) | |
149 | return -ENOMEM; | |
150 | ||
151 | btrfs_set_trans_block_group(trans, inode); | |
152 | ||
153 | key.objectid = inode->i_ino; | |
154 | key.offset = start; | |
155 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); | |
156 | inode_add_bytes(inode, size); | |
157 | datasize = btrfs_file_extent_calc_inline_size(cur_size); | |
158 | ||
159 | inode_add_bytes(inode, size); | |
160 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
161 | datasize); | |
162 | BUG_ON(ret); | |
163 | if (ret) { | |
164 | err = ret; | |
165 | printk("got bad ret %d\n", ret); | |
166 | goto fail; | |
167 | } | |
168 | leaf = path->nodes[0]; | |
169 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
170 | struct btrfs_file_extent_item); | |
171 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
172 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); | |
173 | btrfs_set_file_extent_encryption(leaf, ei, 0); | |
174 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
175 | btrfs_set_file_extent_ram_bytes(leaf, ei, size); | |
176 | ptr = btrfs_file_extent_inline_start(ei); | |
177 | ||
178 | if (use_compress) { | |
179 | struct page *cpage; | |
180 | int i = 0; | |
181 | while(compressed_size > 0) { | |
182 | cpage = compressed_pages[i]; | |
183 | cur_size = min(compressed_size, | |
184 | PAGE_CACHE_SIZE); | |
185 | ||
186 | kaddr = kmap(cpage); | |
187 | write_extent_buffer(leaf, kaddr, ptr, cur_size); | |
188 | kunmap(cpage); | |
189 | ||
190 | i++; | |
191 | ptr += cur_size; | |
192 | compressed_size -= cur_size; | |
193 | } | |
194 | btrfs_set_file_extent_compression(leaf, ei, | |
195 | BTRFS_COMPRESS_ZLIB); | |
196 | } else { | |
197 | page = find_get_page(inode->i_mapping, | |
198 | start >> PAGE_CACHE_SHIFT); | |
199 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
200 | kaddr = kmap_atomic(page, KM_USER0); | |
201 | offset = start & (PAGE_CACHE_SIZE - 1); | |
202 | write_extent_buffer(leaf, kaddr + offset, ptr, size); | |
203 | kunmap_atomic(kaddr, KM_USER0); | |
204 | page_cache_release(page); | |
205 | } | |
206 | btrfs_mark_buffer_dirty(leaf); | |
207 | btrfs_free_path(path); | |
208 | ||
209 | BTRFS_I(inode)->disk_i_size = inode->i_size; | |
210 | btrfs_update_inode(trans, root, inode); | |
211 | return 0; | |
212 | fail: | |
213 | btrfs_free_path(path); | |
214 | return err; | |
215 | } | |
216 | ||
217 | ||
218 | /* | |
219 | * conditionally insert an inline extent into the file. This | |
220 | * does the checks required to make sure the data is small enough | |
221 | * to fit as an inline extent. | |
222 | */ | |
223 | static int cow_file_range_inline(struct btrfs_trans_handle *trans, | |
224 | struct btrfs_root *root, | |
225 | struct inode *inode, u64 start, u64 end, | |
226 | size_t compressed_size, | |
227 | struct page **compressed_pages) | |
228 | { | |
229 | u64 isize = i_size_read(inode); | |
230 | u64 actual_end = min(end + 1, isize); | |
231 | u64 inline_len = actual_end - start; | |
232 | u64 aligned_end = (end + root->sectorsize - 1) & | |
233 | ~((u64)root->sectorsize - 1); | |
234 | u64 hint_byte; | |
235 | u64 data_len = inline_len; | |
236 | int ret; | |
237 | ||
238 | if (compressed_size) | |
239 | data_len = compressed_size; | |
240 | ||
241 | if (start > 0 || | |
70b99e69 | 242 | actual_end >= PAGE_CACHE_SIZE || |
c8b97818 CM |
243 | data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
244 | (!compressed_size && | |
245 | (actual_end & (root->sectorsize - 1)) == 0) || | |
246 | end + 1 < isize || | |
247 | data_len > root->fs_info->max_inline) { | |
248 | return 1; | |
249 | } | |
250 | ||
c8b97818 | 251 | ret = btrfs_drop_extents(trans, root, inode, start, |
70b99e69 | 252 | aligned_end, start, &hint_byte); |
c8b97818 CM |
253 | BUG_ON(ret); |
254 | ||
255 | if (isize > actual_end) | |
256 | inline_len = min_t(u64, isize, actual_end); | |
257 | ret = insert_inline_extent(trans, root, inode, start, | |
258 | inline_len, compressed_size, | |
259 | compressed_pages); | |
260 | BUG_ON(ret); | |
261 | btrfs_drop_extent_cache(inode, start, aligned_end, 0); | |
c8b97818 CM |
262 | return 0; |
263 | } | |
264 | ||
d352ac68 CM |
265 | /* |
266 | * when extent_io.c finds a delayed allocation range in the file, | |
267 | * the call backs end up in this code. The basic idea is to | |
268 | * allocate extents on disk for the range, and create ordered data structs | |
269 | * in ram to track those extents. | |
c8b97818 CM |
270 | * |
271 | * locked_page is the page that writepage had locked already. We use | |
272 | * it to make sure we don't do extra locks or unlocks. | |
273 | * | |
274 | * *page_started is set to one if we unlock locked_page and do everything | |
275 | * required to start IO on it. It may be clean and already done with | |
276 | * IO when we return. | |
d352ac68 | 277 | */ |
c8b97818 CM |
278 | static int cow_file_range(struct inode *inode, struct page *locked_page, |
279 | u64 start, u64 end, int *page_started) | |
b888db2b CM |
280 | { |
281 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
282 | struct btrfs_trans_handle *trans; | |
b888db2b | 283 | u64 alloc_hint = 0; |
db94535d | 284 | u64 num_bytes; |
c8b97818 CM |
285 | unsigned long ram_size; |
286 | u64 orig_start; | |
287 | u64 disk_num_bytes; | |
c59f8951 | 288 | u64 cur_alloc_size; |
db94535d | 289 | u64 blocksize = root->sectorsize; |
c8b97818 | 290 | u64 actual_end; |
be20aa9d | 291 | struct btrfs_key ins; |
e6dcd2dc CM |
292 | struct extent_map *em; |
293 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
294 | int ret = 0; | |
c8b97818 CM |
295 | struct page **pages = NULL; |
296 | unsigned long nr_pages; | |
297 | unsigned long nr_pages_ret = 0; | |
298 | unsigned long total_compressed = 0; | |
299 | unsigned long total_in = 0; | |
300 | unsigned long max_compressed = 128 * 1024; | |
301 | unsigned long max_uncompressed = 256 * 1024; | |
302 | int i; | |
80ff3856 | 303 | int ordered_type; |
c8b97818 | 304 | int will_compress; |
b888db2b | 305 | |
f9295749 | 306 | trans = btrfs_join_transaction(root, 1); |
b888db2b | 307 | BUG_ON(!trans); |
be20aa9d | 308 | btrfs_set_trans_block_group(trans, inode); |
c8b97818 CM |
309 | orig_start = start; |
310 | ||
311 | /* | |
312 | * compression made this loop a bit ugly, but the basic idea is to | |
313 | * compress some pages but keep the total size of the compressed | |
314 | * extent relatively small. If compression is off, this goto target | |
315 | * is never used. | |
316 | */ | |
317 | again: | |
318 | will_compress = 0; | |
319 | nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1; | |
320 | nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE); | |
be20aa9d | 321 | |
c8b97818 CM |
322 | actual_end = min_t(u64, i_size_read(inode), end + 1); |
323 | total_compressed = actual_end - start; | |
324 | ||
325 | /* we want to make sure that amount of ram required to uncompress | |
326 | * an extent is reasonable, so we limit the total size in ram | |
327 | * of a compressed extent to 256k | |
328 | */ | |
329 | total_compressed = min(total_compressed, max_uncompressed); | |
db94535d | 330 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
be20aa9d | 331 | num_bytes = max(blocksize, num_bytes); |
c8b97818 CM |
332 | disk_num_bytes = num_bytes; |
333 | total_in = 0; | |
334 | ret = 0; | |
db94535d | 335 | |
c8b97818 CM |
336 | /* we do compression for mount -o compress and when the |
337 | * inode has not been flagged as nocompress | |
338 | */ | |
339 | if (!btrfs_test_flag(inode, NOCOMPRESS) && | |
340 | btrfs_test_opt(root, COMPRESS)) { | |
341 | WARN_ON(pages); | |
cfbc246e | 342 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); |
c8b97818 CM |
343 | |
344 | /* we want to make sure the amount of IO required to satisfy | |
345 | * a random read is reasonably small, so we limit the size | |
346 | * of a compressed extent to 128k | |
347 | */ | |
348 | ret = btrfs_zlib_compress_pages(inode->i_mapping, start, | |
349 | total_compressed, pages, | |
350 | nr_pages, &nr_pages_ret, | |
351 | &total_in, | |
352 | &total_compressed, | |
353 | max_compressed); | |
354 | ||
355 | if (!ret) { | |
356 | unsigned long offset = total_compressed & | |
357 | (PAGE_CACHE_SIZE - 1); | |
358 | struct page *page = pages[nr_pages_ret - 1]; | |
359 | char *kaddr; | |
360 | ||
361 | /* zero the tail end of the last page, we might be | |
362 | * sending it down to disk | |
363 | */ | |
364 | if (offset) { | |
365 | kaddr = kmap_atomic(page, KM_USER0); | |
366 | memset(kaddr + offset, 0, | |
367 | PAGE_CACHE_SIZE - offset); | |
368 | kunmap_atomic(kaddr, KM_USER0); | |
369 | } | |
370 | will_compress = 1; | |
371 | } | |
372 | } | |
373 | if (start == 0) { | |
374 | /* lets try to make an inline extent */ | |
375 | if (ret || total_in < (end - start + 1)) { | |
376 | /* we didn't compress the entire range, try | |
377 | * to make an uncompressed inline extent. This | |
378 | * is almost sure to fail, but maybe inline sizes | |
379 | * will get bigger later | |
380 | */ | |
381 | ret = cow_file_range_inline(trans, root, inode, | |
382 | start, end, 0, NULL); | |
383 | } else { | |
384 | ret = cow_file_range_inline(trans, root, inode, | |
385 | start, end, | |
386 | total_compressed, pages); | |
387 | } | |
388 | if (ret == 0) { | |
389 | extent_clear_unlock_delalloc(inode, | |
390 | &BTRFS_I(inode)->io_tree, | |
391 | start, end, NULL, | |
392 | 1, 1, 1); | |
393 | *page_started = 1; | |
394 | ret = 0; | |
395 | goto free_pages_out; | |
396 | } | |
397 | } | |
398 | ||
399 | if (will_compress) { | |
400 | /* | |
401 | * we aren't doing an inline extent round the compressed size | |
402 | * up to a block size boundary so the allocator does sane | |
403 | * things | |
404 | */ | |
405 | total_compressed = (total_compressed + blocksize - 1) & | |
406 | ~(blocksize - 1); | |
407 | ||
408 | /* | |
409 | * one last check to make sure the compression is really a | |
410 | * win, compare the page count read with the blocks on disk | |
411 | */ | |
412 | total_in = (total_in + PAGE_CACHE_SIZE - 1) & | |
413 | ~(PAGE_CACHE_SIZE - 1); | |
414 | if (total_compressed >= total_in) { | |
415 | will_compress = 0; | |
416 | } else { | |
417 | disk_num_bytes = total_compressed; | |
418 | num_bytes = total_in; | |
419 | } | |
420 | } | |
421 | if (!will_compress && pages) { | |
422 | /* | |
423 | * the compression code ran but failed to make things smaller, | |
424 | * free any pages it allocated and our page pointer array | |
425 | */ | |
426 | for (i = 0; i < nr_pages_ret; i++) { | |
70b99e69 | 427 | WARN_ON(pages[i]->mapping); |
c8b97818 CM |
428 | page_cache_release(pages[i]); |
429 | } | |
430 | kfree(pages); | |
431 | pages = NULL; | |
432 | total_compressed = 0; | |
433 | nr_pages_ret = 0; | |
434 | ||
435 | /* flag the file so we don't compress in the future */ | |
436 | btrfs_set_flag(inode, NOCOMPRESS); | |
437 | } | |
438 | ||
439 | BUG_ON(disk_num_bytes > | |
440 | btrfs_super_total_bytes(&root->fs_info->super_copy)); | |
179e29e4 | 441 | |
5b21f2ed | 442 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); |
3b951516 | 443 | |
c8b97818 CM |
444 | while(disk_num_bytes > 0) { |
445 | unsigned long min_bytes; | |
446 | ||
447 | /* | |
448 | * the max size of a compressed extent is pretty small, | |
449 | * make the code a little less complex by forcing | |
450 | * the allocator to find a whole compressed extent at once | |
451 | */ | |
452 | if (will_compress) | |
453 | min_bytes = disk_num_bytes; | |
454 | else | |
455 | min_bytes = root->sectorsize; | |
456 | ||
457 | cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); | |
e6dcd2dc | 458 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
c8b97818 | 459 | min_bytes, 0, alloc_hint, |
e6dcd2dc | 460 | (u64)-1, &ins, 1); |
c59f8951 CM |
461 | if (ret) { |
462 | WARN_ON(1); | |
c8b97818 | 463 | goto free_pages_out_fail; |
c59f8951 | 464 | } |
e6dcd2dc CM |
465 | em = alloc_extent_map(GFP_NOFS); |
466 | em->start = start; | |
c8b97818 CM |
467 | |
468 | if (will_compress) { | |
469 | ram_size = num_bytes; | |
470 | em->len = num_bytes; | |
471 | } else { | |
472 | /* ramsize == disk size */ | |
473 | ram_size = ins.offset; | |
474 | em->len = ins.offset; | |
475 | } | |
476 | ||
e6dcd2dc | 477 | em->block_start = ins.objectid; |
c8b97818 | 478 | em->block_len = ins.offset; |
e6dcd2dc | 479 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
7f3c74fb | 480 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
c8b97818 CM |
481 | |
482 | if (will_compress) | |
483 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
484 | ||
e6dcd2dc CM |
485 | while(1) { |
486 | spin_lock(&em_tree->lock); | |
487 | ret = add_extent_mapping(em_tree, em); | |
488 | spin_unlock(&em_tree->lock); | |
489 | if (ret != -EEXIST) { | |
490 | free_extent_map(em); | |
491 | break; | |
492 | } | |
493 | btrfs_drop_extent_cache(inode, start, | |
c8b97818 | 494 | start + ram_size - 1, 0); |
e6dcd2dc CM |
495 | } |
496 | ||
98d20f67 | 497 | cur_alloc_size = ins.offset; |
80ff3856 | 498 | ordered_type = will_compress ? BTRFS_ORDERED_COMPRESSED : 0; |
e6dcd2dc | 499 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
80ff3856 YZ |
500 | ram_size, cur_alloc_size, |
501 | ordered_type); | |
e6dcd2dc | 502 | BUG_ON(ret); |
c8b97818 CM |
503 | |
504 | if (disk_num_bytes < cur_alloc_size) { | |
505 | printk("num_bytes %Lu cur_alloc %Lu\n", disk_num_bytes, | |
3b951516 CM |
506 | cur_alloc_size); |
507 | break; | |
508 | } | |
c8b97818 CM |
509 | |
510 | if (will_compress) { | |
511 | /* | |
512 | * we're doing compression, we and we need to | |
513 | * submit the compressed extents down to the device. | |
514 | * | |
515 | * We lock down all the file pages, clearing their | |
516 | * dirty bits and setting them writeback. Everyone | |
517 | * that wants to modify the page will wait on the | |
518 | * ordered extent above. | |
519 | * | |
520 | * The writeback bits on the file pages are | |
521 | * cleared when the compressed pages are on disk | |
522 | */ | |
523 | btrfs_end_transaction(trans, root); | |
524 | ||
525 | if (start <= page_offset(locked_page) && | |
526 | page_offset(locked_page) < start + ram_size) { | |
527 | *page_started = 1; | |
528 | } | |
529 | ||
530 | extent_clear_unlock_delalloc(inode, | |
531 | &BTRFS_I(inode)->io_tree, | |
532 | start, | |
533 | start + ram_size - 1, | |
534 | NULL, 1, 1, 0); | |
535 | ||
536 | ret = btrfs_submit_compressed_write(inode, start, | |
537 | ram_size, ins.objectid, | |
538 | cur_alloc_size, pages, | |
539 | nr_pages_ret); | |
540 | ||
541 | BUG_ON(ret); | |
542 | trans = btrfs_join_transaction(root, 1); | |
543 | if (start + ram_size < end) { | |
544 | start += ram_size; | |
545 | alloc_hint = ins.objectid + ins.offset; | |
546 | /* pages will be freed at end_bio time */ | |
547 | pages = NULL; | |
548 | goto again; | |
549 | } else { | |
550 | /* we've written everything, time to go */ | |
551 | break; | |
552 | } | |
553 | } | |
554 | /* we're not doing compressed IO, don't unlock the first | |
555 | * page (which the caller expects to stay locked), don't | |
556 | * clear any dirty bits and don't set any writeback bits | |
557 | */ | |
558 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, | |
559 | start, start + ram_size - 1, | |
560 | locked_page, 0, 0, 0); | |
561 | disk_num_bytes -= cur_alloc_size; | |
c59f8951 CM |
562 | num_bytes -= cur_alloc_size; |
563 | alloc_hint = ins.objectid + ins.offset; | |
564 | start += cur_alloc_size; | |
b888db2b | 565 | } |
c8b97818 CM |
566 | |
567 | ret = 0; | |
b888db2b CM |
568 | out: |
569 | btrfs_end_transaction(trans, root); | |
c8b97818 | 570 | |
be20aa9d | 571 | return ret; |
c8b97818 CM |
572 | |
573 | free_pages_out_fail: | |
574 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, | |
575 | start, end, locked_page, 0, 0, 0); | |
576 | free_pages_out: | |
70b99e69 CM |
577 | for (i = 0; i < nr_pages_ret; i++) { |
578 | WARN_ON(pages[i]->mapping); | |
c8b97818 | 579 | page_cache_release(pages[i]); |
70b99e69 | 580 | } |
c8b97818 CM |
581 | if (pages) |
582 | kfree(pages); | |
583 | ||
584 | goto out; | |
be20aa9d CM |
585 | } |
586 | ||
d352ac68 CM |
587 | /* |
588 | * when nowcow writeback call back. This checks for snapshots or COW copies | |
589 | * of the extents that exist in the file, and COWs the file as required. | |
590 | * | |
591 | * If no cow copies or snapshots exist, we write directly to the existing | |
592 | * blocks on disk | |
593 | */ | |
c8b97818 | 594 | static int run_delalloc_nocow(struct inode *inode, struct page *locked_page, |
d899e052 | 595 | u64 start, u64 end, int *page_started, int force) |
be20aa9d | 596 | { |
be20aa9d | 597 | struct btrfs_root *root = BTRFS_I(inode)->root; |
7ea394f1 | 598 | struct btrfs_trans_handle *trans; |
be20aa9d | 599 | struct extent_buffer *leaf; |
be20aa9d | 600 | struct btrfs_path *path; |
80ff3856 | 601 | struct btrfs_file_extent_item *fi; |
be20aa9d | 602 | struct btrfs_key found_key; |
80ff3856 YZ |
603 | u64 cow_start; |
604 | u64 cur_offset; | |
605 | u64 extent_end; | |
606 | u64 disk_bytenr; | |
607 | u64 num_bytes; | |
608 | int extent_type; | |
609 | int ret; | |
d899e052 | 610 | int type; |
80ff3856 YZ |
611 | int nocow; |
612 | int check_prev = 1; | |
be20aa9d CM |
613 | |
614 | path = btrfs_alloc_path(); | |
615 | BUG_ON(!path); | |
7ea394f1 YZ |
616 | trans = btrfs_join_transaction(root, 1); |
617 | BUG_ON(!trans); | |
be20aa9d | 618 | |
80ff3856 YZ |
619 | cow_start = (u64)-1; |
620 | cur_offset = start; | |
621 | while (1) { | |
622 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, | |
623 | cur_offset, 0); | |
624 | BUG_ON(ret < 0); | |
625 | if (ret > 0 && path->slots[0] > 0 && check_prev) { | |
626 | leaf = path->nodes[0]; | |
627 | btrfs_item_key_to_cpu(leaf, &found_key, | |
628 | path->slots[0] - 1); | |
629 | if (found_key.objectid == inode->i_ino && | |
630 | found_key.type == BTRFS_EXTENT_DATA_KEY) | |
631 | path->slots[0]--; | |
632 | } | |
633 | check_prev = 0; | |
634 | next_slot: | |
635 | leaf = path->nodes[0]; | |
636 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
637 | ret = btrfs_next_leaf(root, path); | |
638 | if (ret < 0) | |
639 | BUG_ON(1); | |
640 | if (ret > 0) | |
641 | break; | |
642 | leaf = path->nodes[0]; | |
643 | } | |
be20aa9d | 644 | |
80ff3856 YZ |
645 | nocow = 0; |
646 | disk_bytenr = 0; | |
647 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
648 | ||
649 | if (found_key.objectid > inode->i_ino || | |
650 | found_key.type > BTRFS_EXTENT_DATA_KEY || | |
651 | found_key.offset > end) | |
652 | break; | |
653 | ||
654 | if (found_key.offset > cur_offset) { | |
655 | extent_end = found_key.offset; | |
656 | goto out_check; | |
657 | } | |
658 | ||
659 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
660 | struct btrfs_file_extent_item); | |
661 | extent_type = btrfs_file_extent_type(leaf, fi); | |
662 | ||
d899e052 YZ |
663 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
664 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
80ff3856 YZ |
665 | struct btrfs_block_group_cache *block_group; |
666 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
667 | extent_end = found_key.offset + | |
668 | btrfs_file_extent_num_bytes(leaf, fi); | |
669 | if (extent_end <= start) { | |
670 | path->slots[0]++; | |
671 | goto next_slot; | |
672 | } | |
673 | if (btrfs_file_extent_compression(leaf, fi) || | |
674 | btrfs_file_extent_encryption(leaf, fi) || | |
675 | btrfs_file_extent_other_encoding(leaf, fi)) | |
676 | goto out_check; | |
677 | if (disk_bytenr == 0) | |
678 | goto out_check; | |
d899e052 YZ |
679 | if (extent_type == BTRFS_FILE_EXTENT_REG && !force) |
680 | goto out_check; | |
80ff3856 YZ |
681 | if (btrfs_cross_ref_exist(trans, root, disk_bytenr)) |
682 | goto out_check; | |
683 | block_group = btrfs_lookup_block_group(root->fs_info, | |
684 | disk_bytenr); | |
685 | if (!block_group || block_group->ro) | |
686 | goto out_check; | |
687 | disk_bytenr += btrfs_file_extent_offset(leaf, fi); | |
688 | nocow = 1; | |
689 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { | |
690 | extent_end = found_key.offset + | |
691 | btrfs_file_extent_inline_len(leaf, fi); | |
692 | extent_end = ALIGN(extent_end, root->sectorsize); | |
693 | } else { | |
694 | BUG_ON(1); | |
695 | } | |
696 | out_check: | |
697 | if (extent_end <= start) { | |
698 | path->slots[0]++; | |
699 | goto next_slot; | |
700 | } | |
701 | if (!nocow) { | |
702 | if (cow_start == (u64)-1) | |
703 | cow_start = cur_offset; | |
704 | cur_offset = extent_end; | |
705 | if (cur_offset > end) | |
706 | break; | |
707 | path->slots[0]++; | |
708 | goto next_slot; | |
7ea394f1 YZ |
709 | } |
710 | ||
711 | btrfs_release_path(root, path); | |
80ff3856 YZ |
712 | if (cow_start != (u64)-1) { |
713 | ret = cow_file_range(inode, locked_page, cow_start, | |
714 | found_key.offset - 1, page_started); | |
715 | BUG_ON(ret); | |
716 | cow_start = (u64)-1; | |
7ea394f1 | 717 | } |
80ff3856 YZ |
718 | |
719 | disk_bytenr += cur_offset - found_key.offset; | |
720 | num_bytes = min(end + 1, extent_end) - cur_offset; | |
d899e052 YZ |
721 | if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
722 | struct extent_map *em; | |
723 | struct extent_map_tree *em_tree; | |
724 | em_tree = &BTRFS_I(inode)->extent_tree; | |
725 | em = alloc_extent_map(GFP_NOFS); | |
726 | em->start = cur_offset; | |
727 | em->len = num_bytes; | |
728 | em->block_len = num_bytes; | |
729 | em->block_start = disk_bytenr; | |
730 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
731 | set_bit(EXTENT_FLAG_PINNED, &em->flags); | |
732 | while (1) { | |
733 | spin_lock(&em_tree->lock); | |
734 | ret = add_extent_mapping(em_tree, em); | |
735 | spin_unlock(&em_tree->lock); | |
736 | if (ret != -EEXIST) { | |
737 | free_extent_map(em); | |
738 | break; | |
739 | } | |
740 | btrfs_drop_extent_cache(inode, em->start, | |
741 | em->start + em->len - 1, 0); | |
742 | } | |
743 | type = BTRFS_ORDERED_PREALLOC; | |
744 | } else { | |
745 | type = BTRFS_ORDERED_NOCOW; | |
746 | } | |
80ff3856 YZ |
747 | |
748 | ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, | |
d899e052 YZ |
749 | num_bytes, num_bytes, type); |
750 | BUG_ON(ret); | |
751 | extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, | |
752 | cur_offset, cur_offset + num_bytes - 1, | |
753 | locked_page, 0, 0, 0); | |
80ff3856 YZ |
754 | cur_offset = extent_end; |
755 | if (cur_offset > end) | |
756 | break; | |
be20aa9d | 757 | } |
80ff3856 YZ |
758 | btrfs_release_path(root, path); |
759 | ||
760 | if (cur_offset <= end && cow_start == (u64)-1) | |
761 | cow_start = cur_offset; | |
762 | if (cow_start != (u64)-1) { | |
763 | ret = cow_file_range(inode, locked_page, cow_start, end, | |
764 | page_started); | |
765 | BUG_ON(ret); | |
766 | } | |
767 | ||
768 | ret = btrfs_end_transaction(trans, root); | |
769 | BUG_ON(ret); | |
7ea394f1 | 770 | btrfs_free_path(path); |
80ff3856 | 771 | return 0; |
be20aa9d CM |
772 | } |
773 | ||
d352ac68 CM |
774 | /* |
775 | * extent_io.c call back to do delayed allocation processing | |
776 | */ | |
c8b97818 CM |
777 | static int run_delalloc_range(struct inode *inode, struct page *locked_page, |
778 | u64 start, u64 end, int *page_started) | |
be20aa9d CM |
779 | { |
780 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
781 | int ret; | |
a2135011 | 782 | |
b98b6767 Y |
783 | if (btrfs_test_opt(root, NODATACOW) || |
784 | btrfs_test_flag(inode, NODATACOW)) | |
c8b97818 | 785 | ret = run_delalloc_nocow(inode, locked_page, start, end, |
d899e052 YZ |
786 | page_started, 0); |
787 | else if (btrfs_test_flag(inode, PREALLOC)) | |
788 | ret = run_delalloc_nocow(inode, locked_page, start, end, | |
789 | page_started, 1); | |
be20aa9d | 790 | else |
c8b97818 CM |
791 | ret = cow_file_range(inode, locked_page, start, end, |
792 | page_started); | |
1832a6d5 | 793 | |
b888db2b CM |
794 | return ret; |
795 | } | |
796 | ||
d352ac68 CM |
797 | /* |
798 | * extent_io.c set_bit_hook, used to track delayed allocation | |
799 | * bytes in this file, and to maintain the list of inodes that | |
800 | * have pending delalloc work to be done. | |
801 | */ | |
291d673e | 802 | int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, |
b0c68f8b | 803 | unsigned long old, unsigned long bits) |
291d673e | 804 | { |
bcbfce8a | 805 | unsigned long flags; |
b0c68f8b | 806 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
291d673e | 807 | struct btrfs_root *root = BTRFS_I(inode)->root; |
bcbfce8a | 808 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
9069218d | 809 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
291d673e | 810 | root->fs_info->delalloc_bytes += end - start + 1; |
ea8c2819 CM |
811 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
812 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
813 | &root->fs_info->delalloc_inodes); | |
814 | } | |
bcbfce8a | 815 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
291d673e CM |
816 | } |
817 | return 0; | |
818 | } | |
819 | ||
d352ac68 CM |
820 | /* |
821 | * extent_io.c clear_bit_hook, see set_bit_hook for why | |
822 | */ | |
291d673e | 823 | int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, |
b0c68f8b | 824 | unsigned long old, unsigned long bits) |
291d673e | 825 | { |
b0c68f8b | 826 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
291d673e | 827 | struct btrfs_root *root = BTRFS_I(inode)->root; |
bcbfce8a CM |
828 | unsigned long flags; |
829 | ||
830 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); | |
b0c68f8b CM |
831 | if (end - start + 1 > root->fs_info->delalloc_bytes) { |
832 | printk("warning: delalloc account %Lu %Lu\n", | |
833 | end - start + 1, root->fs_info->delalloc_bytes); | |
834 | root->fs_info->delalloc_bytes = 0; | |
9069218d | 835 | BTRFS_I(inode)->delalloc_bytes = 0; |
b0c68f8b CM |
836 | } else { |
837 | root->fs_info->delalloc_bytes -= end - start + 1; | |
9069218d | 838 | BTRFS_I(inode)->delalloc_bytes -= end - start + 1; |
b0c68f8b | 839 | } |
ea8c2819 CM |
840 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
841 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
842 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); | |
843 | } | |
bcbfce8a | 844 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
291d673e CM |
845 | } |
846 | return 0; | |
847 | } | |
848 | ||
d352ac68 CM |
849 | /* |
850 | * extent_io.c merge_bio_hook, this must check the chunk tree to make sure | |
851 | * we don't create bios that span stripes or chunks | |
852 | */ | |
239b14b3 | 853 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
c8b97818 CM |
854 | size_t size, struct bio *bio, |
855 | unsigned long bio_flags) | |
239b14b3 CM |
856 | { |
857 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
858 | struct btrfs_mapping_tree *map_tree; | |
a62b9401 | 859 | u64 logical = (u64)bio->bi_sector << 9; |
239b14b3 CM |
860 | u64 length = 0; |
861 | u64 map_length; | |
239b14b3 CM |
862 | int ret; |
863 | ||
f2d8d74d | 864 | length = bio->bi_size; |
239b14b3 CM |
865 | map_tree = &root->fs_info->mapping_tree; |
866 | map_length = length; | |
cea9e445 | 867 | ret = btrfs_map_block(map_tree, READ, logical, |
f188591e | 868 | &map_length, NULL, 0); |
cea9e445 | 869 | |
239b14b3 | 870 | if (map_length < length + size) { |
239b14b3 CM |
871 | return 1; |
872 | } | |
873 | return 0; | |
874 | } | |
875 | ||
d352ac68 CM |
876 | /* |
877 | * in order to insert checksums into the metadata in large chunks, | |
878 | * we wait until bio submission time. All the pages in the bio are | |
879 | * checksummed and sums are attached onto the ordered extent record. | |
880 | * | |
881 | * At IO completion time the cums attached on the ordered extent record | |
882 | * are inserted into the btree | |
883 | */ | |
44b8bd7e | 884 | int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
c8b97818 | 885 | int mirror_num, unsigned long bio_flags) |
065631f6 | 886 | { |
065631f6 | 887 | struct btrfs_root *root = BTRFS_I(inode)->root; |
065631f6 | 888 | int ret = 0; |
e015640f | 889 | |
3edf7d33 | 890 | ret = btrfs_csum_one_bio(root, inode, bio); |
44b8bd7e | 891 | BUG_ON(ret); |
e015640f | 892 | |
8b712842 | 893 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
44b8bd7e CM |
894 | } |
895 | ||
d352ac68 CM |
896 | /* |
897 | * extent_io.c submission hook. This does the right thing for csum calculation on write, | |
898 | * or reading the csums from the tree before a read | |
899 | */ | |
44b8bd7e | 900 | int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
c8b97818 | 901 | int mirror_num, unsigned long bio_flags) |
44b8bd7e CM |
902 | { |
903 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
904 | int ret = 0; | |
19b9bdb0 | 905 | int skip_sum; |
44b8bd7e | 906 | |
e6dcd2dc CM |
907 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
908 | BUG_ON(ret); | |
065631f6 | 909 | |
19b9bdb0 CM |
910 | skip_sum = btrfs_test_opt(root, NODATASUM) || |
911 | btrfs_test_flag(inode, NODATASUM); | |
7ea394f1 | 912 | |
4d1b5fb4 | 913 | if (!(rw & (1 << BIO_RW))) { |
19b9bdb0 CM |
914 | if (!skip_sum) |
915 | btrfs_lookup_bio_sums(root, inode, bio); | |
c8b97818 | 916 | |
19b9bdb0 | 917 | if (bio_flags & EXTENT_BIO_COMPRESSED) |
c8b97818 CM |
918 | return btrfs_submit_compressed_read(inode, bio, |
919 | mirror_num, bio_flags); | |
4d1b5fb4 | 920 | goto mapit; |
19b9bdb0 CM |
921 | } else if (!skip_sum) { |
922 | /* we're doing a write, do the async checksumming */ | |
923 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, | |
44b8bd7e | 924 | inode, rw, bio, mirror_num, |
c8b97818 | 925 | bio_flags, __btrfs_submit_bio_hook); |
19b9bdb0 CM |
926 | } |
927 | ||
0b86a832 | 928 | mapit: |
8b712842 | 929 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
065631f6 | 930 | } |
6885f308 | 931 | |
d352ac68 CM |
932 | /* |
933 | * given a list of ordered sums record them in the inode. This happens | |
934 | * at IO completion time based on sums calculated at bio submission time. | |
935 | */ | |
ba1da2f4 | 936 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
937 | struct inode *inode, u64 file_offset, |
938 | struct list_head *list) | |
939 | { | |
940 | struct list_head *cur; | |
941 | struct btrfs_ordered_sum *sum; | |
942 | ||
943 | btrfs_set_trans_block_group(trans, inode); | |
ba1da2f4 | 944 | list_for_each(cur, list) { |
e6dcd2dc | 945 | sum = list_entry(cur, struct btrfs_ordered_sum, list); |
e6dcd2dc CM |
946 | btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root, |
947 | inode, sum); | |
e6dcd2dc CM |
948 | } |
949 | return 0; | |
950 | } | |
951 | ||
ea8c2819 CM |
952 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end) |
953 | { | |
954 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, | |
955 | GFP_NOFS); | |
956 | } | |
957 | ||
d352ac68 | 958 | /* see btrfs_writepage_start_hook for details on why this is required */ |
247e743c CM |
959 | struct btrfs_writepage_fixup { |
960 | struct page *page; | |
961 | struct btrfs_work work; | |
962 | }; | |
963 | ||
247e743c CM |
964 | void btrfs_writepage_fixup_worker(struct btrfs_work *work) |
965 | { | |
966 | struct btrfs_writepage_fixup *fixup; | |
967 | struct btrfs_ordered_extent *ordered; | |
968 | struct page *page; | |
969 | struct inode *inode; | |
970 | u64 page_start; | |
971 | u64 page_end; | |
972 | ||
973 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
974 | page = fixup->page; | |
4a096752 | 975 | again: |
247e743c CM |
976 | lock_page(page); |
977 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
978 | ClearPageChecked(page); | |
979 | goto out_page; | |
980 | } | |
981 | ||
982 | inode = page->mapping->host; | |
983 | page_start = page_offset(page); | |
984 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; | |
985 | ||
986 | lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); | |
4a096752 CM |
987 | |
988 | /* already ordered? We're done */ | |
989 | if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, | |
990 | EXTENT_ORDERED, 0)) { | |
247e743c | 991 | goto out; |
4a096752 CM |
992 | } |
993 | ||
994 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
995 | if (ordered) { | |
996 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, | |
997 | page_end, GFP_NOFS); | |
998 | unlock_page(page); | |
999 | btrfs_start_ordered_extent(inode, ordered, 1); | |
1000 | goto again; | |
1001 | } | |
247e743c | 1002 | |
ea8c2819 | 1003 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
247e743c CM |
1004 | ClearPageChecked(page); |
1005 | out: | |
1006 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); | |
1007 | out_page: | |
1008 | unlock_page(page); | |
1009 | page_cache_release(page); | |
1010 | } | |
1011 | ||
1012 | /* | |
1013 | * There are a few paths in the higher layers of the kernel that directly | |
1014 | * set the page dirty bit without asking the filesystem if it is a | |
1015 | * good idea. This causes problems because we want to make sure COW | |
1016 | * properly happens and the data=ordered rules are followed. | |
1017 | * | |
c8b97818 | 1018 | * In our case any range that doesn't have the ORDERED bit set |
247e743c CM |
1019 | * hasn't been properly setup for IO. We kick off an async process |
1020 | * to fix it up. The async helper will wait for ordered extents, set | |
1021 | * the delalloc bit and make it safe to write the page. | |
1022 | */ | |
1023 | int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) | |
1024 | { | |
1025 | struct inode *inode = page->mapping->host; | |
1026 | struct btrfs_writepage_fixup *fixup; | |
1027 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1028 | int ret; | |
1029 | ||
1030 | ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
1031 | EXTENT_ORDERED, 0); | |
1032 | if (ret) | |
1033 | return 0; | |
1034 | ||
1035 | if (PageChecked(page)) | |
1036 | return -EAGAIN; | |
1037 | ||
1038 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
1039 | if (!fixup) | |
1040 | return -EAGAIN; | |
f421950f | 1041 | |
247e743c CM |
1042 | SetPageChecked(page); |
1043 | page_cache_get(page); | |
1044 | fixup->work.func = btrfs_writepage_fixup_worker; | |
1045 | fixup->page = page; | |
1046 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); | |
1047 | return -EAGAIN; | |
1048 | } | |
1049 | ||
d899e052 YZ |
1050 | static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, |
1051 | struct inode *inode, u64 file_pos, | |
1052 | u64 disk_bytenr, u64 disk_num_bytes, | |
1053 | u64 num_bytes, u64 ram_bytes, | |
1054 | u8 compression, u8 encryption, | |
1055 | u16 other_encoding, int extent_type) | |
1056 | { | |
1057 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1058 | struct btrfs_file_extent_item *fi; | |
1059 | struct btrfs_path *path; | |
1060 | struct extent_buffer *leaf; | |
1061 | struct btrfs_key ins; | |
1062 | u64 hint; | |
1063 | int ret; | |
1064 | ||
1065 | path = btrfs_alloc_path(); | |
1066 | BUG_ON(!path); | |
1067 | ||
1068 | ret = btrfs_drop_extents(trans, root, inode, file_pos, | |
1069 | file_pos + num_bytes, file_pos, &hint); | |
1070 | BUG_ON(ret); | |
1071 | ||
1072 | ins.objectid = inode->i_ino; | |
1073 | ins.offset = file_pos; | |
1074 | ins.type = BTRFS_EXTENT_DATA_KEY; | |
1075 | ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); | |
1076 | BUG_ON(ret); | |
1077 | leaf = path->nodes[0]; | |
1078 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
1079 | struct btrfs_file_extent_item); | |
1080 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); | |
1081 | btrfs_set_file_extent_type(leaf, fi, extent_type); | |
1082 | btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr); | |
1083 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes); | |
1084 | btrfs_set_file_extent_offset(leaf, fi, 0); | |
1085 | btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes); | |
1086 | btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes); | |
1087 | btrfs_set_file_extent_compression(leaf, fi, compression); | |
1088 | btrfs_set_file_extent_encryption(leaf, fi, encryption); | |
1089 | btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding); | |
1090 | btrfs_mark_buffer_dirty(leaf); | |
1091 | ||
1092 | inode_add_bytes(inode, num_bytes); | |
1093 | btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0); | |
1094 | ||
1095 | ins.objectid = disk_bytenr; | |
1096 | ins.offset = disk_num_bytes; | |
1097 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
1098 | ret = btrfs_alloc_reserved_extent(trans, root, leaf->start, | |
1099 | root->root_key.objectid, | |
1100 | trans->transid, inode->i_ino, &ins); | |
1101 | BUG_ON(ret); | |
1102 | ||
1103 | btrfs_free_path(path); | |
1104 | return 0; | |
1105 | } | |
1106 | ||
d352ac68 CM |
1107 | /* as ordered data IO finishes, this gets called so we can finish |
1108 | * an ordered extent if the range of bytes in the file it covers are | |
1109 | * fully written. | |
1110 | */ | |
211f90e6 | 1111 | static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end) |
e6dcd2dc | 1112 | { |
e6dcd2dc CM |
1113 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1114 | struct btrfs_trans_handle *trans; | |
1115 | struct btrfs_ordered_extent *ordered_extent; | |
1116 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
d899e052 | 1117 | int compressed = 0; |
e6dcd2dc CM |
1118 | int ret; |
1119 | ||
1120 | ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1); | |
ba1da2f4 | 1121 | if (!ret) |
e6dcd2dc | 1122 | return 0; |
e6dcd2dc | 1123 | |
f9295749 | 1124 | trans = btrfs_join_transaction(root, 1); |
e6dcd2dc CM |
1125 | |
1126 | ordered_extent = btrfs_lookup_ordered_extent(inode, start); | |
1127 | BUG_ON(!ordered_extent); | |
7ea394f1 YZ |
1128 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) |
1129 | goto nocow; | |
e6dcd2dc CM |
1130 | |
1131 | lock_extent(io_tree, ordered_extent->file_offset, | |
1132 | ordered_extent->file_offset + ordered_extent->len - 1, | |
1133 | GFP_NOFS); | |
1134 | ||
c8b97818 | 1135 | if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) |
d899e052 YZ |
1136 | compressed = 1; |
1137 | if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { | |
1138 | BUG_ON(compressed); | |
1139 | ret = btrfs_mark_extent_written(trans, root, inode, | |
1140 | ordered_extent->file_offset, | |
1141 | ordered_extent->file_offset + | |
1142 | ordered_extent->len); | |
1143 | BUG_ON(ret); | |
1144 | } else { | |
1145 | ret = insert_reserved_file_extent(trans, inode, | |
1146 | ordered_extent->file_offset, | |
1147 | ordered_extent->start, | |
1148 | ordered_extent->disk_len, | |
1149 | ordered_extent->len, | |
1150 | ordered_extent->len, | |
1151 | compressed, 0, 0, | |
1152 | BTRFS_FILE_EXTENT_REG); | |
1153 | BUG_ON(ret); | |
1154 | } | |
e6dcd2dc CM |
1155 | unlock_extent(io_tree, ordered_extent->file_offset, |
1156 | ordered_extent->file_offset + ordered_extent->len - 1, | |
1157 | GFP_NOFS); | |
7ea394f1 | 1158 | nocow: |
e6dcd2dc CM |
1159 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
1160 | &ordered_extent->list); | |
1161 | ||
34353029 | 1162 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
dbe674a9 | 1163 | btrfs_ordered_update_i_size(inode, ordered_extent); |
e02119d5 | 1164 | btrfs_update_inode(trans, root, inode); |
e6dcd2dc | 1165 | btrfs_remove_ordered_extent(inode, ordered_extent); |
34353029 | 1166 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
7f3c74fb | 1167 | |
e6dcd2dc CM |
1168 | /* once for us */ |
1169 | btrfs_put_ordered_extent(ordered_extent); | |
1170 | /* once for the tree */ | |
1171 | btrfs_put_ordered_extent(ordered_extent); | |
1172 | ||
e6dcd2dc CM |
1173 | btrfs_end_transaction(trans, root); |
1174 | return 0; | |
1175 | } | |
1176 | ||
211f90e6 CM |
1177 | int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
1178 | struct extent_state *state, int uptodate) | |
1179 | { | |
1180 | return btrfs_finish_ordered_io(page->mapping->host, start, end); | |
1181 | } | |
1182 | ||
d352ac68 CM |
1183 | /* |
1184 | * When IO fails, either with EIO or csum verification fails, we | |
1185 | * try other mirrors that might have a good copy of the data. This | |
1186 | * io_failure_record is used to record state as we go through all the | |
1187 | * mirrors. If another mirror has good data, the page is set up to date | |
1188 | * and things continue. If a good mirror can't be found, the original | |
1189 | * bio end_io callback is called to indicate things have failed. | |
1190 | */ | |
7e38326f CM |
1191 | struct io_failure_record { |
1192 | struct page *page; | |
1193 | u64 start; | |
1194 | u64 len; | |
1195 | u64 logical; | |
1196 | int last_mirror; | |
1197 | }; | |
1198 | ||
1259ab75 CM |
1199 | int btrfs_io_failed_hook(struct bio *failed_bio, |
1200 | struct page *page, u64 start, u64 end, | |
1201 | struct extent_state *state) | |
7e38326f CM |
1202 | { |
1203 | struct io_failure_record *failrec = NULL; | |
1204 | u64 private; | |
1205 | struct extent_map *em; | |
1206 | struct inode *inode = page->mapping->host; | |
1207 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
3b951516 | 1208 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
7e38326f CM |
1209 | struct bio *bio; |
1210 | int num_copies; | |
1211 | int ret; | |
1259ab75 | 1212 | int rw; |
7e38326f | 1213 | u64 logical; |
c8b97818 | 1214 | unsigned long bio_flags = 0; |
7e38326f CM |
1215 | |
1216 | ret = get_state_private(failure_tree, start, &private); | |
1217 | if (ret) { | |
7e38326f CM |
1218 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
1219 | if (!failrec) | |
1220 | return -ENOMEM; | |
1221 | failrec->start = start; | |
1222 | failrec->len = end - start + 1; | |
1223 | failrec->last_mirror = 0; | |
1224 | ||
3b951516 CM |
1225 | spin_lock(&em_tree->lock); |
1226 | em = lookup_extent_mapping(em_tree, start, failrec->len); | |
1227 | if (em->start > start || em->start + em->len < start) { | |
1228 | free_extent_map(em); | |
1229 | em = NULL; | |
1230 | } | |
1231 | spin_unlock(&em_tree->lock); | |
7e38326f CM |
1232 | |
1233 | if (!em || IS_ERR(em)) { | |
1234 | kfree(failrec); | |
1235 | return -EIO; | |
1236 | } | |
1237 | logical = start - em->start; | |
1238 | logical = em->block_start + logical; | |
c8b97818 CM |
1239 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
1240 | bio_flags = EXTENT_BIO_COMPRESSED; | |
7e38326f CM |
1241 | failrec->logical = logical; |
1242 | free_extent_map(em); | |
1243 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | | |
1244 | EXTENT_DIRTY, GFP_NOFS); | |
587f7704 CM |
1245 | set_state_private(failure_tree, start, |
1246 | (u64)(unsigned long)failrec); | |
7e38326f | 1247 | } else { |
587f7704 | 1248 | failrec = (struct io_failure_record *)(unsigned long)private; |
7e38326f CM |
1249 | } |
1250 | num_copies = btrfs_num_copies( | |
1251 | &BTRFS_I(inode)->root->fs_info->mapping_tree, | |
1252 | failrec->logical, failrec->len); | |
1253 | failrec->last_mirror++; | |
1254 | if (!state) { | |
1255 | spin_lock_irq(&BTRFS_I(inode)->io_tree.lock); | |
1256 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, | |
1257 | failrec->start, | |
1258 | EXTENT_LOCKED); | |
1259 | if (state && state->start != failrec->start) | |
1260 | state = NULL; | |
1261 | spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock); | |
1262 | } | |
1263 | if (!state || failrec->last_mirror > num_copies) { | |
1264 | set_state_private(failure_tree, failrec->start, 0); | |
1265 | clear_extent_bits(failure_tree, failrec->start, | |
1266 | failrec->start + failrec->len - 1, | |
1267 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); | |
1268 | kfree(failrec); | |
1269 | return -EIO; | |
1270 | } | |
1271 | bio = bio_alloc(GFP_NOFS, 1); | |
1272 | bio->bi_private = state; | |
1273 | bio->bi_end_io = failed_bio->bi_end_io; | |
1274 | bio->bi_sector = failrec->logical >> 9; | |
1275 | bio->bi_bdev = failed_bio->bi_bdev; | |
e1c4b745 | 1276 | bio->bi_size = 0; |
7e38326f | 1277 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
1259ab75 CM |
1278 | if (failed_bio->bi_rw & (1 << BIO_RW)) |
1279 | rw = WRITE; | |
1280 | else | |
1281 | rw = READ; | |
1282 | ||
1283 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, | |
c8b97818 CM |
1284 | failrec->last_mirror, |
1285 | bio_flags); | |
1259ab75 CM |
1286 | return 0; |
1287 | } | |
1288 | ||
d352ac68 CM |
1289 | /* |
1290 | * each time an IO finishes, we do a fast check in the IO failure tree | |
1291 | * to see if we need to process or clean up an io_failure_record | |
1292 | */ | |
1259ab75 CM |
1293 | int btrfs_clean_io_failures(struct inode *inode, u64 start) |
1294 | { | |
1295 | u64 private; | |
1296 | u64 private_failure; | |
1297 | struct io_failure_record *failure; | |
1298 | int ret; | |
1299 | ||
1300 | private = 0; | |
1301 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, | |
1302 | (u64)-1, 1, EXTENT_DIRTY)) { | |
1303 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, | |
1304 | start, &private_failure); | |
1305 | if (ret == 0) { | |
1306 | failure = (struct io_failure_record *)(unsigned long) | |
1307 | private_failure; | |
1308 | set_state_private(&BTRFS_I(inode)->io_failure_tree, | |
1309 | failure->start, 0); | |
1310 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, | |
1311 | failure->start, | |
1312 | failure->start + failure->len - 1, | |
1313 | EXTENT_DIRTY | EXTENT_LOCKED, | |
1314 | GFP_NOFS); | |
1315 | kfree(failure); | |
1316 | } | |
1317 | } | |
7e38326f CM |
1318 | return 0; |
1319 | } | |
1320 | ||
d352ac68 CM |
1321 | /* |
1322 | * when reads are done, we need to check csums to verify the data is correct | |
1323 | * if there's a match, we allow the bio to finish. If not, we go through | |
1324 | * the io_failure_record routines to find good copies | |
1325 | */ | |
70dec807 CM |
1326 | int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
1327 | struct extent_state *state) | |
07157aac | 1328 | { |
35ebb934 | 1329 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
07157aac | 1330 | struct inode *inode = page->mapping->host; |
d1310b2e | 1331 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
07157aac | 1332 | char *kaddr; |
aadfeb6e | 1333 | u64 private = ~(u32)0; |
07157aac | 1334 | int ret; |
ff79f819 CM |
1335 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1336 | u32 csum = ~(u32)0; | |
bbf0d006 | 1337 | unsigned long flags; |
d1310b2e | 1338 | |
b98b6767 Y |
1339 | if (btrfs_test_opt(root, NODATASUM) || |
1340 | btrfs_test_flag(inode, NODATASUM)) | |
b6cda9bc | 1341 | return 0; |
c2e639f0 | 1342 | if (state && state->start == start) { |
70dec807 CM |
1343 | private = state->private; |
1344 | ret = 0; | |
1345 | } else { | |
1346 | ret = get_state_private(io_tree, start, &private); | |
1347 | } | |
bbf0d006 | 1348 | local_irq_save(flags); |
07157aac CM |
1349 | kaddr = kmap_atomic(page, KM_IRQ0); |
1350 | if (ret) { | |
1351 | goto zeroit; | |
1352 | } | |
ff79f819 CM |
1353 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
1354 | btrfs_csum_final(csum, (char *)&csum); | |
1355 | if (csum != private) { | |
07157aac CM |
1356 | goto zeroit; |
1357 | } | |
1358 | kunmap_atomic(kaddr, KM_IRQ0); | |
bbf0d006 | 1359 | local_irq_restore(flags); |
7e38326f CM |
1360 | |
1361 | /* if the io failure tree for this inode is non-empty, | |
1362 | * check to see if we've recovered from a failed IO | |
1363 | */ | |
1259ab75 | 1364 | btrfs_clean_io_failures(inode, start); |
07157aac CM |
1365 | return 0; |
1366 | ||
1367 | zeroit: | |
aadfeb6e CM |
1368 | printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n", |
1369 | page->mapping->host->i_ino, (unsigned long long)start, csum, | |
1370 | private); | |
db94535d CM |
1371 | memset(kaddr + offset, 1, end - start + 1); |
1372 | flush_dcache_page(page); | |
07157aac | 1373 | kunmap_atomic(kaddr, KM_IRQ0); |
bbf0d006 | 1374 | local_irq_restore(flags); |
3b951516 CM |
1375 | if (private == 0) |
1376 | return 0; | |
7e38326f | 1377 | return -EIO; |
07157aac | 1378 | } |
b888db2b | 1379 | |
7b128766 JB |
1380 | /* |
1381 | * This creates an orphan entry for the given inode in case something goes | |
1382 | * wrong in the middle of an unlink/truncate. | |
1383 | */ | |
1384 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
1385 | { | |
1386 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1387 | int ret = 0; | |
1388 | ||
bcc63abb | 1389 | spin_lock(&root->list_lock); |
7b128766 JB |
1390 | |
1391 | /* already on the orphan list, we're good */ | |
1392 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { | |
bcc63abb | 1393 | spin_unlock(&root->list_lock); |
7b128766 JB |
1394 | return 0; |
1395 | } | |
1396 | ||
1397 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); | |
1398 | ||
bcc63abb | 1399 | spin_unlock(&root->list_lock); |
7b128766 JB |
1400 | |
1401 | /* | |
1402 | * insert an orphan item to track this unlinked/truncated file | |
1403 | */ | |
1404 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); | |
1405 | ||
1406 | return ret; | |
1407 | } | |
1408 | ||
1409 | /* | |
1410 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
1411 | * item for this particular inode. | |
1412 | */ | |
1413 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) | |
1414 | { | |
1415 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1416 | int ret = 0; | |
1417 | ||
bcc63abb | 1418 | spin_lock(&root->list_lock); |
7b128766 JB |
1419 | |
1420 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { | |
bcc63abb | 1421 | spin_unlock(&root->list_lock); |
7b128766 JB |
1422 | return 0; |
1423 | } | |
1424 | ||
1425 | list_del_init(&BTRFS_I(inode)->i_orphan); | |
1426 | if (!trans) { | |
bcc63abb | 1427 | spin_unlock(&root->list_lock); |
7b128766 JB |
1428 | return 0; |
1429 | } | |
1430 | ||
bcc63abb | 1431 | spin_unlock(&root->list_lock); |
7b128766 JB |
1432 | |
1433 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); | |
1434 | ||
1435 | return ret; | |
1436 | } | |
1437 | ||
1438 | /* | |
1439 | * this cleans up any orphans that may be left on the list from the last use | |
1440 | * of this root. | |
1441 | */ | |
1442 | void btrfs_orphan_cleanup(struct btrfs_root *root) | |
1443 | { | |
1444 | struct btrfs_path *path; | |
1445 | struct extent_buffer *leaf; | |
1446 | struct btrfs_item *item; | |
1447 | struct btrfs_key key, found_key; | |
1448 | struct btrfs_trans_handle *trans; | |
1449 | struct inode *inode; | |
1450 | int ret = 0, nr_unlink = 0, nr_truncate = 0; | |
1451 | ||
1452 | /* don't do orphan cleanup if the fs is readonly. */ | |
5b21f2ed | 1453 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
7b128766 JB |
1454 | return; |
1455 | ||
1456 | path = btrfs_alloc_path(); | |
1457 | if (!path) | |
1458 | return; | |
1459 | path->reada = -1; | |
1460 | ||
1461 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
1462 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); | |
1463 | key.offset = (u64)-1; | |
1464 | ||
7b128766 JB |
1465 | |
1466 | while (1) { | |
1467 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1468 | if (ret < 0) { | |
1469 | printk(KERN_ERR "Error searching slot for orphan: %d" | |
1470 | "\n", ret); | |
1471 | break; | |
1472 | } | |
1473 | ||
1474 | /* | |
1475 | * if ret == 0 means we found what we were searching for, which | |
1476 | * is weird, but possible, so only screw with path if we didnt | |
1477 | * find the key and see if we have stuff that matches | |
1478 | */ | |
1479 | if (ret > 0) { | |
1480 | if (path->slots[0] == 0) | |
1481 | break; | |
1482 | path->slots[0]--; | |
1483 | } | |
1484 | ||
1485 | /* pull out the item */ | |
1486 | leaf = path->nodes[0]; | |
1487 | item = btrfs_item_nr(leaf, path->slots[0]); | |
1488 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1489 | ||
1490 | /* make sure the item matches what we want */ | |
1491 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
1492 | break; | |
1493 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) | |
1494 | break; | |
1495 | ||
1496 | /* release the path since we're done with it */ | |
1497 | btrfs_release_path(root, path); | |
1498 | ||
1499 | /* | |
1500 | * this is where we are basically btrfs_lookup, without the | |
1501 | * crossing root thing. we store the inode number in the | |
1502 | * offset of the orphan item. | |
1503 | */ | |
5b21f2ed | 1504 | inode = btrfs_iget_locked(root->fs_info->sb, |
7b128766 JB |
1505 | found_key.offset, root); |
1506 | if (!inode) | |
1507 | break; | |
1508 | ||
1509 | if (inode->i_state & I_NEW) { | |
1510 | BTRFS_I(inode)->root = root; | |
1511 | ||
1512 | /* have to set the location manually */ | |
1513 | BTRFS_I(inode)->location.objectid = inode->i_ino; | |
1514 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
1515 | BTRFS_I(inode)->location.offset = 0; | |
1516 | ||
1517 | btrfs_read_locked_inode(inode); | |
1518 | unlock_new_inode(inode); | |
1519 | } | |
1520 | ||
1521 | /* | |
1522 | * add this inode to the orphan list so btrfs_orphan_del does | |
1523 | * the proper thing when we hit it | |
1524 | */ | |
bcc63abb | 1525 | spin_lock(&root->list_lock); |
7b128766 | 1526 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
bcc63abb | 1527 | spin_unlock(&root->list_lock); |
7b128766 JB |
1528 | |
1529 | /* | |
1530 | * if this is a bad inode, means we actually succeeded in | |
1531 | * removing the inode, but not the orphan record, which means | |
1532 | * we need to manually delete the orphan since iput will just | |
1533 | * do a destroy_inode | |
1534 | */ | |
1535 | if (is_bad_inode(inode)) { | |
5b21f2ed | 1536 | trans = btrfs_start_transaction(root, 1); |
7b128766 | 1537 | btrfs_orphan_del(trans, inode); |
5b21f2ed | 1538 | btrfs_end_transaction(trans, root); |
7b128766 JB |
1539 | iput(inode); |
1540 | continue; | |
1541 | } | |
1542 | ||
1543 | /* if we have links, this was a truncate, lets do that */ | |
1544 | if (inode->i_nlink) { | |
1545 | nr_truncate++; | |
1546 | btrfs_truncate(inode); | |
1547 | } else { | |
1548 | nr_unlink++; | |
1549 | } | |
1550 | ||
1551 | /* this will do delete_inode and everything for us */ | |
1552 | iput(inode); | |
1553 | } | |
1554 | ||
1555 | if (nr_unlink) | |
1556 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); | |
1557 | if (nr_truncate) | |
1558 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); | |
1559 | ||
1560 | btrfs_free_path(path); | |
7b128766 JB |
1561 | } |
1562 | ||
d352ac68 CM |
1563 | /* |
1564 | * read an inode from the btree into the in-memory inode | |
1565 | */ | |
39279cc3 CM |
1566 | void btrfs_read_locked_inode(struct inode *inode) |
1567 | { | |
1568 | struct btrfs_path *path; | |
5f39d397 | 1569 | struct extent_buffer *leaf; |
39279cc3 | 1570 | struct btrfs_inode_item *inode_item; |
0b86a832 | 1571 | struct btrfs_timespec *tspec; |
39279cc3 CM |
1572 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1573 | struct btrfs_key location; | |
1574 | u64 alloc_group_block; | |
618e21d5 | 1575 | u32 rdev; |
39279cc3 CM |
1576 | int ret; |
1577 | ||
1578 | path = btrfs_alloc_path(); | |
1579 | BUG_ON(!path); | |
39279cc3 | 1580 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 1581 | |
39279cc3 | 1582 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
5f39d397 | 1583 | if (ret) |
39279cc3 | 1584 | goto make_bad; |
39279cc3 | 1585 | |
5f39d397 CM |
1586 | leaf = path->nodes[0]; |
1587 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
1588 | struct btrfs_inode_item); | |
1589 | ||
1590 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); | |
1591 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); | |
1592 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); | |
1593 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); | |
dbe674a9 | 1594 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 CM |
1595 | |
1596 | tspec = btrfs_inode_atime(inode_item); | |
1597 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
1598 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
1599 | ||
1600 | tspec = btrfs_inode_mtime(inode_item); | |
1601 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
1602 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
1603 | ||
1604 | tspec = btrfs_inode_ctime(inode_item); | |
1605 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
1606 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
1607 | ||
a76a3cd4 | 1608 | inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); |
e02119d5 CM |
1609 | BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); |
1610 | inode->i_generation = BTRFS_I(inode)->generation; | |
618e21d5 | 1611 | inode->i_rdev = 0; |
5f39d397 CM |
1612 | rdev = btrfs_inode_rdev(leaf, inode_item); |
1613 | ||
aec7477b JB |
1614 | BTRFS_I(inode)->index_cnt = (u64)-1; |
1615 | ||
5f39d397 | 1616 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
39279cc3 CM |
1617 | BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, |
1618 | alloc_group_block); | |
b98b6767 | 1619 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
e52ec0eb CM |
1620 | if (!BTRFS_I(inode)->block_group) { |
1621 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, | |
0b86a832 CM |
1622 | NULL, 0, |
1623 | BTRFS_BLOCK_GROUP_METADATA, 0); | |
e52ec0eb | 1624 | } |
39279cc3 CM |
1625 | btrfs_free_path(path); |
1626 | inode_item = NULL; | |
1627 | ||
39279cc3 | 1628 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
1629 | case S_IFREG: |
1630 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 1631 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
d1310b2e | 1632 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
1633 | inode->i_fop = &btrfs_file_operations; |
1634 | inode->i_op = &btrfs_file_inode_operations; | |
1635 | break; | |
1636 | case S_IFDIR: | |
1637 | inode->i_fop = &btrfs_dir_file_operations; | |
1638 | if (root == root->fs_info->tree_root) | |
1639 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
1640 | else | |
1641 | inode->i_op = &btrfs_dir_inode_operations; | |
1642 | break; | |
1643 | case S_IFLNK: | |
1644 | inode->i_op = &btrfs_symlink_inode_operations; | |
1645 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
04160088 | 1646 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 | 1647 | break; |
618e21d5 JB |
1648 | default: |
1649 | init_special_inode(inode, inode->i_mode, rdev); | |
1650 | break; | |
39279cc3 CM |
1651 | } |
1652 | return; | |
1653 | ||
1654 | make_bad: | |
39279cc3 | 1655 | btrfs_free_path(path); |
39279cc3 CM |
1656 | make_bad_inode(inode); |
1657 | } | |
1658 | ||
d352ac68 CM |
1659 | /* |
1660 | * given a leaf and an inode, copy the inode fields into the leaf | |
1661 | */ | |
e02119d5 CM |
1662 | static void fill_inode_item(struct btrfs_trans_handle *trans, |
1663 | struct extent_buffer *leaf, | |
5f39d397 | 1664 | struct btrfs_inode_item *item, |
39279cc3 CM |
1665 | struct inode *inode) |
1666 | { | |
5f39d397 CM |
1667 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
1668 | btrfs_set_inode_gid(leaf, item, inode->i_gid); | |
dbe674a9 | 1669 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
5f39d397 CM |
1670 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
1671 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); | |
1672 | ||
1673 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), | |
1674 | inode->i_atime.tv_sec); | |
1675 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), | |
1676 | inode->i_atime.tv_nsec); | |
1677 | ||
1678 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), | |
1679 | inode->i_mtime.tv_sec); | |
1680 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), | |
1681 | inode->i_mtime.tv_nsec); | |
1682 | ||
1683 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), | |
1684 | inode->i_ctime.tv_sec); | |
1685 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), | |
1686 | inode->i_ctime.tv_nsec); | |
1687 | ||
a76a3cd4 | 1688 | btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode)); |
e02119d5 CM |
1689 | btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation); |
1690 | btrfs_set_inode_transid(leaf, item, trans->transid); | |
5f39d397 | 1691 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); |
b98b6767 | 1692 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
5f39d397 | 1693 | btrfs_set_inode_block_group(leaf, item, |
39279cc3 CM |
1694 | BTRFS_I(inode)->block_group->key.objectid); |
1695 | } | |
1696 | ||
d352ac68 CM |
1697 | /* |
1698 | * copy everything in the in-memory inode into the btree. | |
1699 | */ | |
ba1da2f4 | 1700 | int noinline btrfs_update_inode(struct btrfs_trans_handle *trans, |
39279cc3 CM |
1701 | struct btrfs_root *root, |
1702 | struct inode *inode) | |
1703 | { | |
1704 | struct btrfs_inode_item *inode_item; | |
1705 | struct btrfs_path *path; | |
5f39d397 | 1706 | struct extent_buffer *leaf; |
39279cc3 CM |
1707 | int ret; |
1708 | ||
1709 | path = btrfs_alloc_path(); | |
1710 | BUG_ON(!path); | |
39279cc3 CM |
1711 | ret = btrfs_lookup_inode(trans, root, path, |
1712 | &BTRFS_I(inode)->location, 1); | |
1713 | if (ret) { | |
1714 | if (ret > 0) | |
1715 | ret = -ENOENT; | |
1716 | goto failed; | |
1717 | } | |
1718 | ||
5f39d397 CM |
1719 | leaf = path->nodes[0]; |
1720 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
39279cc3 CM |
1721 | struct btrfs_inode_item); |
1722 | ||
e02119d5 | 1723 | fill_inode_item(trans, leaf, inode_item, inode); |
5f39d397 | 1724 | btrfs_mark_buffer_dirty(leaf); |
15ee9bc7 | 1725 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
1726 | ret = 0; |
1727 | failed: | |
39279cc3 CM |
1728 | btrfs_free_path(path); |
1729 | return ret; | |
1730 | } | |
1731 | ||
1732 | ||
d352ac68 CM |
1733 | /* |
1734 | * unlink helper that gets used here in inode.c and in the tree logging | |
1735 | * recovery code. It remove a link in a directory with a given name, and | |
1736 | * also drops the back refs in the inode to the directory | |
1737 | */ | |
e02119d5 CM |
1738 | int btrfs_unlink_inode(struct btrfs_trans_handle *trans, |
1739 | struct btrfs_root *root, | |
1740 | struct inode *dir, struct inode *inode, | |
1741 | const char *name, int name_len) | |
39279cc3 CM |
1742 | { |
1743 | struct btrfs_path *path; | |
39279cc3 | 1744 | int ret = 0; |
5f39d397 | 1745 | struct extent_buffer *leaf; |
39279cc3 | 1746 | struct btrfs_dir_item *di; |
5f39d397 | 1747 | struct btrfs_key key; |
aec7477b | 1748 | u64 index; |
39279cc3 CM |
1749 | |
1750 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
1751 | if (!path) { |
1752 | ret = -ENOMEM; | |
1753 | goto err; | |
1754 | } | |
1755 | ||
39279cc3 CM |
1756 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
1757 | name, name_len, -1); | |
1758 | if (IS_ERR(di)) { | |
1759 | ret = PTR_ERR(di); | |
1760 | goto err; | |
1761 | } | |
1762 | if (!di) { | |
1763 | ret = -ENOENT; | |
1764 | goto err; | |
1765 | } | |
5f39d397 CM |
1766 | leaf = path->nodes[0]; |
1767 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 1768 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
1769 | if (ret) |
1770 | goto err; | |
39279cc3 CM |
1771 | btrfs_release_path(root, path); |
1772 | ||
aec7477b | 1773 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
e02119d5 CM |
1774 | inode->i_ino, |
1775 | dir->i_ino, &index); | |
aec7477b JB |
1776 | if (ret) { |
1777 | printk("failed to delete reference to %.*s, " | |
1778 | "inode %lu parent %lu\n", name_len, name, | |
e02119d5 | 1779 | inode->i_ino, dir->i_ino); |
aec7477b JB |
1780 | goto err; |
1781 | } | |
1782 | ||
39279cc3 | 1783 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
aec7477b | 1784 | index, name, name_len, -1); |
39279cc3 CM |
1785 | if (IS_ERR(di)) { |
1786 | ret = PTR_ERR(di); | |
1787 | goto err; | |
1788 | } | |
1789 | if (!di) { | |
1790 | ret = -ENOENT; | |
1791 | goto err; | |
1792 | } | |
1793 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
925baedd | 1794 | btrfs_release_path(root, path); |
39279cc3 | 1795 | |
e02119d5 CM |
1796 | ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, |
1797 | inode, dir->i_ino); | |
49eb7e46 CM |
1798 | BUG_ON(ret != 0 && ret != -ENOENT); |
1799 | if (ret != -ENOENT) | |
1800 | BTRFS_I(dir)->log_dirty_trans = trans->transid; | |
e02119d5 CM |
1801 | |
1802 | ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, | |
1803 | dir, index); | |
1804 | BUG_ON(ret); | |
39279cc3 CM |
1805 | err: |
1806 | btrfs_free_path(path); | |
e02119d5 CM |
1807 | if (ret) |
1808 | goto out; | |
1809 | ||
1810 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | |
1811 | inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; | |
1812 | btrfs_update_inode(trans, root, dir); | |
1813 | btrfs_drop_nlink(inode); | |
1814 | ret = btrfs_update_inode(trans, root, inode); | |
1815 | dir->i_sb->s_dirt = 1; | |
1816 | out: | |
39279cc3 CM |
1817 | return ret; |
1818 | } | |
1819 | ||
1820 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
1821 | { | |
1822 | struct btrfs_root *root; | |
1823 | struct btrfs_trans_handle *trans; | |
7b128766 | 1824 | struct inode *inode = dentry->d_inode; |
39279cc3 | 1825 | int ret; |
1832a6d5 | 1826 | unsigned long nr = 0; |
39279cc3 CM |
1827 | |
1828 | root = BTRFS_I(dir)->root; | |
1832a6d5 CM |
1829 | |
1830 | ret = btrfs_check_free_space(root, 1, 1); | |
1831 | if (ret) | |
1832 | goto fail; | |
1833 | ||
39279cc3 | 1834 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 1835 | |
39279cc3 | 1836 | btrfs_set_trans_block_group(trans, dir); |
e02119d5 CM |
1837 | ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
1838 | dentry->d_name.name, dentry->d_name.len); | |
7b128766 JB |
1839 | |
1840 | if (inode->i_nlink == 0) | |
1841 | ret = btrfs_orphan_add(trans, inode); | |
1842 | ||
d3c2fdcf | 1843 | nr = trans->blocks_used; |
5f39d397 | 1844 | |
89ce8a63 | 1845 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 1846 | fail: |
d3c2fdcf | 1847 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
1848 | return ret; |
1849 | } | |
1850 | ||
1851 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |
1852 | { | |
1853 | struct inode *inode = dentry->d_inode; | |
1832a6d5 | 1854 | int err = 0; |
39279cc3 CM |
1855 | int ret; |
1856 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
39279cc3 | 1857 | struct btrfs_trans_handle *trans; |
1832a6d5 | 1858 | unsigned long nr = 0; |
39279cc3 | 1859 | |
925baedd | 1860 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) { |
134d4512 | 1861 | return -ENOTEMPTY; |
925baedd | 1862 | } |
134d4512 | 1863 | |
1832a6d5 CM |
1864 | ret = btrfs_check_free_space(root, 1, 1); |
1865 | if (ret) | |
1866 | goto fail; | |
1867 | ||
39279cc3 CM |
1868 | trans = btrfs_start_transaction(root, 1); |
1869 | btrfs_set_trans_block_group(trans, dir); | |
39279cc3 | 1870 | |
7b128766 JB |
1871 | err = btrfs_orphan_add(trans, inode); |
1872 | if (err) | |
1873 | goto fail_trans; | |
1874 | ||
39279cc3 | 1875 | /* now the directory is empty */ |
e02119d5 CM |
1876 | err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, |
1877 | dentry->d_name.name, dentry->d_name.len); | |
39279cc3 | 1878 | if (!err) { |
dbe674a9 | 1879 | btrfs_i_size_write(inode, 0); |
39279cc3 | 1880 | } |
3954401f | 1881 | |
7b128766 | 1882 | fail_trans: |
d3c2fdcf | 1883 | nr = trans->blocks_used; |
89ce8a63 | 1884 | ret = btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 1885 | fail: |
d3c2fdcf | 1886 | btrfs_btree_balance_dirty(root, nr); |
3954401f | 1887 | |
39279cc3 CM |
1888 | if (ret && !err) |
1889 | err = ret; | |
1890 | return err; | |
1891 | } | |
1892 | ||
323ac95b CM |
1893 | /* |
1894 | * when truncating bytes in a file, it is possible to avoid reading | |
1895 | * the leaves that contain only checksum items. This can be the | |
1896 | * majority of the IO required to delete a large file, but it must | |
1897 | * be done carefully. | |
1898 | * | |
1899 | * The keys in the level just above the leaves are checked to make sure | |
1900 | * the lowest key in a given leaf is a csum key, and starts at an offset | |
1901 | * after the new size. | |
1902 | * | |
1903 | * Then the key for the next leaf is checked to make sure it also has | |
1904 | * a checksum item for the same file. If it does, we know our target leaf | |
1905 | * contains only checksum items, and it can be safely freed without reading | |
1906 | * it. | |
1907 | * | |
1908 | * This is just an optimization targeted at large files. It may do | |
1909 | * nothing. It will return 0 unless things went badly. | |
1910 | */ | |
1911 | static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans, | |
1912 | struct btrfs_root *root, | |
1913 | struct btrfs_path *path, | |
1914 | struct inode *inode, u64 new_size) | |
1915 | { | |
1916 | struct btrfs_key key; | |
1917 | int ret; | |
1918 | int nritems; | |
1919 | struct btrfs_key found_key; | |
1920 | struct btrfs_key other_key; | |
5b84e8d6 YZ |
1921 | struct btrfs_leaf_ref *ref; |
1922 | u64 leaf_gen; | |
1923 | u64 leaf_start; | |
323ac95b CM |
1924 | |
1925 | path->lowest_level = 1; | |
1926 | key.objectid = inode->i_ino; | |
1927 | key.type = BTRFS_CSUM_ITEM_KEY; | |
1928 | key.offset = new_size; | |
1929 | again: | |
1930 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1931 | if (ret < 0) | |
1932 | goto out; | |
1933 | ||
1934 | if (path->nodes[1] == NULL) { | |
1935 | ret = 0; | |
1936 | goto out; | |
1937 | } | |
1938 | ret = 0; | |
1939 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]); | |
1940 | nritems = btrfs_header_nritems(path->nodes[1]); | |
1941 | ||
1942 | if (!nritems) | |
1943 | goto out; | |
1944 | ||
1945 | if (path->slots[1] >= nritems) | |
1946 | goto next_node; | |
1947 | ||
1948 | /* did we find a key greater than anything we want to delete? */ | |
1949 | if (found_key.objectid > inode->i_ino || | |
1950 | (found_key.objectid == inode->i_ino && found_key.type > key.type)) | |
1951 | goto out; | |
1952 | ||
1953 | /* we check the next key in the node to make sure the leave contains | |
1954 | * only checksum items. This comparison doesn't work if our | |
1955 | * leaf is the last one in the node | |
1956 | */ | |
1957 | if (path->slots[1] + 1 >= nritems) { | |
1958 | next_node: | |
1959 | /* search forward from the last key in the node, this | |
1960 | * will bring us into the next node in the tree | |
1961 | */ | |
1962 | btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1); | |
1963 | ||
1964 | /* unlikely, but we inc below, so check to be safe */ | |
1965 | if (found_key.offset == (u64)-1) | |
1966 | goto out; | |
1967 | ||
1968 | /* search_forward needs a path with locks held, do the | |
1969 | * search again for the original key. It is possible | |
1970 | * this will race with a balance and return a path that | |
1971 | * we could modify, but this drop is just an optimization | |
1972 | * and is allowed to miss some leaves. | |
1973 | */ | |
1974 | btrfs_release_path(root, path); | |
1975 | found_key.offset++; | |
1976 | ||
1977 | /* setup a max key for search_forward */ | |
1978 | other_key.offset = (u64)-1; | |
1979 | other_key.type = key.type; | |
1980 | other_key.objectid = key.objectid; | |
1981 | ||
1982 | path->keep_locks = 1; | |
1983 | ret = btrfs_search_forward(root, &found_key, &other_key, | |
1984 | path, 0, 0); | |
1985 | path->keep_locks = 0; | |
1986 | if (ret || found_key.objectid != key.objectid || | |
1987 | found_key.type != key.type) { | |
1988 | ret = 0; | |
1989 | goto out; | |
1990 | } | |
1991 | ||
1992 | key.offset = found_key.offset; | |
1993 | btrfs_release_path(root, path); | |
1994 | cond_resched(); | |
1995 | goto again; | |
1996 | } | |
1997 | ||
1998 | /* we know there's one more slot after us in the tree, | |
1999 | * read that key so we can verify it is also a checksum item | |
2000 | */ | |
2001 | btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1); | |
2002 | ||
2003 | if (found_key.objectid < inode->i_ino) | |
2004 | goto next_key; | |
2005 | ||
2006 | if (found_key.type != key.type || found_key.offset < new_size) | |
2007 | goto next_key; | |
2008 | ||
2009 | /* | |
2010 | * if the key for the next leaf isn't a csum key from this objectid, | |
2011 | * we can't be sure there aren't good items inside this leaf. | |
2012 | * Bail out | |
2013 | */ | |
2014 | if (other_key.objectid != inode->i_ino || other_key.type != key.type) | |
2015 | goto out; | |
2016 | ||
5b84e8d6 YZ |
2017 | leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]); |
2018 | leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]); | |
323ac95b CM |
2019 | /* |
2020 | * it is safe to delete this leaf, it contains only | |
2021 | * csum items from this inode at an offset >= new_size | |
2022 | */ | |
5b84e8d6 | 2023 | ret = btrfs_del_leaf(trans, root, path, leaf_start); |
323ac95b CM |
2024 | BUG_ON(ret); |
2025 | ||
5b84e8d6 YZ |
2026 | if (root->ref_cows && leaf_gen < trans->transid) { |
2027 | ref = btrfs_alloc_leaf_ref(root, 0); | |
2028 | if (ref) { | |
2029 | ref->root_gen = root->root_key.offset; | |
2030 | ref->bytenr = leaf_start; | |
2031 | ref->owner = 0; | |
2032 | ref->generation = leaf_gen; | |
2033 | ref->nritems = 0; | |
2034 | ||
2035 | ret = btrfs_add_leaf_ref(root, ref, 0); | |
2036 | WARN_ON(ret); | |
2037 | btrfs_free_leaf_ref(root, ref); | |
2038 | } else { | |
2039 | WARN_ON(1); | |
2040 | } | |
2041 | } | |
323ac95b CM |
2042 | next_key: |
2043 | btrfs_release_path(root, path); | |
2044 | ||
2045 | if (other_key.objectid == inode->i_ino && | |
2046 | other_key.type == key.type && other_key.offset > key.offset) { | |
2047 | key.offset = other_key.offset; | |
2048 | cond_resched(); | |
2049 | goto again; | |
2050 | } | |
2051 | ret = 0; | |
2052 | out: | |
2053 | /* fixup any changes we've made to the path */ | |
2054 | path->lowest_level = 0; | |
2055 | path->keep_locks = 0; | |
2056 | btrfs_release_path(root, path); | |
2057 | return ret; | |
2058 | } | |
2059 | ||
39279cc3 CM |
2060 | /* |
2061 | * this can truncate away extent items, csum items and directory items. | |
2062 | * It starts at a high offset and removes keys until it can't find | |
d352ac68 | 2063 | * any higher than new_size |
39279cc3 CM |
2064 | * |
2065 | * csum items that cross the new i_size are truncated to the new size | |
2066 | * as well. | |
7b128766 JB |
2067 | * |
2068 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
2069 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 | 2070 | */ |
e02119d5 CM |
2071 | noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, |
2072 | struct btrfs_root *root, | |
2073 | struct inode *inode, | |
2074 | u64 new_size, u32 min_type) | |
39279cc3 CM |
2075 | { |
2076 | int ret; | |
2077 | struct btrfs_path *path; | |
2078 | struct btrfs_key key; | |
5f39d397 | 2079 | struct btrfs_key found_key; |
39279cc3 | 2080 | u32 found_type; |
5f39d397 | 2081 | struct extent_buffer *leaf; |
39279cc3 CM |
2082 | struct btrfs_file_extent_item *fi; |
2083 | u64 extent_start = 0; | |
db94535d | 2084 | u64 extent_num_bytes = 0; |
39279cc3 | 2085 | u64 item_end = 0; |
7bb86316 | 2086 | u64 root_gen = 0; |
d8d5f3e1 | 2087 | u64 root_owner = 0; |
39279cc3 CM |
2088 | int found_extent; |
2089 | int del_item; | |
85e21bac CM |
2090 | int pending_del_nr = 0; |
2091 | int pending_del_slot = 0; | |
179e29e4 | 2092 | int extent_type = -1; |
3b951516 | 2093 | u64 mask = root->sectorsize - 1; |
39279cc3 | 2094 | |
e02119d5 | 2095 | if (root->ref_cows) |
5b21f2ed | 2096 | btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); |
39279cc3 | 2097 | path = btrfs_alloc_path(); |
3c69faec | 2098 | path->reada = -1; |
39279cc3 | 2099 | BUG_ON(!path); |
5f39d397 | 2100 | |
39279cc3 CM |
2101 | /* FIXME, add redo link to tree so we don't leak on crash */ |
2102 | key.objectid = inode->i_ino; | |
2103 | key.offset = (u64)-1; | |
5f39d397 CM |
2104 | key.type = (u8)-1; |
2105 | ||
85e21bac | 2106 | btrfs_init_path(path); |
323ac95b CM |
2107 | |
2108 | ret = drop_csum_leaves(trans, root, path, inode, new_size); | |
2109 | BUG_ON(ret); | |
2110 | ||
85e21bac CM |
2111 | search_again: |
2112 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
2113 | if (ret < 0) { | |
2114 | goto error; | |
2115 | } | |
2116 | if (ret > 0) { | |
e02119d5 CM |
2117 | /* there are no items in the tree for us to truncate, we're |
2118 | * done | |
2119 | */ | |
2120 | if (path->slots[0] == 0) { | |
2121 | ret = 0; | |
2122 | goto error; | |
2123 | } | |
85e21bac CM |
2124 | path->slots[0]--; |
2125 | } | |
2126 | ||
39279cc3 | 2127 | while(1) { |
39279cc3 | 2128 | fi = NULL; |
5f39d397 CM |
2129 | leaf = path->nodes[0]; |
2130 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2131 | found_type = btrfs_key_type(&found_key); | |
39279cc3 | 2132 | |
5f39d397 | 2133 | if (found_key.objectid != inode->i_ino) |
39279cc3 | 2134 | break; |
5f39d397 | 2135 | |
85e21bac | 2136 | if (found_type < min_type) |
39279cc3 CM |
2137 | break; |
2138 | ||
5f39d397 | 2139 | item_end = found_key.offset; |
39279cc3 | 2140 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 2141 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 2142 | struct btrfs_file_extent_item); |
179e29e4 CM |
2143 | extent_type = btrfs_file_extent_type(leaf, fi); |
2144 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 2145 | item_end += |
db94535d | 2146 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 | 2147 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
179e29e4 | 2148 | item_end += btrfs_file_extent_inline_len(leaf, |
c8b97818 | 2149 | fi); |
39279cc3 | 2150 | } |
008630c1 | 2151 | item_end--; |
39279cc3 CM |
2152 | } |
2153 | if (found_type == BTRFS_CSUM_ITEM_KEY) { | |
2154 | ret = btrfs_csum_truncate(trans, root, path, | |
e02119d5 | 2155 | new_size); |
39279cc3 CM |
2156 | BUG_ON(ret); |
2157 | } | |
e02119d5 | 2158 | if (item_end < new_size) { |
b888db2b CM |
2159 | if (found_type == BTRFS_DIR_ITEM_KEY) { |
2160 | found_type = BTRFS_INODE_ITEM_KEY; | |
2161 | } else if (found_type == BTRFS_EXTENT_ITEM_KEY) { | |
2162 | found_type = BTRFS_CSUM_ITEM_KEY; | |
85e21bac CM |
2163 | } else if (found_type == BTRFS_EXTENT_DATA_KEY) { |
2164 | found_type = BTRFS_XATTR_ITEM_KEY; | |
2165 | } else if (found_type == BTRFS_XATTR_ITEM_KEY) { | |
2166 | found_type = BTRFS_INODE_REF_KEY; | |
b888db2b CM |
2167 | } else if (found_type) { |
2168 | found_type--; | |
2169 | } else { | |
2170 | break; | |
39279cc3 | 2171 | } |
a61721d5 | 2172 | btrfs_set_key_type(&key, found_type); |
85e21bac | 2173 | goto next; |
39279cc3 | 2174 | } |
e02119d5 | 2175 | if (found_key.offset >= new_size) |
39279cc3 CM |
2176 | del_item = 1; |
2177 | else | |
2178 | del_item = 0; | |
2179 | found_extent = 0; | |
2180 | ||
2181 | /* FIXME, shrink the extent if the ref count is only 1 */ | |
179e29e4 CM |
2182 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
2183 | goto delete; | |
2184 | ||
2185 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 2186 | u64 num_dec; |
db94535d | 2187 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
39279cc3 | 2188 | if (!del_item) { |
db94535d CM |
2189 | u64 orig_num_bytes = |
2190 | btrfs_file_extent_num_bytes(leaf, fi); | |
e02119d5 | 2191 | extent_num_bytes = new_size - |
5f39d397 | 2192 | found_key.offset + root->sectorsize - 1; |
b1632b10 Y |
2193 | extent_num_bytes = extent_num_bytes & |
2194 | ~((u64)root->sectorsize - 1); | |
db94535d CM |
2195 | btrfs_set_file_extent_num_bytes(leaf, fi, |
2196 | extent_num_bytes); | |
2197 | num_dec = (orig_num_bytes - | |
9069218d | 2198 | extent_num_bytes); |
e02119d5 | 2199 | if (root->ref_cows && extent_start != 0) |
a76a3cd4 | 2200 | inode_sub_bytes(inode, num_dec); |
5f39d397 | 2201 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 2202 | } else { |
db94535d CM |
2203 | extent_num_bytes = |
2204 | btrfs_file_extent_disk_num_bytes(leaf, | |
2205 | fi); | |
39279cc3 | 2206 | /* FIXME blocksize != 4096 */ |
9069218d | 2207 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
2208 | if (extent_start != 0) { |
2209 | found_extent = 1; | |
e02119d5 | 2210 | if (root->ref_cows) |
a76a3cd4 | 2211 | inode_sub_bytes(inode, num_dec); |
e02119d5 | 2212 | } |
31840ae1 | 2213 | root_gen = btrfs_header_generation(leaf); |
d8d5f3e1 | 2214 | root_owner = btrfs_header_owner(leaf); |
39279cc3 | 2215 | } |
9069218d | 2216 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
c8b97818 CM |
2217 | /* |
2218 | * we can't truncate inline items that have had | |
2219 | * special encodings | |
2220 | */ | |
2221 | if (!del_item && | |
2222 | btrfs_file_extent_compression(leaf, fi) == 0 && | |
2223 | btrfs_file_extent_encryption(leaf, fi) == 0 && | |
2224 | btrfs_file_extent_other_encoding(leaf, fi) == 0) { | |
e02119d5 CM |
2225 | u32 size = new_size - found_key.offset; |
2226 | ||
2227 | if (root->ref_cows) { | |
a76a3cd4 YZ |
2228 | inode_sub_bytes(inode, item_end + 1 - |
2229 | new_size); | |
e02119d5 CM |
2230 | } |
2231 | size = | |
2232 | btrfs_file_extent_calc_inline_size(size); | |
9069218d | 2233 | ret = btrfs_truncate_item(trans, root, path, |
e02119d5 | 2234 | size, 1); |
9069218d | 2235 | BUG_ON(ret); |
e02119d5 | 2236 | } else if (root->ref_cows) { |
a76a3cd4 YZ |
2237 | inode_sub_bytes(inode, item_end + 1 - |
2238 | found_key.offset); | |
9069218d | 2239 | } |
39279cc3 | 2240 | } |
179e29e4 | 2241 | delete: |
39279cc3 | 2242 | if (del_item) { |
85e21bac CM |
2243 | if (!pending_del_nr) { |
2244 | /* no pending yet, add ourselves */ | |
2245 | pending_del_slot = path->slots[0]; | |
2246 | pending_del_nr = 1; | |
2247 | } else if (pending_del_nr && | |
2248 | path->slots[0] + 1 == pending_del_slot) { | |
2249 | /* hop on the pending chunk */ | |
2250 | pending_del_nr++; | |
2251 | pending_del_slot = path->slots[0]; | |
2252 | } else { | |
2253 | printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot); | |
2254 | } | |
39279cc3 CM |
2255 | } else { |
2256 | break; | |
2257 | } | |
39279cc3 CM |
2258 | if (found_extent) { |
2259 | ret = btrfs_free_extent(trans, root, extent_start, | |
7bb86316 | 2260 | extent_num_bytes, |
31840ae1 | 2261 | leaf->start, root_owner, |
3bb1a1bc | 2262 | root_gen, inode->i_ino, 0); |
39279cc3 CM |
2263 | BUG_ON(ret); |
2264 | } | |
85e21bac CM |
2265 | next: |
2266 | if (path->slots[0] == 0) { | |
2267 | if (pending_del_nr) | |
2268 | goto del_pending; | |
2269 | btrfs_release_path(root, path); | |
2270 | goto search_again; | |
2271 | } | |
2272 | ||
2273 | path->slots[0]--; | |
2274 | if (pending_del_nr && | |
2275 | path->slots[0] + 1 != pending_del_slot) { | |
2276 | struct btrfs_key debug; | |
2277 | del_pending: | |
2278 | btrfs_item_key_to_cpu(path->nodes[0], &debug, | |
2279 | pending_del_slot); | |
2280 | ret = btrfs_del_items(trans, root, path, | |
2281 | pending_del_slot, | |
2282 | pending_del_nr); | |
2283 | BUG_ON(ret); | |
2284 | pending_del_nr = 0; | |
2285 | btrfs_release_path(root, path); | |
2286 | goto search_again; | |
2287 | } | |
39279cc3 CM |
2288 | } |
2289 | ret = 0; | |
2290 | error: | |
85e21bac CM |
2291 | if (pending_del_nr) { |
2292 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
2293 | pending_del_nr); | |
2294 | } | |
39279cc3 CM |
2295 | btrfs_free_path(path); |
2296 | inode->i_sb->s_dirt = 1; | |
2297 | return ret; | |
2298 | } | |
2299 | ||
2300 | /* | |
2301 | * taken from block_truncate_page, but does cow as it zeros out | |
2302 | * any bytes left in the last page in the file. | |
2303 | */ | |
2304 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) | |
2305 | { | |
2306 | struct inode *inode = mapping->host; | |
db94535d | 2307 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
2308 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2309 | struct btrfs_ordered_extent *ordered; | |
2310 | char *kaddr; | |
db94535d | 2311 | u32 blocksize = root->sectorsize; |
39279cc3 CM |
2312 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
2313 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
2314 | struct page *page; | |
39279cc3 | 2315 | int ret = 0; |
a52d9a80 | 2316 | u64 page_start; |
e6dcd2dc | 2317 | u64 page_end; |
39279cc3 CM |
2318 | |
2319 | if ((offset & (blocksize - 1)) == 0) | |
2320 | goto out; | |
2321 | ||
2322 | ret = -ENOMEM; | |
211c17f5 | 2323 | again: |
39279cc3 CM |
2324 | page = grab_cache_page(mapping, index); |
2325 | if (!page) | |
2326 | goto out; | |
e6dcd2dc CM |
2327 | |
2328 | page_start = page_offset(page); | |
2329 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
2330 | ||
39279cc3 | 2331 | if (!PageUptodate(page)) { |
9ebefb18 | 2332 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 2333 | lock_page(page); |
211c17f5 CM |
2334 | if (page->mapping != mapping) { |
2335 | unlock_page(page); | |
2336 | page_cache_release(page); | |
2337 | goto again; | |
2338 | } | |
39279cc3 CM |
2339 | if (!PageUptodate(page)) { |
2340 | ret = -EIO; | |
89642229 | 2341 | goto out_unlock; |
39279cc3 CM |
2342 | } |
2343 | } | |
211c17f5 | 2344 | wait_on_page_writeback(page); |
e6dcd2dc CM |
2345 | |
2346 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
2347 | set_page_extent_mapped(page); | |
2348 | ||
2349 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
2350 | if (ordered) { | |
2351 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
2352 | unlock_page(page); | |
2353 | page_cache_release(page); | |
eb84ae03 | 2354 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
2355 | btrfs_put_ordered_extent(ordered); |
2356 | goto again; | |
2357 | } | |
2358 | ||
ea8c2819 | 2359 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
e6dcd2dc CM |
2360 | ret = 0; |
2361 | if (offset != PAGE_CACHE_SIZE) { | |
2362 | kaddr = kmap(page); | |
2363 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); | |
2364 | flush_dcache_page(page); | |
2365 | kunmap(page); | |
2366 | } | |
247e743c | 2367 | ClearPageChecked(page); |
e6dcd2dc CM |
2368 | set_page_dirty(page); |
2369 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
39279cc3 | 2370 | |
89642229 | 2371 | out_unlock: |
39279cc3 CM |
2372 | unlock_page(page); |
2373 | page_cache_release(page); | |
2374 | out: | |
2375 | return ret; | |
2376 | } | |
2377 | ||
9036c102 | 2378 | int btrfs_cont_expand(struct inode *inode, loff_t size) |
39279cc3 | 2379 | { |
9036c102 YZ |
2380 | struct btrfs_trans_handle *trans; |
2381 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2382 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
2383 | struct extent_map *em; | |
2384 | u64 mask = root->sectorsize - 1; | |
2385 | u64 hole_start = (inode->i_size + mask) & ~mask; | |
2386 | u64 block_end = (size + mask) & ~mask; | |
2387 | u64 last_byte; | |
2388 | u64 cur_offset; | |
2389 | u64 hole_size; | |
39279cc3 CM |
2390 | int err; |
2391 | ||
9036c102 YZ |
2392 | if (size <= hole_start) |
2393 | return 0; | |
2394 | ||
2395 | err = btrfs_check_free_space(root, 1, 0); | |
39279cc3 CM |
2396 | if (err) |
2397 | return err; | |
2398 | ||
9036c102 | 2399 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
2bf5a725 | 2400 | |
9036c102 YZ |
2401 | while (1) { |
2402 | struct btrfs_ordered_extent *ordered; | |
2403 | btrfs_wait_ordered_range(inode, hole_start, | |
2404 | block_end - hole_start); | |
2405 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); | |
2406 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); | |
2407 | if (!ordered) | |
2408 | break; | |
2409 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); | |
2410 | btrfs_put_ordered_extent(ordered); | |
2411 | } | |
39279cc3 | 2412 | |
9036c102 YZ |
2413 | trans = btrfs_start_transaction(root, 1); |
2414 | btrfs_set_trans_block_group(trans, inode); | |
39279cc3 | 2415 | |
9036c102 YZ |
2416 | cur_offset = hole_start; |
2417 | while (1) { | |
2418 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
2419 | block_end - cur_offset, 0); | |
2420 | BUG_ON(IS_ERR(em) || !em); | |
2421 | last_byte = min(extent_map_end(em), block_end); | |
2422 | last_byte = (last_byte + mask) & ~mask; | |
2423 | if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) { | |
2424 | hole_size = last_byte - cur_offset; | |
2425 | err = btrfs_insert_file_extent(trans, root, | |
2426 | inode->i_ino, cur_offset, 0, | |
2427 | 0, hole_size, 0, hole_size, | |
2428 | 0, 0, 0); | |
2429 | btrfs_drop_extent_cache(inode, hole_start, | |
2430 | last_byte - 1, 0); | |
2431 | } | |
2432 | free_extent_map(em); | |
2433 | cur_offset = last_byte; | |
2434 | if (err || cur_offset >= block_end) | |
2435 | break; | |
2436 | } | |
1832a6d5 | 2437 | |
9036c102 YZ |
2438 | btrfs_end_transaction(trans, root); |
2439 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); | |
2440 | return err; | |
2441 | } | |
39279cc3 | 2442 | |
9036c102 YZ |
2443 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
2444 | { | |
2445 | struct inode *inode = dentry->d_inode; | |
2446 | int err; | |
39279cc3 | 2447 | |
9036c102 YZ |
2448 | err = inode_change_ok(inode, attr); |
2449 | if (err) | |
2450 | return err; | |
2bf5a725 | 2451 | |
9036c102 YZ |
2452 | if (S_ISREG(inode->i_mode) && |
2453 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { | |
2454 | err = btrfs_cont_expand(inode, attr->ia_size); | |
54aa1f4d CM |
2455 | if (err) |
2456 | return err; | |
39279cc3 | 2457 | } |
9036c102 | 2458 | |
39279cc3 | 2459 | err = inode_setattr(inode, attr); |
33268eaf JB |
2460 | |
2461 | if (!err && ((attr->ia_valid & ATTR_MODE))) | |
2462 | err = btrfs_acl_chmod(inode); | |
39279cc3 CM |
2463 | return err; |
2464 | } | |
61295eb8 | 2465 | |
39279cc3 CM |
2466 | void btrfs_delete_inode(struct inode *inode) |
2467 | { | |
2468 | struct btrfs_trans_handle *trans; | |
2469 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d3c2fdcf | 2470 | unsigned long nr; |
39279cc3 CM |
2471 | int ret; |
2472 | ||
2473 | truncate_inode_pages(&inode->i_data, 0); | |
2474 | if (is_bad_inode(inode)) { | |
7b128766 | 2475 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
2476 | goto no_delete; |
2477 | } | |
4a096752 | 2478 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
5f39d397 | 2479 | |
dbe674a9 | 2480 | btrfs_i_size_write(inode, 0); |
39279cc3 | 2481 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 2482 | |
39279cc3 | 2483 | btrfs_set_trans_block_group(trans, inode); |
e02119d5 | 2484 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0); |
7b128766 JB |
2485 | if (ret) { |
2486 | btrfs_orphan_del(NULL, inode); | |
54aa1f4d | 2487 | goto no_delete_lock; |
7b128766 JB |
2488 | } |
2489 | ||
2490 | btrfs_orphan_del(trans, inode); | |
85e21bac | 2491 | |
d3c2fdcf | 2492 | nr = trans->blocks_used; |
85e21bac | 2493 | clear_inode(inode); |
5f39d397 | 2494 | |
39279cc3 | 2495 | btrfs_end_transaction(trans, root); |
d3c2fdcf | 2496 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 | 2497 | return; |
54aa1f4d CM |
2498 | |
2499 | no_delete_lock: | |
d3c2fdcf | 2500 | nr = trans->blocks_used; |
54aa1f4d | 2501 | btrfs_end_transaction(trans, root); |
d3c2fdcf | 2502 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
2503 | no_delete: |
2504 | clear_inode(inode); | |
2505 | } | |
2506 | ||
2507 | /* | |
2508 | * this returns the key found in the dir entry in the location pointer. | |
2509 | * If no dir entries were found, location->objectid is 0. | |
2510 | */ | |
2511 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
2512 | struct btrfs_key *location) | |
2513 | { | |
2514 | const char *name = dentry->d_name.name; | |
2515 | int namelen = dentry->d_name.len; | |
2516 | struct btrfs_dir_item *di; | |
2517 | struct btrfs_path *path; | |
2518 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 2519 | int ret = 0; |
39279cc3 CM |
2520 | |
2521 | path = btrfs_alloc_path(); | |
2522 | BUG_ON(!path); | |
3954401f | 2523 | |
39279cc3 CM |
2524 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
2525 | namelen, 0); | |
0d9f7f3e Y |
2526 | if (IS_ERR(di)) |
2527 | ret = PTR_ERR(di); | |
39279cc3 | 2528 | if (!di || IS_ERR(di)) { |
3954401f | 2529 | goto out_err; |
39279cc3 | 2530 | } |
5f39d397 | 2531 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 2532 | out: |
39279cc3 CM |
2533 | btrfs_free_path(path); |
2534 | return ret; | |
3954401f CM |
2535 | out_err: |
2536 | location->objectid = 0; | |
2537 | goto out; | |
39279cc3 CM |
2538 | } |
2539 | ||
2540 | /* | |
2541 | * when we hit a tree root in a directory, the btrfs part of the inode | |
2542 | * needs to be changed to reflect the root directory of the tree root. This | |
2543 | * is kind of like crossing a mount point. | |
2544 | */ | |
2545 | static int fixup_tree_root_location(struct btrfs_root *root, | |
2546 | struct btrfs_key *location, | |
58176a96 JB |
2547 | struct btrfs_root **sub_root, |
2548 | struct dentry *dentry) | |
39279cc3 | 2549 | { |
39279cc3 CM |
2550 | struct btrfs_root_item *ri; |
2551 | ||
2552 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) | |
2553 | return 0; | |
2554 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) | |
2555 | return 0; | |
2556 | ||
58176a96 JB |
2557 | *sub_root = btrfs_read_fs_root(root->fs_info, location, |
2558 | dentry->d_name.name, | |
2559 | dentry->d_name.len); | |
39279cc3 CM |
2560 | if (IS_ERR(*sub_root)) |
2561 | return PTR_ERR(*sub_root); | |
2562 | ||
2563 | ri = &(*sub_root)->root_item; | |
2564 | location->objectid = btrfs_root_dirid(ri); | |
39279cc3 CM |
2565 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
2566 | location->offset = 0; | |
2567 | ||
39279cc3 CM |
2568 | return 0; |
2569 | } | |
2570 | ||
e02119d5 | 2571 | static noinline void init_btrfs_i(struct inode *inode) |
39279cc3 | 2572 | { |
e02119d5 CM |
2573 | struct btrfs_inode *bi = BTRFS_I(inode); |
2574 | ||
2575 | bi->i_acl = NULL; | |
2576 | bi->i_default_acl = NULL; | |
2577 | ||
2578 | bi->generation = 0; | |
2579 | bi->last_trans = 0; | |
2580 | bi->logged_trans = 0; | |
2581 | bi->delalloc_bytes = 0; | |
2582 | bi->disk_i_size = 0; | |
2583 | bi->flags = 0; | |
2584 | bi->index_cnt = (u64)-1; | |
49eb7e46 | 2585 | bi->log_dirty_trans = 0; |
d1310b2e CM |
2586 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
2587 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | |
b888db2b | 2588 | inode->i_mapping, GFP_NOFS); |
7e38326f CM |
2589 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
2590 | inode->i_mapping, GFP_NOFS); | |
ea8c2819 | 2591 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
ba1da2f4 | 2592 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
1b1e2135 | 2593 | mutex_init(&BTRFS_I(inode)->csum_mutex); |
ee6e6504 | 2594 | mutex_init(&BTRFS_I(inode)->extent_mutex); |
e02119d5 CM |
2595 | mutex_init(&BTRFS_I(inode)->log_mutex); |
2596 | } | |
2597 | ||
2598 | static int btrfs_init_locked_inode(struct inode *inode, void *p) | |
2599 | { | |
2600 | struct btrfs_iget_args *args = p; | |
2601 | inode->i_ino = args->ino; | |
2602 | init_btrfs_i(inode); | |
2603 | BTRFS_I(inode)->root = args->root; | |
39279cc3 CM |
2604 | return 0; |
2605 | } | |
2606 | ||
2607 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
2608 | { | |
2609 | struct btrfs_iget_args *args = opaque; | |
2610 | return (args->ino == inode->i_ino && | |
2611 | args->root == BTRFS_I(inode)->root); | |
2612 | } | |
2613 | ||
5b21f2ed ZY |
2614 | struct inode *btrfs_ilookup(struct super_block *s, u64 objectid, |
2615 | struct btrfs_root *root, int wait) | |
2616 | { | |
2617 | struct inode *inode; | |
2618 | struct btrfs_iget_args args; | |
2619 | args.ino = objectid; | |
2620 | args.root = root; | |
2621 | ||
2622 | if (wait) { | |
2623 | inode = ilookup5(s, objectid, btrfs_find_actor, | |
2624 | (void *)&args); | |
2625 | } else { | |
2626 | inode = ilookup5_nowait(s, objectid, btrfs_find_actor, | |
2627 | (void *)&args); | |
2628 | } | |
2629 | return inode; | |
2630 | } | |
2631 | ||
39279cc3 CM |
2632 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
2633 | struct btrfs_root *root) | |
2634 | { | |
2635 | struct inode *inode; | |
2636 | struct btrfs_iget_args args; | |
2637 | args.ino = objectid; | |
2638 | args.root = root; | |
2639 | ||
2640 | inode = iget5_locked(s, objectid, btrfs_find_actor, | |
2641 | btrfs_init_locked_inode, | |
2642 | (void *)&args); | |
2643 | return inode; | |
2644 | } | |
2645 | ||
1a54ef8c BR |
2646 | /* Get an inode object given its location and corresponding root. |
2647 | * Returns in *is_new if the inode was read from disk | |
2648 | */ | |
2649 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
2650 | struct btrfs_root *root, int *is_new) | |
2651 | { | |
2652 | struct inode *inode; | |
2653 | ||
2654 | inode = btrfs_iget_locked(s, location->objectid, root); | |
2655 | if (!inode) | |
2656 | return ERR_PTR(-EACCES); | |
2657 | ||
2658 | if (inode->i_state & I_NEW) { | |
2659 | BTRFS_I(inode)->root = root; | |
2660 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); | |
2661 | btrfs_read_locked_inode(inode); | |
2662 | unlock_new_inode(inode); | |
2663 | if (is_new) | |
2664 | *is_new = 1; | |
2665 | } else { | |
2666 | if (is_new) | |
2667 | *is_new = 0; | |
2668 | } | |
2669 | ||
2670 | return inode; | |
2671 | } | |
2672 | ||
39279cc3 CM |
2673 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
2674 | struct nameidata *nd) | |
2675 | { | |
2676 | struct inode * inode; | |
2677 | struct btrfs_inode *bi = BTRFS_I(dir); | |
2678 | struct btrfs_root *root = bi->root; | |
2679 | struct btrfs_root *sub_root = root; | |
2680 | struct btrfs_key location; | |
1a54ef8c | 2681 | int ret, new, do_orphan = 0; |
39279cc3 CM |
2682 | |
2683 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
2684 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 2685 | |
39279cc3 | 2686 | ret = btrfs_inode_by_name(dir, dentry, &location); |
5f39d397 | 2687 | |
39279cc3 CM |
2688 | if (ret < 0) |
2689 | return ERR_PTR(ret); | |
5f39d397 | 2690 | |
39279cc3 CM |
2691 | inode = NULL; |
2692 | if (location.objectid) { | |
58176a96 JB |
2693 | ret = fixup_tree_root_location(root, &location, &sub_root, |
2694 | dentry); | |
39279cc3 CM |
2695 | if (ret < 0) |
2696 | return ERR_PTR(ret); | |
2697 | if (ret > 0) | |
2698 | return ERR_PTR(-ENOENT); | |
1a54ef8c BR |
2699 | inode = btrfs_iget(dir->i_sb, &location, sub_root, &new); |
2700 | if (IS_ERR(inode)) | |
2701 | return ERR_CAST(inode); | |
2702 | ||
2703 | /* the inode and parent dir are two different roots */ | |
2704 | if (new && root != sub_root) { | |
2705 | igrab(inode); | |
2706 | sub_root->inode = inode; | |
2707 | do_orphan = 1; | |
39279cc3 CM |
2708 | } |
2709 | } | |
7b128766 JB |
2710 | |
2711 | if (unlikely(do_orphan)) | |
2712 | btrfs_orphan_cleanup(sub_root); | |
2713 | ||
39279cc3 CM |
2714 | return d_splice_alias(inode, dentry); |
2715 | } | |
2716 | ||
39279cc3 CM |
2717 | static unsigned char btrfs_filetype_table[] = { |
2718 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK | |
2719 | }; | |
2720 | ||
cbdf5a24 DW |
2721 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
2722 | filldir_t filldir) | |
39279cc3 | 2723 | { |
6da6abae | 2724 | struct inode *inode = filp->f_dentry->d_inode; |
39279cc3 CM |
2725 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2726 | struct btrfs_item *item; | |
2727 | struct btrfs_dir_item *di; | |
2728 | struct btrfs_key key; | |
5f39d397 | 2729 | struct btrfs_key found_key; |
39279cc3 CM |
2730 | struct btrfs_path *path; |
2731 | int ret; | |
2732 | u32 nritems; | |
5f39d397 | 2733 | struct extent_buffer *leaf; |
39279cc3 CM |
2734 | int slot; |
2735 | int advance; | |
2736 | unsigned char d_type; | |
2737 | int over = 0; | |
2738 | u32 di_cur; | |
2739 | u32 di_total; | |
2740 | u32 di_len; | |
2741 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
2742 | char tmp_name[32]; |
2743 | char *name_ptr; | |
2744 | int name_len; | |
39279cc3 CM |
2745 | |
2746 | /* FIXME, use a real flag for deciding about the key type */ | |
2747 | if (root->fs_info->tree_root == root) | |
2748 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 2749 | |
3954401f CM |
2750 | /* special case for "." */ |
2751 | if (filp->f_pos == 0) { | |
2752 | over = filldir(dirent, ".", 1, | |
2753 | 1, inode->i_ino, | |
2754 | DT_DIR); | |
2755 | if (over) | |
2756 | return 0; | |
2757 | filp->f_pos = 1; | |
2758 | } | |
3954401f CM |
2759 | /* special case for .., just use the back ref */ |
2760 | if (filp->f_pos == 1) { | |
5ecc7e5d | 2761 | u64 pino = parent_ino(filp->f_path.dentry); |
3954401f | 2762 | over = filldir(dirent, "..", 2, |
5ecc7e5d | 2763 | 2, pino, DT_DIR); |
3954401f | 2764 | if (over) |
49593bfa | 2765 | return 0; |
3954401f CM |
2766 | filp->f_pos = 2; |
2767 | } | |
2768 | ||
49593bfa DW |
2769 | path = btrfs_alloc_path(); |
2770 | path->reada = 2; | |
2771 | ||
39279cc3 CM |
2772 | btrfs_set_key_type(&key, key_type); |
2773 | key.offset = filp->f_pos; | |
49593bfa | 2774 | key.objectid = inode->i_ino; |
5f39d397 | 2775 | |
39279cc3 CM |
2776 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
2777 | if (ret < 0) | |
2778 | goto err; | |
2779 | advance = 0; | |
49593bfa DW |
2780 | |
2781 | while (1) { | |
5f39d397 CM |
2782 | leaf = path->nodes[0]; |
2783 | nritems = btrfs_header_nritems(leaf); | |
39279cc3 CM |
2784 | slot = path->slots[0]; |
2785 | if (advance || slot >= nritems) { | |
49593bfa | 2786 | if (slot >= nritems - 1) { |
39279cc3 CM |
2787 | ret = btrfs_next_leaf(root, path); |
2788 | if (ret) | |
2789 | break; | |
5f39d397 CM |
2790 | leaf = path->nodes[0]; |
2791 | nritems = btrfs_header_nritems(leaf); | |
39279cc3 CM |
2792 | slot = path->slots[0]; |
2793 | } else { | |
2794 | slot++; | |
2795 | path->slots[0]++; | |
2796 | } | |
2797 | } | |
2798 | advance = 1; | |
5f39d397 CM |
2799 | item = btrfs_item_nr(leaf, slot); |
2800 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
2801 | ||
2802 | if (found_key.objectid != key.objectid) | |
39279cc3 | 2803 | break; |
5f39d397 | 2804 | if (btrfs_key_type(&found_key) != key_type) |
39279cc3 | 2805 | break; |
5f39d397 | 2806 | if (found_key.offset < filp->f_pos) |
39279cc3 | 2807 | continue; |
5f39d397 CM |
2808 | |
2809 | filp->f_pos = found_key.offset; | |
49593bfa | 2810 | |
39279cc3 CM |
2811 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
2812 | di_cur = 0; | |
5f39d397 | 2813 | di_total = btrfs_item_size(leaf, item); |
49593bfa DW |
2814 | |
2815 | while (di_cur < di_total) { | |
5f39d397 CM |
2816 | struct btrfs_key location; |
2817 | ||
2818 | name_len = btrfs_dir_name_len(leaf, di); | |
49593bfa | 2819 | if (name_len <= sizeof(tmp_name)) { |
5f39d397 CM |
2820 | name_ptr = tmp_name; |
2821 | } else { | |
2822 | name_ptr = kmalloc(name_len, GFP_NOFS); | |
49593bfa DW |
2823 | if (!name_ptr) { |
2824 | ret = -ENOMEM; | |
2825 | goto err; | |
2826 | } | |
5f39d397 CM |
2827 | } |
2828 | read_extent_buffer(leaf, name_ptr, | |
2829 | (unsigned long)(di + 1), name_len); | |
2830 | ||
2831 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
2832 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
5f39d397 | 2833 | over = filldir(dirent, name_ptr, name_len, |
49593bfa | 2834 | found_key.offset, location.objectid, |
39279cc3 | 2835 | d_type); |
5f39d397 CM |
2836 | |
2837 | if (name_ptr != tmp_name) | |
2838 | kfree(name_ptr); | |
2839 | ||
39279cc3 CM |
2840 | if (over) |
2841 | goto nopos; | |
49593bfa | 2842 | |
5103e947 | 2843 | di_len = btrfs_dir_name_len(leaf, di) + |
49593bfa | 2844 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
39279cc3 CM |
2845 | di_cur += di_len; |
2846 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
2847 | } | |
2848 | } | |
49593bfa DW |
2849 | |
2850 | /* Reached end of directory/root. Bump pos past the last item. */ | |
5e591a07 YZ |
2851 | if (key_type == BTRFS_DIR_INDEX_KEY) |
2852 | filp->f_pos = INT_LIMIT(typeof(filp->f_pos)); | |
2853 | else | |
2854 | filp->f_pos++; | |
39279cc3 CM |
2855 | nopos: |
2856 | ret = 0; | |
2857 | err: | |
39279cc3 | 2858 | btrfs_free_path(path); |
39279cc3 CM |
2859 | return ret; |
2860 | } | |
2861 | ||
2862 | int btrfs_write_inode(struct inode *inode, int wait) | |
2863 | { | |
2864 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2865 | struct btrfs_trans_handle *trans; | |
2866 | int ret = 0; | |
2867 | ||
4ca8b41e CM |
2868 | if (root->fs_info->closing > 1) |
2869 | return 0; | |
2870 | ||
39279cc3 | 2871 | if (wait) { |
f9295749 | 2872 | trans = btrfs_join_transaction(root, 1); |
39279cc3 CM |
2873 | btrfs_set_trans_block_group(trans, inode); |
2874 | ret = btrfs_commit_transaction(trans, root); | |
39279cc3 CM |
2875 | } |
2876 | return ret; | |
2877 | } | |
2878 | ||
2879 | /* | |
54aa1f4d | 2880 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
2881 | * inode changes. But, it is most likely to find the inode in cache. |
2882 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
2883 | * to keep or drop this code. | |
2884 | */ | |
2885 | void btrfs_dirty_inode(struct inode *inode) | |
2886 | { | |
2887 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2888 | struct btrfs_trans_handle *trans; | |
2889 | ||
f9295749 | 2890 | trans = btrfs_join_transaction(root, 1); |
39279cc3 CM |
2891 | btrfs_set_trans_block_group(trans, inode); |
2892 | btrfs_update_inode(trans, root, inode); | |
2893 | btrfs_end_transaction(trans, root); | |
39279cc3 CM |
2894 | } |
2895 | ||
d352ac68 CM |
2896 | /* |
2897 | * find the highest existing sequence number in a directory | |
2898 | * and then set the in-memory index_cnt variable to reflect | |
2899 | * free sequence numbers | |
2900 | */ | |
aec7477b JB |
2901 | static int btrfs_set_inode_index_count(struct inode *inode) |
2902 | { | |
2903 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2904 | struct btrfs_key key, found_key; | |
2905 | struct btrfs_path *path; | |
2906 | struct extent_buffer *leaf; | |
2907 | int ret; | |
2908 | ||
2909 | key.objectid = inode->i_ino; | |
2910 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); | |
2911 | key.offset = (u64)-1; | |
2912 | ||
2913 | path = btrfs_alloc_path(); | |
2914 | if (!path) | |
2915 | return -ENOMEM; | |
2916 | ||
2917 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2918 | if (ret < 0) | |
2919 | goto out; | |
2920 | /* FIXME: we should be able to handle this */ | |
2921 | if (ret == 0) | |
2922 | goto out; | |
2923 | ret = 0; | |
2924 | ||
2925 | /* | |
2926 | * MAGIC NUMBER EXPLANATION: | |
2927 | * since we search a directory based on f_pos we have to start at 2 | |
2928 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
2929 | * else has to start at 2 | |
2930 | */ | |
2931 | if (path->slots[0] == 0) { | |
2932 | BTRFS_I(inode)->index_cnt = 2; | |
2933 | goto out; | |
2934 | } | |
2935 | ||
2936 | path->slots[0]--; | |
2937 | ||
2938 | leaf = path->nodes[0]; | |
2939 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2940 | ||
2941 | if (found_key.objectid != inode->i_ino || | |
2942 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { | |
2943 | BTRFS_I(inode)->index_cnt = 2; | |
2944 | goto out; | |
2945 | } | |
2946 | ||
2947 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
2948 | out: | |
2949 | btrfs_free_path(path); | |
2950 | return ret; | |
2951 | } | |
2952 | ||
d352ac68 CM |
2953 | /* |
2954 | * helper to find a free sequence number in a given directory. This current | |
2955 | * code is very simple, later versions will do smarter things in the btree | |
2956 | */ | |
00e4e6b3 CM |
2957 | static int btrfs_set_inode_index(struct inode *dir, struct inode *inode, |
2958 | u64 *index) | |
aec7477b JB |
2959 | { |
2960 | int ret = 0; | |
2961 | ||
2962 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
2963 | ret = btrfs_set_inode_index_count(dir); | |
8d5bf1cb | 2964 | if (ret) { |
aec7477b | 2965 | return ret; |
8d5bf1cb | 2966 | } |
aec7477b JB |
2967 | } |
2968 | ||
00e4e6b3 | 2969 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
2970 | BTRFS_I(dir)->index_cnt++; |
2971 | ||
2972 | return ret; | |
2973 | } | |
2974 | ||
39279cc3 CM |
2975 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
2976 | struct btrfs_root *root, | |
aec7477b | 2977 | struct inode *dir, |
9c58309d CM |
2978 | const char *name, int name_len, |
2979 | u64 ref_objectid, | |
39279cc3 CM |
2980 | u64 objectid, |
2981 | struct btrfs_block_group_cache *group, | |
00e4e6b3 | 2982 | int mode, u64 *index) |
39279cc3 CM |
2983 | { |
2984 | struct inode *inode; | |
5f39d397 | 2985 | struct btrfs_inode_item *inode_item; |
6324fbf3 | 2986 | struct btrfs_block_group_cache *new_inode_group; |
39279cc3 | 2987 | struct btrfs_key *location; |
5f39d397 | 2988 | struct btrfs_path *path; |
9c58309d CM |
2989 | struct btrfs_inode_ref *ref; |
2990 | struct btrfs_key key[2]; | |
2991 | u32 sizes[2]; | |
2992 | unsigned long ptr; | |
39279cc3 CM |
2993 | int ret; |
2994 | int owner; | |
2995 | ||
5f39d397 CM |
2996 | path = btrfs_alloc_path(); |
2997 | BUG_ON(!path); | |
2998 | ||
39279cc3 CM |
2999 | inode = new_inode(root->fs_info->sb); |
3000 | if (!inode) | |
3001 | return ERR_PTR(-ENOMEM); | |
3002 | ||
aec7477b | 3003 | if (dir) { |
00e4e6b3 | 3004 | ret = btrfs_set_inode_index(dir, inode, index); |
aec7477b JB |
3005 | if (ret) |
3006 | return ERR_PTR(ret); | |
aec7477b JB |
3007 | } |
3008 | /* | |
3009 | * index_cnt is ignored for everything but a dir, | |
3010 | * btrfs_get_inode_index_count has an explanation for the magic | |
3011 | * number | |
3012 | */ | |
e02119d5 | 3013 | init_btrfs_i(inode); |
aec7477b | 3014 | BTRFS_I(inode)->index_cnt = 2; |
39279cc3 | 3015 | BTRFS_I(inode)->root = root; |
e02119d5 | 3016 | BTRFS_I(inode)->generation = trans->transid; |
b888db2b | 3017 | |
39279cc3 CM |
3018 | if (mode & S_IFDIR) |
3019 | owner = 0; | |
3020 | else | |
3021 | owner = 1; | |
6324fbf3 | 3022 | new_inode_group = btrfs_find_block_group(root, group, 0, |
0b86a832 | 3023 | BTRFS_BLOCK_GROUP_METADATA, owner); |
6324fbf3 CM |
3024 | if (!new_inode_group) { |
3025 | printk("find_block group failed\n"); | |
3026 | new_inode_group = group; | |
3027 | } | |
3028 | BTRFS_I(inode)->block_group = new_inode_group; | |
9c58309d CM |
3029 | |
3030 | key[0].objectid = objectid; | |
3031 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); | |
3032 | key[0].offset = 0; | |
3033 | ||
3034 | key[1].objectid = objectid; | |
3035 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); | |
3036 | key[1].offset = ref_objectid; | |
3037 | ||
3038 | sizes[0] = sizeof(struct btrfs_inode_item); | |
3039 | sizes[1] = name_len + sizeof(*ref); | |
3040 | ||
3041 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); | |
3042 | if (ret != 0) | |
5f39d397 CM |
3043 | goto fail; |
3044 | ||
9c58309d CM |
3045 | if (objectid > root->highest_inode) |
3046 | root->highest_inode = objectid; | |
3047 | ||
39279cc3 CM |
3048 | inode->i_uid = current->fsuid; |
3049 | inode->i_gid = current->fsgid; | |
3050 | inode->i_mode = mode; | |
3051 | inode->i_ino = objectid; | |
a76a3cd4 | 3052 | inode_set_bytes(inode, 0); |
39279cc3 | 3053 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
5f39d397 CM |
3054 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
3055 | struct btrfs_inode_item); | |
e02119d5 | 3056 | fill_inode_item(trans, path->nodes[0], inode_item, inode); |
9c58309d CM |
3057 | |
3058 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
3059 | struct btrfs_inode_ref); | |
3060 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
00e4e6b3 | 3061 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
9c58309d CM |
3062 | ptr = (unsigned long)(ref + 1); |
3063 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
3064 | ||
5f39d397 CM |
3065 | btrfs_mark_buffer_dirty(path->nodes[0]); |
3066 | btrfs_free_path(path); | |
3067 | ||
39279cc3 CM |
3068 | location = &BTRFS_I(inode)->location; |
3069 | location->objectid = objectid; | |
39279cc3 CM |
3070 | location->offset = 0; |
3071 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | |
3072 | ||
39279cc3 CM |
3073 | insert_inode_hash(inode); |
3074 | return inode; | |
5f39d397 | 3075 | fail: |
aec7477b JB |
3076 | if (dir) |
3077 | BTRFS_I(dir)->index_cnt--; | |
5f39d397 CM |
3078 | btrfs_free_path(path); |
3079 | return ERR_PTR(ret); | |
39279cc3 CM |
3080 | } |
3081 | ||
3082 | static inline u8 btrfs_inode_type(struct inode *inode) | |
3083 | { | |
3084 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
3085 | } | |
3086 | ||
d352ac68 CM |
3087 | /* |
3088 | * utility function to add 'inode' into 'parent_inode' with | |
3089 | * a give name and a given sequence number. | |
3090 | * if 'add_backref' is true, also insert a backref from the | |
3091 | * inode to the parent directory. | |
3092 | */ | |
e02119d5 CM |
3093 | int btrfs_add_link(struct btrfs_trans_handle *trans, |
3094 | struct inode *parent_inode, struct inode *inode, | |
3095 | const char *name, int name_len, int add_backref, u64 index) | |
39279cc3 CM |
3096 | { |
3097 | int ret; | |
3098 | struct btrfs_key key; | |
e02119d5 | 3099 | struct btrfs_root *root = BTRFS_I(parent_inode)->root; |
5f39d397 | 3100 | |
39279cc3 | 3101 | key.objectid = inode->i_ino; |
39279cc3 CM |
3102 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
3103 | key.offset = 0; | |
3104 | ||
e02119d5 CM |
3105 | ret = btrfs_insert_dir_item(trans, root, name, name_len, |
3106 | parent_inode->i_ino, | |
aec7477b | 3107 | &key, btrfs_inode_type(inode), |
00e4e6b3 | 3108 | index); |
39279cc3 | 3109 | if (ret == 0) { |
9c58309d CM |
3110 | if (add_backref) { |
3111 | ret = btrfs_insert_inode_ref(trans, root, | |
e02119d5 CM |
3112 | name, name_len, |
3113 | inode->i_ino, | |
3114 | parent_inode->i_ino, | |
3115 | index); | |
9c58309d | 3116 | } |
dbe674a9 | 3117 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
e02119d5 | 3118 | name_len * 2); |
79c44584 | 3119 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
e02119d5 | 3120 | ret = btrfs_update_inode(trans, root, parent_inode); |
39279cc3 CM |
3121 | } |
3122 | return ret; | |
3123 | } | |
3124 | ||
3125 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
9c58309d | 3126 | struct dentry *dentry, struct inode *inode, |
00e4e6b3 | 3127 | int backref, u64 index) |
39279cc3 | 3128 | { |
e02119d5 CM |
3129 | int err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
3130 | inode, dentry->d_name.name, | |
3131 | dentry->d_name.len, backref, index); | |
39279cc3 CM |
3132 | if (!err) { |
3133 | d_instantiate(dentry, inode); | |
3134 | return 0; | |
3135 | } | |
3136 | if (err > 0) | |
3137 | err = -EEXIST; | |
3138 | return err; | |
3139 | } | |
3140 | ||
618e21d5 JB |
3141 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
3142 | int mode, dev_t rdev) | |
3143 | { | |
3144 | struct btrfs_trans_handle *trans; | |
3145 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 3146 | struct inode *inode = NULL; |
618e21d5 JB |
3147 | int err; |
3148 | int drop_inode = 0; | |
3149 | u64 objectid; | |
1832a6d5 | 3150 | unsigned long nr = 0; |
00e4e6b3 | 3151 | u64 index = 0; |
618e21d5 JB |
3152 | |
3153 | if (!new_valid_dev(rdev)) | |
3154 | return -EINVAL; | |
3155 | ||
1832a6d5 CM |
3156 | err = btrfs_check_free_space(root, 1, 0); |
3157 | if (err) | |
3158 | goto fail; | |
3159 | ||
618e21d5 JB |
3160 | trans = btrfs_start_transaction(root, 1); |
3161 | btrfs_set_trans_block_group(trans, dir); | |
3162 | ||
3163 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
3164 | if (err) { | |
3165 | err = -ENOSPC; | |
3166 | goto out_unlock; | |
3167 | } | |
3168 | ||
aec7477b | 3169 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
9c58309d CM |
3170 | dentry->d_name.len, |
3171 | dentry->d_parent->d_inode->i_ino, objectid, | |
00e4e6b3 | 3172 | BTRFS_I(dir)->block_group, mode, &index); |
618e21d5 JB |
3173 | err = PTR_ERR(inode); |
3174 | if (IS_ERR(inode)) | |
3175 | goto out_unlock; | |
3176 | ||
33268eaf JB |
3177 | err = btrfs_init_acl(inode, dir); |
3178 | if (err) { | |
3179 | drop_inode = 1; | |
3180 | goto out_unlock; | |
3181 | } | |
3182 | ||
618e21d5 | 3183 | btrfs_set_trans_block_group(trans, inode); |
00e4e6b3 | 3184 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
618e21d5 JB |
3185 | if (err) |
3186 | drop_inode = 1; | |
3187 | else { | |
3188 | inode->i_op = &btrfs_special_inode_operations; | |
3189 | init_special_inode(inode, inode->i_mode, rdev); | |
1b4ab1bb | 3190 | btrfs_update_inode(trans, root, inode); |
618e21d5 JB |
3191 | } |
3192 | dir->i_sb->s_dirt = 1; | |
3193 | btrfs_update_inode_block_group(trans, inode); | |
3194 | btrfs_update_inode_block_group(trans, dir); | |
3195 | out_unlock: | |
d3c2fdcf | 3196 | nr = trans->blocks_used; |
89ce8a63 | 3197 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 3198 | fail: |
618e21d5 JB |
3199 | if (drop_inode) { |
3200 | inode_dec_link_count(inode); | |
3201 | iput(inode); | |
3202 | } | |
d3c2fdcf | 3203 | btrfs_btree_balance_dirty(root, nr); |
618e21d5 JB |
3204 | return err; |
3205 | } | |
3206 | ||
39279cc3 CM |
3207 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
3208 | int mode, struct nameidata *nd) | |
3209 | { | |
3210 | struct btrfs_trans_handle *trans; | |
3211 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 3212 | struct inode *inode = NULL; |
39279cc3 CM |
3213 | int err; |
3214 | int drop_inode = 0; | |
1832a6d5 | 3215 | unsigned long nr = 0; |
39279cc3 | 3216 | u64 objectid; |
00e4e6b3 | 3217 | u64 index = 0; |
39279cc3 | 3218 | |
1832a6d5 CM |
3219 | err = btrfs_check_free_space(root, 1, 0); |
3220 | if (err) | |
3221 | goto fail; | |
39279cc3 CM |
3222 | trans = btrfs_start_transaction(root, 1); |
3223 | btrfs_set_trans_block_group(trans, dir); | |
3224 | ||
3225 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
3226 | if (err) { | |
3227 | err = -ENOSPC; | |
3228 | goto out_unlock; | |
3229 | } | |
3230 | ||
aec7477b | 3231 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
9c58309d CM |
3232 | dentry->d_name.len, |
3233 | dentry->d_parent->d_inode->i_ino, | |
00e4e6b3 CM |
3234 | objectid, BTRFS_I(dir)->block_group, mode, |
3235 | &index); | |
39279cc3 CM |
3236 | err = PTR_ERR(inode); |
3237 | if (IS_ERR(inode)) | |
3238 | goto out_unlock; | |
3239 | ||
33268eaf JB |
3240 | err = btrfs_init_acl(inode, dir); |
3241 | if (err) { | |
3242 | drop_inode = 1; | |
3243 | goto out_unlock; | |
3244 | } | |
3245 | ||
39279cc3 | 3246 | btrfs_set_trans_block_group(trans, inode); |
00e4e6b3 | 3247 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
39279cc3 CM |
3248 | if (err) |
3249 | drop_inode = 1; | |
3250 | else { | |
3251 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 3252 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 CM |
3253 | inode->i_fop = &btrfs_file_operations; |
3254 | inode->i_op = &btrfs_file_inode_operations; | |
d1310b2e | 3255 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
3256 | } |
3257 | dir->i_sb->s_dirt = 1; | |
3258 | btrfs_update_inode_block_group(trans, inode); | |
3259 | btrfs_update_inode_block_group(trans, dir); | |
3260 | out_unlock: | |
d3c2fdcf | 3261 | nr = trans->blocks_used; |
ab78c84d | 3262 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 3263 | fail: |
39279cc3 CM |
3264 | if (drop_inode) { |
3265 | inode_dec_link_count(inode); | |
3266 | iput(inode); | |
3267 | } | |
d3c2fdcf | 3268 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
3269 | return err; |
3270 | } | |
3271 | ||
3272 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
3273 | struct dentry *dentry) | |
3274 | { | |
3275 | struct btrfs_trans_handle *trans; | |
3276 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
3277 | struct inode *inode = old_dentry->d_inode; | |
00e4e6b3 | 3278 | u64 index; |
1832a6d5 | 3279 | unsigned long nr = 0; |
39279cc3 CM |
3280 | int err; |
3281 | int drop_inode = 0; | |
3282 | ||
3283 | if (inode->i_nlink == 0) | |
3284 | return -ENOENT; | |
3285 | ||
e02119d5 | 3286 | btrfs_inc_nlink(inode); |
1832a6d5 CM |
3287 | err = btrfs_check_free_space(root, 1, 0); |
3288 | if (err) | |
3289 | goto fail; | |
00e4e6b3 | 3290 | err = btrfs_set_inode_index(dir, inode, &index); |
aec7477b JB |
3291 | if (err) |
3292 | goto fail; | |
3293 | ||
39279cc3 | 3294 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 3295 | |
39279cc3 CM |
3296 | btrfs_set_trans_block_group(trans, dir); |
3297 | atomic_inc(&inode->i_count); | |
aec7477b | 3298 | |
00e4e6b3 | 3299 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
5f39d397 | 3300 | |
39279cc3 CM |
3301 | if (err) |
3302 | drop_inode = 1; | |
5f39d397 | 3303 | |
39279cc3 CM |
3304 | dir->i_sb->s_dirt = 1; |
3305 | btrfs_update_inode_block_group(trans, dir); | |
54aa1f4d | 3306 | err = btrfs_update_inode(trans, root, inode); |
5f39d397 | 3307 | |
54aa1f4d CM |
3308 | if (err) |
3309 | drop_inode = 1; | |
39279cc3 | 3310 | |
d3c2fdcf | 3311 | nr = trans->blocks_used; |
ab78c84d | 3312 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 3313 | fail: |
39279cc3 CM |
3314 | if (drop_inode) { |
3315 | inode_dec_link_count(inode); | |
3316 | iput(inode); | |
3317 | } | |
d3c2fdcf | 3318 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
3319 | return err; |
3320 | } | |
3321 | ||
39279cc3 CM |
3322 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
3323 | { | |
b9d86667 | 3324 | struct inode *inode = NULL; |
39279cc3 CM |
3325 | struct btrfs_trans_handle *trans; |
3326 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
3327 | int err = 0; | |
3328 | int drop_on_err = 0; | |
b9d86667 | 3329 | u64 objectid = 0; |
00e4e6b3 | 3330 | u64 index = 0; |
d3c2fdcf | 3331 | unsigned long nr = 1; |
39279cc3 | 3332 | |
1832a6d5 CM |
3333 | err = btrfs_check_free_space(root, 1, 0); |
3334 | if (err) | |
3335 | goto out_unlock; | |
3336 | ||
39279cc3 CM |
3337 | trans = btrfs_start_transaction(root, 1); |
3338 | btrfs_set_trans_block_group(trans, dir); | |
5f39d397 | 3339 | |
39279cc3 CM |
3340 | if (IS_ERR(trans)) { |
3341 | err = PTR_ERR(trans); | |
3342 | goto out_unlock; | |
3343 | } | |
3344 | ||
3345 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
3346 | if (err) { | |
3347 | err = -ENOSPC; | |
3348 | goto out_unlock; | |
3349 | } | |
3350 | ||
aec7477b | 3351 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
9c58309d CM |
3352 | dentry->d_name.len, |
3353 | dentry->d_parent->d_inode->i_ino, objectid, | |
00e4e6b3 CM |
3354 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
3355 | &index); | |
39279cc3 CM |
3356 | if (IS_ERR(inode)) { |
3357 | err = PTR_ERR(inode); | |
3358 | goto out_fail; | |
3359 | } | |
5f39d397 | 3360 | |
39279cc3 | 3361 | drop_on_err = 1; |
33268eaf JB |
3362 | |
3363 | err = btrfs_init_acl(inode, dir); | |
3364 | if (err) | |
3365 | goto out_fail; | |
3366 | ||
39279cc3 CM |
3367 | inode->i_op = &btrfs_dir_inode_operations; |
3368 | inode->i_fop = &btrfs_dir_file_operations; | |
3369 | btrfs_set_trans_block_group(trans, inode); | |
3370 | ||
dbe674a9 | 3371 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
3372 | err = btrfs_update_inode(trans, root, inode); |
3373 | if (err) | |
3374 | goto out_fail; | |
5f39d397 | 3375 | |
e02119d5 CM |
3376 | err = btrfs_add_link(trans, dentry->d_parent->d_inode, |
3377 | inode, dentry->d_name.name, | |
3378 | dentry->d_name.len, 0, index); | |
39279cc3 CM |
3379 | if (err) |
3380 | goto out_fail; | |
5f39d397 | 3381 | |
39279cc3 CM |
3382 | d_instantiate(dentry, inode); |
3383 | drop_on_err = 0; | |
3384 | dir->i_sb->s_dirt = 1; | |
3385 | btrfs_update_inode_block_group(trans, inode); | |
3386 | btrfs_update_inode_block_group(trans, dir); | |
3387 | ||
3388 | out_fail: | |
d3c2fdcf | 3389 | nr = trans->blocks_used; |
ab78c84d | 3390 | btrfs_end_transaction_throttle(trans, root); |
5f39d397 | 3391 | |
39279cc3 | 3392 | out_unlock: |
39279cc3 CM |
3393 | if (drop_on_err) |
3394 | iput(inode); | |
d3c2fdcf | 3395 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
3396 | return err; |
3397 | } | |
3398 | ||
d352ac68 CM |
3399 | /* helper for btfs_get_extent. Given an existing extent in the tree, |
3400 | * and an extent that you want to insert, deal with overlap and insert | |
3401 | * the new extent into the tree. | |
3402 | */ | |
3b951516 CM |
3403 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
3404 | struct extent_map *existing, | |
e6dcd2dc CM |
3405 | struct extent_map *em, |
3406 | u64 map_start, u64 map_len) | |
3b951516 CM |
3407 | { |
3408 | u64 start_diff; | |
3b951516 | 3409 | |
e6dcd2dc CM |
3410 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
3411 | start_diff = map_start - em->start; | |
3412 | em->start = map_start; | |
3413 | em->len = map_len; | |
c8b97818 CM |
3414 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
3415 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
e6dcd2dc | 3416 | em->block_start += start_diff; |
c8b97818 CM |
3417 | em->block_len -= start_diff; |
3418 | } | |
e6dcd2dc | 3419 | return add_extent_mapping(em_tree, em); |
3b951516 CM |
3420 | } |
3421 | ||
c8b97818 CM |
3422 | static noinline int uncompress_inline(struct btrfs_path *path, |
3423 | struct inode *inode, struct page *page, | |
3424 | size_t pg_offset, u64 extent_offset, | |
3425 | struct btrfs_file_extent_item *item) | |
3426 | { | |
3427 | int ret; | |
3428 | struct extent_buffer *leaf = path->nodes[0]; | |
3429 | char *tmp; | |
3430 | size_t max_size; | |
3431 | unsigned long inline_size; | |
3432 | unsigned long ptr; | |
3433 | ||
3434 | WARN_ON(pg_offset != 0); | |
3435 | max_size = btrfs_file_extent_ram_bytes(leaf, item); | |
3436 | inline_size = btrfs_file_extent_inline_item_len(leaf, | |
3437 | btrfs_item_nr(leaf, path->slots[0])); | |
3438 | tmp = kmalloc(inline_size, GFP_NOFS); | |
3439 | ptr = btrfs_file_extent_inline_start(item); | |
3440 | ||
3441 | read_extent_buffer(leaf, tmp, ptr, inline_size); | |
3442 | ||
3443 | max_size = min(PAGE_CACHE_SIZE, max_size); | |
3444 | ret = btrfs_zlib_decompress(tmp, page, extent_offset, | |
3445 | inline_size, max_size); | |
3446 | if (ret) { | |
3447 | char *kaddr = kmap_atomic(page, KM_USER0); | |
3448 | unsigned long copy_size = min_t(u64, | |
3449 | PAGE_CACHE_SIZE - pg_offset, | |
3450 | max_size - extent_offset); | |
3451 | memset(kaddr + pg_offset, 0, copy_size); | |
3452 | kunmap_atomic(kaddr, KM_USER0); | |
3453 | } | |
3454 | kfree(tmp); | |
3455 | return 0; | |
3456 | } | |
3457 | ||
d352ac68 CM |
3458 | /* |
3459 | * a bit scary, this does extent mapping from logical file offset to the disk. | |
3460 | * the ugly parts come from merging extents from the disk with the | |
3461 | * in-ram representation. This gets more complex because of the data=ordered code, | |
3462 | * where the in-ram extents might be locked pending data=ordered completion. | |
3463 | * | |
3464 | * This also copies inline extents directly into the page. | |
3465 | */ | |
a52d9a80 | 3466 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 3467 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
3468 | int create) |
3469 | { | |
3470 | int ret; | |
3471 | int err = 0; | |
db94535d | 3472 | u64 bytenr; |
a52d9a80 CM |
3473 | u64 extent_start = 0; |
3474 | u64 extent_end = 0; | |
3475 | u64 objectid = inode->i_ino; | |
3476 | u32 found_type; | |
f421950f | 3477 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
3478 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3479 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
3480 | struct extent_buffer *leaf; |
3481 | struct btrfs_key found_key; | |
a52d9a80 CM |
3482 | struct extent_map *em = NULL; |
3483 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 3484 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 | 3485 | struct btrfs_trans_handle *trans = NULL; |
c8b97818 | 3486 | int compressed; |
a52d9a80 | 3487 | |
a52d9a80 | 3488 | again: |
d1310b2e CM |
3489 | spin_lock(&em_tree->lock); |
3490 | em = lookup_extent_mapping(em_tree, start, len); | |
a061fc8d CM |
3491 | if (em) |
3492 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
d1310b2e CM |
3493 | spin_unlock(&em_tree->lock); |
3494 | ||
a52d9a80 | 3495 | if (em) { |
e1c4b745 CM |
3496 | if (em->start > start || em->start + em->len <= start) |
3497 | free_extent_map(em); | |
3498 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
3499 | free_extent_map(em); |
3500 | else | |
3501 | goto out; | |
a52d9a80 | 3502 | } |
d1310b2e | 3503 | em = alloc_extent_map(GFP_NOFS); |
a52d9a80 | 3504 | if (!em) { |
d1310b2e CM |
3505 | err = -ENOMEM; |
3506 | goto out; | |
a52d9a80 | 3507 | } |
e6dcd2dc | 3508 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
d1310b2e CM |
3509 | em->start = EXTENT_MAP_HOLE; |
3510 | em->len = (u64)-1; | |
c8b97818 | 3511 | em->block_len = (u64)-1; |
f421950f CM |
3512 | |
3513 | if (!path) { | |
3514 | path = btrfs_alloc_path(); | |
3515 | BUG_ON(!path); | |
3516 | } | |
3517 | ||
179e29e4 CM |
3518 | ret = btrfs_lookup_file_extent(trans, root, path, |
3519 | objectid, start, trans != NULL); | |
a52d9a80 CM |
3520 | if (ret < 0) { |
3521 | err = ret; | |
3522 | goto out; | |
3523 | } | |
3524 | ||
3525 | if (ret != 0) { | |
3526 | if (path->slots[0] == 0) | |
3527 | goto not_found; | |
3528 | path->slots[0]--; | |
3529 | } | |
3530 | ||
5f39d397 CM |
3531 | leaf = path->nodes[0]; |
3532 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 3533 | struct btrfs_file_extent_item); |
a52d9a80 | 3534 | /* are we inside the extent that was found? */ |
5f39d397 CM |
3535 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3536 | found_type = btrfs_key_type(&found_key); | |
3537 | if (found_key.objectid != objectid || | |
a52d9a80 CM |
3538 | found_type != BTRFS_EXTENT_DATA_KEY) { |
3539 | goto not_found; | |
3540 | } | |
3541 | ||
5f39d397 CM |
3542 | found_type = btrfs_file_extent_type(leaf, item); |
3543 | extent_start = found_key.offset; | |
c8b97818 | 3544 | compressed = btrfs_file_extent_compression(leaf, item); |
d899e052 YZ |
3545 | if (found_type == BTRFS_FILE_EXTENT_REG || |
3546 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
a52d9a80 | 3547 | extent_end = extent_start + |
db94535d | 3548 | btrfs_file_extent_num_bytes(leaf, item); |
9036c102 YZ |
3549 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
3550 | size_t size; | |
3551 | size = btrfs_file_extent_inline_len(leaf, item); | |
3552 | extent_end = (extent_start + size + root->sectorsize - 1) & | |
3553 | ~((u64)root->sectorsize - 1); | |
3554 | } | |
3555 | ||
3556 | if (start >= extent_end) { | |
3557 | path->slots[0]++; | |
3558 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
3559 | ret = btrfs_next_leaf(root, path); | |
3560 | if (ret < 0) { | |
3561 | err = ret; | |
3562 | goto out; | |
a52d9a80 | 3563 | } |
9036c102 YZ |
3564 | if (ret > 0) |
3565 | goto not_found; | |
3566 | leaf = path->nodes[0]; | |
a52d9a80 | 3567 | } |
9036c102 YZ |
3568 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
3569 | if (found_key.objectid != objectid || | |
3570 | found_key.type != BTRFS_EXTENT_DATA_KEY) | |
3571 | goto not_found; | |
3572 | if (start + len <= found_key.offset) | |
3573 | goto not_found; | |
3574 | em->start = start; | |
3575 | em->len = found_key.offset - start; | |
3576 | goto not_found_em; | |
3577 | } | |
3578 | ||
d899e052 YZ |
3579 | if (found_type == BTRFS_FILE_EXTENT_REG || |
3580 | found_type == BTRFS_FILE_EXTENT_PREALLOC) { | |
9036c102 YZ |
3581 | em->start = extent_start; |
3582 | em->len = extent_end - extent_start; | |
db94535d CM |
3583 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
3584 | if (bytenr == 0) { | |
5f39d397 | 3585 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 CM |
3586 | goto insert; |
3587 | } | |
c8b97818 CM |
3588 | if (compressed) { |
3589 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
3590 | em->block_start = bytenr; | |
3591 | em->block_len = btrfs_file_extent_disk_num_bytes(leaf, | |
3592 | item); | |
3593 | } else { | |
3594 | bytenr += btrfs_file_extent_offset(leaf, item); | |
3595 | em->block_start = bytenr; | |
3596 | em->block_len = em->len; | |
d899e052 YZ |
3597 | if (found_type == BTRFS_FILE_EXTENT_PREALLOC) |
3598 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); | |
c8b97818 | 3599 | } |
a52d9a80 CM |
3600 | goto insert; |
3601 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 3602 | unsigned long ptr; |
a52d9a80 | 3603 | char *map; |
3326d1b0 CM |
3604 | size_t size; |
3605 | size_t extent_offset; | |
3606 | size_t copy_size; | |
a52d9a80 | 3607 | |
689f9346 | 3608 | em->block_start = EXTENT_MAP_INLINE; |
c8b97818 | 3609 | if (!page || create) { |
689f9346 | 3610 | em->start = extent_start; |
9036c102 | 3611 | em->len = extent_end - extent_start; |
689f9346 Y |
3612 | goto out; |
3613 | } | |
5f39d397 | 3614 | |
9036c102 YZ |
3615 | size = btrfs_file_extent_inline_len(leaf, item); |
3616 | extent_offset = page_offset(page) + pg_offset - extent_start; | |
70dec807 | 3617 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, |
3326d1b0 | 3618 | size - extent_offset); |
3326d1b0 | 3619 | em->start = extent_start + extent_offset; |
70dec807 CM |
3620 | em->len = (copy_size + root->sectorsize - 1) & |
3621 | ~((u64)root->sectorsize - 1); | |
c8b97818 CM |
3622 | if (compressed) |
3623 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); | |
689f9346 | 3624 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; |
179e29e4 | 3625 | if (create == 0 && !PageUptodate(page)) { |
c8b97818 CM |
3626 | if (btrfs_file_extent_compression(leaf, item) == |
3627 | BTRFS_COMPRESS_ZLIB) { | |
3628 | ret = uncompress_inline(path, inode, page, | |
3629 | pg_offset, | |
3630 | extent_offset, item); | |
3631 | BUG_ON(ret); | |
3632 | } else { | |
3633 | map = kmap(page); | |
3634 | read_extent_buffer(leaf, map + pg_offset, ptr, | |
3635 | copy_size); | |
3636 | kunmap(page); | |
3637 | } | |
179e29e4 CM |
3638 | flush_dcache_page(page); |
3639 | } else if (create && PageUptodate(page)) { | |
3640 | if (!trans) { | |
3641 | kunmap(page); | |
3642 | free_extent_map(em); | |
3643 | em = NULL; | |
3644 | btrfs_release_path(root, path); | |
f9295749 | 3645 | trans = btrfs_join_transaction(root, 1); |
179e29e4 CM |
3646 | goto again; |
3647 | } | |
c8b97818 | 3648 | map = kmap(page); |
70dec807 | 3649 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 | 3650 | copy_size); |
c8b97818 | 3651 | kunmap(page); |
179e29e4 | 3652 | btrfs_mark_buffer_dirty(leaf); |
a52d9a80 | 3653 | } |
d1310b2e CM |
3654 | set_extent_uptodate(io_tree, em->start, |
3655 | extent_map_end(em) - 1, GFP_NOFS); | |
a52d9a80 CM |
3656 | goto insert; |
3657 | } else { | |
3658 | printk("unkknown found_type %d\n", found_type); | |
3659 | WARN_ON(1); | |
3660 | } | |
3661 | not_found: | |
3662 | em->start = start; | |
d1310b2e | 3663 | em->len = len; |
a52d9a80 | 3664 | not_found_em: |
5f39d397 | 3665 | em->block_start = EXTENT_MAP_HOLE; |
9036c102 | 3666 | set_bit(EXTENT_FLAG_VACANCY, &em->flags); |
a52d9a80 CM |
3667 | insert: |
3668 | btrfs_release_path(root, path); | |
d1310b2e CM |
3669 | if (em->start > start || extent_map_end(em) <= start) { |
3670 | printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len); | |
a52d9a80 CM |
3671 | err = -EIO; |
3672 | goto out; | |
3673 | } | |
d1310b2e CM |
3674 | |
3675 | err = 0; | |
3676 | spin_lock(&em_tree->lock); | |
a52d9a80 | 3677 | ret = add_extent_mapping(em_tree, em); |
3b951516 CM |
3678 | /* it is possible that someone inserted the extent into the tree |
3679 | * while we had the lock dropped. It is also possible that | |
3680 | * an overlapping map exists in the tree | |
3681 | */ | |
a52d9a80 | 3682 | if (ret == -EEXIST) { |
3b951516 | 3683 | struct extent_map *existing; |
e6dcd2dc CM |
3684 | |
3685 | ret = 0; | |
3686 | ||
3b951516 | 3687 | existing = lookup_extent_mapping(em_tree, start, len); |
e1c4b745 CM |
3688 | if (existing && (existing->start > start || |
3689 | existing->start + existing->len <= start)) { | |
3690 | free_extent_map(existing); | |
3691 | existing = NULL; | |
3692 | } | |
3b951516 CM |
3693 | if (!existing) { |
3694 | existing = lookup_extent_mapping(em_tree, em->start, | |
3695 | em->len); | |
3696 | if (existing) { | |
3697 | err = merge_extent_mapping(em_tree, existing, | |
e6dcd2dc CM |
3698 | em, start, |
3699 | root->sectorsize); | |
3b951516 CM |
3700 | free_extent_map(existing); |
3701 | if (err) { | |
3702 | free_extent_map(em); | |
3703 | em = NULL; | |
3704 | } | |
3705 | } else { | |
3706 | err = -EIO; | |
3707 | printk("failing to insert %Lu %Lu\n", | |
3708 | start, len); | |
3709 | free_extent_map(em); | |
3710 | em = NULL; | |
3711 | } | |
3712 | } else { | |
3713 | free_extent_map(em); | |
3714 | em = existing; | |
e6dcd2dc | 3715 | err = 0; |
a52d9a80 | 3716 | } |
a52d9a80 | 3717 | } |
d1310b2e | 3718 | spin_unlock(&em_tree->lock); |
a52d9a80 | 3719 | out: |
f421950f CM |
3720 | if (path) |
3721 | btrfs_free_path(path); | |
a52d9a80 CM |
3722 | if (trans) { |
3723 | ret = btrfs_end_transaction(trans, root); | |
e6dcd2dc | 3724 | if (!err) { |
a52d9a80 | 3725 | err = ret; |
e6dcd2dc | 3726 | } |
a52d9a80 | 3727 | } |
a52d9a80 CM |
3728 | if (err) { |
3729 | free_extent_map(em); | |
3730 | WARN_ON(1); | |
3731 | return ERR_PTR(err); | |
3732 | } | |
3733 | return em; | |
3734 | } | |
3735 | ||
16432985 CM |
3736 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
3737 | const struct iovec *iov, loff_t offset, | |
3738 | unsigned long nr_segs) | |
3739 | { | |
e1c4b745 | 3740 | return -EINVAL; |
16432985 CM |
3741 | } |
3742 | ||
d396c6f5 | 3743 | static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock) |
39279cc3 | 3744 | { |
d396c6f5 | 3745 | return extent_bmap(mapping, iblock, btrfs_get_extent); |
39279cc3 CM |
3746 | } |
3747 | ||
a52d9a80 | 3748 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 3749 | { |
d1310b2e CM |
3750 | struct extent_io_tree *tree; |
3751 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
a52d9a80 | 3752 | return extent_read_full_page(tree, page, btrfs_get_extent); |
9ebefb18 | 3753 | } |
1832a6d5 | 3754 | |
a52d9a80 | 3755 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 3756 | { |
d1310b2e | 3757 | struct extent_io_tree *tree; |
b888db2b CM |
3758 | |
3759 | ||
3760 | if (current->flags & PF_MEMALLOC) { | |
3761 | redirty_page_for_writepage(wbc, page); | |
3762 | unlock_page(page); | |
3763 | return 0; | |
3764 | } | |
d1310b2e | 3765 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
a52d9a80 | 3766 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
9ebefb18 CM |
3767 | } |
3768 | ||
f421950f CM |
3769 | int btrfs_writepages(struct address_space *mapping, |
3770 | struct writeback_control *wbc) | |
b293f02e | 3771 | { |
d1310b2e CM |
3772 | struct extent_io_tree *tree; |
3773 | tree = &BTRFS_I(mapping->host)->io_tree; | |
b293f02e CM |
3774 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
3775 | } | |
3776 | ||
3ab2fb5a CM |
3777 | static int |
3778 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
3779 | struct list_head *pages, unsigned nr_pages) | |
3780 | { | |
d1310b2e CM |
3781 | struct extent_io_tree *tree; |
3782 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
3783 | return extent_readpages(tree, mapping, pages, nr_pages, |
3784 | btrfs_get_extent); | |
3785 | } | |
e6dcd2dc | 3786 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 3787 | { |
d1310b2e CM |
3788 | struct extent_io_tree *tree; |
3789 | struct extent_map_tree *map; | |
a52d9a80 | 3790 | int ret; |
8c2383c3 | 3791 | |
d1310b2e CM |
3792 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
3793 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 3794 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
3795 | if (ret == 1) { |
3796 | ClearPagePrivate(page); | |
3797 | set_page_private(page, 0); | |
3798 | page_cache_release(page); | |
39279cc3 | 3799 | } |
a52d9a80 | 3800 | return ret; |
39279cc3 CM |
3801 | } |
3802 | ||
e6dcd2dc CM |
3803 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
3804 | { | |
98509cfc CM |
3805 | if (PageWriteback(page) || PageDirty(page)) |
3806 | return 0; | |
e6dcd2dc CM |
3807 | return __btrfs_releasepage(page, gfp_flags); |
3808 | } | |
3809 | ||
a52d9a80 | 3810 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
39279cc3 | 3811 | { |
d1310b2e | 3812 | struct extent_io_tree *tree; |
e6dcd2dc CM |
3813 | struct btrfs_ordered_extent *ordered; |
3814 | u64 page_start = page_offset(page); | |
3815 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; | |
39279cc3 | 3816 | |
e6dcd2dc | 3817 | wait_on_page_writeback(page); |
d1310b2e | 3818 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
e6dcd2dc CM |
3819 | if (offset) { |
3820 | btrfs_releasepage(page, GFP_NOFS); | |
3821 | return; | |
3822 | } | |
3823 | ||
3824 | lock_extent(tree, page_start, page_end, GFP_NOFS); | |
3825 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, | |
3826 | page_offset(page)); | |
3827 | if (ordered) { | |
eb84ae03 CM |
3828 | /* |
3829 | * IO on this page will never be started, so we need | |
3830 | * to account for any ordered extents now | |
3831 | */ | |
e6dcd2dc CM |
3832 | clear_extent_bit(tree, page_start, page_end, |
3833 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
3834 | EXTENT_LOCKED, 1, 0, GFP_NOFS); | |
211f90e6 CM |
3835 | btrfs_finish_ordered_io(page->mapping->host, |
3836 | page_start, page_end); | |
e6dcd2dc CM |
3837 | btrfs_put_ordered_extent(ordered); |
3838 | lock_extent(tree, page_start, page_end, GFP_NOFS); | |
3839 | } | |
3840 | clear_extent_bit(tree, page_start, page_end, | |
3841 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | | |
3842 | EXTENT_ORDERED, | |
3843 | 1, 1, GFP_NOFS); | |
3844 | __btrfs_releasepage(page, GFP_NOFS); | |
3845 | ||
4a096752 | 3846 | ClearPageChecked(page); |
9ad6b7bc | 3847 | if (PagePrivate(page)) { |
9ad6b7bc CM |
3848 | ClearPagePrivate(page); |
3849 | set_page_private(page, 0); | |
3850 | page_cache_release(page); | |
3851 | } | |
39279cc3 CM |
3852 | } |
3853 | ||
9ebefb18 CM |
3854 | /* |
3855 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
3856 | * called from a page fault handler when a page is first dirtied. Hence we must | |
3857 | * be careful to check for EOF conditions here. We set the page up correctly | |
3858 | * for a written page which means we get ENOSPC checking when writing into | |
3859 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
3860 | * support these features. | |
3861 | * | |
3862 | * We are not allowed to take the i_mutex here so we have to play games to | |
3863 | * protect against truncate races as the page could now be beyond EOF. Because | |
3864 | * vmtruncate() writes the inode size before removing pages, once we have the | |
3865 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
3866 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
3867 | * unlock the page. | |
3868 | */ | |
3869 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) | |
3870 | { | |
6da6abae | 3871 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
1832a6d5 | 3872 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
3873 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
3874 | struct btrfs_ordered_extent *ordered; | |
3875 | char *kaddr; | |
3876 | unsigned long zero_start; | |
9ebefb18 | 3877 | loff_t size; |
1832a6d5 | 3878 | int ret; |
a52d9a80 | 3879 | u64 page_start; |
e6dcd2dc | 3880 | u64 page_end; |
9ebefb18 | 3881 | |
1832a6d5 | 3882 | ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0); |
1832a6d5 CM |
3883 | if (ret) |
3884 | goto out; | |
3885 | ||
3886 | ret = -EINVAL; | |
e6dcd2dc | 3887 | again: |
9ebefb18 | 3888 | lock_page(page); |
9ebefb18 | 3889 | size = i_size_read(inode); |
e6dcd2dc CM |
3890 | page_start = page_offset(page); |
3891 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
a52d9a80 | 3892 | |
9ebefb18 | 3893 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 3894 | (page_start >= size)) { |
9ebefb18 CM |
3895 | /* page got truncated out from underneath us */ |
3896 | goto out_unlock; | |
3897 | } | |
e6dcd2dc CM |
3898 | wait_on_page_writeback(page); |
3899 | ||
3900 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
3901 | set_page_extent_mapped(page); | |
3902 | ||
eb84ae03 CM |
3903 | /* |
3904 | * we can't set the delalloc bits if there are pending ordered | |
3905 | * extents. Drop our locks and wait for them to finish | |
3906 | */ | |
e6dcd2dc CM |
3907 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
3908 | if (ordered) { | |
3909 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
3910 | unlock_page(page); | |
eb84ae03 | 3911 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
3912 | btrfs_put_ordered_extent(ordered); |
3913 | goto again; | |
3914 | } | |
3915 | ||
ea8c2819 | 3916 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
e6dcd2dc | 3917 | ret = 0; |
9ebefb18 CM |
3918 | |
3919 | /* page is wholly or partially inside EOF */ | |
a52d9a80 | 3920 | if (page_start + PAGE_CACHE_SIZE > size) |
e6dcd2dc | 3921 | zero_start = size & ~PAGE_CACHE_MASK; |
9ebefb18 | 3922 | else |
e6dcd2dc | 3923 | zero_start = PAGE_CACHE_SIZE; |
9ebefb18 | 3924 | |
e6dcd2dc CM |
3925 | if (zero_start != PAGE_CACHE_SIZE) { |
3926 | kaddr = kmap(page); | |
3927 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); | |
3928 | flush_dcache_page(page); | |
3929 | kunmap(page); | |
3930 | } | |
247e743c | 3931 | ClearPageChecked(page); |
e6dcd2dc CM |
3932 | set_page_dirty(page); |
3933 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
9ebefb18 CM |
3934 | |
3935 | out_unlock: | |
3936 | unlock_page(page); | |
1832a6d5 | 3937 | out: |
9ebefb18 CM |
3938 | return ret; |
3939 | } | |
3940 | ||
39279cc3 CM |
3941 | static void btrfs_truncate(struct inode *inode) |
3942 | { | |
3943 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3944 | int ret; | |
3945 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 3946 | unsigned long nr; |
dbe674a9 | 3947 | u64 mask = root->sectorsize - 1; |
39279cc3 CM |
3948 | |
3949 | if (!S_ISREG(inode->i_mode)) | |
3950 | return; | |
3951 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
3952 | return; | |
3953 | ||
3954 | btrfs_truncate_page(inode->i_mapping, inode->i_size); | |
4a096752 | 3955 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
39279cc3 | 3956 | |
39279cc3 CM |
3957 | trans = btrfs_start_transaction(root, 1); |
3958 | btrfs_set_trans_block_group(trans, inode); | |
dbe674a9 | 3959 | btrfs_i_size_write(inode, inode->i_size); |
39279cc3 | 3960 | |
7b128766 JB |
3961 | ret = btrfs_orphan_add(trans, inode); |
3962 | if (ret) | |
3963 | goto out; | |
39279cc3 | 3964 | /* FIXME, add redo link to tree so we don't leak on crash */ |
e02119d5 | 3965 | ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, |
85e21bac | 3966 | BTRFS_EXTENT_DATA_KEY); |
39279cc3 | 3967 | btrfs_update_inode(trans, root, inode); |
5f39d397 | 3968 | |
7b128766 JB |
3969 | ret = btrfs_orphan_del(trans, inode); |
3970 | BUG_ON(ret); | |
3971 | ||
3972 | out: | |
3973 | nr = trans->blocks_used; | |
89ce8a63 | 3974 | ret = btrfs_end_transaction_throttle(trans, root); |
39279cc3 | 3975 | BUG_ON(ret); |
d3c2fdcf | 3976 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
3977 | } |
3978 | ||
3b96362c SW |
3979 | /* |
3980 | * Invalidate a single dcache entry at the root of the filesystem. | |
3981 | * Needed after creation of snapshot or subvolume. | |
3982 | */ | |
3983 | void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name, | |
3984 | int namelen) | |
3985 | { | |
3986 | struct dentry *alias, *entry; | |
3987 | struct qstr qstr; | |
3988 | ||
3989 | alias = d_find_alias(root->fs_info->sb->s_root->d_inode); | |
3990 | if (alias) { | |
3991 | qstr.name = name; | |
3992 | qstr.len = namelen; | |
3993 | /* change me if btrfs ever gets a d_hash operation */ | |
3994 | qstr.hash = full_name_hash(qstr.name, qstr.len); | |
3995 | entry = d_lookup(alias, &qstr); | |
3996 | dput(alias); | |
3997 | if (entry) { | |
3998 | d_invalidate(entry); | |
3999 | dput(entry); | |
4000 | } | |
4001 | } | |
4002 | } | |
4003 | ||
d352ac68 CM |
4004 | /* |
4005 | * create a new subvolume directory/inode (helper for the ioctl). | |
4006 | */ | |
cb8e7090 | 4007 | int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry, |
f46b5a66 CH |
4008 | struct btrfs_trans_handle *trans, u64 new_dirid, |
4009 | struct btrfs_block_group_cache *block_group) | |
39279cc3 | 4010 | { |
39279cc3 | 4011 | struct inode *inode; |
cb8e7090 | 4012 | int error; |
00e4e6b3 | 4013 | u64 index = 0; |
39279cc3 | 4014 | |
aec7477b | 4015 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
00e4e6b3 | 4016 | new_dirid, block_group, S_IFDIR | 0700, &index); |
54aa1f4d | 4017 | if (IS_ERR(inode)) |
f46b5a66 | 4018 | return PTR_ERR(inode); |
39279cc3 CM |
4019 | inode->i_op = &btrfs_dir_inode_operations; |
4020 | inode->i_fop = &btrfs_dir_file_operations; | |
34088780 | 4021 | new_root->inode = inode; |
39279cc3 | 4022 | |
39279cc3 | 4023 | inode->i_nlink = 1; |
dbe674a9 | 4024 | btrfs_i_size_write(inode, 0); |
3b96362c | 4025 | |
cb8e7090 CH |
4026 | error = btrfs_update_inode(trans, new_root, inode); |
4027 | if (error) | |
4028 | return error; | |
4029 | ||
d899e052 | 4030 | atomic_inc(&inode->i_count); |
cb8e7090 CH |
4031 | d_instantiate(dentry, inode); |
4032 | return 0; | |
39279cc3 CM |
4033 | } |
4034 | ||
d352ac68 CM |
4035 | /* helper function for file defrag and space balancing. This |
4036 | * forces readahead on a given range of bytes in an inode | |
4037 | */ | |
edbd8d4e | 4038 | unsigned long btrfs_force_ra(struct address_space *mapping, |
86479a04 CM |
4039 | struct file_ra_state *ra, struct file *file, |
4040 | pgoff_t offset, pgoff_t last_index) | |
4041 | { | |
8e7bf94f | 4042 | pgoff_t req_size = last_index - offset + 1; |
86479a04 | 4043 | |
86479a04 CM |
4044 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
4045 | return offset + req_size; | |
86479a04 CM |
4046 | } |
4047 | ||
39279cc3 CM |
4048 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
4049 | { | |
4050 | struct btrfs_inode *ei; | |
4051 | ||
4052 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
4053 | if (!ei) | |
4054 | return NULL; | |
15ee9bc7 | 4055 | ei->last_trans = 0; |
e02119d5 | 4056 | ei->logged_trans = 0; |
e6dcd2dc | 4057 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
33268eaf JB |
4058 | ei->i_acl = BTRFS_ACL_NOT_CACHED; |
4059 | ei->i_default_acl = BTRFS_ACL_NOT_CACHED; | |
7b128766 | 4060 | INIT_LIST_HEAD(&ei->i_orphan); |
39279cc3 CM |
4061 | return &ei->vfs_inode; |
4062 | } | |
4063 | ||
4064 | void btrfs_destroy_inode(struct inode *inode) | |
4065 | { | |
e6dcd2dc | 4066 | struct btrfs_ordered_extent *ordered; |
39279cc3 CM |
4067 | WARN_ON(!list_empty(&inode->i_dentry)); |
4068 | WARN_ON(inode->i_data.nrpages); | |
4069 | ||
33268eaf JB |
4070 | if (BTRFS_I(inode)->i_acl && |
4071 | BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED) | |
4072 | posix_acl_release(BTRFS_I(inode)->i_acl); | |
4073 | if (BTRFS_I(inode)->i_default_acl && | |
4074 | BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED) | |
4075 | posix_acl_release(BTRFS_I(inode)->i_default_acl); | |
4076 | ||
bcc63abb | 4077 | spin_lock(&BTRFS_I(inode)->root->list_lock); |
7b128766 JB |
4078 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
4079 | printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan" | |
4080 | " list\n", inode->i_ino); | |
4081 | dump_stack(); | |
4082 | } | |
bcc63abb | 4083 | spin_unlock(&BTRFS_I(inode)->root->list_lock); |
7b128766 | 4084 | |
e6dcd2dc CM |
4085 | while(1) { |
4086 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); | |
4087 | if (!ordered) | |
4088 | break; | |
4089 | else { | |
4090 | printk("found ordered extent %Lu %Lu\n", | |
4091 | ordered->file_offset, ordered->len); | |
4092 | btrfs_remove_ordered_extent(inode, ordered); | |
4093 | btrfs_put_ordered_extent(ordered); | |
4094 | btrfs_put_ordered_extent(ordered); | |
4095 | } | |
4096 | } | |
5b21f2ed | 4097 | btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); |
39279cc3 CM |
4098 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
4099 | } | |
4100 | ||
0ee0fda0 | 4101 | static void init_once(void *foo) |
39279cc3 CM |
4102 | { |
4103 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
4104 | ||
4105 | inode_init_once(&ei->vfs_inode); | |
4106 | } | |
4107 | ||
4108 | void btrfs_destroy_cachep(void) | |
4109 | { | |
4110 | if (btrfs_inode_cachep) | |
4111 | kmem_cache_destroy(btrfs_inode_cachep); | |
4112 | if (btrfs_trans_handle_cachep) | |
4113 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
4114 | if (btrfs_transaction_cachep) | |
4115 | kmem_cache_destroy(btrfs_transaction_cachep); | |
4116 | if (btrfs_bit_radix_cachep) | |
4117 | kmem_cache_destroy(btrfs_bit_radix_cachep); | |
4118 | if (btrfs_path_cachep) | |
4119 | kmem_cache_destroy(btrfs_path_cachep); | |
4120 | } | |
4121 | ||
86479a04 | 4122 | struct kmem_cache *btrfs_cache_create(const char *name, size_t size, |
92fee66d | 4123 | unsigned long extra_flags, |
2b1f55b0 | 4124 | void (*ctor)(void *)) |
92fee66d CM |
4125 | { |
4126 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | | |
2b1f55b0 | 4127 | SLAB_MEM_SPREAD | extra_flags), ctor); |
92fee66d CM |
4128 | } |
4129 | ||
39279cc3 CM |
4130 | int btrfs_init_cachep(void) |
4131 | { | |
86479a04 | 4132 | btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache", |
92fee66d CM |
4133 | sizeof(struct btrfs_inode), |
4134 | 0, init_once); | |
39279cc3 CM |
4135 | if (!btrfs_inode_cachep) |
4136 | goto fail; | |
86479a04 CM |
4137 | btrfs_trans_handle_cachep = |
4138 | btrfs_cache_create("btrfs_trans_handle_cache", | |
4139 | sizeof(struct btrfs_trans_handle), | |
4140 | 0, NULL); | |
39279cc3 CM |
4141 | if (!btrfs_trans_handle_cachep) |
4142 | goto fail; | |
86479a04 | 4143 | btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache", |
39279cc3 | 4144 | sizeof(struct btrfs_transaction), |
92fee66d | 4145 | 0, NULL); |
39279cc3 CM |
4146 | if (!btrfs_transaction_cachep) |
4147 | goto fail; | |
86479a04 | 4148 | btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache", |
23223584 | 4149 | sizeof(struct btrfs_path), |
92fee66d | 4150 | 0, NULL); |
39279cc3 CM |
4151 | if (!btrfs_path_cachep) |
4152 | goto fail; | |
86479a04 | 4153 | btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256, |
92fee66d | 4154 | SLAB_DESTROY_BY_RCU, NULL); |
39279cc3 CM |
4155 | if (!btrfs_bit_radix_cachep) |
4156 | goto fail; | |
4157 | return 0; | |
4158 | fail: | |
4159 | btrfs_destroy_cachep(); | |
4160 | return -ENOMEM; | |
4161 | } | |
4162 | ||
4163 | static int btrfs_getattr(struct vfsmount *mnt, | |
4164 | struct dentry *dentry, struct kstat *stat) | |
4165 | { | |
4166 | struct inode *inode = dentry->d_inode; | |
4167 | generic_fillattr(inode, stat); | |
d6667462 | 4168 | stat->blksize = PAGE_CACHE_SIZE; |
a76a3cd4 YZ |
4169 | stat->blocks = (inode_get_bytes(inode) + |
4170 | BTRFS_I(inode)->delalloc_bytes) >> 9; | |
39279cc3 CM |
4171 | return 0; |
4172 | } | |
4173 | ||
4174 | static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, | |
4175 | struct inode * new_dir,struct dentry *new_dentry) | |
4176 | { | |
4177 | struct btrfs_trans_handle *trans; | |
4178 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
4179 | struct inode *new_inode = new_dentry->d_inode; | |
4180 | struct inode *old_inode = old_dentry->d_inode; | |
4181 | struct timespec ctime = CURRENT_TIME; | |
00e4e6b3 | 4182 | u64 index = 0; |
39279cc3 CM |
4183 | int ret; |
4184 | ||
4185 | if (S_ISDIR(old_inode->i_mode) && new_inode && | |
4186 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { | |
4187 | return -ENOTEMPTY; | |
4188 | } | |
5f39d397 | 4189 | |
1832a6d5 CM |
4190 | ret = btrfs_check_free_space(root, 1, 0); |
4191 | if (ret) | |
4192 | goto out_unlock; | |
4193 | ||
39279cc3 | 4194 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 4195 | |
39279cc3 | 4196 | btrfs_set_trans_block_group(trans, new_dir); |
39279cc3 | 4197 | |
e02119d5 | 4198 | btrfs_inc_nlink(old_dentry->d_inode); |
39279cc3 CM |
4199 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
4200 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
4201 | old_inode->i_ctime = ctime; | |
5f39d397 | 4202 | |
e02119d5 CM |
4203 | ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode, |
4204 | old_dentry->d_name.name, | |
4205 | old_dentry->d_name.len); | |
39279cc3 CM |
4206 | if (ret) |
4207 | goto out_fail; | |
4208 | ||
4209 | if (new_inode) { | |
4210 | new_inode->i_ctime = CURRENT_TIME; | |
e02119d5 CM |
4211 | ret = btrfs_unlink_inode(trans, root, new_dir, |
4212 | new_dentry->d_inode, | |
4213 | new_dentry->d_name.name, | |
4214 | new_dentry->d_name.len); | |
39279cc3 CM |
4215 | if (ret) |
4216 | goto out_fail; | |
7b128766 | 4217 | if (new_inode->i_nlink == 0) { |
e02119d5 | 4218 | ret = btrfs_orphan_add(trans, new_dentry->d_inode); |
7b128766 JB |
4219 | if (ret) |
4220 | goto out_fail; | |
4221 | } | |
e02119d5 | 4222 | |
39279cc3 | 4223 | } |
00e4e6b3 | 4224 | ret = btrfs_set_inode_index(new_dir, old_inode, &index); |
aec7477b JB |
4225 | if (ret) |
4226 | goto out_fail; | |
4227 | ||
e02119d5 CM |
4228 | ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode, |
4229 | old_inode, new_dentry->d_name.name, | |
4230 | new_dentry->d_name.len, 1, index); | |
39279cc3 CM |
4231 | if (ret) |
4232 | goto out_fail; | |
4233 | ||
4234 | out_fail: | |
ab78c84d | 4235 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 4236 | out_unlock: |
39279cc3 CM |
4237 | return ret; |
4238 | } | |
4239 | ||
d352ac68 CM |
4240 | /* |
4241 | * some fairly slow code that needs optimization. This walks the list | |
4242 | * of all the inodes with pending delalloc and forces them to disk. | |
4243 | */ | |
ea8c2819 CM |
4244 | int btrfs_start_delalloc_inodes(struct btrfs_root *root) |
4245 | { | |
4246 | struct list_head *head = &root->fs_info->delalloc_inodes; | |
4247 | struct btrfs_inode *binode; | |
5b21f2ed | 4248 | struct inode *inode; |
ea8c2819 CM |
4249 | unsigned long flags; |
4250 | ||
4251 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); | |
4252 | while(!list_empty(head)) { | |
4253 | binode = list_entry(head->next, struct btrfs_inode, | |
4254 | delalloc_inodes); | |
5b21f2ed ZY |
4255 | inode = igrab(&binode->vfs_inode); |
4256 | if (!inode) | |
4257 | list_del_init(&binode->delalloc_inodes); | |
ea8c2819 | 4258 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
5b21f2ed | 4259 | if (inode) { |
8c8bee1d | 4260 | filemap_flush(inode->i_mapping); |
5b21f2ed ZY |
4261 | iput(inode); |
4262 | } | |
4263 | cond_resched(); | |
ea8c2819 CM |
4264 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
4265 | } | |
4266 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); | |
8c8bee1d CM |
4267 | |
4268 | /* the filemap_flush will queue IO into the worker threads, but | |
4269 | * we have to make sure the IO is actually started and that | |
4270 | * ordered extents get created before we return | |
4271 | */ | |
4272 | atomic_inc(&root->fs_info->async_submit_draining); | |
4273 | while(atomic_read(&root->fs_info->nr_async_submits)) { | |
4274 | wait_event(root->fs_info->async_submit_wait, | |
4275 | (atomic_read(&root->fs_info->nr_async_submits) == 0)); | |
4276 | } | |
4277 | atomic_dec(&root->fs_info->async_submit_draining); | |
ea8c2819 CM |
4278 | return 0; |
4279 | } | |
4280 | ||
39279cc3 CM |
4281 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
4282 | const char *symname) | |
4283 | { | |
4284 | struct btrfs_trans_handle *trans; | |
4285 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
4286 | struct btrfs_path *path; | |
4287 | struct btrfs_key key; | |
1832a6d5 | 4288 | struct inode *inode = NULL; |
39279cc3 CM |
4289 | int err; |
4290 | int drop_inode = 0; | |
4291 | u64 objectid; | |
00e4e6b3 | 4292 | u64 index = 0 ; |
39279cc3 CM |
4293 | int name_len; |
4294 | int datasize; | |
5f39d397 | 4295 | unsigned long ptr; |
39279cc3 | 4296 | struct btrfs_file_extent_item *ei; |
5f39d397 | 4297 | struct extent_buffer *leaf; |
1832a6d5 | 4298 | unsigned long nr = 0; |
39279cc3 CM |
4299 | |
4300 | name_len = strlen(symname) + 1; | |
4301 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) | |
4302 | return -ENAMETOOLONG; | |
1832a6d5 | 4303 | |
1832a6d5 CM |
4304 | err = btrfs_check_free_space(root, 1, 0); |
4305 | if (err) | |
4306 | goto out_fail; | |
4307 | ||
39279cc3 CM |
4308 | trans = btrfs_start_transaction(root, 1); |
4309 | btrfs_set_trans_block_group(trans, dir); | |
4310 | ||
4311 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
4312 | if (err) { | |
4313 | err = -ENOSPC; | |
4314 | goto out_unlock; | |
4315 | } | |
4316 | ||
aec7477b | 4317 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
9c58309d CM |
4318 | dentry->d_name.len, |
4319 | dentry->d_parent->d_inode->i_ino, objectid, | |
00e4e6b3 CM |
4320 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
4321 | &index); | |
39279cc3 CM |
4322 | err = PTR_ERR(inode); |
4323 | if (IS_ERR(inode)) | |
4324 | goto out_unlock; | |
4325 | ||
33268eaf JB |
4326 | err = btrfs_init_acl(inode, dir); |
4327 | if (err) { | |
4328 | drop_inode = 1; | |
4329 | goto out_unlock; | |
4330 | } | |
4331 | ||
39279cc3 | 4332 | btrfs_set_trans_block_group(trans, inode); |
00e4e6b3 | 4333 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
39279cc3 CM |
4334 | if (err) |
4335 | drop_inode = 1; | |
4336 | else { | |
4337 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 4338 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 CM |
4339 | inode->i_fop = &btrfs_file_operations; |
4340 | inode->i_op = &btrfs_file_inode_operations; | |
d1310b2e | 4341 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
4342 | } |
4343 | dir->i_sb->s_dirt = 1; | |
4344 | btrfs_update_inode_block_group(trans, inode); | |
4345 | btrfs_update_inode_block_group(trans, dir); | |
4346 | if (drop_inode) | |
4347 | goto out_unlock; | |
4348 | ||
4349 | path = btrfs_alloc_path(); | |
4350 | BUG_ON(!path); | |
4351 | key.objectid = inode->i_ino; | |
4352 | key.offset = 0; | |
39279cc3 CM |
4353 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
4354 | datasize = btrfs_file_extent_calc_inline_size(name_len); | |
4355 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
4356 | datasize); | |
54aa1f4d CM |
4357 | if (err) { |
4358 | drop_inode = 1; | |
4359 | goto out_unlock; | |
4360 | } | |
5f39d397 CM |
4361 | leaf = path->nodes[0]; |
4362 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
4363 | struct btrfs_file_extent_item); | |
4364 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
4365 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 | 4366 | BTRFS_FILE_EXTENT_INLINE); |
c8b97818 CM |
4367 | btrfs_set_file_extent_encryption(leaf, ei, 0); |
4368 | btrfs_set_file_extent_compression(leaf, ei, 0); | |
4369 | btrfs_set_file_extent_other_encoding(leaf, ei, 0); | |
4370 | btrfs_set_file_extent_ram_bytes(leaf, ei, name_len); | |
4371 | ||
39279cc3 | 4372 | ptr = btrfs_file_extent_inline_start(ei); |
5f39d397 CM |
4373 | write_extent_buffer(leaf, symname, ptr, name_len); |
4374 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 4375 | btrfs_free_path(path); |
5f39d397 | 4376 | |
39279cc3 CM |
4377 | inode->i_op = &btrfs_symlink_inode_operations; |
4378 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
04160088 | 4379 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
d899e052 | 4380 | inode_set_bytes(inode, name_len); |
dbe674a9 | 4381 | btrfs_i_size_write(inode, name_len - 1); |
54aa1f4d CM |
4382 | err = btrfs_update_inode(trans, root, inode); |
4383 | if (err) | |
4384 | drop_inode = 1; | |
39279cc3 CM |
4385 | |
4386 | out_unlock: | |
d3c2fdcf | 4387 | nr = trans->blocks_used; |
ab78c84d | 4388 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 4389 | out_fail: |
39279cc3 CM |
4390 | if (drop_inode) { |
4391 | inode_dec_link_count(inode); | |
4392 | iput(inode); | |
4393 | } | |
d3c2fdcf | 4394 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
4395 | return err; |
4396 | } | |
16432985 | 4397 | |
d899e052 YZ |
4398 | static int prealloc_file_range(struct inode *inode, u64 start, u64 end, |
4399 | u64 alloc_hint, int mode) | |
4400 | { | |
4401 | struct btrfs_trans_handle *trans; | |
4402 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4403 | struct btrfs_key ins; | |
4404 | u64 alloc_size; | |
4405 | u64 cur_offset = start; | |
4406 | u64 num_bytes = end - start; | |
4407 | int ret = 0; | |
4408 | ||
4409 | trans = btrfs_join_transaction(root, 1); | |
4410 | BUG_ON(!trans); | |
4411 | btrfs_set_trans_block_group(trans, inode); | |
4412 | ||
4413 | while (num_bytes > 0) { | |
4414 | alloc_size = min(num_bytes, root->fs_info->max_extent); | |
4415 | ret = btrfs_reserve_extent(trans, root, alloc_size, | |
4416 | root->sectorsize, 0, alloc_hint, | |
4417 | (u64)-1, &ins, 1); | |
4418 | if (ret) { | |
4419 | WARN_ON(1); | |
4420 | goto out; | |
4421 | } | |
4422 | ret = insert_reserved_file_extent(trans, inode, | |
4423 | cur_offset, ins.objectid, | |
4424 | ins.offset, ins.offset, | |
4425 | ins.offset, 0, 0, 0, | |
4426 | BTRFS_FILE_EXTENT_PREALLOC); | |
4427 | BUG_ON(ret); | |
4428 | num_bytes -= ins.offset; | |
4429 | cur_offset += ins.offset; | |
4430 | alloc_hint = ins.objectid + ins.offset; | |
4431 | } | |
4432 | out: | |
4433 | if (cur_offset > start) { | |
4434 | inode->i_ctime = CURRENT_TIME; | |
4435 | btrfs_set_flag(inode, PREALLOC); | |
4436 | if (!(mode & FALLOC_FL_KEEP_SIZE) && | |
4437 | cur_offset > i_size_read(inode)) | |
4438 | btrfs_i_size_write(inode, cur_offset); | |
4439 | ret = btrfs_update_inode(trans, root, inode); | |
4440 | BUG_ON(ret); | |
4441 | } | |
4442 | ||
4443 | btrfs_end_transaction(trans, root); | |
4444 | return ret; | |
4445 | } | |
4446 | ||
4447 | static long btrfs_fallocate(struct inode *inode, int mode, | |
4448 | loff_t offset, loff_t len) | |
4449 | { | |
4450 | u64 cur_offset; | |
4451 | u64 last_byte; | |
4452 | u64 alloc_start; | |
4453 | u64 alloc_end; | |
4454 | u64 alloc_hint = 0; | |
4455 | u64 mask = BTRFS_I(inode)->root->sectorsize - 1; | |
4456 | struct extent_map *em; | |
4457 | int ret; | |
4458 | ||
4459 | alloc_start = offset & ~mask; | |
4460 | alloc_end = (offset + len + mask) & ~mask; | |
4461 | ||
4462 | mutex_lock(&inode->i_mutex); | |
4463 | if (alloc_start > inode->i_size) { | |
4464 | ret = btrfs_cont_expand(inode, alloc_start); | |
4465 | if (ret) | |
4466 | goto out; | |
4467 | } | |
4468 | ||
4469 | while (1) { | |
4470 | struct btrfs_ordered_extent *ordered; | |
4471 | lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, | |
4472 | alloc_end - 1, GFP_NOFS); | |
4473 | ordered = btrfs_lookup_first_ordered_extent(inode, | |
4474 | alloc_end - 1); | |
4475 | if (ordered && | |
4476 | ordered->file_offset + ordered->len > alloc_start && | |
4477 | ordered->file_offset < alloc_end) { | |
4478 | btrfs_put_ordered_extent(ordered); | |
4479 | unlock_extent(&BTRFS_I(inode)->io_tree, | |
4480 | alloc_start, alloc_end - 1, GFP_NOFS); | |
4481 | btrfs_wait_ordered_range(inode, alloc_start, | |
4482 | alloc_end - alloc_start); | |
4483 | } else { | |
4484 | if (ordered) | |
4485 | btrfs_put_ordered_extent(ordered); | |
4486 | break; | |
4487 | } | |
4488 | } | |
4489 | ||
4490 | cur_offset = alloc_start; | |
4491 | while (1) { | |
4492 | em = btrfs_get_extent(inode, NULL, 0, cur_offset, | |
4493 | alloc_end - cur_offset, 0); | |
4494 | BUG_ON(IS_ERR(em) || !em); | |
4495 | last_byte = min(extent_map_end(em), alloc_end); | |
4496 | last_byte = (last_byte + mask) & ~mask; | |
4497 | if (em->block_start == EXTENT_MAP_HOLE) { | |
4498 | ret = prealloc_file_range(inode, cur_offset, | |
4499 | last_byte, alloc_hint, mode); | |
4500 | if (ret < 0) { | |
4501 | free_extent_map(em); | |
4502 | break; | |
4503 | } | |
4504 | } | |
4505 | if (em->block_start <= EXTENT_MAP_LAST_BYTE) | |
4506 | alloc_hint = em->block_start; | |
4507 | free_extent_map(em); | |
4508 | ||
4509 | cur_offset = last_byte; | |
4510 | if (cur_offset >= alloc_end) { | |
4511 | ret = 0; | |
4512 | break; | |
4513 | } | |
4514 | } | |
4515 | unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1, | |
4516 | GFP_NOFS); | |
4517 | out: | |
4518 | mutex_unlock(&inode->i_mutex); | |
4519 | return ret; | |
4520 | } | |
4521 | ||
e6dcd2dc CM |
4522 | static int btrfs_set_page_dirty(struct page *page) |
4523 | { | |
e6dcd2dc CM |
4524 | return __set_page_dirty_nobuffers(page); |
4525 | } | |
4526 | ||
0ee0fda0 | 4527 | static int btrfs_permission(struct inode *inode, int mask) |
fdebe2bd Y |
4528 | { |
4529 | if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE)) | |
4530 | return -EACCES; | |
33268eaf | 4531 | return generic_permission(inode, mask, btrfs_check_acl); |
fdebe2bd | 4532 | } |
39279cc3 CM |
4533 | |
4534 | static struct inode_operations btrfs_dir_inode_operations = { | |
4535 | .lookup = btrfs_lookup, | |
4536 | .create = btrfs_create, | |
4537 | .unlink = btrfs_unlink, | |
4538 | .link = btrfs_link, | |
4539 | .mkdir = btrfs_mkdir, | |
4540 | .rmdir = btrfs_rmdir, | |
4541 | .rename = btrfs_rename, | |
4542 | .symlink = btrfs_symlink, | |
4543 | .setattr = btrfs_setattr, | |
618e21d5 | 4544 | .mknod = btrfs_mknod, |
95819c05 CH |
4545 | .setxattr = btrfs_setxattr, |
4546 | .getxattr = btrfs_getxattr, | |
5103e947 | 4547 | .listxattr = btrfs_listxattr, |
95819c05 | 4548 | .removexattr = btrfs_removexattr, |
fdebe2bd | 4549 | .permission = btrfs_permission, |
39279cc3 | 4550 | }; |
39279cc3 CM |
4551 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
4552 | .lookup = btrfs_lookup, | |
fdebe2bd | 4553 | .permission = btrfs_permission, |
39279cc3 | 4554 | }; |
39279cc3 CM |
4555 | static struct file_operations btrfs_dir_file_operations = { |
4556 | .llseek = generic_file_llseek, | |
4557 | .read = generic_read_dir, | |
cbdf5a24 | 4558 | .readdir = btrfs_real_readdir, |
34287aa3 | 4559 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 4560 | #ifdef CONFIG_COMPAT |
34287aa3 | 4561 | .compat_ioctl = btrfs_ioctl, |
39279cc3 | 4562 | #endif |
6bf13c0c | 4563 | .release = btrfs_release_file, |
e02119d5 | 4564 | .fsync = btrfs_sync_file, |
39279cc3 CM |
4565 | }; |
4566 | ||
d1310b2e | 4567 | static struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 4568 | .fill_delalloc = run_delalloc_range, |
065631f6 | 4569 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 4570 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 4571 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 4572 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 4573 | .writepage_start_hook = btrfs_writepage_start_hook, |
1259ab75 | 4574 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
b0c68f8b CM |
4575 | .set_bit_hook = btrfs_set_bit_hook, |
4576 | .clear_bit_hook = btrfs_clear_bit_hook, | |
07157aac CM |
4577 | }; |
4578 | ||
39279cc3 CM |
4579 | static struct address_space_operations btrfs_aops = { |
4580 | .readpage = btrfs_readpage, | |
4581 | .writepage = btrfs_writepage, | |
b293f02e | 4582 | .writepages = btrfs_writepages, |
3ab2fb5a | 4583 | .readpages = btrfs_readpages, |
39279cc3 | 4584 | .sync_page = block_sync_page, |
39279cc3 | 4585 | .bmap = btrfs_bmap, |
16432985 | 4586 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
4587 | .invalidatepage = btrfs_invalidatepage, |
4588 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 4589 | .set_page_dirty = btrfs_set_page_dirty, |
39279cc3 CM |
4590 | }; |
4591 | ||
4592 | static struct address_space_operations btrfs_symlink_aops = { | |
4593 | .readpage = btrfs_readpage, | |
4594 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
4595 | .invalidatepage = btrfs_invalidatepage, |
4596 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
4597 | }; |
4598 | ||
4599 | static struct inode_operations btrfs_file_inode_operations = { | |
4600 | .truncate = btrfs_truncate, | |
4601 | .getattr = btrfs_getattr, | |
4602 | .setattr = btrfs_setattr, | |
95819c05 CH |
4603 | .setxattr = btrfs_setxattr, |
4604 | .getxattr = btrfs_getxattr, | |
5103e947 | 4605 | .listxattr = btrfs_listxattr, |
95819c05 | 4606 | .removexattr = btrfs_removexattr, |
fdebe2bd | 4607 | .permission = btrfs_permission, |
d899e052 | 4608 | .fallocate = btrfs_fallocate, |
39279cc3 | 4609 | }; |
618e21d5 JB |
4610 | static struct inode_operations btrfs_special_inode_operations = { |
4611 | .getattr = btrfs_getattr, | |
4612 | .setattr = btrfs_setattr, | |
fdebe2bd | 4613 | .permission = btrfs_permission, |
95819c05 CH |
4614 | .setxattr = btrfs_setxattr, |
4615 | .getxattr = btrfs_getxattr, | |
33268eaf | 4616 | .listxattr = btrfs_listxattr, |
95819c05 | 4617 | .removexattr = btrfs_removexattr, |
618e21d5 | 4618 | }; |
39279cc3 CM |
4619 | static struct inode_operations btrfs_symlink_inode_operations = { |
4620 | .readlink = generic_readlink, | |
4621 | .follow_link = page_follow_link_light, | |
4622 | .put_link = page_put_link, | |
fdebe2bd | 4623 | .permission = btrfs_permission, |
39279cc3 | 4624 | }; |