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