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