2 * Copyright (c) 2011, 2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/nlookup.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/fcntl.h>
43 #include <sys/vfsops.h>
46 #include "hammer2_disk.h"
47 #include "hammer2_mount.h"
49 static int hammer2_init(struct vfsconf *conf);
50 static int hammer2_mount(struct mount *mp, char *path, caddr_t data,
52 static int hammer2_remount(struct mount *, char *, struct vnode *,
54 static int hammer2_unmount(struct mount *mp, int mntflags);
55 static int hammer2_root(struct mount *mp, struct vnode **vpp);
56 static int hammer2_statfs(struct mount *mp, struct statfs *sbp,
58 static int hammer2_statvfs(struct mount *mp, struct statvfs *sbp,
60 static int hammer2_sync(struct mount *mp, int waitfor);
61 static int hammer2_vget(struct mount *mp, struct vnode *dvp,
62 ino_t ino, struct vnode **vpp);
63 static int hammer2_fhtovp(struct mount *mp, struct vnode *rootvp,
64 struct fid *fhp, struct vnode **vpp);
65 static int hammer2_vptofh(struct vnode *vp, struct fid *fhp);
66 static int hammer2_checkexp(struct mount *mp, struct sockaddr *nam,
67 int *exflagsp, struct ucred **credanonp);
69 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
72 * HAMMER2 vfs operations.
74 static struct vfsops hammer2_vfsops = {
75 .vfs_init = hammer2_init,
76 .vfs_sync = hammer2_sync,
77 .vfs_mount = hammer2_mount,
78 .vfs_unmount = hammer2_unmount,
79 .vfs_root = hammer2_root,
80 .vfs_statfs = hammer2_statfs,
81 .vfs_statvfs = hammer2_statvfs,
82 .vfs_vget = hammer2_vget,
83 .vfs_vptofh = hammer2_vptofh,
84 .vfs_fhtovp = hammer2_fhtovp,
85 .vfs_checkexp = hammer2_checkexp
88 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
90 VFS_SET(hammer2_vfsops, hammer2, 0);
91 MODULE_VERSION(hammer2, 1);
95 hammer2_init(struct vfsconf *conf)
101 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
103 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
105 if (HAMMER2_ALLOCREF_BYTES != sizeof(struct hammer2_allocref))
107 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
111 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
117 * Mount or remount HAMMER2 fileystem from physical media
120 * mp mount point structure
126 * mp mount point structure
127 * path path to mount point
128 * data pointer to argument structure in user space
129 * volume volume path (device@LABEL form)
130 * hflags user mount flags
131 * cred user credentials
138 hammer2_mount(struct mount *mp, char *path, caddr_t data,
141 struct hammer2_mount_info info;
142 hammer2_mount_t *hmp;
145 struct nlookupdata nd;
146 hammer2_chain_t *schain;
147 hammer2_chain_t *rchain;
148 char devstr[MNAMELEN];
161 kprintf("hammer2_mount\n");
170 * Non-root mount or updating a mount
172 error = copyin(data, &info, sizeof(info));
176 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
180 /* Extract device and label */
182 label = strchr(devstr, '@');
184 ((label + 1) - dev) > done) {
192 if (mp->mnt_flag & MNT_UPDATE) {
194 /* HAMMER2 implements NFS export via mountctl */
197 error = hammer2_remount(mp, path, devvp, cred);
205 /* Lookup name and verify it refers to a block device */
206 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
208 error = nlookup(&nd);
210 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
214 if (vn_isdisk(devvp, &error))
215 error = vfs_mountedon(devvp);
217 if (error == 0 && vcount(devvp) > 0)
221 * Now open the device
224 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
225 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
226 error = vinvalbuf(devvp, V_SAVE, 0, 0);
228 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD | FWRITE,
233 if (error && devvp) {
241 * Block device opened successfully, finish initializing the
244 * From this point on we have to call hammer2_unmount() on failure.
246 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
247 mp->mnt_data = (qaddr_t)hmp;
251 lockinit(&hmp->lk, "h2mp", 0, 0);
252 kmalloc_create(&hmp->inodes, "HAMMER2-inodes");
253 kmalloc_create(&hmp->ipstacks, "HAMMER2-ipstacks");
255 mp->mnt_flag = MNT_LOCAL;
256 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
258 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
261 * Install the volume header
263 error = hammer2_install_volume_header(hmp);
265 hammer2_unmount(mp, MNT_FORCE);
270 * required mount structure initializations
272 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
273 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
275 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
276 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
279 * First locate the super-root inode, which is key 0 relative to the
280 * volume header's blockset.
282 * Then locate the root inode by scanning the directory keyspace
283 * represented by the label.
285 lhc = hammer2_dirhash(label, strlen(label));
286 schain = hammer2_chain_push(hmp, &hmp->vchain, HAMMER2_SROOT_KEY);
287 if (schain == NULL) {
288 kprintf("hammer2_mount: invalid super-root\n");
289 hammer2_unmount(mp, MNT_FORCE);
292 rchain = hammer2_chain_first(hmp, schain, lhc, HAMMER2_DIRHASH_LOMASK);
294 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
296 strcmp(label, rchain->u.ip->data.filename) == 0) {
299 rchain = hammer2_chain_next(hmp, rchain,
300 lhc, HAMMER2_DIRHASH_LOMASK);
302 if (rchain == NULL) {
303 kprintf("hammer2_mount: root label not found\n");
304 hammer2_chain_drop(hmp, schain);
305 hammer2_unmount(mp, MNT_FORCE);
308 hmp->schain = schain;
309 hmp->rchain = rchain;
310 hmp->iroot = rchain->u.ip;
311 hammer2_inode_ref(hmp->iroot); /* additional ref */
314 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
315 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
316 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
318 copystr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
319 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
320 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
321 copyinstr(path, mp->mnt_stat.f_mntonname,
322 sizeof(mp->mnt_stat.f_mntonname) - 1,
325 hammer2_statfs(mp, &mp->mnt_stat, cred);
332 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
340 hammer2_unmount(struct mount *mp, int mntflags)
342 hammer2_mount_t *hmp;
345 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
348 kprintf("hammer2_unmount\n");
353 if (mntflags & MNT_FORCE)
356 hammer2_mount_exlock(hmp);
359 * If mount initialization proceeded far enough we must flush
363 error = vflush(mp, 0, flags);
370 * 1) Wait on the flusher having no work; heat up if needed
371 * 2) Scan inode RB tree till all the inodes are free
372 * 3) Destroy the kmalloc inode zone
373 * 4) Free the mount point
376 hammer2_chain_drop(hmp, hmp->rchain);
380 hammer2_chain_drop(hmp, hmp->schain);
384 hammer2_inode_drop(hmp->iroot);
387 if ((devvp = hmp->devvp) != NULL) {
388 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
390 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
395 kmalloc_destroy(&hmp->inodes);
396 kmalloc_destroy(&hmp->ipstacks);
398 hammer2_mount_unlock(hmp);
402 kfree(hmp, M_HAMMER2);
409 hammer2_vget(struct mount *mp, struct vnode *dvp,
410 ino_t ino, struct vnode **vpp)
412 kprintf("hammer2_vget\n");
418 hammer2_root(struct mount *mp, struct vnode **vpp)
420 hammer2_mount_t *hmp;
424 kprintf("hammer2_root\n");
427 hammer2_mount_exlock(hmp);
428 if (hmp->iroot == NULL) {
432 vp = hammer2_igetv(hmp->iroot, &error);
435 kprintf("vnodefail\n");
437 hammer2_mount_unlock(hmp);
444 hammer2_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
446 hammer2_mount_t *hmp;
450 mp->mnt_stat.f_files = 10;
451 mp->mnt_stat.f_bfree = 10;
452 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
460 hammer2_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
462 hammer2_mount_t *hmp;
466 mp->mnt_vstat.f_files = 10;
467 mp->mnt_vstat.f_bfree = 10;
468 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
470 *sbp = mp->mnt_vstat;
475 * Sync the entire filesystem; this is called from the filesystem syncer
476 * process periodically and whenever a user calls sync(1) on the hammer
479 * Currently is actually called from the syncer! \o/
481 * This task will have to snapshot the state of the dirty inode chain.
482 * From that, it will have to make sure all of the inodes on the dirty
483 * chain have IO initiated. We make sure that io is initiated for the root
486 * If waitfor is set, we wait for media to acknowledge the new rootblock.
488 * THINKS: side A vs side B, to have sync not stall all I/O?
492 hammer2_sync(struct mount *mp, int waitfor)
495 hammer2_mount_t *hmp;
499 kprintf("hammer2_sync \n");
510 hammer2_vptofh(struct vnode *vp, struct fid *fhp)
517 hammer2_fhtovp(struct mount *mp, struct vnode *rootvp,
518 struct fid *fhp, struct vnode **vpp)
525 hammer2_checkexp(struct mount *mp, struct sockaddr *nam,
526 int *exflagsp, struct ucred **credanonp)
532 * Support code for hammer2_mount(). Read, verify, and install the volume
533 * header into the HMP
535 * XXX read four volhdrs and use the one with the highest TID whos CRC
540 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
541 * nonexistant locations.
543 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
547 hammer2_install_volume_header(hammer2_mount_t *hmp)
549 hammer2_volume_data_t *vd;
551 hammer2_crc32_t ccrc, crc;
563 * There are up to 4 copies of the volume header (syncs iterate
564 * between them so there is no single master). We don't trust the
565 * volu_size field so we don't know precisely how large the filesystem
566 * is, so depend on the OS to return an error if we go beyond the
567 * block device's EOF.
569 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
570 error = bread(hmp->devvp, i * HAMMER2_RESERVE_BYTES64,
571 HAMMER2_VOLUME_BYTES, &bp);
578 vd = (struct hammer2_volume_data *)bp->b_data;
579 if (vd->magic != HAMMER2_VOLUME_ID_HBO)
582 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
583 ccrc = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
584 HAMMER2_VOLUME_ICRC0_SIZE);
586 kprintf("hammer2 volume header crc "
587 "mismatch copy #%d\t%08x %08x",
594 if (valid == 0 || hmp->voldata.last_tid < vd->last_tid) {
604 kprintf("hammer2: a valid volume header was found\n");
607 kprintf("hammer2: no valid volume headers found!\n");