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