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