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