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