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