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