]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/btrfs/super.c
Btrfs: corruptions fixed
[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
19 #define BTRFS_SUPER_MAGIC 0x9123682E
20
21 static struct inode_operations btrfs_dir_inode_operations;
22 static struct super_operations btrfs_super_ops;
23 static struct file_operations btrfs_dir_file_operations;
24 static struct inode_operations btrfs_file_inode_operations;
25 static struct address_space_operations btrfs_aops;
26 static struct file_operations btrfs_file_operations;
27
28 static int check_inode(struct inode *inode)
29 {
30 struct btrfs_inode *ei = BTRFS_I(inode);
31 WARN_ON(ei->magic != 0xDEADBEEF);
32 WARN_ON(ei->magic2 != 0xDEADBEAF);
33 return 0;
34 }
35
36 static void btrfs_read_locked_inode(struct inode *inode)
37 {
38 struct btrfs_path *path;
39 struct btrfs_inode_item *inode_item;
40 struct btrfs_root *root = btrfs_sb(inode->i_sb);
41 int ret;
42
43 path = btrfs_alloc_path();
44 BUG_ON(!path);
45 btrfs_init_path(path);
46 mutex_lock(&root->fs_info->fs_mutex);
47
48 check_inode(inode);
49 ret = btrfs_lookup_inode(NULL, root, path, inode->i_ino, 0);
50 if (ret) {
51 btrfs_release_path(root, path);
52 btrfs_free_path(path);
53 mutex_unlock(&root->fs_info->fs_mutex);
54 make_bad_inode(inode);
55 return;
56 }
57 check_inode(inode);
58 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
59 path->slots[0],
60 struct btrfs_inode_item);
61
62 inode->i_mode = btrfs_inode_mode(inode_item);
63 inode->i_nlink = btrfs_inode_nlink(inode_item);
64 inode->i_uid = btrfs_inode_uid(inode_item);
65 inode->i_gid = btrfs_inode_gid(inode_item);
66 inode->i_size = btrfs_inode_size(inode_item);
67 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
68 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
69 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
70 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
71 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
72 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
73 inode->i_blocks = btrfs_inode_nblocks(inode_item);
74 inode->i_generation = btrfs_inode_generation(inode_item);
75
76 btrfs_release_path(root, path);
77 btrfs_free_path(path);
78 inode_item = NULL;
79
80 mutex_unlock(&root->fs_info->fs_mutex);
81 check_inode(inode);
82 switch (inode->i_mode & S_IFMT) {
83 #if 0
84 default:
85 init_special_inode(inode, inode->i_mode,
86 btrfs_inode_rdev(inode_item));
87 break;
88 #endif
89 case S_IFREG:
90 inode->i_mapping->a_ops = &btrfs_aops;
91 inode->i_fop = &btrfs_file_operations;
92 inode->i_op = &btrfs_file_inode_operations;
93 break;
94 case S_IFDIR:
95 inode->i_op = &btrfs_dir_inode_operations;
96 inode->i_fop = &btrfs_dir_file_operations;
97 break;
98 case S_IFLNK:
99 // inode->i_op = &page_symlink_inode_operations;
100 break;
101 }
102 check_inode(inode);
103 return;
104 }
105
106 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
107 struct btrfs_root *root,
108 struct inode *dir,
109 struct dentry *dentry)
110 {
111 struct btrfs_path *path;
112 const char *name = dentry->d_name.name;
113 int name_len = dentry->d_name.len;
114 int ret;
115 u64 objectid;
116 struct btrfs_dir_item *di;
117
118 path = btrfs_alloc_path();
119 BUG_ON(!path);
120 btrfs_init_path(path);
121 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
122 name, name_len, -1);
123 if (ret < 0)
124 goto err;
125 if (ret > 0) {
126 ret = -ENOENT;
127 goto err;
128 }
129 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
130 struct btrfs_dir_item);
131 objectid = btrfs_dir_objectid(di);
132
133 ret = btrfs_del_item(trans, root, path);
134 BUG_ON(ret);
135 dentry->d_inode->i_ctime = dir->i_ctime;
136 err:
137 btrfs_release_path(root, path);
138 btrfs_free_path(path);
139 if (ret == 0)
140 inode_dec_link_count(dentry->d_inode);
141 return ret;
142 }
143
144 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
145 {
146 struct btrfs_root *root;
147 struct btrfs_trans_handle *trans;
148 int ret;
149
150 root = btrfs_sb(dir->i_sb);
151 mutex_lock(&root->fs_info->fs_mutex);
152 trans = btrfs_start_transaction(root, 1);
153 ret = btrfs_unlink_trans(trans, root, dir, dentry);
154 btrfs_end_transaction(trans, root);
155 mutex_unlock(&root->fs_info->fs_mutex);
156 return ret;
157 }
158
159 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
160 {
161 struct inode *inode = dentry->d_inode;
162 int err;
163 int ret;
164 struct btrfs_root *root = btrfs_sb(dir->i_sb);
165 struct btrfs_path *path;
166 struct btrfs_key key;
167 struct btrfs_trans_handle *trans;
168 struct btrfs_disk_key *found_key;
169 struct btrfs_leaf *leaf;
170
171 path = btrfs_alloc_path();
172 BUG_ON(!path);
173 btrfs_init_path(path);
174 mutex_lock(&root->fs_info->fs_mutex);
175 trans = btrfs_start_transaction(root, 1);
176 key.objectid = inode->i_ino;
177 key.offset = (u64)-1;
178 key.flags = 0;
179 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
180 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
181 if (ret < 0) {
182 err = ret;
183 goto out;
184 }
185
186 BUG_ON(ret == 0);
187 BUG_ON(path->slots[0] == 0);
188 path->slots[0]--;
189 leaf = btrfs_buffer_leaf(path->nodes[0]);
190 found_key = &leaf->items[path->slots[0]].key;
191 if (btrfs_disk_key_objectid(found_key) != inode->i_ino) {
192 err = -ENOENT;
193 goto out;
194 }
195 if (btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
196 btrfs_disk_key_offset(found_key) != 2) {
197 err = -ENOTEMPTY;
198 goto out;
199 }
200 ret = btrfs_del_item(trans, root, path);
201 BUG_ON(ret);
202 btrfs_release_path(root, path);
203 key.offset = 1;
204 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
205 if (ret < 0) {
206 err = ret;
207 goto out;
208 }
209 if (ret > 0) {
210 err = -ENOTEMPTY;
211 goto out;
212 }
213 ret = btrfs_del_item(trans, root, path);
214 if (ret) {
215 err = ret;
216 goto out;
217 }
218 btrfs_release_path(root, path);
219
220 /* now the directory is empty */
221 err = btrfs_unlink_trans(trans, root, dir, dentry);
222 if (!err) {
223 inode->i_size = 0;
224 }
225 out:
226 btrfs_release_path(root, path);
227 btrfs_free_path(path);
228 mutex_unlock(&root->fs_info->fs_mutex);
229 ret = btrfs_end_transaction(trans, root);
230 if (ret && !err)
231 err = ret;
232 return err;
233 }
234
235 static int btrfs_free_inode(struct btrfs_trans_handle *trans,
236 struct btrfs_root *root,
237 struct inode *inode)
238 {
239 u64 objectid = inode->i_ino;
240 struct btrfs_path *path;
241 struct btrfs_inode_map_item *map;
242 struct btrfs_key stat_data_key;
243 int ret;
244
245 clear_inode(inode);
246
247 path = btrfs_alloc_path();
248 BUG_ON(!path);
249 btrfs_init_path(path);
250 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
251 if (ret) {
252 if (ret > 0)
253 ret = -ENOENT;
254 goto error;
255 }
256 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
257 struct btrfs_inode_map_item);
258 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
259 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
260 BUG_ON(ret);
261 btrfs_release_path(root, path);
262
263 ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
264 BUG_ON(ret);
265 ret = btrfs_del_item(trans, root, path);
266 BUG_ON(ret);
267 error:
268 btrfs_release_path(root, path);
269 btrfs_free_path(path);
270 return ret;
271 }
272
273 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
274 struct btrfs_root *root,
275 struct inode *inode)
276 {
277 int ret;
278 struct btrfs_path *path;
279 struct btrfs_key key;
280 struct btrfs_disk_key *found_key;
281 struct btrfs_leaf *leaf;
282 struct btrfs_file_extent_item *fi = NULL;
283 u64 extent_start = 0;
284 u64 extent_num_blocks = 0;
285 int found_extent;
286
287 path = btrfs_alloc_path();
288 BUG_ON(!path);
289 /* FIXME, add redo link to tree so we don't leak on crash */
290 key.objectid = inode->i_ino;
291 key.offset = (u64)-1;
292 key.flags = 0;
293 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
294 while(1) {
295 btrfs_init_path(path);
296 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
297 if (ret < 0) {
298 goto error;
299 }
300 if (ret > 0) {
301 BUG_ON(path->slots[0] == 0);
302 path->slots[0]--;
303 }
304 leaf = btrfs_buffer_leaf(path->nodes[0]);
305 found_key = &leaf->items[path->slots[0]].key;
306 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
307 break;
308 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
309 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
310 break;
311 if (btrfs_disk_key_offset(found_key) < inode->i_size)
312 break;
313 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
314 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
315 path->slots[0],
316 struct btrfs_file_extent_item);
317 extent_start = btrfs_file_extent_disk_blocknr(fi);
318 extent_num_blocks =
319 btrfs_file_extent_disk_num_blocks(fi);
320 inode->i_blocks -=
321 btrfs_file_extent_num_blocks(fi) >> 9;
322 found_extent = 1;
323 } else {
324 found_extent = 0;
325 }
326 ret = btrfs_del_item(trans, root, path);
327 BUG_ON(ret);
328 btrfs_release_path(root, path);
329 if (found_extent) {
330 ret = btrfs_free_extent(trans, root, extent_start,
331 extent_num_blocks, 0);
332 BUG_ON(ret);
333 }
334 }
335 ret = 0;
336 error:
337 btrfs_release_path(root, path);
338 btrfs_free_path(path);
339 return ret;
340 }
341
342 static void btrfs_delete_inode(struct inode *inode)
343 {
344 struct btrfs_trans_handle *trans;
345 struct btrfs_root *root = btrfs_sb(inode->i_sb);
346 int ret;
347
348 truncate_inode_pages(&inode->i_data, 0);
349 if (is_bad_inode(inode)) {
350 goto no_delete;
351 }
352 inode->i_size = 0;
353 mutex_lock(&root->fs_info->fs_mutex);
354 trans = btrfs_start_transaction(root, 1);
355 if (S_ISREG(inode->i_mode)) {
356 ret = btrfs_truncate_in_trans(trans, root, inode);
357 BUG_ON(ret);
358 }
359 btrfs_free_inode(trans, root, inode);
360 btrfs_end_transaction(trans, root);
361 mutex_unlock(&root->fs_info->fs_mutex);
362 return;
363 no_delete:
364 clear_inode(inode);
365 }
366
367 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
368 ino_t *ino)
369 {
370 const char *name = dentry->d_name.name;
371 int namelen = dentry->d_name.len;
372 struct btrfs_dir_item *di;
373 struct btrfs_path *path;
374 struct btrfs_root *root = btrfs_sb(dir->i_sb);
375 int ret;
376
377 path = btrfs_alloc_path();
378 BUG_ON(!path);
379 btrfs_init_path(path);
380 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
381 namelen, 0);
382 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
383 *ino = 0;
384 ret = 0;
385 goto out;
386 }
387 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
388 struct btrfs_dir_item);
389 *ino = btrfs_dir_objectid(di);
390 out:
391 btrfs_release_path(root, path);
392 btrfs_free_path(path);
393 check_inode(dir);
394 return ret;
395 }
396
397 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
398 struct nameidata *nd)
399 {
400 struct inode * inode;
401 struct btrfs_root *root = btrfs_sb(dir->i_sb);
402 ino_t ino;
403 int ret;
404
405 if (dentry->d_name.len > BTRFS_NAME_LEN)
406 return ERR_PTR(-ENAMETOOLONG);
407 mutex_lock(&root->fs_info->fs_mutex);
408 ret = btrfs_inode_by_name(dir, dentry, &ino);
409 mutex_unlock(&root->fs_info->fs_mutex);
410 if (ret < 0)
411 return ERR_PTR(ret);
412 inode = NULL;
413 if (ino) {
414 inode = iget(dir->i_sb, ino);
415 if (!inode)
416 return ERR_PTR(-EACCES);
417 check_inode(inode);
418 }
419 check_inode(dir);
420 return d_splice_alias(inode, dentry);
421 }
422
423 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
424 {
425 struct inode *inode = filp->f_path.dentry->d_inode;
426 struct btrfs_root *root = btrfs_sb(inode->i_sb);
427 struct btrfs_item *item;
428 struct btrfs_dir_item *di;
429 struct btrfs_key key;
430 struct btrfs_path *path;
431 int ret;
432 u32 nritems;
433 struct btrfs_leaf *leaf;
434 int slot;
435 int advance;
436 unsigned char d_type = DT_UNKNOWN;
437 int over = 0;
438
439 mutex_lock(&root->fs_info->fs_mutex);
440 key.objectid = inode->i_ino;
441 key.flags = 0;
442 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
443 key.offset = filp->f_pos;
444 path = btrfs_alloc_path();
445 btrfs_init_path(path);
446 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
447 if (ret < 0) {
448 goto err;
449 }
450 advance = 0;
451 while(1) {
452 leaf = btrfs_buffer_leaf(path->nodes[0]);
453 nritems = btrfs_header_nritems(&leaf->header);
454 slot = path->slots[0];
455 if (advance || slot >= nritems) {
456 if (slot >= nritems -1) {
457 ret = btrfs_next_leaf(root, path);
458 if (ret)
459 break;
460 leaf = btrfs_buffer_leaf(path->nodes[0]);
461 nritems = btrfs_header_nritems(&leaf->header);
462 slot = path->slots[0];
463 } else {
464 slot++;
465 path->slots[0]++;
466 }
467 }
468 advance = 1;
469 item = leaf->items + slot;
470 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
471 break;
472 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
473 continue;
474 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
475 continue;
476
477 advance = 1;
478 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
479 over = filldir(dirent, (const char *)(di + 1),
480 btrfs_dir_name_len(di),
481 btrfs_disk_key_offset(&item->key),
482 btrfs_dir_objectid(di), d_type);
483 if (over) {
484 filp->f_pos = btrfs_disk_key_offset(&item->key);
485 break;
486 }
487 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
488 }
489 ret = 0;
490 err:
491 btrfs_release_path(root, path);
492 btrfs_free_path(path);
493 mutex_unlock(&root->fs_info->fs_mutex);
494 return ret;
495 }
496
497 static void btrfs_put_super (struct super_block * sb)
498 {
499 struct btrfs_root *root = btrfs_sb(sb);
500 int ret;
501
502 ret = close_ctree(root);
503 if (ret) {
504 printk("close ctree returns %d\n", ret);
505 }
506 sb->s_fs_info = NULL;
507 }
508
509 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
510 {
511 struct inode * inode;
512 struct dentry * root_dentry;
513 struct btrfs_super_block *disk_super;
514 struct btrfs_root *root;
515
516 sb->s_maxbytes = MAX_LFS_FILESIZE;
517 sb->s_magic = BTRFS_SUPER_MAGIC;
518 sb->s_op = &btrfs_super_ops;
519 sb->s_time_gran = 1;
520
521 root = open_ctree(sb);
522
523 if (!root) {
524 printk("btrfs: open_ctree failed\n");
525 return -EIO;
526 }
527 sb->s_fs_info = root;
528 disk_super = root->fs_info->disk_super;
529 printk("read in super total blocks %Lu root %Lu\n",
530 btrfs_super_total_blocks(disk_super),
531 btrfs_super_root_dir(disk_super));
532
533 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
534 if (!inode)
535 return -ENOMEM;
536 if (inode->i_state & I_NEW) {
537 btrfs_read_locked_inode(inode);
538 unlock_new_inode(inode);
539 }
540
541 root_dentry = d_alloc_root(inode);
542 if (!root_dentry) {
543 iput(inode);
544 return -ENOMEM;
545 }
546 sb->s_root = root_dentry;
547
548 return 0;
549 }
550
551 static void fill_inode_item(struct btrfs_inode_item *item,
552 struct inode *inode)
553 {
554 btrfs_set_inode_uid(item, inode->i_uid);
555 btrfs_set_inode_gid(item, inode->i_gid);
556 btrfs_set_inode_size(item, inode->i_size);
557 btrfs_set_inode_mode(item, inode->i_mode);
558 btrfs_set_inode_nlink(item, inode->i_nlink);
559 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
560 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
561 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
562 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
563 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
564 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
565 btrfs_set_inode_nblocks(item, inode->i_blocks);
566 btrfs_set_inode_generation(item, inode->i_generation);
567 check_inode(inode);
568 }
569
570 static int btrfs_update_inode(struct btrfs_trans_handle *trans,
571 struct btrfs_root *root,
572 struct inode *inode)
573 {
574 struct btrfs_inode_item *inode_item;
575 struct btrfs_path *path;
576 int ret;
577
578 path = btrfs_alloc_path();
579 BUG_ON(!path);
580 btrfs_init_path(path);
581
582 ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
583 if (ret) {
584 if (ret > 0)
585 ret = -ENOENT;
586 goto failed;
587 }
588
589 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
590 path->slots[0],
591 struct btrfs_inode_item);
592
593 fill_inode_item(inode_item, inode);
594 btrfs_mark_buffer_dirty(path->nodes[0]);
595 failed:
596 btrfs_release_path(root, path);
597 btrfs_free_path(path);
598 check_inode(inode);
599 return 0;
600 }
601
602 static int btrfs_write_inode(struct inode *inode, int wait)
603 {
604 struct btrfs_root *root = btrfs_sb(inode->i_sb);
605 struct btrfs_trans_handle *trans;
606 int ret;
607
608 mutex_lock(&root->fs_info->fs_mutex);
609 trans = btrfs_start_transaction(root, 1);
610 ret = btrfs_update_inode(trans, root, inode);
611 if (wait)
612 btrfs_commit_transaction(trans, root);
613 else
614 btrfs_end_transaction(trans, root);
615 mutex_unlock(&root->fs_info->fs_mutex);
616 check_inode(inode);
617 return ret;
618 }
619
620 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
621 struct inode *dir, int mode)
622 {
623 struct inode *inode;
624 struct btrfs_inode_item inode_item;
625 struct btrfs_root *root = btrfs_sb(dir->i_sb);
626 struct btrfs_key key;
627 int ret;
628 u64 objectid;
629
630 inode = new_inode(dir->i_sb);
631 if (!inode)
632 return ERR_PTR(-ENOMEM);
633
634 check_inode(inode);
635 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
636 BUG_ON(ret);
637
638 inode->i_uid = current->fsuid;
639 inode->i_gid = current->fsgid;
640 inode->i_mode = mode;
641 inode->i_ino = objectid;
642 inode->i_blocks = 0;
643 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
644 fill_inode_item(&inode_item, inode);
645
646 key.objectid = objectid;
647 key.flags = 0;
648 key.offset = 0;
649 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
650 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
651 BUG_ON(ret);
652
653 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
654 BUG_ON(ret);
655
656 insert_inode_hash(inode);
657 check_inode(inode);
658 check_inode(dir);
659 return inode;
660 }
661
662 static int btrfs_add_link(struct btrfs_trans_handle *trans,
663 struct dentry *dentry, struct inode *inode)
664 {
665 int ret;
666 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
667 dentry->d_name.name, dentry->d_name.len,
668 dentry->d_parent->d_inode->i_ino,
669 inode->i_ino, 0);
670 if (ret == 0) {
671 dentry->d_parent->d_inode->i_size += dentry->d_name.len;
672 ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
673 dentry->d_parent->d_inode);
674 }
675 check_inode(inode);
676 check_inode(dentry->d_parent->d_inode);
677 return ret;
678 }
679
680 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
681 struct dentry *dentry, struct inode *inode)
682 {
683 int err = btrfs_add_link(trans, dentry, inode);
684 if (!err) {
685 d_instantiate(dentry, inode);
686 return 0;
687 }
688 if (err > 0)
689 err = -EEXIST;
690 check_inode(inode);
691 return err;
692 }
693
694 static int btrfs_create(struct inode *dir, struct dentry *dentry,
695 int mode, struct nameidata *nd)
696 {
697 struct btrfs_trans_handle *trans;
698 struct btrfs_root *root = btrfs_sb(dir->i_sb);
699 struct inode *inode;
700 int err;
701 int drop_inode = 0;
702
703 mutex_lock(&root->fs_info->fs_mutex);
704 trans = btrfs_start_transaction(root, 1);
705 inode = btrfs_new_inode(trans, dir, mode);
706 err = PTR_ERR(inode);
707 if (IS_ERR(inode))
708 goto out_unlock;
709 // FIXME mark the inode dirty
710 err = btrfs_add_nondir(trans, dentry, inode);
711 if (err)
712 drop_inode = 1;
713 else {
714 inode->i_mapping->a_ops = &btrfs_aops;
715 inode->i_fop = &btrfs_file_operations;
716 inode->i_op = &btrfs_file_inode_operations;
717 }
718 dir->i_sb->s_dirt = 1;
719 out_unlock:
720 btrfs_end_transaction(trans, root);
721 mutex_unlock(&root->fs_info->fs_mutex);
722 check_inode(inode);
723 check_inode(dir);
724
725 if (drop_inode) {
726 inode_dec_link_count(inode);
727 iput(inode);
728 }
729 return err;
730 }
731
732 static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
733 struct inode *inode, struct inode *dir)
734 {
735 struct btrfs_root *root = btrfs_sb(inode->i_sb);
736 int ret;
737 char buf[2];
738 buf[0] = '.';
739 buf[1] = '.';
740
741 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
742 inode->i_ino, 1);
743 if (ret)
744 goto error;
745 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
746 dir->i_ino, 1);
747 if (ret)
748 goto error;
749 inode->i_size = 3;
750 ret = btrfs_update_inode(trans, root, inode);
751 error:
752 return ret;
753 }
754
755 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
756 {
757 struct inode *inode;
758 struct btrfs_trans_handle *trans;
759 struct btrfs_root *root = btrfs_sb(dir->i_sb);
760 int err = 0;
761 int drop_on_err = 0;
762
763 mutex_lock(&root->fs_info->fs_mutex);
764 trans = btrfs_start_transaction(root, 1);
765 if (IS_ERR(trans)) {
766 err = PTR_ERR(trans);
767 goto out_unlock;
768 }
769 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
770 if (IS_ERR(inode)) {
771 err = PTR_ERR(inode);
772 goto out_fail;
773 }
774 drop_on_err = 1;
775 inode->i_op = &btrfs_dir_inode_operations;
776 inode->i_fop = &btrfs_dir_file_operations;
777
778 err = btrfs_make_empty_dir(trans, inode, dir);
779 if (err)
780 goto out_fail;
781 err = btrfs_add_link(trans, dentry, inode);
782 if (err)
783 goto out_fail;
784 d_instantiate(dentry, inode);
785 drop_on_err = 0;
786
787 out_fail:
788 btrfs_end_transaction(trans, root);
789 out_unlock:
790 mutex_unlock(&root->fs_info->fs_mutex);
791 if (drop_on_err)
792 iput(inode);
793 return err;
794 }
795
796 static int btrfs_sync_fs(struct super_block *sb, int wait)
797 {
798 struct btrfs_trans_handle *trans;
799 struct btrfs_root *root;
800 int ret;
801 root = btrfs_sb(sb);
802
803 sb->s_dirt = 0;
804 if (!wait) {
805 filemap_flush(root->fs_info->btree_inode->i_mapping);
806 return 0;
807 }
808 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
809 mutex_lock(&root->fs_info->fs_mutex);
810 trans = btrfs_start_transaction(root, 1);
811 ret = btrfs_commit_transaction(trans, root);
812 sb->s_dirt = 0;
813 BUG_ON(ret);
814 printk("btrfs sync_fs\n");
815 mutex_unlock(&root->fs_info->fs_mutex);
816 return 0;
817 }
818
819 static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
820 struct buffer_head *result, int create)
821 {
822 int ret;
823 int err = 0;
824 u64 blocknr;
825 u64 extent_start = 0;
826 u64 extent_end = 0;
827 u64 objectid = inode->i_ino;
828 struct btrfs_path *path;
829 struct btrfs_root *root = btrfs_sb(inode->i_sb);
830 struct btrfs_trans_handle *trans = NULL;
831 struct btrfs_file_extent_item *item;
832 struct btrfs_leaf *leaf;
833 struct btrfs_disk_key *found_key;
834
835 path = btrfs_alloc_path();
836 BUG_ON(!path);
837 btrfs_init_path(path);
838 if (create)
839 trans = btrfs_start_transaction(root, 1);
840
841
842 ret = btrfs_lookup_file_extent(trans, root, path,
843 inode->i_ino,
844 iblock << inode->i_blkbits, 0);
845 if (ret < 0) {
846 err = ret;
847 goto out;
848 }
849
850 if (ret != 0) {
851 if (path->slots[0] == 0) {
852 btrfs_release_path(root, path);
853 goto allocate;
854 }
855 path->slots[0]--;
856 }
857
858 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
859 struct btrfs_file_extent_item);
860 leaf = btrfs_buffer_leaf(path->nodes[0]);
861 blocknr = btrfs_file_extent_disk_blocknr(item);
862 blocknr += btrfs_file_extent_offset(item);
863
864 /* exact match found, use it */
865 if (ret == 0) {
866 err = 0;
867 map_bh(result, inode->i_sb, blocknr);
868 goto out;
869 }
870
871 /* are we inside the extent that was found? */
872 found_key = &leaf->items[path->slots[0]].key;
873 if (btrfs_disk_key_objectid(found_key) != objectid ||
874 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
875 extent_end = 0;
876 extent_start = 0;
877 btrfs_release_path(root, path);
878 goto allocate;
879 }
880
881 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
882 extent_start = extent_start >> inode->i_blkbits;
883 extent_start += btrfs_file_extent_offset(item);
884 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
885 if (iblock >= extent_start && iblock < extent_end) {
886 err = 0;
887 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
888 goto out;
889 }
890 allocate:
891 /* ok, create a new extent */
892 if (!create) {
893 err = 0;
894 goto out;
895 }
896 ret = btrfs_alloc_file_extent(trans, root, objectid,
897 iblock << inode->i_blkbits,
898 1, extent_end, &blocknr);
899 if (ret) {
900 err = ret;
901 goto out;
902 }
903 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
904 set_buffer_new(result);
905 map_bh(result, inode->i_sb, blocknr);
906
907 out:
908 btrfs_release_path(root, path);
909 btrfs_free_path(path);
910 if (trans)
911 btrfs_end_transaction(trans, root);
912 return err;
913 }
914
915 static int btrfs_get_block(struct inode *inode, sector_t iblock,
916 struct buffer_head *result, int create)
917 {
918 int err;
919 struct btrfs_root *root = btrfs_sb(inode->i_sb);
920 mutex_lock(&root->fs_info->fs_mutex);
921 err = btrfs_get_block_lock(inode, iblock, result, create);
922 mutex_unlock(&root->fs_info->fs_mutex);
923 return err;
924 }
925
926 static int btrfs_prepare_write(struct file *file, struct page *page,
927 unsigned from, unsigned to)
928 {
929 WARN_ON(1);
930 return nobh_prepare_write(page, from, to, btrfs_get_block);
931 }
932 static int btrfs_commit_write(struct file *file, struct page *page,
933 unsigned from, unsigned to)
934 {
935 WARN_ON(1);
936 return nobh_commit_write(file, page, from, to);
937 }
938
939 static void btrfs_write_super(struct super_block *sb)
940 {
941 btrfs_sync_fs(sb, 1);
942 }
943
944 static int btrfs_readpage(struct file *file, struct page *page)
945 {
946 return mpage_readpage(page, btrfs_get_block);
947 }
948
949 static int btrfs_readpages(struct file *file, struct address_space *mapping,
950 struct list_head *pages, unsigned nr_pages)
951 {
952 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
953 }
954
955 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
956 {
957 return nobh_writepage(page, btrfs_get_block, wbc);
958 }
959
960 static void btrfs_truncate(struct inode *inode)
961 {
962 struct btrfs_root *root = btrfs_sb(inode->i_sb);
963 int ret;
964 struct btrfs_trans_handle *trans;
965
966 if (!S_ISREG(inode->i_mode))
967 return;
968 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
969 return;
970
971 nobh_truncate_page(inode->i_mapping, inode->i_size);
972
973 /* FIXME, add redo link to tree so we don't leak on crash */
974 mutex_lock(&root->fs_info->fs_mutex);
975 trans = btrfs_start_transaction(root, 1);
976 ret = btrfs_truncate_in_trans(trans, root, inode);
977 BUG_ON(ret);
978 ret = btrfs_end_transaction(trans, root);
979 BUG_ON(ret);
980 mutex_unlock(&root->fs_info->fs_mutex);
981 mark_inode_dirty(inode);
982 }
983
984 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
985 struct page **prepared_pages,
986 const char __user * buf)
987 {
988 long page_fault = 0;
989 int i;
990 int offset = pos & (PAGE_CACHE_SIZE - 1);
991
992 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
993 size_t count = min_t(size_t,
994 PAGE_CACHE_SIZE - offset, write_bytes);
995 struct page *page = prepared_pages[i];
996 fault_in_pages_readable(buf, count);
997
998 /* Copy data from userspace to the current page */
999 kmap(page);
1000 page_fault = __copy_from_user(page_address(page) + offset,
1001 buf, count);
1002 /* Flush processor's dcache for this page */
1003 flush_dcache_page(page);
1004 kunmap(page);
1005 buf += count;
1006 write_bytes -= count;
1007
1008 if (page_fault)
1009 break;
1010 }
1011 return page_fault ? -EFAULT : 0;
1012 }
1013
1014 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1015 {
1016 size_t i;
1017 for (i = 0; i < num_pages; i++) {
1018 if (!pages[i])
1019 break;
1020 unlock_page(pages[i]);
1021 mark_page_accessed(pages[i]);
1022 page_cache_release(pages[i]);
1023 }
1024 }
1025 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1026 struct btrfs_root *root,
1027 struct file *file,
1028 struct page **pages,
1029 size_t num_pages,
1030 loff_t pos,
1031 size_t write_bytes)
1032 {
1033 int i;
1034 int offset;
1035 int err = 0;
1036 int ret;
1037 int this_write;
1038 struct inode *inode = file->f_path.dentry->d_inode;
1039
1040 for (i = 0; i < num_pages; i++) {
1041 offset = pos & (PAGE_CACHE_SIZE -1);
1042 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1043 /* FIXME, one block at a time */
1044
1045 mutex_lock(&root->fs_info->fs_mutex);
1046 trans = btrfs_start_transaction(root, 1);
1047 btrfs_csum_file_block(trans, root, inode->i_ino,
1048 pages[i]->index << PAGE_CACHE_SHIFT,
1049 kmap(pages[i]), PAGE_CACHE_SIZE);
1050 kunmap(pages[i]);
1051 SetPageChecked(pages[i]);
1052 ret = btrfs_end_transaction(trans, root);
1053 BUG_ON(ret);
1054 mutex_unlock(&root->fs_info->fs_mutex);
1055
1056 ret = nobh_commit_write(file, pages[i], offset,
1057 offset + this_write);
1058 pos += this_write;
1059 if (ret) {
1060 err = ret;
1061 goto failed;
1062 }
1063 WARN_ON(this_write > write_bytes);
1064 write_bytes -= this_write;
1065 }
1066 failed:
1067 return err;
1068 }
1069
1070 static int prepare_pages(struct btrfs_trans_handle *trans,
1071 struct btrfs_root *root,
1072 struct file *file,
1073 struct page **pages,
1074 size_t num_pages,
1075 loff_t pos,
1076 size_t write_bytes)
1077 {
1078 int i;
1079 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1080 struct inode *inode = file->f_path.dentry->d_inode;
1081 int offset;
1082 int err = 0;
1083 int ret;
1084 int this_write;
1085 loff_t isize = i_size_read(inode);
1086
1087 memset(pages, 0, num_pages * sizeof(struct page *));
1088
1089 for (i = 0; i < num_pages; i++) {
1090 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1091 if (!pages[i]) {
1092 err = -ENOMEM;
1093 goto failed_release;
1094 }
1095 offset = pos & (PAGE_CACHE_SIZE -1);
1096 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1097 ret = nobh_prepare_write(pages[i], offset,
1098 offset + this_write,
1099 btrfs_get_block);
1100 pos += this_write;
1101 if (ret) {
1102 err = ret;
1103 goto failed_truncate;
1104 }
1105 WARN_ON(this_write > write_bytes);
1106 write_bytes -= this_write;
1107 }
1108 return 0;
1109
1110 failed_release:
1111 btrfs_drop_pages(pages, num_pages);
1112 return err;
1113
1114 failed_truncate:
1115 btrfs_drop_pages(pages, num_pages);
1116 if (pos > isize)
1117 vmtruncate(inode, isize);
1118 return err;
1119 }
1120
1121 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1122 size_t count, loff_t *ppos)
1123 {
1124 loff_t pos;
1125 size_t num_written = 0;
1126 int err = 0;
1127 int ret = 0;
1128 struct inode *inode = file->f_path.dentry->d_inode;
1129 struct btrfs_root *root = btrfs_sb(inode->i_sb);
1130 struct page *pages[1];
1131
1132 if (file->f_flags & O_DIRECT)
1133 return -EINVAL;
1134 pos = *ppos;
1135
1136 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1137 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1138 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1139 if (err)
1140 goto out;
1141 if (count == 0)
1142 goto out;
1143 err = remove_suid(file->f_path.dentry);
1144 if (err)
1145 goto out;
1146 file_update_time(file);
1147 mutex_lock(&inode->i_mutex);
1148 while(count > 0) {
1149 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1150 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1151 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1152 PAGE_CACHE_SHIFT;
1153 ret = prepare_pages(NULL, root, file, pages, num_pages,
1154 pos, write_bytes);
1155 BUG_ON(ret);
1156 ret = btrfs_copy_from_user(pos, num_pages,
1157 write_bytes, pages, buf);
1158 BUG_ON(ret);
1159
1160 ret = dirty_and_release_pages(NULL, root, file, pages,
1161 num_pages, pos, write_bytes);
1162 BUG_ON(ret);
1163 btrfs_drop_pages(pages, num_pages);
1164
1165 buf += write_bytes;
1166 count -= write_bytes;
1167 pos += write_bytes;
1168 num_written += write_bytes;
1169
1170 balance_dirty_pages_ratelimited(inode->i_mapping);
1171 cond_resched();
1172 }
1173 mutex_unlock(&inode->i_mutex);
1174 out:
1175 *ppos = pos;
1176 current->backing_dev_info = NULL;
1177 return num_written ? num_written : err;
1178 }
1179
1180 static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1181 unsigned long offset, unsigned long size)
1182 {
1183 char *kaddr;
1184 unsigned long left, count = desc->count;
1185
1186 if (size > count)
1187 size = count;
1188
1189 if (!PageChecked(page)) {
1190 /* FIXME, do it per block */
1191 struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
1192 int ret = btrfs_csum_verify_file_block(root,
1193 page->mapping->host->i_ino,
1194 page->index << PAGE_CACHE_SHIFT,
1195 kmap(page), PAGE_CACHE_SIZE);
1196 if (ret) {
1197 printk("failed to verify ino %lu page %lu\n",
1198 page->mapping->host->i_ino,
1199 page->index);
1200 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1201 }
1202 SetPageChecked(page);
1203 kunmap(page);
1204 }
1205 /*
1206 * Faults on the destination of a read are common, so do it before
1207 * taking the kmap.
1208 */
1209 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1210 kaddr = kmap_atomic(page, KM_USER0);
1211 left = __copy_to_user_inatomic(desc->arg.buf,
1212 kaddr + offset, size);
1213 kunmap_atomic(kaddr, KM_USER0);
1214 if (left == 0)
1215 goto success;
1216 }
1217
1218 /* Do it the slow way */
1219 kaddr = kmap(page);
1220 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1221 kunmap(page);
1222
1223 if (left) {
1224 size -= left;
1225 desc->error = -EFAULT;
1226 }
1227 success:
1228 desc->count = count - size;
1229 desc->written += size;
1230 desc->arg.buf += size;
1231 return size;
1232 }
1233
1234 /**
1235 * btrfs_file_aio_read - filesystem read routine
1236 * @iocb: kernel I/O control block
1237 * @iov: io vector request
1238 * @nr_segs: number of segments in the iovec
1239 * @pos: current file position
1240 */
1241 static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1242 unsigned long nr_segs, loff_t pos)
1243 {
1244 struct file *filp = iocb->ki_filp;
1245 ssize_t retval;
1246 unsigned long seg;
1247 size_t count;
1248 loff_t *ppos = &iocb->ki_pos;
1249
1250 count = 0;
1251 for (seg = 0; seg < nr_segs; seg++) {
1252 const struct iovec *iv = &iov[seg];
1253
1254 /*
1255 * If any segment has a negative length, or the cumulative
1256 * length ever wraps negative then return -EINVAL.
1257 */
1258 count += iv->iov_len;
1259 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1260 return -EINVAL;
1261 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1262 continue;
1263 if (seg == 0)
1264 return -EFAULT;
1265 nr_segs = seg;
1266 count -= iv->iov_len; /* This segment is no good */
1267 break;
1268 }
1269 retval = 0;
1270 if (count) {
1271 for (seg = 0; seg < nr_segs; seg++) {
1272 read_descriptor_t desc;
1273
1274 desc.written = 0;
1275 desc.arg.buf = iov[seg].iov_base;
1276 desc.count = iov[seg].iov_len;
1277 if (desc.count == 0)
1278 continue;
1279 desc.error = 0;
1280 do_generic_file_read(filp, ppos, &desc,
1281 btrfs_read_actor);
1282 retval += desc.written;
1283 if (desc.error) {
1284 retval = retval ?: desc.error;
1285 break;
1286 }
1287 }
1288 }
1289 return retval;
1290 }
1291
1292 static struct kmem_cache *btrfs_inode_cachep;
1293 struct kmem_cache *btrfs_trans_handle_cachep;
1294 struct kmem_cache *btrfs_transaction_cachep;
1295 struct kmem_cache *btrfs_bit_radix_cachep;
1296 struct kmem_cache *btrfs_path_cachep;
1297
1298 /*
1299 * Called inside transaction, so use GFP_NOFS
1300 */
1301 static struct inode *btrfs_alloc_inode(struct super_block *sb)
1302 {
1303 struct btrfs_inode *ei;
1304
1305 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1306 if (!ei)
1307 return NULL;
1308 ei->magic = 0xDEADBEEF;
1309 ei->magic2 = 0xDEADBEAF;
1310 return &ei->vfs_inode;
1311 }
1312
1313 static void btrfs_destroy_inode(struct inode *inode)
1314 {
1315 struct btrfs_inode *ei = BTRFS_I(inode);
1316 WARN_ON(ei->magic != 0xDEADBEEF);
1317 WARN_ON(ei->magic2 != 0xDEADBEAF);
1318 WARN_ON(!list_empty(&inode->i_dentry));
1319 WARN_ON(inode->i_ino == 1);
1320 WARN_ON(inode->i_data.nrpages);
1321
1322 ei->magic = 0;
1323 ei->magic2 = 0;
1324 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1325 }
1326
1327 static void init_once(void * foo, struct kmem_cache * cachep,
1328 unsigned long flags)
1329 {
1330 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1331
1332 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1333 SLAB_CTOR_CONSTRUCTOR) {
1334 inode_init_once(&ei->vfs_inode);
1335 }
1336 }
1337
1338 static int init_inodecache(void)
1339 {
1340 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1341 sizeof(struct btrfs_inode),
1342 0, (SLAB_RECLAIM_ACCOUNT|
1343 SLAB_MEM_SPREAD),
1344 init_once, NULL);
1345 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1346 sizeof(struct btrfs_trans_handle),
1347 0, (SLAB_RECLAIM_ACCOUNT|
1348 SLAB_MEM_SPREAD),
1349 NULL, NULL);
1350 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1351 sizeof(struct btrfs_transaction),
1352 0, (SLAB_RECLAIM_ACCOUNT|
1353 SLAB_MEM_SPREAD),
1354 NULL, NULL);
1355 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1356 sizeof(struct btrfs_transaction),
1357 0, (SLAB_RECLAIM_ACCOUNT|
1358 SLAB_MEM_SPREAD),
1359 NULL, NULL);
1360 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1361 256,
1362 0, (SLAB_RECLAIM_ACCOUNT|
1363 SLAB_MEM_SPREAD |
1364 SLAB_DESTROY_BY_RCU),
1365 NULL, NULL);
1366 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1367 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1368 return -ENOMEM;
1369 return 0;
1370 }
1371
1372 static void destroy_inodecache(void)
1373 {
1374 kmem_cache_destroy(btrfs_inode_cachep);
1375 kmem_cache_destroy(btrfs_trans_handle_cachep);
1376 kmem_cache_destroy(btrfs_transaction_cachep);
1377 kmem_cache_destroy(btrfs_bit_radix_cachep);
1378 kmem_cache_destroy(btrfs_path_cachep);
1379 }
1380
1381 static int btrfs_get_sb(struct file_system_type *fs_type,
1382 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1383 {
1384 return get_sb_bdev(fs_type, flags, dev_name, data,
1385 btrfs_fill_super, mnt);
1386 }
1387
1388 static struct file_system_type btrfs_fs_type = {
1389 .owner = THIS_MODULE,
1390 .name = "btrfs",
1391 .get_sb = btrfs_get_sb,
1392 .kill_sb = kill_block_super,
1393 .fs_flags = FS_REQUIRES_DEV,
1394 };
1395
1396 static struct super_operations btrfs_super_ops = {
1397 .statfs = simple_statfs,
1398 .delete_inode = btrfs_delete_inode,
1399 .put_super = btrfs_put_super,
1400 .read_inode = btrfs_read_locked_inode,
1401 .write_super = btrfs_write_super,
1402 .sync_fs = btrfs_sync_fs,
1403 .write_inode = btrfs_write_inode,
1404 .alloc_inode = btrfs_alloc_inode,
1405 .destroy_inode = btrfs_destroy_inode,
1406 };
1407
1408 static struct inode_operations btrfs_dir_inode_operations = {
1409 .lookup = btrfs_lookup,
1410 .create = btrfs_create,
1411 .unlink = btrfs_unlink,
1412 .mkdir = btrfs_mkdir,
1413 .rmdir = btrfs_rmdir,
1414 };
1415
1416 static struct file_operations btrfs_dir_file_operations = {
1417 .llseek = generic_file_llseek,
1418 .read = generic_read_dir,
1419 .readdir = btrfs_readdir,
1420 };
1421
1422 static struct address_space_operations btrfs_aops = {
1423 .readpage = btrfs_readpage,
1424 .readpages = btrfs_readpages,
1425 .writepage = btrfs_writepage,
1426 .sync_page = block_sync_page,
1427 .prepare_write = btrfs_prepare_write,
1428 .commit_write = btrfs_commit_write,
1429 };
1430
1431 static struct inode_operations btrfs_file_inode_operations = {
1432 .truncate = btrfs_truncate,
1433 };
1434
1435 static struct file_operations btrfs_file_operations = {
1436 .llseek = generic_file_llseek,
1437 .read = do_sync_read,
1438 .aio_read = btrfs_file_aio_read,
1439 .write = btrfs_file_write,
1440 .mmap = generic_file_mmap,
1441 .open = generic_file_open,
1442 };
1443
1444 static int __init init_btrfs_fs(void)
1445 {
1446 int err;
1447 printk("btrfs loaded!\n");
1448 err = init_inodecache();
1449 if (err)
1450 return err;
1451 return register_filesystem(&btrfs_fs_type);
1452 }
1453
1454 static void __exit exit_btrfs_fs(void)
1455 {
1456 destroy_inodecache();
1457 unregister_filesystem(&btrfs_fs_type);
1458 printk("btrfs unloaded\n");
1459 }
1460
1461 module_init(init_btrfs_fs)
1462 module_exit(exit_btrfs_fs)
1463
1464 MODULE_LICENSE("GPL");