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