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