Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / reiserfs / super.c
1 /*
2  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3  *
4  * Trivial changes by Alan Cox to add the LFS fixes
5  *
6  * Trivial Changes:
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.
10  *
11  * NO WARRANTY
12  */
13
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/smp_lock.h>
22 #include <linux/init.h>
23 #include <linux/blkdev.h>
24 #include <linux/buffer_head.h>
25 #include <linux/vfs.h>
26 #include <linux/namespace.h>
27 #include <linux/mount.h>
28 #include <linux/namei.h>
29 #include <linux/quotaops.h>
30
31 struct file_system_type reiserfs_fs_type;
32
33 static const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
34 static const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
35 static const char reiserfs_jr_magic_string[] = REISER2FS_JR_SUPER_MAGIC_STRING;
36
37 int is_reiserfs_3_5(struct reiserfs_super_block *rs)
38 {
39         return !strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
40                         strlen(reiserfs_3_5_magic_string));
41 }
42
43 int is_reiserfs_3_6(struct reiserfs_super_block *rs)
44 {
45         return !strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
46                         strlen(reiserfs_3_6_magic_string));
47 }
48
49 int is_reiserfs_jr(struct reiserfs_super_block *rs)
50 {
51         return !strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
52                         strlen(reiserfs_jr_magic_string));
53 }
54
55 static int is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
56 {
57         return (is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
58                 is_reiserfs_jr(rs));
59 }
60
61 static int reiserfs_remount(struct super_block *s, int *flags, char *data);
62 static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf);
63
64 static int reiserfs_sync_fs(struct super_block *s, int wait)
65 {
66         if (!(s->s_flags & MS_RDONLY)) {
67                 struct reiserfs_transaction_handle th;
68                 reiserfs_write_lock(s);
69                 if (!journal_begin(&th, s, 1))
70                         if (!journal_end_sync(&th, s, 1))
71                                 reiserfs_flush_old_commits(s);
72                 s->s_dirt = 0;  /* Even if it's not true.
73                                  * We'll loop forever in sync_supers otherwise */
74                 reiserfs_write_unlock(s);
75         } else {
76                 s->s_dirt = 0;
77         }
78         return 0;
79 }
80
81 static void reiserfs_write_super(struct super_block *s)
82 {
83         reiserfs_sync_fs(s, 1);
84 }
85
86 static void reiserfs_write_super_lockfs(struct super_block *s)
87 {
88         struct reiserfs_transaction_handle th;
89         reiserfs_write_lock(s);
90         if (!(s->s_flags & MS_RDONLY)) {
91                 int err = journal_begin(&th, s, 1);
92                 if (err) {
93                         reiserfs_block_writes(&th);
94                 } else {
95                         reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
96                                                      1);
97                         journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
98                         reiserfs_block_writes(&th);
99                         journal_end_sync(&th, s, 1);
100                 }
101         }
102         s->s_dirt = 0;
103         reiserfs_write_unlock(s);
104 }
105
106 static void reiserfs_unlockfs(struct super_block *s)
107 {
108         reiserfs_allow_writes(s);
109 }
110
111 extern const struct in_core_key MAX_IN_CORE_KEY;
112
113 /* this is used to delete "save link" when there are no items of a
114    file it points to. It can either happen if unlink is completed but
115    "save unlink" removal, or if file has both unlink and truncate
116    pending and as unlink completes first (because key of "save link"
117    protecting unlink is bigger that a key lf "save link" which
118    protects truncate), so there left no items to make truncate
119    completion on */
120 static int remove_save_link_only(struct super_block *s,
121                                  struct reiserfs_key *key, int oid_free)
122 {
123         struct reiserfs_transaction_handle th;
124         int err;
125
126         /* we are going to do one balancing */
127         err = journal_begin(&th, s, JOURNAL_PER_BALANCE_CNT);
128         if (err)
129                 return err;
130
131         reiserfs_delete_solid_item(&th, NULL, key);
132         if (oid_free)
133                 /* removals are protected by direct items */
134                 reiserfs_release_objectid(&th, le32_to_cpu(key->k_objectid));
135
136         return journal_end(&th, s, JOURNAL_PER_BALANCE_CNT);
137 }
138
139 #ifdef CONFIG_QUOTA
140 static int reiserfs_quota_on_mount(struct super_block *, int);
141 #endif
142
143 /* look for uncompleted unlinks and truncates and complete them */
144 static int finish_unfinished(struct super_block *s)
145 {
146         INITIALIZE_PATH(path);
147         struct cpu_key max_cpu_key, obj_key;
148         struct reiserfs_key save_link_key;
149         int retval = 0;
150         struct item_head *ih;
151         struct buffer_head *bh;
152         int item_pos;
153         char *item;
154         int done;
155         struct inode *inode;
156         int truncate;
157 #ifdef CONFIG_QUOTA
158         int i;
159         int ms_active_set;
160 #endif
161
162         /* compose key to look for "save" links */
163         max_cpu_key.version = KEY_FORMAT_3_5;
164         max_cpu_key.on_disk_key.k_dir_id = ~0U;
165         max_cpu_key.on_disk_key.k_objectid = ~0U;
166         set_cpu_key_k_offset(&max_cpu_key, ~0U);
167         max_cpu_key.key_length = 3;
168
169 #ifdef CONFIG_QUOTA
170         /* Needed for iput() to work correctly and not trash data */
171         if (s->s_flags & MS_ACTIVE) {
172                 ms_active_set = 0;
173         } else {
174                 ms_active_set = 1;
175                 s->s_flags |= MS_ACTIVE;
176         }
177         /* Turn on quotas so that they are updated correctly */
178         for (i = 0; i < MAXQUOTAS; i++) {
179                 if (REISERFS_SB(s)->s_qf_names[i]) {
180                         int ret = reiserfs_quota_on_mount(s, i);
181                         if (ret < 0)
182                                 reiserfs_warning(s,
183                                                  "reiserfs: cannot turn on journalled quota: error %d",
184                                                  ret);
185                 }
186         }
187 #endif
188
189         done = 0;
190         REISERFS_SB(s)->s_is_unlinked_ok = 1;
191         while (!retval) {
192                 retval = search_item(s, &max_cpu_key, &path);
193                 if (retval != ITEM_NOT_FOUND) {
194                         reiserfs_warning(s,
195                                          "vs-2140: finish_unfinished: search_by_key returned %d",
196                                          retval);
197                         break;
198                 }
199
200                 bh = get_last_bh(&path);
201                 item_pos = get_item_pos(&path);
202                 if (item_pos != B_NR_ITEMS(bh)) {
203                         reiserfs_warning(s,
204                                          "vs-2060: finish_unfinished: wrong position found");
205                         break;
206                 }
207                 item_pos--;
208                 ih = B_N_PITEM_HEAD(bh, item_pos);
209
210                 if (le32_to_cpu(ih->ih_key.k_dir_id) != MAX_KEY_OBJECTID)
211                         /* there are no "save" links anymore */
212                         break;
213
214                 save_link_key = ih->ih_key;
215                 if (is_indirect_le_ih(ih))
216                         truncate = 1;
217                 else
218                         truncate = 0;
219
220                 /* reiserfs_iget needs k_dirid and k_objectid only */
221                 item = B_I_PITEM(bh, ih);
222                 obj_key.on_disk_key.k_dir_id = le32_to_cpu(*(__le32 *) item);
223                 obj_key.on_disk_key.k_objectid =
224                     le32_to_cpu(ih->ih_key.k_objectid);
225                 obj_key.on_disk_key.k_offset = 0;
226                 obj_key.on_disk_key.k_type = 0;
227
228                 pathrelse(&path);
229
230                 inode = reiserfs_iget(s, &obj_key);
231                 if (!inode) {
232                         /* the unlink almost completed, it just did not manage to remove
233                            "save" link and release objectid */
234                         reiserfs_warning(s,
235                                          "vs-2180: finish_unfinished: iget failed for %K",
236                                          &obj_key);
237                         retval = remove_save_link_only(s, &save_link_key, 1);
238                         continue;
239                 }
240
241                 if (!truncate && inode->i_nlink) {
242                         /* file is not unlinked */
243                         reiserfs_warning(s,
244                                          "vs-2185: finish_unfinished: file %K is not unlinked",
245                                          &obj_key);
246                         retval = remove_save_link_only(s, &save_link_key, 0);
247                         continue;
248                 }
249                 DQUOT_INIT(inode);
250
251                 if (truncate && S_ISDIR(inode->i_mode)) {
252                         /* We got a truncate request for a dir which is impossible.
253                            The only imaginable way is to execute unfinished truncate request
254                            then boot into old kernel, remove the file and create dir with
255                            the same key. */
256                         reiserfs_warning(s,
257                                          "green-2101: impossible truncate on a directory %k. Please report",
258                                          INODE_PKEY(inode));
259                         retval = remove_save_link_only(s, &save_link_key, 0);
260                         truncate = 0;
261                         iput(inode);
262                         continue;
263                 }
264
265                 if (truncate) {
266                         REISERFS_I(inode)->i_flags |=
267                             i_link_saved_truncate_mask;
268                         /* not completed truncate found. New size was committed together
269                            with "save" link */
270                         reiserfs_info(s, "Truncating %k to %Ld ..",
271                                       INODE_PKEY(inode), inode->i_size);
272                         reiserfs_truncate_file(inode,
273                                                0
274                                                /*don't update modification time */
275                                                );
276                         retval = remove_save_link(inode, truncate);
277                 } else {
278                         REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
279                         /* not completed unlink (rmdir) found */
280                         reiserfs_info(s, "Removing %k..", INODE_PKEY(inode));
281                         /* removal gets completed in iput */
282                         retval = 0;
283                 }
284
285                 iput(inode);
286                 printk("done\n");
287                 done++;
288         }
289         REISERFS_SB(s)->s_is_unlinked_ok = 0;
290
291 #ifdef CONFIG_QUOTA
292         /* Turn quotas off */
293         for (i = 0; i < MAXQUOTAS; i++) {
294                 if (sb_dqopt(s)->files[i])
295                         vfs_quota_off_mount(s, i);
296         }
297         if (ms_active_set)
298                 /* Restore the flag back */
299                 s->s_flags &= ~MS_ACTIVE;
300 #endif
301         pathrelse(&path);
302         if (done)
303                 reiserfs_info(s, "There were %d uncompleted unlinks/truncates. "
304                               "Completed\n", done);
305         return retval;
306 }
307
308 /* to protect file being unlinked from getting lost we "safe" link files
309    being unlinked. This link will be deleted in the same transaction with last
310    item of file. mounting the filesytem we scan all these links and remove
311    files which almost got lost */
312 void add_save_link(struct reiserfs_transaction_handle *th,
313                    struct inode *inode, int truncate)
314 {
315         INITIALIZE_PATH(path);
316         int retval;
317         struct cpu_key key;
318         struct item_head ih;
319         __le32 link;
320
321         BUG_ON(!th->t_trans_id);
322
323         /* file can only get one "save link" of each kind */
324         RFALSE(truncate &&
325                (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask),
326                "saved link already exists for truncated inode %lx",
327                (long)inode->i_ino);
328         RFALSE(!truncate &&
329                (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask),
330                "saved link already exists for unlinked inode %lx",
331                (long)inode->i_ino);
332
333         /* setup key of "save" link */
334         key.version = KEY_FORMAT_3_5;
335         key.on_disk_key.k_dir_id = MAX_KEY_OBJECTID;
336         key.on_disk_key.k_objectid = inode->i_ino;
337         if (!truncate) {
338                 /* unlink, rmdir, rename */
339                 set_cpu_key_k_offset(&key, 1 + inode->i_sb->s_blocksize);
340                 set_cpu_key_k_type(&key, TYPE_DIRECT);
341
342                 /* item head of "safe" link */
343                 make_le_item_head(&ih, &key, key.version,
344                                   1 + inode->i_sb->s_blocksize, TYPE_DIRECT,
345                                   4 /*length */ , 0xffff /*free space */ );
346         } else {
347                 /* truncate */
348                 if (S_ISDIR(inode->i_mode))
349                         reiserfs_warning(inode->i_sb,
350                                          "green-2102: Adding a truncate savelink for a directory %k! Please report",
351                                          INODE_PKEY(inode));
352                 set_cpu_key_k_offset(&key, 1);
353                 set_cpu_key_k_type(&key, TYPE_INDIRECT);
354
355                 /* item head of "safe" link */
356                 make_le_item_head(&ih, &key, key.version, 1, TYPE_INDIRECT,
357                                   4 /*length */ , 0 /*free space */ );
358         }
359         key.key_length = 3;
360
361         /* look for its place in the tree */
362         retval = search_item(inode->i_sb, &key, &path);
363         if (retval != ITEM_NOT_FOUND) {
364                 if (retval != -ENOSPC)
365                         reiserfs_warning(inode->i_sb, "vs-2100: add_save_link:"
366                                          "search_by_key (%K) returned %d", &key,
367                                          retval);
368                 pathrelse(&path);
369                 return;
370         }
371
372         /* body of "save" link */
373         link = INODE_PKEY(inode)->k_dir_id;
374
375         /* put "save" link inot tree, don't charge quota to anyone */
376         retval =
377             reiserfs_insert_item(th, &path, &key, &ih, NULL, (char *)&link);
378         if (retval) {
379                 if (retval != -ENOSPC)
380                         reiserfs_warning(inode->i_sb,
381                                          "vs-2120: add_save_link: insert_item returned %d",
382                                          retval);
383         } else {
384                 if (truncate)
385                         REISERFS_I(inode)->i_flags |=
386                             i_link_saved_truncate_mask;
387                 else
388                         REISERFS_I(inode)->i_flags |= i_link_saved_unlink_mask;
389         }
390 }
391
392 /* this opens transaction unlike add_save_link */
393 int remove_save_link(struct inode *inode, int truncate)
394 {
395         struct reiserfs_transaction_handle th;
396         struct reiserfs_key key;
397         int err;
398
399         /* we are going to do one balancing only */
400         err = journal_begin(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
401         if (err)
402                 return err;
403
404         /* setup key of "save" link */
405         key.k_dir_id = cpu_to_le32(MAX_KEY_OBJECTID);
406         key.k_objectid = INODE_PKEY(inode)->k_objectid;
407         if (!truncate) {
408                 /* unlink, rmdir, rename */
409                 set_le_key_k_offset(KEY_FORMAT_3_5, &key,
410                                     1 + inode->i_sb->s_blocksize);
411                 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_DIRECT);
412         } else {
413                 /* truncate */
414                 set_le_key_k_offset(KEY_FORMAT_3_5, &key, 1);
415                 set_le_key_k_type(KEY_FORMAT_3_5, &key, TYPE_INDIRECT);
416         }
417
418         if ((truncate &&
419              (REISERFS_I(inode)->i_flags & i_link_saved_truncate_mask)) ||
420             (!truncate &&
421              (REISERFS_I(inode)->i_flags & i_link_saved_unlink_mask)))
422                 /* don't take quota bytes from anywhere */
423                 reiserfs_delete_solid_item(&th, NULL, &key);
424         if (!truncate) {
425                 reiserfs_release_objectid(&th, inode->i_ino);
426                 REISERFS_I(inode)->i_flags &= ~i_link_saved_unlink_mask;
427         } else
428                 REISERFS_I(inode)->i_flags &= ~i_link_saved_truncate_mask;
429
430         return journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT);
431 }
432
433 static void reiserfs_kill_sb(struct super_block *s)
434 {
435         if (REISERFS_SB(s)) {
436                 if (REISERFS_SB(s)->xattr_root) {
437                         d_invalidate(REISERFS_SB(s)->xattr_root);
438                         dput(REISERFS_SB(s)->xattr_root);
439                         REISERFS_SB(s)->xattr_root = NULL;
440                 }
441                 if (REISERFS_SB(s)->priv_root) {
442                         d_invalidate(REISERFS_SB(s)->priv_root);
443                         dput(REISERFS_SB(s)->priv_root);
444                         REISERFS_SB(s)->priv_root = NULL;
445                 }
446                 kill_block_super(s);
447         }
448 }
449
450 static void reiserfs_put_super(struct super_block *s)
451 {
452         int i;
453         struct reiserfs_transaction_handle th;
454         th.t_trans_id = 0;
455
456         /* change file system state to current state if it was mounted with read-write permissions */
457         if (!(s->s_flags & MS_RDONLY)) {
458                 if (!journal_begin(&th, s, 10)) {
459                         reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
460                                                      1);
461                         set_sb_umount_state(SB_DISK_SUPER_BLOCK(s),
462                                             REISERFS_SB(s)->s_mount_state);
463                         journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
464                 }
465         }
466
467         /* note, journal_release checks for readonly mount, and can decide not
468          ** to do a journal_end
469          */
470         journal_release(&th, s);
471
472         for (i = 0; i < SB_BMAP_NR(s); i++)
473                 brelse(SB_AP_BITMAP(s)[i].bh);
474
475         vfree(SB_AP_BITMAP(s));
476
477         brelse(SB_BUFFER_WITH_SB(s));
478
479         print_statistics(s);
480
481         if (REISERFS_SB(s)->reserved_blocks != 0) {
482                 reiserfs_warning(s,
483                                  "green-2005: reiserfs_put_super: reserved blocks left %d",
484                                  REISERFS_SB(s)->reserved_blocks);
485         }
486
487         reiserfs_proc_info_done(s);
488
489         kfree(s->s_fs_info);
490         s->s_fs_info = NULL;
491
492         return;
493 }
494
495 static kmem_cache_t *reiserfs_inode_cachep;
496
497 static struct inode *reiserfs_alloc_inode(struct super_block *sb)
498 {
499         struct reiserfs_inode_info *ei;
500         ei = (struct reiserfs_inode_info *)
501             kmem_cache_alloc(reiserfs_inode_cachep, SLAB_KERNEL);
502         if (!ei)
503                 return NULL;
504         return &ei->vfs_inode;
505 }
506
507 static void reiserfs_destroy_inode(struct inode *inode)
508 {
509         kmem_cache_free(reiserfs_inode_cachep, REISERFS_I(inode));
510 }
511
512 static void init_once(void *foo, kmem_cache_t * cachep, unsigned long flags)
513 {
514         struct reiserfs_inode_info *ei = (struct reiserfs_inode_info *)foo;
515
516         if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) ==
517             SLAB_CTOR_CONSTRUCTOR) {
518                 INIT_LIST_HEAD(&ei->i_prealloc_list);
519                 inode_init_once(&ei->vfs_inode);
520                 ei->i_acl_access = NULL;
521                 ei->i_acl_default = NULL;
522         }
523 }
524
525 static int init_inodecache(void)
526 {
527         reiserfs_inode_cachep = kmem_cache_create("reiser_inode_cache",
528                                                   sizeof(struct
529                                                          reiserfs_inode_info),
530                                                   0, (SLAB_RECLAIM_ACCOUNT|
531                                                         SLAB_MEM_SPREAD),
532                                                   init_once, NULL);
533         if (reiserfs_inode_cachep == NULL)
534                 return -ENOMEM;
535         return 0;
536 }
537
538 static void destroy_inodecache(void)
539 {
540         if (kmem_cache_destroy(reiserfs_inode_cachep))
541                 reiserfs_warning(NULL,
542                                  "reiserfs_inode_cache: not all structures were freed");
543 }
544
545 /* we don't mark inodes dirty, we just log them */
546 static void reiserfs_dirty_inode(struct inode *inode)
547 {
548         struct reiserfs_transaction_handle th;
549
550         int err = 0;
551         if (inode->i_sb->s_flags & MS_RDONLY) {
552                 reiserfs_warning(inode->i_sb,
553                                  "clm-6006: writing inode %lu on readonly FS",
554                                  inode->i_ino);
555                 return;
556         }
557         reiserfs_write_lock(inode->i_sb);
558
559         /* this is really only used for atime updates, so they don't have
560          ** to be included in O_SYNC or fsync
561          */
562         err = journal_begin(&th, inode->i_sb, 1);
563         if (err) {
564                 reiserfs_write_unlock(inode->i_sb);
565                 return;
566         }
567         reiserfs_update_sd(&th, inode);
568         journal_end(&th, inode->i_sb, 1);
569         reiserfs_write_unlock(inode->i_sb);
570 }
571
572 static void reiserfs_clear_inode(struct inode *inode)
573 {
574         struct posix_acl *acl;
575
576         acl = REISERFS_I(inode)->i_acl_access;
577         if (acl && !IS_ERR(acl))
578                 posix_acl_release(acl);
579         REISERFS_I(inode)->i_acl_access = NULL;
580
581         acl = REISERFS_I(inode)->i_acl_default;
582         if (acl && !IS_ERR(acl))
583                 posix_acl_release(acl);
584         REISERFS_I(inode)->i_acl_default = NULL;
585 }
586
587 #ifdef CONFIG_QUOTA
588 static ssize_t reiserfs_quota_write(struct super_block *, int, const char *,
589                                     size_t, loff_t);
590 static ssize_t reiserfs_quota_read(struct super_block *, int, char *, size_t,
591                                    loff_t);
592 #endif
593
594 static struct super_operations reiserfs_sops = {
595         .alloc_inode = reiserfs_alloc_inode,
596         .destroy_inode = reiserfs_destroy_inode,
597         .write_inode = reiserfs_write_inode,
598         .dirty_inode = reiserfs_dirty_inode,
599         .delete_inode = reiserfs_delete_inode,
600         .clear_inode = reiserfs_clear_inode,
601         .put_super = reiserfs_put_super,
602         .write_super = reiserfs_write_super,
603         .sync_fs = reiserfs_sync_fs,
604         .write_super_lockfs = reiserfs_write_super_lockfs,
605         .unlockfs = reiserfs_unlockfs,
606         .statfs = reiserfs_statfs,
607         .remount_fs = reiserfs_remount,
608 #ifdef CONFIG_QUOTA
609         .quota_read = reiserfs_quota_read,
610         .quota_write = reiserfs_quota_write,
611 #endif
612 };
613
614 #ifdef CONFIG_QUOTA
615 #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
616
617 static int reiserfs_dquot_initialize(struct inode *, int);
618 static int reiserfs_dquot_drop(struct inode *);
619 static int reiserfs_write_dquot(struct dquot *);
620 static int reiserfs_acquire_dquot(struct dquot *);
621 static int reiserfs_release_dquot(struct dquot *);
622 static int reiserfs_mark_dquot_dirty(struct dquot *);
623 static int reiserfs_write_info(struct super_block *, int);
624 static int reiserfs_quota_on(struct super_block *, int, int, char *);
625
626 static struct dquot_operations reiserfs_quota_operations = {
627         .initialize = reiserfs_dquot_initialize,
628         .drop = reiserfs_dquot_drop,
629         .alloc_space = dquot_alloc_space,
630         .alloc_inode = dquot_alloc_inode,
631         .free_space = dquot_free_space,
632         .free_inode = dquot_free_inode,
633         .transfer = dquot_transfer,
634         .write_dquot = reiserfs_write_dquot,
635         .acquire_dquot = reiserfs_acquire_dquot,
636         .release_dquot = reiserfs_release_dquot,
637         .mark_dirty = reiserfs_mark_dquot_dirty,
638         .write_info = reiserfs_write_info,
639 };
640
641 static struct quotactl_ops reiserfs_qctl_operations = {
642         .quota_on = reiserfs_quota_on,
643         .quota_off = vfs_quota_off,
644         .quota_sync = vfs_quota_sync,
645         .get_info = vfs_get_dqinfo,
646         .set_info = vfs_set_dqinfo,
647         .get_dqblk = vfs_get_dqblk,
648         .set_dqblk = vfs_set_dqblk,
649 };
650 #endif
651
652 static struct export_operations reiserfs_export_ops = {
653         .encode_fh = reiserfs_encode_fh,
654         .decode_fh = reiserfs_decode_fh,
655         .get_parent = reiserfs_get_parent,
656         .get_dentry = reiserfs_get_dentry,
657 };
658
659 /* this struct is used in reiserfs_getopt () for containing the value for those
660    mount options that have values rather than being toggles. */
661 typedef struct {
662         char *value;
663         int setmask;            /* bitmask which is to set on mount_options bitmask when this
664                                    value is found, 0 is no bits are to be changed. */
665         int clrmask;            /* bitmask which is to clear on mount_options bitmask when  this
666                                    value is found, 0 is no bits are to be changed. This is
667                                    applied BEFORE setmask */
668 } arg_desc_t;
669
670 /* Set this bit in arg_required to allow empty arguments */
671 #define REISERFS_OPT_ALLOWEMPTY 31
672
673 /* this struct is used in reiserfs_getopt() for describing the set of reiserfs
674    mount options */
675 typedef struct {
676         char *option_name;
677         int arg_required;       /* 0 if argument is not required, not 0 otherwise */
678         const arg_desc_t *values;       /* list of values accepted by an option */
679         int setmask;            /* bitmask which is to set on mount_options bitmask when this
680                                    value is found, 0 is no bits are to be changed. */
681         int clrmask;            /* bitmask which is to clear on mount_options bitmask when  this
682                                    value is found, 0 is no bits are to be changed. This is
683                                    applied BEFORE setmask */
684 } opt_desc_t;
685
686 /* possible values for -o data= */
687 static const arg_desc_t logging_mode[] = {
688         {"ordered", 1 << REISERFS_DATA_ORDERED,
689          (1 << REISERFS_DATA_LOG | 1 << REISERFS_DATA_WRITEBACK)},
690         {"journal", 1 << REISERFS_DATA_LOG,
691          (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_WRITEBACK)},
692         {"writeback", 1 << REISERFS_DATA_WRITEBACK,
693          (1 << REISERFS_DATA_ORDERED | 1 << REISERFS_DATA_LOG)},
694         {.value = NULL}
695 };
696
697 /* possible values for -o barrier= */
698 static const arg_desc_t barrier_mode[] = {
699         {"none", 1 << REISERFS_BARRIER_NONE, 1 << REISERFS_BARRIER_FLUSH},
700         {"flush", 1 << REISERFS_BARRIER_FLUSH, 1 << REISERFS_BARRIER_NONE},
701         {.value = NULL}
702 };
703
704 /* possible values for "-o block-allocator=" and bits which are to be set in
705    s_mount_opt of reiserfs specific part of in-core super block */
706 static const arg_desc_t balloc[] = {
707         {"noborder", 1 << REISERFS_NO_BORDER, 0},
708         {"border", 0, 1 << REISERFS_NO_BORDER},
709         {"no_unhashed_relocation", 1 << REISERFS_NO_UNHASHED_RELOCATION, 0},
710         {"hashed_relocation", 1 << REISERFS_HASHED_RELOCATION, 0},
711         {"test4", 1 << REISERFS_TEST4, 0},
712         {"notest4", 0, 1 << REISERFS_TEST4},
713         {NULL, 0, 0}
714 };
715
716 static const arg_desc_t tails[] = {
717         {"on", 1 << REISERFS_LARGETAIL, 1 << REISERFS_SMALLTAIL},
718         {"off", 0, (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
719         {"small", 1 << REISERFS_SMALLTAIL, 1 << REISERFS_LARGETAIL},
720         {NULL, 0, 0}
721 };
722
723 static const arg_desc_t error_actions[] = {
724         {"panic", 1 << REISERFS_ERROR_PANIC,
725          (1 << REISERFS_ERROR_RO | 1 << REISERFS_ERROR_CONTINUE)},
726         {"ro-remount", 1 << REISERFS_ERROR_RO,
727          (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_CONTINUE)},
728 #ifdef REISERFS_JOURNAL_ERROR_ALLOWS_NO_LOG
729         {"continue", 1 << REISERFS_ERROR_CONTINUE,
730          (1 << REISERFS_ERROR_PANIC | 1 << REISERFS_ERROR_RO)},
731 #endif
732         {NULL, 0, 0},
733 };
734
735 int reiserfs_default_io_size = 128 * 1024;      /* Default recommended I/O size is 128k.
736                                                    There might be broken applications that are
737                                                    confused by this. Use nolargeio mount option
738                                                    to get usual i/o size = PAGE_SIZE.
739                                                  */
740
741 /* proceed only one option from a list *cur - string containing of mount options
742    opts - array of options which are accepted
743    opt_arg - if option is found and requires an argument and if it is specifed
744    in the input - pointer to the argument is stored here
745    bit_flags - if option requires to set a certain bit - it is set here
746    return -1 if unknown option is found, opt->arg_required otherwise */
747 static int reiserfs_getopt(struct super_block *s, char **cur, opt_desc_t * opts,
748                            char **opt_arg, unsigned long *bit_flags)
749 {
750         char *p;
751         /* foo=bar, 
752            ^   ^  ^
753            |   |  +-- option_end
754            |   +-- arg_start
755            +-- option_start
756          */
757         const opt_desc_t *opt;
758         const arg_desc_t *arg;
759
760         p = *cur;
761
762         /* assume argument cannot contain commas */
763         *cur = strchr(p, ',');
764         if (*cur) {
765                 *(*cur) = '\0';
766                 (*cur)++;
767         }
768
769         if (!strncmp(p, "alloc=", 6)) {
770                 /* Ugly special case, probably we should redo options parser so that
771                    it can understand several arguments for some options, also so that
772                    it can fill several bitfields with option values. */
773                 if (reiserfs_parse_alloc_options(s, p + 6)) {
774                         return -1;
775                 } else {
776                         return 0;
777                 }
778         }
779
780         /* for every option in the list */
781         for (opt = opts; opt->option_name; opt++) {
782                 if (!strncmp(p, opt->option_name, strlen(opt->option_name))) {
783                         if (bit_flags) {
784                                 if (opt->clrmask ==
785                                     (1 << REISERFS_UNSUPPORTED_OPT))
786                                         reiserfs_warning(s, "%s not supported.",
787                                                          p);
788                                 else
789                                         *bit_flags &= ~opt->clrmask;
790                                 if (opt->setmask ==
791                                     (1 << REISERFS_UNSUPPORTED_OPT))
792                                         reiserfs_warning(s, "%s not supported.",
793                                                          p);
794                                 else
795                                         *bit_flags |= opt->setmask;
796                         }
797                         break;
798                 }
799         }
800         if (!opt->option_name) {
801                 reiserfs_warning(s, "unknown mount option \"%s\"", p);
802                 return -1;
803         }
804
805         p += strlen(opt->option_name);
806         switch (*p) {
807         case '=':
808                 if (!opt->arg_required) {
809                         reiserfs_warning(s,
810                                          "the option \"%s\" does not require an argument",
811                                          opt->option_name);
812                         return -1;
813                 }
814                 break;
815
816         case 0:
817                 if (opt->arg_required) {
818                         reiserfs_warning(s,
819                                          "the option \"%s\" requires an argument",
820                                          opt->option_name);
821                         return -1;
822                 }
823                 break;
824         default:
825                 reiserfs_warning(s, "head of option \"%s\" is only correct",
826                                  opt->option_name);
827                 return -1;
828         }
829
830         /* move to the argument, or to next option if argument is not required */
831         p++;
832
833         if (opt->arg_required
834             && !(opt->arg_required & (1 << REISERFS_OPT_ALLOWEMPTY))
835             && !strlen(p)) {
836                 /* this catches "option=," if not allowed */
837                 reiserfs_warning(s, "empty argument for \"%s\"",
838                                  opt->option_name);
839                 return -1;
840         }
841
842         if (!opt->values) {
843                 /* *=NULLopt_arg contains pointer to argument */
844                 *opt_arg = p;
845                 return opt->arg_required & ~(1 << REISERFS_OPT_ALLOWEMPTY);
846         }
847
848         /* values possible for this option are listed in opt->values */
849         for (arg = opt->values; arg->value; arg++) {
850                 if (!strcmp(p, arg->value)) {
851                         if (bit_flags) {
852                                 *bit_flags &= ~arg->clrmask;
853                                 *bit_flags |= arg->setmask;
854                         }
855                         return opt->arg_required;
856                 }
857         }
858
859         reiserfs_warning(s, "bad value \"%s\" for option \"%s\"", p,
860                          opt->option_name);
861         return -1;
862 }
863
864 /* returns 0 if something is wrong in option string, 1 - otherwise */
865 static int reiserfs_parse_options(struct super_block *s, char *options, /* string given via mount's -o */
866                                   unsigned long *mount_options,
867                                   /* after the parsing phase, contains the
868                                      collection of bitflags defining what
869                                      mount options were selected. */
870                                   unsigned long *blocks,        /* strtol-ed from NNN of resize=NNN */
871                                   char **jdev_name,
872                                   unsigned int *commit_max_age)
873 {
874         int c;
875         char *arg = NULL;
876         char *pos;
877         opt_desc_t opts[] = {
878                 /* Compatibility stuff, so that -o notail for old setups still work */
879                 {"tails",.arg_required = 't',.values = tails},
880                 {"notail",.clrmask =
881                  (1 << REISERFS_LARGETAIL) | (1 << REISERFS_SMALLTAIL)},
882                 {"conv",.setmask = 1 << REISERFS_CONVERT},
883                 {"attrs",.setmask = 1 << REISERFS_ATTRS},
884                 {"noattrs",.clrmask = 1 << REISERFS_ATTRS},
885 #ifdef CONFIG_REISERFS_FS_XATTR
886                 {"user_xattr",.setmask = 1 << REISERFS_XATTRS_USER},
887                 {"nouser_xattr",.clrmask = 1 << REISERFS_XATTRS_USER},
888 #else
889                 {"user_xattr",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
890                 {"nouser_xattr",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
891 #endif
892 #ifndef CONFIG_INOXID_NONE
893                 {"tagxid",.setmask = 1 << REISERFS_TAGXID},
894 #endif
895 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
896                 {"acl",.setmask = 1 << REISERFS_POSIXACL},
897                 {"noacl",.clrmask = 1 << REISERFS_POSIXACL},
898 #else
899                 {"acl",.setmask = 1 << REISERFS_UNSUPPORTED_OPT},
900                 {"noacl",.clrmask = 1 << REISERFS_UNSUPPORTED_OPT},
901 #endif
902                 {.option_name = "nolog"},
903                 {"replayonly",.setmask = 1 << REPLAYONLY},
904                 {"block-allocator",.arg_required = 'a',.values = balloc},
905                 {"data",.arg_required = 'd',.values = logging_mode},
906                 {"barrier",.arg_required = 'b',.values = barrier_mode},
907                 {"resize",.arg_required = 'r',.values = NULL},
908                 {"jdev",.arg_required = 'j',.values = NULL},
909                 {"nolargeio",.arg_required = 'w',.values = NULL},
910                 {"commit",.arg_required = 'c',.values = NULL},
911                 {"usrquota",.setmask = 1 << REISERFS_QUOTA},
912                 {"grpquota",.setmask = 1 << REISERFS_QUOTA},
913                 {"noquota",.clrmask = 1 << REISERFS_QUOTA},
914                 {"errors",.arg_required = 'e',.values = error_actions},
915                 {"usrjquota",.arg_required =
916                  'u' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
917                 {"grpjquota",.arg_required =
918                  'g' | (1 << REISERFS_OPT_ALLOWEMPTY),.values = NULL},
919                 {"jqfmt",.arg_required = 'f',.values = NULL},
920                 {.option_name = NULL}
921         };
922
923         *blocks = 0;
924         if (!options || !*options)
925                 /* use default configuration: create tails, journaling on, no
926                    conversion to newest format */
927                 return 1;
928
929         for (pos = options; pos;) {
930                 c = reiserfs_getopt(s, &pos, opts, &arg, mount_options);
931                 if (c == -1)
932                         /* wrong option is given */
933                         return 0;
934
935                 if (c == 'r') {
936                         char *p;
937
938                         p = NULL;
939                         /* "resize=NNN" or "resize=auto" */
940
941                         if (!strcmp(arg, "auto")) {
942                                 /* From JFS code, to auto-get the size. */
943                                 *blocks =
944                                     s->s_bdev->bd_inode->i_size >> s->
945                                     s_blocksize_bits;
946                         } else {
947                                 *blocks = simple_strtoul(arg, &p, 0);
948                                 if (*p != '\0') {
949                                         /* NNN does not look like a number */
950                                         reiserfs_warning(s,
951                                                          "reiserfs_parse_options: bad value %s",
952                                                          arg);
953                                         return 0;
954                                 }
955                         }
956                 }
957
958                 if (c == 'c') {
959                         char *p = NULL;
960                         unsigned long val = simple_strtoul(arg, &p, 0);
961                         /* commit=NNN (time in seconds) */
962                         if (*p != '\0' || val >= (unsigned int)-1) {
963                                 reiserfs_warning(s,
964                                                  "reiserfs_parse_options: bad value %s",
965                                                  arg);
966                                 return 0;
967                         }
968                         *commit_max_age = (unsigned int)val;
969                 }
970
971                 if (c == 'w') {
972                         char *p = NULL;
973                         int val = simple_strtoul(arg, &p, 0);
974
975                         if (*p != '\0') {
976                                 reiserfs_warning(s,
977                                                  "reiserfs_parse_options: non-numeric value %s for nolargeio option",
978                                                  arg);
979                                 return 0;
980                         }
981                         if (val)
982                                 reiserfs_default_io_size = PAGE_SIZE;
983                         else
984                                 reiserfs_default_io_size = 128 * 1024;
985                 }
986
987                 if (c == 'j') {
988                         if (arg && *arg && jdev_name) {
989                                 if (*jdev_name) {       //Hm, already assigned?
990                                         reiserfs_warning(s,
991                                                          "reiserfs_parse_options: journal device was already  specified to be %s",
992                                                          *jdev_name);
993                                         return 0;
994                                 }
995                                 *jdev_name = arg;
996                         }
997                 }
998 #ifdef CONFIG_QUOTA
999                 if (c == 'u' || c == 'g') {
1000                         int qtype = c == 'u' ? USRQUOTA : GRPQUOTA;
1001
1002                         if (sb_any_quota_enabled(s)) {
1003                                 reiserfs_warning(s,
1004                                                  "reiserfs_parse_options: cannot change journalled quota options when quota turned on.");
1005                                 return 0;
1006                         }
1007                         if (*arg) {     /* Some filename specified? */
1008                                 if (REISERFS_SB(s)->s_qf_names[qtype]
1009                                     && strcmp(REISERFS_SB(s)->s_qf_names[qtype],
1010                                               arg)) {
1011                                         reiserfs_warning(s,
1012                                                          "reiserfs_parse_options: %s quota file already specified.",
1013                                                          QTYPE2NAME(qtype));
1014                                         return 0;
1015                                 }
1016                                 if (strchr(arg, '/')) {
1017                                         reiserfs_warning(s,
1018                                                          "reiserfs_parse_options: quotafile must be on filesystem root.");
1019                                         return 0;
1020                                 }
1021                                 REISERFS_SB(s)->s_qf_names[qtype] =
1022                                     kmalloc(strlen(arg) + 1, GFP_KERNEL);
1023                                 if (!REISERFS_SB(s)->s_qf_names[qtype]) {
1024                                         reiserfs_warning(s,
1025                                                          "reiserfs_parse_options: not enough memory for storing quotafile name.");
1026                                         return 0;
1027                                 }
1028                                 strcpy(REISERFS_SB(s)->s_qf_names[qtype], arg);
1029                                 *mount_options |= 1 << REISERFS_QUOTA;
1030                         } else {
1031                                 kfree(REISERFS_SB(s)->s_qf_names[qtype]);
1032                                 REISERFS_SB(s)->s_qf_names[qtype] = NULL;
1033                         }
1034                 }
1035                 if (c == 'f') {
1036                         if (!strcmp(arg, "vfsold"))
1037                                 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_OLD;
1038                         else if (!strcmp(arg, "vfsv0"))
1039                                 REISERFS_SB(s)->s_jquota_fmt = QFMT_VFS_V0;
1040                         else {
1041                                 reiserfs_warning(s,
1042                                                  "reiserfs_parse_options: unknown quota format specified.");
1043                                 return 0;
1044                         }
1045                 }
1046 #else
1047                 if (c == 'u' || c == 'g' || c == 'f') {
1048                         reiserfs_warning(s,
1049                                          "reiserfs_parse_options: journalled quota options not supported.");
1050                         return 0;
1051                 }
1052 #endif
1053         }
1054
1055 #ifdef CONFIG_QUOTA
1056         if (!REISERFS_SB(s)->s_jquota_fmt
1057             && (REISERFS_SB(s)->s_qf_names[USRQUOTA]
1058                 || REISERFS_SB(s)->s_qf_names[GRPQUOTA])) {
1059                 reiserfs_warning(s,
1060                                  "reiserfs_parse_options: journalled quota format not specified.");
1061                 return 0;
1062         }
1063         /* This checking is not precise wrt the quota type but for our purposes it is sufficient */
1064         if (!(*mount_options & (1 << REISERFS_QUOTA))
1065             && sb_any_quota_enabled(s)) {
1066                 reiserfs_warning(s,
1067                                  "reiserfs_parse_options: quota options must be present when quota is turned on.");
1068                 return 0;
1069         }
1070 #endif
1071
1072         return 1;
1073 }
1074
1075 static void switch_data_mode(struct super_block *s, unsigned long mode)
1076 {
1077         REISERFS_SB(s)->s_mount_opt &= ~((1 << REISERFS_DATA_LOG) |
1078                                          (1 << REISERFS_DATA_ORDERED) |
1079                                          (1 << REISERFS_DATA_WRITEBACK));
1080         REISERFS_SB(s)->s_mount_opt |= (1 << mode);
1081 }
1082
1083 static void handle_data_mode(struct super_block *s, unsigned long mount_options)
1084 {
1085         if (mount_options & (1 << REISERFS_DATA_LOG)) {
1086                 if (!reiserfs_data_log(s)) {
1087                         switch_data_mode(s, REISERFS_DATA_LOG);
1088                         reiserfs_info(s, "switching to journaled data mode\n");
1089                 }
1090         } else if (mount_options & (1 << REISERFS_DATA_ORDERED)) {
1091                 if (!reiserfs_data_ordered(s)) {
1092                         switch_data_mode(s, REISERFS_DATA_ORDERED);
1093                         reiserfs_info(s, "switching to ordered data mode\n");
1094                 }
1095         } else if (mount_options & (1 << REISERFS_DATA_WRITEBACK)) {
1096                 if (!reiserfs_data_writeback(s)) {
1097                         switch_data_mode(s, REISERFS_DATA_WRITEBACK);
1098                         reiserfs_info(s, "switching to writeback data mode\n");
1099                 }
1100         }
1101 }
1102
1103 static void handle_barrier_mode(struct super_block *s, unsigned long bits)
1104 {
1105         int flush = (1 << REISERFS_BARRIER_FLUSH);
1106         int none = (1 << REISERFS_BARRIER_NONE);
1107         int all_barrier = flush | none;
1108
1109         if (bits & all_barrier) {
1110                 REISERFS_SB(s)->s_mount_opt &= ~all_barrier;
1111                 if (bits & flush) {
1112                         REISERFS_SB(s)->s_mount_opt |= flush;
1113                         printk("reiserfs: enabling write barrier flush mode\n");
1114                 } else if (bits & none) {
1115                         REISERFS_SB(s)->s_mount_opt |= none;
1116                         printk("reiserfs: write barriers turned off\n");
1117                 }
1118         }
1119 }
1120
1121 static void handle_attrs(struct super_block *s)
1122 {
1123         struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1124
1125         if (reiserfs_attrs(s)) {
1126                 if (old_format_only(s)) {
1127                         reiserfs_warning(s,
1128                                          "reiserfs: cannot support attributes on 3.5.x disk format");
1129                         REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
1130                         return;
1131                 }
1132                 if (!(le32_to_cpu(rs->s_flags) & reiserfs_attrs_cleared)) {
1133                         reiserfs_warning(s,
1134                                          "reiserfs: cannot support attributes until flag is set in super-block");
1135                         REISERFS_SB(s)->s_mount_opt &= ~(1 << REISERFS_ATTRS);
1136                 }
1137         }
1138 }
1139
1140 static int reiserfs_remount(struct super_block *s, int *mount_flags, char *arg)
1141 {
1142         struct reiserfs_super_block *rs;
1143         struct reiserfs_transaction_handle th;
1144         unsigned long blocks;
1145         unsigned long mount_options = REISERFS_SB(s)->s_mount_opt;
1146         unsigned long safe_mask = 0;
1147         unsigned int commit_max_age = (unsigned int)-1;
1148         struct reiserfs_journal *journal = SB_JOURNAL(s);
1149         int err;
1150 #ifdef CONFIG_QUOTA
1151         int i;
1152 #endif
1153
1154         rs = SB_DISK_SUPER_BLOCK(s);
1155
1156         if (!reiserfs_parse_options
1157             (s, arg, &mount_options, &blocks, NULL, &commit_max_age)) {
1158 #ifdef CONFIG_QUOTA
1159                 for (i = 0; i < MAXQUOTAS; i++) {
1160                         kfree(REISERFS_SB(s)->s_qf_names[i]);
1161                         REISERFS_SB(s)->s_qf_names[i] = NULL;
1162                 }
1163 #endif
1164                 return -EINVAL;
1165         }
1166
1167         if ((mount_options & (1 << REISERFS_TAGXID)) &&
1168                 !(s->s_flags & MS_TAGXID)) {
1169                 reiserfs_warning(s, "reiserfs: tagxid not permitted on remount.");
1170                 return -EINVAL;
1171         }
1172
1173         handle_attrs(s);
1174
1175         /* Add options that are safe here */
1176         safe_mask |= 1 << REISERFS_SMALLTAIL;
1177         safe_mask |= 1 << REISERFS_LARGETAIL;
1178         safe_mask |= 1 << REISERFS_NO_BORDER;
1179         safe_mask |= 1 << REISERFS_NO_UNHASHED_RELOCATION;
1180         safe_mask |= 1 << REISERFS_HASHED_RELOCATION;
1181         safe_mask |= 1 << REISERFS_TEST4;
1182         safe_mask |= 1 << REISERFS_ATTRS;
1183         safe_mask |= 1 << REISERFS_XATTRS_USER;
1184         safe_mask |= 1 << REISERFS_POSIXACL;
1185         safe_mask |= 1 << REISERFS_BARRIER_FLUSH;
1186         safe_mask |= 1 << REISERFS_BARRIER_NONE;
1187         safe_mask |= 1 << REISERFS_ERROR_RO;
1188         safe_mask |= 1 << REISERFS_ERROR_CONTINUE;
1189         safe_mask |= 1 << REISERFS_ERROR_PANIC;
1190         safe_mask |= 1 << REISERFS_QUOTA;
1191
1192         /* Update the bitmask, taking care to keep
1193          * the bits we're not allowed to change here */
1194         REISERFS_SB(s)->s_mount_opt =
1195             (REISERFS_SB(s)->
1196              s_mount_opt & ~safe_mask) | (mount_options & safe_mask);
1197
1198         if (commit_max_age != 0 && commit_max_age != (unsigned int)-1) {
1199                 journal->j_max_commit_age = commit_max_age;
1200                 journal->j_max_trans_age = commit_max_age;
1201         } else if (commit_max_age == 0) {
1202                 /* 0 means restore defaults. */
1203                 journal->j_max_commit_age = journal->j_default_max_commit_age;
1204                 journal->j_max_trans_age = JOURNAL_MAX_TRANS_AGE;
1205         }
1206
1207         if (blocks) {
1208                 int rc = reiserfs_resize(s, blocks);
1209                 if (rc != 0)
1210                         return rc;
1211         }
1212
1213         if (*mount_flags & MS_RDONLY) {
1214                 reiserfs_xattr_init(s, *mount_flags);
1215                 /* remount read-only */
1216                 if (s->s_flags & MS_RDONLY)
1217                         /* it is read-only already */
1218                         return 0;
1219                 /* try to remount file system with read-only permissions */
1220                 if (sb_umount_state(rs) == REISERFS_VALID_FS
1221                     || REISERFS_SB(s)->s_mount_state != REISERFS_VALID_FS) {
1222                         return 0;
1223                 }
1224
1225                 err = journal_begin(&th, s, 10);
1226                 if (err)
1227                         return err;
1228
1229                 /* Mounting a rw partition read-only. */
1230                 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1231                 set_sb_umount_state(rs, REISERFS_SB(s)->s_mount_state);
1232                 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1233         } else {
1234                 /* remount read-write */
1235                 if (!(s->s_flags & MS_RDONLY)) {
1236                         reiserfs_xattr_init(s, *mount_flags);
1237                         return 0;       /* We are read-write already */
1238                 }
1239
1240                 if (reiserfs_is_journal_aborted(journal))
1241                         return journal->j_errno;
1242
1243                 handle_data_mode(s, mount_options);
1244                 handle_barrier_mode(s, mount_options);
1245                 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1246                 s->s_flags &= ~MS_RDONLY;       /* now it is safe to call journal_begin */
1247                 err = journal_begin(&th, s, 10);
1248                 if (err)
1249                         return err;
1250
1251                 /* Mount a partition which is read-only, read-write */
1252                 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1253                 REISERFS_SB(s)->s_mount_state = sb_umount_state(rs);
1254                 s->s_flags &= ~MS_RDONLY;
1255                 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1256                 /* mark_buffer_dirty (SB_BUFFER_WITH_SB (s), 1); */
1257                 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1258                 REISERFS_SB(s)->s_mount_state = REISERFS_VALID_FS;
1259         }
1260         /* this will force a full flush of all journal lists */
1261         SB_JOURNAL(s)->j_must_wait = 1;
1262         err = journal_end(&th, s, 10);
1263         if (err)
1264                 return err;
1265         s->s_dirt = 0;
1266
1267         if (!(*mount_flags & MS_RDONLY)) {
1268                 finish_unfinished(s);
1269                 reiserfs_xattr_init(s, *mount_flags);
1270         }
1271
1272         return 0;
1273 }
1274
1275 /* load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure from disk.
1276  * @sb - superblock for this filesystem
1277  * @bi - the bitmap info to be loaded. Requires that bi->bh is valid.
1278  *
1279  * This routine counts how many free bits there are, finding the first zero
1280  * as a side effect. Could also be implemented as a loop of test_bit() calls, or
1281  * a loop of find_first_zero_bit() calls. This implementation is similar to
1282  * find_first_zero_bit(), but doesn't return after it finds the first bit.
1283  * Should only be called on fs mount, but should be fairly efficient anyways.
1284  *
1285  * bi->first_zero_hint is considered unset if it == 0, since the bitmap itself
1286  * will * invariably occupt block 0 represented in the bitmap. The only
1287  * exception to this is when free_count also == 0, since there will be no
1288  * free blocks at all.
1289  */
1290
1291 static void load_bitmap_info_data(struct super_block *sb,
1292                                   struct reiserfs_bitmap_info *bi)
1293 {
1294         unsigned long *cur = (unsigned long *)bi->bh->b_data;
1295
1296         while ((char *)cur < (bi->bh->b_data + sb->s_blocksize)) {
1297
1298                 /* No need to scan if all 0's or all 1's.
1299                  * Since we're only counting 0's, we can simply ignore all 1's */
1300                 if (*cur == 0) {
1301                         if (bi->first_zero_hint == 0) {
1302                                 bi->first_zero_hint =
1303                                     ((char *)cur - bi->bh->b_data) << 3;
1304                         }
1305                         bi->free_count += sizeof(unsigned long) * 8;
1306                 } else if (*cur != ~0L) {
1307                         int b;
1308                         for (b = 0; b < sizeof(unsigned long) * 8; b++) {
1309                                 if (!reiserfs_test_le_bit(b, cur)) {
1310                                         bi->free_count++;
1311                                         if (bi->first_zero_hint == 0)
1312                                                 bi->first_zero_hint =
1313                                                     (((char *)cur -
1314                                                       bi->bh->b_data) << 3) + b;
1315                                 }
1316                         }
1317                 }
1318                 cur++;
1319         }
1320
1321 #ifdef CONFIG_REISERFS_CHECK
1322 // This outputs a lot of unneded info on big FSes
1323 //    reiserfs_warning ("bitmap loaded from block %d: %d free blocks",
1324 //                    bi->bh->b_blocknr, bi->free_count);
1325 #endif
1326 }
1327
1328 static int read_bitmaps(struct super_block *s)
1329 {
1330         int i, bmap_nr;
1331
1332         SB_AP_BITMAP(s) =
1333             vmalloc(sizeof(struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
1334         if (SB_AP_BITMAP(s) == 0)
1335                 return 1;
1336         memset(SB_AP_BITMAP(s), 0,
1337                sizeof(struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
1338         for (i = 0, bmap_nr =
1339              REISERFS_DISK_OFFSET_IN_BYTES / s->s_blocksize + 1;
1340              i < SB_BMAP_NR(s); i++, bmap_nr = s->s_blocksize * 8 * i) {
1341                 SB_AP_BITMAP(s)[i].bh = sb_getblk(s, bmap_nr);
1342                 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
1343                         ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);
1344         }
1345         for (i = 0; i < SB_BMAP_NR(s); i++) {
1346                 wait_on_buffer(SB_AP_BITMAP(s)[i].bh);
1347                 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
1348                         reiserfs_warning(s, "sh-2029: reiserfs read_bitmaps: "
1349                                          "bitmap block (#%lu) reading failed",
1350                                          SB_AP_BITMAP(s)[i].bh->b_blocknr);
1351                         for (i = 0; i < SB_BMAP_NR(s); i++)
1352                                 brelse(SB_AP_BITMAP(s)[i].bh);
1353                         vfree(SB_AP_BITMAP(s));
1354                         SB_AP_BITMAP(s) = NULL;
1355                         return 1;
1356                 }
1357                 load_bitmap_info_data(s, SB_AP_BITMAP(s) + i);
1358         }
1359         return 0;
1360 }
1361
1362 static int read_old_bitmaps(struct super_block *s)
1363 {
1364         int i;
1365         struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(s);
1366         int bmp1 = (REISERFS_OLD_DISK_OFFSET_IN_BYTES / s->s_blocksize) + 1;    /* first of bitmap blocks */
1367
1368         /* read true bitmap */
1369         SB_AP_BITMAP(s) =
1370             vmalloc(sizeof(struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
1371         if (SB_AP_BITMAP(s) == 0)
1372                 return 1;
1373
1374         memset(SB_AP_BITMAP(s), 0,
1375                sizeof(struct reiserfs_buffer_info *) * sb_bmap_nr(rs));
1376
1377         for (i = 0; i < sb_bmap_nr(rs); i++) {
1378                 SB_AP_BITMAP(s)[i].bh = sb_bread(s, bmp1 + i);
1379                 if (!SB_AP_BITMAP(s)[i].bh)
1380                         return 1;
1381                 load_bitmap_info_data(s, SB_AP_BITMAP(s) + i);
1382         }
1383
1384         return 0;
1385 }
1386
1387 static int read_super_block(struct super_block *s, int offset)
1388 {
1389         struct buffer_head *bh;
1390         struct reiserfs_super_block *rs;
1391         int fs_blocksize;
1392
1393         bh = sb_bread(s, offset / s->s_blocksize);
1394         if (!bh) {
1395                 reiserfs_warning(s, "sh-2006: read_super_block: "
1396                                  "bread failed (dev %s, block %lu, size %lu)",
1397                                  reiserfs_bdevname(s), offset / s->s_blocksize,
1398                                  s->s_blocksize);
1399                 return 1;
1400         }
1401
1402         rs = (struct reiserfs_super_block *)bh->b_data;
1403         if (!is_any_reiserfs_magic_string(rs)) {
1404                 brelse(bh);
1405                 return 1;
1406         }
1407         //
1408         // ok, reiserfs signature (old or new) found in at the given offset
1409         //    
1410         fs_blocksize = sb_blocksize(rs);
1411         brelse(bh);
1412         sb_set_blocksize(s, fs_blocksize);
1413
1414         bh = sb_bread(s, offset / s->s_blocksize);
1415         if (!bh) {
1416                 reiserfs_warning(s, "sh-2007: read_super_block: "
1417                                  "bread failed (dev %s, block %lu, size %lu)\n",
1418                                  reiserfs_bdevname(s), offset / s->s_blocksize,
1419                                  s->s_blocksize);
1420                 return 1;
1421         }
1422
1423         rs = (struct reiserfs_super_block *)bh->b_data;
1424         if (sb_blocksize(rs) != s->s_blocksize) {
1425                 reiserfs_warning(s, "sh-2011: read_super_block: "
1426                                  "can't find a reiserfs filesystem on (dev %s, block %Lu, size %lu)\n",
1427                                  reiserfs_bdevname(s),
1428                                  (unsigned long long)bh->b_blocknr,
1429                                  s->s_blocksize);
1430                 brelse(bh);
1431                 return 1;
1432         }
1433
1434         if (rs->s_v1.s_root_block == cpu_to_le32(-1)) {
1435                 brelse(bh);
1436                 reiserfs_warning(s,
1437                                  "Unfinished reiserfsck --rebuild-tree run detected. Please run\n"
1438                                  "reiserfsck --rebuild-tree and wait for a completion. If that fails\n"
1439                                  "get newer reiserfsprogs package");
1440                 return 1;
1441         }
1442
1443         SB_BUFFER_WITH_SB(s) = bh;
1444         SB_DISK_SUPER_BLOCK(s) = rs;
1445
1446         if (is_reiserfs_jr(rs)) {
1447                 /* magic is of non-standard journal filesystem, look at s_version to
1448                    find which format is in use */
1449                 if (sb_version(rs) == REISERFS_VERSION_2)
1450                         reiserfs_warning(s,
1451                                          "read_super_block: found reiserfs format \"3.6\""
1452                                          " with non-standard journal");
1453                 else if (sb_version(rs) == REISERFS_VERSION_1)
1454                         reiserfs_warning(s,
1455                                          "read_super_block: found reiserfs format \"3.5\""
1456                                          " with non-standard journal");
1457                 else {
1458                         reiserfs_warning(s,
1459                                          "sh-2012: read_super_block: found unknown "
1460                                          "format \"%u\" of reiserfs with non-standard magic",
1461                                          sb_version(rs));
1462                         return 1;
1463                 }
1464         } else
1465                 /* s_version of standard format may contain incorrect information,
1466                    so we just look at the magic string */
1467                 reiserfs_info(s,
1468                               "found reiserfs format \"%s\" with standard journal\n",
1469                               is_reiserfs_3_5(rs) ? "3.5" : "3.6");
1470
1471         s->s_op = &reiserfs_sops;
1472         s->s_export_op = &reiserfs_export_ops;
1473 #ifdef CONFIG_QUOTA
1474         s->s_qcop = &reiserfs_qctl_operations;
1475         s->dq_op = &reiserfs_quota_operations;
1476 #endif
1477
1478         /* new format is limited by the 32 bit wide i_blocks field, want to
1479          ** be one full block below that.
1480          */
1481         s->s_maxbytes = (512LL << 32) - s->s_blocksize;
1482         return 0;
1483 }
1484
1485 /* after journal replay, reread all bitmap and super blocks */
1486 static int reread_meta_blocks(struct super_block *s)
1487 {
1488         int i;
1489         ll_rw_block(READ, 1, &(SB_BUFFER_WITH_SB(s)));
1490         wait_on_buffer(SB_BUFFER_WITH_SB(s));
1491         if (!buffer_uptodate(SB_BUFFER_WITH_SB(s))) {
1492                 reiserfs_warning(s,
1493                                  "reread_meta_blocks, error reading the super");
1494                 return 1;
1495         }
1496
1497         for (i = 0; i < SB_BMAP_NR(s); i++) {
1498                 ll_rw_block(READ, 1, &(SB_AP_BITMAP(s)[i].bh));
1499                 wait_on_buffer(SB_AP_BITMAP(s)[i].bh);
1500                 if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
1501                         reiserfs_warning(s,
1502                                          "reread_meta_blocks, error reading bitmap block number %d at %llu",
1503                                          i,
1504                                          (unsigned long long)SB_AP_BITMAP(s)[i].
1505                                          bh->b_blocknr);
1506                         return 1;
1507                 }
1508         }
1509         return 0;
1510
1511 }
1512
1513 /////////////////////////////////////////////////////
1514 // hash detection stuff
1515
1516 // if root directory is empty - we set default - Yura's - hash and
1517 // warn about it
1518 // FIXME: we look for only one name in a directory. If tea and yura
1519 // bith have the same value - we ask user to send report to the
1520 // mailing list
1521 static __u32 find_hash_out(struct super_block *s)
1522 {
1523         int retval;
1524         struct inode *inode;
1525         struct cpu_key key;
1526         INITIALIZE_PATH(path);
1527         struct reiserfs_dir_entry de;
1528         __u32 hash = DEFAULT_HASH;
1529
1530         inode = s->s_root->d_inode;
1531
1532         do {                    // Some serious "goto"-hater was there ;)
1533                 u32 teahash, r5hash, yurahash;
1534
1535                 make_cpu_key(&key, inode, ~0, TYPE_DIRENTRY, 3);
1536                 retval = search_by_entry_key(s, &key, &path, &de);
1537                 if (retval == IO_ERROR) {
1538                         pathrelse(&path);
1539                         return UNSET_HASH;
1540                 }
1541                 if (retval == NAME_NOT_FOUND)
1542                         de.de_entry_num--;
1543                 set_de_name_and_namelen(&de);
1544                 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1545                         /* allow override in this case */
1546                         if (reiserfs_rupasov_hash(s)) {
1547                                 hash = YURA_HASH;
1548                         }
1549                         reiserfs_warning(s, "FS seems to be empty, autodetect "
1550                                          "is using the default hash");
1551                         break;
1552                 }
1553                 r5hash = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1554                 teahash = GET_HASH_VALUE(keyed_hash(de.de_name, de.de_namelen));
1555                 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1556                 if (((teahash == r5hash)
1557                      &&
1558                      (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num])))
1559                       == r5hash)) || ((teahash == yurahash)
1560                                       && (yurahash ==
1561                                           GET_HASH_VALUE(deh_offset
1562                                                          (&
1563                                                           (de.
1564                                                            de_deh[de.
1565                                                                   de_entry_num])))))
1566                     || ((r5hash == yurahash)
1567                         && (yurahash ==
1568                             GET_HASH_VALUE(deh_offset
1569                                            (&(de.de_deh[de.de_entry_num])))))) {
1570                         reiserfs_warning(s,
1571                                          "Unable to automatically detect hash function. "
1572                                          "Please mount with -o hash={tea,rupasov,r5}",
1573                                          reiserfs_bdevname(s));
1574                         hash = UNSET_HASH;
1575                         break;
1576                 }
1577                 if (GET_HASH_VALUE(deh_offset(&(de.de_deh[de.de_entry_num]))) ==
1578                     yurahash)
1579                         hash = YURA_HASH;
1580                 else if (GET_HASH_VALUE
1581                          (deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash)
1582                         hash = TEA_HASH;
1583                 else if (GET_HASH_VALUE
1584                          (deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)
1585                         hash = R5_HASH;
1586                 else {
1587                         reiserfs_warning(s, "Unrecognised hash function");
1588                         hash = UNSET_HASH;
1589                 }
1590         } while (0);
1591
1592         pathrelse(&path);
1593         return hash;
1594 }
1595
1596 // finds out which hash names are sorted with
1597 static int what_hash(struct super_block *s)
1598 {
1599         __u32 code;
1600
1601         code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(s));
1602
1603         /* reiserfs_hash_detect() == true if any of the hash mount options
1604          ** were used.  We must check them to make sure the user isn't
1605          ** using a bad hash value
1606          */
1607         if (code == UNSET_HASH || reiserfs_hash_detect(s))
1608                 code = find_hash_out(s);
1609
1610         if (code != UNSET_HASH && reiserfs_hash_detect(s)) {
1611                 /* detection has found the hash, and we must check against the 
1612                  ** mount options 
1613                  */
1614                 if (reiserfs_rupasov_hash(s) && code != YURA_HASH) {
1615                         reiserfs_warning(s, "Error, %s hash detected, "
1616                                          "unable to force rupasov hash",
1617                                          reiserfs_hashname(code));
1618                         code = UNSET_HASH;
1619                 } else if (reiserfs_tea_hash(s) && code != TEA_HASH) {
1620                         reiserfs_warning(s, "Error, %s hash detected, "
1621                                          "unable to force tea hash",
1622                                          reiserfs_hashname(code));
1623                         code = UNSET_HASH;
1624                 } else if (reiserfs_r5_hash(s) && code != R5_HASH) {
1625                         reiserfs_warning(s, "Error, %s hash detected, "
1626                                          "unable to force r5 hash",
1627                                          reiserfs_hashname(code));
1628                         code = UNSET_HASH;
1629                 }
1630         } else {
1631                 /* find_hash_out was not called or could not determine the hash */
1632                 if (reiserfs_rupasov_hash(s)) {
1633                         code = YURA_HASH;
1634                 } else if (reiserfs_tea_hash(s)) {
1635                         code = TEA_HASH;
1636                 } else if (reiserfs_r5_hash(s)) {
1637                         code = R5_HASH;
1638                 }
1639         }
1640
1641         /* if we are mounted RW, and we have a new valid hash code, update 
1642          ** the super
1643          */
1644         if (code != UNSET_HASH &&
1645             !(s->s_flags & MS_RDONLY) &&
1646             code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1647                 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1648         }
1649         return code;
1650 }
1651
1652 // return pointer to appropriate function
1653 static hashf_t hash_function(struct super_block *s)
1654 {
1655         switch (what_hash(s)) {
1656         case TEA_HASH:
1657                 reiserfs_info(s, "Using tea hash to sort names\n");
1658                 return keyed_hash;
1659         case YURA_HASH:
1660                 reiserfs_info(s, "Using rupasov hash to sort names\n");
1661                 return yura_hash;
1662         case R5_HASH:
1663                 reiserfs_info(s, "Using r5 hash to sort names\n");
1664                 return r5_hash;
1665         }
1666         return NULL;
1667 }
1668
1669 // this is used to set up correct value for old partitions
1670 static int function2code(hashf_t func)
1671 {
1672         if (func == keyed_hash)
1673                 return TEA_HASH;
1674         if (func == yura_hash)
1675                 return YURA_HASH;
1676         if (func == r5_hash)
1677                 return R5_HASH;
1678
1679         BUG();                  // should never happen
1680
1681         return 0;
1682 }
1683
1684 #define SWARN(silent, s, ...)                   \
1685         if (!(silent))                          \
1686                 reiserfs_warning (s, __VA_ARGS__)
1687
1688 static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
1689 {
1690         struct inode *root_inode;
1691         int j;
1692         struct reiserfs_transaction_handle th;
1693         int old_format = 0;
1694         unsigned long blocks;
1695         unsigned int commit_max_age = 0;
1696         int jinit_done = 0;
1697         struct reiserfs_iget_args args;
1698         struct reiserfs_super_block *rs;
1699         char *jdev_name;
1700         struct reiserfs_sb_info *sbi;
1701         int errval = -EINVAL;
1702
1703         sbi = kmalloc(sizeof(struct reiserfs_sb_info), GFP_KERNEL);
1704         if (!sbi) {
1705                 errval = -ENOMEM;
1706                 goto error;
1707         }
1708         s->s_fs_info = sbi;
1709         memset(sbi, 0, sizeof(struct reiserfs_sb_info));
1710         /* Set default values for options: non-aggressive tails, RO on errors */
1711         REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_SMALLTAIL);
1712         REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_ERROR_RO);
1713         /* no preallocation minimum, be smart in
1714            reiserfs_file_write instead */
1715         REISERFS_SB(s)->s_alloc_options.preallocmin = 0;
1716         /* Preallocate by 16 blocks (17-1) at once */
1717         REISERFS_SB(s)->s_alloc_options.preallocsize = 17;
1718         /* Initialize the rwsem for xattr dir */
1719         init_rwsem(&REISERFS_SB(s)->xattr_dir_sem);
1720
1721         /* setup default block allocator options */
1722         reiserfs_init_alloc_options(s);
1723
1724         jdev_name = NULL;
1725         if (reiserfs_parse_options
1726             (s, (char *)data, &(sbi->s_mount_opt), &blocks, &jdev_name,
1727              &commit_max_age) == 0) {
1728                 goto error;
1729         }
1730
1731         if (blocks) {
1732                 SWARN(silent, s, "jmacd-7: reiserfs_fill_super: resize option "
1733                       "for remount only");
1734                 goto error;
1735         }
1736
1737         /* try old format (undistributed bitmap, super block in 8-th 1k block of a device) */
1738         if (!read_super_block(s, REISERFS_OLD_DISK_OFFSET_IN_BYTES))
1739                 old_format = 1;
1740         /* try new format (64-th 1k block), which can contain reiserfs super block */
1741         else if (read_super_block(s, REISERFS_DISK_OFFSET_IN_BYTES)) {
1742                 SWARN(silent, s,
1743                       "sh-2021: reiserfs_fill_super: can not find reiserfs on %s",
1744                       reiserfs_bdevname(s));
1745                 goto error;
1746         }
1747
1748         /* map mount option tagxid */
1749         if (REISERFS_SB(s)->s_mount_opt & (1 << REISERFS_TAGXID))
1750                 s->s_flags |= MS_TAGXID;
1751
1752         rs = SB_DISK_SUPER_BLOCK(s);
1753         /* Let's do basic sanity check to verify that underlying device is not
1754            smaller than the filesystem. If the check fails then abort and scream,
1755            because bad stuff will happen otherwise. */
1756         if (s->s_bdev && s->s_bdev->bd_inode
1757             && i_size_read(s->s_bdev->bd_inode) <
1758             sb_block_count(rs) * sb_blocksize(rs)) {
1759                 SWARN(silent, s,
1760                       "Filesystem on %s cannot be mounted because it is bigger than the device",
1761                       reiserfs_bdevname(s));
1762                 SWARN(silent, s,
1763                       "You may need to run fsck or increase size of your LVM partition");
1764                 SWARN(silent, s,
1765                       "Or may be you forgot to reboot after fdisk when it told you to");
1766                 goto error;
1767         }
1768
1769         sbi->s_mount_state = SB_REISERFS_STATE(s);
1770         sbi->s_mount_state = REISERFS_VALID_FS;
1771
1772         if (old_format ? read_old_bitmaps(s) : read_bitmaps(s)) {
1773                 SWARN(silent, s,
1774                       "jmacd-8: reiserfs_fill_super: unable to read bitmap");
1775                 goto error;
1776         }
1777 #ifdef CONFIG_REISERFS_CHECK
1778         SWARN(silent, s, "CONFIG_REISERFS_CHECK is set ON");
1779         SWARN(silent, s, "- it is slow mode for debugging.");
1780 #endif
1781
1782         /* make data=ordered the default */
1783         if (!reiserfs_data_log(s) && !reiserfs_data_ordered(s) &&
1784             !reiserfs_data_writeback(s)) {
1785                 REISERFS_SB(s)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
1786         }
1787
1788         if (reiserfs_data_log(s)) {
1789                 reiserfs_info(s, "using journaled data mode\n");
1790         } else if (reiserfs_data_ordered(s)) {
1791                 reiserfs_info(s, "using ordered data mode\n");
1792         } else {
1793                 reiserfs_info(s, "using writeback data mode\n");
1794         }
1795         if (reiserfs_barrier_flush(s)) {
1796                 printk("reiserfs: using flush barriers\n");
1797         }
1798         // set_device_ro(s->s_dev, 1) ;
1799         if (journal_init(s, jdev_name, old_format, commit_max_age)) {
1800                 SWARN(silent, s,
1801                       "sh-2022: reiserfs_fill_super: unable to initialize journal space");
1802                 goto error;
1803         } else {
1804                 jinit_done = 1; /* once this is set, journal_release must be called
1805                                  ** if we error out of the mount
1806                                  */
1807         }
1808         if (reread_meta_blocks(s)) {
1809                 SWARN(silent, s,
1810                       "jmacd-9: reiserfs_fill_super: unable to reread meta blocks after journal init");
1811                 goto error;
1812         }
1813
1814         if (replay_only(s))
1815                 goto error;
1816
1817         if (bdev_read_only(s->s_bdev) && !(s->s_flags & MS_RDONLY)) {
1818                 SWARN(silent, s,
1819                       "clm-7000: Detected readonly device, marking FS readonly");
1820                 s->s_flags |= MS_RDONLY;
1821         }
1822         args.objectid = REISERFS_ROOT_OBJECTID;
1823         args.dirid = REISERFS_ROOT_PARENT_OBJECTID;
1824         root_inode =
1825             iget5_locked(s, REISERFS_ROOT_OBJECTID, reiserfs_find_actor,
1826                          reiserfs_init_locked_inode, (void *)(&args));
1827         if (!root_inode) {
1828                 SWARN(silent, s,
1829                       "jmacd-10: reiserfs_fill_super: get root inode failed");
1830                 goto error;
1831         }
1832
1833         if (root_inode->i_state & I_NEW) {
1834                 reiserfs_read_locked_inode(root_inode, &args);
1835                 unlock_new_inode(root_inode);
1836         }
1837
1838         s->s_root = d_alloc_root(root_inode);
1839         if (!s->s_root) {
1840                 iput(root_inode);
1841                 goto error;
1842         }
1843         // define and initialize hash function
1844         sbi->s_hash_function = hash_function(s);
1845         if (sbi->s_hash_function == NULL) {
1846                 dput(s->s_root);
1847                 s->s_root = NULL;
1848                 goto error;
1849         }
1850
1851         if (is_reiserfs_3_5(rs)
1852             || (is_reiserfs_jr(rs) && SB_VERSION(s) == REISERFS_VERSION_1))
1853                 set_bit(REISERFS_3_5, &(sbi->s_properties));
1854         else
1855                 set_bit(REISERFS_3_6, &(sbi->s_properties));
1856
1857         if (!(s->s_flags & MS_RDONLY)) {
1858
1859                 errval = journal_begin(&th, s, 1);
1860                 if (errval) {
1861                         dput(s->s_root);
1862                         s->s_root = NULL;
1863                         goto error;
1864                 }
1865                 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
1866
1867                 set_sb_umount_state(rs, REISERFS_ERROR_FS);
1868                 set_sb_fs_state(rs, 0);
1869
1870                 if (old_format_only(s)) {
1871                         /* filesystem of format 3.5 either with standard or non-standard
1872                            journal */
1873                         if (convert_reiserfs(s)) {
1874                                 /* and -o conv is given */
1875                                 if (!silent)
1876                                         reiserfs_info(s,
1877                                                       "converting 3.5 filesystem to the 3.6 format");
1878
1879                                 if (is_reiserfs_3_5(rs))
1880                                         /* put magic string of 3.6 format. 2.2 will not be able to
1881                                            mount this filesystem anymore */
1882                                         memcpy(rs->s_v1.s_magic,
1883                                                reiserfs_3_6_magic_string,
1884                                                sizeof
1885                                                (reiserfs_3_6_magic_string));
1886
1887                                 set_sb_version(rs, REISERFS_VERSION_2);
1888                                 reiserfs_convert_objectid_map_v1(s);
1889                                 set_bit(REISERFS_3_6, &(sbi->s_properties));
1890                                 clear_bit(REISERFS_3_5, &(sbi->s_properties));
1891                         } else if (!silent) {
1892                                 reiserfs_info(s, "using 3.5.x disk format\n");
1893                         }
1894                 }
1895
1896                 journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
1897                 errval = journal_end(&th, s, 1);
1898                 if (errval) {
1899                         dput(s->s_root);
1900                         s->s_root = NULL;
1901                         goto error;
1902                 }
1903
1904                 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1905                         dput(s->s_root);
1906                         s->s_root = NULL;
1907                         goto error;
1908                 }
1909
1910                 /* look for files which were to be removed in previous session */
1911                 finish_unfinished(s);
1912         } else {
1913                 if (old_format_only(s) && !silent) {
1914                         reiserfs_info(s, "using 3.5.x disk format\n");
1915                 }
1916
1917                 if ((errval = reiserfs_xattr_init(s, s->s_flags))) {
1918                         dput(s->s_root);
1919                         s->s_root = NULL;
1920                         goto error;
1921                 }
1922         }
1923         // mark hash in super block: it could be unset. overwrite should be ok
1924         set_sb_hash_function_code(rs, function2code(sbi->s_hash_function));
1925
1926         handle_attrs(s);
1927
1928         reiserfs_proc_info_init(s);
1929
1930         init_waitqueue_head(&(sbi->s_wait));
1931         spin_lock_init(&sbi->bitmap_lock);
1932
1933         return (0);
1934
1935       error:
1936         if (jinit_done) {       /* kill the commit thread, free journal ram */
1937                 journal_release_error(NULL, s);
1938         }
1939         if (SB_DISK_SUPER_BLOCK(s)) {
1940                 for (j = 0; j < SB_BMAP_NR(s); j++) {
1941                         if (SB_AP_BITMAP(s))
1942                                 brelse(SB_AP_BITMAP(s)[j].bh);
1943                 }
1944                 vfree(SB_AP_BITMAP(s));
1945         }
1946         if (SB_BUFFER_WITH_SB(s))
1947                 brelse(SB_BUFFER_WITH_SB(s));
1948 #ifdef CONFIG_QUOTA
1949         for (j = 0; j < MAXQUOTAS; j++) {
1950                 kfree(sbi->s_qf_names[j]);
1951                 sbi->s_qf_names[j] = NULL;
1952         }
1953 #endif
1954         kfree(sbi);
1955
1956         s->s_fs_info = NULL;
1957         return errval;
1958 }
1959
1960 static int reiserfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1961 {
1962         struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(dentry->d_sb);
1963
1964         buf->f_namelen = (REISERFS_MAX_NAME(s->s_blocksize));
1965         buf->f_bfree = sb_free_blocks(rs);
1966         buf->f_bavail = buf->f_bfree;
1967         buf->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
1968         buf->f_bsize = dentry->d_sb->s_blocksize;
1969         /* changed to accommodate gcc folks. */
1970         buf->f_type = REISERFS_SUPER_MAGIC;
1971         return 0;
1972 }
1973
1974 #ifdef CONFIG_QUOTA
1975 static int reiserfs_dquot_initialize(struct inode *inode, int type)
1976 {
1977         struct reiserfs_transaction_handle th;
1978         int ret, err;
1979
1980         /* We may create quota structure so we need to reserve enough blocks */
1981         reiserfs_write_lock(inode->i_sb);
1982         ret =
1983             journal_begin(&th, inode->i_sb,
1984                           2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1985         if (ret)
1986                 goto out;
1987         ret = dquot_initialize(inode, type);
1988         err =
1989             journal_end(&th, inode->i_sb,
1990                         2 * REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb));
1991         if (!ret && err)
1992                 ret = err;
1993       out:
1994         reiserfs_write_unlock(inode->i_sb);
1995         return ret;
1996 }
1997
1998 static int reiserfs_dquot_drop(struct inode *inode)
1999 {
2000         struct reiserfs_transaction_handle th;
2001         int ret, err;
2002
2003         /* We may delete quota structure so we need to reserve enough blocks */
2004         reiserfs_write_lock(inode->i_sb);
2005         ret =
2006             journal_begin(&th, inode->i_sb,
2007                           2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
2008         if (ret)
2009                 goto out;
2010         ret = dquot_drop(inode);
2011         err =
2012             journal_end(&th, inode->i_sb,
2013                         2 * REISERFS_QUOTA_DEL_BLOCKS(inode->i_sb));
2014         if (!ret && err)
2015                 ret = err;
2016       out:
2017         reiserfs_write_unlock(inode->i_sb);
2018         return ret;
2019 }
2020
2021 static int reiserfs_write_dquot(struct dquot *dquot)
2022 {
2023         struct reiserfs_transaction_handle th;
2024         int ret, err;
2025
2026         reiserfs_write_lock(dquot->dq_sb);
2027         ret =
2028             journal_begin(&th, dquot->dq_sb,
2029                           REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2030         if (ret)
2031                 goto out;
2032         ret = dquot_commit(dquot);
2033         err =
2034             journal_end(&th, dquot->dq_sb,
2035                         REISERFS_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2036         if (!ret && err)
2037                 ret = err;
2038       out:
2039         reiserfs_write_unlock(dquot->dq_sb);
2040         return ret;
2041 }
2042
2043 static int reiserfs_acquire_dquot(struct dquot *dquot)
2044 {
2045         struct reiserfs_transaction_handle th;
2046         int ret, err;
2047
2048         reiserfs_write_lock(dquot->dq_sb);
2049         ret =
2050             journal_begin(&th, dquot->dq_sb,
2051                           REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2052         if (ret)
2053                 goto out;
2054         ret = dquot_acquire(dquot);
2055         err =
2056             journal_end(&th, dquot->dq_sb,
2057                         REISERFS_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2058         if (!ret && err)
2059                 ret = err;
2060       out:
2061         reiserfs_write_unlock(dquot->dq_sb);
2062         return ret;
2063 }
2064
2065 static int reiserfs_release_dquot(struct dquot *dquot)
2066 {
2067         struct reiserfs_transaction_handle th;
2068         int ret, err;
2069
2070         reiserfs_write_lock(dquot->dq_sb);
2071         ret =
2072             journal_begin(&th, dquot->dq_sb,
2073                           REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2074         if (ret)
2075                 goto out;
2076         ret = dquot_release(dquot);
2077         err =
2078             journal_end(&th, dquot->dq_sb,
2079                         REISERFS_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2080         if (!ret && err)
2081                 ret = err;
2082       out:
2083         reiserfs_write_unlock(dquot->dq_sb);
2084         return ret;
2085 }
2086
2087 static int reiserfs_mark_dquot_dirty(struct dquot *dquot)
2088 {
2089         /* Are we journalling quotas? */
2090         if (REISERFS_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2091             REISERFS_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2092                 dquot_mark_dquot_dirty(dquot);
2093                 return reiserfs_write_dquot(dquot);
2094         } else
2095                 return dquot_mark_dquot_dirty(dquot);
2096 }
2097
2098 static int reiserfs_write_info(struct super_block *sb, int type)
2099 {
2100         struct reiserfs_transaction_handle th;
2101         int ret, err;
2102
2103         /* Data block + inode block */
2104         reiserfs_write_lock(sb);
2105         ret = journal_begin(&th, sb, 2);
2106         if (ret)
2107                 goto out;
2108         ret = dquot_commit_info(sb, type);
2109         err = journal_end(&th, sb, 2);
2110         if (!ret && err)
2111                 ret = err;
2112       out:
2113         reiserfs_write_unlock(sb);
2114         return ret;
2115 }
2116
2117 /*
2118  * Turn on quotas during mount time - we need to find the quota file and such...
2119  */
2120 static int reiserfs_quota_on_mount(struct super_block *sb, int type)
2121 {
2122         return vfs_quota_on_mount(sb, REISERFS_SB(sb)->s_qf_names[type],
2123                                   REISERFS_SB(sb)->s_jquota_fmt, type);
2124 }
2125
2126 /*
2127  * Standard function to be called on quota_on
2128  */
2129 static int reiserfs_quota_on(struct super_block *sb, int type, int format_id,
2130                              char *path)
2131 {
2132         int err;
2133         struct nameidata nd;
2134
2135         if (!(REISERFS_SB(sb)->s_mount_opt & (1 << REISERFS_QUOTA)))
2136                 return -EINVAL;
2137         err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2138         if (err)
2139                 return err;
2140         /* Quotafile not on the same filesystem? */
2141         if (nd.mnt->mnt_sb != sb) {
2142                 path_release(&nd);
2143                 return -EXDEV;
2144         }
2145         /* We must not pack tails for quota files on reiserfs for quota IO to work */
2146         if (!REISERFS_I(nd.dentry->d_inode)->i_flags & i_nopack_mask) {
2147                 reiserfs_warning(sb,
2148                                  "reiserfs: Quota file must have tail packing disabled.");
2149                 path_release(&nd);
2150                 return -EINVAL;
2151         }
2152         /* Not journalling quota? No more tests needed... */
2153         if (!REISERFS_SB(sb)->s_qf_names[USRQUOTA] &&
2154             !REISERFS_SB(sb)->s_qf_names[GRPQUOTA]) {
2155                 path_release(&nd);
2156                 return vfs_quota_on(sb, type, format_id, path);
2157         }
2158         /* Quotafile not of fs root? */
2159         if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2160                 reiserfs_warning(sb,
2161                                  "reiserfs: Quota file not on filesystem root. "
2162                                  "Journalled quota will not work.");
2163         path_release(&nd);
2164         return vfs_quota_on(sb, type, format_id, path);
2165 }
2166
2167 /* Read data from quotafile - avoid pagecache and such because we cannot afford
2168  * acquiring the locks... As quota files are never truncated and quota code
2169  * itself serializes the operations (and noone else should touch the files)
2170  * we don't have to be afraid of races */
2171 static ssize_t reiserfs_quota_read(struct super_block *sb, int type, char *data,
2172                                    size_t len, loff_t off)
2173 {
2174         struct inode *inode = sb_dqopt(sb)->files[type];
2175         unsigned long blk = off >> sb->s_blocksize_bits;
2176         int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2177         size_t toread;
2178         struct buffer_head tmp_bh, *bh;
2179         loff_t i_size = i_size_read(inode);
2180
2181         if (off > i_size)
2182                 return 0;
2183         if (off + len > i_size)
2184                 len = i_size - off;
2185         toread = len;
2186         while (toread > 0) {
2187                 tocopy =
2188                     sb->s_blocksize - offset <
2189                     toread ? sb->s_blocksize - offset : toread;
2190                 tmp_bh.b_state = 0;
2191                 /* Quota files are without tails so we can safely use this function */
2192                 reiserfs_write_lock(sb);
2193                 err = reiserfs_get_block(inode, blk, &tmp_bh, 0);
2194                 reiserfs_write_unlock(sb);
2195                 if (err)
2196                         return err;
2197                 if (!buffer_mapped(&tmp_bh))    /* A hole? */
2198                         memset(data, 0, tocopy);
2199                 else {
2200                         bh = sb_bread(sb, tmp_bh.b_blocknr);
2201                         if (!bh)
2202                                 return -EIO;
2203                         memcpy(data, bh->b_data + offset, tocopy);
2204                         brelse(bh);
2205                 }
2206                 offset = 0;
2207                 toread -= tocopy;
2208                 data += tocopy;
2209                 blk++;
2210         }
2211         return len;
2212 }
2213
2214 /* Write to quotafile (we know the transaction is already started and has
2215  * enough credits) */
2216 static ssize_t reiserfs_quota_write(struct super_block *sb, int type,
2217                                     const char *data, size_t len, loff_t off)
2218 {
2219         struct inode *inode = sb_dqopt(sb)->files[type];
2220         unsigned long blk = off >> sb->s_blocksize_bits;
2221         int err = 0, offset = off & (sb->s_blocksize - 1), tocopy;
2222         int journal_quota = REISERFS_SB(sb)->s_qf_names[type] != NULL;
2223         size_t towrite = len;
2224         struct buffer_head tmp_bh, *bh;
2225
2226         mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2227         while (towrite > 0) {
2228                 tocopy = sb->s_blocksize - offset < towrite ?
2229                     sb->s_blocksize - offset : towrite;
2230                 tmp_bh.b_state = 0;
2231                 err = reiserfs_get_block(inode, blk, &tmp_bh, GET_BLOCK_CREATE);
2232                 if (err)
2233                         goto out;
2234                 if (offset || tocopy != sb->s_blocksize)
2235                         bh = sb_bread(sb, tmp_bh.b_blocknr);
2236                 else
2237                         bh = sb_getblk(sb, tmp_bh.b_blocknr);
2238                 if (!bh) {
2239                         err = -EIO;
2240                         goto out;
2241                 }
2242                 lock_buffer(bh);
2243                 memcpy(bh->b_data + offset, data, tocopy);
2244                 flush_dcache_page(bh->b_page);
2245                 set_buffer_uptodate(bh);
2246                 unlock_buffer(bh);
2247                 reiserfs_prepare_for_journal(sb, bh, 1);
2248                 journal_mark_dirty(current->journal_info, sb, bh);
2249                 if (!journal_quota)
2250                         reiserfs_add_ordered_list(inode, bh);
2251                 brelse(bh);
2252                 offset = 0;
2253                 towrite -= tocopy;
2254                 data += tocopy;
2255                 blk++;
2256         }
2257       out:
2258         if (len == towrite)
2259                 return err;
2260         if (inode->i_size < off + len - towrite)
2261                 i_size_write(inode, off + len - towrite);
2262         inode->i_version++;
2263         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2264         mark_inode_dirty(inode);
2265         mutex_unlock(&inode->i_mutex);
2266         return len - towrite;
2267 }
2268
2269 #endif
2270
2271 static int get_super_block(struct file_system_type *fs_type,
2272                            int flags, const char *dev_name,
2273                            void *data, struct vfsmount *mnt)
2274 {
2275         return get_sb_bdev(fs_type, flags, dev_name, data, reiserfs_fill_super,
2276                            mnt);
2277 }
2278
2279 static int __init init_reiserfs_fs(void)
2280 {
2281         int ret;
2282
2283         if ((ret = init_inodecache())) {
2284                 return ret;
2285         }
2286
2287         if ((ret = reiserfs_xattr_register_handlers()))
2288                 goto failed_reiserfs_xattr_register_handlers;
2289
2290         reiserfs_proc_info_global_init();
2291         reiserfs_proc_register_global("version",
2292                                       reiserfs_global_version_in_proc);
2293
2294         ret = register_filesystem(&reiserfs_fs_type);
2295
2296         if (ret == 0) {
2297                 return 0;
2298         }
2299
2300         reiserfs_xattr_unregister_handlers();
2301
2302       failed_reiserfs_xattr_register_handlers:
2303         reiserfs_proc_unregister_global("version");
2304         reiserfs_proc_info_global_done();
2305         destroy_inodecache();
2306
2307         return ret;
2308 }
2309
2310 static void __exit exit_reiserfs_fs(void)
2311 {
2312         reiserfs_xattr_unregister_handlers();
2313         reiserfs_proc_unregister_global("version");
2314         reiserfs_proc_info_global_done();
2315         unregister_filesystem(&reiserfs_fs_type);
2316         destroy_inodecache();
2317 }
2318
2319 struct file_system_type reiserfs_fs_type = {
2320         .owner = THIS_MODULE,
2321         .name = "reiserfs",
2322         .get_sb = get_super_block,
2323         .kill_sb = reiserfs_kill_sb,
2324         .fs_flags = FS_REQUIRES_DEV,
2325 };
2326
2327 MODULE_DESCRIPTION("ReiserFS journaled filesystem");
2328 MODULE_AUTHOR("Hans Reiser <reiser@namesys.com>");
2329 MODULE_LICENSE("GPL");
2330
2331 module_init(init_reiserfs_fs);
2332 module_exit(exit_reiserfs_fs);