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