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