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