]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/gnu/fs/reiserfs/reiserfs_vfsops.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / 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
30 static int      reiserfs_mountfs(struct vnode *devvp, struct mount *mp,
31                     struct thread *td);
32 static void     load_bitmap_info_data(struct reiserfs_sb_info *sbi,
33                     struct reiserfs_bitmap_info *bi);
34 static int      read_bitmaps(struct reiserfs_mount *rmp);
35 static int      read_old_bitmaps(struct reiserfs_mount *rmp);
36 static int      read_super_block(struct reiserfs_mount *rmp, int offset);
37 static hashf_t  hash_function(struct reiserfs_mount *rmp);
38
39 static int      get_root_node(struct reiserfs_mount *rmp,
40                     struct reiserfs_node **root);
41 uint32_t        find_hash_out(struct reiserfs_mount *rmp);
42
43 MALLOC_DEFINE(M_REISERFSMNT, "reiserfs_mount", "ReiserFS mount structure");
44 MALLOC_DEFINE(M_REISERFSPATH, "reiserfs_path", "ReiserFS path structure");
45 MALLOC_DEFINE(M_REISERFSNODE, "reiserfs_node", "ReiserFS vnode private part");
46
47 /* -------------------------------------------------------------------
48  * VFS operations
49  * -------------------------------------------------------------------*/
50
51 static int
52 reiserfs_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  */
72 static int
73 reiserfs_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  */
172 static int
173 reiserfs_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  */ 
256 static int
257 reiserfs_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  */
276 static int
277 reiserfs_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  */
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  * Functions for the journal
382  * -------------------------------------------------------------------*/
383
384 int
385 is_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
392 int
393 is_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
400 int
401 is_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
408 static int
409 is_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  */ 
423 static int
424 reiserfs_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 #if (__FreeBSD_version >= 600000)
433         struct g_consumer *cp;
434         struct bufobj *bo;
435 #endif
436
437         //ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
438
439 #if (__FreeBSD_version < 600000)
440         /*
441          * Disallow multiple mounts of the same device.
442          * Disallow mounting of a device that is currently in use
443          * (except for root, which might share swap device for miniroot).
444          * Flush out any old buffers remaining from a previous use.
445          */
446         if ((error = vfs_mountedon(devvp)) != 0)
447                 return (error);
448         if (vcount(devvp) > 1)
449                 return (EBUSY);
450
451         error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0);
452         if (error) {
453                 VOP_UNLOCK(devvp, 0);
454                 return (error);
455         }
456
457         /*
458          * Open the device in read-only, 'cause we don't support write
459          * for now
460          */
461         error = VOP_OPEN(devvp, FREAD, FSCRED, td, NULL);
462         VOP_UNLOCK(devvp, 0);
463         if (error)
464                 return (error);
465 #else
466         DROP_GIANT();
467         g_topology_lock();
468         error = g_vfs_open(devvp, &cp, "reiserfs", /* read-only */ 0);
469         g_topology_unlock();
470         PICKUP_GIANT();
471         VOP_UNLOCK(devvp, 0);
472         if (error)
473                 return (error);
474
475         bo = &devvp->v_bufobj;
476         bo->bo_private = cp;
477         bo->bo_ops = g_vfs_bufops;
478 #endif
479
480         if (devvp->v_rdev->si_iosize_max != 0)
481                 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
482         if (mp->mnt_iosize_max > MAXPHYS)
483                 mp->mnt_iosize_max = MAXPHYS;
484
485         rmp = NULL;
486         sbi = NULL;
487
488         /* rmp contains any information about this specific mount */
489         rmp = malloc(sizeof *rmp, M_REISERFSMNT, M_WAITOK | M_ZERO);
490         if (!rmp) {
491                 error = (ENOMEM);
492                 goto out;
493         }
494         sbi = malloc(sizeof *sbi, M_REISERFSMNT, M_WAITOK | M_ZERO);
495         if (!sbi) {
496                 error = (ENOMEM);
497                 goto out;
498         }
499         rmp->rm_reiserfs = sbi;
500         rmp->rm_mountp   = mp;
501         rmp->rm_devvp    = devvp;
502         rmp->rm_dev      = dev;
503 #if (__FreeBSD_version >= 600000)
504         rmp->rm_bo       = &devvp->v_bufobj;
505         rmp->rm_cp       = cp;
506 #endif
507
508         /* Set default values for options: non-aggressive tails */
509         REISERFS_SB(sbi)->s_mount_opt = (1 << REISERFS_SMALLTAIL);
510         REISERFS_SB(sbi)->s_rd_only   = 1;
511         REISERFS_SB(sbi)->s_devvp     = devvp;
512
513         /* Read the super block */
514         if ((error = read_super_block(rmp, REISERFS_OLD_DISK_OFFSET)) == 0) {
515                 /* The read process succeeded, it's an old format */
516                 old_format = 1;
517         } else if ((error = read_super_block(rmp, REISERFS_DISK_OFFSET)) != 0) {
518                 reiserfs_log(LOG_ERR, "can not find a ReiserFS filesystem\n");
519                 goto out;
520         }
521
522         rs = SB_DISK_SUPER_BLOCK(sbi);
523
524         /*
525          * Let's do basic sanity check to verify that underlying device is
526          * not smaller than the filesystem. If the check fails then abort and
527          * scream, because bad stuff will happen otherwise.
528          */
529 #if 0
530         if (s->s_bdev && s->s_bdev->bd_inode &&
531             i_size_read(s->s_bdev->bd_inode) <
532             sb_block_count(rs) * sb_blocksize(rs)) {
533                 reiserfs_log(LOG_ERR,
534                     "reiserfs: filesystem cannot be mounted because it is "
535                     "bigger than the device.\n");
536                 reiserfs_log(LOG_ERR, "reiserfs: you may need to run fsck "
537                     "rr may be you forgot to reboot after fdisk when it "
538                     "told you to.\n");
539                 goto out;
540         }
541 #endif
542
543         /*
544          * XXX This is from the original Linux code, but why affecting 2 values
545          * to the same variable?
546          */
547         sbi->s_mount_state = SB_REISERFS_STATE(sbi);
548         sbi->s_mount_state = REISERFS_VALID_FS;
549
550         if ((error = (old_format ?
551             read_old_bitmaps(rmp) : read_bitmaps(rmp)))) {
552                 reiserfs_log(LOG_ERR, "unable to read bitmap\n");
553                 goto out;
554         }
555
556         /* Make data=ordered the default */
557         if (!reiserfs_data_log(sbi) && !reiserfs_data_ordered(sbi) &&
558             !reiserfs_data_writeback(sbi)) {
559                 REISERFS_SB(sbi)->s_mount_opt |= (1 << REISERFS_DATA_ORDERED);
560         }
561
562         if (reiserfs_data_log(sbi)) {
563                 reiserfs_log(LOG_INFO, "using journaled data mode\n");
564         } else if (reiserfs_data_ordered(sbi)) {
565                 reiserfs_log(LOG_INFO, "using ordered data mode\n");
566         } else {
567                 reiserfs_log(LOG_INFO, "using writeback data mode\n");
568         }
569
570         /* TODO Not yet supported */
571 #if 0
572         if(journal_init(sbi, jdev_name, old_format, commit_max_age)) {
573                 reiserfs_log(LOG_ERR, "unable to initialize journal space\n");
574                 goto out;
575         } else {
576                 jinit_done = 1 ; /* once this is set, journal_release must
577                                     be called if we error out of the mount */
578         }
579
580         if (reread_meta_blocks(sbi)) {
581                 reiserfs_log(LOG_ERR,
582                     "unable to reread meta blocks after journal init\n");
583                 goto out;
584         }
585 #endif
586
587         /* Define and initialize hash function */
588         sbi->s_hash_function = hash_function(rmp);
589
590         if (sbi->s_hash_function == NULL) {
591                 reiserfs_log(LOG_ERR, "couldn't determined hash function\n");
592                 error = (EINVAL);
593                 goto out;
594         }
595
596         if (is_reiserfs_3_5(rs) ||
597             (is_reiserfs_jr(rs) && SB_VERSION(sbi) == REISERFS_VERSION_1))
598                 bit_set(&(sbi->s_properties), REISERFS_3_5);
599         else
600                 bit_set(&(sbi->s_properties), REISERFS_3_6);
601
602         mp->mnt_data = rmp;
603         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
604         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
605         MNT_ILOCK(mp);
606         mp->mnt_flag |= MNT_LOCAL;
607         MNT_IUNLOCK(mp);
608 #if defined(si_mountpoint)
609         devvp->v_rdev->si_mountpoint = mp;
610 #endif
611
612         return (0);
613
614 out:
615         reiserfs_log(LOG_INFO, "*** error during mount ***\n");
616         if (sbi) {
617                 if (SB_AP_BITMAP(sbi)) {
618                         int i;
619                         for (i = 0; i < SB_BMAP_NR(sbi); i++) {
620                                 if (!SB_AP_BITMAP(sbi)[i].bp_data)
621                                         break;
622                                 free(SB_AP_BITMAP(sbi)[i].bp_data, M_REISERFSMNT);
623                         }
624                         free(SB_AP_BITMAP(sbi), M_REISERFSMNT);
625                 }
626
627                 if (sbi->s_rs) {
628                         free(sbi->s_rs, M_REISERFSMNT);
629                         sbi->s_rs = NULL;
630                 }
631         }
632
633 #if (__FreeBSD_version < 600000)
634         (void)VOP_CLOSE(devvp, FREAD, NOCRED, td);
635 #else
636         if (cp != NULL) {
637                 DROP_GIANT();
638                 g_topology_lock();
639                 g_wither_geom_close(cp->geom, ENXIO);
640                 g_topology_unlock();
641                 PICKUP_GIANT();
642         }
643 #endif
644
645         if (sbi)
646                 free(sbi, M_REISERFSMNT);
647         if (rmp)
648                 free(rmp, M_REISERFSMNT);
649         return (error);
650 }
651
652 /*
653  * Read the super block
654  */
655 static int
656 read_super_block(struct reiserfs_mount *rmp, int offset)
657 {
658         struct buf *bp;
659         int error, bits;
660         struct reiserfs_super_block *rs;
661         struct reiserfs_sb_info *sbi;
662         uint16_t fs_blocksize;
663
664         if (offset == REISERFS_OLD_DISK_OFFSET) {
665                 reiserfs_log(LOG_DEBUG,
666                     "reiserfs/super: read old format super block\n");
667         } else {
668                 reiserfs_log(LOG_DEBUG,
669                     "reiserfs/super: read new format super block\n");
670         }
671
672         /* Read the super block */
673         if ((error = bread(rmp->rm_devvp, offset * btodb(REISERFS_BSIZE),
674             REISERFS_BSIZE, NOCRED, &bp)) != 0) {
675                 reiserfs_log(LOG_ERR, "can't read device\n");
676                 return (error);
677         }
678
679         /* Get it from the buffer data */
680         rs = (struct reiserfs_super_block *)bp->b_data;
681         if (!is_any_reiserfs_magic_string(rs)) {
682                 brelse(bp);
683                 return (EINVAL);
684         }
685
686         fs_blocksize = sb_blocksize(rs);
687         brelse(bp);
688         bp = NULL;
689
690         if (fs_blocksize <= 0) {
691                 reiserfs_log(LOG_ERR, "unexpected null block size");
692                 return (EINVAL);
693         }
694
695         /* Read the super block (for double check)
696          * We can't read the same blkno with a different size: it causes
697          * panic() if INVARIANTS is set. So we keep REISERFS_BSIZE */
698         if ((error = bread(rmp->rm_devvp,
699             offset * REISERFS_BSIZE / fs_blocksize * btodb(fs_blocksize),
700             REISERFS_BSIZE, NOCRED, &bp)) != 0) {
701                 reiserfs_log(LOG_ERR, "can't reread the super block\n");
702                 return (error);
703         }
704
705         rs = (struct reiserfs_super_block *)bp->b_data;
706         if (sb_blocksize(rs) != fs_blocksize) {
707                 reiserfs_log(LOG_ERR, "unexpected block size "
708                     "(found=%u, expected=%u)\n",
709                     sb_blocksize(rs), fs_blocksize);
710                 brelse(bp);
711                 return (EINVAL);
712         }
713
714         reiserfs_log(LOG_DEBUG, "magic: `%s'\n", rs->s_v1.s_magic);
715         reiserfs_log(LOG_DEBUG, "label: `%s'\n", rs->s_label);
716         reiserfs_log(LOG_DEBUG, "block size:     %6d\n", sb_blocksize(rs));
717         reiserfs_log(LOG_DEBUG, "block count:    %6u\n",
718             rs->s_v1.s_block_count);
719         reiserfs_log(LOG_DEBUG, "bitmaps number: %6u\n",
720             rs->s_v1.s_bmap_nr);
721
722         if (rs->s_v1.s_root_block == -1) {
723                 log(LOG_ERR,
724                     "reiserfs: Unfinished reiserfsck --rebuild-tree run "
725                     "detected. Please\n"
726                     "run reiserfsck --rebuild-tree and wait for a "
727                     "completion. If that\n"
728                     "fails, get newer reiserfsprogs package");
729                 brelse(bp);
730                 return (EINVAL);
731         }
732
733         sbi = rmp->rm_reiserfs;
734         sbi->s_blocksize = fs_blocksize;
735
736         for (bits = 9, fs_blocksize >>= 9; fs_blocksize >>= 1; bits++)
737                 ;
738         sbi->s_blocksize_bits = bits;
739
740         /* Copy the buffer and release it */
741         sbi->s_rs = malloc(sizeof *rs, M_REISERFSMNT, M_WAITOK | M_ZERO);
742         if (!sbi->s_rs) {
743                 reiserfs_log(LOG_ERR, "can not read the super block\n");
744                 brelse(bp);
745                 return (ENOMEM);
746         }
747         bcopy(rs, sbi->s_rs, sizeof(struct reiserfs_super_block));
748         brelse(bp);
749
750         if (is_reiserfs_jr(rs)) {
751                 if (sb_version(rs) == REISERFS_VERSION_2)
752                         reiserfs_log(LOG_INFO, "found reiserfs format \"3.6\""
753                             " with non-standard journal");
754                 else if (sb_version(rs) == REISERFS_VERSION_1)
755                         reiserfs_log(LOG_INFO, "found reiserfs format \"3.5\""
756                             " with non-standard journal");
757                 else {
758                         reiserfs_log(LOG_ERR, "found unknown "
759                             "format \"%u\" of reiserfs with non-standard magic",
760                             sb_version(rs));
761                         return (EINVAL);
762                 }
763         } else {
764                 /*
765                  * s_version of standard format may contain incorrect
766                  * information, so we just look at the magic string
767                  */
768                 reiserfs_log(LOG_INFO,
769                     "found reiserfs format \"%s\" with standard journal\n",
770                     is_reiserfs_3_5(rs) ? "3.5" : "3.6");
771         }
772
773         return (0);
774 }
775
776 /*
777  * load_bitmap_info_data - Sets up the reiserfs_bitmap_info structure
778  * from disk.
779  * @sbi - superblock info for this filesystem
780  * @bi  - the bitmap info to be loaded. Requires that bi->bp is valid.
781  *
782  * This routine counts how many free bits there are, finding the first
783  * zero as a side effect. Could also be implemented as a loop of
784  * test_bit() calls, or a loop of find_first_zero_bit() calls. This
785  * implementation is similar to find_first_zero_bit(), but doesn't
786  * return after it finds the first bit. Should only be called on fs
787  * mount, but should be fairly efficient anyways.
788  *
789  * bi->first_zero_hint is considered unset if it == 0, since the bitmap
790  * itself will invariably occupt block 0 represented in the bitmap. The
791  * only exception to this is when free_count also == 0, since there will
792  * be no free blocks at all.
793  */
794 static void
795 load_bitmap_info_data(struct reiserfs_sb_info *sbi,
796     struct reiserfs_bitmap_info *bi)
797 {
798         unsigned long *cur;
799
800         cur = (unsigned long *)bi->bp_data;
801         while ((char *)cur < (bi->bp_data + sbi->s_blocksize)) {
802                 /*
803                  * No need to scan if all 0's or all 1's.
804                  * Since we're only counting 0's, we can simply ignore
805                  * all 1's
806                  */
807                 if (*cur == 0) {
808                         if (bi->first_zero_hint == 0) {
809                                 bi->first_zero_hint =
810                                     ((char *)cur - bi->bp_data) << 3;
811                         }
812                         bi->free_count += sizeof(unsigned long) * 8;
813                 } else if (*cur != ~0L) {
814                         int b;
815
816                         for (b = 0; b < sizeof(unsigned long) * 8; b++) {
817                                 if (!reiserfs_test_le_bit(b, cur)) {
818                                         bi->free_count++;
819                                         if (bi->first_zero_hint == 0)
820                                                 bi->first_zero_hint =
821                                                     (((char *)cur -
822                                                       bi->bp_data) << 3) + b;
823                                 }
824                         }
825                 }
826                 cur++;
827         }
828 }
829
830 /*
831  * Read the bitmaps
832  */
833 static int
834 read_bitmaps(struct reiserfs_mount *rmp)
835 {
836         int i, bmap_nr;
837         struct buf *bp = NULL;
838         struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
839
840         /* Allocate memory for the table of bitmaps */
841         SB_AP_BITMAP(sbi) =
842             malloc(sizeof(struct reiserfs_bitmap_info) * SB_BMAP_NR(sbi),
843                 M_REISERFSMNT, M_WAITOK | M_ZERO);
844         if (!SB_AP_BITMAP(sbi))
845                 return (ENOMEM);
846
847         /* Read all the bitmaps */
848         for (i = 0,
849             bmap_nr = (REISERFS_DISK_OFFSET_IN_BYTES / sbi->s_blocksize + 1) *
850             btodb(sbi->s_blocksize);
851             i < SB_BMAP_NR(sbi); i++, bmap_nr = sbi->s_blocksize * 8 * i) {
852                 SB_AP_BITMAP(sbi)[i].bp_data = malloc(sbi->s_blocksize,
853                     M_REISERFSMNT, M_WAITOK | M_ZERO);
854                 if (!SB_AP_BITMAP(sbi)[i].bp_data)
855                         return (ENOMEM);
856                 bread(rmp->rm_devvp, bmap_nr, sbi->s_blocksize, NOCRED, &bp);
857                 bcopy(bp->b_data, SB_AP_BITMAP(sbi)[i].bp_data,
858                     sbi->s_blocksize);
859                 brelse(bp);
860                 bp = NULL;
861
862                 /*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh))
863                         ll_rw_block(READ, 1, &SB_AP_BITMAP(s)[i].bh);*/
864         }
865
866         for (i = 0; i < SB_BMAP_NR(sbi); i++) {
867                 /*if (!buffer_uptodate(SB_AP_BITMAP(s)[i].bh)) {
868                   reiserfs_warning(s,"sh-2029: reiserfs read_bitmaps: "
869                   "bitmap block (#%lu) reading failed",
870                   SB_AP_BITMAP(s)[i].bh->b_blocknr);
871                   for (i = 0; i < SB_BMAP_NR(s); i++)
872                   brelse(SB_AP_BITMAP(s)[i].bh);
873                   vfree(SB_AP_BITMAP(s));
874                   SB_AP_BITMAP(s) = NULL;
875                   return 1;
876                   }*/
877                 load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i);
878                 reiserfs_log(LOG_DEBUG,
879                     "%d free blocks (starting at block %ld)\n",
880                     SB_AP_BITMAP(sbi)[i].free_count,
881                     (long)SB_AP_BITMAP(sbi)[i].first_zero_hint);
882         }
883
884         return (0);
885 }
886
887 // TODO Not supported
888 static int
889 read_old_bitmaps(struct reiserfs_mount *rmp)
890 {
891
892         return (EOPNOTSUPP);
893 #if 0
894         int i;
895         struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
896         struct reiserfs_super_block *rs = SB_DISK_SUPER_BLOCK(sbi);
897
898         /* First of bitmap blocks */
899         int bmp1 = (REISERFS_OLD_DISK_OFFSET / sbi->s_blocksize) *
900             btodb(sbi->s_blocksize);
901
902         /* Read true bitmap */
903         SB_AP_BITMAP(sbi) =
904             malloc(sizeof (struct reiserfs_buffer_info *) * sb_bmap_nr(rs),
905                 M_REISERFSMNT, M_WAITOK | M_ZERO);
906         if (!SB_AP_BITMAP(sbi))
907                 return 1;
908
909         for (i = 0; i < sb_bmap_nr(rs); i ++) {
910                 SB_AP_BITMAP(sbi)[i].bp = getblk(rmp->rm_devvp,
911                     (bmp1 + i) * btodb(sbi->s_blocksize), sbi->s_blocksize, 0, 0, 0);
912                 if (!SB_AP_BITMAP(sbi)[i].bp)
913                         return 1;
914                 load_bitmap_info_data(sbi, SB_AP_BITMAP(sbi) + i);
915         }
916
917         return 0;
918 #endif
919 }
920
921 /* -------------------------------------------------------------------
922  * Hash detection stuff
923  * -------------------------------------------------------------------*/
924
925 static int
926 get_root_node(struct reiserfs_mount *rmp, struct reiserfs_node **root)
927 {
928         struct reiserfs_node *ip;
929         struct reiserfs_iget_args args;
930
931         /* Allocate the node structure */
932         reiserfs_log(LOG_DEBUG, "malloc(struct reiserfs_node)\n");
933         ip = malloc(sizeof(struct reiserfs_node),
934             M_REISERFSNODE, M_WAITOK | M_ZERO);
935
936         /* Fill the structure */
937         reiserfs_log(LOG_DEBUG, "filling *ip\n");
938         ip->i_dev      = rmp->rm_dev;
939         ip->i_number   = REISERFS_ROOT_OBJECTID;
940         ip->i_ino      = REISERFS_ROOT_PARENT_OBJECTID;
941         ip->i_reiserfs = rmp->rm_reiserfs;
942
943         /* Read the inode */
944         args.objectid = ip->i_number;
945         args.dirid    = ip->i_ino;
946         reiserfs_log(LOG_DEBUG, "call reiserfs_read_locked_inode("
947             "objectid=%d,dirid=%d)\n", args.objectid, args.dirid);
948         reiserfs_read_locked_inode(ip, &args);
949
950         ip->i_devvp = rmp->rm_devvp;
951         //XXX VREF(ip->i_devvp); Is it necessary ?
952
953         *root = ip;
954         return (0);
955 }
956
957 /*
958  * If root directory is empty - we set default - Yura's - hash and warn
959  * about it.
960  * FIXME: we look for only one name in a directory. If tea and yura both
961  * have the same value - we ask user to send report to the mailing list
962  */
963 uint32_t find_hash_out(struct reiserfs_mount *rmp)
964 {
965         int retval;
966         struct cpu_key key;
967         INITIALIZE_PATH(path);
968         struct reiserfs_node *ip;
969         struct reiserfs_sb_info *sbi;
970         struct reiserfs_dir_entry de;
971         uint32_t hash = DEFAULT_HASH;
972
973         get_root_node(rmp, &ip);
974         if (!ip)
975                 return (UNSET_HASH);
976
977         sbi = rmp->rm_reiserfs;
978
979         do {
980                 uint32_t teahash, r5hash, yurahash;
981
982                 reiserfs_log(LOG_DEBUG, "make_cpu_key\n");
983                 make_cpu_key(&key, ip, ~0, TYPE_DIRENTRY, 3);
984                 reiserfs_log(LOG_DEBUG, "search_by_entry_key for "
985                     "key(objectid=%d,dirid=%d)\n",
986                     key.on_disk_key.k_objectid, key.on_disk_key.k_dir_id);
987                 retval = search_by_entry_key(sbi, &key, &path, &de);
988                 if (retval == IO_ERROR) {
989                         pathrelse(&path);
990                         return (UNSET_HASH);
991                 }
992                 if (retval == NAME_NOT_FOUND)
993                         de.de_entry_num--;
994
995                 reiserfs_log(LOG_DEBUG, "name found\n");
996
997                 set_de_name_and_namelen(&de);
998
999                 if (deh_offset(&(de.de_deh[de.de_entry_num])) == DOT_DOT_OFFSET) {
1000                         /* Allow override in this case */
1001                         if (reiserfs_rupasov_hash(sbi)) {
1002                                 hash = YURA_HASH;
1003                         }
1004                         reiserfs_log(LOG_DEBUG,
1005                             "FS seems to be empty, autodetect "
1006                             "is using the default hash");
1007                         break;
1008                 }
1009
1010                 r5hash   = GET_HASH_VALUE(r5_hash(de.de_name, de.de_namelen));
1011                 teahash  = GET_HASH_VALUE(keyed_hash(de.de_name,
1012                     de.de_namelen));
1013                 yurahash = GET_HASH_VALUE(yura_hash(de.de_name, de.de_namelen));
1014                 if (((teahash == r5hash) &&
1015                     (GET_HASH_VALUE(
1016                      deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash)) ||
1017                     ((teahash == yurahash) &&
1018                      (yurahash ==
1019                       GET_HASH_VALUE(
1020                       deh_offset(&(de.de_deh[de.de_entry_num]))))) ||
1021                     ((r5hash == yurahash) &&
1022                      (yurahash ==
1023                       GET_HASH_VALUE(
1024                       deh_offset(&(de.de_deh[de.de_entry_num])))))) {
1025                         reiserfs_log(LOG_ERR,
1026                             "unable to automatically detect hash "
1027                             "function. Please mount with -o "
1028                             "hash={tea,rupasov,r5}");
1029                         hash = UNSET_HASH;
1030                         break;
1031                 }
1032
1033                 if (GET_HASH_VALUE(
1034                     deh_offset(&(de.de_deh[de.de_entry_num]))) == yurahash) {
1035                         reiserfs_log(LOG_DEBUG, "detected YURA hash\n");
1036                         hash = YURA_HASH;
1037                 } else if (GET_HASH_VALUE(
1038                     deh_offset(&(de.de_deh[de.de_entry_num]))) == teahash) {
1039                         reiserfs_log(LOG_DEBUG, "detected TEA hash\n");
1040                         hash = TEA_HASH;
1041                 } else if (GET_HASH_VALUE(
1042                     deh_offset(&(de.de_deh[de.de_entry_num]))) == r5hash) {
1043                         reiserfs_log(LOG_DEBUG, "detected R5 hash\n");
1044                         hash = R5_HASH;
1045                 } else {
1046                         reiserfs_log(LOG_WARNING, "unrecognised hash function");
1047                         hash = UNSET_HASH;
1048                 }
1049         } while (0);
1050
1051         pathrelse(&path);
1052         return (hash);
1053 }
1054
1055 /* Finds out which hash names are sorted with */
1056 static int
1057 what_hash(struct reiserfs_mount *rmp)
1058 {
1059         uint32_t code;
1060         struct reiserfs_sb_info *sbi = rmp->rm_reiserfs;
1061
1062         find_hash_out(rmp);
1063         code = sb_hash_function_code(SB_DISK_SUPER_BLOCK(sbi));
1064
1065         /*
1066          * reiserfs_hash_detect() == true if any of the hash mount options
1067          * were used. We must check them to make sure the user isn't using a
1068          * bad hash value
1069          */
1070         if (code == UNSET_HASH || reiserfs_hash_detect(sbi))
1071                 code = find_hash_out(rmp);
1072
1073         if (code != UNSET_HASH && reiserfs_hash_detect(sbi)) {
1074                 /*
1075                  * Detection has found the hash, and we must check against
1076                  * the mount options
1077                  */
1078                 if (reiserfs_rupasov_hash(sbi) && code != YURA_HASH) {
1079                         reiserfs_log(LOG_ERR, "error, %s hash detected, "
1080                             "unable to force rupasov hash",
1081                             reiserfs_hashname(code));
1082                         code = UNSET_HASH;
1083                 } else if (reiserfs_tea_hash(sbi) && code != TEA_HASH) {
1084                         reiserfs_log(LOG_ERR, "error, %s hash detected, "
1085                             "unable to force tea hash",
1086                             reiserfs_hashname(code));
1087                         code = UNSET_HASH;
1088                 } else if (reiserfs_r5_hash(sbi) && code != R5_HASH) {
1089                         reiserfs_log(LOG_ERR, "error, %s hash detected, "
1090                             "unable to force r5 hash",
1091                             reiserfs_hashname(code));
1092                         code = UNSET_HASH;
1093                 }
1094         } else {
1095                 /*
1096                  * Find_hash_out was not called or could not determine
1097                  * the hash
1098                  */
1099                 if (reiserfs_rupasov_hash(sbi)) {
1100                         code = YURA_HASH;
1101                 } else if (reiserfs_tea_hash(sbi)) {
1102                         code = TEA_HASH;
1103                 } else if (reiserfs_r5_hash(sbi)) {
1104                         code = R5_HASH;
1105                 }
1106         }
1107
1108         /* TODO Not supported yet */
1109 #if 0
1110         /* If we are mounted RW, and we have a new valid hash code, update
1111          * the super */
1112         if (code != UNSET_HASH &&
1113             !(s->s_flags & MS_RDONLY) &&
1114             code != sb_hash_function_code(SB_DISK_SUPER_BLOCK(s))) {
1115                 set_sb_hash_function_code(SB_DISK_SUPER_BLOCK(s), code);
1116         }
1117 #endif
1118
1119         return (code);
1120 }
1121
1122 /* Return pointer to appropriate function */
1123 static hashf_t
1124 hash_function(struct reiserfs_mount *rmp)
1125 {
1126
1127         switch (what_hash(rmp)) {
1128         case TEA_HASH:
1129                 reiserfs_log(LOG_INFO, "using tea hash to sort names\n");
1130                 return (keyed_hash);
1131         case YURA_HASH:
1132                 reiserfs_log(LOG_INFO, "using rupasov hash to sort names\n");
1133                 return (yura_hash);
1134         case R5_HASH:
1135                 reiserfs_log(LOG_INFO, "using r5 hash to sort names\n");
1136                 return (r5_hash);
1137         }
1138
1139         return (NULL);
1140 }
1141
1142 /* -------------------------------------------------------------------
1143  * VFS registration
1144  * -------------------------------------------------------------------*/
1145
1146 static struct vfsops reiser_vfsops = {
1147         .vfs_cmount     = reiserfs_cmount,
1148         .vfs_mount      = reiserfs_mount,
1149         .vfs_unmount    = reiserfs_unmount,
1150         //.vfs_checkexp = reiserfs_checkexp,
1151         //.vfs_extattrctl = reiserfs_extattrctl,
1152         .vfs_fhtovp     = reiserfs_fhtovp,
1153         //.vfs_quotactl = reiserfs_quotactl,
1154         .vfs_root       = reiserfs_root,
1155         //.vfs_start    = reiserfs_start,
1156         .vfs_statfs     = reiserfs_statfs,
1157         //.vfs_sync     = reiserfs_sync,
1158         //.vfs_vget     = reiserfs_vget,
1159 };
1160
1161 VFS_SET(reiser_vfsops, reiserfs, VFCF_READONLY);