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