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