1 /* Copyright 2005 by Hans Reiser, licensing governed by
5 #include "page_cache.h"
10 #include <linux/vfs.h>
11 #include <linux/writeback.h>
12 #include <linux/mount.h>
13 #include <linux/seq_file.h>
14 #include <linux/debugfs.h>
16 /* slab cache for inodes */
17 static struct kmem_cache
*inode_cache
;
19 static struct dentry
*reiser4_debugfs_root
= NULL
;
22 * init_once - constructor for reiser4 inodes
23 * @cache: cache @obj belongs to
24 * @obj: inode to be initialized
26 * Initialization function to be called when new page is allocated by reiser4
27 * inode cache. It is set on inode cache creation.
29 static void init_once(void *obj
)
31 struct reiser4_inode_object
*info
;
35 /* initialize vfs inode */
36 inode_init_once(&info
->vfs_inode
);
39 * initialize reiser4 specific part fo inode.
40 * NOTE-NIKITA add here initializations for locks, list heads,
41 * etc. that will be added to our private inode part.
43 INIT_LIST_HEAD(get_readdir_list(&info
->vfs_inode
));
44 init_rwsem(&info
->p
.conv_sem
);
45 /* init semaphore which is used during inode loading */
46 loading_init_once(&info
->p
);
47 INIT_RADIX_TREE(jnode_tree_by_reiser4_inode(&info
->p
),
50 info
->p
.nr_jnodes
= 0;
55 * init_inodes - create znode cache
57 * Initializes slab cache of inodes. It is part of reiser4 module initialization
59 static int init_inodes(void)
61 inode_cache
= kmem_cache_create("reiser4_inode",
62 sizeof(struct reiser4_inode_object
),
65 SLAB_RECLAIM_ACCOUNT
, init_once
);
66 if (inode_cache
== NULL
)
67 return RETERR(-ENOMEM
);
72 * done_inodes - delete inode cache
74 * This is called on reiser4 module unloading or system shutdown.
76 static void done_inodes(void)
78 destroy_reiser4_cache(&inode_cache
);
82 * reiser4_alloc_inode - alloc_inode of super operations
83 * @super: super block new inode is allocated for
85 * Allocates new inode, initializes reiser4 specific part of it.
87 static struct inode
*reiser4_alloc_inode(struct super_block
*super
)
89 struct reiser4_inode_object
*obj
;
91 assert("nikita-1696", super
!= NULL
);
92 obj
= kmem_cache_alloc(inode_cache
, reiser4_ctx_gfp_mask_get());
98 info
->pset
= plugin_set_get_empty();
99 info
->hset
= plugin_set_get_empty();
101 info
->locality_id
= 0ull;
102 info
->plugin_mask
= 0;
104 #if !REISER4_INO_IS_OID
107 reiser4_seal_init(&info
->sd_seal
, NULL
, NULL
);
108 coord_init_invalid(&info
->sd_coord
, NULL
);
110 spin_lock_init(&info
->guard
);
111 /* this deals with info's loading semaphore */
113 info
->vroot
= UBER_TREE_ADDR
;
114 return &obj
->vfs_inode
;
120 * reiser4_destroy_inode - destroy_inode of super operations
121 * @inode: inode being destroyed
123 * Puts reiser4 specific portion of inode, frees memory occupied by inode.
125 static void reiser4_destroy_inode(struct inode
*inode
)
129 info
= reiser4_inode_data(inode
);
131 assert("vs-1220", inode_has_no_jnodes(info
));
133 if (!is_bad_inode(inode
) && is_inode_loaded(inode
)) {
134 file_plugin
*fplug
= inode_file_plugin(inode
);
135 if (fplug
->destroy_inode
!= NULL
)
136 fplug
->destroy_inode(inode
);
138 reiser4_dispose_cursors(inode
);
140 plugin_set_put(info
->pset
);
142 plugin_set_put(info
->hset
);
145 * cannot add similar assertion about ->i_list as prune_icache return
146 * inode into slab with dangling ->list.{next,prev}. This is safe,
147 * because they are re-initialized in the new_inode().
149 assert("nikita-2895", list_empty(&inode
->i_dentry
));
150 assert("nikita-2896", hlist_unhashed(&inode
->i_hash
));
151 assert("nikita-2898", list_empty_careful(get_readdir_list(inode
)));
153 /* this deals with info's loading semaphore */
154 loading_destroy(info
);
156 kmem_cache_free(inode_cache
,
157 container_of(info
, struct reiser4_inode_object
, p
));
161 * reiser4_dirty_inode - dirty_inode of super operations
162 * @inode: inode being dirtied
166 static void reiser4_dirty_inode(struct inode
*inode
)
170 if (!is_in_reiser4_context())
172 assert("", !IS_RDONLY(inode
));
173 assert("", (inode_file_plugin(inode
)->estimate
.update(inode
) <=
174 get_current_context()->grabbed_blocks
));
176 result
= reiser4_update_sd(inode
);
178 warning("", "failed to dirty inode for %llu: %d",
179 get_inode_oid(inode
), result
);
183 * reiser4_delete_inode - delete_inode of super operations
184 * @inode: inode to delete
186 * Calls file plugin's delete_object method to delete object items from
187 * filesystem tree and calls clear_inode.
189 static void reiser4_delete_inode(struct inode
*inode
)
191 reiser4_context
*ctx
;
194 ctx
= reiser4_init_context(inode
->i_sb
);
196 warning("vs-15", "failed to init context");
200 if (is_inode_loaded(inode
)) {
201 fplug
= inode_file_plugin(inode
);
202 if (fplug
!= NULL
&& fplug
->delete_object
!= NULL
)
203 fplug
->delete_object(inode
);
206 truncate_inode_pages(&inode
->i_data
, 0);
209 reiser4_exit_context(ctx
);
213 * reiser4_put_super - put_super of super operations
214 * @super: super block to free
216 * Stops daemons, release resources, umounts in short.
218 static void reiser4_put_super(struct super_block
*super
)
220 reiser4_super_info_data
*sbinfo
;
221 reiser4_context
*ctx
;
223 sbinfo
= get_super_private(super
);
224 assert("vs-1699", sbinfo
);
226 debugfs_remove(sbinfo
->tmgr
.debugfs_atom_count
);
227 debugfs_remove(sbinfo
->tmgr
.debugfs_id_count
);
228 debugfs_remove(sbinfo
->debugfs_root
);
230 ctx
= reiser4_init_context(super
);
232 warning("vs-17", "failed to init context");
236 /* have disk format plugin to free its resources */
237 if (get_super_private(super
)->df_plug
->release
)
238 get_super_private(super
)->df_plug
->release(super
);
240 reiser4_done_formatted_fake(super
);
242 /* stop daemons: ktxnmgr and entd */
243 reiser4_done_entd(super
);
244 reiser4_done_ktxnmgrd(super
);
245 reiser4_done_txnmgr(&sbinfo
->tmgr
);
247 reiser4_done_fs_info(super
);
248 reiser4_exit_context(ctx
);
252 * reiser4_write_super - write_super of super operations
253 * @super: super block to write
255 * Captures znode associated with super block, comit all transactions.
257 static void reiser4_write_super(struct super_block
*super
)
260 reiser4_context
*ctx
;
262 assert("vs-1700", !rofs_super(super
));
264 ctx
= reiser4_init_context(super
);
266 warning("vs-16", "failed to init context");
270 ret
= reiser4_capture_super_block(super
);
273 "reiser4_capture_super_block failed in write_super: %d",
275 ret
= txnmgr_force_commit_all(super
, 0);
277 warning("jmacd-77113",
278 "txn_force failed in write_super: %d", ret
);
282 reiser4_exit_context(ctx
);
286 * reiser4_statfs - statfs of super operations
287 * @super: super block of file system in queried
288 * @stafs: buffer to fill with statistics
290 * Returns information about filesystem.
292 static int reiser4_statfs(struct dentry
*dentry
, struct kstatfs
*statfs
)
299 reiser4_context
*ctx
;
300 struct super_block
*super
= dentry
->d_sb
;
302 assert("nikita-408", super
!= NULL
);
303 assert("nikita-409", statfs
!= NULL
);
305 ctx
= reiser4_init_context(super
);
309 statfs
->f_type
= reiser4_statfs_type(super
);
310 statfs
->f_bsize
= super
->s_blocksize
;
313 * 5% of total block space is reserved. This is needed for flush and
314 * for truncates (so that we are able to perform truncate/unlink even
315 * on the otherwise completely full file system). If this reservation
316 * is hidden from statfs(2), users will mistakenly guess that they
317 * have enough free space to complete some operation, which is
320 * Another possible solution is to subtract ->blocks_reserved from
321 * ->f_bfree, but changing available space seems less intrusive than
322 * letting user to see 5% of disk space to be used directly after
325 total
= reiser4_block_count(super
);
326 reserved
= get_super_private(super
)->blocks_reserved
;
327 deleted
= txnmgr_count_deleted_blocks();
328 free
= reiser4_free_blocks(super
) + deleted
;
329 forroot
= reiser4_reserved_blocks(super
, 0, 0);
332 * These counters may be in inconsistent state because we take the
333 * values without keeping any global spinlock. Here we do a sanity
334 * check that free block counter does not exceed the number of all
339 statfs
->f_blocks
= total
- reserved
;
340 /* make sure statfs->f_bfree is never larger than statfs->f_blocks */
345 statfs
->f_bfree
= free
;
351 statfs
->f_bavail
= free
;
356 /* maximal acceptable name length depends on directory plugin. */
357 assert("nikita-3351", super
->s_root
->d_inode
!= NULL
);
358 statfs
->f_namelen
= reiser4_max_filename_len(super
->s_root
->d_inode
);
359 reiser4_exit_context(ctx
);
364 * reiser4_clear_inode - clear_inode of super operation
365 * @inode: inode about to destroy
367 * Does sanity checks: being destroyed should have all jnodes detached.
369 static void reiser4_clear_inode(struct inode
*inode
)
372 reiser4_inode
*r4_inode
;
374 r4_inode
= reiser4_inode_data(inode
);
375 if (!inode_has_no_jnodes(r4_inode
))
376 warning("vs-1732", "reiser4 inode has %ld jnodes\n",
377 r4_inode
->nr_jnodes
);
382 * reiser4_sync_inodes - sync_inodes of super operations
386 * This method is called by background and non-backgound writeback. Reiser4's
387 * implementation uses generic_sync_sb_inodes to call reiser4_writepages for
388 * each of dirty inodes. Reiser4_writepages handles pages dirtied via shared
389 * mapping - dirty pages get into atoms. Writeout is called to flush some
392 static void reiser4_sync_inodes(struct super_block
*super
,
393 struct writeback_control
*wbc
)
395 reiser4_context
*ctx
;
398 if (wbc
->for_kupdate
)
399 /* reiser4 has its own means of periodical write-out */
402 to_write
= wbc
->nr_to_write
;
403 assert("vs-49", wbc
->older_than_this
== NULL
);
405 ctx
= reiser4_init_context(super
);
407 warning("vs-13", "failed to init context");
412 * call reiser4_writepages for each of dirty inodes to turn dirty pages
413 * into transactions if they were not yet.
415 generic_sync_sb_inodes(wbc
);
417 /* flush goes here */
418 wbc
->nr_to_write
= to_write
;
419 reiser4_writeout(super
, wbc
);
421 /* avoid recursive calls to ->sync_inodes */
422 context_set_commit_async(ctx
);
423 reiser4_exit_context(ctx
);
427 * reiser4_show_options - show_options of super operations
428 * @m: file where to write information
429 * @mnt: mount structure
431 * Makes reiser4 mount options visible in /proc/mounts.
433 static int reiser4_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
435 struct super_block
*super
;
436 reiser4_super_info_data
*sbinfo
;
439 sbinfo
= get_super_private(super
);
441 seq_printf(m
, ",atom_max_size=0x%x", sbinfo
->tmgr
.atom_max_size
);
442 seq_printf(m
, ",atom_max_age=0x%x", sbinfo
->tmgr
.atom_max_age
);
443 seq_printf(m
, ",atom_min_size=0x%x", sbinfo
->tmgr
.atom_min_size
);
444 seq_printf(m
, ",atom_max_flushers=0x%x",
445 sbinfo
->tmgr
.atom_max_flushers
);
446 seq_printf(m
, ",cbk_cache_slots=0x%x",
447 sbinfo
->tree
.cbk_cache
.nr_slots
);
452 struct super_operations reiser4_super_operations
= {
453 .alloc_inode
= reiser4_alloc_inode
,
454 .destroy_inode
= reiser4_destroy_inode
,
455 .dirty_inode
= reiser4_dirty_inode
,
456 .delete_inode
= reiser4_delete_inode
,
457 .put_super
= reiser4_put_super
,
458 .write_super
= reiser4_write_super
,
459 .statfs
= reiser4_statfs
,
460 .clear_inode
= reiser4_clear_inode
,
461 .sync_inodes
= reiser4_sync_inodes
,
462 .show_options
= reiser4_show_options
466 * fill_super - initialize super block on mount
467 * @super: super block to fill
468 * @data: reiser4 specific mount option
471 * This is to be called by reiser4_get_sb. Mounts filesystem.
473 static int fill_super(struct super_block
*super
, void *data
, int silent
)
477 reiser4_super_info_data
*sbinfo
;
479 assert("zam-989", super
!= NULL
);
482 init_stack_context(&ctx
, super
);
484 /* allocate reiser4 specific super block */
485 if ((result
= reiser4_init_fs_info(super
)) != 0)
486 goto failed_init_sinfo
;
488 sbinfo
= get_super_private(super
);
489 /* initialize various reiser4 parameters, parse mount options */
490 if ((result
= reiser4_init_super_data(super
, data
)) != 0)
491 goto failed_init_super_data
;
493 /* read reiser4 master super block, initialize disk format plugin */
494 if ((result
= reiser4_init_read_super(super
, silent
)) != 0)
495 goto failed_init_read_super
;
497 /* initialize transaction manager */
498 reiser4_init_txnmgr(&sbinfo
->tmgr
);
500 /* initialize ktxnmgrd context and start kernel thread ktxnmrgd */
501 if ((result
= reiser4_init_ktxnmgrd(super
)) != 0)
502 goto failed_init_ktxnmgrd
;
504 /* initialize entd context and start kernel thread entd */
505 if ((result
= reiser4_init_entd(super
)) != 0)
506 goto failed_init_entd
;
508 /* initialize address spaces for formatted nodes and bitmaps */
509 if ((result
= reiser4_init_formatted_fake(super
)) != 0)
510 goto failed_init_formatted_fake
;
512 /* initialize disk format plugin */
513 if ((result
= get_super_private(super
)->df_plug
->init_format(super
,
515 goto failed_init_disk_format
;
518 * There are some 'committed' versions of reiser4 super block counters,
519 * which correspond to reiser4 on-disk state. These counters are
522 sbinfo
->blocks_free_committed
= sbinfo
->blocks_free
;
523 sbinfo
->nr_files_committed
= oids_used(super
);
525 /* get inode of root directory */
526 if ((result
= reiser4_init_root_inode(super
)) != 0)
527 goto failed_init_root_inode
;
529 if ((result
= get_super_private(super
)->df_plug
->version_update(super
)) != 0)
530 goto failed_update_format_version
;
532 process_safelinks(super
);
533 reiser4_exit_context(&ctx
);
535 sbinfo
->debugfs_root
= debugfs_create_dir(super
->s_id
,
536 reiser4_debugfs_root
);
537 if (sbinfo
->debugfs_root
) {
538 sbinfo
->tmgr
.debugfs_atom_count
=
539 debugfs_create_u32("atom_count", S_IFREG
|S_IRUSR
,
540 sbinfo
->debugfs_root
,
541 &sbinfo
->tmgr
.atom_count
);
542 sbinfo
->tmgr
.debugfs_id_count
=
543 debugfs_create_u32("id_count", S_IFREG
|S_IRUSR
,
544 sbinfo
->debugfs_root
,
545 &sbinfo
->tmgr
.id_count
);
549 failed_update_format_version
:
550 failed_init_root_inode
:
551 if (sbinfo
->df_plug
->release
)
552 sbinfo
->df_plug
->release(super
);
553 failed_init_disk_format
:
554 reiser4_done_formatted_fake(super
);
555 failed_init_formatted_fake
:
556 reiser4_done_entd(super
);
558 reiser4_done_ktxnmgrd(super
);
559 failed_init_ktxnmgrd
:
560 reiser4_done_txnmgr(&sbinfo
->tmgr
);
561 failed_init_read_super
:
562 failed_init_super_data
:
563 reiser4_done_fs_info(super
);
565 reiser4_exit_context(&ctx
);
570 * reiser4_get_sb - get_sb of file_system_type operations
572 * @flags: mount flags MS_RDONLY, MS_VERBOSE, etc
573 * @dev_name: block device file name
574 * @data: specific mount options
576 * Reiser4 mount entry.
578 static int reiser4_get_sb(struct file_system_type
*fs_type
, int flags
,
579 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
581 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, fill_super
, mnt
);
584 /* structure describing the reiser4 filesystem implementation */
585 static struct file_system_type reiser4_fs_type
= {
586 .owner
= THIS_MODULE
,
588 .fs_flags
= FS_REQUIRES_DEV
,
589 .get_sb
= reiser4_get_sb
,
590 .kill_sb
= kill_block_super
,
594 void destroy_reiser4_cache(struct kmem_cache
**cachep
)
596 BUG_ON(*cachep
== NULL
);
597 kmem_cache_destroy(*cachep
);
602 * init_reiser4 - reiser4 initialization entry point
604 * Initializes reiser4 slabs, registers reiser4 filesystem type. It is called
605 * on kernel initialization or during reiser4 module load.
607 static int __init
init_reiser4(void)
613 "See www.namesys.com for a description of Reiser4.\n");
615 /* initialize slab cache of inodes */
616 if ((result
= init_inodes()) != 0)
617 goto failed_inode_cache
;
619 /* initialize cache of znodes */
620 if ((result
= init_znodes()) != 0)
621 goto failed_init_znodes
;
623 /* initialize all plugins */
624 if ((result
= init_plugins()) != 0)
625 goto failed_init_plugins
;
627 /* initialize cache of plugin_set-s and plugin_set's hash table */
628 if ((result
= init_plugin_set()) != 0)
629 goto failed_init_plugin_set
;
631 /* initialize caches of txn_atom-s and txn_handle-s */
632 if ((result
= init_txnmgr_static()) != 0)
633 goto failed_init_txnmgr_static
;
635 /* initialize cache of jnodes */
636 if ((result
= init_jnodes()) != 0)
637 goto failed_init_jnodes
;
639 /* initialize cache of flush queues */
640 if ((result
= reiser4_init_fqs()) != 0)
641 goto failed_init_fqs
;
643 /* initialize cache of structures attached to dentry->d_fsdata */
644 if ((result
= reiser4_init_dentry_fsdata()) != 0)
645 goto failed_init_dentry_fsdata
;
647 /* initialize cache of structures attached to file->private_data */
648 if ((result
= reiser4_init_file_fsdata()) != 0)
649 goto failed_init_file_fsdata
;
652 * initialize cache of d_cursors. See plugin/file_ops_readdir.c for
655 if ((result
= reiser4_init_d_cursor()) != 0)
656 goto failed_init_d_cursor
;
658 if ((result
= register_filesystem(&reiser4_fs_type
)) == 0) {
659 reiser4_debugfs_root
= debugfs_create_dir("reiser4", NULL
);
663 reiser4_done_d_cursor();
664 failed_init_d_cursor
:
665 reiser4_done_file_fsdata();
666 failed_init_file_fsdata
:
667 reiser4_done_dentry_fsdata();
668 failed_init_dentry_fsdata
:
673 done_txnmgr_static();
674 failed_init_txnmgr_static
:
676 failed_init_plugin_set
:
686 * done_reiser4 - reiser4 exit entry point
688 * Unregister reiser4 filesystem type, deletes caches. It is called on shutdown
689 * or at module unload.
691 static void __exit
done_reiser4(void)
695 debugfs_remove(reiser4_debugfs_root
);
696 result
= unregister_filesystem(&reiser4_fs_type
);
698 reiser4_done_d_cursor();
699 reiser4_done_file_fsdata();
700 reiser4_done_dentry_fsdata();
703 done_txnmgr_static();
706 destroy_reiser4_cache(&inode_cache
);
709 module_init(init_reiser4
);
710 module_exit(done_reiser4
);
712 MODULE_DESCRIPTION("Reiser4 filesystem");
713 MODULE_AUTHOR("Hans Reiser <Reiser@Namesys.COM>");
715 MODULE_LICENSE("GPL");
719 * c-indentation-style: "K&R"