2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
4 * Trivial changes by Alan Cox to add the LFS fixes
7 * Rights granted to Hans Reiser to redistribute under other terms providing
8 * he accepts all liability including but not limited to patent, fitness
9 * for purpose, and direct or indirect claims arising from failure to perform.
14 #include <linux/module.h>
15 #include <linux/vmalloc.h>
16 #include <linux/time.h>
17 #include <asm/uaccess.h>
18 #include <linux/reiserfs_fs.h>
19 #include <linux/reiserfs_acl.h>
20 #include <linux/reiserfs_xattr.h>
21 #include <linux/init.h>
22 #include <linux/blkdev.h>
23 #include <linux/buffer_head.h>
24 #include <linux/exportfs.h>
25 #include <linux/quotaops.h>
26 #include <linux/vfs.h>
27 #include <linux/mnt_namespace.h>
28 #include <linux/mount.h>
29 #include <linux/namei.h>
30 #include <linux/quotaops.h>
32 struct file_system_type reiserfs_fs_type;
34 static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
35 static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
36 static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
38 int is_reiserfs_3_5(struct reiserfs_super_block *rs)
40 return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
41 strlen(reiserfs_3_5_magic_string));
44 int is_reiserfs_3_6(struct reiserfs_super_block *rs)
46 return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
47 strlen(reiserfs_3_6_magic_string));
50 int is_reiserfs_jr(struct reiserfs_super_block *rs)
52 return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
53 strlen(reiserfs_jr_magic_string));
56 static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
58 return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
62 static int reiserfs_remount(struct super_block *s, int *flags, char *data);
63 static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
65 static int reiserfs_sync_fs(struct super_block *s, int wait)
67 if (!(s->s_flags & MS_RDONLY)) {
68 struct reiserfs_transaction_handle th;
69 reiserfs_write_lock(s);
70 if (!journal_begin(&th, s, 1))
71 if (!journal_end_sync(&th, s, 1))
72 reiserfs_flush_old_commits(s);
73 s->s_dirt = 0; /* Even if it's not true.
74 * We'll loop forever in sync_supers otherwise */
75 reiserfs_write_unlock(s);
82 static void reiserfs_write_super(struct super_block *s)
84 reiserfs_sync_fs(s, 1);
87 static void reiserfs_write_super_lockfs(struct super_block *s)
89 struct reiserfs_transaction_handle th;
90 reiserfs_write_lock(s);
91 if (!(s->s_flags & MS_RDONLY)) {
92 int err = journal_begin(&th, s, 1);
94 reiserfs_block_writes(&th);
96 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
98 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
99 reiserfs_block_writes(&th);
100 journal_end_sync(&th, s, 1);
104 reiserfs_write_unlock(s);
107 static void reiserfs_unlockfs(struct super_block *s)
109 reiserfs_allow_writes(s);
112 extern const struct in_core_key MAX_IN_CORE_KEY;
114 /* this is used to delete "save link" when there are no items of a
115 file it points to. It can either happen if unlink is completed but
116 "save unlink" removal, or if file has both unlink and truncate
117 pending and as unlink completes first (because key of "save link"
118 protecting unlink is bigger that a key lf "save link" which
119 protects truncate), so there left no items to make truncate
121 static int remove_save_link_only(struct super_block *s,
122 struct reiserfs_key *key, int oid_free)
124 struct reiserfs_transaction_handle th;
127 /* we are going to do one balancing */
128 err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
132 reiserfs_delete_solid_item(&th, NULL, key);
134 /* removals are protected by direct items */
135 reiserfs_release_objectid(&th, le32_to_cpu(key->k_objectid));
137 return journal_end(&th, s, JOURNAL_PER_BALANCE_CNT);
141 static int reiserfs_quota_on_mount(struct super_block *, int);
144 /* look for uncompleted unlinks and truncates and complete them */
145 static int finish_unfinished(struct super_block *s)
147 INITIALIZE_PATH(path);
148 struct cpu_key max_cpu_key, obj_key;
149 struct reiserfs_key save_link_key, last_inode_key;
151 struct item_head *ih;
152 struct buffer_head *bh;
163 /* compose key to look for "save" links */
164 max_cpu_key.version = KEY_FORMAT_3_5;
165 max_cpu_key.on_disk_key.k_dir_id = ~0U;
166 max_cpu_key.on_disk_key.k_objectid = ~0U;
167 set_cpu_key_k_offset(&max_cpu_key, ~0U);
168 max_cpu_key.key_length = 3;
170 memset(&last_inode_key, 0, sizeof(last_inode_key));
173 /* Needed for iput() to work correctly and not trash data */
174 if (s->s_flags & MS_ACTIVE) {
178 s->s_flags |= MS_ACTIVE;
180 /* Turn on quotas so that they are updated correctly */
181 for (i = 0; i < MAXQUOTAS; i++) {
182 if (REISERFS_SB(s)->s_qf_names[i]) {
183 int ret = reiserfs_quota_on_mount(s, i);
186 "reiserfs: cannot turn on journaled quota: error %d",
193 REISERFS_SB(s)->s_is_unlinked_ok = 1;
195 retval = search_item(s, &max_cpu_key, &path);
196 if (retval != ITEM_NOT_FOUND) {
198 "vs-2140: finish_unfinished: search_by_key returned %d",
203 bh = get_last_bh(&path);
204 item_pos = get_item_pos(&path);
205 if (item_pos != B_NR_ITEMS(bh)) {
207 "vs-2060: finish_unfinished: wrong position found");
211 ih = B_N_PITEM_HEAD(bh, item_pos);
213 if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
214 /* there are no "save" links anymore */
217 save_link_key = ih->ih_key;
218 if (is_indirect_le_ih(ih))
223 /* reiserfs_iget needs k_dirid and k_objectid only */
224 item = B_I_PITEM(bh, ih);
225 obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
226 obj_key.on_disk_key.k_objectid =
227 le32_to_cpu(ih->ih_key.k_objectid);
228 obj_key.on_disk_key.k_offset = 0;
229 obj_key.on_disk_key.k_type = 0;
233 inode = reiserfs_iget(s, &obj_key);
235 /* the unlink almost completed, it just did not manage to remove
236 "save" link and release objectid */
238 "vs-2180: finish_unfinished: iget failed for %K",
240 retval = remove_save_link_only(s, &save_link_key, 1);
244 if (!truncate && inode->i_nlink) {
245 /* file is not unlinked */
247 "vs-2185: finish_unfinished: file %K is not unlinked",
249 retval = remove_save_link_only(s, &save_link_key, 0);
254 if (truncate && S_ISDIR(inode->i_mode)) {
255 /* We got a truncate request for a dir which is impossible.
256 The only imaginable way is to execute unfinished truncate request
257 then boot into old kernel, remove the file and create dir with
260 "green-2101: impossible truncate on a directory %k. Please report",
262 retval = remove_save_link_only(s, &save_link_key, 0);
269 REISERFS_I(inode)->i_flags |=
270 i_link_saved_truncate_mask;
271 /* not completed truncate found. New size was committed together
273 reiserfs_info(s, "Truncating %k to %Ld ..",
274 INODE_PKEY(inode), inode->i_size);
275 reiserfs_truncate_file(inode,
277 /*don't update modification time */
279 retval = remove_save_link(inode, truncate);
281 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
282 /* not completed unlink (rmdir) found */
283 reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
284 if (memcmp(&last_inode_key, INODE_PKEY(inode),
285 sizeof(last_inode_key))){
286 last_inode_key = *INODE_PKEY(inode);
287 /* removal gets completed in iput */
290 reiserfs_warning(s, "Dead loop in "
291 "finish_unfinished detected, "
292 "just remove save link\n");
293 retval = remove_save_link_only(s,
302 REISERFS_SB(s)->s_is_unlinked_ok = 0;
305 /* Turn quotas off */
306 for (i = 0; i < MAXQUOTAS; i++) {
307 if (sb_dqopt(s)->files[i])
308 vfs_quota_off(s, i, 0);
311 /* Restore the flag back */
312 s->s_flags &= ~MS_ACTIVE;
316 reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
317 "Completed\n", done);
321 /* to protect file being unlinked from getting lost we "safe" link files
322 being unlinked. This link will be deleted in the same transaction with last
323 item of file. mounting the filesystem we scan all these links and remove
324 files which almost got lost */
325 void add_save_link(struct reiserfs_transaction_handle *th,
326 struct inode *inode, int truncate)
328 INITIALIZE_PATH(path);
334 BUG_ON(!th->t_trans_id);
336 /* file can only get one "save link" of each kind */
338 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
339 "saved link already exists for truncated inode %lx",
342 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
343 "saved link already exists for unlinked inode %lx",
346 /* setup key of "save" link */
347 key.version = KEY_FORMAT_3_5;
348 key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
349 key.on_disk_key.k_objectid = inode->i_ino;
351 /* unlink, rmdir, rename */
352 set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
353 set_cpu_key_k_type(&key, TYPE_DIRECT);
355 /* item head of "safe" link */
356 make_le_item_head(&ih, &key, key.version,
357 1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
358 4 /*length */ , 0xffff /*free space */ );
361 if (S_ISDIR(inode->i_mode))
362 reiserfs_warning(inode->i_sb,
363 "green-2102: Adding a truncate savelink for a directory %k! Please report",
365 set_cpu_key_k_offset(&key, 1);
366 set_cpu_key_k_type(&key, TYPE_INDIRECT);
368 /* item head of "safe" link */
369 make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
370 4 /*length */ , 0 /*free space */ );
374 /* look for its place in the tree */
375 retval = search_item(inode->i_sb, &key, &path);
376 if (retval != ITEM_NOT_FOUND) {
377 if (retval != -ENOSPC)
378 reiserfs_warning(inode->i_sb, "vs-2100: add_save_link:"
379 "search_by_key (%K) returned %d", &key,
385 /* body of "save" link */
386 link = INODE_PKEY(inode)->k_dir_id;
388 /* put "save" link inot tree, don't charge quota to anyone */
390 reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
392 if (retval != -ENOSPC)
393 reiserfs_warning(inode->i_sb,
394 "vs-2120: add_save_link: insert_item returned %d",
398 REISERFS_I(inode)->i_flags |=
399 i_link_saved_truncate_mask;
401 REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
405 /* this opens transaction unlike add_save_link */
406 int remove_save_link(struct inode *inode, int truncate)
408 struct reiserfs_transaction_handle th;
409 struct reiserfs_key key;
412 /* we are going to do one balancing only */
413 err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
417 /* setup key of "save" link */
418 key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
419 key.k_objectid = INODE_PKEY(inode)->k_objectid;
421 /* unlink, rmdir, rename */
422 set_le_key_k_offset(KEY_FORMAT_3_5, &key,
423 1 + inode->i_sb->s_blocksize);
424 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
427 set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
428 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
432 (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
434 (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
435 /* don't take quota bytes from anywhere */
436 reiserfs_delete_solid_item(&th, NULL, &key);
438 reiserfs_release_objectid(&th, inode->i_ino);
439 REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
441 REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;
443 return journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
446 static void reiserfs_kill_sb(struct super_block *s)
448 if (REISERFS_SB(s)) {
449 #ifdef CONFIG_REISERFS_FS_XATTR
450 if (REISERFS_SB(s)->xattr_root) {
451 d_invalidate(REISERFS_SB(s)->xattr_root);
452 dput(REISERFS_SB(s)->xattr_root);
453 REISERFS_SB(s)->xattr_root = NULL;
456 if (REISERFS_SB(s)->priv_root) {
457 d_invalidate(REISERFS_SB(s)->priv_root);
458 dput(REISERFS_SB(s)->priv_root);
459 REISERFS_SB(s)->priv_root = NULL;
466 static void reiserfs_put_super(struct super_block *s)
468 struct reiserfs_transaction_handle th;
471 /* change file system state to current state if it was mounted with read-write permissions */
472 if (!(s->s_flags & MS_RDONLY)) {
473 if (!journal_begin(&th, s, 10)) {
474 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
476 set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
477 REISERFS_SB(s)->s_mount_state);
478 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
482 /* note, journal_release checks for readonly mount, and can decide not
483 ** to do a journal_end
485 journal_release(&th, s);
487 reiserfs_free_bitmap_cache(s);
489 brelse(SB_BUFFER_WITH_SB(s));
493 if (REISERFS_SB(s)->reserved_blocks != 0) {
495 "green-2005: reiserfs_put_super: reserved blocks left %d",
496 REISERFS_SB(s)->reserved_blocks);
499 reiserfs_proc_info_done(s);
507 static struct kmem_cache *reiserfs_inode_cachep;
509 static struct inode *reiserfs_alloc_inode(struct super_block *sb)
511 struct reiserfs_inode_info *ei;
512 ei = (struct reiserfs_inode_info *)
513 kmem_cache_alloc(reiserfs_inode_cachep, GFP_KERNEL);
516 return &ei->vfs_inode;
519 static void reiserfs_destroy_inode(struct inode *inode)
521 kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
524 static void init_once(struct kmem_cache * cachep, void *foo)
526 struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
528 INIT_LIST_HEAD(&ei->i_prealloc_list);
529 inode_init_once(&ei->vfs_inode);
530 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
531 ei->i_acl_access = NULL;
532 ei->i_acl_default = NULL;
536 static int init_inodecache(void)
538 reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
540 reiserfs_inode_info),
541 0, (SLAB_RECLAIM_ACCOUNT|
544 if (reiserfs_inode_cachep == NULL)
549 static void destroy_inodecache(void)
551 kmem_cache_destroy(reiserfs_inode_cachep);
554 /* we don't mark inodes dirty, we just log them */
555 static void reiserfs_dirty_inode(struct inode *inode)
557 struct reiserfs_transaction_handle th;
560 if (inode->i_sb->s_flags & MS_RDONLY) {
561 reiserfs_warning(inode->i_sb,
562 "clm-6006: writing inode %lu on readonly FS",
566 reiserfs_write_lock(inode->i_sb);
568 /* this is really only used for atime updates, so they don't have
569 ** to be included in O_SYNC or fsync
571 err = journal_begin(&th, inode->i_sb, 1);
573 reiserfs_write_unlock(inode->i_sb);
576 reiserfs_update_sd(&th, inode);
577 journal_end(&th, inode->i_sb, 1);
578 reiserfs_write_unlock(inode->i_sb);
581 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
582 static void reiserfs_clear_inode(struct inode *inode)
584 struct posix_acl *acl;
586 acl = REISERFS_I(inode)->i_acl_access;
587 if (acl && !IS_ERR(acl))
588 posix_acl_release(acl);
589 REISERFS_I(inode)->i_acl_access = NULL;
591 acl = REISERFS_I(inode)->i_acl_default;
592 if (acl && !IS_ERR(acl))
593 posix_acl_release(acl);
594 REISERFS_I(inode)->i_acl_default = NULL;
597 #define reiserfs_clear_inode NULL
601 static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
603 static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
607 static const struct super_operations reiserfs_sops = {
608 .alloc_inode = reiserfs_alloc_inode,
609 .destroy_inode = reiserfs_destroy_inode,
610 .write_inode = reiserfs_write_inode,
611 .dirty_inode = reiserfs_dirty_inode,
612 .delete_inode = reiserfs_delete_inode,
613 .clear_inode = reiserfs_clear_inode,
614 .put_super = reiserfs_put_super,
615 .write_super = reiserfs_write_super,
616 .sync_fs = reiserfs_sync_fs,
617 .write_super_lockfs = reiserfs_write_super_lockfs,
618 .unlockfs = reiserfs_unlockfs,
619 .statfs = reiserfs_statfs,
620 .remount_fs = reiserfs_remount,
621 .show_options = generic_show_options,
623 .quota_read = reiserfs_quota_read,
624 .quota_write = reiserfs_quota_write,
629 #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
631 static int reiserfs_dquot_initialize(struct inode *, int);
632 static int reiserfs_dquot_drop(struct inode *);
633 static int reiserfs_write_dquot(struct dquot *);
634 static int reiserfs_acquire_dquot(struct dquot *);
635 static int reiserfs_release_dquot(struct dquot *);
636 static int reiserfs_mark_dquot_dirty(struct dquot *);
637 static int reiserfs_write_info(struct super_block *, int);
638 static int reiserfs_quota_on(struct super_block *, int, int, char *, int);
640 static struct dquot_operations reiserfs_quota_operations = {
641 .initialize = reiserfs_dquot_initialize,
642 .drop = reiserfs_dquot_drop,
643 .alloc_space = dquot_alloc_space,
644 .alloc_inode = dquot_alloc_inode,
645 .free_space = dquot_free_space,
646 .free_inode = dquot_free_inode,
647 .transfer = dquot_transfer,
648 .write_dquot = reiserfs_write_dquot,
649 .acquire_dquot = reiserfs_acquire_dquot,
650 .release_dquot = reiserfs_release_dquot,
651 .mark_dirty = reiserfs_mark_dquot_dirty,
652 .write_info = reiserfs_write_info,
655 static struct quotactl_ops reiserfs_qctl_operations = {
656 .quota_on = reiserfs_quota_on,
657 .quota_off = vfs_quota_off,
658 .quota_sync = vfs_quota_sync,
659 .get_info = vfs_get_dqinfo,
660 .set_info = vfs_set_dqinfo,
661 .get_dqblk = vfs_get_dqblk,
662 .set_dqblk = vfs_set_dqblk,
666 static const struct export_operations reiserfs_export_ops = {
667 .encode_fh = reiserfs_encode_fh,
668 .fh_to_dentry = reiserfs_fh_to_dentry,
669 .fh_to_parent = reiserfs_fh_to_parent,
670 .get_parent = reiserfs_get_parent,
673 /* this struct is used in reiserfs_getopt () for containing the value for those
674 mount options that have values rather than being toggles. */
677 int setmask; /* bitmask which is to set on mount_options bitmask when this
678 value is found, 0 is no bits are to be changed. */
679 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
680 value is found, 0 is no bits are to be changed. This is
681 applied BEFORE setmask */
684 /* Set this bit in arg_required to allow empty arguments */
685 #define REISERFS_OPT_ALLOWEMPTY 31
687 /* this struct is used in reiserfs_getopt() for describing the set of reiserfs
691 int arg_required; /* 0 if argument is not required, not 0 otherwise */
692 const arg_desc_t *values; /* list of values accepted by an option */
693 int setmask; /* bitmask which is to set on mount_options bitmask when this
694 value is found, 0 is no bits are to be changed. */
695 int clrmask; /* bitmask which is to clear on mount_options bitmask when this
696 value is found, 0 is no bits are to be changed. This is
697 applied BEFORE setmask */
700 /* possible values for -o data= */
701 static const arg_desc_t logging_mode[] = {
702 {"ordered", 1 << REISERFS_DATA_ORDERED,
703 (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
704 {"journal", 1 << REISERFS_DATA_LOG,
705 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
706 {"writeback", 1 << REISERFS_DATA_WRITEBACK,
707 (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
711 /* possible values for -o barrier= */
712 static const arg_desc_t barrier_mode[] = {
713 {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
714 {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
718 /* possible values for "-o block-allocator=" and bits which are to be set in
719 s_mount_opt of reiserfs specific part of in-core super block */
720 static const arg_desc_t balloc[] = {
721 {"noborder", 1 << REISERFS_NO_BORDER, 0},
722 {"border", 0, 1 << REISERFS_NO_BORDER},
723 {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
724 {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
725 {"test4", 1 << REISERFS_TEST4, 0},
726 {"notest4", 0, 1 << REISERFS_TEST4},
730 static const arg_desc_t tails[] = {
731 {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
732 {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
733 {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
737 static const arg_desc_t error_actions[] = {
738 {"panic", 1 << REISERFS_ERROR_PANIC,
739 (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
740 {"ro-remount", 1 << REISERFS_ERROR_RO,
741 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
742 #ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
743 {"continue", 1 << REISERFS_ERROR_CONTINUE,
744 (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
749 /* proceed only one option from a list *cur - string containing of mount options
750 opts - array of options which are accepted
751 opt_arg - if option is found and requires an argument and if it is specifed
752 in the input - pointer to the argument is stored here
753 bit_flags - if option requires to set a certain bit - it is set here
754 return -1 if unknown option is found, opt->arg_required otherwise */
755 static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
756 char **opt_arg, unsigned long *bit_flags)
765 const opt_desc_t *opt;
766 const arg_desc_t *arg;
770 /* assume argument cannot contain commas */
771 *cur = strchr(p, ',');
777 if (!strncmp(p, "alloc=", 6)) {
778 /* Ugly special case, probably we should redo options parser so that
779 it can understand several arguments for some options, also so that
780 it can fill several bitfields with option values. */
781 if (reiserfs_parse_alloc_options(s, p + 6)) {
788 /* for every option in the list */
789 for (opt = opts; opt->option_name; opt++) {
790 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
793 (1 << REISERFS_UNSUPPORTED_OPT))
794 reiserfs_warning(s, "%s not supported.",
797 *bit_flags &= ~opt->clrmask;
799 (1 << REISERFS_UNSUPPORTED_OPT))
800 reiserfs_warning(s, "%s not supported.",
803 *bit_flags |= opt->setmask;
808 if (!opt->option_name) {
809 reiserfs_warning(s, "unknown mount option \"%s\"", p);
813 p += strlen(opt->option_name);
816 if (!opt->arg_required) {
818 "the option \"%s\" does not require an argument",
825 if (opt->arg_required) {
827 "the option \"%s\" requires an argument",
833 reiserfs_warning(s, "head of option \"%s\" is only correct",
838 /* move to the argument, or to next option if argument is not required */
841 if (opt->arg_required
842 && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
844 /* this catches "option=," if not allowed */
845 reiserfs_warning(s, "empty argument for \"%s\"",
851 /* *=NULLopt_arg contains pointer to argument */
853 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
856 /* values possible for this option are listed in opt->values */
857 for (arg = opt->values; arg->value; arg++) {
858 if (!strcmp(p, arg->value)) {
860 *bit_flags &= ~arg->clrmask;
861 *bit_flags |= arg->setmask;
863 return opt->arg_required;
867 reiserfs_warning(s, "bad value \"%s\" for option \"%s\"", p,
872 /* returns 0 if something is wrong in option string, 1 - otherwise */
873 static int reiserfs_parse_options(struct super_block *s, char *options, /* string given via mount's -o */
874 unsigned long *mount_options,
875 /* after the parsing phase, contains the
876 collection of bitflags defining what
877 mount options were selected. */
878 unsigned long *blocks, /* strtol-ed from NNN of resize=NNN */
880 unsigned int *commit_max_age,
887 opt_desc_t opts[] = {
888 /* Compatibility stuff, so that -o notail for old setups still work */
889 {"tails",.arg_required = 't',.values = tails},
891 (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
892 {"conv",.setmask = 1 << REISERFS_CONVERT},
893 {"attrs",.setmask = 1 << REISERFS_ATTRS},
894 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
895 #ifdef CONFIG_REISERFS_FS_XATTR
896 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
897 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
899 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
900 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
902 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
903 {"acl",.setmask = 1 << REISERFS_POSIXACL},
904 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
906 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
907 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
909 {.option_name = "nolog"},
910 {"replayonly",.setmask = 1 << REPLAYONLY},
911 {"block-allocator",.arg_required = 'a',.values = balloc},
912 {"data",.arg_required = 'd',.values = logging_mode},
913 {"barrier",.arg_required = 'b',.values = barrier_mode},
914 {"resize",.arg_required = 'r',.values = NULL},
915 {"jdev",.arg_required = 'j',.values = NULL},
916 {"nolargeio",.arg_required = 'w',.values = NULL},
917 {"commit",.arg_required = 'c',.values = NULL},
918 {"usrquota",.setmask = 1 << REISERFS_QUOTA},
919 {"grpquota",.setmask = 1 << REISERFS_QUOTA},
920 {"noquota",.clrmask = 1 << REISERFS_QUOTA},
921 {"errors",.arg_required = 'e',.values = error_actions},
922 {"usrjquota",.arg_required =
923 'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
924 {"grpjquota",.arg_required =
925 'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
926 {"jqfmt",.arg_required = 'f',.values = NULL},
927 {.option_name = NULL}
931 if (!options || !*options)
932 /* use default configuration: create tails, journaling on, no
933 conversion to newest format */
936 for (pos = options; pos;) {
937 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
939 /* wrong option is given */
946 /* "resize=NNN" or "resize=auto" */
948 if (!strcmp(arg, "auto")) {
949 /* From JFS code, to auto-get the size. */
951 s->s_bdev->bd_inode->i_size >> s->
954 *blocks = simple_strtoul(arg, &p, 0);
956 /* NNN does not look like a number */
958 "reiserfs_parse_options: bad value %s",
967 unsigned long val = simple_strtoul(arg, &p, 0);
968 /* commit=NNN (time in seconds) */
969 if (*p != '\0' || val >= (unsigned int)-1) {
971 "reiserfs_parse_options: bad value %s",
975 *commit_max_age = (unsigned int)val;
979 reiserfs_warning(s, "reiserfs: nolargeio option is no longer supported");
984 if (arg && *arg && jdev_name) {
985 if (*jdev_name) { //Hm, already assigned?
987 "reiserfs_parse_options: journal device was already specified to be %s",
995 if (c == 'u' || c == 'g') {
996 int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
998 if ((sb_any_quota_enabled(s) ||
999 sb_any_quota_suspended(s)) &&
1000 (!*arg != !REISERFS_SB(s)->s_qf_names[qtype])) {
1002 "reiserfs_parse_options: cannot change journaled quota options when quota turned on.");
1005 if (*arg) { /* Some filename specified? */
1006 if (REISERFS_SB(s)->s_qf_names[qtype]
1007 && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
1010 "reiserfs_parse_options: %s quota file already specified.",
1014 if (strchr(arg, '/')) {
1016 "reiserfs_parse_options: quotafile must be on filesystem root.");
1020 kmalloc(strlen(arg) + 1, GFP_KERNEL);
1021 if (!qf_names[qtype]) {
1023 "reiserfs_parse_options: not enough memory for storing quotafile name.");
1026 strcpy(qf_names[qtype], arg);
1027 *mount_options |= 1 << REISERFS_QUOTA;
1029 if (qf_names[qtype] !=
1030 REISERFS_SB(s)->s_qf_names[qtype])
1031 kfree(qf_names[qtype]);
1032 qf_names[qtype] = NULL;
1036 if (!strcmp(arg, "vfsold"))
1037 *qfmt = QFMT_VFS_OLD;
1038 else if (!strcmp(arg, "vfsv0"))
1039 *qfmt = QFMT_VFS_V0;
1042 "reiserfs_parse_options: unknown quota format specified.");
1045 if ((sb_any_quota_enabled(s) ||
1046 sb_any_quota_suspended(s)) &&
1047 *qfmt != REISERFS_SB(s)->s_jquota_fmt) {
1049 "reiserfs_parse_options: cannot change journaled quota options when quota turned on.");
1054 if (c == 'u' || c == 'g' || c == 'f') {
1056 "reiserfs_parse_options: journaled quota options not supported.");
1063 if (!REISERFS_SB(s)->s_jquota_fmt && !*qfmt
1064 && (qf_names[USRQUOTA] || qf_names[GRPQUOTA])) {
1066 "reiserfs_parse_options: journaled quota format not specified.");
1069 /* This checking is not precise wrt the quota type but for our purposes it is sufficient */
1070 if (!(*mount_options & (1 << REISERFS_QUOTA))
1071 && sb_any_quota_enabled(s)) {
1073 "reiserfs_parse_options: quota options must be present when quota is turned on.");
1081 static void switch_data_mode(struct super_block *s, unsigned long mode)
1083 REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1084 (1 << REISERFS_DATA_ORDERED) |
1085 (1 << REISERFS_DATA_WRITEBACK));
1086 REISERFS_SB(s)->s_mount_opt |= (1 << mode);
1089 static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1091 if (mount_options & (1 << REISERFS_DATA_LOG)) {
1092 if (!reiserfs_data_log(s)) {
1093 switch_data_mode(s, REISERFS_DATA_LOG);
1094 reiserfs_info(s, "switching to journaled data mode\n");
1096 } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1097 if (!reiserfs_data_ordered(s)) {
1098 switch_data_mode(s, REISERFS_DATA_ORDERED);
1099 reiserfs_info(s, "switching to ordered data mode\n");
1101 } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1102 if (!reiserfs_data_writeback(s)) {
1103 switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1104 reiserfs_info(s, "switching to writeback data mode\n");
1109 static void handle_barrier_mode(struct super_block *s, unsigned long bits)
1111 int flush = (1 << REISERFS_BARRIER_FLUSH);
1112 int none = (1 << REISERFS_BARRIER_NONE);
1113 int all_barrier = flush | none;
1115 if (bits & all_barrier) {
1116 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1118 REISERFS_SB(s)->s_mount_opt |= flush;
1119 printk("reiserfs: enabling write barrier flush mode\n");
1120 } else if (bits & none) {
1121 REISERFS_SB(s)->s_mount_opt |= none;
1122 printk("reiserfs: write barriers turned off\n");
1127 static void handle_attrs(struct super_block *s)
1129 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1131 if (reiserfs_attrs(s)) {
1132 if (old_format_only(s)) {
1134 "reiserfs: cannot support attributes on 3.5.x disk format");
1135 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
1138 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
1140 "reiserfs: cannot support attributes until flag is set in super-block");
1141 REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
1147 static void handle_quota_files(struct super_block *s, char **qf_names,
1152 for (i = 0; i < MAXQUOTAS; i++) {
1153 if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1154 kfree(REISERFS_SB(s)->s_qf_names[i]);
1155 REISERFS_SB(s)->s_qf_names[i] = qf_names[i];
1157 REISERFS_SB(s)->s_jquota_fmt = *qfmt;
1161 static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
1163 struct reiserfs_super_block *rs;
1164 struct reiserfs_transaction_handle th;
1165 unsigned long blocks;
1166 unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1167 unsigned long safe_mask = 0;
1168 unsigned int commit_max_age = (unsigned int)-1;
1169 struct reiserfs_journal *journal = SB_JOURNAL(s);
1170 char *new_opts = kstrdup(arg, GFP_KERNEL);
1172 char *qf_names[MAXQUOTAS];
1173 unsigned int qfmt = 0;
1177 memcpy(qf_names, REISERFS_SB(s)->s_qf_names, sizeof(qf_names));
1180 rs = SB_DISK_SUPER_BLOCK(s);
1182 if (!reiserfs_parse_options
1183 (s, arg, &mount_options, &blocks, NULL, &commit_max_age,
1186 for (i = 0; i < MAXQUOTAS; i++)
1187 if (qf_names[i] != REISERFS_SB(s)->s_qf_names[i])
1194 handle_quota_files(s, qf_names, &qfmt);
1199 /* Add options that are safe here */
1200 safe_mask |= 1 << REISERFS_SMALLTAIL;
1201 safe_mask |= 1 << REISERFS_LARGETAIL;
1202 safe_mask |= 1 << REISERFS_NO_BORDER;
1203 safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1204 safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1205 safe_mask |= 1 << REISERFS_TEST4;
1206 safe_mask |= 1 << REISERFS_ATTRS;
1207 safe_mask |= 1 << REISERFS_XATTRS_USER;
1208 safe_mask |= 1 << REISERFS_POSIXACL;
1209 safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1210 safe_mask |= 1 << REISERFS_BARRIER_NONE;
1211 safe_mask |= 1 << REISERFS_ERROR_RO;
1212 safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1213 safe_mask |= 1 << REISERFS_ERROR_PANIC;
1214 safe_mask |= 1 << REISERFS_QUOTA;
1216 /* Update the bitmask, taking care to keep
1217 * the bits we're not allowed to change here */
1218 REISERFS_SB(s)->s_mount_opt =
1220 s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
1222 if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1223 journal->j_max_commit_age = commit_max_age;
1224 journal->j_max_trans_age = commit_max_age;
1225 } else if (commit_max_age == 0) {
1226 /* 0 means restore defaults. */
1227 journal->j_max_commit_age = journal->j_default_max_commit_age;
1228 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1232 err = reiserfs_resize(s, blocks);
1237 if (*mount_flags & MS_RDONLY) {
1238 reiserfs_xattr_init(s, *mount_flags);
1239 /* remount read-only */
1240 if (s->s_flags & MS_RDONLY)
1241 /* it is read-only already */
1243 /* try to remount file system with read-only permissions */
1244 if (sb_umount_state(rs) == REISERFS_VALID_FS
1245 || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
1249 err = journal_begin(&th, s, 10);
1253 /* Mounting a rw partition read-only. */
1254 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1255 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1256 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1258 /* remount read-write */
1259 if (!(s->s_flags & MS_RDONLY)) {
1260 reiserfs_xattr_init(s, *mount_flags);
1261 goto out_ok; /* We are read-write already */
1264 if (reiserfs_is_journal_aborted(journal)) {
1265 err = journal->j_errno;
1269 handle_data_mode(s, mount_options);
1270 handle_barrier_mode(s, mount_options);
1271 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1272 s->s_flags &= ~MS_RDONLY; /* now it is safe to call journal_begin */
1273 err = journal_begin(&th, s, 10);
1277 /* Mount a partition which is read-only, read-write */
1278 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1279 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1280 s->s_flags &= ~MS_RDONLY;
1281 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1282 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1283 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1284 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1286 /* this will force a full flush of all journal lists */
1287 SB_JOURNAL(s)->j_must_wait = 1;
1288 err = journal_end(&th, s, 10);
1293 if (!(*mount_flags & MS_RDONLY)) {
1294 finish_unfinished(s);
1295 reiserfs_xattr_init(s, *mount_flags);
1299 kfree(s->s_options);
1300 s->s_options = new_opts;
1308 static int read_super_block(struct super_block *s, int offset)
1310 struct buffer_head *bh;
1311 struct reiserfs_super_block *rs;
1314 bh = sb_bread(s, offset / s->s_blocksize);
1316 reiserfs_warning(s, "sh-2006: read_super_block: "
1317 "bread failed (dev %s, block %lu, size %lu)",
1318 reiserfs_bdevname(s), offset / s->s_blocksize,
1323 rs = (struct reiserfs_super_block *)bh->b_data;
1324 if (!is_any_reiserfs_magic_string(rs)) {
1329 // ok, reiserfs signature (old or new) found in at the given offset
1331 fs_blocksize = sb_blocksize(rs);
1333 sb_set_blocksize(s, fs_blocksize);
1335 bh = sb_bread(s, offset / s->s_blocksize);
1337 reiserfs_warning(s, "sh-2007: read_super_block: "
1338 "bread failed (dev %s, block %lu, size %lu)\n",
1339 reiserfs_bdevname(s), offset / s->s_blocksize,
1344 rs = (struct reiserfs_super_block *)bh->b_data;
1345 if (sb_blocksize(rs) != s->s_blocksize) {
1346 reiserfs_warning(s, "sh-2011: read_super_block: "
1347 "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
1348 reiserfs_bdevname(s),
1349 (unsigned long long)bh->b_blocknr,
1355 if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1358 "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
1359 "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
1360 "get newer reiserfsprogs package");
1364 SB_BUFFER_WITH_SB(s) = bh;
1365 SB_DISK_SUPER_BLOCK(s) = rs;
1367 if (is_reiserfs_jr(rs)) {
1368 /* magic is of non-standard journal filesystem, look at s_version to
1369 find which format is in use */
1370 if (sb_version(rs) == REISERFS_VERSION_2)
1372 "read_super_block: found reiserfs format \"3.6\""
1373 " with non-standard journal");
1374 else if (sb_version(rs) == REISERFS_VERSION_1)
1376 "read_super_block: found reiserfs format \"3.5\""
1377 " with non-standard journal");
1380 "sh-2012: read_super_block: found unknown "
1381 "format \"%u\" of reiserfs with non-standard magic",
1386 /* s_version of standard format may contain incorrect information,
1387 so we just look at the magic string */
1389 "found reiserfs format \"%s\" with standard journal\n",
1390 is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1392 s->s_op = &reiserfs_sops;
1393 s->s_export_op = &reiserfs_export_ops;
1395 s->s_qcop = &reiserfs_qctl_operations;
1396 s->dq_op = &reiserfs_quota_operations;
1399 /* new format is limited by the 32 bit wide i_blocks field, want to
1400 ** be one full block below that.
1402 s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1406 /* after journal replay, reread all bitmap and super blocks */
1407 static int reread_meta_blocks(struct super_block *s)
1409 ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
1410 wait_on_buffer(SB_BUFFER_WITH_SB(s));
1411 if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1413 "reread_meta_blocks, error reading the super");
1420 /////////////////////////////////////////////////////
1421 // hash detection stuff
1423 // if root directory is empty - we set default - Yura's - hash and
1425 // FIXME: we look for only one name in a directory. If tea and yura
1426 // bith have the same value - we ask user to send report to the
1428 static __u32 find_hash_out(struct super_block *s)
1431 struct inode *inode;
1433 INITIALIZE_PATH(path);
1434 struct reiserfs_dir_entry de;
1435 __u32 hash = DEFAULT_HASH;
1437 inode = s->s_root->d_inode;
1439 do { // Some serious "goto"-hater was there ;)
1440 u32 teahash, r5hash, yurahash;
1442 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1443 retval = search_by_entry_key(s, &key, &path, &de);
1444 if (retval == IO_ERROR) {
1448 if (retval == NAME_NOT_FOUND)
1450 set_de_name_and_namelen(&de);
1451 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1452 /* allow override in this case */
1453 if (reiserfs_rupasov_hash(s)) {
1456 reiserfs_warning(s, "FS seems to be empty, autodetect "
1457 "is using the default hash");
1460 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1461 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1462 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1463 if (((teahash == r5hash)
1465 (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
1466 == r5hash)) || ((teahash == yurahash)
1468 GET_HASH_VALUE(deh_offset
1473 || ((r5hash == yurahash)
1475 GET_HASH_VALUE(deh_offset
1476 (&(de.de_deh[de.de_entry_num])))))) {
1478 "Unable to automatically detect hash function. "
1479 "Please mount with -o hash={tea,rupasov,r5}",
1480 reiserfs_bdevname(s));
1484 if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
1487 else if (GET_HASH_VALUE
1488 (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
1490 else if (GET_HASH_VALUE
1491 (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
1494 reiserfs_warning(s, "Unrecognised hash function");
1503 // finds out which hash names are sorted with
1504 static int what_hash(struct super_block *s)
1508 code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
1510 /* reiserfs_hash_detect() == true if any of the hash mount options
1511 ** were used. We must check them to make sure the user isn't
1512 ** using a bad hash value
1514 if (code == UNSET_HASH || reiserfs_hash_detect(s))
1515 code = find_hash_out(s);
1517 if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1518 /* detection has found the hash, and we must check against the
1521 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1522 reiserfs_warning(s, "Error, %s hash detected, "
1523 "unable to force rupasov hash",
1524 reiserfs_hashname(code));
1526 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1527 reiserfs_warning(s, "Error, %s hash detected, "
1528 "unable to force tea hash",
1529 reiserfs_hashname(code));
1531 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1532 reiserfs_warning(s, "Error, %s hash detected, "
1533 "unable to force r5 hash",
1534 reiserfs_hashname(code));
1538 /* find_hash_out was not called or could not determine the hash */
1539 if (reiserfs_rupasov_hash(s)) {
1541 } else if (reiserfs_tea_hash(s)) {
1543 } else if (reiserfs_r5_hash(s)) {
1548 /* if we are mounted RW, and we have a new valid hash code, update
1551 if (code != UNSET_HASH &&
1552 !(s->s_flags & MS_RDONLY) &&
1553 code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1554 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1559 // return pointer to appropriate function
1560 static hashf_t hash_function(struct super_block *s)
1562 switch (what_hash(s)) {
1564 reiserfs_info(s, "Using tea hash to sort names\n");
1567 reiserfs_info(s, "Using rupasov hash to sort names\n");
1570 reiserfs_info(s, "Using r5 hash to sort names\n");
1576 // this is used to set up correct value for old partitions
1577 static int function2code(hashf_t func)
1579 if (func == keyed_hash)
1581 if (func == yura_hash)
1583 if (func == r5_hash)
1586 BUG(); // should never happen
1591 #define SWARN(silent, s, ...) \
1593 reiserfs_warning (s, __VA_ARGS__)
1595 static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
1597 struct inode *root_inode;
1598 struct reiserfs_transaction_handle th;
1600 unsigned long blocks;
1601 unsigned int commit_max_age = 0;
1603 struct reiserfs_iget_args args;
1604 struct reiserfs_super_block *rs;
1606 struct reiserfs_sb_info *sbi;
1607 int errval = -EINVAL;
1608 char *qf_names[MAXQUOTAS] = {};
1609 unsigned int qfmt = 0;
1611 save_mount_options(s, data);
1613 sbi = kzalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
1619 /* Set default values for options: non-aggressive tails, RO on errors */
1620 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1621 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1622 /* no preallocation minimum, be smart in
1623 reiserfs_file_write instead */
1624 REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
1625 /* Preallocate by 16 blocks (17-1) at once */
1626 REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
1627 #ifdef CONFIG_REISERFS_FS_XATTR
1628 /* Initialize the rwsem for xattr dir */
1629 init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
1631 /* setup default block allocator options */
1632 reiserfs_init_alloc_options(s);
1635 if (reiserfs_parse_options
1636 (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
1637 &commit_max_age, qf_names, &qfmt) == 0) {
1641 handle_quota_files(s, qf_names, &qfmt);
1645 SWARN(silent, s, "jmacd-7: reiserfs_fill_super: resize option "
1646 "for remount only");
1650 /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1651 if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1653 /* try new format (64-th 1k block), which can contain reiserfs super block */
1654 else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1656 "sh-2021: reiserfs_fill_super: can not find reiserfs on %s",
1657 reiserfs_bdevname(s));
1661 rs = SB_DISK_SUPER_BLOCK(s);
1662 /* Let's do basic sanity check to verify that underlying device is not
1663 smaller than the filesystem. If the check fails then abort and scream,
1664 because bad stuff will happen otherwise. */
1665 if (s->s_bdev && s->s_bdev->bd_inode
1666 && i_size_read(s->s_bdev->bd_inode) <
1667 sb_block_count(rs) * sb_blocksize(rs)) {
1669 "Filesystem on %s cannot be mounted because it is bigger than the device",
1670 reiserfs_bdevname(s));
1672 "You may need to run fsck or increase size of your LVM partition");
1674 "Or may be you forgot to reboot after fdisk when it told you to");
1678 sbi->s_mount_state = SB_REISERFS_STATE(s);
1679 sbi->s_mount_state = REISERFS_VALID_FS;
1681 if ((errval = reiserfs_init_bitmap_cache(s))) {
1683 "jmacd-8: reiserfs_fill_super: unable to read bitmap");
1687 #ifdef CONFIG_REISERFS_CHECK
1688 SWARN(silent, s, "CONFIG_REISERFS_CHECK is set ON");
1689 SWARN(silent, s, "- it is slow mode for debugging.");
1692 /* make data=ordered the default */
1693 if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1694 !reiserfs_data_writeback(s)) {
1695 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
1698 if (reiserfs_data_log(s)) {
1699 reiserfs_info(s, "using journaled data mode\n");
1700 } else if (reiserfs_data_ordered(s)) {
1701 reiserfs_info(s, "using ordered data mode\n");
1703 reiserfs_info(s, "using writeback data mode\n");
1705 if (reiserfs_barrier_flush(s)) {
1706 printk("reiserfs: using flush barriers\n");
1708 // set_device_ro(s->s_dev, 1) ;
1709 if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1711 "sh-2022: reiserfs_fill_super: unable to initialize journal space");
1714 jinit_done = 1; /* once this is set, journal_release must be called
1715 ** if we error out of the mount
1718 if (reread_meta_blocks(s)) {
1720 "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init");
1727 if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1729 "clm-7000: Detected readonly device, marking FS readonly");
1730 s->s_flags |= MS_RDONLY;
1732 args.objectid = REISERFS_ROOT_OBJECTID;
1733 args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
1735 iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
1736 reiserfs_init_locked_inode, (void *)(&args));
1739 "jmacd-10: reiserfs_fill_super: get root inode failed");
1743 if (root_inode->i_state & I_NEW) {
1744 reiserfs_read_locked_inode(root_inode, &args);
1745 unlock_new_inode(root_inode);
1748 s->s_root = d_alloc_root(root_inode);
1753 // define and initialize hash function
1754 sbi->s_hash_function = hash_function(s);
1755 if (sbi->s_hash_function == NULL) {
1761 if (is_reiserfs_3_5(rs)
1762 || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
1763 set_bit(REISERFS_3_5, &(sbi->s_properties));
1764 else if (old_format)
1765 set_bit(REISERFS_OLD_FORMAT, &(sbi->s_properties));
1767 set_bit(REISERFS_3_6, &(sbi->s_properties));
1769 if (!(s->s_flags & MS_RDONLY)) {
1771 errval = journal_begin(&th, s, 1);
1777 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1779 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1780 set_sb_fs_state(rs, 0);
1782 /* Clear out s_bmap_nr if it would wrap. We can handle this
1783 * case, but older revisions can't. This will cause the
1784 * file system to fail mount on those older implementations,
1785 * avoiding corruption. -jeffm */
1786 if (bmap_would_wrap(reiserfs_bmap_count(s)) &&
1787 sb_bmap_nr(rs) != 0) {
1788 reiserfs_warning(s, "super-2030: This file system "
1789 "claims to use %u bitmap blocks in "
1790 "its super block, but requires %u. "
1791 "Clearing to zero.", sb_bmap_nr(rs),
1792 reiserfs_bmap_count(s));
1794 set_sb_bmap_nr(rs, 0);
1797 if (old_format_only(s)) {
1798 /* filesystem of format 3.5 either with standard or non-standard
1800 if (convert_reiserfs(s)) {
1801 /* and -o conv is given */
1804 "converting 3.5 filesystem to the 3.6 format");
1806 if (is_reiserfs_3_5(rs))
1807 /* put magic string of 3.6 format. 2.2 will not be able to
1808 mount this filesystem anymore */
1809 memcpy(rs->s_v1.s_magic,
1810 reiserfs_3_6_magic_string,
1812 (reiserfs_3_6_magic_string));
1814 set_sb_version(rs, REISERFS_VERSION_2);
1815 reiserfs_convert_objectid_map_v1(s);
1816 set_bit(REISERFS_3_6, &(sbi->s_properties));
1817 clear_bit(REISERFS_3_5, &(sbi->s_properties));
1818 } else if (!silent) {
1819 reiserfs_info(s, "using 3.5.x disk format\n");
1823 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1824 errval = journal_end(&th, s, 1);
1831 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1837 /* look for files which were to be removed in previous session */
1838 finish_unfinished(s);
1840 if (old_format_only(s) && !silent) {
1841 reiserfs_info(s, "using 3.5.x disk format\n");
1844 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1850 // mark hash in super block: it could be unset. overwrite should be ok
1851 set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
1855 reiserfs_proc_info_init(s);
1857 init_waitqueue_head(&(sbi->s_wait));
1858 spin_lock_init(&sbi->bitmap_lock);
1863 if (jinit_done) { /* kill the commit thread, free journal ram */
1864 journal_release_error(NULL, s);
1867 reiserfs_free_bitmap_cache(s);
1868 if (SB_BUFFER_WITH_SB(s))
1869 brelse(SB_BUFFER_WITH_SB(s));
1873 for (j = 0; j < MAXQUOTAS; j++)
1879 s->s_fs_info = NULL;
1883 static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1885 struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
1887 buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
1888 buf->f_bfree = sb_free_blocks(rs);
1889 buf->f_bavail = buf->f_bfree;
1890 buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
1891 buf->f_bsize = dentry->d_sb->s_blocksize;
1892 /* changed to accommodate gcc folks. */
1893 buf->f_type = REISERFS_SUPER_MAGIC;
1898 static int reiserfs_dquot_initialize(struct inode *inode, int type)
1900 struct reiserfs_transaction_handle th;
1903 /* We may create quota structure so we need to reserve enough blocks */
1904 reiserfs_write_lock(inode->i_sb);
1906 journal_begin(&th, inode->i_sb,
1907 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1910 ret = dquot_initialize(inode, type);
1912 journal_end(&th, inode->i_sb,
1913 2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1917 reiserfs_write_unlock(inode->i_sb);
1921 static int reiserfs_dquot_drop(struct inode *inode)
1923 struct reiserfs_transaction_handle th;
1926 /* We may delete quota structure so we need to reserve enough blocks */
1927 reiserfs_write_lock(inode->i_sb);
1929 journal_begin(&th, inode->i_sb,
1930 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1933 * We call dquot_drop() anyway to at least release references
1934 * to quota structures so that umount does not hang.
1939 ret = dquot_drop(inode);
1941 journal_end(&th, inode->i_sb,
1942 2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
1946 reiserfs_write_unlock(inode->i_sb);
1950 static int reiserfs_write_dquot(struct dquot *dquot)
1952 struct reiserfs_transaction_handle th;
1955 reiserfs_write_lock(dquot->dq_sb);
1957 journal_begin(&th, dquot->dq_sb,
1958 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1961 ret = dquot_commit(dquot);
1963 journal_end(&th, dquot->dq_sb,
1964 REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
1968 reiserfs_write_unlock(dquot->dq_sb);
1972 static int reiserfs_acquire_dquot(struct dquot *dquot)
1974 struct reiserfs_transaction_handle th;
1977 reiserfs_write_lock(dquot->dq_sb);
1979 journal_begin(&th, dquot->dq_sb,
1980 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1983 ret = dquot_acquire(dquot);
1985 journal_end(&th, dquot->dq_sb,
1986 REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
1990 reiserfs_write_unlock(dquot->dq_sb);
1994 static int reiserfs_release_dquot(struct dquot *dquot)
1996 struct reiserfs_transaction_handle th;
1999 reiserfs_write_lock(dquot->dq_sb);
2001 journal_begin(&th, dquot->dq_sb,
2002 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2004 /* Release dquot anyway to avoid endless cycle in dqput() */
2005 dquot_release(dquot);
2008 ret = dquot_release(dquot);
2010 journal_end(&th, dquot->dq_sb,
2011 REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2015 reiserfs_write_unlock(dquot->dq_sb);
2019 static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
2021 /* Are we journaling quotas? */
2022 if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2023 REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2024 dquot_mark_dquot_dirty(dquot);
2025 return reiserfs_write_dquot(dquot);
2027 return dquot_mark_dquot_dirty(dquot);
2030 static int reiserfs_write_info(struct super_block *sb, int type)
2032 struct reiserfs_transaction_handle th;
2035 /* Data block + inode block */
2036 reiserfs_write_lock(sb);
2037 ret = journal_begin(&th, sb, 2);
2040 ret = dquot_commit_info(sb, type);
2041 err = journal_end(&th, sb, 2);
2045 reiserfs_write_unlock(sb);
2050 * Turn on quotas during mount time - we need to find the quota file and such...
2052 static int reiserfs_quota_on_mount(struct super_block *sb, int type)
2054 return vfs_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
2055 REISERFS_SB(sb)->s_jquota_fmt, type);
2059 * Standard function to be called on quota_on
2061 static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
2062 char *path, int remount)
2065 struct nameidata nd;
2066 struct inode *inode;
2067 struct reiserfs_transaction_handle th;
2069 if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
2071 /* No more checks needed? Path and format_id are bogus anyway... */
2073 return vfs_quota_on(sb, type, format_id, path, 1);
2074 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2077 /* Quotafile not on the same filesystem? */
2078 if (nd.path.mnt->mnt_sb != sb) {
2082 inode = nd.path.dentry->d_inode;
2083 /* We must not pack tails for quota files on reiserfs for quota IO to work */
2084 if (!(REISERFS_I(inode)->i_flags & i_nopack_mask)) {
2085 err = reiserfs_unpack(inode, NULL);
2087 reiserfs_warning(sb,
2088 "reiserfs: Unpacking tail of quota file failed"
2089 " (%d). Cannot turn on quotas.", err);
2093 mark_inode_dirty(inode);
2095 /* Journaling quota? */
2096 if (REISERFS_SB(sb)->s_qf_names[type]) {
2097 /* Quotafile not of fs root? */
2098 if (nd.path.dentry->d_parent->d_inode != sb->s_root->d_inode)
2099 reiserfs_warning(sb,
2100 "reiserfs: Quota file not on filesystem root. "
2101 "Journalled quota will not work.");
2105 * When we journal data on quota file, we have to flush journal to see
2106 * all updates to the file when we bypass pagecache...
2108 if (reiserfs_file_data_log(inode)) {
2109 /* Just start temporary transaction and finish it */
2110 err = journal_begin(&th, sb, 1);
2113 err = journal_end_sync(&th, sb, 1);
2118 return vfs_quota_on(sb, type, format_id, path, 0);
2121 /* Read data from quotafile - avoid pagecache and such because we cannot afford
2122 * acquiring the locks... As quota files are never truncated and quota code
2123 * itself serializes the operations (and noone else should touch the files)
2124 * we don't have to be afraid of races */
2125 static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2126 size_t len, loff_t off)
2128 struct inode *inode = sb_dqopt(sb)->files[type];
2129 unsigned long blk = off >> sb->s_blocksize_bits;
2130 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2132 struct buffer_head tmp_bh, *bh;
2133 loff_t i_size = i_size_read(inode);
2137 if (off + len > i_size)
2140 while (toread > 0) {
2142 sb->s_blocksize - offset <
2143 toread ? sb->s_blocksize - offset : toread;
2145 /* Quota files are without tails so we can safely use this function */
2146 reiserfs_write_lock(sb);
2147 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2148 reiserfs_write_unlock(sb);
2151 if (!buffer_mapped(&tmp_bh)) /* A hole? */
2152 memset(data, 0, tocopy);
2154 bh = sb_bread(sb, tmp_bh.b_blocknr);
2157 memcpy(data, bh->b_data + offset, tocopy);
2168 /* Write to quotafile (we know the transaction is already started and has
2169 * enough credits) */
2170 static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2171 const char *data, size_t len, loff_t off)
2173 struct inode *inode = sb_dqopt(sb)->files[type];
2174 unsigned long blk = off >> sb->s_blocksize_bits;
2175 int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2176 int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2177 size_t towrite = len;
2178 struct buffer_head tmp_bh, *bh;
2180 if (!current->journal_info) {
2181 printk(KERN_WARNING "reiserfs: Quota write (off=%Lu, len=%Lu)"
2182 " cancelled because transaction is not started.\n",
2183 (unsigned long long)off, (unsigned long long)len);
2186 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2187 while (towrite > 0) {
2188 tocopy = sb->s_blocksize - offset < towrite ?
2189 sb->s_blocksize - offset : towrite;
2191 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2194 if (offset || tocopy != sb->s_blocksize)
2195 bh = sb_bread(sb, tmp_bh.b_blocknr);
2197 bh = sb_getblk(sb, tmp_bh.b_blocknr);
2203 memcpy(bh->b_data + offset, data, tocopy);
2204 flush_dcache_page(bh->b_page);
2205 set_buffer_uptodate(bh);
2207 reiserfs_prepare_for_journal(sb, bh, 1);
2208 journal_mark_dirty(current->journal_info, sb, bh);
2210 reiserfs_add_ordered_list(inode, bh);
2218 if (len == towrite) {
2219 mutex_unlock(&inode->i_mutex);
2222 if (inode->i_size < off + len - towrite)
2223 i_size_write(inode, off + len - towrite);
2225 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2226 mark_inode_dirty(inode);
2227 mutex_unlock(&inode->i_mutex);
2228 return len - towrite;
2233 static int get_super_block(struct file_system_type *fs_type,
2234 int flags, const char *dev_name,
2235 void *data, struct vfsmount *mnt)
2237 return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super,
2241 static int __init init_reiserfs_fs(void)
2245 if ((ret = init_inodecache())) {
2249 if ((ret = reiserfs_xattr_register_handlers()))
2250 goto failed_reiserfs_xattr_register_handlers;
2252 reiserfs_proc_info_global_init();
2253 reiserfs_proc_register_global("version",
2254 reiserfs_global_version_in_proc);
2256 ret = register_filesystem(&reiserfs_fs_type);
2262 reiserfs_xattr_unregister_handlers();
2264 failed_reiserfs_xattr_register_handlers:
2265 reiserfs_proc_unregister_global("version");
2266 reiserfs_proc_info_global_done();
2267 destroy_inodecache();
2272 static void __exit exit_reiserfs_fs(void)
2274 reiserfs_xattr_unregister_handlers();
2275 reiserfs_proc_unregister_global("version");
2276 reiserfs_proc_info_global_done();
2277 unregister_filesystem(&reiserfs_fs_type);
2278 destroy_inodecache();
2281 struct file_system_type reiserfs_fs_type = {
2282 .owner = THIS_MODULE,
2284 .get_sb = get_super_block,
2285 .kill_sb = reiserfs_kill_sb,
2286 .fs_flags = FS_REQUIRES_DEV,
2289 MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2290 MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2291 MODULE_LICENSE("GPL");
2293 module_init(init_reiserfs_fs);
2294 module_exit(exit_reiserfs_fs);