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