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