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;
144 struct nlookupdata nd;
145 char devstr[MNAMELEN];
158 kprintf("hammer2_mount\n");
167 * Non-root mount or updating a mount
169 error = copyin(data, &info, sizeof(info));
173 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
177 /* Extract device and label */
179 label = strchr(devstr, '@');
181 ((label + 1) - dev) > done) {
189 if (mp->mnt_flag & MNT_UPDATE) {
191 /* HAMMER2 implements NFS export via mountctl */
194 error = hammer2_remount(mp, path, devvp, cred);
202 /* Lookup name and verify it refers to a block device */
203 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
205 error = nlookup(&nd);
207 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
211 if (vn_isdisk(devvp, &error))
212 error = vfs_mountedon(devvp);
214 if (error == 0 && vcount(devvp) > 0)
218 * Now open the device
221 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
222 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
223 error = vinvalbuf(devvp, V_SAVE, 0, 0);
225 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD | FWRITE,
230 if (error && devvp) {
238 * Block device opened successfully, finish initializing the
241 * From this point on we have to call hammer2_unmount() on failure.
243 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
244 mp->mnt_data = (qaddr_t)hmp;
248 lockinit(&hmp->lk, "h2mp", 0, 0);
249 kmalloc_create(&hmp->inodes, "HAMMER2-inodes");
250 kmalloc_create(&hmp->ipstacks, "HAMMER2-ipstacks");
252 mp->mnt_flag = MNT_LOCAL;
253 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
256 * Install the volume header
258 error = hammer2_install_volume_header(hmp);
260 hammer2_unmount(mp, MNT_FORCE);
265 * required mount structure initializations
267 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
268 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
270 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
271 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
274 * Locate the root inode. The volume header points to the
275 * super-root directory.
280 /* Setup root inode */
281 hmp->iroot = hammer2_alloci(hmp);
282 hmp->iroot->type = HAMMER2_INODE_TYPE_DIR | HAMMER2_INODE_TYPE_ROOT;
283 hmp->iroot->data.inum = 1;
286 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
287 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
288 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
290 copystr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
291 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
292 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
293 copyinstr(path, mp->mnt_stat.f_mntonname,
294 sizeof(mp->mnt_stat.f_mntonname) - 1,
297 hammer2_statfs(mp, &mp->mnt_stat, cred);
299 hammer2_inode_unlock_ex(hmp->iroot);
306 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
314 hammer2_unmount(struct mount *mp, int mntflags)
316 hammer2_mount_t *hmp;
319 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
322 kprintf("hammer2_unmount\n");
327 if (mntflags & MNT_FORCE)
330 hammer2_mount_exlock(hmp);
333 * If mount initialization proceeded far enough we must flush
337 error = vflush(mp, 0, flags);
344 * 1) Wait on the flusher having no work; heat up if needed
345 * 2) Scan inode RB tree till all the inodes are free
346 * 3) Destroy the kmalloc inode zone
347 * 4) Free the mount point
351 hammer2_inode_lock_ex(hmp->iroot);
352 hammer2_freei(hmp->iroot);
355 if ((devvp = hmp->devvp) != NULL) {
356 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
358 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
363 kmalloc_destroy(&hmp->inodes);
364 kmalloc_destroy(&hmp->ipstacks);
366 hammer2_mount_unlock(hmp);
370 kfree(hmp, M_HAMMER2);
377 hammer2_vget(struct mount *mp, struct vnode *dvp,
378 ino_t ino, struct vnode **vpp)
380 kprintf("hammer2_vget\n");
386 hammer2_root(struct mount *mp, struct vnode **vpp)
388 hammer2_mount_t *hmp;
392 kprintf("hammer2_root\n");
395 hammer2_mount_exlock(hmp);
396 if (hmp->iroot == NULL) {
400 vp = hammer2_igetv(hmp->iroot, &error);
403 kprintf("vnodefail\n");
405 hammer2_mount_unlock(hmp);
412 hammer2_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
414 hammer2_mount_t *hmp;
418 mp->mnt_stat.f_files = 10;
419 mp->mnt_stat.f_bfree = 10;
420 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
428 hammer2_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
430 hammer2_mount_t *hmp;
434 mp->mnt_vstat.f_files = 10;
435 mp->mnt_vstat.f_bfree = 10;
436 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
438 *sbp = mp->mnt_vstat;
443 * Sync the entire filesystem; this is called from the filesystem syncer
444 * process periodically and whenever a user calls sync(1) on the hammer
447 * Currently is actually called from the syncer! \o/
449 * This task will have to snapshot the state of the dirty inode chain.
450 * From that, it will have to make sure all of the inodes on the dirty
451 * chain have IO initiated. We make sure that io is initiated for the root
454 * If waitfor is set, we wait for media to acknowledge the new rootblock.
456 * THINKS: side A vs side B, to have sync not stall all I/O?
460 hammer2_sync(struct mount *mp, int waitfor)
463 hammer2_mount_t *hmp;
467 kprintf("hammer2_sync \n");
478 hammer2_vptofh(struct vnode *vp, struct fid *fhp)
485 hammer2_fhtovp(struct mount *mp, struct vnode *rootvp,
486 struct fid *fhp, struct vnode **vpp)
493 hammer2_checkexp(struct mount *mp, struct sockaddr *nam,
494 int *exflagsp, struct ucred **credanonp)
500 * Support code for hammer2_mount(). Read, verify, and install the volume
501 * header into the HMP
503 * XXX read four volhdrs and use the one with the highest TID whos CRC
510 hammer2_install_volume_header(hammer2_mount_t *hmp)
512 hammer2_volume_data_t *vd;
513 struct buf *bp[HAMMER2_NUM_VOLHDRS];
514 hammer2_tid_t hi_tid;
515 hammer2_crc32_t ccrc, crc;
525 bzero(bp, sizeof(bp));
527 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
528 error = bread(hmp->devvp, i * HAMMER2_RESERVE_BYTES64,
529 HAMMER2_VOLUME_BYTES, &bp[i]);
536 vd = (struct hammer2_volume_data *) bp[i]->b_data;
538 if (vd->magic != HAMMER2_VOLUME_ID_HBO)
541 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
542 ccrc = hammer2_icrc32(bp[i]->b_data, HAMMER2_VOLUME_ICRC0_SIZE);
544 kprintf("hammer2 volume header %d crc mismatch\n", i);
550 if (hi_tid < vd->last_tid) {
551 hi_tid = vd->last_tid;
556 KKASSERT(hi_idx != -1);
558 bcopy(bp[hi_idx]->b_data, &hmp->voldata, sizeof(hmp->voldata));
563 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {