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