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