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