]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/btrfs/super.c
Btrfs: rework csums and extent item ordering
[mirror_ubuntu-artful-kernel.git] / fs / btrfs / super.c
1 #include <linux/module.h>
2 #include <linux/buffer_head.h>
3 #include <linux/fs.h>
4 #include <linux/pagemap.h>
5 #include <linux/highmem.h>
6 #include <linux/time.h>
7 #include <linux/init.h>
8 #include <linux/string.h>
9 #include <linux/smp_lock.h>
10 #include <linux/backing-dev.h>
11 #include <linux/mpage.h>
12 #include <linux/swap.h>
13 #include <linux/writeback.h>
14 #include "ctree.h"
15 #include "disk-io.h"
16 #include "transaction.h"
17 #include "btrfs_inode.h"
18 #include "ioctl.h"
19
20 void btrfs_fsinfo_release(struct kobject *obj)
21 {
22 struct btrfs_fs_info *fsinfo = container_of(obj,
23 struct btrfs_fs_info, kobj);
24 kfree(fsinfo);
25 }
26
27 struct kobj_type btrfs_fsinfo_ktype = {
28 .release = btrfs_fsinfo_release,
29 };
30
31 struct btrfs_iget_args {
32 u64 ino;
33 struct btrfs_root *root;
34 };
35
36 decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
37
38 #define BTRFS_SUPER_MAGIC 0x9123682E
39
40 static struct inode_operations btrfs_dir_inode_operations;
41 static struct inode_operations btrfs_dir_ro_inode_operations;
42 static struct super_operations btrfs_super_ops;
43 static struct file_operations btrfs_dir_file_operations;
44 static struct inode_operations btrfs_file_inode_operations;
45 static struct address_space_operations btrfs_aops;
46 static struct file_operations btrfs_file_operations;
47
48 static void btrfs_read_locked_inode(struct inode *inode)
49 {
50 struct btrfs_path *path;
51 struct btrfs_inode_item *inode_item;
52 struct btrfs_root *root = BTRFS_I(inode)->root;
53 struct btrfs_key location;
54 int ret;
55
56 path = btrfs_alloc_path();
57 BUG_ON(!path);
58 btrfs_init_path(path);
59 mutex_lock(&root->fs_info->fs_mutex);
60
61 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
62 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
63 if (ret) {
64 btrfs_free_path(path);
65 goto make_bad;
66 }
67 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
68 path->slots[0],
69 struct btrfs_inode_item);
70
71 inode->i_mode = btrfs_inode_mode(inode_item);
72 inode->i_nlink = btrfs_inode_nlink(inode_item);
73 inode->i_uid = btrfs_inode_uid(inode_item);
74 inode->i_gid = btrfs_inode_gid(inode_item);
75 inode->i_size = btrfs_inode_size(inode_item);
76 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
77 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
78 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
79 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
80 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
81 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
82 inode->i_blocks = btrfs_inode_nblocks(inode_item);
83 inode->i_generation = btrfs_inode_generation(inode_item);
84
85 btrfs_free_path(path);
86 inode_item = NULL;
87
88 mutex_unlock(&root->fs_info->fs_mutex);
89
90 switch (inode->i_mode & S_IFMT) {
91 #if 0
92 default:
93 init_special_inode(inode, inode->i_mode,
94 btrfs_inode_rdev(inode_item));
95 break;
96 #endif
97 case S_IFREG:
98 inode->i_mapping->a_ops = &btrfs_aops;
99 inode->i_fop = &btrfs_file_operations;
100 inode->i_op = &btrfs_file_inode_operations;
101 break;
102 case S_IFDIR:
103 inode->i_fop = &btrfs_dir_file_operations;
104 if (root == root->fs_info->tree_root)
105 inode->i_op = &btrfs_dir_ro_inode_operations;
106 else
107 inode->i_op = &btrfs_dir_inode_operations;
108 break;
109 case S_IFLNK:
110 // inode->i_op = &page_symlink_inode_operations;
111 break;
112 }
113 return;
114
115 make_bad:
116 btrfs_release_path(root, path);
117 btrfs_free_path(path);
118 mutex_unlock(&root->fs_info->fs_mutex);
119 make_bad_inode(inode);
120 }
121
122 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
123 struct btrfs_root *root,
124 struct inode *dir,
125 struct dentry *dentry)
126 {
127 struct btrfs_path *path;
128 const char *name = dentry->d_name.name;
129 int name_len = dentry->d_name.len;
130 int ret;
131 u64 objectid;
132 struct btrfs_dir_item *di;
133
134 path = btrfs_alloc_path();
135 BUG_ON(!path);
136 btrfs_init_path(path);
137 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
138 name, name_len, -1);
139 if (ret < 0)
140 goto err;
141 if (ret > 0) {
142 ret = -ENOENT;
143 goto err;
144 }
145 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
146 struct btrfs_dir_item);
147 objectid = btrfs_disk_key_objectid(&di->location);
148
149 ret = btrfs_del_item(trans, root, path);
150 BUG_ON(ret);
151
152 btrfs_release_path(root, path);
153 ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
154 objectid, -1);
155 BUG_ON(ret);
156 ret = btrfs_del_item(trans, root, path);
157 BUG_ON(ret);
158 dentry->d_inode->i_ctime = dir->i_ctime;
159 err:
160 btrfs_release_path(root, path);
161 btrfs_free_path(path);
162 if (ret == 0) {
163 inode_dec_link_count(dentry->d_inode);
164 dir->i_size -= name_len * 2;
165 mark_inode_dirty(dir);
166 }
167 return ret;
168 }
169
170 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
171 {
172 struct btrfs_root *root;
173 struct btrfs_trans_handle *trans;
174 int ret;
175
176 root = BTRFS_I(dir)->root;
177 mutex_lock(&root->fs_info->fs_mutex);
178 trans = btrfs_start_transaction(root, 1);
179 ret = btrfs_unlink_trans(trans, root, dir, dentry);
180 btrfs_end_transaction(trans, root);
181 mutex_unlock(&root->fs_info->fs_mutex);
182 return ret;
183 }
184
185 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
186 {
187 struct inode *inode = dentry->d_inode;
188 int err;
189 int ret;
190 struct btrfs_root *root = BTRFS_I(dir)->root;
191 struct btrfs_path *path;
192 struct btrfs_key key;
193 struct btrfs_trans_handle *trans;
194 struct btrfs_key found_key;
195 int found_type;
196 struct btrfs_leaf *leaf;
197 char *goodnames = "..";
198
199 path = btrfs_alloc_path();
200 BUG_ON(!path);
201 btrfs_init_path(path);
202 mutex_lock(&root->fs_info->fs_mutex);
203 trans = btrfs_start_transaction(root, 1);
204 key.objectid = inode->i_ino;
205 key.offset = (u64)-1;
206 key.flags = (u32)-1;
207 while(1) {
208 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
209 if (ret < 0) {
210 err = ret;
211 goto out;
212 }
213 BUG_ON(ret == 0);
214 if (path->slots[0] == 0) {
215 err = -ENOENT;
216 goto out;
217 }
218 path->slots[0]--;
219 leaf = btrfs_buffer_leaf(path->nodes[0]);
220 btrfs_disk_key_to_cpu(&found_key,
221 &leaf->items[path->slots[0]].key);
222 found_type = btrfs_key_type(&found_key);
223 if (found_key.objectid != inode->i_ino) {
224 err = -ENOENT;
225 goto out;
226 }
227 if ((found_type != BTRFS_DIR_ITEM_KEY &&
228 found_type != BTRFS_DIR_INDEX_KEY) ||
229 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
230 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
231 err = -ENOTEMPTY;
232 goto out;
233 }
234 ret = btrfs_del_item(trans, root, path);
235 BUG_ON(ret);
236
237 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
238 break;
239 btrfs_release_path(root, path);
240 }
241 ret = 0;
242 btrfs_release_path(root, path);
243
244 /* now the directory is empty */
245 err = btrfs_unlink_trans(trans, root, dir, dentry);
246 if (!err) {
247 inode->i_size = 0;
248 }
249 out:
250 btrfs_release_path(root, path);
251 btrfs_free_path(path);
252 mutex_unlock(&root->fs_info->fs_mutex);
253 ret = btrfs_end_transaction(trans, root);
254 if (ret && !err)
255 err = ret;
256 return err;
257 }
258
259 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
260 struct btrfs_root *root,
261 struct inode *inode)
262 {
263 struct btrfs_path *path;
264 int ret;
265
266 clear_inode(inode);
267
268 path = btrfs_alloc_path();
269 BUG_ON(!path);
270 btrfs_init_path(path);
271 ret = btrfs_lookup_inode(trans, root, path,
272 &BTRFS_I(inode)->location, -1);
273 BUG_ON(ret);
274 ret = btrfs_del_item(trans, root, path);
275 BUG_ON(ret);
276 btrfs_free_path(path);
277 return ret;
278 }
279
280 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
281 struct btrfs_root *root,
282 struct inode *inode)
283 {
284 int ret;
285 struct btrfs_path *path;
286 struct btrfs_key key;
287 struct btrfs_disk_key *found_key;
288 struct btrfs_leaf *leaf;
289 struct btrfs_file_extent_item *fi = NULL;
290 u64 extent_start = 0;
291 u64 extent_num_blocks = 0;
292 int found_extent;
293
294 path = btrfs_alloc_path();
295 BUG_ON(!path);
296 /* FIXME, add redo link to tree so we don't leak on crash */
297 key.objectid = inode->i_ino;
298 key.offset = (u64)-1;
299 key.flags = 0;
300 /*
301 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
302 * or extent data
303 */
304 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
305 while(1) {
306 btrfs_init_path(path);
307 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
308 if (ret < 0) {
309 goto error;
310 }
311 if (ret > 0) {
312 BUG_ON(path->slots[0] == 0);
313 path->slots[0]--;
314 }
315 leaf = btrfs_buffer_leaf(path->nodes[0]);
316 found_key = &leaf->items[path->slots[0]].key;
317 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
318 break;
319 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
320 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
321 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
322 break;
323 if (btrfs_disk_key_offset(found_key) < inode->i_size)
324 break;
325 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
326 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
327 path->slots[0],
328 struct btrfs_file_extent_item);
329 extent_start = btrfs_file_extent_disk_blocknr(fi);
330 extent_num_blocks =
331 btrfs_file_extent_disk_num_blocks(fi);
332 inode->i_blocks -=
333 btrfs_file_extent_num_blocks(fi) >> 9;
334 found_extent = 1;
335 } else {
336 found_extent = 0;
337 }
338 ret = btrfs_del_item(trans, root, path);
339 BUG_ON(ret);
340 btrfs_release_path(root, path);
341 if (found_extent) {
342 ret = btrfs_free_extent(trans, root, extent_start,
343 extent_num_blocks, 0);
344 BUG_ON(ret);
345 }
346 }
347 ret = 0;
348 error:
349 btrfs_release_path(root, path);
350 btrfs_free_path(path);
351 return ret;
352 }
353
354 static void btrfs_delete_inode(struct inode *inode)
355 {
356 struct btrfs_trans_handle *trans;
357 struct btrfs_root *root = BTRFS_I(inode)->root;
358 int ret;
359
360 truncate_inode_pages(&inode->i_data, 0);
361 if (is_bad_inode(inode)) {
362 goto no_delete;
363 }
364 inode->i_size = 0;
365 mutex_lock(&root->fs_info->fs_mutex);
366 trans = btrfs_start_transaction(root, 1);
367 if (S_ISREG(inode->i_mode)) {
368 ret = btrfs_truncate_in_trans(trans, root, inode);
369 BUG_ON(ret);
370 }
371 btrfs_free_inode(trans, root, inode);
372 btrfs_end_transaction(trans, root);
373 mutex_unlock(&root->fs_info->fs_mutex);
374 return;
375 no_delete:
376 clear_inode(inode);
377 }
378
379 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
380 struct btrfs_key *location)
381 {
382 const char *name = dentry->d_name.name;
383 int namelen = dentry->d_name.len;
384 struct btrfs_dir_item *di;
385 struct btrfs_path *path;
386 struct btrfs_root *root = BTRFS_I(dir)->root;
387 int ret;
388
389 path = btrfs_alloc_path();
390 BUG_ON(!path);
391 btrfs_init_path(path);
392 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
393 namelen, 0);
394 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
395 location->objectid = 0;
396 ret = 0;
397 goto out;
398 }
399 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
400 struct btrfs_dir_item);
401 btrfs_disk_key_to_cpu(location, &di->location);
402 out:
403 btrfs_release_path(root, path);
404 btrfs_free_path(path);
405 return ret;
406 }
407
408 int fixup_tree_root_location(struct btrfs_root *root,
409 struct btrfs_key *location,
410 struct btrfs_root **sub_root)
411 {
412 struct btrfs_path *path;
413 struct btrfs_root_item *ri;
414
415 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
416 return 0;
417 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
418 return 0;
419
420 path = btrfs_alloc_path();
421 BUG_ON(!path);
422 mutex_lock(&root->fs_info->fs_mutex);
423
424 *sub_root = btrfs_read_fs_root(root->fs_info, location);
425 if (IS_ERR(*sub_root))
426 return PTR_ERR(*sub_root);
427
428 ri = &(*sub_root)->root_item;
429 location->objectid = btrfs_root_dirid(ri);
430 location->flags = 0;
431 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
432 location->offset = 0;
433
434 btrfs_free_path(path);
435 mutex_unlock(&root->fs_info->fs_mutex);
436 return 0;
437 }
438
439 int btrfs_init_locked_inode(struct inode *inode, void *p)
440 {
441 struct btrfs_iget_args *args = p;
442 inode->i_ino = args->ino;
443 BTRFS_I(inode)->root = args->root;
444 return 0;
445 }
446
447 int btrfs_find_actor(struct inode *inode, void *opaque)
448 {
449 struct btrfs_iget_args *args = opaque;
450 return (args->ino == inode->i_ino &&
451 args->root == BTRFS_I(inode)->root);
452 }
453
454 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
455 struct btrfs_root *root)
456 {
457 struct inode *inode;
458 struct btrfs_iget_args args;
459 args.ino = objectid;
460 args.root = root;
461
462 inode = iget5_locked(s, objectid, btrfs_find_actor,
463 btrfs_init_locked_inode,
464 (void *)&args);
465 return inode;
466 }
467
468 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
469 struct nameidata *nd)
470 {
471 struct inode * inode;
472 struct btrfs_inode *bi = BTRFS_I(dir);
473 struct btrfs_root *root = bi->root;
474 struct btrfs_root *sub_root = root;
475 struct btrfs_key location;
476 int ret;
477
478 if (dentry->d_name.len > BTRFS_NAME_LEN)
479 return ERR_PTR(-ENAMETOOLONG);
480 mutex_lock(&root->fs_info->fs_mutex);
481 ret = btrfs_inode_by_name(dir, dentry, &location);
482 mutex_unlock(&root->fs_info->fs_mutex);
483 if (ret < 0)
484 return ERR_PTR(ret);
485 inode = NULL;
486 if (location.objectid) {
487 ret = fixup_tree_root_location(root, &location, &sub_root);
488 if (ret < 0)
489 return ERR_PTR(ret);
490 if (ret > 0)
491 return ERR_PTR(-ENOENT);
492 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
493 sub_root);
494 if (!inode)
495 return ERR_PTR(-EACCES);
496 if (inode->i_state & I_NEW) {
497 if (sub_root != root) {
498 printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root);
499 igrab(inode);
500 sub_root->inode = inode;
501 }
502 BTRFS_I(inode)->root = sub_root;
503 memcpy(&BTRFS_I(inode)->location, &location,
504 sizeof(location));
505 btrfs_read_locked_inode(inode);
506 unlock_new_inode(inode);
507 }
508 }
509 return d_splice_alias(inode, dentry);
510 }
511
512 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
513 {
514 struct inode *inode = filp->f_path.dentry->d_inode;
515 struct btrfs_root *root = BTRFS_I(inode)->root;
516 struct btrfs_item *item;
517 struct btrfs_dir_item *di;
518 struct btrfs_key key;
519 struct btrfs_path *path;
520 int ret;
521 u32 nritems;
522 struct btrfs_leaf *leaf;
523 int slot;
524 int advance;
525 unsigned char d_type = DT_UNKNOWN;
526 int over = 0;
527 int key_type = BTRFS_DIR_ITEM_KEY;
528
529 /* FIXME, use a real flag for deciding about the key type */
530 if (root->fs_info->tree_root == root)
531 key_type = BTRFS_DIR_ITEM_KEY;
532 mutex_lock(&root->fs_info->fs_mutex);
533 key.objectid = inode->i_ino;
534 key.flags = 0;
535 btrfs_set_key_type(&key, key_type);
536 key.offset = filp->f_pos;
537 path = btrfs_alloc_path();
538 btrfs_init_path(path);
539 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
540 if (ret < 0)
541 goto err;
542 advance = 0;
543 while(1) {
544 leaf = btrfs_buffer_leaf(path->nodes[0]);
545 nritems = btrfs_header_nritems(&leaf->header);
546 slot = path->slots[0];
547 if (advance || slot >= nritems) {
548 if (slot >= nritems -1) {
549 ret = btrfs_next_leaf(root, path);
550 if (ret)
551 break;
552 leaf = btrfs_buffer_leaf(path->nodes[0]);
553 nritems = btrfs_header_nritems(&leaf->header);
554 slot = path->slots[0];
555 } else {
556 slot++;
557 path->slots[0]++;
558 }
559 }
560 advance = 1;
561 item = leaf->items + slot;
562 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
563 break;
564 if (btrfs_disk_key_type(&item->key) != key_type)
565 continue;
566 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
567 continue;
568 filp->f_pos = btrfs_disk_key_offset(&item->key);
569 advance = 1;
570 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
571 over = filldir(dirent, (const char *)(di + 1),
572 btrfs_dir_name_len(di),
573 btrfs_disk_key_offset(&item->key),
574 btrfs_disk_key_objectid(&di->location), d_type);
575 if (over)
576 goto nopos;
577 }
578 filp->f_pos++;
579 nopos:
580 ret = 0;
581 err:
582 btrfs_release_path(root, path);
583 btrfs_free_path(path);
584 mutex_unlock(&root->fs_info->fs_mutex);
585 return ret;
586 }
587
588 static void btrfs_put_super (struct super_block * sb)
589 {
590 struct btrfs_root *root = btrfs_sb(sb);
591 int ret;
592
593 ret = close_ctree(root);
594 if (ret) {
595 printk("close ctree returns %d\n", ret);
596 }
597 sb->s_fs_info = NULL;
598 }
599
600 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
601 {
602 struct inode * inode;
603 struct dentry * root_dentry;
604 struct btrfs_super_block *disk_super;
605 struct btrfs_root *tree_root;
606 struct btrfs_inode *bi;
607
608 sb->s_maxbytes = MAX_LFS_FILESIZE;
609 sb->s_magic = BTRFS_SUPER_MAGIC;
610 sb->s_op = &btrfs_super_ops;
611 sb->s_time_gran = 1;
612
613 tree_root = open_ctree(sb);
614
615 if (!tree_root) {
616 printk("btrfs: open_ctree failed\n");
617 return -EIO;
618 }
619 sb->s_fs_info = tree_root;
620 disk_super = tree_root->fs_info->disk_super;
621 printk("read in super total blocks %Lu root %Lu\n",
622 btrfs_super_total_blocks(disk_super),
623 btrfs_super_root_dir(disk_super));
624
625 inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super),
626 tree_root);
627 bi = BTRFS_I(inode);
628 bi->location.objectid = inode->i_ino;
629 bi->location.offset = 0;
630 bi->location.flags = 0;
631 bi->root = tree_root;
632 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
633
634 if (!inode)
635 return -ENOMEM;
636 if (inode->i_state & I_NEW) {
637 btrfs_read_locked_inode(inode);
638 unlock_new_inode(inode);
639 }
640
641 root_dentry = d_alloc_root(inode);
642 if (!root_dentry) {
643 iput(inode);
644 return -ENOMEM;
645 }
646 sb->s_root = root_dentry;
647
648 return 0;
649 }
650
651 static void fill_inode_item(struct btrfs_inode_item *item,
652 struct inode *inode)
653 {
654 btrfs_set_inode_uid(item, inode->i_uid);
655 btrfs_set_inode_gid(item, inode->i_gid);
656 btrfs_set_inode_size(item, inode->i_size);
657 btrfs_set_inode_mode(item, inode->i_mode);
658 btrfs_set_inode_nlink(item, inode->i_nlink);
659 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
660 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
661 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
662 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
663 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
664 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
665 btrfs_set_inode_nblocks(item, inode->i_blocks);
666 btrfs_set_inode_generation(item, inode->i_generation);
667 }
668
669 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
670 struct btrfs_root *root,
671 struct inode *inode)
672 {
673 struct btrfs_inode_item *inode_item;
674 struct btrfs_path *path;
675 int ret;
676
677 path = btrfs_alloc_path();
678 BUG_ON(!path);
679 btrfs_init_path(path);
680 ret = btrfs_lookup_inode(trans, root, path,
681 &BTRFS_I(inode)->location, 1);
682 if (ret) {
683 if (ret > 0)
684 ret = -ENOENT;
685 goto failed;
686 }
687
688 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
689 path->slots[0],
690 struct btrfs_inode_item);
691
692 fill_inode_item(inode_item, inode);
693 btrfs_mark_buffer_dirty(path->nodes[0]);
694 ret = 0;
695 failed:
696 btrfs_release_path(root, path);
697 btrfs_free_path(path);
698 return ret;
699 }
700
701 static int btrfs_write_inode(struct inode *inode, int wait)
702 {
703 struct btrfs_root *root = BTRFS_I(inode)->root;
704 struct btrfs_trans_handle *trans;
705 int ret;
706
707 mutex_lock(&root->fs_info->fs_mutex);
708 trans = btrfs_start_transaction(root, 1);
709 ret = btrfs_update_inode(trans, root, inode);
710 if (wait)
711 btrfs_commit_transaction(trans, root);
712 else
713 btrfs_end_transaction(trans, root);
714 mutex_unlock(&root->fs_info->fs_mutex);
715 return ret;
716 }
717
718 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
719 struct btrfs_root *root,
720 u64 objectid, int mode)
721 {
722 struct inode *inode;
723 struct btrfs_inode_item inode_item;
724 struct btrfs_key *location;
725 int ret;
726
727 inode = new_inode(root->fs_info->sb);
728 if (!inode)
729 return ERR_PTR(-ENOMEM);
730
731 BTRFS_I(inode)->root = root;
732
733 inode->i_uid = current->fsuid;
734 inode->i_gid = current->fsgid;
735 inode->i_mode = mode;
736 inode->i_ino = objectid;
737 inode->i_blocks = 0;
738 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
739 fill_inode_item(&inode_item, inode);
740 location = &BTRFS_I(inode)->location;
741 location->objectid = objectid;
742 location->flags = 0;
743 location->offset = 0;
744 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
745
746 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
747 BUG_ON(ret);
748
749 insert_inode_hash(inode);
750 return inode;
751 }
752
753 static int btrfs_add_link(struct btrfs_trans_handle *trans,
754 struct dentry *dentry, struct inode *inode)
755 {
756 int ret;
757 struct btrfs_key key;
758 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
759 key.objectid = inode->i_ino;
760 key.flags = 0;
761 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
762 key.offset = 0;
763
764 ret = btrfs_insert_dir_item(trans, root,
765 dentry->d_name.name, dentry->d_name.len,
766 dentry->d_parent->d_inode->i_ino,
767 &key, 0);
768 if (ret == 0) {
769 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
770 ret = btrfs_update_inode(trans, root,
771 dentry->d_parent->d_inode);
772 }
773 return ret;
774 }
775
776 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
777 struct dentry *dentry, struct inode *inode)
778 {
779 int err = btrfs_add_link(trans, dentry, inode);
780 if (!err) {
781 d_instantiate(dentry, inode);
782 return 0;
783 }
784 if (err > 0)
785 err = -EEXIST;
786 return err;
787 }
788
789 static int btrfs_create(struct inode *dir, struct dentry *dentry,
790 int mode, struct nameidata *nd)
791 {
792 struct btrfs_trans_handle *trans;
793 struct btrfs_root *root = BTRFS_I(dir)->root;
794 struct inode *inode;
795 int err;
796 int drop_inode = 0;
797 u64 objectid;
798
799 mutex_lock(&root->fs_info->fs_mutex);
800 trans = btrfs_start_transaction(root, 1);
801
802 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
803 if (err) {
804 err = -ENOSPC;
805 goto out_unlock;
806 }
807
808 inode = btrfs_new_inode(trans, root, objectid, mode);
809 err = PTR_ERR(inode);
810 if (IS_ERR(inode))
811 goto out_unlock;
812 // FIXME mark the inode dirty
813 err = btrfs_add_nondir(trans, dentry, inode);
814 if (err)
815 drop_inode = 1;
816 else {
817 inode->i_mapping->a_ops = &btrfs_aops;
818 inode->i_fop = &btrfs_file_operations;
819 inode->i_op = &btrfs_file_inode_operations;
820 }
821 dir->i_sb->s_dirt = 1;
822 out_unlock:
823 btrfs_end_transaction(trans, root);
824 mutex_unlock(&root->fs_info->fs_mutex);
825
826 if (drop_inode) {
827 inode_dec_link_count(inode);
828 iput(inode);
829 }
830 return err;
831 }
832
833 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
834 struct btrfs_root *root,
835 u64 objectid, u64 dirid)
836 {
837 int ret;
838 char buf[2];
839 struct btrfs_key key;
840
841 buf[0] = '.';
842 buf[1] = '.';
843
844 key.objectid = objectid;
845 key.offset = 0;
846 key.flags = 0;
847 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
848
849 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
850 &key, 1);
851 if (ret)
852 goto error;
853 key.objectid = dirid;
854 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
855 &key, 1);
856 if (ret)
857 goto error;
858 error:
859 return ret;
860 }
861
862 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
863 {
864 struct inode *inode;
865 struct btrfs_trans_handle *trans;
866 struct btrfs_root *root = BTRFS_I(dir)->root;
867 int err = 0;
868 int drop_on_err = 0;
869 u64 objectid;
870
871 mutex_lock(&root->fs_info->fs_mutex);
872 trans = btrfs_start_transaction(root, 1);
873 if (IS_ERR(trans)) {
874 err = PTR_ERR(trans);
875 goto out_unlock;
876 }
877
878 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
879 if (err) {
880 err = -ENOSPC;
881 goto out_unlock;
882 }
883
884 inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode);
885 if (IS_ERR(inode)) {
886 err = PTR_ERR(inode);
887 goto out_fail;
888 }
889 drop_on_err = 1;
890 inode->i_op = &btrfs_dir_inode_operations;
891 inode->i_fop = &btrfs_dir_file_operations;
892
893 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
894 if (err)
895 goto out_fail;
896
897 inode->i_size = 6;
898 err = btrfs_update_inode(trans, root, inode);
899 if (err)
900 goto out_fail;
901 err = btrfs_add_link(trans, dentry, inode);
902 if (err)
903 goto out_fail;
904 d_instantiate(dentry, inode);
905 drop_on_err = 0;
906
907 out_fail:
908 btrfs_end_transaction(trans, root);
909 out_unlock:
910 mutex_unlock(&root->fs_info->fs_mutex);
911 if (drop_on_err)
912 iput(inode);
913 return err;
914 }
915
916 static int btrfs_sync_fs(struct super_block *sb, int wait)
917 {
918 struct btrfs_trans_handle *trans;
919 struct btrfs_root *root;
920 int ret;
921 root = btrfs_sb(sb);
922
923 sb->s_dirt = 0;
924 if (!wait) {
925 filemap_flush(root->fs_info->btree_inode->i_mapping);
926 return 0;
927 }
928 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
929 mutex_lock(&root->fs_info->fs_mutex);
930 trans = btrfs_start_transaction(root, 1);
931 ret = btrfs_commit_transaction(trans, root);
932 sb->s_dirt = 0;
933 BUG_ON(ret);
934 printk("btrfs sync_fs\n");
935 mutex_unlock(&root->fs_info->fs_mutex);
936 return 0;
937 }
938
939 #if 0
940 static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
941 struct buffer_head *result, int create)
942 {
943 struct btrfs_root *root = btrfs_sb(inode->i_sb);
944 struct btrfs_path *path;
945 struct btrfs_key key;
946 struct btrfs_leaf *leaf;
947 int num_bytes = result->b_size;
948 int item_size;
949 int ret;
950 u64 pos;
951 char *ptr;
952 int copy_size;
953 int err = 0;
954 char *safe_ptr;
955 char *data_ptr;
956
957 path = btrfs_alloc_path();
958 BUG_ON(!path);
959
960 WARN_ON(create);
961 if (create) {
962 return 0;
963 }
964 pos = iblock << inode->i_blkbits;
965 key.objectid = inode->i_ino;
966 key.flags = 0;
967 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
968 ptr = kmap(result->b_page);
969 safe_ptr = ptr;
970 ptr += (pos & (PAGE_CACHE_SIZE -1));
971 again:
972 key.offset = pos;
973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
974 if (ret) {
975 if (ret < 0)
976 err = ret;
977 else
978 err = 0;
979 goto out;
980 }
981 leaf = btrfs_buffer_leaf(path->nodes[0]);
982 item_size = btrfs_item_size(leaf->items + path->slots[0]);
983 copy_size = min(num_bytes, item_size);
984 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
985 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
986 memcpy(ptr, data_ptr, copy_size);
987 pos += copy_size;
988 num_bytes -= copy_size;
989 WARN_ON(num_bytes < 0);
990 ptr += copy_size;
991 btrfs_release_path(root, path);
992 if (num_bytes != 0) {
993 if (pos >= i_size_read(inode))
994 memset(ptr, 0, num_bytes);
995 else
996 goto again;
997 }
998 set_buffer_uptodate(result);
999 map_bh(result, inode->i_sb, 0);
1000 err = 0;
1001 out:
1002 btrfs_free_path(path);
1003 kunmap(result->b_page);
1004 return err;
1005 }
1006 #endif
1007
1008 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
1009 struct buffer_head *result, int create)
1010 {
1011 int ret;
1012 int err = 0;
1013 u64 blocknr;
1014 u64 extent_start = 0;
1015 u64 extent_end = 0;
1016 u64 objectid = inode->i_ino;
1017 struct btrfs_path *path;
1018 struct btrfs_root *root = BTRFS_I(inode)->root;
1019 struct btrfs_trans_handle *trans = NULL;
1020 struct btrfs_file_extent_item *item;
1021 struct btrfs_leaf *leaf;
1022 struct btrfs_disk_key *found_key;
1023
1024 path = btrfs_alloc_path();
1025 BUG_ON(!path);
1026 btrfs_init_path(path);
1027 if (create) {
1028 trans = btrfs_start_transaction(root, 1);
1029 WARN_ON(1);
1030 }
1031
1032 ret = btrfs_lookup_file_extent(trans, root, path,
1033 inode->i_ino,
1034 iblock << inode->i_blkbits, create);
1035 if (ret < 0) {
1036 err = ret;
1037 goto out;
1038 }
1039
1040 if (ret != 0) {
1041 if (path->slots[0] == 0) {
1042 btrfs_release_path(root, path);
1043 goto allocate;
1044 }
1045 path->slots[0]--;
1046 }
1047
1048 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1049 struct btrfs_file_extent_item);
1050 leaf = btrfs_buffer_leaf(path->nodes[0]);
1051 blocknr = btrfs_file_extent_disk_blocknr(item);
1052 blocknr += btrfs_file_extent_offset(item);
1053
1054 /* exact match found, use it, FIXME, deal with extents
1055 * other than the page size
1056 */
1057 if (0 && ret == 0) {
1058 err = 0;
1059 if (create &&
1060 btrfs_file_extent_generation(item) != trans->transid) {
1061 struct btrfs_key ins;
1062 ret = btrfs_alloc_extent(trans, root, 1,
1063 blocknr, (u64)-1, &ins);
1064 BUG_ON(ret);
1065 btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
1066 mark_buffer_dirty(path->nodes[0]);
1067 ret = btrfs_free_extent(trans, root,
1068 blocknr, 1, 0);
1069 BUG_ON(ret);
1070 blocknr = ins.objectid;
1071
1072 }
1073 btrfs_map_bh_to_logical(root, result, blocknr);
1074 goto out;
1075 }
1076
1077 /* are we inside the extent that was found? */
1078 found_key = &leaf->items[path->slots[0]].key;
1079 if (btrfs_disk_key_objectid(found_key) != objectid ||
1080 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1081 extent_end = 0;
1082 extent_start = 0;
1083 btrfs_release_path(root, path);
1084 goto allocate;
1085 }
1086
1087 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1088 extent_start = extent_start >> inode->i_blkbits;
1089 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
1090 if (iblock >= extent_start && iblock < extent_end) {
1091 err = 0;
1092 btrfs_map_bh_to_logical(root, result, blocknr + iblock -
1093 extent_start);
1094 goto out;
1095 }
1096 allocate:
1097 /* ok, create a new extent */
1098 if (!create) {
1099 err = 0;
1100 goto out;
1101 }
1102 #if 0
1103 ret = btrfs_alloc_file_extent(trans, root, objectid,
1104 iblock << inode->i_blkbits,
1105 1, extent_end, &blocknr);
1106 if (ret) {
1107 err = ret;
1108 goto out;
1109 }
1110 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1111 set_buffer_new(result);
1112 map_bh(result, inode->i_sb, blocknr);
1113
1114 btrfs_map_bh_to_logical(root, result, blocknr);
1115 #endif
1116 out:
1117 btrfs_release_path(root, path);
1118 btrfs_free_path(path);
1119 if (trans)
1120 btrfs_end_transaction(trans, root);
1121 return err;
1122 }
1123
1124 static int btrfs_get_block(struct inode *inode, sector_t iblock,
1125 struct buffer_head *result, int create)
1126 {
1127 int err;
1128 struct btrfs_root *root = BTRFS_I(inode)->root;
1129 mutex_lock(&root->fs_info->fs_mutex);
1130 err = btrfs_get_block_lock(inode, iblock, result, create);
1131 // err = btrfs_get_block_inline(inode, iblock, result, create);
1132 mutex_unlock(&root->fs_info->fs_mutex);
1133 return err;
1134 }
1135
1136 static int btrfs_prepare_write(struct file *file, struct page *page,
1137 unsigned from, unsigned to)
1138 {
1139 return nobh_prepare_write(page, from, to, btrfs_get_block);
1140 }
1141 static int btrfs_commit_write(struct file *file, struct page *page,
1142 unsigned from, unsigned to)
1143 {
1144 return nobh_commit_write(file, page, from, to);
1145 }
1146
1147 static void btrfs_write_super(struct super_block *sb)
1148 {
1149 btrfs_sync_fs(sb, 1);
1150 }
1151
1152 static int btrfs_readpage(struct file *file, struct page *page)
1153 {
1154 return mpage_readpage(page, btrfs_get_block);
1155 }
1156
1157 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1158 {
1159 return nobh_writepage(page, btrfs_get_block, wbc);
1160 }
1161
1162 static void btrfs_truncate(struct inode *inode)
1163 {
1164 struct btrfs_root *root = BTRFS_I(inode)->root;
1165 int ret;
1166 struct btrfs_trans_handle *trans;
1167
1168 if (!S_ISREG(inode->i_mode))
1169 return;
1170 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1171 return;
1172
1173 nobh_truncate_page(inode->i_mapping, inode->i_size);
1174
1175 /* FIXME, add redo link to tree so we don't leak on crash */
1176 mutex_lock(&root->fs_info->fs_mutex);
1177 trans = btrfs_start_transaction(root, 1);
1178 ret = btrfs_truncate_in_trans(trans, root, inode);
1179 BUG_ON(ret);
1180 ret = btrfs_end_transaction(trans, root);
1181 BUG_ON(ret);
1182 mutex_unlock(&root->fs_info->fs_mutex);
1183 mark_inode_dirty(inode);
1184 }
1185
1186 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1187 struct page **prepared_pages,
1188 const char __user * buf)
1189 {
1190 long page_fault = 0;
1191 int i;
1192 int offset = pos & (PAGE_CACHE_SIZE - 1);
1193
1194 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1195 size_t count = min_t(size_t,
1196 PAGE_CACHE_SIZE - offset, write_bytes);
1197 struct page *page = prepared_pages[i];
1198 fault_in_pages_readable(buf, count);
1199
1200 /* Copy data from userspace to the current page */
1201 kmap(page);
1202 page_fault = __copy_from_user(page_address(page) + offset,
1203 buf, count);
1204 /* Flush processor's dcache for this page */
1205 flush_dcache_page(page);
1206 kunmap(page);
1207 buf += count;
1208 write_bytes -= count;
1209
1210 if (page_fault)
1211 break;
1212 }
1213 return page_fault ? -EFAULT : 0;
1214 }
1215
1216 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1217 {
1218 size_t i;
1219 for (i = 0; i < num_pages; i++) {
1220 if (!pages[i])
1221 break;
1222 unlock_page(pages[i]);
1223 mark_page_accessed(pages[i]);
1224 page_cache_release(pages[i]);
1225 }
1226 }
1227 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1228 struct btrfs_root *root,
1229 struct file *file,
1230 struct page **pages,
1231 size_t num_pages,
1232 loff_t pos,
1233 size_t write_bytes)
1234 {
1235 int i;
1236 int offset;
1237 int err = 0;
1238 int ret;
1239 int this_write;
1240 struct inode *inode = file->f_path.dentry->d_inode;
1241
1242 for (i = 0; i < num_pages; i++) {
1243 offset = pos & (PAGE_CACHE_SIZE -1);
1244 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1245 /* FIXME, one block at a time */
1246
1247 mutex_lock(&root->fs_info->fs_mutex);
1248 trans = btrfs_start_transaction(root, 1);
1249 btrfs_csum_file_block(trans, root, inode->i_ino,
1250 pages[i]->index << PAGE_CACHE_SHIFT,
1251 kmap(pages[i]), PAGE_CACHE_SIZE);
1252 kunmap(pages[i]);
1253 SetPageChecked(pages[i]);
1254 ret = btrfs_end_transaction(trans, root);
1255 BUG_ON(ret);
1256 mutex_unlock(&root->fs_info->fs_mutex);
1257
1258 ret = nobh_commit_write(file, pages[i], offset,
1259 offset + this_write);
1260 pos += this_write;
1261 if (ret) {
1262 err = ret;
1263 goto failed;
1264 }
1265 WARN_ON(this_write > write_bytes);
1266 write_bytes -= this_write;
1267 }
1268 failed:
1269 return err;
1270 }
1271
1272 static int drop_extents(struct btrfs_trans_handle *trans,
1273 struct btrfs_root *root,
1274 struct inode *inode,
1275 u64 start, u64 end)
1276 {
1277 int ret;
1278 struct btrfs_key key;
1279 struct btrfs_leaf *leaf;
1280 int slot;
1281 struct btrfs_file_extent_item *extent;
1282 u64 extent_end;
1283 int keep;
1284 struct btrfs_file_extent_item old;
1285 struct btrfs_path *path;
1286 u64 search_start = start;
1287 int bookend;
1288
1289 path = btrfs_alloc_path();
1290 if (!path)
1291 return -ENOMEM;
1292 search_again:
1293 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1294 search_start, -1);
1295 if (ret < 0)
1296 goto out;
1297 if (ret > 0) {
1298 if (path->slots[0] == 0) {
1299 ret = -ENOENT;
1300 goto out;
1301 }
1302 path->slots[0]--;
1303 }
1304 while(1) {
1305 keep = 0;
1306 bookend = 0;
1307 leaf = btrfs_buffer_leaf(path->nodes[0]);
1308 slot = path->slots[0];
1309 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
1310
1311 extent = btrfs_item_ptr(leaf, slot,
1312 struct btrfs_file_extent_item);
1313 extent_end = key.offset +
1314 (btrfs_file_extent_num_blocks(extent) <<
1315 inode->i_blkbits);
1316 if (key.offset >= end || key.objectid != inode->i_ino) {
1317 ret = 0;
1318 goto out;
1319 }
1320 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1321 goto next_leaf;
1322
1323 if (end < extent_end && end >= key.offset) {
1324 memcpy(&old, extent, sizeof(old));
1325 ret = btrfs_inc_extent_ref(trans, root,
1326 btrfs_file_extent_disk_blocknr(&old),
1327 btrfs_file_extent_disk_num_blocks(&old));
1328 BUG_ON(ret);
1329 bookend = 1;
1330 }
1331
1332 if (start > key.offset) {
1333 u64 new_num;
1334 /* truncate existing extent */
1335 keep = 1;
1336 WARN_ON(start & (root->blocksize - 1));
1337 new_num = (start - key.offset) >> inode->i_blkbits;
1338 btrfs_set_file_extent_num_blocks(extent, new_num);
1339 mark_buffer_dirty(path->nodes[0]);
1340 }
1341 if (!keep) {
1342 u64 disk_blocknr;
1343 u64 disk_num_blocks;
1344 disk_blocknr = btrfs_file_extent_disk_blocknr(extent);
1345 disk_num_blocks =
1346 btrfs_file_extent_disk_num_blocks(extent);
1347 search_start = key.offset +
1348 (btrfs_file_extent_num_blocks(extent) <<
1349 inode->i_blkbits);
1350 ret = btrfs_del_item(trans, root, path);
1351 BUG_ON(ret);
1352 btrfs_release_path(root, path);
1353
1354 ret = btrfs_free_extent(trans, root, disk_blocknr,
1355 disk_num_blocks, 0);
1356
1357 BUG_ON(ret);
1358 if (!bookend && search_start >= end) {
1359 ret = 0;
1360 goto out;
1361 }
1362 if (!bookend)
1363 goto search_again;
1364 }
1365 if (bookend) {
1366 /* create bookend */
1367 struct btrfs_key ins;
1368 ins.objectid = inode->i_ino;
1369 ins.offset = end;
1370 ins.flags = 0;
1371 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
1372
1373 btrfs_release_path(root, path);
1374 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1375 sizeof(*extent));
1376 BUG_ON(ret);
1377 extent = btrfs_item_ptr(
1378 btrfs_buffer_leaf(path->nodes[0]),
1379 path->slots[0],
1380 struct btrfs_file_extent_item);
1381 btrfs_set_file_extent_disk_blocknr(extent,
1382 btrfs_file_extent_disk_blocknr(&old));
1383 btrfs_set_file_extent_disk_num_blocks(extent,
1384 btrfs_file_extent_disk_num_blocks(&old));
1385
1386 btrfs_set_file_extent_offset(extent,
1387 btrfs_file_extent_offset(&old) +
1388 ((end - key.offset) >> inode->i_blkbits));
1389 WARN_ON(btrfs_file_extent_num_blocks(&old) <
1390 (end - key.offset) >> inode->i_blkbits);
1391 btrfs_set_file_extent_num_blocks(extent,
1392 btrfs_file_extent_num_blocks(&old) -
1393 ((end - key.offset) >> inode->i_blkbits));
1394
1395 btrfs_set_file_extent_generation(extent,
1396 btrfs_file_extent_generation(&old));
1397 btrfs_mark_buffer_dirty(path->nodes[0]);
1398 ret = 0;
1399 goto out;
1400 }
1401 next_leaf:
1402 if (slot >= btrfs_header_nritems(&leaf->header) - 1) {
1403 ret = btrfs_next_leaf(root, path);
1404 if (ret) {
1405 ret = 0;
1406 goto out;
1407 }
1408 } else {
1409 path->slots[0]++;
1410 }
1411 }
1412
1413 out:
1414 btrfs_free_path(path);
1415 return ret;
1416 }
1417
1418 static int prepare_pages(struct btrfs_root *root,
1419 struct file *file,
1420 struct page **pages,
1421 size_t num_pages,
1422 loff_t pos,
1423 unsigned long first_index,
1424 unsigned long last_index,
1425 size_t write_bytes,
1426 u64 alloc_extent_start)
1427 {
1428 int i;
1429 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1430 struct inode *inode = file->f_path.dentry->d_inode;
1431 int offset;
1432 int err = 0;
1433 int this_write;
1434 struct buffer_head *bh;
1435 struct buffer_head *head;
1436 loff_t isize = i_size_read(inode);
1437
1438 memset(pages, 0, num_pages * sizeof(struct page *));
1439
1440 for (i = 0; i < num_pages; i++) {
1441 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1442 if (!pages[i]) {
1443 err = -ENOMEM;
1444 goto failed_release;
1445 }
1446 offset = pos & (PAGE_CACHE_SIZE -1);
1447 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1448 #if 0
1449 if ((pages[i]->index == first_index ||
1450 pages[i]->index == last_index) && pos < isize &&
1451 !PageUptodate(pages[i])) {
1452 ret = mpage_readpage(pages[i], btrfs_get_block);
1453 BUG_ON(ret);
1454 lock_page(pages[i]);
1455 }
1456 #endif
1457 create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize,
1458 (1 << BH_Uptodate));
1459 head = page_buffers(pages[i]);
1460 bh = head;
1461 do {
1462 err = btrfs_map_bh_to_logical(root, bh,
1463 alloc_extent_start);
1464 BUG_ON(err);
1465 if (err)
1466 goto failed_truncate;
1467 bh = bh->b_this_page;
1468 alloc_extent_start++;
1469 } while (bh != head);
1470 pos += this_write;
1471 WARN_ON(this_write > write_bytes);
1472 write_bytes -= this_write;
1473 }
1474 return 0;
1475
1476 failed_release:
1477 btrfs_drop_pages(pages, num_pages);
1478 return err;
1479
1480 failed_truncate:
1481 btrfs_drop_pages(pages, num_pages);
1482 if (pos > isize)
1483 vmtruncate(inode, isize);
1484 return err;
1485 }
1486
1487 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1488 size_t count, loff_t *ppos)
1489 {
1490 loff_t pos;
1491 size_t num_written = 0;
1492 int err = 0;
1493 int ret = 0;
1494 struct inode *inode = file->f_path.dentry->d_inode;
1495 struct btrfs_root *root = BTRFS_I(inode)->root;
1496 struct page *pages[8];
1497 unsigned long first_index;
1498 unsigned long last_index;
1499 u64 start_pos;
1500 u64 num_blocks;
1501 u64 alloc_extent_start;
1502 struct btrfs_trans_handle *trans;
1503 struct btrfs_key ins;
1504
1505 if (file->f_flags & O_DIRECT)
1506 return -EINVAL;
1507 pos = *ppos;
1508
1509 start_pos = pos & ~(root->blocksize - 1);
1510 /* FIXME */
1511 if (start_pos != pos)
1512 return -EINVAL;
1513 num_blocks = (count + pos - start_pos + root->blocksize - 1) >>
1514 inode->i_blkbits;
1515
1516 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1517 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1518 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1519 if (err)
1520 goto out;
1521 if (count == 0)
1522 goto out;
1523 err = remove_suid(file->f_path.dentry);
1524 if (err)
1525 goto out;
1526 file_update_time(file);
1527 mutex_lock(&inode->i_mutex);
1528 first_index = pos >> PAGE_CACHE_SHIFT;
1529 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
1530
1531 mutex_lock(&root->fs_info->fs_mutex);
1532 trans = btrfs_start_transaction(root, 1);
1533 if (!trans) {
1534 err = -ENOMEM;
1535 mutex_unlock(&root->fs_info->fs_mutex);
1536 goto out_unlock;
1537 }
1538 if (start_pos < inode->i_size) {
1539 ret = drop_extents(trans, root, inode,
1540 start_pos,
1541 (pos + count + root->blocksize -1) &
1542 ~(root->blocksize - 1));
1543 }
1544 ret = btrfs_alloc_extent(trans, root, num_blocks, 1,
1545 (u64)-1, &ins);
1546 BUG_ON(ret);
1547 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
1548 start_pos, ins.objectid, ins.offset);
1549 BUG_ON(ret);
1550 alloc_extent_start = ins.objectid;
1551 ret = btrfs_end_transaction(trans, root);
1552 mutex_unlock(&root->fs_info->fs_mutex);
1553
1554 while(count > 0) {
1555 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1556 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1557 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1558 PAGE_CACHE_SHIFT;
1559
1560 memset(pages, 0, sizeof(pages));
1561 ret = prepare_pages(root, file, pages, num_pages,
1562 pos, first_index, last_index,
1563 write_bytes, alloc_extent_start);
1564 BUG_ON(ret);
1565
1566 /* FIXME blocks != pagesize */
1567 alloc_extent_start += num_pages;
1568 ret = btrfs_copy_from_user(pos, num_pages,
1569 write_bytes, pages, buf);
1570 BUG_ON(ret);
1571
1572 ret = dirty_and_release_pages(NULL, root, file, pages,
1573 num_pages, pos, write_bytes);
1574 BUG_ON(ret);
1575 btrfs_drop_pages(pages, num_pages);
1576
1577 buf += write_bytes;
1578 count -= write_bytes;
1579 pos += write_bytes;
1580 num_written += write_bytes;
1581
1582 balance_dirty_pages_ratelimited(inode->i_mapping);
1583 cond_resched();
1584 }
1585 out_unlock:
1586 mutex_unlock(&inode->i_mutex);
1587 out:
1588 *ppos = pos;
1589 current->backing_dev_info = NULL;
1590 return num_written ? num_written : err;
1591 }
1592
1593 #if 0
1594 static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1595 struct page *page, loff_t pos,
1596 size_t offset, size_t write_bytes)
1597 {
1598 struct btrfs_path *path;
1599 struct btrfs_trans_handle *trans;
1600 struct btrfs_key key;
1601 struct btrfs_leaf *leaf;
1602 struct btrfs_key found_key;
1603 int ret;
1604 size_t copy_size = 0;
1605 char *dst = NULL;
1606 int err = 0;
1607 size_t num_written = 0;
1608
1609 path = btrfs_alloc_path();
1610 BUG_ON(!path);
1611 mutex_lock(&root->fs_info->fs_mutex);
1612 trans = btrfs_start_transaction(root, 1);
1613 key.objectid = inode->i_ino;
1614 key.flags = 0;
1615 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1616
1617 again:
1618 key.offset = pos;
1619 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1620 if (ret < 0) {
1621 err = ret;
1622 goto out;
1623 }
1624 if (ret == 0) {
1625 leaf = btrfs_buffer_leaf(path->nodes[0]);
1626 btrfs_disk_key_to_cpu(&found_key,
1627 &leaf->items[path->slots[0]].key);
1628 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1629 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1630 copy_size = min(write_bytes, copy_size);
1631 goto copyit;
1632 } else {
1633 int slot = path->slots[0];
1634 if (slot > 0) {
1635 slot--;
1636 }
1637 // FIXME find max key
1638 leaf = btrfs_buffer_leaf(path->nodes[0]);
1639 btrfs_disk_key_to_cpu(&found_key,
1640 &leaf->items[slot].key);
1641 if (found_key.objectid != inode->i_ino)
1642 goto insert;
1643 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1644 goto insert;
1645 copy_size = btrfs_item_size(leaf->items + slot);
1646 if (found_key.offset + copy_size <= pos)
1647 goto insert;
1648 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1649 dst += pos - found_key.offset;
1650 copy_size = copy_size - (pos - found_key.offset);
1651 BUG_ON(copy_size < 0);
1652 copy_size = min(write_bytes, copy_size);
1653 WARN_ON(copy_size == 0);
1654 goto copyit;
1655 }
1656 insert:
1657 btrfs_release_path(root, path);
1658 copy_size = min(write_bytes,
1659 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1660 sizeof(struct btrfs_item) * 4);
1661 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1662 BUG_ON(ret);
1663 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1664 path->slots[0], char);
1665 copyit:
1666 WARN_ON(copy_size == 0);
1667 WARN_ON(dst + copy_size >
1668 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1669 path->slots[0], char) +
1670 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1671 path->slots[0]));
1672 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1673 page_address(page) + offset, copy_size);
1674 mark_buffer_dirty(path->nodes[0]);
1675 btrfs_release_path(root, path);
1676 pos += copy_size;
1677 offset += copy_size;
1678 num_written += copy_size;
1679 write_bytes -= copy_size;
1680 if (write_bytes)
1681 goto again;
1682 out:
1683 btrfs_free_path(path);
1684 ret = btrfs_end_transaction(trans, root);
1685 BUG_ON(ret);
1686 mutex_unlock(&root->fs_info->fs_mutex);
1687 return num_written ? num_written : err;
1688 }
1689
1690 static ssize_t btrfs_file_inline_write(struct file *file,
1691 const char __user *buf,
1692 size_t count, loff_t *ppos)
1693 {
1694 loff_t pos;
1695 size_t num_written = 0;
1696 int err = 0;
1697 int ret = 0;
1698 struct inode *inode = file->f_path.dentry->d_inode;
1699 struct btrfs_root *root = BTRFS_I(inode)->root;
1700 unsigned long page_index;
1701
1702 if (file->f_flags & O_DIRECT)
1703 return -EINVAL;
1704 pos = *ppos;
1705
1706 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1707 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1708 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1709 if (err)
1710 goto out;
1711 if (count == 0)
1712 goto out;
1713 err = remove_suid(file->f_path.dentry);
1714 if (err)
1715 goto out;
1716 file_update_time(file);
1717 mutex_lock(&inode->i_mutex);
1718 while(count > 0) {
1719 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1720 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1721 struct page *page;
1722
1723 page_index = pos >> PAGE_CACHE_SHIFT;
1724 page = grab_cache_page(inode->i_mapping, page_index);
1725 if (!PageUptodate(page)) {
1726 ret = mpage_readpage(page, btrfs_get_block);
1727 BUG_ON(ret);
1728 lock_page(page);
1729 }
1730 ret = btrfs_copy_from_user(pos, 1,
1731 write_bytes, &page, buf);
1732 BUG_ON(ret);
1733 write_bytes = inline_one_page(root, inode, page, pos,
1734 offset, write_bytes);
1735 SetPageUptodate(page);
1736 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1737 i_size_write(inode, pos + write_bytes);
1738 mark_inode_dirty(inode);
1739 }
1740 page_cache_release(page);
1741 unlock_page(page);
1742 if (write_bytes < 0)
1743 goto out_unlock;
1744 buf += write_bytes;
1745 count -= write_bytes;
1746 pos += write_bytes;
1747 num_written += write_bytes;
1748
1749 balance_dirty_pages_ratelimited(inode->i_mapping);
1750 cond_resched();
1751 }
1752 out_unlock:
1753 mutex_unlock(&inode->i_mutex);
1754 out:
1755 *ppos = pos;
1756 current->backing_dev_info = NULL;
1757 return num_written ? num_written : err;
1758 }
1759 #endif
1760
1761 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1762 unsigned long offset, unsigned long size)
1763 {
1764 char *kaddr;
1765 unsigned long left, count = desc->count;
1766 struct inode *inode = page->mapping->host;
1767
1768 if (size > count)
1769 size = count;
1770
1771 if (!PageChecked(page)) {
1772 /* FIXME, do it per block */
1773 struct btrfs_root *root = BTRFS_I(inode)->root;
1774 int ret = btrfs_csum_verify_file_block(root,
1775 page->mapping->host->i_ino,
1776 page->index << PAGE_CACHE_SHIFT,
1777 kmap(page), PAGE_CACHE_SIZE);
1778 if (ret) {
1779 printk("failed to verify ino %lu page %lu\n",
1780 page->mapping->host->i_ino,
1781 page->index);
1782 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1783 }
1784 SetPageChecked(page);
1785 kunmap(page);
1786 }
1787 /*
1788 * Faults on the destination of a read are common, so do it before
1789 * taking the kmap.
1790 */
1791 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1792 kaddr = kmap_atomic(page, KM_USER0);
1793 left = __copy_to_user_inatomic(desc->arg.buf,
1794 kaddr + offset, size);
1795 kunmap_atomic(kaddr, KM_USER0);
1796 if (left == 0)
1797 goto success;
1798 }
1799
1800 /* Do it the slow way */
1801 kaddr = kmap(page);
1802 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1803 kunmap(page);
1804
1805 if (left) {
1806 size -= left;
1807 desc->error = -EFAULT;
1808 }
1809 success:
1810 desc->count = count - size;
1811 desc->written += size;
1812 desc->arg.buf += size;
1813 return size;
1814 }
1815
1816 /**
1817 * btrfs_file_aio_read - filesystem read routine
1818 * @iocb: kernel I/O control block
1819 * @iov: io vector request
1820 * @nr_segs: number of segments in the iovec
1821 * @pos: current file position
1822 */
1823 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1824 unsigned long nr_segs, loff_t pos)
1825 {
1826 struct file *filp = iocb->ki_filp;
1827 ssize_t retval;
1828 unsigned long seg;
1829 size_t count;
1830 loff_t *ppos = &iocb->ki_pos;
1831
1832 count = 0;
1833 for (seg = 0; seg < nr_segs; seg++) {
1834 const struct iovec *iv = &iov[seg];
1835
1836 /*
1837 * If any segment has a negative length, or the cumulative
1838 * length ever wraps negative then return -EINVAL.
1839 */
1840 count += iv->iov_len;
1841 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1842 return -EINVAL;
1843 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1844 continue;
1845 if (seg == 0)
1846 return -EFAULT;
1847 nr_segs = seg;
1848 count -= iv->iov_len; /* This segment is no good */
1849 break;
1850 }
1851 retval = 0;
1852 if (count) {
1853 for (seg = 0; seg < nr_segs; seg++) {
1854 read_descriptor_t desc;
1855
1856 desc.written = 0;
1857 desc.arg.buf = iov[seg].iov_base;
1858 desc.count = iov[seg].iov_len;
1859 if (desc.count == 0)
1860 continue;
1861 desc.error = 0;
1862 do_generic_file_read(filp, ppos, &desc,
1863 btrfs_read_actor);
1864 retval += desc.written;
1865 if (desc.error) {
1866 retval = retval ?: desc.error;
1867 break;
1868 }
1869 }
1870 }
1871 return retval;
1872 }
1873
1874 static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1875 {
1876 struct btrfs_trans_handle *trans;
1877 struct btrfs_key key;
1878 struct btrfs_root_item root_item;
1879 struct btrfs_inode_item *inode_item;
1880 struct buffer_head *subvol;
1881 struct btrfs_leaf *leaf;
1882 struct btrfs_root *new_root;
1883 struct inode *inode;
1884 int ret;
1885 u64 objectid;
1886 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1887
1888 mutex_lock(&root->fs_info->fs_mutex);
1889 trans = btrfs_start_transaction(root, 1);
1890 BUG_ON(!trans);
1891
1892 subvol = btrfs_alloc_free_block(trans, root);
1893 leaf = btrfs_buffer_leaf(subvol);
1894 btrfs_set_header_nritems(&leaf->header, 0);
1895 btrfs_set_header_level(&leaf->header, 0);
1896 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
1897 btrfs_set_header_generation(&leaf->header, trans->transid);
1898 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
1899 sizeof(leaf->header.fsid));
1900
1901 inode_item = &root_item.inode;
1902 memset(inode_item, 0, sizeof(*inode_item));
1903 btrfs_set_inode_generation(inode_item, 1);
1904 btrfs_set_inode_size(inode_item, 3);
1905 btrfs_set_inode_nlink(inode_item, 1);
1906 btrfs_set_inode_nblocks(inode_item, 1);
1907 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
1908
1909 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
1910 btrfs_set_root_refs(&root_item, 1);
1911
1912 mark_buffer_dirty(subvol);
1913 brelse(subvol);
1914 subvol = NULL;
1915
1916 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1917 0, &objectid);
1918 BUG_ON(ret);
1919
1920 btrfs_set_root_dirid(&root_item, new_dirid);
1921
1922 key.objectid = objectid;
1923 key.offset = 1;
1924 key.flags = 0;
1925 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1926 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1927 &root_item);
1928 BUG_ON(ret);
1929
1930 /*
1931 * insert the directory item
1932 */
1933 key.offset = (u64)-1;
1934 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1935 name, namelen,
1936 root->fs_info->sb->s_root->d_inode->i_ino,
1937 &key, 0);
1938 BUG_ON(ret);
1939
1940 ret = btrfs_commit_transaction(trans, root);
1941 BUG_ON(ret);
1942
1943 new_root = btrfs_read_fs_root(root->fs_info, &key);
1944 BUG_ON(!new_root);
1945
1946 trans = btrfs_start_transaction(new_root, 1);
1947 BUG_ON(!trans);
1948
1949 inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700);
1950 inode->i_op = &btrfs_dir_inode_operations;
1951 inode->i_fop = &btrfs_dir_file_operations;
1952
1953 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
1954 BUG_ON(ret);
1955
1956 inode->i_nlink = 1;
1957 inode->i_size = 6;
1958 ret = btrfs_update_inode(trans, new_root, inode);
1959 BUG_ON(ret);
1960
1961 ret = btrfs_commit_transaction(trans, new_root);
1962 BUG_ON(ret);
1963
1964 iput(inode);
1965
1966 mutex_unlock(&root->fs_info->fs_mutex);
1967 return 0;
1968 }
1969
1970 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1971 {
1972 struct btrfs_trans_handle *trans;
1973 struct btrfs_key key;
1974 struct btrfs_root_item new_root_item;
1975 int ret;
1976 u64 objectid;
1977
1978 if (!root->ref_cows)
1979 return -EINVAL;
1980
1981 mutex_lock(&root->fs_info->fs_mutex);
1982 trans = btrfs_start_transaction(root, 1);
1983 BUG_ON(!trans);
1984
1985 ret = btrfs_update_inode(trans, root, root->inode);
1986 BUG_ON(ret);
1987
1988 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1989 0, &objectid);
1990 BUG_ON(ret);
1991
1992 memcpy(&new_root_item, &root->root_item,
1993 sizeof(new_root_item));
1994
1995 key.objectid = objectid;
1996 key.offset = 1;
1997 key.flags = 0;
1998 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1999 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
2000
2001 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2002 &new_root_item);
2003 BUG_ON(ret);
2004
2005 /*
2006 * insert the directory item
2007 */
2008 key.offset = (u64)-1;
2009 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2010 name, namelen,
2011 root->fs_info->sb->s_root->d_inode->i_ino,
2012 &key, 0);
2013
2014 BUG_ON(ret);
2015
2016 ret = btrfs_inc_root_ref(trans, root);
2017 BUG_ON(ret);
2018
2019 ret = btrfs_commit_transaction(trans, root);
2020 BUG_ON(ret);
2021 mutex_unlock(&root->fs_info->fs_mutex);
2022 return 0;
2023 }
2024
2025 static int add_disk(struct btrfs_root *root, char *name, int namelen)
2026 {
2027 struct block_device *bdev;
2028 struct btrfs_path *path;
2029 struct super_block *sb = root->fs_info->sb;
2030 struct btrfs_root *dev_root = root->fs_info->dev_root;
2031 struct btrfs_trans_handle *trans;
2032 struct btrfs_device_item *dev_item;
2033 struct btrfs_key key;
2034 u16 item_size;
2035 u64 num_blocks;
2036 u64 new_blocks;
2037 u64 device_id;
2038 int ret;
2039
2040 printk("adding disk %s\n", name);
2041 path = btrfs_alloc_path();
2042 if (!path)
2043 return -ENOMEM;
2044 num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super);
2045 bdev = open_bdev_excl(name, O_RDWR, sb);
2046 if (IS_ERR(bdev)) {
2047 ret = PTR_ERR(bdev);
2048 printk("open bdev excl failed ret %d\n", ret);
2049 goto out_nolock;
2050 }
2051 set_blocksize(bdev, sb->s_blocksize);
2052 new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2053 key.objectid = num_blocks;
2054 key.offset = new_blocks;
2055 key.flags = 0;
2056 btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY);
2057
2058 mutex_lock(&dev_root->fs_info->fs_mutex);
2059 trans = btrfs_start_transaction(dev_root, 1);
2060 item_size = sizeof(*dev_item) + namelen;
2061 printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size);
2062 ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size);
2063 if (ret) {
2064 printk("insert failed %d\n", ret);
2065 close_bdev_excl(bdev);
2066 if (ret > 0)
2067 ret = -EEXIST;
2068 goto out;
2069 }
2070 dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2071 path->slots[0], struct btrfs_device_item);
2072 btrfs_set_device_pathlen(dev_item, namelen);
2073 memcpy(dev_item + 1, name, namelen);
2074
2075 device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1;
2076 btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id);
2077 btrfs_set_device_id(dev_item, device_id);
2078 mark_buffer_dirty(path->nodes[0]);
2079
2080 ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks,
2081 new_blocks);
2082
2083 if (!ret) {
2084 btrfs_set_super_total_blocks(root->fs_info->disk_super,
2085 num_blocks + new_blocks);
2086 i_size_write(root->fs_info->btree_inode,
2087 (num_blocks + new_blocks) <<
2088 root->fs_info->btree_inode->i_blkbits);
2089 }
2090
2091 out:
2092 ret = btrfs_commit_transaction(trans, dev_root);
2093 BUG_ON(ret);
2094 mutex_unlock(&root->fs_info->fs_mutex);
2095 out_nolock:
2096 btrfs_free_path(path);
2097
2098 return ret;
2099 }
2100
2101 static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
2102 cmd, unsigned long arg)
2103 {
2104 struct btrfs_root *root = BTRFS_I(inode)->root;
2105 struct btrfs_ioctl_vol_args vol_args;
2106 int ret = 0;
2107 int namelen;
2108 struct btrfs_path *path;
2109 u64 root_dirid;
2110
2111 switch (cmd) {
2112 case BTRFS_IOC_SNAP_CREATE:
2113 if (copy_from_user(&vol_args,
2114 (struct btrfs_ioctl_vol_args __user *)arg,
2115 sizeof(vol_args)))
2116 return -EFAULT;
2117 namelen = strlen(vol_args.name);
2118 if (namelen > BTRFS_VOL_NAME_MAX)
2119 return -EINVAL;
2120 path = btrfs_alloc_path();
2121 if (!path)
2122 return -ENOMEM;
2123 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2124 mutex_lock(&root->fs_info->fs_mutex);
2125 ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2126 path, root_dirid,
2127 vol_args.name, namelen, 0);
2128 mutex_unlock(&root->fs_info->fs_mutex);
2129 btrfs_free_path(path);
2130 if (ret == 0)
2131 return -EEXIST;
2132
2133 if (root == root->fs_info->tree_root)
2134 ret = create_subvol(root, vol_args.name, namelen);
2135 else
2136 ret = create_snapshot(root, vol_args.name, namelen);
2137 WARN_ON(ret);
2138 break;
2139 case BTRFS_IOC_ADD_DISK:
2140 if (copy_from_user(&vol_args,
2141 (struct btrfs_ioctl_vol_args __user *)arg,
2142 sizeof(vol_args)))
2143 return -EFAULT;
2144 namelen = strlen(vol_args.name);
2145 if (namelen > BTRFS_VOL_NAME_MAX)
2146 return -EINVAL;
2147 vol_args.name[namelen] = '\0';
2148 ret = add_disk(root, vol_args.name, namelen);
2149 break;
2150 default:
2151 return -ENOTTY;
2152 }
2153 return ret;
2154 }
2155
2156 static struct kmem_cache *btrfs_inode_cachep;
2157 struct kmem_cache *btrfs_trans_handle_cachep;
2158 struct kmem_cache *btrfs_transaction_cachep;
2159 struct kmem_cache *btrfs_bit_radix_cachep;
2160 struct kmem_cache *btrfs_path_cachep;
2161
2162 /*
2163 * Called inside transaction, so use GFP_NOFS
2164 */
2165 static struct inode *btrfs_alloc_inode(struct super_block *sb)
2166 {
2167 struct btrfs_inode *ei;
2168
2169 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2170 if (!ei)
2171 return NULL;
2172 return &ei->vfs_inode;
2173 }
2174
2175 static void btrfs_destroy_inode(struct inode *inode)
2176 {
2177 WARN_ON(!list_empty(&inode->i_dentry));
2178 WARN_ON(inode->i_data.nrpages);
2179
2180 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2181 }
2182
2183 static void init_once(void * foo, struct kmem_cache * cachep,
2184 unsigned long flags)
2185 {
2186 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2187
2188 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2189 SLAB_CTOR_CONSTRUCTOR) {
2190 inode_init_once(&ei->vfs_inode);
2191 }
2192 }
2193
2194 static int init_inodecache(void)
2195 {
2196 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
2197 sizeof(struct btrfs_inode),
2198 0, (SLAB_RECLAIM_ACCOUNT|
2199 SLAB_MEM_SPREAD),
2200 init_once, NULL);
2201 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
2202 sizeof(struct btrfs_trans_handle),
2203 0, (SLAB_RECLAIM_ACCOUNT|
2204 SLAB_MEM_SPREAD),
2205 NULL, NULL);
2206 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
2207 sizeof(struct btrfs_transaction),
2208 0, (SLAB_RECLAIM_ACCOUNT|
2209 SLAB_MEM_SPREAD),
2210 NULL, NULL);
2211 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
2212 sizeof(struct btrfs_transaction),
2213 0, (SLAB_RECLAIM_ACCOUNT|
2214 SLAB_MEM_SPREAD),
2215 NULL, NULL);
2216 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
2217 256,
2218 0, (SLAB_RECLAIM_ACCOUNT|
2219 SLAB_MEM_SPREAD |
2220 SLAB_DESTROY_BY_RCU),
2221 NULL, NULL);
2222 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
2223 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
2224 return -ENOMEM;
2225 return 0;
2226 }
2227
2228 static void destroy_inodecache(void)
2229 {
2230 kmem_cache_destroy(btrfs_inode_cachep);
2231 kmem_cache_destroy(btrfs_trans_handle_cachep);
2232 kmem_cache_destroy(btrfs_transaction_cachep);
2233 kmem_cache_destroy(btrfs_bit_radix_cachep);
2234 kmem_cache_destroy(btrfs_path_cachep);
2235 }
2236
2237 static int btrfs_get_sb(struct file_system_type *fs_type,
2238 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2239 {
2240 return get_sb_bdev(fs_type, flags, dev_name, data,
2241 btrfs_fill_super, mnt);
2242 }
2243
2244 static struct file_system_type btrfs_fs_type = {
2245 .owner = THIS_MODULE,
2246 .name = "btrfs",
2247 .get_sb = btrfs_get_sb,
2248 .kill_sb = kill_block_super,
2249 .fs_flags = FS_REQUIRES_DEV,
2250 };
2251
2252 static struct super_operations btrfs_super_ops = {
2253 .statfs = simple_statfs,
2254 .delete_inode = btrfs_delete_inode,
2255 .put_super = btrfs_put_super,
2256 .read_inode = btrfs_read_locked_inode,
2257 .write_super = btrfs_write_super,
2258 .sync_fs = btrfs_sync_fs,
2259 .write_inode = btrfs_write_inode,
2260 .alloc_inode = btrfs_alloc_inode,
2261 .destroy_inode = btrfs_destroy_inode,
2262 };
2263
2264 static struct inode_operations btrfs_dir_inode_operations = {
2265 .lookup = btrfs_lookup,
2266 .create = btrfs_create,
2267 .unlink = btrfs_unlink,
2268 .mkdir = btrfs_mkdir,
2269 .rmdir = btrfs_rmdir,
2270 };
2271
2272 static struct inode_operations btrfs_dir_ro_inode_operations = {
2273 .lookup = btrfs_lookup,
2274 };
2275
2276 static struct file_operations btrfs_dir_file_operations = {
2277 .llseek = generic_file_llseek,
2278 .read = generic_read_dir,
2279 .readdir = btrfs_readdir,
2280 .ioctl = btrfs_ioctl,
2281 };
2282
2283 static struct address_space_operations btrfs_aops = {
2284 .readpage = btrfs_readpage,
2285 .writepage = btrfs_writepage,
2286 .sync_page = block_sync_page,
2287 .prepare_write = btrfs_prepare_write,
2288 .commit_write = btrfs_commit_write,
2289 };
2290
2291 static struct inode_operations btrfs_file_inode_operations = {
2292 .truncate = btrfs_truncate,
2293 };
2294
2295 static struct file_operations btrfs_file_operations = {
2296 .llseek = generic_file_llseek,
2297 .read = do_sync_read,
2298 .aio_read = btrfs_file_aio_read,
2299 .write = btrfs_file_write,
2300 .mmap = generic_file_mmap,
2301 .open = generic_file_open,
2302 .ioctl = btrfs_ioctl,
2303 };
2304
2305 static int __init init_btrfs_fs(void)
2306 {
2307 int err;
2308 printk("btrfs loaded!\n");
2309 err = init_inodecache();
2310 if (err)
2311 return err;
2312 kset_set_kset_s(&btrfs_subsys, fs_subsys);
2313 err = subsystem_register(&btrfs_subsys);
2314 if (err)
2315 goto out;
2316 return register_filesystem(&btrfs_fs_type);
2317 out:
2318 destroy_inodecache();
2319 return err;
2320 }
2321
2322 static void __exit exit_btrfs_fs(void)
2323 {
2324 destroy_inodecache();
2325 unregister_filesystem(&btrfs_fs_type);
2326 subsystem_unregister(&btrfs_subsys);
2327 printk("btrfs unloaded\n");
2328 }
2329
2330 module_init(init_btrfs_fs)
2331 module_exit(exit_btrfs_fs)
2332
2333 MODULE_LICENSE("GPL");