revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / fs / reiser4 / super_ops.c
blob2d2a51024c7a1db45b38e43fff7a535691066afa
1 /* Copyright 2005 by Hans Reiser, licensing governed by
2 * reiser4/README */
4 #include "inode.h"
5 #include "page_cache.h"
6 #include "ktxnmgrd.h"
7 #include "flush.h"
8 #include "safe_link.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;
21 /**
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;
33 info = obj;
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),
48 GFP_ATOMIC);
49 #if REISER4_DEBUG
50 info->p.nr_jnodes = 0;
51 #endif
54 /**
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),
64 SLAB_HWCACHE_ALIGN |
65 SLAB_RECLAIM_ACCOUNT, init_once);
66 if (inode_cache == NULL)
67 return RETERR(-ENOMEM);
68 return 0;
71 /**
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);
81 /**
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());
93 if (obj != NULL) {
94 reiser4_inode *info;
96 info = &obj->p;
98 info->pset = plugin_set_get_empty();
99 info->hset = plugin_set_get_empty();
100 info->extmask = 0;
101 info->locality_id = 0ull;
102 info->plugin_mask = 0;
103 info->heir_mask = 0;
104 #if !REISER4_INO_IS_OID
105 info->oid_hi = 0;
106 #endif
107 reiser4_seal_init(&info->sd_seal, NULL, NULL);
108 coord_init_invalid(&info->sd_coord, NULL);
109 info->flags = 0;
110 spin_lock_init(&info->guard);
111 /* this deals with info's loading semaphore */
112 loading_alloc(info);
113 info->vroot = UBER_TREE_ADDR;
114 return &obj->vfs_inode;
115 } else
116 return NULL;
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)
127 reiser4_inode *info;
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);
139 if (info->pset)
140 plugin_set_put(info->pset);
141 if (info->hset)
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
164 * Updates stat data.
166 static void reiser4_dirty_inode(struct inode *inode)
168 int result;
170 if (!is_in_reiser4_context())
171 return;
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);
177 if (result)
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;
192 file_plugin *fplug;
194 ctx = reiser4_init_context(inode->i_sb);
195 if (IS_ERR(ctx)) {
196 warning("vs-15", "failed to init context");
197 return;
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);
207 inode->i_blocks = 0;
208 clear_inode(inode);
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);
231 if (IS_ERR(ctx)) {
232 warning("vs-17", "failed to init context");
233 return;
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)
259 int ret;
260 reiser4_context *ctx;
262 assert("vs-1700", !rofs_super(super));
264 ctx = reiser4_init_context(super);
265 if (IS_ERR(ctx)) {
266 warning("vs-16", "failed to init context");
267 return;
270 ret = reiser4_capture_super_block(super);
271 if (ret != 0)
272 warning("vs-1701",
273 "reiser4_capture_super_block failed in write_super: %d",
274 ret);
275 ret = txnmgr_force_commit_all(super, 0);
276 if (ret != 0)
277 warning("jmacd-77113",
278 "txn_force failed in write_super: %d", ret);
280 super->s_dirt = 0;
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)
294 sector_t total;
295 sector_t reserved;
296 sector_t free;
297 sector_t forroot;
298 sector_t deleted;
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);
306 if (IS_ERR(ctx))
307 return PTR_ERR(ctx);
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
318 * frustrating.
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
323 * mkfs.
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
335 * blocks.
337 if (free > total)
338 free = total;
339 statfs->f_blocks = total - reserved;
340 /* make sure statfs->f_bfree is never larger than statfs->f_blocks */
341 if (free > reserved)
342 free -= reserved;
343 else
344 free = 0;
345 statfs->f_bfree = free;
347 if (free > forroot)
348 free -= forroot;
349 else
350 free = 0;
351 statfs->f_bavail = free;
353 statfs->f_files = 0;
354 statfs->f_ffree = 0;
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);
360 return 0;
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)
371 #if REISER4_DEBUG
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);
378 #endif
382 * reiser4_sync_inodes - sync_inodes of super operations
383 * @super:
384 * @wbc:
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
390 * atoms.
392 static int reiser4_sync_inodes(struct super_block *super,
393 struct writeback_control *wbc)
395 reiser4_context *ctx;
396 long to_write;
397 int ret;
399 if (wbc->for_kupdate)
400 /* reiser4 has its own means of periodical write-out */
401 return 0;
403 to_write = wbc->nr_to_write;
404 assert("vs-49", wbc->older_than_this == NULL);
406 ctx = reiser4_init_context(super);
407 if (IS_ERR(ctx)) {
408 warning("vs-13", "failed to init context");
409 return PTR_ERR(ctx);
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);
425 return ret;
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;
440 super = mnt->mnt_sb;
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);
451 return 0;
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
471 * @silent:
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)
477 reiser4_context ctx;
478 int result;
479 reiser4_super_info_data *sbinfo;
481 assert("zam-989", super != NULL);
483 super->s_op = 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
521 * initialized here
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);
548 return 0;
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);
558 failed_init_entd:
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);
565 failed_init_sinfo:
566 reiser4_exit_context(&ctx);
567 return result;
571 * reiser4_get_sb - get_sb of file_system_type operations
572 * @fs_type:
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,
588 .name = "reiser4",
589 .fs_flags = FS_REQUIRES_DEV,
590 .get_sb = reiser4_get_sb,
591 .kill_sb = kill_block_super,
592 .next = NULL
595 void destroy_reiser4_cache(struct kmem_cache **cachep)
597 BUG_ON(*cachep == NULL);
598 kmem_cache_destroy(*cachep);
599 *cachep = NULL;
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)
610 int result;
612 printk(KERN_INFO
613 "Loading Reiser4. "
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
654 * more details
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);
661 return 0;
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:
670 reiser4_done_fqs();
671 failed_init_fqs:
672 done_jnodes();
673 failed_init_jnodes:
674 done_txnmgr_static();
675 failed_init_txnmgr_static:
676 done_plugin_set();
677 failed_init_plugin_set:
678 failed_init_plugins:
679 done_znodes();
680 failed_init_znodes:
681 done_inodes();
682 failed_inode_cache:
683 return result;
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)
694 int result;
696 debugfs_remove(reiser4_debugfs_root);
697 result = unregister_filesystem(&reiser4_fs_type);
698 BUG_ON(result != 0);
699 reiser4_done_d_cursor();
700 reiser4_done_file_fsdata();
701 reiser4_done_dentry_fsdata();
702 reiser4_done_fqs();
703 done_jnodes();
704 done_txnmgr_static();
705 done_plugin_set();
706 done_znodes();
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");
719 * Local variables:
720 * c-indentation-style: "K&R"
721 * mode-name: "LC"
722 * c-basic-offset: 8
723 * tab-width: 8
724 * fill-column: 79
725 * End: