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