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