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