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