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