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