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 | ||
065631f6 | 19 | #include <linux/bio.h> |
39279cc3 CM |
20 | #include <linux/buffer_head.h> |
21 | #include <linux/fs.h> | |
22 | #include <linux/pagemap.h> | |
23 | #include <linux/highmem.h> | |
24 | #include <linux/time.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/string.h> | |
27 | #include <linux/smp_lock.h> | |
28 | #include <linux/backing-dev.h> | |
29 | #include <linux/mpage.h> | |
30 | #include <linux/swap.h> | |
31 | #include <linux/writeback.h> | |
32 | #include <linux/statfs.h> | |
33 | #include <linux/compat.h> | |
9ebefb18 | 34 | #include <linux/bit_spinlock.h> |
92fee66d | 35 | #include <linux/version.h> |
5103e947 | 36 | #include <linux/xattr.h> |
39279cc3 CM |
37 | #include "ctree.h" |
38 | #include "disk-io.h" | |
39 | #include "transaction.h" | |
40 | #include "btrfs_inode.h" | |
41 | #include "ioctl.h" | |
42 | #include "print-tree.h" | |
0b86a832 | 43 | #include "volumes.h" |
39279cc3 CM |
44 | |
45 | struct btrfs_iget_args { | |
46 | u64 ino; | |
47 | struct btrfs_root *root; | |
48 | }; | |
49 | ||
50 | static struct inode_operations btrfs_dir_inode_operations; | |
51 | static struct inode_operations btrfs_symlink_inode_operations; | |
52 | static struct inode_operations btrfs_dir_ro_inode_operations; | |
618e21d5 | 53 | static struct inode_operations btrfs_special_inode_operations; |
39279cc3 CM |
54 | static struct inode_operations btrfs_file_inode_operations; |
55 | static struct address_space_operations btrfs_aops; | |
56 | static struct address_space_operations btrfs_symlink_aops; | |
57 | static struct file_operations btrfs_dir_file_operations; | |
d1310b2e | 58 | static struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
59 | |
60 | static struct kmem_cache *btrfs_inode_cachep; | |
61 | struct kmem_cache *btrfs_trans_handle_cachep; | |
62 | struct kmem_cache *btrfs_transaction_cachep; | |
63 | struct kmem_cache *btrfs_bit_radix_cachep; | |
64 | struct kmem_cache *btrfs_path_cachep; | |
65 | ||
66 | #define S_SHIFT 12 | |
67 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { | |
68 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, | |
69 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
70 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
71 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
72 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
73 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
74 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
75 | }; | |
76 | ||
1832a6d5 CM |
77 | int btrfs_check_free_space(struct btrfs_root *root, u64 num_required, |
78 | int for_del) | |
79 | { | |
80 | u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy); | |
81 | u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy); | |
82 | u64 thresh; | |
83 | int ret = 0; | |
84 | ||
85 | if (for_del) | |
f9ef6604 | 86 | thresh = total * 90; |
1832a6d5 | 87 | else |
f9ef6604 CM |
88 | thresh = total * 85; |
89 | ||
90 | do_div(thresh, 100); | |
1832a6d5 CM |
91 | |
92 | spin_lock(&root->fs_info->delalloc_lock); | |
93 | if (used + root->fs_info->delalloc_bytes + num_required > thresh) | |
94 | ret = -ENOSPC; | |
95 | spin_unlock(&root->fs_info->delalloc_lock); | |
96 | return ret; | |
97 | } | |
98 | ||
be20aa9d | 99 | static int cow_file_range(struct inode *inode, u64 start, u64 end) |
b888db2b CM |
100 | { |
101 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
102 | struct btrfs_trans_handle *trans; | |
b888db2b | 103 | u64 alloc_hint = 0; |
db94535d | 104 | u64 num_bytes; |
c59f8951 | 105 | u64 cur_alloc_size; |
db94535d | 106 | u64 blocksize = root->sectorsize; |
d1310b2e CM |
107 | u64 orig_start = start; |
108 | u64 orig_num_bytes; | |
be20aa9d CM |
109 | struct btrfs_key ins; |
110 | int ret; | |
b888db2b | 111 | |
b888db2b | 112 | trans = btrfs_start_transaction(root, 1); |
b888db2b | 113 | BUG_ON(!trans); |
be20aa9d CM |
114 | btrfs_set_trans_block_group(trans, inode); |
115 | ||
db94535d | 116 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
be20aa9d | 117 | num_bytes = max(blocksize, num_bytes); |
b888db2b | 118 | ret = btrfs_drop_extents(trans, root, inode, |
3326d1b0 | 119 | start, start + num_bytes, start, &alloc_hint); |
d1310b2e | 120 | orig_num_bytes = num_bytes; |
db94535d | 121 | |
179e29e4 CM |
122 | if (alloc_hint == EXTENT_MAP_INLINE) |
123 | goto out; | |
124 | ||
c59f8951 CM |
125 | while(num_bytes > 0) { |
126 | cur_alloc_size = min(num_bytes, root->fs_info->max_extent); | |
127 | ret = btrfs_alloc_extent(trans, root, cur_alloc_size, | |
128 | root->root_key.objectid, | |
129 | trans->transid, | |
130 | inode->i_ino, start, 0, | |
131 | alloc_hint, (u64)-1, &ins, 1); | |
132 | if (ret) { | |
133 | WARN_ON(1); | |
134 | goto out; | |
135 | } | |
136 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, | |
137 | start, ins.objectid, ins.offset, | |
138 | ins.offset); | |
9069218d | 139 | inode->i_blocks += ins.offset >> 9; |
5f56406a | 140 | btrfs_check_file(root, inode); |
c59f8951 CM |
141 | num_bytes -= cur_alloc_size; |
142 | alloc_hint = ins.objectid + ins.offset; | |
143 | start += cur_alloc_size; | |
b888db2b | 144 | } |
d1310b2e CM |
145 | btrfs_drop_extent_cache(inode, orig_start, |
146 | orig_start + orig_num_bytes - 1); | |
dc17ff8f | 147 | btrfs_add_ordered_inode(inode); |
9069218d | 148 | btrfs_update_inode(trans, root, inode); |
b888db2b CM |
149 | out: |
150 | btrfs_end_transaction(trans, root); | |
be20aa9d CM |
151 | return ret; |
152 | } | |
153 | ||
154 | static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end) | |
155 | { | |
156 | u64 extent_start; | |
157 | u64 extent_end; | |
158 | u64 bytenr; | |
159 | u64 cow_end; | |
1832a6d5 | 160 | u64 loops = 0; |
c31f8830 | 161 | u64 total_fs_bytes; |
be20aa9d CM |
162 | struct btrfs_root *root = BTRFS_I(inode)->root; |
163 | struct extent_buffer *leaf; | |
164 | int found_type; | |
165 | struct btrfs_path *path; | |
166 | struct btrfs_file_extent_item *item; | |
167 | int ret; | |
168 | int err; | |
169 | struct btrfs_key found_key; | |
170 | ||
c31f8830 | 171 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
be20aa9d CM |
172 | path = btrfs_alloc_path(); |
173 | BUG_ON(!path); | |
174 | again: | |
175 | ret = btrfs_lookup_file_extent(NULL, root, path, | |
176 | inode->i_ino, start, 0); | |
177 | if (ret < 0) { | |
178 | btrfs_free_path(path); | |
179 | return ret; | |
180 | } | |
181 | ||
182 | cow_end = end; | |
183 | if (ret != 0) { | |
184 | if (path->slots[0] == 0) | |
185 | goto not_found; | |
186 | path->slots[0]--; | |
187 | } | |
188 | ||
189 | leaf = path->nodes[0]; | |
190 | item = btrfs_item_ptr(leaf, path->slots[0], | |
191 | struct btrfs_file_extent_item); | |
192 | ||
193 | /* are we inside the extent that was found? */ | |
194 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
195 | found_type = btrfs_key_type(&found_key); | |
196 | if (found_key.objectid != inode->i_ino || | |
197 | found_type != BTRFS_EXTENT_DATA_KEY) { | |
198 | goto not_found; | |
199 | } | |
200 | ||
201 | found_type = btrfs_file_extent_type(leaf, item); | |
202 | extent_start = found_key.offset; | |
203 | if (found_type == BTRFS_FILE_EXTENT_REG) { | |
c31f8830 CM |
204 | u64 extent_num_bytes; |
205 | ||
206 | extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item); | |
207 | extent_end = extent_start + extent_num_bytes; | |
be20aa9d CM |
208 | err = 0; |
209 | ||
1832a6d5 CM |
210 | if (loops && start != extent_start) |
211 | goto not_found; | |
212 | ||
be20aa9d CM |
213 | if (start < extent_start || start >= extent_end) |
214 | goto not_found; | |
215 | ||
216 | cow_end = min(end, extent_end - 1); | |
217 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); | |
218 | if (bytenr == 0) | |
219 | goto not_found; | |
220 | ||
c31f8830 CM |
221 | /* |
222 | * we may be called by the resizer, make sure we're inside | |
223 | * the limits of the FS | |
224 | */ | |
225 | if (bytenr + extent_num_bytes > total_fs_bytes) | |
226 | goto not_found; | |
227 | ||
be20aa9d CM |
228 | if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) { |
229 | goto not_found; | |
230 | } | |
231 | ||
232 | start = extent_end; | |
bd09835d | 233 | } else { |
be20aa9d CM |
234 | goto not_found; |
235 | } | |
236 | loop: | |
237 | if (start > end) { | |
238 | btrfs_free_path(path); | |
239 | return 0; | |
240 | } | |
241 | btrfs_release_path(root, path); | |
1832a6d5 | 242 | loops++; |
be20aa9d CM |
243 | goto again; |
244 | ||
245 | not_found: | |
246 | cow_file_range(inode, start, cow_end); | |
247 | start = cow_end + 1; | |
248 | goto loop; | |
249 | } | |
250 | ||
251 | static int run_delalloc_range(struct inode *inode, u64 start, u64 end) | |
252 | { | |
253 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
254 | int ret; | |
be20aa9d | 255 | mutex_lock(&root->fs_info->fs_mutex); |
b98b6767 Y |
256 | if (btrfs_test_opt(root, NODATACOW) || |
257 | btrfs_test_flag(inode, NODATACOW)) | |
be20aa9d CM |
258 | ret = run_delalloc_nocow(inode, start, end); |
259 | else | |
260 | ret = cow_file_range(inode, start, end); | |
1832a6d5 | 261 | |
b888db2b CM |
262 | mutex_unlock(&root->fs_info->fs_mutex); |
263 | return ret; | |
264 | } | |
265 | ||
291d673e | 266 | int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, |
b0c68f8b | 267 | unsigned long old, unsigned long bits) |
291d673e | 268 | { |
b0c68f8b | 269 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
291d673e CM |
270 | struct btrfs_root *root = BTRFS_I(inode)->root; |
271 | spin_lock(&root->fs_info->delalloc_lock); | |
9069218d | 272 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
291d673e CM |
273 | root->fs_info->delalloc_bytes += end - start + 1; |
274 | spin_unlock(&root->fs_info->delalloc_lock); | |
275 | } | |
276 | return 0; | |
277 | } | |
278 | ||
279 | int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, | |
b0c68f8b | 280 | unsigned long old, unsigned long bits) |
291d673e | 281 | { |
b0c68f8b | 282 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
291d673e CM |
283 | struct btrfs_root *root = BTRFS_I(inode)->root; |
284 | spin_lock(&root->fs_info->delalloc_lock); | |
b0c68f8b CM |
285 | if (end - start + 1 > root->fs_info->delalloc_bytes) { |
286 | printk("warning: delalloc account %Lu %Lu\n", | |
287 | end - start + 1, root->fs_info->delalloc_bytes); | |
288 | root->fs_info->delalloc_bytes = 0; | |
9069218d | 289 | BTRFS_I(inode)->delalloc_bytes = 0; |
b0c68f8b CM |
290 | } else { |
291 | root->fs_info->delalloc_bytes -= end - start + 1; | |
9069218d | 292 | BTRFS_I(inode)->delalloc_bytes -= end - start + 1; |
b0c68f8b | 293 | } |
291d673e CM |
294 | spin_unlock(&root->fs_info->delalloc_lock); |
295 | } | |
296 | return 0; | |
297 | } | |
298 | ||
239b14b3 CM |
299 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
300 | size_t size, struct bio *bio) | |
301 | { | |
302 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
303 | struct btrfs_mapping_tree *map_tree; | |
304 | struct btrfs_device *dev; | |
305 | u64 logical = bio->bi_sector << 9; | |
306 | u64 physical; | |
307 | u64 length = 0; | |
308 | u64 map_length; | |
309 | struct bio_vec *bvec; | |
310 | int i; | |
311 | int ret; | |
312 | ||
313 | bio_for_each_segment(bvec, bio, i) { | |
314 | length += bvec->bv_len; | |
315 | } | |
316 | map_tree = &root->fs_info->mapping_tree; | |
317 | map_length = length; | |
318 | ret = btrfs_map_block(map_tree, logical, &physical, &map_length, &dev); | |
319 | if (map_length < length + size) { | |
239b14b3 CM |
320 | return 1; |
321 | } | |
322 | return 0; | |
323 | } | |
324 | ||
0b86a832 | 325 | int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio) |
065631f6 | 326 | { |
065631f6 CM |
327 | struct btrfs_root *root = BTRFS_I(inode)->root; |
328 | struct btrfs_trans_handle *trans; | |
329 | int ret = 0; | |
330 | ||
0b86a832 CM |
331 | if (rw != WRITE) { |
332 | goto mapit; | |
333 | } | |
065631f6 CM |
334 | |
335 | if (btrfs_test_opt(root, NODATASUM) || | |
0b86a832 CM |
336 | btrfs_test_flag(inode, NODATASUM)) { |
337 | goto mapit; | |
338 | } | |
065631f6 CM |
339 | |
340 | mutex_lock(&root->fs_info->fs_mutex); | |
341 | trans = btrfs_start_transaction(root, 1); | |
342 | btrfs_set_trans_block_group(trans, inode); | |
343 | btrfs_csum_file_blocks(trans, root, inode, bio); | |
344 | ret = btrfs_end_transaction(trans, root); | |
345 | BUG_ON(ret); | |
346 | mutex_unlock(&root->fs_info->fs_mutex); | |
0b86a832 CM |
347 | mapit: |
348 | return btrfs_map_bio(root, rw, bio); | |
065631f6 | 349 | } |
6885f308 | 350 | |
07157aac CM |
351 | int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end) |
352 | { | |
353 | int ret = 0; | |
354 | struct inode *inode = page->mapping->host; | |
355 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d1310b2e | 356 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
07157aac CM |
357 | struct btrfs_csum_item *item; |
358 | struct btrfs_path *path = NULL; | |
ff79f819 | 359 | u32 csum; |
b98b6767 Y |
360 | if (btrfs_test_opt(root, NODATASUM) || |
361 | btrfs_test_flag(inode, NODATASUM)) | |
b6cda9bc | 362 | return 0; |
07157aac CM |
363 | mutex_lock(&root->fs_info->fs_mutex); |
364 | path = btrfs_alloc_path(); | |
365 | item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0); | |
366 | if (IS_ERR(item)) { | |
367 | ret = PTR_ERR(item); | |
368 | /* a csum that isn't present is a preallocated region. */ | |
369 | if (ret == -ENOENT || ret == -EFBIG) | |
370 | ret = 0; | |
ff79f819 | 371 | csum = 0; |
aadfeb6e | 372 | printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start); |
07157aac CM |
373 | goto out; |
374 | } | |
ff79f819 CM |
375 | read_extent_buffer(path->nodes[0], &csum, (unsigned long)item, |
376 | BTRFS_CRC32_SIZE); | |
d1310b2e | 377 | set_state_private(io_tree, start, csum); |
07157aac CM |
378 | out: |
379 | if (path) | |
380 | btrfs_free_path(path); | |
381 | mutex_unlock(&root->fs_info->fs_mutex); | |
382 | return ret; | |
383 | } | |
384 | ||
70dec807 CM |
385 | int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
386 | struct extent_state *state) | |
07157aac | 387 | { |
35ebb934 | 388 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
07157aac | 389 | struct inode *inode = page->mapping->host; |
d1310b2e | 390 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
07157aac | 391 | char *kaddr; |
aadfeb6e | 392 | u64 private = ~(u32)0; |
07157aac | 393 | int ret; |
ff79f819 CM |
394 | struct btrfs_root *root = BTRFS_I(inode)->root; |
395 | u32 csum = ~(u32)0; | |
bbf0d006 | 396 | unsigned long flags; |
d1310b2e | 397 | |
b98b6767 Y |
398 | if (btrfs_test_opt(root, NODATASUM) || |
399 | btrfs_test_flag(inode, NODATASUM)) | |
b6cda9bc | 400 | return 0; |
c2e639f0 | 401 | if (state && state->start == start) { |
70dec807 CM |
402 | private = state->private; |
403 | ret = 0; | |
404 | } else { | |
405 | ret = get_state_private(io_tree, start, &private); | |
406 | } | |
bbf0d006 | 407 | local_irq_save(flags); |
07157aac CM |
408 | kaddr = kmap_atomic(page, KM_IRQ0); |
409 | if (ret) { | |
410 | goto zeroit; | |
411 | } | |
ff79f819 CM |
412 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
413 | btrfs_csum_final(csum, (char *)&csum); | |
414 | if (csum != private) { | |
07157aac CM |
415 | goto zeroit; |
416 | } | |
417 | kunmap_atomic(kaddr, KM_IRQ0); | |
bbf0d006 | 418 | local_irq_restore(flags); |
07157aac CM |
419 | return 0; |
420 | ||
421 | zeroit: | |
aadfeb6e CM |
422 | printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n", |
423 | page->mapping->host->i_ino, (unsigned long long)start, csum, | |
424 | private); | |
db94535d CM |
425 | memset(kaddr + offset, 1, end - start + 1); |
426 | flush_dcache_page(page); | |
07157aac | 427 | kunmap_atomic(kaddr, KM_IRQ0); |
bbf0d006 | 428 | local_irq_restore(flags); |
07157aac CM |
429 | return 0; |
430 | } | |
b888db2b | 431 | |
39279cc3 CM |
432 | void btrfs_read_locked_inode(struct inode *inode) |
433 | { | |
434 | struct btrfs_path *path; | |
5f39d397 | 435 | struct extent_buffer *leaf; |
39279cc3 | 436 | struct btrfs_inode_item *inode_item; |
0b86a832 | 437 | struct btrfs_timespec *tspec; |
39279cc3 CM |
438 | struct btrfs_root *root = BTRFS_I(inode)->root; |
439 | struct btrfs_key location; | |
440 | u64 alloc_group_block; | |
618e21d5 | 441 | u32 rdev; |
39279cc3 CM |
442 | int ret; |
443 | ||
444 | path = btrfs_alloc_path(); | |
445 | BUG_ON(!path); | |
39279cc3 | 446 | mutex_lock(&root->fs_info->fs_mutex); |
39279cc3 | 447 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 448 | |
39279cc3 | 449 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
5f39d397 | 450 | if (ret) |
39279cc3 | 451 | goto make_bad; |
39279cc3 | 452 | |
5f39d397 CM |
453 | leaf = path->nodes[0]; |
454 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
455 | struct btrfs_inode_item); | |
456 | ||
457 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); | |
458 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); | |
459 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); | |
460 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); | |
461 | inode->i_size = btrfs_inode_size(leaf, inode_item); | |
462 | ||
463 | tspec = btrfs_inode_atime(inode_item); | |
464 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
465 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
466 | ||
467 | tspec = btrfs_inode_mtime(inode_item); | |
468 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
469 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
470 | ||
471 | tspec = btrfs_inode_ctime(inode_item); | |
472 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
473 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
474 | ||
475 | inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item); | |
476 | inode->i_generation = btrfs_inode_generation(leaf, inode_item); | |
618e21d5 | 477 | inode->i_rdev = 0; |
5f39d397 CM |
478 | rdev = btrfs_inode_rdev(leaf, inode_item); |
479 | ||
480 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); | |
39279cc3 CM |
481 | BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, |
482 | alloc_group_block); | |
b98b6767 | 483 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
e52ec0eb CM |
484 | if (!BTRFS_I(inode)->block_group) { |
485 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, | |
0b86a832 CM |
486 | NULL, 0, |
487 | BTRFS_BLOCK_GROUP_METADATA, 0); | |
e52ec0eb | 488 | } |
39279cc3 CM |
489 | btrfs_free_path(path); |
490 | inode_item = NULL; | |
491 | ||
492 | mutex_unlock(&root->fs_info->fs_mutex); | |
493 | ||
494 | switch (inode->i_mode & S_IFMT) { | |
39279cc3 CM |
495 | case S_IFREG: |
496 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 497 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
d1310b2e | 498 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
499 | inode->i_fop = &btrfs_file_operations; |
500 | inode->i_op = &btrfs_file_inode_operations; | |
501 | break; | |
502 | case S_IFDIR: | |
503 | inode->i_fop = &btrfs_dir_file_operations; | |
504 | if (root == root->fs_info->tree_root) | |
505 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
506 | else | |
507 | inode->i_op = &btrfs_dir_inode_operations; | |
508 | break; | |
509 | case S_IFLNK: | |
510 | inode->i_op = &btrfs_symlink_inode_operations; | |
511 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
04160088 | 512 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 | 513 | break; |
618e21d5 JB |
514 | default: |
515 | init_special_inode(inode, inode->i_mode, rdev); | |
516 | break; | |
39279cc3 CM |
517 | } |
518 | return; | |
519 | ||
520 | make_bad: | |
521 | btrfs_release_path(root, path); | |
522 | btrfs_free_path(path); | |
523 | mutex_unlock(&root->fs_info->fs_mutex); | |
524 | make_bad_inode(inode); | |
525 | } | |
526 | ||
5f39d397 CM |
527 | static void fill_inode_item(struct extent_buffer *leaf, |
528 | struct btrfs_inode_item *item, | |
39279cc3 CM |
529 | struct inode *inode) |
530 | { | |
5f39d397 CM |
531 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
532 | btrfs_set_inode_gid(leaf, item, inode->i_gid); | |
533 | btrfs_set_inode_size(leaf, item, inode->i_size); | |
534 | btrfs_set_inode_mode(leaf, item, inode->i_mode); | |
535 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); | |
536 | ||
537 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), | |
538 | inode->i_atime.tv_sec); | |
539 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), | |
540 | inode->i_atime.tv_nsec); | |
541 | ||
542 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), | |
543 | inode->i_mtime.tv_sec); | |
544 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), | |
545 | inode->i_mtime.tv_nsec); | |
546 | ||
547 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), | |
548 | inode->i_ctime.tv_sec); | |
549 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), | |
550 | inode->i_ctime.tv_nsec); | |
551 | ||
552 | btrfs_set_inode_nblocks(leaf, item, inode->i_blocks); | |
553 | btrfs_set_inode_generation(leaf, item, inode->i_generation); | |
554 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); | |
b98b6767 | 555 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
5f39d397 | 556 | btrfs_set_inode_block_group(leaf, item, |
39279cc3 CM |
557 | BTRFS_I(inode)->block_group->key.objectid); |
558 | } | |
559 | ||
a52d9a80 | 560 | int btrfs_update_inode(struct btrfs_trans_handle *trans, |
39279cc3 CM |
561 | struct btrfs_root *root, |
562 | struct inode *inode) | |
563 | { | |
564 | struct btrfs_inode_item *inode_item; | |
565 | struct btrfs_path *path; | |
5f39d397 | 566 | struct extent_buffer *leaf; |
39279cc3 CM |
567 | int ret; |
568 | ||
569 | path = btrfs_alloc_path(); | |
570 | BUG_ON(!path); | |
39279cc3 CM |
571 | ret = btrfs_lookup_inode(trans, root, path, |
572 | &BTRFS_I(inode)->location, 1); | |
573 | if (ret) { | |
574 | if (ret > 0) | |
575 | ret = -ENOENT; | |
576 | goto failed; | |
577 | } | |
578 | ||
5f39d397 CM |
579 | leaf = path->nodes[0]; |
580 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
39279cc3 CM |
581 | struct btrfs_inode_item); |
582 | ||
5f39d397 CM |
583 | fill_inode_item(leaf, inode_item, inode); |
584 | btrfs_mark_buffer_dirty(leaf); | |
15ee9bc7 | 585 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
586 | ret = 0; |
587 | failed: | |
588 | btrfs_release_path(root, path); | |
589 | btrfs_free_path(path); | |
590 | return ret; | |
591 | } | |
592 | ||
593 | ||
594 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, | |
595 | struct btrfs_root *root, | |
596 | struct inode *dir, | |
597 | struct dentry *dentry) | |
598 | { | |
599 | struct btrfs_path *path; | |
600 | const char *name = dentry->d_name.name; | |
601 | int name_len = dentry->d_name.len; | |
602 | int ret = 0; | |
5f39d397 | 603 | struct extent_buffer *leaf; |
39279cc3 | 604 | struct btrfs_dir_item *di; |
5f39d397 | 605 | struct btrfs_key key; |
39279cc3 CM |
606 | |
607 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
608 | if (!path) { |
609 | ret = -ENOMEM; | |
610 | goto err; | |
611 | } | |
612 | ||
39279cc3 CM |
613 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
614 | name, name_len, -1); | |
615 | if (IS_ERR(di)) { | |
616 | ret = PTR_ERR(di); | |
617 | goto err; | |
618 | } | |
619 | if (!di) { | |
620 | ret = -ENOENT; | |
621 | goto err; | |
622 | } | |
5f39d397 CM |
623 | leaf = path->nodes[0]; |
624 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 625 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
626 | if (ret) |
627 | goto err; | |
39279cc3 CM |
628 | btrfs_release_path(root, path); |
629 | ||
630 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, | |
5f39d397 | 631 | key.objectid, name, name_len, -1); |
39279cc3 CM |
632 | if (IS_ERR(di)) { |
633 | ret = PTR_ERR(di); | |
634 | goto err; | |
635 | } | |
636 | if (!di) { | |
637 | ret = -ENOENT; | |
638 | goto err; | |
639 | } | |
640 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
39279cc3 CM |
641 | |
642 | dentry->d_inode->i_ctime = dir->i_ctime; | |
76fea00a CM |
643 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
644 | dentry->d_inode->i_ino, | |
645 | dentry->d_parent->d_inode->i_ino); | |
646 | if (ret) { | |
647 | printk("failed to delete reference to %.*s, " | |
648 | "inode %lu parent %lu\n", name_len, name, | |
649 | dentry->d_inode->i_ino, | |
650 | dentry->d_parent->d_inode->i_ino); | |
3954401f | 651 | } |
39279cc3 CM |
652 | err: |
653 | btrfs_free_path(path); | |
654 | if (!ret) { | |
655 | dir->i_size -= name_len * 2; | |
79c44584 | 656 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
39279cc3 | 657 | btrfs_update_inode(trans, root, dir); |
6da6abae CM |
658 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
659 | dentry->d_inode->i_nlink--; | |
660 | #else | |
39279cc3 | 661 | drop_nlink(dentry->d_inode); |
6da6abae | 662 | #endif |
54aa1f4d | 663 | ret = btrfs_update_inode(trans, root, dentry->d_inode); |
39279cc3 CM |
664 | dir->i_sb->s_dirt = 1; |
665 | } | |
666 | return ret; | |
667 | } | |
668 | ||
669 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
670 | { | |
671 | struct btrfs_root *root; | |
672 | struct btrfs_trans_handle *trans; | |
2da98f00 | 673 | struct inode *inode = dentry->d_inode; |
39279cc3 | 674 | int ret; |
1832a6d5 | 675 | unsigned long nr = 0; |
39279cc3 CM |
676 | |
677 | root = BTRFS_I(dir)->root; | |
678 | mutex_lock(&root->fs_info->fs_mutex); | |
1832a6d5 CM |
679 | |
680 | ret = btrfs_check_free_space(root, 1, 1); | |
681 | if (ret) | |
682 | goto fail; | |
683 | ||
39279cc3 | 684 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 685 | |
39279cc3 CM |
686 | btrfs_set_trans_block_group(trans, dir); |
687 | ret = btrfs_unlink_trans(trans, root, dir, dentry); | |
d3c2fdcf | 688 | nr = trans->blocks_used; |
5f39d397 | 689 | |
2da98f00 CM |
690 | if (inode->i_nlink == 0) { |
691 | int found; | |
692 | /* if the inode isn't linked anywhere, | |
693 | * we don't need to worry about | |
694 | * data=ordered | |
695 | */ | |
696 | found = btrfs_del_ordered_inode(inode); | |
697 | if (found == 1) { | |
698 | atomic_dec(&inode->i_count); | |
699 | } | |
700 | } | |
701 | ||
39279cc3 | 702 | btrfs_end_transaction(trans, root); |
1832a6d5 | 703 | fail: |
39279cc3 | 704 | mutex_unlock(&root->fs_info->fs_mutex); |
d3c2fdcf | 705 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 706 | btrfs_throttle(root); |
39279cc3 CM |
707 | return ret; |
708 | } | |
709 | ||
710 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |
711 | { | |
712 | struct inode *inode = dentry->d_inode; | |
1832a6d5 | 713 | int err = 0; |
39279cc3 CM |
714 | int ret; |
715 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
39279cc3 | 716 | struct btrfs_trans_handle *trans; |
1832a6d5 | 717 | unsigned long nr = 0; |
39279cc3 | 718 | |
134d4512 Y |
719 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) |
720 | return -ENOTEMPTY; | |
721 | ||
39279cc3 | 722 | mutex_lock(&root->fs_info->fs_mutex); |
1832a6d5 CM |
723 | ret = btrfs_check_free_space(root, 1, 1); |
724 | if (ret) | |
725 | goto fail; | |
726 | ||
39279cc3 CM |
727 | trans = btrfs_start_transaction(root, 1); |
728 | btrfs_set_trans_block_group(trans, dir); | |
39279cc3 CM |
729 | |
730 | /* now the directory is empty */ | |
731 | err = btrfs_unlink_trans(trans, root, dir, dentry); | |
732 | if (!err) { | |
733 | inode->i_size = 0; | |
734 | } | |
3954401f | 735 | |
d3c2fdcf | 736 | nr = trans->blocks_used; |
39279cc3 | 737 | ret = btrfs_end_transaction(trans, root); |
1832a6d5 | 738 | fail: |
134d4512 | 739 | mutex_unlock(&root->fs_info->fs_mutex); |
d3c2fdcf | 740 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 741 | btrfs_throttle(root); |
3954401f | 742 | |
39279cc3 CM |
743 | if (ret && !err) |
744 | err = ret; | |
745 | return err; | |
746 | } | |
747 | ||
39279cc3 CM |
748 | /* |
749 | * this can truncate away extent items, csum items and directory items. | |
750 | * It starts at a high offset and removes keys until it can't find | |
751 | * any higher than i_size. | |
752 | * | |
753 | * csum items that cross the new i_size are truncated to the new size | |
754 | * as well. | |
755 | */ | |
756 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, | |
757 | struct btrfs_root *root, | |
85e21bac CM |
758 | struct inode *inode, |
759 | u32 min_type) | |
39279cc3 CM |
760 | { |
761 | int ret; | |
762 | struct btrfs_path *path; | |
763 | struct btrfs_key key; | |
5f39d397 | 764 | struct btrfs_key found_key; |
39279cc3 | 765 | u32 found_type; |
5f39d397 | 766 | struct extent_buffer *leaf; |
39279cc3 CM |
767 | struct btrfs_file_extent_item *fi; |
768 | u64 extent_start = 0; | |
db94535d | 769 | u64 extent_num_bytes = 0; |
39279cc3 | 770 | u64 item_end = 0; |
7bb86316 | 771 | u64 root_gen = 0; |
d8d5f3e1 | 772 | u64 root_owner = 0; |
39279cc3 CM |
773 | int found_extent; |
774 | int del_item; | |
85e21bac CM |
775 | int pending_del_nr = 0; |
776 | int pending_del_slot = 0; | |
179e29e4 | 777 | int extent_type = -1; |
39279cc3 | 778 | |
a52d9a80 | 779 | btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1); |
39279cc3 | 780 | path = btrfs_alloc_path(); |
3c69faec | 781 | path->reada = -1; |
39279cc3 | 782 | BUG_ON(!path); |
5f39d397 | 783 | |
39279cc3 CM |
784 | /* FIXME, add redo link to tree so we don't leak on crash */ |
785 | key.objectid = inode->i_ino; | |
786 | key.offset = (u64)-1; | |
5f39d397 CM |
787 | key.type = (u8)-1; |
788 | ||
85e21bac CM |
789 | btrfs_init_path(path); |
790 | search_again: | |
791 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
792 | if (ret < 0) { | |
793 | goto error; | |
794 | } | |
795 | if (ret > 0) { | |
796 | BUG_ON(path->slots[0] == 0); | |
797 | path->slots[0]--; | |
798 | } | |
799 | ||
39279cc3 | 800 | while(1) { |
39279cc3 | 801 | fi = NULL; |
5f39d397 CM |
802 | leaf = path->nodes[0]; |
803 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
804 | found_type = btrfs_key_type(&found_key); | |
39279cc3 | 805 | |
5f39d397 | 806 | if (found_key.objectid != inode->i_ino) |
39279cc3 | 807 | break; |
5f39d397 | 808 | |
85e21bac | 809 | if (found_type < min_type) |
39279cc3 CM |
810 | break; |
811 | ||
5f39d397 | 812 | item_end = found_key.offset; |
39279cc3 | 813 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 814 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 815 | struct btrfs_file_extent_item); |
179e29e4 CM |
816 | extent_type = btrfs_file_extent_type(leaf, fi); |
817 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 818 | item_end += |
db94535d | 819 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 CM |
820 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
821 | struct btrfs_item *item = btrfs_item_nr(leaf, | |
822 | path->slots[0]); | |
823 | item_end += btrfs_file_extent_inline_len(leaf, | |
824 | item); | |
39279cc3 | 825 | } |
008630c1 | 826 | item_end--; |
39279cc3 CM |
827 | } |
828 | if (found_type == BTRFS_CSUM_ITEM_KEY) { | |
829 | ret = btrfs_csum_truncate(trans, root, path, | |
830 | inode->i_size); | |
831 | BUG_ON(ret); | |
832 | } | |
008630c1 | 833 | if (item_end < inode->i_size) { |
b888db2b CM |
834 | if (found_type == BTRFS_DIR_ITEM_KEY) { |
835 | found_type = BTRFS_INODE_ITEM_KEY; | |
836 | } else if (found_type == BTRFS_EXTENT_ITEM_KEY) { | |
837 | found_type = BTRFS_CSUM_ITEM_KEY; | |
85e21bac CM |
838 | } else if (found_type == BTRFS_EXTENT_DATA_KEY) { |
839 | found_type = BTRFS_XATTR_ITEM_KEY; | |
840 | } else if (found_type == BTRFS_XATTR_ITEM_KEY) { | |
841 | found_type = BTRFS_INODE_REF_KEY; | |
b888db2b CM |
842 | } else if (found_type) { |
843 | found_type--; | |
844 | } else { | |
845 | break; | |
39279cc3 | 846 | } |
a61721d5 | 847 | btrfs_set_key_type(&key, found_type); |
85e21bac | 848 | goto next; |
39279cc3 | 849 | } |
5f39d397 | 850 | if (found_key.offset >= inode->i_size) |
39279cc3 CM |
851 | del_item = 1; |
852 | else | |
853 | del_item = 0; | |
854 | found_extent = 0; | |
855 | ||
856 | /* FIXME, shrink the extent if the ref count is only 1 */ | |
179e29e4 CM |
857 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
858 | goto delete; | |
859 | ||
860 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 861 | u64 num_dec; |
db94535d | 862 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
39279cc3 | 863 | if (!del_item) { |
db94535d CM |
864 | u64 orig_num_bytes = |
865 | btrfs_file_extent_num_bytes(leaf, fi); | |
866 | extent_num_bytes = inode->i_size - | |
5f39d397 | 867 | found_key.offset + root->sectorsize - 1; |
b1632b10 Y |
868 | extent_num_bytes = extent_num_bytes & |
869 | ~((u64)root->sectorsize - 1); | |
db94535d CM |
870 | btrfs_set_file_extent_num_bytes(leaf, fi, |
871 | extent_num_bytes); | |
872 | num_dec = (orig_num_bytes - | |
9069218d CM |
873 | extent_num_bytes); |
874 | if (extent_start != 0) | |
875 | dec_i_blocks(inode, num_dec); | |
5f39d397 | 876 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 877 | } else { |
db94535d CM |
878 | extent_num_bytes = |
879 | btrfs_file_extent_disk_num_bytes(leaf, | |
880 | fi); | |
39279cc3 | 881 | /* FIXME blocksize != 4096 */ |
9069218d | 882 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
883 | if (extent_start != 0) { |
884 | found_extent = 1; | |
9069218d | 885 | dec_i_blocks(inode, num_dec); |
39279cc3 | 886 | } |
d8d5f3e1 CM |
887 | root_gen = btrfs_header_generation(leaf); |
888 | root_owner = btrfs_header_owner(leaf); | |
39279cc3 | 889 | } |
9069218d CM |
890 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
891 | if (!del_item) { | |
892 | u32 newsize = inode->i_size - found_key.offset; | |
893 | dec_i_blocks(inode, item_end + 1 - | |
894 | found_key.offset - newsize); | |
895 | newsize = | |
896 | btrfs_file_extent_calc_inline_size(newsize); | |
897 | ret = btrfs_truncate_item(trans, root, path, | |
898 | newsize, 1); | |
899 | BUG_ON(ret); | |
900 | } else { | |
901 | dec_i_blocks(inode, item_end + 1 - | |
902 | found_key.offset); | |
903 | } | |
39279cc3 | 904 | } |
179e29e4 | 905 | delete: |
39279cc3 | 906 | if (del_item) { |
85e21bac CM |
907 | if (!pending_del_nr) { |
908 | /* no pending yet, add ourselves */ | |
909 | pending_del_slot = path->slots[0]; | |
910 | pending_del_nr = 1; | |
911 | } else if (pending_del_nr && | |
912 | path->slots[0] + 1 == pending_del_slot) { | |
913 | /* hop on the pending chunk */ | |
914 | pending_del_nr++; | |
915 | pending_del_slot = path->slots[0]; | |
916 | } else { | |
917 | printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot); | |
918 | } | |
39279cc3 CM |
919 | } else { |
920 | break; | |
921 | } | |
39279cc3 CM |
922 | if (found_extent) { |
923 | ret = btrfs_free_extent(trans, root, extent_start, | |
7bb86316 | 924 | extent_num_bytes, |
d8d5f3e1 | 925 | root_owner, |
7bb86316 CM |
926 | root_gen, inode->i_ino, |
927 | found_key.offset, 0); | |
39279cc3 CM |
928 | BUG_ON(ret); |
929 | } | |
85e21bac CM |
930 | next: |
931 | if (path->slots[0] == 0) { | |
932 | if (pending_del_nr) | |
933 | goto del_pending; | |
934 | btrfs_release_path(root, path); | |
935 | goto search_again; | |
936 | } | |
937 | ||
938 | path->slots[0]--; | |
939 | if (pending_del_nr && | |
940 | path->slots[0] + 1 != pending_del_slot) { | |
941 | struct btrfs_key debug; | |
942 | del_pending: | |
943 | btrfs_item_key_to_cpu(path->nodes[0], &debug, | |
944 | pending_del_slot); | |
945 | ret = btrfs_del_items(trans, root, path, | |
946 | pending_del_slot, | |
947 | pending_del_nr); | |
948 | BUG_ON(ret); | |
949 | pending_del_nr = 0; | |
950 | btrfs_release_path(root, path); | |
951 | goto search_again; | |
952 | } | |
39279cc3 CM |
953 | } |
954 | ret = 0; | |
955 | error: | |
85e21bac CM |
956 | if (pending_del_nr) { |
957 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
958 | pending_del_nr); | |
959 | } | |
39279cc3 CM |
960 | btrfs_release_path(root, path); |
961 | btrfs_free_path(path); | |
962 | inode->i_sb->s_dirt = 1; | |
963 | return ret; | |
964 | } | |
965 | ||
b888db2b | 966 | static int btrfs_cow_one_page(struct inode *inode, struct page *page, |
a52d9a80 CM |
967 | size_t zero_start) |
968 | { | |
969 | char *kaddr; | |
d1310b2e | 970 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
35ebb934 | 971 | u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
b888db2b | 972 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; |
1832a6d5 | 973 | int ret = 0; |
a52d9a80 | 974 | |
190662b2 | 975 | WARN_ON(!PageLocked(page)); |
b3cfa35a | 976 | set_page_extent_mapped(page); |
a52d9a80 | 977 | |
d1310b2e | 978 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
d1310b2e | 979 | set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, |
b888db2b | 980 | page_end, GFP_NOFS); |
1832a6d5 | 981 | |
a52d9a80 | 982 | if (zero_start != PAGE_CACHE_SIZE) { |
b888db2b | 983 | kaddr = kmap(page); |
a52d9a80 CM |
984 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); |
985 | flush_dcache_page(page); | |
b888db2b | 986 | kunmap(page); |
a52d9a80 | 987 | } |
b888db2b | 988 | set_page_dirty(page); |
d1310b2e | 989 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
a52d9a80 | 990 | |
a52d9a80 CM |
991 | return ret; |
992 | } | |
993 | ||
39279cc3 CM |
994 | /* |
995 | * taken from block_truncate_page, but does cow as it zeros out | |
996 | * any bytes left in the last page in the file. | |
997 | */ | |
998 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) | |
999 | { | |
1000 | struct inode *inode = mapping->host; | |
db94535d CM |
1001 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1002 | u32 blocksize = root->sectorsize; | |
39279cc3 CM |
1003 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
1004 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
1005 | struct page *page; | |
39279cc3 | 1006 | int ret = 0; |
a52d9a80 | 1007 | u64 page_start; |
39279cc3 CM |
1008 | |
1009 | if ((offset & (blocksize - 1)) == 0) | |
1010 | goto out; | |
1011 | ||
1012 | ret = -ENOMEM; | |
1013 | page = grab_cache_page(mapping, index); | |
1014 | if (!page) | |
1015 | goto out; | |
39279cc3 | 1016 | if (!PageUptodate(page)) { |
9ebefb18 | 1017 | ret = btrfs_readpage(NULL, page); |
39279cc3 CM |
1018 | lock_page(page); |
1019 | if (!PageUptodate(page)) { | |
1020 | ret = -EIO; | |
1021 | goto out; | |
1022 | } | |
1023 | } | |
35ebb934 | 1024 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
a52d9a80 | 1025 | |
b888db2b | 1026 | ret = btrfs_cow_one_page(inode, page, offset); |
39279cc3 | 1027 | |
39279cc3 CM |
1028 | unlock_page(page); |
1029 | page_cache_release(page); | |
1030 | out: | |
1031 | return ret; | |
1032 | } | |
1033 | ||
1034 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) | |
1035 | { | |
1036 | struct inode *inode = dentry->d_inode; | |
1037 | int err; | |
1038 | ||
1039 | err = inode_change_ok(inode, attr); | |
1040 | if (err) | |
1041 | return err; | |
1042 | ||
1043 | if (S_ISREG(inode->i_mode) && | |
1044 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { | |
1045 | struct btrfs_trans_handle *trans; | |
1046 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d1310b2e | 1047 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2bf5a725 | 1048 | |
5f39d397 | 1049 | u64 mask = root->sectorsize - 1; |
1b0f7c29 | 1050 | u64 hole_start = (inode->i_size + mask) & ~mask; |
f392a938 | 1051 | u64 block_end = (attr->ia_size + mask) & ~mask; |
39279cc3 | 1052 | u64 hole_size; |
179e29e4 | 1053 | u64 alloc_hint = 0; |
39279cc3 | 1054 | |
1b0f7c29 | 1055 | if (attr->ia_size <= hole_start) |
39279cc3 CM |
1056 | goto out; |
1057 | ||
1832a6d5 CM |
1058 | mutex_lock(&root->fs_info->fs_mutex); |
1059 | err = btrfs_check_free_space(root, 1, 0); | |
1060 | mutex_unlock(&root->fs_info->fs_mutex); | |
1061 | if (err) | |
1062 | goto fail; | |
1063 | ||
39279cc3 CM |
1064 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
1065 | ||
1b0f7c29 | 1066 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
5f56406a | 1067 | hole_size = block_end - hole_start; |
39279cc3 CM |
1068 | |
1069 | mutex_lock(&root->fs_info->fs_mutex); | |
1070 | trans = btrfs_start_transaction(root, 1); | |
1071 | btrfs_set_trans_block_group(trans, inode); | |
2bf5a725 | 1072 | err = btrfs_drop_extents(trans, root, inode, |
1b0f7c29 | 1073 | hole_start, block_end, hole_start, |
3326d1b0 | 1074 | &alloc_hint); |
2bf5a725 | 1075 | |
179e29e4 CM |
1076 | if (alloc_hint != EXTENT_MAP_INLINE) { |
1077 | err = btrfs_insert_file_extent(trans, root, | |
1078 | inode->i_ino, | |
5f56406a CM |
1079 | hole_start, 0, 0, |
1080 | hole_size); | |
d1310b2e CM |
1081 | btrfs_drop_extent_cache(inode, hole_start, |
1082 | hole_size - 1); | |
5f56406a | 1083 | btrfs_check_file(root, inode); |
179e29e4 | 1084 | } |
39279cc3 CM |
1085 | btrfs_end_transaction(trans, root); |
1086 | mutex_unlock(&root->fs_info->fs_mutex); | |
1b0f7c29 | 1087 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
54aa1f4d CM |
1088 | if (err) |
1089 | return err; | |
39279cc3 CM |
1090 | } |
1091 | out: | |
1092 | err = inode_setattr(inode, attr); | |
1832a6d5 | 1093 | fail: |
39279cc3 CM |
1094 | return err; |
1095 | } | |
61295eb8 | 1096 | |
2da98f00 | 1097 | void btrfs_put_inode(struct inode *inode) |
61295eb8 | 1098 | { |
2da98f00 CM |
1099 | int ret; |
1100 | ||
1101 | if (!BTRFS_I(inode)->ordered_trans) { | |
61295eb8 CM |
1102 | return; |
1103 | } | |
2da98f00 CM |
1104 | |
1105 | if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) || | |
1106 | mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)) | |
1107 | return; | |
1108 | ||
1109 | ret = btrfs_del_ordered_inode(inode); | |
1110 | if (ret == 1) { | |
1111 | atomic_dec(&inode->i_count); | |
1112 | } | |
61295eb8 CM |
1113 | } |
1114 | ||
39279cc3 CM |
1115 | void btrfs_delete_inode(struct inode *inode) |
1116 | { | |
1117 | struct btrfs_trans_handle *trans; | |
1118 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d3c2fdcf | 1119 | unsigned long nr; |
39279cc3 CM |
1120 | int ret; |
1121 | ||
1122 | truncate_inode_pages(&inode->i_data, 0); | |
1123 | if (is_bad_inode(inode)) { | |
1124 | goto no_delete; | |
1125 | } | |
5f39d397 | 1126 | |
39279cc3 CM |
1127 | inode->i_size = 0; |
1128 | mutex_lock(&root->fs_info->fs_mutex); | |
1129 | trans = btrfs_start_transaction(root, 1); | |
5f39d397 | 1130 | |
39279cc3 | 1131 | btrfs_set_trans_block_group(trans, inode); |
85e21bac | 1132 | ret = btrfs_truncate_in_trans(trans, root, inode, 0); |
54aa1f4d CM |
1133 | if (ret) |
1134 | goto no_delete_lock; | |
85e21bac | 1135 | |
d3c2fdcf | 1136 | nr = trans->blocks_used; |
85e21bac | 1137 | clear_inode(inode); |
5f39d397 | 1138 | |
39279cc3 CM |
1139 | btrfs_end_transaction(trans, root); |
1140 | mutex_unlock(&root->fs_info->fs_mutex); | |
d3c2fdcf | 1141 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 1142 | btrfs_throttle(root); |
39279cc3 | 1143 | return; |
54aa1f4d CM |
1144 | |
1145 | no_delete_lock: | |
d3c2fdcf | 1146 | nr = trans->blocks_used; |
54aa1f4d CM |
1147 | btrfs_end_transaction(trans, root); |
1148 | mutex_unlock(&root->fs_info->fs_mutex); | |
d3c2fdcf | 1149 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 1150 | btrfs_throttle(root); |
39279cc3 CM |
1151 | no_delete: |
1152 | clear_inode(inode); | |
1153 | } | |
1154 | ||
1155 | /* | |
1156 | * this returns the key found in the dir entry in the location pointer. | |
1157 | * If no dir entries were found, location->objectid is 0. | |
1158 | */ | |
1159 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
1160 | struct btrfs_key *location) | |
1161 | { | |
1162 | const char *name = dentry->d_name.name; | |
1163 | int namelen = dentry->d_name.len; | |
1164 | struct btrfs_dir_item *di; | |
1165 | struct btrfs_path *path; | |
1166 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 1167 | int ret = 0; |
39279cc3 | 1168 | |
3954401f CM |
1169 | if (namelen == 1 && strcmp(name, ".") == 0) { |
1170 | location->objectid = dir->i_ino; | |
1171 | location->type = BTRFS_INODE_ITEM_KEY; | |
1172 | location->offset = 0; | |
1173 | return 0; | |
1174 | } | |
39279cc3 CM |
1175 | path = btrfs_alloc_path(); |
1176 | BUG_ON(!path); | |
3954401f | 1177 | |
7a720536 | 1178 | if (namelen == 2 && strcmp(name, "..") == 0) { |
3954401f CM |
1179 | struct btrfs_key key; |
1180 | struct extent_buffer *leaf; | |
1181 | u32 nritems; | |
1182 | int slot; | |
1183 | ||
1184 | key.objectid = dir->i_ino; | |
1185 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); | |
1186 | key.offset = 0; | |
1187 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1188 | BUG_ON(ret == 0); | |
1189 | ret = 0; | |
1190 | ||
1191 | leaf = path->nodes[0]; | |
1192 | slot = path->slots[0]; | |
1193 | nritems = btrfs_header_nritems(leaf); | |
1194 | if (slot >= nritems) | |
1195 | goto out_err; | |
1196 | ||
1197 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
1198 | if (key.objectid != dir->i_ino || | |
1199 | key.type != BTRFS_INODE_REF_KEY) { | |
1200 | goto out_err; | |
1201 | } | |
1202 | location->objectid = key.offset; | |
1203 | location->type = BTRFS_INODE_ITEM_KEY; | |
1204 | location->offset = 0; | |
1205 | goto out; | |
1206 | } | |
1207 | ||
39279cc3 CM |
1208 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
1209 | namelen, 0); | |
0d9f7f3e Y |
1210 | if (IS_ERR(di)) |
1211 | ret = PTR_ERR(di); | |
39279cc3 | 1212 | if (!di || IS_ERR(di)) { |
3954401f | 1213 | goto out_err; |
39279cc3 | 1214 | } |
5f39d397 | 1215 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 1216 | out: |
39279cc3 CM |
1217 | btrfs_free_path(path); |
1218 | return ret; | |
3954401f CM |
1219 | out_err: |
1220 | location->objectid = 0; | |
1221 | goto out; | |
39279cc3 CM |
1222 | } |
1223 | ||
1224 | /* | |
1225 | * when we hit a tree root in a directory, the btrfs part of the inode | |
1226 | * needs to be changed to reflect the root directory of the tree root. This | |
1227 | * is kind of like crossing a mount point. | |
1228 | */ | |
1229 | static int fixup_tree_root_location(struct btrfs_root *root, | |
1230 | struct btrfs_key *location, | |
58176a96 JB |
1231 | struct btrfs_root **sub_root, |
1232 | struct dentry *dentry) | |
39279cc3 CM |
1233 | { |
1234 | struct btrfs_path *path; | |
1235 | struct btrfs_root_item *ri; | |
1236 | ||
1237 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) | |
1238 | return 0; | |
1239 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) | |
1240 | return 0; | |
1241 | ||
1242 | path = btrfs_alloc_path(); | |
1243 | BUG_ON(!path); | |
1244 | mutex_lock(&root->fs_info->fs_mutex); | |
1245 | ||
58176a96 JB |
1246 | *sub_root = btrfs_read_fs_root(root->fs_info, location, |
1247 | dentry->d_name.name, | |
1248 | dentry->d_name.len); | |
39279cc3 CM |
1249 | if (IS_ERR(*sub_root)) |
1250 | return PTR_ERR(*sub_root); | |
1251 | ||
1252 | ri = &(*sub_root)->root_item; | |
1253 | location->objectid = btrfs_root_dirid(ri); | |
39279cc3 CM |
1254 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
1255 | location->offset = 0; | |
1256 | ||
1257 | btrfs_free_path(path); | |
1258 | mutex_unlock(&root->fs_info->fs_mutex); | |
1259 | return 0; | |
1260 | } | |
1261 | ||
1262 | static int btrfs_init_locked_inode(struct inode *inode, void *p) | |
1263 | { | |
1264 | struct btrfs_iget_args *args = p; | |
1265 | inode->i_ino = args->ino; | |
1266 | BTRFS_I(inode)->root = args->root; | |
9069218d | 1267 | BTRFS_I(inode)->delalloc_bytes = 0; |
d1310b2e CM |
1268 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
1269 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | |
b888db2b | 1270 | inode->i_mapping, GFP_NOFS); |
39279cc3 CM |
1271 | return 0; |
1272 | } | |
1273 | ||
1274 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
1275 | { | |
1276 | struct btrfs_iget_args *args = opaque; | |
1277 | return (args->ino == inode->i_ino && | |
1278 | args->root == BTRFS_I(inode)->root); | |
1279 | } | |
1280 | ||
dc17ff8f CM |
1281 | struct inode *btrfs_ilookup(struct super_block *s, u64 objectid, |
1282 | u64 root_objectid) | |
1283 | { | |
1284 | struct btrfs_iget_args args; | |
1285 | args.ino = objectid; | |
1286 | args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid); | |
1287 | ||
1288 | if (!args.root) | |
1289 | return NULL; | |
1290 | ||
1291 | return ilookup5(s, objectid, btrfs_find_actor, (void *)&args); | |
1292 | } | |
1293 | ||
39279cc3 CM |
1294 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
1295 | struct btrfs_root *root) | |
1296 | { | |
1297 | struct inode *inode; | |
1298 | struct btrfs_iget_args args; | |
1299 | args.ino = objectid; | |
1300 | args.root = root; | |
1301 | ||
1302 | inode = iget5_locked(s, objectid, btrfs_find_actor, | |
1303 | btrfs_init_locked_inode, | |
1304 | (void *)&args); | |
1305 | return inode; | |
1306 | } | |
1307 | ||
1308 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, | |
1309 | struct nameidata *nd) | |
1310 | { | |
1311 | struct inode * inode; | |
1312 | struct btrfs_inode *bi = BTRFS_I(dir); | |
1313 | struct btrfs_root *root = bi->root; | |
1314 | struct btrfs_root *sub_root = root; | |
1315 | struct btrfs_key location; | |
1316 | int ret; | |
1317 | ||
1318 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
1319 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 1320 | |
39279cc3 CM |
1321 | mutex_lock(&root->fs_info->fs_mutex); |
1322 | ret = btrfs_inode_by_name(dir, dentry, &location); | |
1323 | mutex_unlock(&root->fs_info->fs_mutex); | |
5f39d397 | 1324 | |
39279cc3 CM |
1325 | if (ret < 0) |
1326 | return ERR_PTR(ret); | |
5f39d397 | 1327 | |
39279cc3 CM |
1328 | inode = NULL; |
1329 | if (location.objectid) { | |
58176a96 JB |
1330 | ret = fixup_tree_root_location(root, &location, &sub_root, |
1331 | dentry); | |
39279cc3 CM |
1332 | if (ret < 0) |
1333 | return ERR_PTR(ret); | |
1334 | if (ret > 0) | |
1335 | return ERR_PTR(-ENOENT); | |
1336 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, | |
1337 | sub_root); | |
1338 | if (!inode) | |
1339 | return ERR_PTR(-EACCES); | |
1340 | if (inode->i_state & I_NEW) { | |
1341 | /* the inode and parent dir are two different roots */ | |
1342 | if (sub_root != root) { | |
1343 | igrab(inode); | |
1344 | sub_root->inode = inode; | |
1345 | } | |
1346 | BTRFS_I(inode)->root = sub_root; | |
1347 | memcpy(&BTRFS_I(inode)->location, &location, | |
1348 | sizeof(location)); | |
1349 | btrfs_read_locked_inode(inode); | |
1350 | unlock_new_inode(inode); | |
1351 | } | |
1352 | } | |
1353 | return d_splice_alias(inode, dentry); | |
1354 | } | |
1355 | ||
39279cc3 CM |
1356 | static unsigned char btrfs_filetype_table[] = { |
1357 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK | |
1358 | }; | |
1359 | ||
1360 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) | |
1361 | { | |
6da6abae | 1362 | struct inode *inode = filp->f_dentry->d_inode; |
39279cc3 CM |
1363 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1364 | struct btrfs_item *item; | |
1365 | struct btrfs_dir_item *di; | |
1366 | struct btrfs_key key; | |
5f39d397 | 1367 | struct btrfs_key found_key; |
39279cc3 CM |
1368 | struct btrfs_path *path; |
1369 | int ret; | |
1370 | u32 nritems; | |
5f39d397 | 1371 | struct extent_buffer *leaf; |
39279cc3 CM |
1372 | int slot; |
1373 | int advance; | |
1374 | unsigned char d_type; | |
1375 | int over = 0; | |
1376 | u32 di_cur; | |
1377 | u32 di_total; | |
1378 | u32 di_len; | |
1379 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
1380 | char tmp_name[32]; |
1381 | char *name_ptr; | |
1382 | int name_len; | |
39279cc3 CM |
1383 | |
1384 | /* FIXME, use a real flag for deciding about the key type */ | |
1385 | if (root->fs_info->tree_root == root) | |
1386 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 1387 | |
3954401f CM |
1388 | /* special case for "." */ |
1389 | if (filp->f_pos == 0) { | |
1390 | over = filldir(dirent, ".", 1, | |
1391 | 1, inode->i_ino, | |
1392 | DT_DIR); | |
1393 | if (over) | |
1394 | return 0; | |
1395 | filp->f_pos = 1; | |
1396 | } | |
1397 | ||
39279cc3 CM |
1398 | mutex_lock(&root->fs_info->fs_mutex); |
1399 | key.objectid = inode->i_ino; | |
3954401f CM |
1400 | path = btrfs_alloc_path(); |
1401 | path->reada = 2; | |
1402 | ||
1403 | /* special case for .., just use the back ref */ | |
1404 | if (filp->f_pos == 1) { | |
1405 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); | |
1406 | key.offset = 0; | |
1407 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1408 | BUG_ON(ret == 0); | |
1409 | leaf = path->nodes[0]; | |
1410 | slot = path->slots[0]; | |
1411 | nritems = btrfs_header_nritems(leaf); | |
1412 | if (slot >= nritems) { | |
1413 | btrfs_release_path(root, path); | |
1414 | goto read_dir_items; | |
1415 | } | |
1416 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
1417 | btrfs_release_path(root, path); | |
1418 | if (found_key.objectid != key.objectid || | |
1419 | found_key.type != BTRFS_INODE_REF_KEY) | |
1420 | goto read_dir_items; | |
1421 | over = filldir(dirent, "..", 2, | |
1422 | 2, found_key.offset, DT_DIR); | |
1423 | if (over) | |
1424 | goto nopos; | |
1425 | filp->f_pos = 2; | |
1426 | } | |
1427 | ||
1428 | read_dir_items: | |
39279cc3 CM |
1429 | btrfs_set_key_type(&key, key_type); |
1430 | key.offset = filp->f_pos; | |
5f39d397 | 1431 | |
39279cc3 CM |
1432 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
1433 | if (ret < 0) | |
1434 | goto err; | |
1435 | advance = 0; | |
39279cc3 | 1436 | while(1) { |
5f39d397 CM |
1437 | leaf = path->nodes[0]; |
1438 | nritems = btrfs_header_nritems(leaf); | |
39279cc3 CM |
1439 | slot = path->slots[0]; |
1440 | if (advance || slot >= nritems) { | |
1441 | if (slot >= nritems -1) { | |
39279cc3 CM |
1442 | ret = btrfs_next_leaf(root, path); |
1443 | if (ret) | |
1444 | break; | |
5f39d397 CM |
1445 | leaf = path->nodes[0]; |
1446 | nritems = btrfs_header_nritems(leaf); | |
39279cc3 CM |
1447 | slot = path->slots[0]; |
1448 | } else { | |
1449 | slot++; | |
1450 | path->slots[0]++; | |
1451 | } | |
1452 | } | |
1453 | advance = 1; | |
5f39d397 CM |
1454 | item = btrfs_item_nr(leaf, slot); |
1455 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
1456 | ||
1457 | if (found_key.objectid != key.objectid) | |
39279cc3 | 1458 | break; |
5f39d397 | 1459 | if (btrfs_key_type(&found_key) != key_type) |
39279cc3 | 1460 | break; |
5f39d397 | 1461 | if (found_key.offset < filp->f_pos) |
39279cc3 | 1462 | continue; |
5f39d397 CM |
1463 | |
1464 | filp->f_pos = found_key.offset; | |
39279cc3 CM |
1465 | advance = 1; |
1466 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); | |
1467 | di_cur = 0; | |
5f39d397 | 1468 | di_total = btrfs_item_size(leaf, item); |
39279cc3 | 1469 | while(di_cur < di_total) { |
5f39d397 CM |
1470 | struct btrfs_key location; |
1471 | ||
1472 | name_len = btrfs_dir_name_len(leaf, di); | |
1473 | if (name_len < 32) { | |
1474 | name_ptr = tmp_name; | |
1475 | } else { | |
1476 | name_ptr = kmalloc(name_len, GFP_NOFS); | |
1477 | BUG_ON(!name_ptr); | |
1478 | } | |
1479 | read_extent_buffer(leaf, name_ptr, | |
1480 | (unsigned long)(di + 1), name_len); | |
1481 | ||
1482 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
1483 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
5f39d397 CM |
1484 | over = filldir(dirent, name_ptr, name_len, |
1485 | found_key.offset, | |
1486 | location.objectid, | |
39279cc3 | 1487 | d_type); |
5f39d397 CM |
1488 | |
1489 | if (name_ptr != tmp_name) | |
1490 | kfree(name_ptr); | |
1491 | ||
39279cc3 CM |
1492 | if (over) |
1493 | goto nopos; | |
5103e947 JB |
1494 | di_len = btrfs_dir_name_len(leaf, di) + |
1495 | btrfs_dir_data_len(leaf, di) +sizeof(*di); | |
39279cc3 CM |
1496 | di_cur += di_len; |
1497 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
1498 | } | |
1499 | } | |
5e591a07 YZ |
1500 | if (key_type == BTRFS_DIR_INDEX_KEY) |
1501 | filp->f_pos = INT_LIMIT(typeof(filp->f_pos)); | |
1502 | else | |
1503 | filp->f_pos++; | |
39279cc3 CM |
1504 | nopos: |
1505 | ret = 0; | |
1506 | err: | |
1507 | btrfs_release_path(root, path); | |
1508 | btrfs_free_path(path); | |
1509 | mutex_unlock(&root->fs_info->fs_mutex); | |
1510 | return ret; | |
1511 | } | |
1512 | ||
1513 | int btrfs_write_inode(struct inode *inode, int wait) | |
1514 | { | |
1515 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1516 | struct btrfs_trans_handle *trans; | |
1517 | int ret = 0; | |
1518 | ||
1519 | if (wait) { | |
1520 | mutex_lock(&root->fs_info->fs_mutex); | |
1521 | trans = btrfs_start_transaction(root, 1); | |
1522 | btrfs_set_trans_block_group(trans, inode); | |
1523 | ret = btrfs_commit_transaction(trans, root); | |
1524 | mutex_unlock(&root->fs_info->fs_mutex); | |
1525 | } | |
1526 | return ret; | |
1527 | } | |
1528 | ||
1529 | /* | |
54aa1f4d | 1530 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
1531 | * inode changes. But, it is most likely to find the inode in cache. |
1532 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
1533 | * to keep or drop this code. | |
1534 | */ | |
1535 | void btrfs_dirty_inode(struct inode *inode) | |
1536 | { | |
1537 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1538 | struct btrfs_trans_handle *trans; | |
1539 | ||
1540 | mutex_lock(&root->fs_info->fs_mutex); | |
1541 | trans = btrfs_start_transaction(root, 1); | |
1542 | btrfs_set_trans_block_group(trans, inode); | |
1543 | btrfs_update_inode(trans, root, inode); | |
1544 | btrfs_end_transaction(trans, root); | |
1545 | mutex_unlock(&root->fs_info->fs_mutex); | |
39279cc3 CM |
1546 | } |
1547 | ||
1548 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, | |
1549 | struct btrfs_root *root, | |
9c58309d CM |
1550 | const char *name, int name_len, |
1551 | u64 ref_objectid, | |
39279cc3 CM |
1552 | u64 objectid, |
1553 | struct btrfs_block_group_cache *group, | |
1554 | int mode) | |
1555 | { | |
1556 | struct inode *inode; | |
5f39d397 | 1557 | struct btrfs_inode_item *inode_item; |
6324fbf3 | 1558 | struct btrfs_block_group_cache *new_inode_group; |
39279cc3 | 1559 | struct btrfs_key *location; |
5f39d397 | 1560 | struct btrfs_path *path; |
9c58309d CM |
1561 | struct btrfs_inode_ref *ref; |
1562 | struct btrfs_key key[2]; | |
1563 | u32 sizes[2]; | |
1564 | unsigned long ptr; | |
39279cc3 CM |
1565 | int ret; |
1566 | int owner; | |
1567 | ||
5f39d397 CM |
1568 | path = btrfs_alloc_path(); |
1569 | BUG_ON(!path); | |
1570 | ||
39279cc3 CM |
1571 | inode = new_inode(root->fs_info->sb); |
1572 | if (!inode) | |
1573 | return ERR_PTR(-ENOMEM); | |
1574 | ||
d1310b2e CM |
1575 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
1576 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | |
b888db2b | 1577 | inode->i_mapping, GFP_NOFS); |
9069218d | 1578 | BTRFS_I(inode)->delalloc_bytes = 0; |
39279cc3 | 1579 | BTRFS_I(inode)->root = root; |
b888db2b | 1580 | |
39279cc3 CM |
1581 | if (mode & S_IFDIR) |
1582 | owner = 0; | |
1583 | else | |
1584 | owner = 1; | |
6324fbf3 | 1585 | new_inode_group = btrfs_find_block_group(root, group, 0, |
0b86a832 | 1586 | BTRFS_BLOCK_GROUP_METADATA, owner); |
6324fbf3 CM |
1587 | if (!new_inode_group) { |
1588 | printk("find_block group failed\n"); | |
1589 | new_inode_group = group; | |
1590 | } | |
1591 | BTRFS_I(inode)->block_group = new_inode_group; | |
b98b6767 | 1592 | BTRFS_I(inode)->flags = 0; |
9c58309d CM |
1593 | |
1594 | key[0].objectid = objectid; | |
1595 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); | |
1596 | key[0].offset = 0; | |
1597 | ||
1598 | key[1].objectid = objectid; | |
1599 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); | |
1600 | key[1].offset = ref_objectid; | |
1601 | ||
1602 | sizes[0] = sizeof(struct btrfs_inode_item); | |
1603 | sizes[1] = name_len + sizeof(*ref); | |
1604 | ||
1605 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); | |
1606 | if (ret != 0) | |
5f39d397 CM |
1607 | goto fail; |
1608 | ||
9c58309d CM |
1609 | if (objectid > root->highest_inode) |
1610 | root->highest_inode = objectid; | |
1611 | ||
39279cc3 CM |
1612 | inode->i_uid = current->fsuid; |
1613 | inode->i_gid = current->fsgid; | |
1614 | inode->i_mode = mode; | |
1615 | inode->i_ino = objectid; | |
1616 | inode->i_blocks = 0; | |
1617 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
5f39d397 CM |
1618 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
1619 | struct btrfs_inode_item); | |
1620 | fill_inode_item(path->nodes[0], inode_item, inode); | |
9c58309d CM |
1621 | |
1622 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
1623 | struct btrfs_inode_ref); | |
1624 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
1625 | ptr = (unsigned long)(ref + 1); | |
1626 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
1627 | ||
5f39d397 CM |
1628 | btrfs_mark_buffer_dirty(path->nodes[0]); |
1629 | btrfs_free_path(path); | |
1630 | ||
39279cc3 CM |
1631 | location = &BTRFS_I(inode)->location; |
1632 | location->objectid = objectid; | |
39279cc3 CM |
1633 | location->offset = 0; |
1634 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | |
1635 | ||
39279cc3 CM |
1636 | insert_inode_hash(inode); |
1637 | return inode; | |
5f39d397 CM |
1638 | fail: |
1639 | btrfs_free_path(path); | |
1640 | return ERR_PTR(ret); | |
39279cc3 CM |
1641 | } |
1642 | ||
1643 | static inline u8 btrfs_inode_type(struct inode *inode) | |
1644 | { | |
1645 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
1646 | } | |
1647 | ||
1648 | static int btrfs_add_link(struct btrfs_trans_handle *trans, | |
9c58309d CM |
1649 | struct dentry *dentry, struct inode *inode, |
1650 | int add_backref) | |
39279cc3 CM |
1651 | { |
1652 | int ret; | |
1653 | struct btrfs_key key; | |
1654 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; | |
79c44584 | 1655 | struct inode *parent_inode; |
5f39d397 | 1656 | |
39279cc3 | 1657 | key.objectid = inode->i_ino; |
39279cc3 CM |
1658 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
1659 | key.offset = 0; | |
1660 | ||
1661 | ret = btrfs_insert_dir_item(trans, root, | |
1662 | dentry->d_name.name, dentry->d_name.len, | |
1663 | dentry->d_parent->d_inode->i_ino, | |
1664 | &key, btrfs_inode_type(inode)); | |
1665 | if (ret == 0) { | |
9c58309d CM |
1666 | if (add_backref) { |
1667 | ret = btrfs_insert_inode_ref(trans, root, | |
1668 | dentry->d_name.name, | |
1669 | dentry->d_name.len, | |
1670 | inode->i_ino, | |
1671 | dentry->d_parent->d_inode->i_ino); | |
1672 | } | |
79c44584 CM |
1673 | parent_inode = dentry->d_parent->d_inode; |
1674 | parent_inode->i_size += dentry->d_name.len * 2; | |
1675 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; | |
39279cc3 CM |
1676 | ret = btrfs_update_inode(trans, root, |
1677 | dentry->d_parent->d_inode); | |
1678 | } | |
1679 | return ret; | |
1680 | } | |
1681 | ||
1682 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
9c58309d CM |
1683 | struct dentry *dentry, struct inode *inode, |
1684 | int backref) | |
39279cc3 | 1685 | { |
9c58309d | 1686 | int err = btrfs_add_link(trans, dentry, inode, backref); |
39279cc3 CM |
1687 | if (!err) { |
1688 | d_instantiate(dentry, inode); | |
1689 | return 0; | |
1690 | } | |
1691 | if (err > 0) | |
1692 | err = -EEXIST; | |
1693 | return err; | |
1694 | } | |
1695 | ||
618e21d5 JB |
1696 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
1697 | int mode, dev_t rdev) | |
1698 | { | |
1699 | struct btrfs_trans_handle *trans; | |
1700 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 1701 | struct inode *inode = NULL; |
618e21d5 JB |
1702 | int err; |
1703 | int drop_inode = 0; | |
1704 | u64 objectid; | |
1832a6d5 | 1705 | unsigned long nr = 0; |
618e21d5 JB |
1706 | |
1707 | if (!new_valid_dev(rdev)) | |
1708 | return -EINVAL; | |
1709 | ||
1710 | mutex_lock(&root->fs_info->fs_mutex); | |
1832a6d5 CM |
1711 | err = btrfs_check_free_space(root, 1, 0); |
1712 | if (err) | |
1713 | goto fail; | |
1714 | ||
618e21d5 JB |
1715 | trans = btrfs_start_transaction(root, 1); |
1716 | btrfs_set_trans_block_group(trans, dir); | |
1717 | ||
1718 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
1719 | if (err) { | |
1720 | err = -ENOSPC; | |
1721 | goto out_unlock; | |
1722 | } | |
1723 | ||
9c58309d CM |
1724 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
1725 | dentry->d_name.len, | |
1726 | dentry->d_parent->d_inode->i_ino, objectid, | |
618e21d5 JB |
1727 | BTRFS_I(dir)->block_group, mode); |
1728 | err = PTR_ERR(inode); | |
1729 | if (IS_ERR(inode)) | |
1730 | goto out_unlock; | |
1731 | ||
1732 | btrfs_set_trans_block_group(trans, inode); | |
9c58309d | 1733 | err = btrfs_add_nondir(trans, dentry, inode, 0); |
618e21d5 JB |
1734 | if (err) |
1735 | drop_inode = 1; | |
1736 | else { | |
1737 | inode->i_op = &btrfs_special_inode_operations; | |
1738 | init_special_inode(inode, inode->i_mode, rdev); | |
1b4ab1bb | 1739 | btrfs_update_inode(trans, root, inode); |
618e21d5 JB |
1740 | } |
1741 | dir->i_sb->s_dirt = 1; | |
1742 | btrfs_update_inode_block_group(trans, inode); | |
1743 | btrfs_update_inode_block_group(trans, dir); | |
1744 | out_unlock: | |
d3c2fdcf | 1745 | nr = trans->blocks_used; |
618e21d5 | 1746 | btrfs_end_transaction(trans, root); |
1832a6d5 | 1747 | fail: |
618e21d5 JB |
1748 | mutex_unlock(&root->fs_info->fs_mutex); |
1749 | ||
1750 | if (drop_inode) { | |
1751 | inode_dec_link_count(inode); | |
1752 | iput(inode); | |
1753 | } | |
d3c2fdcf | 1754 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 1755 | btrfs_throttle(root); |
618e21d5 JB |
1756 | return err; |
1757 | } | |
1758 | ||
39279cc3 CM |
1759 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
1760 | int mode, struct nameidata *nd) | |
1761 | { | |
1762 | struct btrfs_trans_handle *trans; | |
1763 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 1764 | struct inode *inode = NULL; |
39279cc3 CM |
1765 | int err; |
1766 | int drop_inode = 0; | |
1832a6d5 | 1767 | unsigned long nr = 0; |
39279cc3 CM |
1768 | u64 objectid; |
1769 | ||
1770 | mutex_lock(&root->fs_info->fs_mutex); | |
1832a6d5 CM |
1771 | err = btrfs_check_free_space(root, 1, 0); |
1772 | if (err) | |
1773 | goto fail; | |
39279cc3 CM |
1774 | trans = btrfs_start_transaction(root, 1); |
1775 | btrfs_set_trans_block_group(trans, dir); | |
1776 | ||
1777 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
1778 | if (err) { | |
1779 | err = -ENOSPC; | |
1780 | goto out_unlock; | |
1781 | } | |
1782 | ||
9c58309d CM |
1783 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
1784 | dentry->d_name.len, | |
1785 | dentry->d_parent->d_inode->i_ino, | |
1786 | objectid, BTRFS_I(dir)->block_group, mode); | |
39279cc3 CM |
1787 | err = PTR_ERR(inode); |
1788 | if (IS_ERR(inode)) | |
1789 | goto out_unlock; | |
1790 | ||
1791 | btrfs_set_trans_block_group(trans, inode); | |
9c58309d | 1792 | err = btrfs_add_nondir(trans, dentry, inode, 0); |
39279cc3 CM |
1793 | if (err) |
1794 | drop_inode = 1; | |
1795 | else { | |
1796 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 1797 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 CM |
1798 | inode->i_fop = &btrfs_file_operations; |
1799 | inode->i_op = &btrfs_file_inode_operations; | |
d1310b2e CM |
1800 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
1801 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | |
a52d9a80 | 1802 | inode->i_mapping, GFP_NOFS); |
9069218d | 1803 | BTRFS_I(inode)->delalloc_bytes = 0; |
d1310b2e | 1804 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
1805 | } |
1806 | dir->i_sb->s_dirt = 1; | |
1807 | btrfs_update_inode_block_group(trans, inode); | |
1808 | btrfs_update_inode_block_group(trans, dir); | |
1809 | out_unlock: | |
d3c2fdcf | 1810 | nr = trans->blocks_used; |
39279cc3 | 1811 | btrfs_end_transaction(trans, root); |
1832a6d5 | 1812 | fail: |
39279cc3 CM |
1813 | mutex_unlock(&root->fs_info->fs_mutex); |
1814 | ||
1815 | if (drop_inode) { | |
1816 | inode_dec_link_count(inode); | |
1817 | iput(inode); | |
1818 | } | |
d3c2fdcf | 1819 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 1820 | btrfs_throttle(root); |
39279cc3 CM |
1821 | return err; |
1822 | } | |
1823 | ||
1824 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
1825 | struct dentry *dentry) | |
1826 | { | |
1827 | struct btrfs_trans_handle *trans; | |
1828 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1829 | struct inode *inode = old_dentry->d_inode; | |
1832a6d5 | 1830 | unsigned long nr = 0; |
39279cc3 CM |
1831 | int err; |
1832 | int drop_inode = 0; | |
1833 | ||
1834 | if (inode->i_nlink == 0) | |
1835 | return -ENOENT; | |
1836 | ||
6da6abae CM |
1837 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
1838 | inode->i_nlink++; | |
1839 | #else | |
39279cc3 | 1840 | inc_nlink(inode); |
6da6abae | 1841 | #endif |
39279cc3 | 1842 | mutex_lock(&root->fs_info->fs_mutex); |
1832a6d5 CM |
1843 | err = btrfs_check_free_space(root, 1, 0); |
1844 | if (err) | |
1845 | goto fail; | |
39279cc3 | 1846 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 1847 | |
39279cc3 CM |
1848 | btrfs_set_trans_block_group(trans, dir); |
1849 | atomic_inc(&inode->i_count); | |
9c58309d | 1850 | err = btrfs_add_nondir(trans, dentry, inode, 1); |
5f39d397 | 1851 | |
39279cc3 CM |
1852 | if (err) |
1853 | drop_inode = 1; | |
5f39d397 | 1854 | |
39279cc3 CM |
1855 | dir->i_sb->s_dirt = 1; |
1856 | btrfs_update_inode_block_group(trans, dir); | |
54aa1f4d | 1857 | err = btrfs_update_inode(trans, root, inode); |
5f39d397 | 1858 | |
54aa1f4d CM |
1859 | if (err) |
1860 | drop_inode = 1; | |
39279cc3 | 1861 | |
d3c2fdcf | 1862 | nr = trans->blocks_used; |
39279cc3 | 1863 | btrfs_end_transaction(trans, root); |
1832a6d5 | 1864 | fail: |
39279cc3 CM |
1865 | mutex_unlock(&root->fs_info->fs_mutex); |
1866 | ||
1867 | if (drop_inode) { | |
1868 | inode_dec_link_count(inode); | |
1869 | iput(inode); | |
1870 | } | |
d3c2fdcf | 1871 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 1872 | btrfs_throttle(root); |
39279cc3 CM |
1873 | return err; |
1874 | } | |
1875 | ||
39279cc3 CM |
1876 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
1877 | { | |
1878 | struct inode *inode; | |
1879 | struct btrfs_trans_handle *trans; | |
1880 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1881 | int err = 0; | |
1882 | int drop_on_err = 0; | |
1883 | u64 objectid; | |
d3c2fdcf | 1884 | unsigned long nr = 1; |
39279cc3 CM |
1885 | |
1886 | mutex_lock(&root->fs_info->fs_mutex); | |
1832a6d5 CM |
1887 | err = btrfs_check_free_space(root, 1, 0); |
1888 | if (err) | |
1889 | goto out_unlock; | |
1890 | ||
39279cc3 CM |
1891 | trans = btrfs_start_transaction(root, 1); |
1892 | btrfs_set_trans_block_group(trans, dir); | |
5f39d397 | 1893 | |
39279cc3 CM |
1894 | if (IS_ERR(trans)) { |
1895 | err = PTR_ERR(trans); | |
1896 | goto out_unlock; | |
1897 | } | |
1898 | ||
1899 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
1900 | if (err) { | |
1901 | err = -ENOSPC; | |
1902 | goto out_unlock; | |
1903 | } | |
1904 | ||
9c58309d CM |
1905 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
1906 | dentry->d_name.len, | |
1907 | dentry->d_parent->d_inode->i_ino, objectid, | |
39279cc3 CM |
1908 | BTRFS_I(dir)->block_group, S_IFDIR | mode); |
1909 | if (IS_ERR(inode)) { | |
1910 | err = PTR_ERR(inode); | |
1911 | goto out_fail; | |
1912 | } | |
5f39d397 | 1913 | |
39279cc3 CM |
1914 | drop_on_err = 1; |
1915 | inode->i_op = &btrfs_dir_inode_operations; | |
1916 | inode->i_fop = &btrfs_dir_file_operations; | |
1917 | btrfs_set_trans_block_group(trans, inode); | |
1918 | ||
3954401f | 1919 | inode->i_size = 0; |
39279cc3 CM |
1920 | err = btrfs_update_inode(trans, root, inode); |
1921 | if (err) | |
1922 | goto out_fail; | |
5f39d397 | 1923 | |
9c58309d | 1924 | err = btrfs_add_link(trans, dentry, inode, 0); |
39279cc3 CM |
1925 | if (err) |
1926 | goto out_fail; | |
5f39d397 | 1927 | |
39279cc3 CM |
1928 | d_instantiate(dentry, inode); |
1929 | drop_on_err = 0; | |
1930 | dir->i_sb->s_dirt = 1; | |
1931 | btrfs_update_inode_block_group(trans, inode); | |
1932 | btrfs_update_inode_block_group(trans, dir); | |
1933 | ||
1934 | out_fail: | |
d3c2fdcf | 1935 | nr = trans->blocks_used; |
39279cc3 | 1936 | btrfs_end_transaction(trans, root); |
5f39d397 | 1937 | |
39279cc3 CM |
1938 | out_unlock: |
1939 | mutex_unlock(&root->fs_info->fs_mutex); | |
1940 | if (drop_on_err) | |
1941 | iput(inode); | |
d3c2fdcf | 1942 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 1943 | btrfs_throttle(root); |
39279cc3 CM |
1944 | return err; |
1945 | } | |
1946 | ||
a52d9a80 | 1947 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 1948 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
1949 | int create) |
1950 | { | |
1951 | int ret; | |
1952 | int err = 0; | |
db94535d | 1953 | u64 bytenr; |
a52d9a80 CM |
1954 | u64 extent_start = 0; |
1955 | u64 extent_end = 0; | |
1956 | u64 objectid = inode->i_ino; | |
1957 | u32 found_type; | |
a52d9a80 CM |
1958 | struct btrfs_path *path; |
1959 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1960 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
1961 | struct extent_buffer *leaf; |
1962 | struct btrfs_key found_key; | |
a52d9a80 CM |
1963 | struct extent_map *em = NULL; |
1964 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 1965 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 CM |
1966 | struct btrfs_trans_handle *trans = NULL; |
1967 | ||
1968 | path = btrfs_alloc_path(); | |
1969 | BUG_ON(!path); | |
1970 | mutex_lock(&root->fs_info->fs_mutex); | |
1971 | ||
1972 | again: | |
d1310b2e CM |
1973 | spin_lock(&em_tree->lock); |
1974 | em = lookup_extent_mapping(em_tree, start, len); | |
1975 | spin_unlock(&em_tree->lock); | |
1976 | ||
a52d9a80 | 1977 | if (em) { |
56b453c9 | 1978 | if (em->start > start) { |
d1310b2e CM |
1979 | printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n", |
1980 | start, len, em->start, em->len); | |
56b453c9 CM |
1981 | WARN_ON(1); |
1982 | } | |
70dec807 CM |
1983 | if (em->block_start == EXTENT_MAP_INLINE && page) |
1984 | free_extent_map(em); | |
1985 | else | |
1986 | goto out; | |
a52d9a80 | 1987 | } |
d1310b2e | 1988 | em = alloc_extent_map(GFP_NOFS); |
a52d9a80 | 1989 | if (!em) { |
d1310b2e CM |
1990 | err = -ENOMEM; |
1991 | goto out; | |
a52d9a80 | 1992 | } |
d1310b2e CM |
1993 | |
1994 | em->start = EXTENT_MAP_HOLE; | |
1995 | em->len = (u64)-1; | |
a52d9a80 | 1996 | em->bdev = inode->i_sb->s_bdev; |
179e29e4 CM |
1997 | ret = btrfs_lookup_file_extent(trans, root, path, |
1998 | objectid, start, trans != NULL); | |
a52d9a80 CM |
1999 | if (ret < 0) { |
2000 | err = ret; | |
2001 | goto out; | |
2002 | } | |
2003 | ||
2004 | if (ret != 0) { | |
2005 | if (path->slots[0] == 0) | |
2006 | goto not_found; | |
2007 | path->slots[0]--; | |
2008 | } | |
2009 | ||
5f39d397 CM |
2010 | leaf = path->nodes[0]; |
2011 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 2012 | struct btrfs_file_extent_item); |
a52d9a80 | 2013 | /* are we inside the extent that was found? */ |
5f39d397 CM |
2014 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
2015 | found_type = btrfs_key_type(&found_key); | |
2016 | if (found_key.objectid != objectid || | |
a52d9a80 CM |
2017 | found_type != BTRFS_EXTENT_DATA_KEY) { |
2018 | goto not_found; | |
2019 | } | |
2020 | ||
5f39d397 CM |
2021 | found_type = btrfs_file_extent_type(leaf, item); |
2022 | extent_start = found_key.offset; | |
a52d9a80 CM |
2023 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
2024 | extent_end = extent_start + | |
db94535d | 2025 | btrfs_file_extent_num_bytes(leaf, item); |
a52d9a80 | 2026 | err = 0; |
b888db2b | 2027 | if (start < extent_start || start >= extent_end) { |
a52d9a80 CM |
2028 | em->start = start; |
2029 | if (start < extent_start) { | |
d1310b2e | 2030 | if (start + len <= extent_start) |
b888db2b | 2031 | goto not_found; |
d1310b2e | 2032 | em->len = extent_end - extent_start; |
a52d9a80 | 2033 | } else { |
d1310b2e | 2034 | em->len = len; |
a52d9a80 CM |
2035 | } |
2036 | goto not_found_em; | |
2037 | } | |
db94535d CM |
2038 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
2039 | if (bytenr == 0) { | |
a52d9a80 | 2040 | em->start = extent_start; |
d1310b2e | 2041 | em->len = extent_end - extent_start; |
5f39d397 | 2042 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 CM |
2043 | goto insert; |
2044 | } | |
db94535d CM |
2045 | bytenr += btrfs_file_extent_offset(leaf, item); |
2046 | em->block_start = bytenr; | |
a52d9a80 | 2047 | em->start = extent_start; |
d1310b2e | 2048 | em->len = extent_end - extent_start; |
a52d9a80 CM |
2049 | goto insert; |
2050 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
70dec807 | 2051 | u64 page_start; |
5f39d397 | 2052 | unsigned long ptr; |
a52d9a80 | 2053 | char *map; |
3326d1b0 CM |
2054 | size_t size; |
2055 | size_t extent_offset; | |
2056 | size_t copy_size; | |
a52d9a80 | 2057 | |
5f39d397 CM |
2058 | size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf, |
2059 | path->slots[0])); | |
d1310b2e CM |
2060 | extent_end = (extent_start + size + root->sectorsize - 1) & |
2061 | ~((u64)root->sectorsize - 1); | |
b888db2b | 2062 | if (start < extent_start || start >= extent_end) { |
a52d9a80 CM |
2063 | em->start = start; |
2064 | if (start < extent_start) { | |
d1310b2e | 2065 | if (start + len <= extent_start) |
b888db2b | 2066 | goto not_found; |
d1310b2e | 2067 | em->len = extent_end - extent_start; |
a52d9a80 | 2068 | } else { |
d1310b2e | 2069 | em->len = len; |
a52d9a80 CM |
2070 | } |
2071 | goto not_found_em; | |
2072 | } | |
689f9346 | 2073 | em->block_start = EXTENT_MAP_INLINE; |
689f9346 Y |
2074 | |
2075 | if (!page) { | |
2076 | em->start = extent_start; | |
d1310b2e | 2077 | em->len = size; |
689f9346 Y |
2078 | goto out; |
2079 | } | |
5f39d397 | 2080 | |
70dec807 CM |
2081 | page_start = page_offset(page) + pg_offset; |
2082 | extent_offset = page_start - extent_start; | |
2083 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, | |
3326d1b0 | 2084 | size - extent_offset); |
3326d1b0 | 2085 | em->start = extent_start + extent_offset; |
70dec807 CM |
2086 | em->len = (copy_size + root->sectorsize - 1) & |
2087 | ~((u64)root->sectorsize - 1); | |
689f9346 Y |
2088 | map = kmap(page); |
2089 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; | |
179e29e4 | 2090 | if (create == 0 && !PageUptodate(page)) { |
70dec807 | 2091 | read_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 CM |
2092 | copy_size); |
2093 | flush_dcache_page(page); | |
2094 | } else if (create && PageUptodate(page)) { | |
2095 | if (!trans) { | |
2096 | kunmap(page); | |
2097 | free_extent_map(em); | |
2098 | em = NULL; | |
2099 | btrfs_release_path(root, path); | |
2100 | trans = btrfs_start_transaction(root, 1); | |
2101 | goto again; | |
2102 | } | |
70dec807 | 2103 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 CM |
2104 | copy_size); |
2105 | btrfs_mark_buffer_dirty(leaf); | |
a52d9a80 | 2106 | } |
a52d9a80 | 2107 | kunmap(page); |
d1310b2e CM |
2108 | set_extent_uptodate(io_tree, em->start, |
2109 | extent_map_end(em) - 1, GFP_NOFS); | |
a52d9a80 CM |
2110 | goto insert; |
2111 | } else { | |
2112 | printk("unkknown found_type %d\n", found_type); | |
2113 | WARN_ON(1); | |
2114 | } | |
2115 | not_found: | |
2116 | em->start = start; | |
d1310b2e | 2117 | em->len = len; |
a52d9a80 | 2118 | not_found_em: |
5f39d397 | 2119 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 CM |
2120 | insert: |
2121 | btrfs_release_path(root, path); | |
d1310b2e CM |
2122 | if (em->start > start || extent_map_end(em) <= start) { |
2123 | printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len); | |
a52d9a80 CM |
2124 | err = -EIO; |
2125 | goto out; | |
2126 | } | |
d1310b2e CM |
2127 | |
2128 | err = 0; | |
2129 | spin_lock(&em_tree->lock); | |
a52d9a80 CM |
2130 | ret = add_extent_mapping(em_tree, em); |
2131 | if (ret == -EEXIST) { | |
2132 | free_extent_map(em); | |
d1310b2e CM |
2133 | em = lookup_extent_mapping(em_tree, start, len); |
2134 | if (!em) { | |
a52d9a80 | 2135 | err = -EIO; |
d1310b2e | 2136 | printk("failing to insert %Lu %Lu\n", start, len); |
a52d9a80 | 2137 | } |
a52d9a80 | 2138 | } |
d1310b2e | 2139 | spin_unlock(&em_tree->lock); |
a52d9a80 CM |
2140 | out: |
2141 | btrfs_free_path(path); | |
2142 | if (trans) { | |
2143 | ret = btrfs_end_transaction(trans, root); | |
2144 | if (!err) | |
2145 | err = ret; | |
2146 | } | |
2147 | mutex_unlock(&root->fs_info->fs_mutex); | |
2148 | if (err) { | |
2149 | free_extent_map(em); | |
2150 | WARN_ON(1); | |
2151 | return ERR_PTR(err); | |
2152 | } | |
2153 | return em; | |
2154 | } | |
2155 | ||
d396c6f5 | 2156 | static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock) |
39279cc3 | 2157 | { |
d396c6f5 | 2158 | return extent_bmap(mapping, iblock, btrfs_get_extent); |
39279cc3 CM |
2159 | } |
2160 | ||
a52d9a80 | 2161 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 2162 | { |
d1310b2e CM |
2163 | struct extent_io_tree *tree; |
2164 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
a52d9a80 | 2165 | return extent_read_full_page(tree, page, btrfs_get_extent); |
9ebefb18 | 2166 | } |
1832a6d5 | 2167 | |
a52d9a80 | 2168 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 2169 | { |
d1310b2e | 2170 | struct extent_io_tree *tree; |
b888db2b CM |
2171 | |
2172 | ||
2173 | if (current->flags & PF_MEMALLOC) { | |
2174 | redirty_page_for_writepage(wbc, page); | |
2175 | unlock_page(page); | |
2176 | return 0; | |
2177 | } | |
d1310b2e | 2178 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
a52d9a80 | 2179 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
9ebefb18 CM |
2180 | } |
2181 | ||
b293f02e CM |
2182 | static int btrfs_writepages(struct address_space *mapping, |
2183 | struct writeback_control *wbc) | |
2184 | { | |
d1310b2e CM |
2185 | struct extent_io_tree *tree; |
2186 | tree = &BTRFS_I(mapping->host)->io_tree; | |
b293f02e CM |
2187 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
2188 | } | |
2189 | ||
3ab2fb5a CM |
2190 | static int |
2191 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
2192 | struct list_head *pages, unsigned nr_pages) | |
2193 | { | |
d1310b2e CM |
2194 | struct extent_io_tree *tree; |
2195 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
2196 | return extent_readpages(tree, mapping, pages, nr_pages, |
2197 | btrfs_get_extent); | |
2198 | } | |
2199 | ||
70dec807 | 2200 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 2201 | { |
d1310b2e CM |
2202 | struct extent_io_tree *tree; |
2203 | struct extent_map_tree *map; | |
a52d9a80 | 2204 | int ret; |
8c2383c3 | 2205 | |
d1310b2e CM |
2206 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
2207 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 2208 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
2209 | if (ret == 1) { |
2210 | ClearPagePrivate(page); | |
2211 | set_page_private(page, 0); | |
2212 | page_cache_release(page); | |
39279cc3 | 2213 | } |
a52d9a80 | 2214 | return ret; |
39279cc3 CM |
2215 | } |
2216 | ||
a52d9a80 | 2217 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
39279cc3 | 2218 | { |
d1310b2e | 2219 | struct extent_io_tree *tree; |
39279cc3 | 2220 | |
d1310b2e | 2221 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
a52d9a80 CM |
2222 | extent_invalidatepage(tree, page, offset); |
2223 | btrfs_releasepage(page, GFP_NOFS); | |
39279cc3 CM |
2224 | } |
2225 | ||
9ebefb18 CM |
2226 | /* |
2227 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
2228 | * called from a page fault handler when a page is first dirtied. Hence we must | |
2229 | * be careful to check for EOF conditions here. We set the page up correctly | |
2230 | * for a written page which means we get ENOSPC checking when writing into | |
2231 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
2232 | * support these features. | |
2233 | * | |
2234 | * We are not allowed to take the i_mutex here so we have to play games to | |
2235 | * protect against truncate races as the page could now be beyond EOF. Because | |
2236 | * vmtruncate() writes the inode size before removing pages, once we have the | |
2237 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
2238 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
2239 | * unlock the page. | |
2240 | */ | |
2241 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) | |
2242 | { | |
6da6abae | 2243 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
1832a6d5 | 2244 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9ebefb18 CM |
2245 | unsigned long end; |
2246 | loff_t size; | |
1832a6d5 | 2247 | int ret; |
a52d9a80 | 2248 | u64 page_start; |
9ebefb18 | 2249 | |
1832a6d5 CM |
2250 | mutex_lock(&root->fs_info->fs_mutex); |
2251 | ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0); | |
8f662a76 | 2252 | mutex_unlock(&root->fs_info->fs_mutex); |
1832a6d5 CM |
2253 | if (ret) |
2254 | goto out; | |
2255 | ||
2256 | ret = -EINVAL; | |
2257 | ||
9ebefb18 CM |
2258 | lock_page(page); |
2259 | wait_on_page_writeback(page); | |
9ebefb18 | 2260 | size = i_size_read(inode); |
35ebb934 | 2261 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
a52d9a80 | 2262 | |
9ebefb18 | 2263 | if ((page->mapping != inode->i_mapping) || |
a52d9a80 | 2264 | (page_start > size)) { |
9ebefb18 CM |
2265 | /* page got truncated out from underneath us */ |
2266 | goto out_unlock; | |
2267 | } | |
2268 | ||
2269 | /* page is wholly or partially inside EOF */ | |
a52d9a80 | 2270 | if (page_start + PAGE_CACHE_SIZE > size) |
9ebefb18 CM |
2271 | end = size & ~PAGE_CACHE_MASK; |
2272 | else | |
2273 | end = PAGE_CACHE_SIZE; | |
2274 | ||
b888db2b | 2275 | ret = btrfs_cow_one_page(inode, page, end); |
9ebefb18 CM |
2276 | |
2277 | out_unlock: | |
2278 | unlock_page(page); | |
1832a6d5 | 2279 | out: |
9ebefb18 CM |
2280 | return ret; |
2281 | } | |
2282 | ||
39279cc3 CM |
2283 | static void btrfs_truncate(struct inode *inode) |
2284 | { | |
2285 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2286 | int ret; | |
2287 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 2288 | unsigned long nr; |
39279cc3 CM |
2289 | |
2290 | if (!S_ISREG(inode->i_mode)) | |
2291 | return; | |
2292 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
2293 | return; | |
2294 | ||
2295 | btrfs_truncate_page(inode->i_mapping, inode->i_size); | |
2296 | ||
2297 | mutex_lock(&root->fs_info->fs_mutex); | |
2298 | trans = btrfs_start_transaction(root, 1); | |
2299 | btrfs_set_trans_block_group(trans, inode); | |
2300 | ||
2301 | /* FIXME, add redo link to tree so we don't leak on crash */ | |
85e21bac CM |
2302 | ret = btrfs_truncate_in_trans(trans, root, inode, |
2303 | BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 2304 | btrfs_update_inode(trans, root, inode); |
d3c2fdcf | 2305 | nr = trans->blocks_used; |
5f39d397 | 2306 | |
39279cc3 CM |
2307 | ret = btrfs_end_transaction(trans, root); |
2308 | BUG_ON(ret); | |
2309 | mutex_unlock(&root->fs_info->fs_mutex); | |
d3c2fdcf | 2310 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 2311 | btrfs_throttle(root); |
39279cc3 CM |
2312 | } |
2313 | ||
4313b399 CM |
2314 | static int noinline create_subvol(struct btrfs_root *root, char *name, |
2315 | int namelen) | |
39279cc3 CM |
2316 | { |
2317 | struct btrfs_trans_handle *trans; | |
2318 | struct btrfs_key key; | |
2319 | struct btrfs_root_item root_item; | |
2320 | struct btrfs_inode_item *inode_item; | |
5f39d397 | 2321 | struct extent_buffer *leaf; |
dc17ff8f | 2322 | struct btrfs_root *new_root = root; |
39279cc3 CM |
2323 | struct inode *inode; |
2324 | struct inode *dir; | |
2325 | int ret; | |
54aa1f4d | 2326 | int err; |
39279cc3 CM |
2327 | u64 objectid; |
2328 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
d3c2fdcf | 2329 | unsigned long nr = 1; |
39279cc3 CM |
2330 | |
2331 | mutex_lock(&root->fs_info->fs_mutex); | |
1832a6d5 CM |
2332 | ret = btrfs_check_free_space(root, 1, 0); |
2333 | if (ret) | |
2334 | goto fail_commit; | |
2335 | ||
39279cc3 CM |
2336 | trans = btrfs_start_transaction(root, 1); |
2337 | BUG_ON(!trans); | |
2338 | ||
7bb86316 CM |
2339 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
2340 | 0, &objectid); | |
2341 | if (ret) | |
2342 | goto fail; | |
2343 | ||
2344 | leaf = __btrfs_alloc_free_block(trans, root, root->leafsize, | |
2345 | objectid, trans->transid, 0, 0, | |
2346 | 0, 0); | |
5f39d397 CM |
2347 | if (IS_ERR(leaf)) |
2348 | return PTR_ERR(leaf); | |
2349 | ||
2350 | btrfs_set_header_nritems(leaf, 0); | |
2351 | btrfs_set_header_level(leaf, 0); | |
db94535d | 2352 | btrfs_set_header_bytenr(leaf, leaf->start); |
5f39d397 | 2353 | btrfs_set_header_generation(leaf, trans->transid); |
7bb86316 CM |
2354 | btrfs_set_header_owner(leaf, objectid); |
2355 | ||
5f39d397 CM |
2356 | write_extent_buffer(leaf, root->fs_info->fsid, |
2357 | (unsigned long)btrfs_header_fsid(leaf), | |
2358 | BTRFS_FSID_SIZE); | |
2359 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 CM |
2360 | |
2361 | inode_item = &root_item.inode; | |
2362 | memset(inode_item, 0, sizeof(*inode_item)); | |
5f39d397 CM |
2363 | inode_item->generation = cpu_to_le64(1); |
2364 | inode_item->size = cpu_to_le64(3); | |
2365 | inode_item->nlink = cpu_to_le32(1); | |
2366 | inode_item->nblocks = cpu_to_le64(1); | |
2367 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); | |
39279cc3 | 2368 | |
db94535d CM |
2369 | btrfs_set_root_bytenr(&root_item, leaf->start); |
2370 | btrfs_set_root_level(&root_item, 0); | |
39279cc3 | 2371 | btrfs_set_root_refs(&root_item, 1); |
5f39d397 CM |
2372 | btrfs_set_root_used(&root_item, 0); |
2373 | ||
5eda7b5e CM |
2374 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
2375 | root_item.drop_level = 0; | |
5f39d397 CM |
2376 | |
2377 | free_extent_buffer(leaf); | |
2378 | leaf = NULL; | |
39279cc3 | 2379 | |
39279cc3 CM |
2380 | btrfs_set_root_dirid(&root_item, new_dirid); |
2381 | ||
2382 | key.objectid = objectid; | |
2383 | key.offset = 1; | |
39279cc3 CM |
2384 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
2385 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
2386 | &root_item); | |
54aa1f4d CM |
2387 | if (ret) |
2388 | goto fail; | |
39279cc3 CM |
2389 | |
2390 | /* | |
2391 | * insert the directory item | |
2392 | */ | |
2393 | key.offset = (u64)-1; | |
2394 | dir = root->fs_info->sb->s_root->d_inode; | |
2395 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, | |
2396 | name, namelen, dir->i_ino, &key, | |
2397 | BTRFS_FT_DIR); | |
54aa1f4d CM |
2398 | if (ret) |
2399 | goto fail; | |
39279cc3 | 2400 | |
3954401f CM |
2401 | ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root, |
2402 | name, namelen, objectid, | |
2403 | root->fs_info->sb->s_root->d_inode->i_ino); | |
2404 | if (ret) | |
2405 | goto fail; | |
2406 | ||
39279cc3 | 2407 | ret = btrfs_commit_transaction(trans, root); |
54aa1f4d CM |
2408 | if (ret) |
2409 | goto fail_commit; | |
39279cc3 | 2410 | |
58176a96 | 2411 | new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen); |
39279cc3 CM |
2412 | BUG_ON(!new_root); |
2413 | ||
2414 | trans = btrfs_start_transaction(new_root, 1); | |
2415 | BUG_ON(!trans); | |
2416 | ||
9c58309d CM |
2417 | inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid, |
2418 | new_dirid, | |
39279cc3 | 2419 | BTRFS_I(dir)->block_group, S_IFDIR | 0700); |
54aa1f4d CM |
2420 | if (IS_ERR(inode)) |
2421 | goto fail; | |
39279cc3 CM |
2422 | inode->i_op = &btrfs_dir_inode_operations; |
2423 | inode->i_fop = &btrfs_dir_file_operations; | |
34088780 | 2424 | new_root->inode = inode; |
39279cc3 | 2425 | |
3954401f CM |
2426 | ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid, |
2427 | new_dirid); | |
39279cc3 | 2428 | inode->i_nlink = 1; |
3954401f | 2429 | inode->i_size = 0; |
39279cc3 | 2430 | ret = btrfs_update_inode(trans, new_root, inode); |
54aa1f4d CM |
2431 | if (ret) |
2432 | goto fail; | |
2433 | fail: | |
d3c2fdcf | 2434 | nr = trans->blocks_used; |
dc17ff8f | 2435 | err = btrfs_commit_transaction(trans, new_root); |
54aa1f4d CM |
2436 | if (err && !ret) |
2437 | ret = err; | |
2438 | fail_commit: | |
39279cc3 | 2439 | mutex_unlock(&root->fs_info->fs_mutex); |
d3c2fdcf | 2440 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 2441 | btrfs_throttle(root); |
54aa1f4d | 2442 | return ret; |
39279cc3 CM |
2443 | } |
2444 | ||
2445 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) | |
2446 | { | |
3063d29f | 2447 | struct btrfs_pending_snapshot *pending_snapshot; |
39279cc3 | 2448 | struct btrfs_trans_handle *trans; |
39279cc3 | 2449 | int ret; |
54aa1f4d | 2450 | int err; |
1832a6d5 | 2451 | unsigned long nr = 0; |
39279cc3 CM |
2452 | |
2453 | if (!root->ref_cows) | |
2454 | return -EINVAL; | |
2455 | ||
2456 | mutex_lock(&root->fs_info->fs_mutex); | |
1832a6d5 CM |
2457 | ret = btrfs_check_free_space(root, 1, 0); |
2458 | if (ret) | |
2459 | goto fail_unlock; | |
2460 | ||
3063d29f CM |
2461 | pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS); |
2462 | if (!pending_snapshot) { | |
2463 | ret = -ENOMEM; | |
2464 | goto fail_unlock; | |
2465 | } | |
fb4bc1e0 | 2466 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); |
3063d29f CM |
2467 | if (!pending_snapshot->name) { |
2468 | ret = -ENOMEM; | |
2469 | kfree(pending_snapshot); | |
2470 | goto fail_unlock; | |
2471 | } | |
fb4bc1e0 Y |
2472 | memcpy(pending_snapshot->name, name, namelen); |
2473 | pending_snapshot->name[namelen] = '\0'; | |
39279cc3 CM |
2474 | trans = btrfs_start_transaction(root, 1); |
2475 | BUG_ON(!trans); | |
3063d29f CM |
2476 | pending_snapshot->root = root; |
2477 | list_add(&pending_snapshot->list, | |
2478 | &trans->transaction->pending_snapshots); | |
39279cc3 | 2479 | ret = btrfs_update_inode(trans, root, root->inode); |
54aa1f4d | 2480 | err = btrfs_commit_transaction(trans, root); |
5f39d397 | 2481 | |
1832a6d5 | 2482 | fail_unlock: |
39279cc3 | 2483 | mutex_unlock(&root->fs_info->fs_mutex); |
d3c2fdcf | 2484 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 2485 | btrfs_throttle(root); |
54aa1f4d | 2486 | return ret; |
39279cc3 CM |
2487 | } |
2488 | ||
edbd8d4e | 2489 | unsigned long btrfs_force_ra(struct address_space *mapping, |
86479a04 CM |
2490 | struct file_ra_state *ra, struct file *file, |
2491 | pgoff_t offset, pgoff_t last_index) | |
2492 | { | |
2493 | pgoff_t req_size; | |
2494 | ||
2495 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) | |
2496 | req_size = last_index - offset + 1; | |
2497 | offset = page_cache_readahead(mapping, ra, file, offset, req_size); | |
2498 | return offset; | |
2499 | #else | |
2500 | req_size = min(last_index - offset + 1, (pgoff_t)128); | |
2501 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); | |
2502 | return offset + req_size; | |
2503 | #endif | |
2504 | } | |
2505 | ||
2506 | int btrfs_defrag_file(struct file *file) { | |
6da6abae | 2507 | struct inode *inode = fdentry(file)->d_inode; |
1832a6d5 | 2508 | struct btrfs_root *root = BTRFS_I(inode)->root; |
d1310b2e | 2509 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
86479a04 CM |
2510 | struct page *page; |
2511 | unsigned long last_index; | |
2512 | unsigned long ra_index = 0; | |
2513 | u64 page_start; | |
2514 | u64 page_end; | |
2515 | unsigned long i; | |
1832a6d5 CM |
2516 | int ret; |
2517 | ||
2518 | mutex_lock(&root->fs_info->fs_mutex); | |
2519 | ret = btrfs_check_free_space(root, inode->i_size, 0); | |
2520 | mutex_unlock(&root->fs_info->fs_mutex); | |
2521 | if (ret) | |
2522 | return -ENOSPC; | |
86479a04 CM |
2523 | |
2524 | mutex_lock(&inode->i_mutex); | |
2525 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; | |
2526 | for (i = 0; i <= last_index; i++) { | |
2527 | if (i == ra_index) { | |
edbd8d4e CM |
2528 | ra_index = btrfs_force_ra(inode->i_mapping, |
2529 | &file->f_ra, | |
2530 | file, ra_index, last_index); | |
86479a04 CM |
2531 | } |
2532 | page = grab_cache_page(inode->i_mapping, i); | |
2533 | if (!page) | |
2534 | goto out_unlock; | |
2535 | if (!PageUptodate(page)) { | |
2536 | btrfs_readpage(NULL, page); | |
2537 | lock_page(page); | |
2538 | if (!PageUptodate(page)) { | |
2539 | unlock_page(page); | |
2540 | page_cache_release(page); | |
2541 | goto out_unlock; | |
2542 | } | |
2543 | } | |
35ebb934 | 2544 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
86479a04 CM |
2545 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
2546 | ||
d1310b2e | 2547 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
d1310b2e | 2548 | set_extent_delalloc(io_tree, page_start, |
86479a04 | 2549 | page_end, GFP_NOFS); |
edbd8d4e | 2550 | |
d1310b2e | 2551 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
86479a04 CM |
2552 | set_page_dirty(page); |
2553 | unlock_page(page); | |
2554 | page_cache_release(page); | |
2555 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); | |
2556 | } | |
2557 | ||
2558 | out_unlock: | |
2559 | mutex_unlock(&inode->i_mutex); | |
2560 | return 0; | |
2561 | } | |
2562 | ||
edbd8d4e CM |
2563 | static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg) |
2564 | { | |
2565 | u64 new_size; | |
2566 | u64 old_size; | |
2567 | struct btrfs_ioctl_vol_args *vol_args; | |
2568 | struct btrfs_trans_handle *trans; | |
2569 | char *sizestr; | |
2570 | int ret = 0; | |
2571 | int namelen; | |
2572 | int mod = 0; | |
2573 | ||
2574 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); | |
2575 | ||
2576 | if (!vol_args) | |
2577 | return -ENOMEM; | |
2578 | ||
2579 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { | |
2580 | ret = -EFAULT; | |
2581 | goto out; | |
2582 | } | |
2583 | namelen = strlen(vol_args->name); | |
2584 | if (namelen > BTRFS_VOL_NAME_MAX) { | |
2585 | ret = -EINVAL; | |
2586 | goto out; | |
2587 | } | |
2588 | ||
2589 | sizestr = vol_args->name; | |
2590 | if (!strcmp(sizestr, "max")) | |
2591 | new_size = root->fs_info->sb->s_bdev->bd_inode->i_size; | |
2592 | else { | |
2593 | if (sizestr[0] == '-') { | |
2594 | mod = -1; | |
2595 | sizestr++; | |
2596 | } else if (sizestr[0] == '+') { | |
2597 | mod = 1; | |
2598 | sizestr++; | |
2599 | } | |
2600 | new_size = btrfs_parse_size(sizestr); | |
2601 | if (new_size == 0) { | |
2602 | ret = -EINVAL; | |
2603 | goto out; | |
2604 | } | |
2605 | } | |
2606 | ||
2607 | mutex_lock(&root->fs_info->fs_mutex); | |
2608 | old_size = btrfs_super_total_bytes(&root->fs_info->super_copy); | |
2609 | ||
2610 | if (mod < 0) { | |
2611 | if (new_size > old_size) { | |
2612 | ret = -EINVAL; | |
2613 | goto out_unlock; | |
2614 | } | |
2615 | new_size = old_size - new_size; | |
2616 | } else if (mod > 0) { | |
2617 | new_size = old_size + new_size; | |
2618 | } | |
2619 | ||
2620 | if (new_size < 256 * 1024 * 1024) { | |
2621 | ret = -EINVAL; | |
2622 | goto out_unlock; | |
2623 | } | |
2624 | if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) { | |
2625 | ret = -EFBIG; | |
2626 | goto out_unlock; | |
2627 | } | |
f9ef6604 CM |
2628 | |
2629 | do_div(new_size, root->sectorsize); | |
2630 | new_size *= root->sectorsize; | |
edbd8d4e CM |
2631 | |
2632 | printk("new size is %Lu\n", new_size); | |
2633 | if (new_size > old_size) { | |
2634 | trans = btrfs_start_transaction(root, 1); | |
2635 | ret = btrfs_grow_extent_tree(trans, root, new_size); | |
2636 | btrfs_commit_transaction(trans, root); | |
2637 | } else { | |
2638 | ret = btrfs_shrink_extent_tree(root, new_size); | |
2639 | } | |
2640 | ||
2641 | out_unlock: | |
2642 | mutex_unlock(&root->fs_info->fs_mutex); | |
2643 | out: | |
2644 | kfree(vol_args); | |
2645 | return ret; | |
2646 | } | |
2647 | ||
4313b399 CM |
2648 | static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root, |
2649 | void __user *arg) | |
39279cc3 | 2650 | { |
4aec2b52 | 2651 | struct btrfs_ioctl_vol_args *vol_args; |
39279cc3 | 2652 | struct btrfs_dir_item *di; |
39279cc3 CM |
2653 | struct btrfs_path *path; |
2654 | u64 root_dirid; | |
4aec2b52 CM |
2655 | int namelen; |
2656 | int ret; | |
39279cc3 | 2657 | |
4aec2b52 | 2658 | vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); |
5f39d397 | 2659 | |
4aec2b52 CM |
2660 | if (!vol_args) |
2661 | return -ENOMEM; | |
2662 | ||
2663 | if (copy_from_user(vol_args, arg, sizeof(*vol_args))) { | |
2664 | ret = -EFAULT; | |
2665 | goto out; | |
2666 | } | |
2667 | ||
2668 | namelen = strlen(vol_args->name); | |
2669 | if (namelen > BTRFS_VOL_NAME_MAX) { | |
2670 | ret = -EINVAL; | |
2671 | goto out; | |
2672 | } | |
2673 | if (strchr(vol_args->name, '/')) { | |
2674 | ret = -EINVAL; | |
2675 | goto out; | |
2676 | } | |
d03581f4 CH |
2677 | |
2678 | path = btrfs_alloc_path(); | |
4aec2b52 CM |
2679 | if (!path) { |
2680 | ret = -ENOMEM; | |
2681 | goto out; | |
2682 | } | |
d03581f4 CH |
2683 | |
2684 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, | |
2685 | mutex_lock(&root->fs_info->fs_mutex); | |
2686 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, | |
2687 | path, root_dirid, | |
4aec2b52 | 2688 | vol_args->name, namelen, 0); |
d03581f4 CH |
2689 | mutex_unlock(&root->fs_info->fs_mutex); |
2690 | btrfs_free_path(path); | |
4aec2b52 CM |
2691 | |
2692 | if (di && !IS_ERR(di)) { | |
2693 | ret = -EEXIST; | |
2694 | goto out; | |
2695 | } | |
2696 | ||
2697 | if (IS_ERR(di)) { | |
2698 | ret = PTR_ERR(di); | |
2699 | goto out; | |
2700 | } | |
d03581f4 CH |
2701 | |
2702 | if (root == root->fs_info->tree_root) | |
4aec2b52 CM |
2703 | ret = create_subvol(root, vol_args->name, namelen); |
2704 | else | |
2705 | ret = create_snapshot(root, vol_args->name, namelen); | |
2706 | out: | |
2707 | kfree(vol_args); | |
2708 | return ret; | |
d03581f4 CH |
2709 | } |
2710 | ||
2711 | static int btrfs_ioctl_defrag(struct file *file) | |
2712 | { | |
6da6abae | 2713 | struct inode *inode = fdentry(file)->d_inode; |
d03581f4 CH |
2714 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2715 | ||
2716 | switch (inode->i_mode & S_IFMT) { | |
2717 | case S_IFDIR: | |
39279cc3 | 2718 | mutex_lock(&root->fs_info->fs_mutex); |
d03581f4 CH |
2719 | btrfs_defrag_root(root, 0); |
2720 | btrfs_defrag_root(root->fs_info->extent_root, 0); | |
39279cc3 | 2721 | mutex_unlock(&root->fs_info->fs_mutex); |
39279cc3 | 2722 | break; |
d03581f4 CH |
2723 | case S_IFREG: |
2724 | btrfs_defrag_file(file); | |
2725 | break; | |
2726 | } | |
2727 | ||
2728 | return 0; | |
2729 | } | |
6702ed49 | 2730 | |
d03581f4 CH |
2731 | long btrfs_ioctl(struct file *file, unsigned int |
2732 | cmd, unsigned long arg) | |
2733 | { | |
6da6abae | 2734 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
d03581f4 CH |
2735 | |
2736 | switch (cmd) { | |
2737 | case BTRFS_IOC_SNAP_CREATE: | |
2738 | return btrfs_ioctl_snap_create(root, (void __user *)arg); | |
6702ed49 | 2739 | case BTRFS_IOC_DEFRAG: |
d03581f4 | 2740 | return btrfs_ioctl_defrag(file); |
edbd8d4e CM |
2741 | case BTRFS_IOC_RESIZE: |
2742 | return btrfs_ioctl_resize(root, (void __user *)arg); | |
39279cc3 | 2743 | } |
d03581f4 CH |
2744 | |
2745 | return -ENOTTY; | |
39279cc3 CM |
2746 | } |
2747 | ||
39279cc3 CM |
2748 | /* |
2749 | * Called inside transaction, so use GFP_NOFS | |
2750 | */ | |
2751 | struct inode *btrfs_alloc_inode(struct super_block *sb) | |
2752 | { | |
2753 | struct btrfs_inode *ei; | |
2754 | ||
2755 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
2756 | if (!ei) | |
2757 | return NULL; | |
15ee9bc7 | 2758 | ei->last_trans = 0; |
dc17ff8f | 2759 | ei->ordered_trans = 0; |
39279cc3 CM |
2760 | return &ei->vfs_inode; |
2761 | } | |
2762 | ||
2763 | void btrfs_destroy_inode(struct inode *inode) | |
2764 | { | |
2765 | WARN_ON(!list_empty(&inode->i_dentry)); | |
2766 | WARN_ON(inode->i_data.nrpages); | |
2767 | ||
8c416c9e | 2768 | btrfs_drop_extent_cache(inode, 0, (u64)-1); |
39279cc3 CM |
2769 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
2770 | } | |
2771 | ||
44ec0b71 CM |
2772 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
2773 | static void init_once(struct kmem_cache * cachep, void *foo) | |
2774 | #else | |
39279cc3 CM |
2775 | static void init_once(void * foo, struct kmem_cache * cachep, |
2776 | unsigned long flags) | |
44ec0b71 | 2777 | #endif |
39279cc3 CM |
2778 | { |
2779 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
2780 | ||
2781 | inode_init_once(&ei->vfs_inode); | |
2782 | } | |
2783 | ||
2784 | void btrfs_destroy_cachep(void) | |
2785 | { | |
2786 | if (btrfs_inode_cachep) | |
2787 | kmem_cache_destroy(btrfs_inode_cachep); | |
2788 | if (btrfs_trans_handle_cachep) | |
2789 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
2790 | if (btrfs_transaction_cachep) | |
2791 | kmem_cache_destroy(btrfs_transaction_cachep); | |
2792 | if (btrfs_bit_radix_cachep) | |
2793 | kmem_cache_destroy(btrfs_bit_radix_cachep); | |
2794 | if (btrfs_path_cachep) | |
2795 | kmem_cache_destroy(btrfs_path_cachep); | |
2796 | } | |
2797 | ||
86479a04 | 2798 | struct kmem_cache *btrfs_cache_create(const char *name, size_t size, |
92fee66d | 2799 | unsigned long extra_flags, |
44ec0b71 CM |
2800 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) |
2801 | void (*ctor)(struct kmem_cache *, void *) | |
2802 | #else | |
92fee66d | 2803 | void (*ctor)(void *, struct kmem_cache *, |
44ec0b71 CM |
2804 | unsigned long) |
2805 | #endif | |
2806 | ) | |
92fee66d CM |
2807 | { |
2808 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | | |
2809 | SLAB_MEM_SPREAD | extra_flags), ctor | |
2810 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) | |
2811 | ,NULL | |
2812 | #endif | |
2813 | ); | |
2814 | } | |
2815 | ||
39279cc3 CM |
2816 | int btrfs_init_cachep(void) |
2817 | { | |
86479a04 | 2818 | btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache", |
92fee66d CM |
2819 | sizeof(struct btrfs_inode), |
2820 | 0, init_once); | |
39279cc3 CM |
2821 | if (!btrfs_inode_cachep) |
2822 | goto fail; | |
86479a04 CM |
2823 | btrfs_trans_handle_cachep = |
2824 | btrfs_cache_create("btrfs_trans_handle_cache", | |
2825 | sizeof(struct btrfs_trans_handle), | |
2826 | 0, NULL); | |
39279cc3 CM |
2827 | if (!btrfs_trans_handle_cachep) |
2828 | goto fail; | |
86479a04 | 2829 | btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache", |
39279cc3 | 2830 | sizeof(struct btrfs_transaction), |
92fee66d | 2831 | 0, NULL); |
39279cc3 CM |
2832 | if (!btrfs_transaction_cachep) |
2833 | goto fail; | |
86479a04 | 2834 | btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache", |
23223584 | 2835 | sizeof(struct btrfs_path), |
92fee66d | 2836 | 0, NULL); |
39279cc3 CM |
2837 | if (!btrfs_path_cachep) |
2838 | goto fail; | |
86479a04 | 2839 | btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256, |
92fee66d | 2840 | SLAB_DESTROY_BY_RCU, NULL); |
39279cc3 CM |
2841 | if (!btrfs_bit_radix_cachep) |
2842 | goto fail; | |
2843 | return 0; | |
2844 | fail: | |
2845 | btrfs_destroy_cachep(); | |
2846 | return -ENOMEM; | |
2847 | } | |
2848 | ||
2849 | static int btrfs_getattr(struct vfsmount *mnt, | |
2850 | struct dentry *dentry, struct kstat *stat) | |
2851 | { | |
2852 | struct inode *inode = dentry->d_inode; | |
2853 | generic_fillattr(inode, stat); | |
d6667462 | 2854 | stat->blksize = PAGE_CACHE_SIZE; |
9069218d | 2855 | stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9); |
39279cc3 CM |
2856 | return 0; |
2857 | } | |
2858 | ||
2859 | static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, | |
2860 | struct inode * new_dir,struct dentry *new_dentry) | |
2861 | { | |
2862 | struct btrfs_trans_handle *trans; | |
2863 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
2864 | struct inode *new_inode = new_dentry->d_inode; | |
2865 | struct inode *old_inode = old_dentry->d_inode; | |
2866 | struct timespec ctime = CURRENT_TIME; | |
2867 | struct btrfs_path *path; | |
39279cc3 CM |
2868 | int ret; |
2869 | ||
2870 | if (S_ISDIR(old_inode->i_mode) && new_inode && | |
2871 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { | |
2872 | return -ENOTEMPTY; | |
2873 | } | |
5f39d397 | 2874 | |
39279cc3 | 2875 | mutex_lock(&root->fs_info->fs_mutex); |
1832a6d5 CM |
2876 | ret = btrfs_check_free_space(root, 1, 0); |
2877 | if (ret) | |
2878 | goto out_unlock; | |
2879 | ||
39279cc3 | 2880 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 2881 | |
39279cc3 CM |
2882 | btrfs_set_trans_block_group(trans, new_dir); |
2883 | path = btrfs_alloc_path(); | |
2884 | if (!path) { | |
2885 | ret = -ENOMEM; | |
2886 | goto out_fail; | |
2887 | } | |
2888 | ||
2889 | old_dentry->d_inode->i_nlink++; | |
2890 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
2891 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
2892 | old_inode->i_ctime = ctime; | |
5f39d397 | 2893 | |
39279cc3 CM |
2894 | ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry); |
2895 | if (ret) | |
2896 | goto out_fail; | |
2897 | ||
2898 | if (new_inode) { | |
2899 | new_inode->i_ctime = CURRENT_TIME; | |
2900 | ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry); | |
2901 | if (ret) | |
2902 | goto out_fail; | |
39279cc3 | 2903 | } |
9c58309d | 2904 | ret = btrfs_add_link(trans, new_dentry, old_inode, 1); |
39279cc3 CM |
2905 | if (ret) |
2906 | goto out_fail; | |
2907 | ||
2908 | out_fail: | |
2909 | btrfs_free_path(path); | |
2910 | btrfs_end_transaction(trans, root); | |
1832a6d5 | 2911 | out_unlock: |
39279cc3 CM |
2912 | mutex_unlock(&root->fs_info->fs_mutex); |
2913 | return ret; | |
2914 | } | |
2915 | ||
2916 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, | |
2917 | const char *symname) | |
2918 | { | |
2919 | struct btrfs_trans_handle *trans; | |
2920 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2921 | struct btrfs_path *path; | |
2922 | struct btrfs_key key; | |
1832a6d5 | 2923 | struct inode *inode = NULL; |
39279cc3 CM |
2924 | int err; |
2925 | int drop_inode = 0; | |
2926 | u64 objectid; | |
2927 | int name_len; | |
2928 | int datasize; | |
5f39d397 | 2929 | unsigned long ptr; |
39279cc3 | 2930 | struct btrfs_file_extent_item *ei; |
5f39d397 | 2931 | struct extent_buffer *leaf; |
1832a6d5 | 2932 | unsigned long nr = 0; |
39279cc3 CM |
2933 | |
2934 | name_len = strlen(symname) + 1; | |
2935 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) | |
2936 | return -ENAMETOOLONG; | |
1832a6d5 | 2937 | |
39279cc3 | 2938 | mutex_lock(&root->fs_info->fs_mutex); |
1832a6d5 CM |
2939 | err = btrfs_check_free_space(root, 1, 0); |
2940 | if (err) | |
2941 | goto out_fail; | |
2942 | ||
39279cc3 CM |
2943 | trans = btrfs_start_transaction(root, 1); |
2944 | btrfs_set_trans_block_group(trans, dir); | |
2945 | ||
2946 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
2947 | if (err) { | |
2948 | err = -ENOSPC; | |
2949 | goto out_unlock; | |
2950 | } | |
2951 | ||
9c58309d CM |
2952 | inode = btrfs_new_inode(trans, root, dentry->d_name.name, |
2953 | dentry->d_name.len, | |
2954 | dentry->d_parent->d_inode->i_ino, objectid, | |
39279cc3 CM |
2955 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO); |
2956 | err = PTR_ERR(inode); | |
2957 | if (IS_ERR(inode)) | |
2958 | goto out_unlock; | |
2959 | ||
2960 | btrfs_set_trans_block_group(trans, inode); | |
9c58309d | 2961 | err = btrfs_add_nondir(trans, dentry, inode, 0); |
39279cc3 CM |
2962 | if (err) |
2963 | drop_inode = 1; | |
2964 | else { | |
2965 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 2966 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 CM |
2967 | inode->i_fop = &btrfs_file_operations; |
2968 | inode->i_op = &btrfs_file_inode_operations; | |
d1310b2e CM |
2969 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
2970 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | |
a52d9a80 | 2971 | inode->i_mapping, GFP_NOFS); |
9069218d | 2972 | BTRFS_I(inode)->delalloc_bytes = 0; |
d1310b2e | 2973 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
2974 | } |
2975 | dir->i_sb->s_dirt = 1; | |
2976 | btrfs_update_inode_block_group(trans, inode); | |
2977 | btrfs_update_inode_block_group(trans, dir); | |
2978 | if (drop_inode) | |
2979 | goto out_unlock; | |
2980 | ||
2981 | path = btrfs_alloc_path(); | |
2982 | BUG_ON(!path); | |
2983 | key.objectid = inode->i_ino; | |
2984 | key.offset = 0; | |
39279cc3 CM |
2985 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
2986 | datasize = btrfs_file_extent_calc_inline_size(name_len); | |
2987 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
2988 | datasize); | |
54aa1f4d CM |
2989 | if (err) { |
2990 | drop_inode = 1; | |
2991 | goto out_unlock; | |
2992 | } | |
5f39d397 CM |
2993 | leaf = path->nodes[0]; |
2994 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
2995 | struct btrfs_file_extent_item); | |
2996 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
2997 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 CM |
2998 | BTRFS_FILE_EXTENT_INLINE); |
2999 | ptr = btrfs_file_extent_inline_start(ei); | |
5f39d397 CM |
3000 | write_extent_buffer(leaf, symname, ptr, name_len); |
3001 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 3002 | btrfs_free_path(path); |
5f39d397 | 3003 | |
39279cc3 CM |
3004 | inode->i_op = &btrfs_symlink_inode_operations; |
3005 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
04160088 | 3006 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 | 3007 | inode->i_size = name_len - 1; |
54aa1f4d CM |
3008 | err = btrfs_update_inode(trans, root, inode); |
3009 | if (err) | |
3010 | drop_inode = 1; | |
39279cc3 CM |
3011 | |
3012 | out_unlock: | |
d3c2fdcf | 3013 | nr = trans->blocks_used; |
39279cc3 | 3014 | btrfs_end_transaction(trans, root); |
1832a6d5 | 3015 | out_fail: |
39279cc3 | 3016 | mutex_unlock(&root->fs_info->fs_mutex); |
39279cc3 CM |
3017 | if (drop_inode) { |
3018 | inode_dec_link_count(inode); | |
3019 | iput(inode); | |
3020 | } | |
d3c2fdcf | 3021 | btrfs_btree_balance_dirty(root, nr); |
e2008b61 | 3022 | btrfs_throttle(root); |
39279cc3 CM |
3023 | return err; |
3024 | } | |
fdebe2bd Y |
3025 | static int btrfs_permission(struct inode *inode, int mask, |
3026 | struct nameidata *nd) | |
3027 | { | |
3028 | if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE)) | |
3029 | return -EACCES; | |
3030 | return generic_permission(inode, mask, NULL); | |
3031 | } | |
39279cc3 CM |
3032 | |
3033 | static struct inode_operations btrfs_dir_inode_operations = { | |
3034 | .lookup = btrfs_lookup, | |
3035 | .create = btrfs_create, | |
3036 | .unlink = btrfs_unlink, | |
3037 | .link = btrfs_link, | |
3038 | .mkdir = btrfs_mkdir, | |
3039 | .rmdir = btrfs_rmdir, | |
3040 | .rename = btrfs_rename, | |
3041 | .symlink = btrfs_symlink, | |
3042 | .setattr = btrfs_setattr, | |
618e21d5 | 3043 | .mknod = btrfs_mknod, |
5103e947 JB |
3044 | .setxattr = generic_setxattr, |
3045 | .getxattr = generic_getxattr, | |
3046 | .listxattr = btrfs_listxattr, | |
3047 | .removexattr = generic_removexattr, | |
fdebe2bd | 3048 | .permission = btrfs_permission, |
39279cc3 | 3049 | }; |
39279cc3 CM |
3050 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
3051 | .lookup = btrfs_lookup, | |
fdebe2bd | 3052 | .permission = btrfs_permission, |
39279cc3 | 3053 | }; |
39279cc3 CM |
3054 | static struct file_operations btrfs_dir_file_operations = { |
3055 | .llseek = generic_file_llseek, | |
3056 | .read = generic_read_dir, | |
3057 | .readdir = btrfs_readdir, | |
34287aa3 | 3058 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 3059 | #ifdef CONFIG_COMPAT |
34287aa3 | 3060 | .compat_ioctl = btrfs_ioctl, |
39279cc3 CM |
3061 | #endif |
3062 | }; | |
3063 | ||
d1310b2e | 3064 | static struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 3065 | .fill_delalloc = run_delalloc_range, |
065631f6 | 3066 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 3067 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac CM |
3068 | .readpage_io_hook = btrfs_readpage_io_hook, |
3069 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, | |
b0c68f8b CM |
3070 | .set_bit_hook = btrfs_set_bit_hook, |
3071 | .clear_bit_hook = btrfs_clear_bit_hook, | |
07157aac CM |
3072 | }; |
3073 | ||
39279cc3 CM |
3074 | static struct address_space_operations btrfs_aops = { |
3075 | .readpage = btrfs_readpage, | |
3076 | .writepage = btrfs_writepage, | |
b293f02e | 3077 | .writepages = btrfs_writepages, |
3ab2fb5a | 3078 | .readpages = btrfs_readpages, |
39279cc3 | 3079 | .sync_page = block_sync_page, |
39279cc3 | 3080 | .bmap = btrfs_bmap, |
a52d9a80 CM |
3081 | .invalidatepage = btrfs_invalidatepage, |
3082 | .releasepage = btrfs_releasepage, | |
3083 | .set_page_dirty = __set_page_dirty_nobuffers, | |
39279cc3 CM |
3084 | }; |
3085 | ||
3086 | static struct address_space_operations btrfs_symlink_aops = { | |
3087 | .readpage = btrfs_readpage, | |
3088 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
3089 | .invalidatepage = btrfs_invalidatepage, |
3090 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
3091 | }; |
3092 | ||
3093 | static struct inode_operations btrfs_file_inode_operations = { | |
3094 | .truncate = btrfs_truncate, | |
3095 | .getattr = btrfs_getattr, | |
3096 | .setattr = btrfs_setattr, | |
5103e947 JB |
3097 | .setxattr = generic_setxattr, |
3098 | .getxattr = generic_getxattr, | |
3099 | .listxattr = btrfs_listxattr, | |
3100 | .removexattr = generic_removexattr, | |
fdebe2bd | 3101 | .permission = btrfs_permission, |
39279cc3 | 3102 | }; |
618e21d5 JB |
3103 | static struct inode_operations btrfs_special_inode_operations = { |
3104 | .getattr = btrfs_getattr, | |
3105 | .setattr = btrfs_setattr, | |
fdebe2bd | 3106 | .permission = btrfs_permission, |
618e21d5 | 3107 | }; |
39279cc3 CM |
3108 | static struct inode_operations btrfs_symlink_inode_operations = { |
3109 | .readlink = generic_readlink, | |
3110 | .follow_link = page_follow_link_light, | |
3111 | .put_link = page_put_link, | |
fdebe2bd | 3112 | .permission = btrfs_permission, |
39279cc3 | 3113 | }; |