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