reiserfs_vfsops.c revision 200071
1/*-
2 * Copyright 2000 Hans Reiser
3 * See README for licensing and copyright details
4 *
5 * Ported to FreeBSD by Jean-S�bastien P�dron <jspedron@club-internet.fr>
6 *
7 * $FreeBSD: head/sys/gnu/fs/reiserfs/reiserfs_vfsops.c 200071 2009-12-03 18:16:14Z trasz $
8 */
9
10#include <gnu/fs/reiserfs/reiserfs_fs.h>
11
12const char reiserfs_3_5_magic_string[] = REISERFS_SUPER_MAGIC_STRING;
13const char reiserfs_3_6_magic_string[] = REISER2FS_SUPER_MAGIC_STRING;
14const char reiserfs_jr_magic_string[]  = REISER2FS_JR_SUPER_MAGIC_STRING;
15
16/*
17 * Default recommended I/O size is 128k. There might be broken
18 * applications that are confused by this. Use nolargeio mount option to
19 * get usual i/o size = PAGE_SIZE.
20 */
21int reiserfs_default_io_size = 128 * 1024;
22
23static vfs_cmount_t	reiserfs_cmount;
24static vfs_fhtovp_t	reiserfs_fhtovp;
25static vfs_mount_t	reiserfs_mount;
26static vfs_root_t	reiserfs_root;
27static vfs_statfs_t	reiserfs_statfs;
28static vfs_unmount_t	reiserfs_unmount;
29
30static int	reiserfs_mountfs(struct vnode *devvp, struct mount *mp,
31		    struct thread *td);
32static void	load_bitmap_info_data(struct reiserfs_sb_info *sbi,
33		    struct reiserfs_bitmap_info *bi);
34static int	read_bitmaps(struct reiserfs_mount *rmp);
35static int	read_old_bitmaps(struct reiserfs_mount *rmp);
36static int	read_super_block(struct reiserfs_mount *rmp, int offset);
37static hashf_t	hash_function(struct reiserfs_mount *rmp);
38
39static int	get_root_node(struct reiserfs_mount *rmp,
40		    struct reiserfs_node **root);
41uint32_t	find_hash_out(struct reiserfs_mount *rmp);
42
43MALLOC_DEFINE(M_REISERFSMNT, "reiserfs_mount", "ReiserFS mount structure");
44MALLOC_DEFINE(M_REISERFSPATH, "reiserfs_path", "ReiserFS path structure");
45MALLOC_DEFINE(M_REISERFSNODE, "reiserfs_node", "ReiserFS vnode private part");
46
47/* -------------------------------------------------------------------
48 * VFS operations
49 * -------------------------------------------------------------------*/
50
51static int
52reiserfs_cmount(struct mntarg *ma, void *data, int flags)
53{
54	struct reiserfs_args args;
55	int error;
56
57	error = copyin(data, &args, sizeof(args));
58	if (error)
59		return (error);
60
61	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
62	ma = mount_arg(ma, "export", &args.export, sizeof args.export);
63
64	error = kernel_mount(ma, flags);
65
66	return (error);
67}
68
69/*
70 * Mount system call
71 */
72static int
73reiserfs_mount(struct mount *mp)
74{
75	size_t size;
76	int error, len;
77	accmode_t accmode;
78	char *path, *fspec;
79	struct vnode *devvp;
80	struct vfsoptlist *opts;
81	struct reiserfs_mount *rmp;
82	struct reiserfs_sb_info *sbi;
83	struct nameidata nd, *ndp = &nd;
84	struct thread *td;
85
86	td = curthread;
87	if (!(mp->mnt_flag & MNT_RDONLY))
88		return EROFS;
89
90	/* Get the new options passed to mount */
91	opts = mp->mnt_optnew;
92
93	/* `fspath' contains the mount point (eg. /mnt/linux); REQUIRED */
94	vfs_getopt(opts, "fspath", (void **)&path, NULL);
95	reiserfs_log(LOG_INFO, "mount point is `%s'\n", path);
96
97	/* `from' contains the device name (eg. /dev/ad0s1); REQUIRED */
98	fspec = NULL;
99	error = vfs_getopt(opts, "from", (void **)&fspec, &len);
100	if (!error && fspec[len - 1] != '\0')
101		return (EINVAL);
102	reiserfs_log(LOG_INFO, "device is `%s'\n", fspec);
103
104	/* Handle MNT_UPDATE (mp->mnt_flag) */
105	if (mp->mnt_flag & MNT_UPDATE) {
106		/* For now, only NFS export is supported. */
107		if (vfs_flagopt(opts, "export", NULL, 0))
108			return (0);
109	}
110
111	/* Not an update, or updating the name: look up the name
112	 * and verify that it refers to a sensible disk device. */
113	if (fspec == NULL)
114		return (EINVAL);
115
116	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
117	if ((error = namei(ndp)) != 0)
118		return (error);
119	NDFREE(ndp, NDF_ONLY_PNBUF);
120	devvp = ndp->ni_vp;
121
122	if (!vn_isdisk(devvp, &error)) {
123		vput(devvp);
124		return (error);
125	}
126
127	/* If mount by non-root, then verify that user has necessary
128	 * permissions on the device. */
129	accmode = VREAD;
130	if ((mp->mnt_flag & MNT_RDONLY) == 0)
131		accmode |= VWRITE;
132	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
133	if (error)
134		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
135	if (error) {
136		vput(devvp);
137		return (error);
138	}
139
140	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
141		error = reiserfs_mountfs(devvp, mp, td);
142	} else {
143		/* TODO Handle MNT_UPDATE */
144		vput(devvp);
145		return (EOPNOTSUPP);
146	}
147
148	if (error) {
149		vrele(devvp);
150		return (error);
151	}
152
153	rmp = VFSTOREISERFS(mp);
154	sbi = rmp->rm_reiserfs;
155
156	/*
157	 * Note that this strncpy() is ok because of a check at the start
158	 * of reiserfs_mount().
159	 */
160	reiserfs_log(LOG_DEBUG, "prepare statfs data\n");
161	(void)copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
162	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
163	(void)reiserfs_statfs(mp, &mp->mnt_stat);
164
165	reiserfs_log(LOG_DEBUG, "done\n");
166	return (0);
167}
168
169/*
170 * Unmount system call
171 */
172static int
173reiserfs_unmount(struct mount *mp, int mntflags)
174{
175	int error, flags = 0;
176	struct reiserfs_mount *rmp;
177	struct reiserfs_sb_info *sbi;
178
179	reiserfs_log(LOG_DEBUG, "get private data\n");
180	rmp = VFSTOREISERFS(mp);
181	sbi = rmp->rm_reiserfs;
182
183	/* Flangs handling */
184	reiserfs_log(LOG_DEBUG, "handle mntflags\n");
185	if (mntflags & MNT_FORCE)
186		flags |= FORCECLOSE;
187
188	/* Flush files -> vflush */
189	reiserfs_log(LOG_DEBUG, "flush vnodes\n");
190	if ((error = vflush(mp, 0, flags, curthread)))
191		return (error);
192
193	/* XXX Super block update */
194
195	if (sbi) {
196		if (SB_AP_BITMAP(sbi)) {
197			int i;
198			reiserfs_log(LOG_DEBUG,
199			    "release bitmap buffers (total: %d)\n",
200			    SB_BMAP_NR(sbi));
201			for (i = 0; i < SB_BMAP_NR(sbi); i++) {
202				if (SB_AP_BITMAP(sbi)[i].bp_data) {
203					free(SB_AP_BITMAP(sbi)[i].bp_data,
204					    M_REISERFSMNT);
205					SB_AP_BITMAP(sbi)[i].bp_data = NULL;
206				}
207			}
208
209			reiserfs_log(LOG_DEBUG, "free bitmaps structure\n");
210			free(SB_AP_BITMAP(sbi), M_REISERFSMNT);
211			SB_AP_BITMAP(sbi) = NULL;
212		}
213
214		if (sbi->s_rs) {
215			reiserfs_log(LOG_DEBUG, "free super block data\n");
216			free(sbi->s_rs, M_REISERFSMNT);
217			sbi->s_rs = NULL;
218		}
219	}
220
221	reiserfs_log(LOG_DEBUG, "close device\n");
222#if defined(si_mountpoint)
223	rmp->rm_devvp->v_rdev->si_mountpoint = NULL;
224#endif
225
226	DROP_GIANT();
227	g_topology_lock();
228	g_wither_geom_close(rmp->rm_cp->geom, ENXIO);
229	g_topology_unlock();
230	PICKUP_GIANT();
231	vrele(rmp->rm_devvp);
232
233	if (sbi) {
234		reiserfs_log(LOG_DEBUG, "free sbi\n");
235		free(sbi, M_REISERFSMNT);
236		sbi = rmp->rm_reiserfs = NULL;
237	}
238	if (rmp) {
239		reiserfs_log(LOG_DEBUG, "free rmp\n");
240		free(rmp, M_REISERFSMNT);
241		rmp = NULL;
242	}
243
244	mp->mnt_data  = 0;
245	MNT_ILOCK(mp);
246	mp->mnt_flag &= ~MNT_LOCAL;
247	MNT_IUNLOCK(mp);
248
249	reiserfs_log(LOG_DEBUG, "done\n");
250	return (error);
251}
252
253/*
254 * Return the root of a filesystem.
255 */
256static int
257reiserfs_root(struct mount *mp, int flags, struct vnode **vpp)
258{
259	int error;
260	struct vnode *vp;
261	struct cpu_key rootkey;
262
263	rootkey.on_disk_key.k_dir_id = REISERFS_ROOT_PARENT_OBJECTID;
264	rootkey.on_disk_key.k_objectid = REISERFS_ROOT_OBJECTID;
265
266	error = reiserfs_iget(mp, &rootkey, &vp, curthread);
267
268	if (error == 0)
269		*vpp = vp;
270	return (error);
271}
272
273/*
274 * The statfs syscall
275 */
276static int
277reiserfs_statfs(struct mount *mp, struct statfs *sbp)
278{
279	struct reiserfs_mount *rmp;
280	struct reiserfs_sb_info *sbi;
281	struct reiserfs_super_block *rs;
282
283	reiserfs_log(LOG_DEBUG, "get private data\n");
284	rmp = VFSTOREISERFS(mp);
285	sbi = rmp->rm_reiserfs;
286	rs  = sbi->s_rs;
287
288	reiserfs_log(LOG_DEBUG, "fill statfs structure\n");
289	sbp->f_bsize  = sbi->s_blocksize;
290	sbp->f_iosize = sbp->f_bsize;
291	sbp->f_blocks = sb_block_count(rs) - sb_bmap_nr(rs) - 1;
292	sbp->f_bfree  = sb_free_blocks(rs);
293	sbp->f_bavail = sbp->f_bfree;
294	sbp->f_files  = 0;
295	sbp->f_ffree  = 0;
296	reiserfs_log(LOG_DEBUG, "  block size   = %ju\n",
297	    (intmax_t)sbp->f_bsize);
298	reiserfs_log(LOG_DEBUG, "  IO size      = %ju\n",
299	    (intmax_t)sbp->f_iosize);
300	reiserfs_log(LOG_DEBUG, "  block count  = %ju\n",
301	    (intmax_t)sbp->f_blocks);
302	reiserfs_log(LOG_DEBUG, "  free blocks  = %ju\n",
303	    (intmax_t)sbp->f_bfree);
304	reiserfs_log(LOG_DEBUG, "  avail blocks = %ju\n",
305	    (intmax_t)sbp->f_bavail);
306	reiserfs_log(LOG_DEBUG, "...done\n");
307
308	if (sbp != &mp->mnt_stat) {
309		reiserfs_log(LOG_DEBUG, "copying monut point info\n");
310		sbp->f_type = mp->mnt_vfc->vfc_typenum;
311		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
312		    (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
313		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
314		    (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
315		reiserfs_log(LOG_DEBUG, "  mount from: %s\n",
316		    sbp->f_mntfromname);
317		reiserfs_log(LOG_DEBUG, "  mount on:   %s\n",
318		    sbp->f_mntonname);
319		reiserfs_log(LOG_DEBUG, "...done\n");
320	}
321
322	return (0);
323}
324
325/*
326 * File handle to vnode
327 *
328 * Have to be really careful about stale file handles:
329 * - check that the inode key is valid
330 * - call ffs_vget() to get the locked inode
331 * - check for an unallocated inode (i_mode == 0)
332 * - check that the given client host has export rights and return
333 *   those rights via. exflagsp and credanonp
334 */
335static int
336reiserfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
337{
338	int error;
339	struct rfid *rfhp;
340	struct vnode *nvp;
341	struct cpu_key key;
342	struct reiserfs_node *ip;
343	struct reiserfs_sb_info *sbi;
344	struct thread *td = curthread;
345
346	rfhp = (struct rfid *)fhp;
347	sbi  = VFSTOREISERFS(mp)->rm_reiserfs;
348
349	/* Check that the key is valid */
350	if (rfhp->rfid_dirid < REISERFS_ROOT_PARENT_OBJECTID &&
351	    rfhp->rfid_objectid < REISERFS_ROOT_OBJECTID)
352		return (ESTALE);
353
354	reiserfs_log(LOG_DEBUG,
355	    "file handle key is (dirid=%d, objectid=%d)\n",
356	    rfhp->rfid_dirid, rfhp->rfid_objectid);
357	key.on_disk_key.k_dir_id   = rfhp->rfid_dirid;
358	key.on_disk_key.k_objectid = rfhp->rfid_objectid;
359
360	reiserfs_log(LOG_DEBUG, "read this inode\n");
361	error = reiserfs_iget(mp, &key, &nvp, td);
362	if (error) {
363		*vpp = NULLVP;
364		return (error);
365	}
366
367	reiserfs_log(LOG_DEBUG, "check validity\n");
368	ip = VTOI(nvp);
369	if (ip->i_mode == 0 || ip->i_generation != rfhp->rfid_gen) {
370		vput(nvp);
371		*vpp = NULLVP;
372		return (ESTALE);
373	}
374
375	reiserfs_log(LOG_DEBUG, "return it\n");
376	*vpp = nvp;
377	return (0);
378}
379
380/* -------------------------------------------------------------------
381 * Functions for the journal
382 * -------------------------------------------------------------------*/
383
384int
385is_reiserfs_3_5(struct reiserfs_super_block *rs)
386{
387
388	return (!strncmp(rs->s_v1.s_magic, reiserfs_3_5_magic_string,
389	    strlen(reiserfs_3_5_magic_string)));
390}
391
392int
393is_reiserfs_3_6(struct reiserfs_super_block *rs)
394{
395
396	return (!strncmp(rs->s_v1.s_magic, reiserfs_3_6_magic_string,
397	    strlen(reiserfs_3_6_magic_string)));
398}
399
400int
401is_reiserfs_jr(struct reiserfs_super_block *rs)
402{
403
404	return (!strncmp(rs->s_v1.s_magic, reiserfs_jr_magic_string,
405	    strlen(reiserfs_jr_magic_string)));
406}
407
408static int
409is_any_reiserfs_magic_string(struct reiserfs_super_block *rs)
410{
411
412	return ((is_reiserfs_3_5(rs) || is_reiserfs_3_6(rs) ||
413	    is_reiserfs_jr(rs)));
414}
415
416/* -------------------------------------------------------------------
417 * Internal functions
418 * -------------------------------------------------------------------*/
419
420/*
421 * Common code for mount and mountroot
422 */
423static int
424reiserfs_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td)
425{
426	int error, old_format = 0;
427	struct reiserfs_mount *rmp;
428	struct reiserfs_sb_info *sbi;
429	struct reiserfs_super_block *rs;
430	struct cdev *dev = devvp->v_rdev;
431
432	struct g_consumer *cp;
433	struct bufobj *bo;
434
435	//ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
436
437	DROP_GIANT();
438	g_topology_lock();
439	error = g_vfs_open(devvp, &cp, "reiserfs", /* read-only */ 0);
440	g_topology_unlock();
441	PICKUP_GIANT();
442	VOP_UNLOCK(devvp, 0);
443	if (error)
444		return (error);
445
446	bo = &devvp->v_bufobj;
447	bo->bo_private = cp;
448	bo->bo_ops = g_vfs_bufops;
449
450	if (devvp->v_rdev->si_iosize_max != 0)
451		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
452	if (mp->mnt_iosize_max > MAXPHYS)
453		mp->mnt_iosize_max = MAXPHYS;
454
455	rmp = NULL;
456	sbi = NULL;
457
458	/* rmp contains any information about this specific mount */
459	rmp = malloc(sizeof *rmp, M_REISERFSMNT, M_WAITOK | M_ZERO);
460	if (!rmp) {
461		error = (ENOMEM);
462		goto out;
463	}
464	sbi = malloc(sizeof *sbi, M_REISERFSMNT, M_WAITOK | M_ZERO);
465	if (!sbi) {
466		error = (ENOMEM);
467		goto out;
468	}
469	rmp->rm_reiserfs = sbi;
470	rmp->rm_mountp   = mp;
471	rmp->rm_devvp    = devvp;
472	rmp->rm_dev      = dev;
473	rmp->rm_bo       = &devvp->v_bufobj;
474	rmp->rm_cp       = cp;
475
476	/* Set default values for options: non-aggressive tails */
477	REISERFS_SB(sbi)->s_mount_opt = (1 << REISERFS_SMALLTAIL);
478	REISERFS_SB(sbi)->s_rd_only   = 1;
479	REISERFS_SB(sbi)->s_devvp     = devvp;
480
481	/* Read the super block */
482	if ((error = read_super_block(rmp, REISERFS_OLD_DISK_OFFSET)) == 0) {
483		/* The read process succeeded, it's an old format */
484		old_format = 1;
485	} else if ((error = read_super_block(rmp, REISERFS_DISK_OFFSET)) != 0) {
486		reiserfs_log(LOG_ERR, "can not find a ReiserFS filesystem\n");
487		goto out;
488	}
489
490	rs = SB_DISK_SUPER_BLOCK(sbi);
491
492	/*
493	 * Let's do basic sanity check to verify that underlying device is
494	 * not smaller than the filesystem. If the check fails then abort and
495	 * scream, because bad stuff will happen otherwise.
496	 */
497#if 0
498	if (s->s_bdev && s->s_bdev->bd_inode &&
499	    i_size_read(s->s_bdev->bd_inode) <
500	    sb_block_count(rs) * sb_blocksize(rs)) {
501		reiserfs_log(LOG_ERR,
502		    "reiserfs: filesystem cannot be mounted because it is "
503		    "bigger than the device.\n");
504		reiserfs_log(LOG_ERR, "reiserfs: you may need to run fsck "
505		    "rr may be you forgot to reboot after fdisk when it "
506		    "told you to.\n");
507		goto out;
508	}
509#endif
510
511	/*
512	 * XXX This is from the original Linux code, but why affecting 2 values
513	 * to the same variable?
514	 */
515	sbi->s_mount_state = SB_REISERFS_STATE(sbi);
516	sbi->s_mount_state = REISERFS_VALID_FS;
517
518	if ((error = (old_format ?
519	    read_old_bitmaps(rmp) : read_bitmaps(rmp)))) {
520		reiserfs_log(LOG_ERR, "unable to read bitmap\n");
521		goto out;
522	}
523
524	/* Make data=ordered the default */
525	if (!reiserfs_data_log(sbi) && !reiserfs_data_ordered(sbi) &&
526	    !reiserfs_data_writeback(sbi)) {
527		REISERFS_SB(sbi)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
528	}
529
530	if (reiserfs_data_log(sbi)) {
531		reiserfs_log(LOG_INFO, "using journaled data mode\n");
532	} else if (reiserfs_data_ordered(sbi)) {
533		reiserfs_log(LOG_INFO, "using ordered data mode\n");
534	} else {
535		reiserfs_log(LOG_INFO, "using writeback data mode\n");
536	}
537
538	/* TODO Not yet supported */
539#if 0
540	if(journal_init(sbi, jdev_name, old_format, commit_max_age)) {
541		reiserfs_log(LOG_ERR, "unable to initialize journal space\n");
542		goto out;
543	} else {
544		jinit_done = 1 ; /* once this is set, journal_release must
545				    be called if we error out of the mount */
546	}
547
548	if (reread_meta_blocks(sbi)) {
549		reiserfs_log(LOG_ERR,
550		    "unable to reread meta blocks after journal init\n");
551		goto out;
552	}
553#endif
554
555	/* Define and initialize hash function */
556	sbi->s_hash_function = hash_function(rmp);
557
558	if (sbi->s_hash_function == NULL) {
559		reiserfs_log(LOG_ERR, "couldn't determined hash function\n");
560		error = (EINVAL);
561		goto out;
562	}
563
564	if (is_reiserfs_3_5(rs) ||
565	    (is_reiserfs_jr(rs) && SB_VERSION(sbi) == REISERFS_VERSION_1))
566		bit_set(&(sbi->s_properties), REISERFS_3_5);
567	else
568		bit_set(&(sbi->s_properties), REISERFS_3_6);
569
570	mp->mnt_data = rmp;
571	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
572	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
573	MNT_ILOCK(mp);
574	mp->mnt_flag |= MNT_LOCAL;
575	MNT_IUNLOCK(mp);
576#if defined(si_mountpoint)
577	devvp->v_rdev->si_mountpoint = mp;
578#endif
579
580	return (0);
581
582out:
583	reiserfs_log(LOG_INFO, "*** error during mount ***\n");
584	if (sbi) {
585		if (SB_AP_BITMAP(sbi)) {
586			int i;
587			for (i = 0; i < SB_BMAP_NR(sbi); i++) {
588				if (!SB_AP_BITMAP(sbi)[i].bp_data)
589					break;
590				free(SB_AP_BITMAP(sbi)[i].bp_data, M_REISERFSMNT);
591			}
592			free(SB_AP_BITMAP(sbi), M_REISERFSMNT);
593		}
594
595		if (sbi->s_rs) {
596			free(sbi->s_rs, M_REISERFSMNT);
597			sbi->s_rs = NULL;
598		}
599	}
600
601	if (cp != NULL) {
602		DROP_GIANT();
603		g_topology_lock();
604		g_wither_geom_close(cp->geom, ENXIO);
605		g_topology_unlock();
606		PICKUP_GIANT();
607	}
608
609	if (sbi)
610		free(sbi, M_REISERFSMNT);
611	if (rmp)
612		free(rmp, M_REISERFSMNT);
613	return (error);
614}
615
616/*
617 * Read the super block
618 */
619static int
620read_super_block(struct reiserfs_mount *rmp, int offset)
621{
622	struct buf *bp;
623	int error, bits;
624	struct reiserfs_super_block *rs;
625	struct reiserfs_sb_info *sbi;
626	uint16_t fs_blocksize;
627
628	if (offset == REISERFS_OLD_DISK_OFFSET) {
629		reiserfs_log(LOG_DEBUG,
630		    "reiserfs/super: read old format super block\n");
631	} else {
632		reiserfs_log(LOG_DEBUG,
633		    "reiserfs/super: read new format super block\n");
634	}
635
636	/* Read the super block */
637	if ((error = bread(rmp->rm_devvp, offset * btodb(REISERFS_BSIZE),
638	    REISERFS_BSIZE, NOCRED, &bp)) != 0) {
639		reiserfs_log(LOG_ERR, "can't read device\n");
640		return (error);
641	}
642
643	/* Get it from the buffer data */
644	rs = (struct reiserfs_super_block *)bp->b_data;
645	if (!is_any_reiserfs_magic_string(rs)) {
646		brelse(bp);
647		return (EINVAL);
648	}
649
650	fs_blocksize = sb_blocksize(rs);
651	brelse(bp);
652	bp = NULL;
653
654	if (fs_blocksize <= 0) {
655		reiserfs_log(LOG_ERR, "unexpected null block size");
656		return (EINVAL);
657	}
658
659	/* Read the super block (for double check)
660	 * We can't read the same blkno with a different size: it causes
661	 * panic() if INVARIANTS is set. So we keep REISERFS_BSIZE */
662	if ((error = bread(rmp->rm_devvp,
663	    offset * REISERFS_BSIZE / fs_blocksize * btodb(fs_blocksize),
664	    REISERFS_BSIZE, NOCRED, &bp)) != 0) {
665		reiserfs_log(LOG_ERR, "can't reread the super block\n");
666		return (error);
667	}
668
669	rs = (struct reiserfs_super_block *)bp->b_data;
670	if (sb_blocksize(rs) != fs_blocksize) {
671		reiserfs_log(LOG_ERR, "unexpected block size "
672		    "(found=%u, expected=%u)\n",
673		    sb_blocksize(rs), fs_blocksize);
674		brelse(bp);
675		return (EINVAL);
676	}
677
678	reiserfs_log(LOG_DEBUG, "magic: `%s'\n", rs->s_v1.s_magic);
679	reiserfs_log(LOG_DEBUG, "label: `%s'\n", rs->s_label);
680	reiserfs_log(LOG_DEBUG, "block size:     %6d\n", sb_blocksize(rs));
681	reiserfs_log(LOG_DEBUG, "block count:    %6u\n",
682	    rs->s_v1.s_block_count);
683	reiserfs_log(LOG_DEBUG, "bitmaps number: %6u\n",
684	    rs->s_v1.s_bmap_nr);
685
686	if (rs->s_v1.s_root_block == -1) {
687		log(LOG_ERR,
688		    "reiserfs: Unfinished reiserfsck --rebuild-tree run "
689		    "detected. Please\n"
690		    "run reiserfsck --rebuild-tree and wait for a "
691		    "completion. If that\n"
692		    "fails, get newer reiserfsprogs package");
693		brelse(bp);
694		return (EINVAL);
695	}
696
697	sbi = rmp->rm_reiserfs;
698	sbi->s_blocksize = fs_blocksize;
699
700	for (bits = 9, fs_blocksize >>= 9; fs_blocksize >>= 1; bits++)
701		;
702	sbi->s_blocksize_bits = bits;
703
704	/* Copy the buffer and release it */
705	sbi->s_rs = malloc(sizeof *rs, M_REISERFSMNT, M_WAITOK | M_ZERO);
706	if (!sbi->s_rs) {
707		reiserfs_log(LOG_ERR, "can not read the super block\n");
708		brelse(bp);
709		return (ENOMEM);
710	}
711	bcopy(rs, sbi->s_rs, sizeof(struct reiserfs_super_block));
712	brelse(bp);
713
714	if (is_reiserfs_jr(rs)) {
715		if (sb_version(rs) == REISERFS_VERSION_2)
716			reiserfs_log(LOG_INFO, "found reiserfs format \"3.6\""
717			    " with non-standard journal");
718		else if (sb_version(rs) == REISERFS_VERSION_1)
719			reiserfs_log(LOG_INFO, "found reiserfs format \"3.5\""
720			    " with non-standard journal");
721		else {
722			reiserfs_log(LOG_ERR, "found unknown "
723			    "format \"%u\" of reiserfs with non-standard magic",
724			    sb_version(rs));
725			return (EINVAL);
726		}
727	} else {
728		/*
729		 * s_version of standard format may contain incorrect
730		 * information, so we just look at the magic string
731		 */
732		reiserfs_log(LOG_INFO,
733		    "found reiserfs format \"%s\" with standard journal\n",
734		    is_reiserfs_3_5(rs) ? "3.5" : "3.6");
735	}
736
737	return (0);
738}
739
740/*
741 * load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure
742 * from disk.
743 * @sbi - superblock info for this filesystem
744 * @bi  - the bitmap info to be loaded. Requires that bi->bp is valid.
745 *
746 * This routine counts how many free bits there are, finding the first
747 * zero as a side effect. Could also be implemented as a loop of
748 * test_bit() calls, or a loop of find_first_zero_bit() calls. This
749 * implementation is similar to find_first_zero_bit(), but doesn't
750 * return after it finds the first bit. Should only be called on fs
751 * mount, but should be fairly efficient anyways.
752 *
753 * bi->first_zero_hint is considered unset if it == 0, since the bitmap
754 * itself will invariably occupt block 0 represented in the bitmap. The
755 * only exception to this is when free_count also == 0, since there will
756 * be no free blocks at all.
757 */
758static void
759load_bitmap_info_data(struct reiserfs_sb_info *sbi,
760    struct reiserfs_bitmap_info *bi)
761{
762	unsigned long *cur;
763
764	cur = (unsigned long *)bi->bp_data;
765	while ((char *)cur < (bi->bp_data + sbi->s_blocksize)) {
766		/*
767		 * No need to scan if all 0's or all 1's.
768		 * Since we're only counting 0's, we can simply ignore
769		 * all 1's
770		 */
771		if (*cur == 0) {
772			if (bi->first_zero_hint == 0) {
773				bi->first_zero_hint =
774				    ((char *)cur - bi->bp_data) << 3;
775			}
776			bi->free_count += sizeof(unsigned long) * 8;
777		} else if (*cur != ~0L) {
778			int b;
779
780			for (b = 0; b < sizeof(unsigned long) * 8; b++) {
781				if (!reiserfs_test_le_bit(b, cur)) {
782					bi->free_count++;
783					if (bi->first_zero_hint == 0)
784						bi->first_zero_hint =
785						    (((char *)cur -
786						      bi->bp_data) << 3) + b;
787				}
788			}
789		}
790		cur++;
791	}
792}
793
794/*
795 * Read the bitmaps
796 */
797static int
798read_bitmaps(struct reiserfs_mount *rmp)
799{
800	int i, bmap_nr;
801	struct buf *bp = NULL;
802	struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
803
804	/* Allocate memory for the table of bitmaps */
805	SB_AP_BITMAP(sbi) =
806	    malloc(sizeof(struct reiserfs_bitmap_info) * SB_BMAP_NR(sbi),
807		M_REISERFSMNT, M_WAITOK | M_ZERO);
808	if (!SB_AP_BITMAP(sbi))
809		return (ENOMEM);
810
811	/* Read all the bitmaps */
812	for (i = 0,
813	    bmap_nr = (REISERFS_DISK_OFFSET_IN_BYTES / sbi->s_blocksize + 1) *
814	    btodb(sbi->s_blocksize);
815	    i < SB_BMAP_NR(sbi); i++, bmap_nr = sbi->s_blocksize * 8 * i) {
816		SB_AP_BITMAP(sbi)[i].bp_data = malloc(sbi->s_blocksize,
817		    M_REISERFSMNT, M_WAITOK | M_ZERO);
818		if (!SB_AP_BITMAP(sbi)[i].bp_data)
819			return (ENOMEM);
820		bread(rmp->rm_devvp, bmap_nr, sbi->s_blocksize, NOCRED, &bp);
821		bcopy(bp->b_data, SB_AP_BITMAP(sbi)[i].bp_data,
822		    sbi->s_blocksize);
823		brelse(bp);
824		bp = NULL;
825
826		/*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
827			ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);*/
828	}
829
830	for (i = 0; i < SB_BMAP_NR(sbi); i++) {
831		/*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
832		  reiserfs_warning(s,"sh-2029: reiserfs read_bitmaps: "
833		  "bitmap block (#%lu) reading failed",
834		  SB_AP_BITMAP(s)[i].bh->b_blocknr);
835		  for (i = 0; i < SB_BMAP_NR(s); i++)
836		  brelse(SB_AP_BITMAP(s)[i].bh);
837		  vfree(SB_AP_BITMAP(s));
838		  SB_AP_BITMAP(s) = NULL;
839		  return 1;
840		  }*/
841		load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i);
842		reiserfs_log(LOG_DEBUG,
843		    "%d free blocks (starting at block %ld)\n",
844		    SB_AP_BITMAP(sbi)[i].free_count,
845		    (long)SB_AP_BITMAP(sbi)[i].first_zero_hint);
846	}
847
848	return (0);
849}
850
851// TODO Not supported
852static int
853read_old_bitmaps(struct reiserfs_mount *rmp)
854{
855
856	return (EOPNOTSUPP);
857#if 0
858	int i;
859	struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
860	struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(sbi);
861
862	/* First of bitmap blocks */
863	int bmp1 = (REISERFS_OLD_DISK_OFFSET / sbi->s_blocksize) *
864	    btodb(sbi->s_blocksize);
865
866	/* Read true bitmap */
867	SB_AP_BITMAP(sbi) =
868	    malloc(sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs),
869		M_REISERFSMNT, M_WAITOK | M_ZERO);
870	if (!SB_AP_BITMAP(sbi))
871		return 1;
872
873	for (i = 0; i < sb_bmap_nr(rs); i ++) {
874		SB_AP_BITMAP(sbi)[i].bp = getblk(rmp->rm_devvp,
875		    (bmp1 + i) * btodb(sbi->s_blocksize), sbi->s_blocksize, 0, 0, 0);
876		if (!SB_AP_BITMAP(sbi)[i].bp)
877			return 1;
878		load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i);
879	}
880
881	return 0;
882#endif
883}
884
885/* -------------------------------------------------------------------
886 * Hash detection stuff
887 * -------------------------------------------------------------------*/
888
889static int
890get_root_node(struct reiserfs_mount *rmp, struct reiserfs_node **root)
891{
892	struct reiserfs_node *ip;
893	struct reiserfs_iget_args args;
894
895	/* Allocate the node structure */
896	reiserfs_log(LOG_DEBUG, "malloc(struct reiserfs_node)\n");
897	ip = malloc(sizeof(struct reiserfs_node),
898	    M_REISERFSNODE, M_WAITOK | M_ZERO);
899
900	/* Fill the structure */
901	reiserfs_log(LOG_DEBUG, "filling *ip\n");
902	ip->i_dev      = rmp->rm_dev;
903	ip->i_number   = REISERFS_ROOT_OBJECTID;
904	ip->i_ino      = REISERFS_ROOT_PARENT_OBJECTID;
905	ip->i_reiserfs = rmp->rm_reiserfs;
906
907	/* Read the inode */
908	args.objectid = ip->i_number;
909	args.dirid    = ip->i_ino;
910	reiserfs_log(LOG_DEBUG, "call reiserfs_read_locked_inode("
911	    "objectid=%d,dirid=%d)\n", args.objectid, args.dirid);
912	reiserfs_read_locked_inode(ip, &args);
913
914	ip->i_devvp = rmp->rm_devvp;
915	//XXX VREF(ip->i_devvp); Is it necessary ?
916
917	*root = ip;
918	return (0);
919}
920
921/*
922 * If root directory is empty - we set default - Yura's - hash and warn
923 * about it.
924 * FIXME: we look for only one name in a directory. If tea and yura both
925 * have the same value - we ask user to send report to the mailing list
926 */
927uint32_t find_hash_out(struct reiserfs_mount *rmp)
928{
929	int retval;
930	struct cpu_key key;
931	INITIALIZE_PATH(path);
932	struct reiserfs_node *ip;
933	struct reiserfs_sb_info *sbi;
934	struct reiserfs_dir_entry de;
935	uint32_t hash = DEFAULT_HASH;
936
937	get_root_node(rmp, &ip);
938	if (!ip)
939		return (UNSET_HASH);
940
941	sbi = rmp->rm_reiserfs;
942
943	do {
944		uint32_t teahash, r5hash, yurahash;
945
946		reiserfs_log(LOG_DEBUG, "make_cpu_key\n");
947		make_cpu_key(&key, ip, ~0, TYPE_DIRENTRY, 3);
948		reiserfs_log(LOG_DEBUG, "search_by_entry_key for "
949		    "key(objectid=%d,dirid=%d)\n",
950		    key.on_disk_key.k_objectid, key.on_disk_key.k_dir_id);
951		retval = search_by_entry_key(sbi, &key, &path, &de);
952		if (retval == IO_ERROR) {
953			pathrelse(&path);
954			return (UNSET_HASH);
955		}
956		if (retval == NAME_NOT_FOUND)
957			de.de_entry_num--;
958
959		reiserfs_log(LOG_DEBUG, "name found\n");
960
961		set_de_name_and_namelen(&de);
962
963		if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
964			/* Allow override in this case */
965			if (reiserfs_rupasov_hash(sbi)) {
966				hash = YURA_HASH;
967			}
968			reiserfs_log(LOG_DEBUG,
969			    "FS seems to be empty, autodetect "
970			    "is using the default hash");
971			break;
972		}
973
974		r5hash   = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
975		teahash  = GET_HASH_VALUE(keyed_hash(de.de_name,
976		    de.de_namelen));
977		yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
978		if (((teahash == r5hash) &&
979		    (GET_HASH_VALUE(
980		     deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)) ||
981		    ((teahash == yurahash) &&
982		     (yurahash ==
983		      GET_HASH_VALUE(
984		      deh_offset(&(de.de_deh[de.de_entry_num]))))) ||
985		    ((r5hash == yurahash) &&
986		     (yurahash ==
987		      GET_HASH_VALUE(
988		      deh_offset(&(de.de_deh[de.de_entry_num])))))) {
989			reiserfs_log(LOG_ERR,
990			    "unable to automatically detect hash "
991			    "function. Please mount with -o "
992			    "hash={tea,rupasov,r5}");
993			hash = UNSET_HASH;
994			break;
995		}
996
997		if (GET_HASH_VALUE(
998		    deh_offset(&(de.de_deh[de.de_entry_num]))) == yurahash) {
999			reiserfs_log(LOG_DEBUG, "detected YURA hash\n");
1000			hash = YURA_HASH;
1001		} else if (GET_HASH_VALUE(
1002		    deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash) {
1003			reiserfs_log(LOG_DEBUG, "detected TEA hash\n");
1004			hash = TEA_HASH;
1005		} else if (GET_HASH_VALUE(
1006		    deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) {
1007			reiserfs_log(LOG_DEBUG, "detected R5 hash\n");
1008			hash = R5_HASH;
1009		} else {
1010			reiserfs_log(LOG_WARNING, "unrecognised hash function");
1011			hash = UNSET_HASH;
1012		}
1013	} while (0);
1014
1015	pathrelse(&path);
1016	return (hash);
1017}
1018
1019/* Finds out which hash names are sorted with */
1020static int
1021what_hash(struct reiserfs_mount *rmp)
1022{
1023	uint32_t code;
1024	struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
1025
1026	find_hash_out(rmp);
1027	code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(sbi));
1028
1029	/*
1030	 * reiserfs_hash_detect() == true if any of the hash mount options
1031	 * were used. We must check them to make sure the user isn't using a
1032	 * bad hash value
1033	 */
1034	if (code == UNSET_HASH || reiserfs_hash_detect(sbi))
1035		code = find_hash_out(rmp);
1036
1037	if (code != UNSET_HASH && reiserfs_hash_detect(sbi)) {
1038		/*
1039		 * Detection has found the hash, and we must check against
1040		 * the mount options
1041		 */
1042		if (reiserfs_rupasov_hash(sbi) && code != YURA_HASH) {
1043			reiserfs_log(LOG_ERR, "error, %s hash detected, "
1044			    "unable to force rupasov hash",
1045			    reiserfs_hashname(code));
1046			code = UNSET_HASH;
1047		} else if (reiserfs_tea_hash(sbi) && code != TEA_HASH) {
1048			reiserfs_log(LOG_ERR, "error, %s hash detected, "
1049			    "unable to force tea hash",
1050			    reiserfs_hashname(code));
1051			code = UNSET_HASH;
1052		} else if (reiserfs_r5_hash(sbi) && code != R5_HASH) {
1053			reiserfs_log(LOG_ERR, "error, %s hash detected, "
1054			    "unable to force r5 hash",
1055			    reiserfs_hashname(code));
1056			code = UNSET_HASH;
1057		}
1058	} else {
1059		/*
1060		 * Find_hash_out was not called or could not determine
1061		 * the hash
1062		 */
1063		if (reiserfs_rupasov_hash(sbi)) {
1064			code = YURA_HASH;
1065		} else if (reiserfs_tea_hash(sbi)) {
1066			code = TEA_HASH;
1067		} else if (reiserfs_r5_hash(sbi)) {
1068			code = R5_HASH;
1069		}
1070	}
1071
1072	/* TODO Not supported yet */
1073#if 0
1074	/* If we are mounted RW, and we have a new valid hash code, update
1075	 * the super */
1076	if (code != UNSET_HASH &&
1077	    !(s->s_flags & MS_RDONLY) &&
1078	    code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1079		set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1080	}
1081#endif
1082
1083	return (code);
1084}
1085
1086/* Return pointer to appropriate function */
1087static hashf_t
1088hash_function(struct reiserfs_mount *rmp)
1089{
1090
1091	switch (what_hash(rmp)) {
1092	case TEA_HASH:
1093		reiserfs_log(LOG_INFO, "using tea hash to sort names\n");
1094		return (keyed_hash);
1095	case YURA_HASH:
1096		reiserfs_log(LOG_INFO, "using rupasov hash to sort names\n");
1097		return (yura_hash);
1098	case R5_HASH:
1099		reiserfs_log(LOG_INFO, "using r5 hash to sort names\n");
1100		return (r5_hash);
1101	}
1102
1103	return (NULL);
1104}
1105
1106/* -------------------------------------------------------------------
1107 * VFS registration
1108 * -------------------------------------------------------------------*/
1109
1110static struct vfsops reiser_vfsops = {
1111	.vfs_cmount	= reiserfs_cmount,
1112	.vfs_mount	= reiserfs_mount,
1113	.vfs_unmount	= reiserfs_unmount,
1114	//.vfs_checkexp	= reiserfs_checkexp,
1115	//.vfs_extattrctl = reiserfs_extattrctl,
1116	.vfs_fhtovp	= reiserfs_fhtovp,
1117	//.vfs_quotactl	= reiserfs_quotactl,
1118	.vfs_root	= reiserfs_root,
1119	//.vfs_start	= reiserfs_start,
1120	.vfs_statfs	= reiserfs_statfs,
1121	//.vfs_sync	= reiserfs_sync,
1122	//.vfs_vget	= reiserfs_vget,
1123};
1124
1125VFS_SET(reiser_vfsops, reiserfs, VFCF_READONLY);
1126