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