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