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(struct kmem_cache
*cache
, 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 int reiser4_sync_inodes(struct super_block
*super
,
393 struct writeback_control
*wbc
)
395 reiser4_context
*ctx
;
399 if (wbc
->for_kupdate
)
400 /* reiser4 has its own means of periodical write-out */
403 to_write
= wbc
->nr_to_write
;
404 assert("vs-49", wbc
->older_than_this
== NULL
);
406 ctx
= reiser4_init_context(super
);
408 warning("vs-13", "failed to init context");
413 * call reiser4_writepages for each of dirty inodes to turn dirty pages
414 * into transactions if they were not yet.
416 ret
= generic_sync_sb_inodes(super
, wbc
);
418 /* flush goes here */
419 wbc
->nr_to_write
= to_write
;
420 reiser4_writeout(super
, wbc
);
422 /* avoid recursive calls to ->sync_inodes */
423 context_set_commit_async(ctx
);
424 reiser4_exit_context(ctx
);
429 * reiser4_show_options - show_options of super operations
430 * @m: file where to write information
431 * @mnt: mount structure
433 * Makes reiser4 mount options visible in /proc/mounts.
435 static int reiser4_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
437 struct super_block
*super
;
438 reiser4_super_info_data
*sbinfo
;
441 sbinfo
= get_super_private(super
);
443 seq_printf(m
, ",atom_max_size=0x%x", sbinfo
->tmgr
.atom_max_size
);
444 seq_printf(m
, ",atom_max_age=0x%x", sbinfo
->tmgr
.atom_max_age
);
445 seq_printf(m
, ",atom_min_size=0x%x", sbinfo
->tmgr
.atom_min_size
);
446 seq_printf(m
, ",atom_max_flushers=0x%x",
447 sbinfo
->tmgr
.atom_max_flushers
);
448 seq_printf(m
, ",cbk_cache_slots=0x%x",
449 sbinfo
->tree
.cbk_cache
.nr_slots
);
454 struct super_operations reiser4_super_operations
= {
455 .alloc_inode
= reiser4_alloc_inode
,
456 .destroy_inode
= reiser4_destroy_inode
,
457 .dirty_inode
= reiser4_dirty_inode
,
458 .delete_inode
= reiser4_delete_inode
,
459 .put_super
= reiser4_put_super
,
460 .write_super
= reiser4_write_super
,
461 .statfs
= reiser4_statfs
,
462 .clear_inode
= reiser4_clear_inode
,
463 .sync_inodes
= reiser4_sync_inodes
,
464 .show_options
= reiser4_show_options
468 * fill_super - initialize super block on mount
469 * @super: super block to fill
470 * @data: reiser4 specific mount option
473 * This is to be called by reiser4_get_sb. Mounts filesystem.
475 static int fill_super(struct super_block
*super
, void *data
, int silent
)
479 reiser4_super_info_data
*sbinfo
;
481 assert("zam-989", super
!= NULL
);
484 init_stack_context(&ctx
, super
);
486 /* allocate reiser4 specific super block */
487 if ((result
= reiser4_init_fs_info(super
)) != 0)
488 goto failed_init_sinfo
;
490 sbinfo
= get_super_private(super
);
491 /* initialize various reiser4 parameters, parse mount options */
492 if ((result
= reiser4_init_super_data(super
, data
)) != 0)
493 goto failed_init_super_data
;
495 /* read reiser4 master super block, initialize disk format plugin */
496 if ((result
= reiser4_init_read_super(super
, silent
)) != 0)
497 goto failed_init_read_super
;
499 /* initialize transaction manager */
500 reiser4_init_txnmgr(&sbinfo
->tmgr
);
502 /* initialize ktxnmgrd context and start kernel thread ktxnmrgd */
503 if ((result
= reiser4_init_ktxnmgrd(super
)) != 0)
504 goto failed_init_ktxnmgrd
;
506 /* initialize entd context and start kernel thread entd */
507 if ((result
= reiser4_init_entd(super
)) != 0)
508 goto failed_init_entd
;
510 /* initialize address spaces for formatted nodes and bitmaps */
511 if ((result
= reiser4_init_formatted_fake(super
)) != 0)
512 goto failed_init_formatted_fake
;
514 /* initialize disk format plugin */
515 if ((result
= get_super_private(super
)->df_plug
->init_format(super
, data
)) != 0 )
516 goto failed_init_disk_format
;
519 * There are some 'committed' versions of reiser4 super block counters,
520 * which correspond to reiser4 on-disk state. These counters are
523 sbinfo
->blocks_free_committed
= sbinfo
->blocks_free
;
524 sbinfo
->nr_files_committed
= oids_used(super
);
526 /* get inode of root directory */
527 if ((result
= reiser4_init_root_inode(super
)) != 0)
528 goto failed_init_root_inode
;
530 if ((result
= get_super_private(super
)->df_plug
->version_update(super
)) != 0 )
531 goto failed_update_format_version
;
533 process_safelinks(super
);
534 reiser4_exit_context(&ctx
);
536 sbinfo
->debugfs_root
= debugfs_create_dir(super
->s_id
,
537 reiser4_debugfs_root
);
538 if (sbinfo
->debugfs_root
) {
539 sbinfo
->tmgr
.debugfs_atom_count
=
540 debugfs_create_u32("atom_count", S_IFREG
|S_IRUSR
,
541 sbinfo
->debugfs_root
,
542 &sbinfo
->tmgr
.atom_count
);
543 sbinfo
->tmgr
.debugfs_id_count
=
544 debugfs_create_u32("id_count", S_IFREG
|S_IRUSR
,
545 sbinfo
->debugfs_root
,
546 &sbinfo
->tmgr
.id_count
);
550 failed_update_format_version
:
551 failed_init_root_inode
:
552 if (sbinfo
->df_plug
->release
)
553 sbinfo
->df_plug
->release(super
);
554 failed_init_disk_format
:
555 reiser4_done_formatted_fake(super
);
556 failed_init_formatted_fake
:
557 reiser4_done_entd(super
);
559 reiser4_done_ktxnmgrd(super
);
560 failed_init_ktxnmgrd
:
561 reiser4_done_txnmgr(&sbinfo
->tmgr
);
562 failed_init_read_super
:
563 failed_init_super_data
:
564 reiser4_done_fs_info(super
);
566 reiser4_exit_context(&ctx
);
571 * reiser4_get_sb - get_sb of file_system_type operations
573 * @flags: mount flags MS_RDONLY, MS_VERBOSE, etc
574 * @dev_name: block device file name
575 * @data: specific mount options
577 * Reiser4 mount entry.
579 static int reiser4_get_sb(struct file_system_type
*fs_type
, int flags
,
580 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
582 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, fill_super
, mnt
);
585 /* structure describing the reiser4 filesystem implementation */
586 static struct file_system_type reiser4_fs_type
= {
587 .owner
= THIS_MODULE
,
589 .fs_flags
= FS_REQUIRES_DEV
,
590 .get_sb
= reiser4_get_sb
,
591 .kill_sb
= kill_block_super
,
595 void destroy_reiser4_cache(struct kmem_cache
**cachep
)
597 BUG_ON(*cachep
== NULL
);
598 kmem_cache_destroy(*cachep
);
603 * init_reiser4 - reiser4 initialization entry point
605 * Initializes reiser4 slabs, registers reiser4 filesystem type. It is called
606 * on kernel initialization or during reiser4 module load.
608 static int __init
init_reiser4(void)
614 "See www.namesys.com for a description of Reiser4.\n");
616 /* initialize slab cache of inodes */
617 if ((result
= init_inodes()) != 0)
618 goto failed_inode_cache
;
620 /* initialize cache of znodes */
621 if ((result
= init_znodes()) != 0)
622 goto failed_init_znodes
;
624 /* initialize all plugins */
625 if ((result
= init_plugins()) != 0)
626 goto failed_init_plugins
;
628 /* initialize cache of plugin_set-s and plugin_set's hash table */
629 if ((result
= init_plugin_set()) != 0)
630 goto failed_init_plugin_set
;
632 /* initialize caches of txn_atom-s and txn_handle-s */
633 if ((result
= init_txnmgr_static()) != 0)
634 goto failed_init_txnmgr_static
;
636 /* initialize cache of jnodes */
637 if ((result
= init_jnodes()) != 0)
638 goto failed_init_jnodes
;
640 /* initialize cache of flush queues */
641 if ((result
= reiser4_init_fqs()) != 0)
642 goto failed_init_fqs
;
644 /* initialize cache of structures attached to dentry->d_fsdata */
645 if ((result
= reiser4_init_dentry_fsdata()) != 0)
646 goto failed_init_dentry_fsdata
;
648 /* initialize cache of structures attached to file->private_data */
649 if ((result
= reiser4_init_file_fsdata()) != 0)
650 goto failed_init_file_fsdata
;
653 * initialize cache of d_cursors. See plugin/file_ops_readdir.c for
656 if ((result
= reiser4_init_d_cursor()) != 0)
657 goto failed_init_d_cursor
;
659 if ((result
= register_filesystem(&reiser4_fs_type
)) == 0) {
660 reiser4_debugfs_root
= debugfs_create_dir("reiser4", NULL
);
664 reiser4_done_d_cursor();
665 failed_init_d_cursor
:
666 reiser4_done_file_fsdata();
667 failed_init_file_fsdata
:
668 reiser4_done_dentry_fsdata();
669 failed_init_dentry_fsdata
:
674 done_txnmgr_static();
675 failed_init_txnmgr_static
:
677 failed_init_plugin_set
:
687 * done_reiser4 - reiser4 exit entry point
689 * Unregister reiser4 filesystem type, deletes caches. It is called on shutdown
690 * or at module unload.
692 static void __exit
done_reiser4(void)
696 debugfs_remove(reiser4_debugfs_root
);
697 result
= unregister_filesystem(&reiser4_fs_type
);
699 reiser4_done_d_cursor();
700 reiser4_done_file_fsdata();
701 reiser4_done_dentry_fsdata();
704 done_txnmgr_static();
707 destroy_reiser4_cache(&inode_cache
);
710 module_init(init_reiser4
);
711 module_exit(done_reiser4
);
713 MODULE_DESCRIPTION("Reiser4 filesystem");
714 MODULE_AUTHOR("Hans Reiser <Reiser@Namesys.COM>");
716 MODULE_LICENSE("GPL");
720 * c-indentation-style: "K&R"