Home | History | Annotate | Line # | Download | only in nilfs
nilfs_vfsops.c revision 1.1
      1  1.1  reinoud /* $NetBSD: nilfs_vfsops.c,v 1.1 2009/07/18 16:31:42 reinoud Exp $ */
      2  1.1  reinoud 
      3  1.1  reinoud /*
      4  1.1  reinoud  * Copyright (c) 2008, 2009 Reinoud Zandijk
      5  1.1  reinoud  * All rights reserved.
      6  1.1  reinoud  *
      7  1.1  reinoud  * Redistribution and use in source and binary forms, with or without
      8  1.1  reinoud  * modification, are permitted provided that the following conditions
      9  1.1  reinoud  * are met:
     10  1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     11  1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     12  1.1  reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  reinoud  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  reinoud  *    documentation and/or other materials provided with the distribution.
     15  1.1  reinoud  *
     16  1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  reinoud  *
     27  1.1  reinoud  */
     28  1.1  reinoud 
     29  1.1  reinoud #include <sys/cdefs.h>
     30  1.1  reinoud #ifndef lint
     31  1.1  reinoud __KERNEL_RCSID(0, "$NetBSD: nilfs_vfsops.c,v 1.1 2009/07/18 16:31:42 reinoud Exp $");
     32  1.1  reinoud #endif /* not lint */
     33  1.1  reinoud 
     34  1.1  reinoud 
     35  1.1  reinoud #if defined(_KERNEL_OPT)
     36  1.1  reinoud #include "opt_compat_netbsd.h"
     37  1.1  reinoud #endif
     38  1.1  reinoud 
     39  1.1  reinoud #include <sys/param.h>
     40  1.1  reinoud #include <sys/systm.h>
     41  1.1  reinoud #include <sys/sysctl.h>
     42  1.1  reinoud #include <sys/namei.h>
     43  1.1  reinoud #include <sys/proc.h>
     44  1.1  reinoud #include <sys/kernel.h>
     45  1.1  reinoud #include <sys/vnode.h>
     46  1.1  reinoud #include <miscfs/genfs/genfs.h>
     47  1.1  reinoud #include <miscfs/specfs/specdev.h>
     48  1.1  reinoud #include <sys/mount.h>
     49  1.1  reinoud #include <sys/buf.h>
     50  1.1  reinoud #include <sys/file.h>
     51  1.1  reinoud #include <sys/device.h>
     52  1.1  reinoud #include <sys/disklabel.h>
     53  1.1  reinoud #include <sys/ioctl.h>
     54  1.1  reinoud #include <sys/malloc.h>
     55  1.1  reinoud #include <sys/dirent.h>
     56  1.1  reinoud #include <sys/stat.h>
     57  1.1  reinoud #include <sys/conf.h>
     58  1.1  reinoud #include <sys/kauth.h>
     59  1.1  reinoud #include <sys/module.h>
     60  1.1  reinoud 
     61  1.1  reinoud #include <fs/nilfs/nilfs_mount.h>
     62  1.1  reinoud #include <sys/dirhash.h>
     63  1.1  reinoud 
     64  1.1  reinoud 
     65  1.1  reinoud #include "nilfs.h"
     66  1.1  reinoud #include "nilfs_subr.h"
     67  1.1  reinoud #include "nilfs_bswap.h"
     68  1.1  reinoud 
     69  1.1  reinoud MODULE(MODULE_CLASS_VFS, nilfs, NULL);
     70  1.1  reinoud 
     71  1.1  reinoud #define VTOI(vnode) ((struct nilfs_node *) vnode->v_data)
     72  1.1  reinoud 
     73  1.1  reinoud #define NILFS_SET_SYSTEMFILE(vp) { \
     74  1.1  reinoud 	/* XXXAD Is the vnode locked? */	\
     75  1.1  reinoud 	(vp)->v_vflag |= VV_SYSTEM;		\
     76  1.1  reinoud 	vref(vp);				\
     77  1.1  reinoud 	vput(vp); }
     78  1.1  reinoud 
     79  1.1  reinoud #define NILFS_UNSET_SYSTEMFILE(vp) { \
     80  1.1  reinoud 	/* XXXAD Is the vnode locked? */	\
     81  1.1  reinoud 	(vp)->v_vflag &= ~VV_SYSTEM;		\
     82  1.1  reinoud 	vrele(vp); }
     83  1.1  reinoud 
     84  1.1  reinoud 
     85  1.1  reinoud /* verbose levels of the nilfs filingsystem */
     86  1.1  reinoud int nilfs_verbose = NILFS_DEBUGGING;
     87  1.1  reinoud 
     88  1.1  reinoud /* malloc regions */
     89  1.1  reinoud MALLOC_JUSTDEFINE(M_NILFSMNT,   "NILFS mount",	"NILFS mount structures");
     90  1.1  reinoud MALLOC_JUSTDEFINE(M_NILFSTEMP,  "NILFS temp",	"NILFS scrap space");
     91  1.1  reinoud struct pool nilfs_node_pool;
     92  1.1  reinoud 
     93  1.1  reinoud /* globals */
     94  1.1  reinoud struct _nilfs_devices nilfs_devices;
     95  1.1  reinoud static struct sysctllog *nilfs_sysctl_log;
     96  1.1  reinoud 
     97  1.1  reinoud /* supported functions predefined */
     98  1.1  reinoud VFS_PROTOS(nilfs);
     99  1.1  reinoud 
    100  1.1  reinoud 
    101  1.1  reinoud /* --------------------------------------------------------------------- */
    102  1.1  reinoud 
    103  1.1  reinoud /* predefine vnode-op list descriptor */
    104  1.1  reinoud extern const struct vnodeopv_desc nilfs_vnodeop_opv_desc;
    105  1.1  reinoud 
    106  1.1  reinoud const struct vnodeopv_desc * const nilfs_vnodeopv_descs[] = {
    107  1.1  reinoud 	&nilfs_vnodeop_opv_desc,
    108  1.1  reinoud 	NULL,
    109  1.1  reinoud };
    110  1.1  reinoud 
    111  1.1  reinoud 
    112  1.1  reinoud /* vfsops descriptor linked in as anchor point for the filingsystem */
    113  1.1  reinoud struct vfsops nilfs_vfsops = {
    114  1.1  reinoud 	MOUNT_NILFS,			/* vfs_name */
    115  1.1  reinoud 	sizeof (struct nilfs_args),
    116  1.1  reinoud 	nilfs_mount,
    117  1.1  reinoud 	nilfs_start,
    118  1.1  reinoud 	nilfs_unmount,
    119  1.1  reinoud 	nilfs_root,
    120  1.1  reinoud 	(void *)eopnotsupp,		/* vfs_quotactl */
    121  1.1  reinoud 	nilfs_statvfs,
    122  1.1  reinoud 	nilfs_sync,
    123  1.1  reinoud 	nilfs_vget,
    124  1.1  reinoud 	nilfs_fhtovp,
    125  1.1  reinoud 	nilfs_vptofh,
    126  1.1  reinoud 	nilfs_init,
    127  1.1  reinoud 	nilfs_reinit,
    128  1.1  reinoud 	nilfs_done,
    129  1.1  reinoud 	nilfs_mountroot,
    130  1.1  reinoud 	nilfs_snapshot,
    131  1.1  reinoud 	vfs_stdextattrctl,
    132  1.1  reinoud 	(void *)eopnotsupp,		/* vfs_suspendctl */
    133  1.1  reinoud 	genfs_renamelock_enter,
    134  1.1  reinoud 	genfs_renamelock_exit,
    135  1.1  reinoud 	(void *)eopnotsupp,		/* vfs_full_fsync */
    136  1.1  reinoud 	nilfs_vnodeopv_descs,
    137  1.1  reinoud 	0, /* int vfs_refcount   */
    138  1.1  reinoud 	{ NULL, NULL, }, /* LIST_ENTRY(vfsops) */
    139  1.1  reinoud };
    140  1.1  reinoud 
    141  1.1  reinoud /* --------------------------------------------------------------------- */
    142  1.1  reinoud 
    143  1.1  reinoud /* file system starts here */
    144  1.1  reinoud void
    145  1.1  reinoud nilfs_init(void)
    146  1.1  reinoud {
    147  1.1  reinoud 	size_t size;
    148  1.1  reinoud 
    149  1.1  reinoud 	/* setup memory types */
    150  1.1  reinoud 	malloc_type_attach(M_NILFSMNT);
    151  1.1  reinoud 	malloc_type_attach(M_NILFSTEMP);
    152  1.1  reinoud 
    153  1.1  reinoud 	/* init device lists */
    154  1.1  reinoud 	SLIST_INIT(&nilfs_devices);
    155  1.1  reinoud 
    156  1.1  reinoud 	/* init node pools */
    157  1.1  reinoud 	size = sizeof(struct nilfs_node);
    158  1.1  reinoud 	pool_init(&nilfs_node_pool, size, 0, 0, 0,
    159  1.1  reinoud 		"nilfs_node_pool", NULL, IPL_NONE);
    160  1.1  reinoud }
    161  1.1  reinoud 
    162  1.1  reinoud 
    163  1.1  reinoud void
    164  1.1  reinoud nilfs_reinit(void)
    165  1.1  reinoud {
    166  1.1  reinoud 	/* nothing to do */
    167  1.1  reinoud }
    168  1.1  reinoud 
    169  1.1  reinoud 
    170  1.1  reinoud void
    171  1.1  reinoud nilfs_done(void)
    172  1.1  reinoud {
    173  1.1  reinoud 	/* remove pools */
    174  1.1  reinoud 	pool_destroy(&nilfs_node_pool);
    175  1.1  reinoud 
    176  1.1  reinoud 	malloc_type_detach(M_NILFSMNT);
    177  1.1  reinoud 	malloc_type_detach(M_NILFSTEMP);
    178  1.1  reinoud }
    179  1.1  reinoud 
    180  1.1  reinoud /*
    181  1.1  reinoud  * If running a DEBUG kernel, provide an easy way to set the debug flags when
    182  1.1  reinoud  * running into a problem.
    183  1.1  reinoud  */
    184  1.1  reinoud #define NILFS_VERBOSE_SYSCTLOPT        1
    185  1.1  reinoud 
    186  1.1  reinoud static int
    187  1.1  reinoud nilfs_modcmd(modcmd_t cmd, void *arg)
    188  1.1  reinoud {
    189  1.1  reinoud 	const struct sysctlnode *node;
    190  1.1  reinoud 	int error;
    191  1.1  reinoud 
    192  1.1  reinoud 	switch (cmd) {
    193  1.1  reinoud 	case MODULE_CMD_INIT:
    194  1.1  reinoud 		error = vfs_attach(&nilfs_vfsops);
    195  1.1  reinoud 		if (error != 0)
    196  1.1  reinoud 			break;
    197  1.1  reinoud 		/*
    198  1.1  reinoud 		 * XXX the "30" below could be dynamic, thereby eliminating one
    199  1.1  reinoud 		 * more instance of the "number to vfs" mapping problem, but
    200  1.1  reinoud 		 * "30" is the order as taken from sys/mount.h
    201  1.1  reinoud 		 */
    202  1.1  reinoud 		sysctl_createv(&nilfs_sysctl_log, 0, NULL, NULL,
    203  1.1  reinoud 			       CTLFLAG_PERMANENT,
    204  1.1  reinoud 			       CTLTYPE_NODE, "vfs", NULL,
    205  1.1  reinoud 			       NULL, 0, NULL, 0,
    206  1.1  reinoud 			       CTL_VFS, CTL_EOL);
    207  1.1  reinoud 		sysctl_createv(&nilfs_sysctl_log, 0, NULL, &node,
    208  1.1  reinoud 			       CTLFLAG_PERMANENT,
    209  1.1  reinoud 			       CTLTYPE_NODE, "nilfs",
    210  1.1  reinoud 			       SYSCTL_DESCR("NTT's NILFSv2"),
    211  1.1  reinoud 			       NULL, 0, NULL, 0,
    212  1.1  reinoud 			       CTL_VFS, 30, CTL_EOL);
    213  1.1  reinoud #ifdef DEBUG
    214  1.1  reinoud 		sysctl_createv(&nilfs_sysctl_log, 0, NULL, &node,
    215  1.1  reinoud 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    216  1.1  reinoud 			       CTLTYPE_INT, "verbose",
    217  1.1  reinoud 			       SYSCTL_DESCR("Bitmask for filesystem debugging"),
    218  1.1  reinoud 			       NULL, 0, &nilfs_verbose, 0,
    219  1.1  reinoud 			       CTL_VFS, 30, NILFS_VERBOSE_SYSCTLOPT, CTL_EOL);
    220  1.1  reinoud #endif
    221  1.1  reinoud 		break;
    222  1.1  reinoud 	case MODULE_CMD_FINI:
    223  1.1  reinoud 		error = vfs_detach(&nilfs_vfsops);
    224  1.1  reinoud 		if (error != 0)
    225  1.1  reinoud 			break;
    226  1.1  reinoud 		sysctl_teardown(&nilfs_sysctl_log);
    227  1.1  reinoud 		break;
    228  1.1  reinoud 	default:
    229  1.1  reinoud 		error = ENOTTY;
    230  1.1  reinoud 		break;
    231  1.1  reinoud 	}
    232  1.1  reinoud 
    233  1.1  reinoud 	return (error);
    234  1.1  reinoud }
    235  1.1  reinoud 
    236  1.1  reinoud /* --------------------------------------------------------------------- */
    237  1.1  reinoud 
    238  1.1  reinoud int
    239  1.1  reinoud nilfs_mountroot(void)
    240  1.1  reinoud {
    241  1.1  reinoud 	return EOPNOTSUPP;
    242  1.1  reinoud }
    243  1.1  reinoud 
    244  1.1  reinoud /* --------------------------------------------------------------------- */
    245  1.1  reinoud 
    246  1.1  reinoud /* system nodes */
    247  1.1  reinoud static int
    248  1.1  reinoud nilfs_create_system_nodes(struct nilfs_device *nilfsdev)
    249  1.1  reinoud {
    250  1.1  reinoud 	int error;
    251  1.1  reinoud 
    252  1.1  reinoud 	error = nilfs_get_node_raw(nilfsdev, NULL, NILFS_DAT_INO,
    253  1.1  reinoud 		&nilfsdev->super_root.sr_dat, &nilfsdev->dat_node);
    254  1.1  reinoud 	if (error)
    255  1.1  reinoud 		goto errorout;
    256  1.1  reinoud 
    257  1.1  reinoud 	error = nilfs_get_node_raw(nilfsdev, NULL, NILFS_CPFILE_INO,
    258  1.1  reinoud 		&nilfsdev->super_root.sr_cpfile, &nilfsdev->cp_node);
    259  1.1  reinoud 	if (error)
    260  1.1  reinoud 		goto errorout;
    261  1.1  reinoud 
    262  1.1  reinoud 	error = nilfs_get_node_raw(nilfsdev, NULL, NILFS_SUFILE_INO,
    263  1.1  reinoud 		&nilfsdev->super_root.sr_sufile, &nilfsdev->su_node);
    264  1.1  reinoud 	if (error)
    265  1.1  reinoud 		goto errorout;
    266  1.1  reinoud 
    267  1.1  reinoud 	NILFS_SET_SYSTEMFILE(nilfsdev->dat_node->vnode);
    268  1.1  reinoud 	NILFS_SET_SYSTEMFILE(nilfsdev->cp_node->vnode);
    269  1.1  reinoud 	NILFS_SET_SYSTEMFILE(nilfsdev->su_node->vnode);
    270  1.1  reinoud 
    271  1.1  reinoud 	return 0;
    272  1.1  reinoud errorout:
    273  1.1  reinoud 	nilfs_dispose_node(&nilfsdev->dat_node);
    274  1.1  reinoud 	nilfs_dispose_node(&nilfsdev->cp_node);
    275  1.1  reinoud 	nilfs_dispose_node(&nilfsdev->su_node);
    276  1.1  reinoud 
    277  1.1  reinoud 	return error;
    278  1.1  reinoud }
    279  1.1  reinoud 
    280  1.1  reinoud 
    281  1.1  reinoud static void
    282  1.1  reinoud nilfs_release_system_nodes(struct nilfs_device *nilfsdev)
    283  1.1  reinoud {
    284  1.1  reinoud 	if (!nilfsdev)
    285  1.1  reinoud 		return;
    286  1.1  reinoud 	if (nilfsdev->refcnt > 0)
    287  1.1  reinoud 		return;
    288  1.1  reinoud 
    289  1.1  reinoud 	if (nilfsdev->dat_node)
    290  1.1  reinoud 		NILFS_UNSET_SYSTEMFILE(nilfsdev->dat_node->vnode);
    291  1.1  reinoud 	if (nilfsdev->cp_node)
    292  1.1  reinoud 		NILFS_UNSET_SYSTEMFILE(nilfsdev->cp_node->vnode);
    293  1.1  reinoud 	if (nilfsdev->su_node)
    294  1.1  reinoud 		NILFS_UNSET_SYSTEMFILE(nilfsdev->su_node->vnode);
    295  1.1  reinoud 
    296  1.1  reinoud 	nilfs_dispose_node(&nilfsdev->dat_node);
    297  1.1  reinoud 	nilfs_dispose_node(&nilfsdev->cp_node);
    298  1.1  reinoud 	nilfs_dispose_node(&nilfsdev->su_node);
    299  1.1  reinoud }
    300  1.1  reinoud 
    301  1.1  reinoud 
    302  1.1  reinoud /* --------------------------------------------------------------------- */
    303  1.1  reinoud 
    304  1.1  reinoud static int
    305  1.1  reinoud nilfs_check_superblock_crc(struct nilfs_super_block *super)
    306  1.1  reinoud {
    307  1.1  reinoud 	uint32_t super_crc, comp_crc;
    308  1.1  reinoud 
    309  1.1  reinoud 	/* check super block magic */
    310  1.1  reinoud 	if (nilfs_rw16(super->s_magic) != NILFS_SUPER_MAGIC)
    311  1.1  reinoud 		return 0;
    312  1.1  reinoud 
    313  1.1  reinoud 	/* preserve crc */
    314  1.1  reinoud 	super_crc  = nilfs_rw32(super->s_sum);
    315  1.1  reinoud 
    316  1.1  reinoud 	/* calculate */
    317  1.1  reinoud 	super->s_sum = 0;
    318  1.1  reinoud 	comp_crc = crc32_le(nilfs_rw32(super->s_crc_seed),
    319  1.1  reinoud 		(uint8_t *) super, nilfs_rw16(super->s_bytes));
    320  1.1  reinoud 
    321  1.1  reinoud 	/* restore */
    322  1.1  reinoud 	super->s_sum = nilfs_rw32(super_crc);
    323  1.1  reinoud 
    324  1.1  reinoud 	/* check CRC */
    325  1.1  reinoud 	return (super_crc == comp_crc);
    326  1.1  reinoud }
    327  1.1  reinoud 
    328  1.1  reinoud 
    329  1.1  reinoud 
    330  1.1  reinoud static int
    331  1.1  reinoud nilfs_read_superblock(struct nilfs_device *nilfsdev)
    332  1.1  reinoud {
    333  1.1  reinoud 	struct nilfs_super_block *super, tmp_super;
    334  1.1  reinoud 	struct buf *bp;
    335  1.1  reinoud 	uint64_t sb1off, sb2off;
    336  1.1  reinoud 	uint64_t time1, time2;
    337  1.1  reinoud 	uint64_t dev_blk;
    338  1.1  reinoud 	int dev_bsize, dev_blks;
    339  1.1  reinoud 	int sb1ok, sb2ok, swp;
    340  1.1  reinoud 	int error;
    341  1.1  reinoud 
    342  1.1  reinoud 	sb1off = NILFS_SB_OFFSET_BYTES;
    343  1.1  reinoud 	sb2off = NILFS_SB2_OFFSET_BYTES(nilfsdev->devsize);
    344  1.1  reinoud 
    345  1.1  reinoud 	dev_bsize = 1 << nilfsdev->devvp->v_mount->mnt_fs_bshift;
    346  1.1  reinoud 
    347  1.1  reinoud 	/* read our superblock regardless of backing device blocksize */
    348  1.1  reinoud 	dev_blk   = 0;
    349  1.1  reinoud 	dev_blks  = (sb1off + dev_bsize -1)/dev_bsize;
    350  1.1  reinoud 	error = bread(nilfsdev->devvp, dev_blk, dev_blks * dev_bsize, NOCRED, 0, &bp);
    351  1.1  reinoud 	if (error) {
    352  1.1  reinoud 		brelse(bp, BC_AGE);
    353  1.1  reinoud 		return error;
    354  1.1  reinoud 	}
    355  1.1  reinoud 
    356  1.1  reinoud 	/* copy read-in super block at the offset */
    357  1.1  reinoud 	super = &nilfsdev->super;
    358  1.1  reinoud 	memcpy(super, (uint8_t *) bp->b_data + NILFS_SB_OFFSET_BYTES,
    359  1.1  reinoud 		sizeof(struct nilfs_super_block));
    360  1.1  reinoud 	brelse(bp, BC_AGE);
    361  1.1  reinoud 
    362  1.1  reinoud 	/* read our 2nd superblock regardless of backing device blocksize */
    363  1.1  reinoud 	dev_blk   = sb2off / dev_bsize;
    364  1.1  reinoud 	dev_blks  = 2;		/* assumption max one dev_bsize */
    365  1.1  reinoud 	error = bread(nilfsdev->devvp, dev_blk, dev_blks * dev_bsize, NOCRED, 0, &bp);
    366  1.1  reinoud 	if (error) {
    367  1.1  reinoud 		brelse(bp, BC_AGE);
    368  1.1  reinoud 		return error;
    369  1.1  reinoud 	}
    370  1.1  reinoud 
    371  1.1  reinoud 	/* copy read-in superblock2 at the offset */
    372  1.1  reinoud 	super = &nilfsdev->super2;
    373  1.1  reinoud 	memcpy(super, (uint8_t *) bp->b_data + NILFS_SB_OFFSET_BYTES,
    374  1.1  reinoud 		sizeof(struct nilfs_super_block));
    375  1.1  reinoud 	brelse(bp, BC_AGE);
    376  1.1  reinoud 
    377  1.1  reinoud 	sb1ok = nilfs_check_superblock_crc(&nilfsdev->super);
    378  1.1  reinoud 	sb2ok = nilfs_check_superblock_crc(&nilfsdev->super2);
    379  1.1  reinoud 
    380  1.1  reinoud 	time1 = nilfs_rw64(nilfsdev->super.s_wtime);
    381  1.1  reinoud 	time2 = nilfs_rw64(nilfsdev->super2.s_wtime);
    382  1.1  reinoud 	swp = sb2ok && (time2 > time1);
    383  1.1  reinoud 
    384  1.1  reinoud 	if (swp) {
    385  1.1  reinoud 		printf("nilfs warning: broken superblock, using spare\n");
    386  1.1  reinoud 		tmp_super = nilfsdev->super2;
    387  1.1  reinoud 		nilfsdev->super2 = nilfsdev->super;	/* why preserve? */
    388  1.1  reinoud 		nilfsdev->super  = tmp_super;
    389  1.1  reinoud 	}
    390  1.1  reinoud 
    391  1.1  reinoud 	if (!sb1ok && !sb2ok) {
    392  1.1  reinoud 		printf("nilfs: no valid superblocks found\n");
    393  1.1  reinoud 		return EINVAL;
    394  1.1  reinoud 	}
    395  1.1  reinoud 
    396  1.1  reinoud 	return 0;
    397  1.1  reinoud }
    398  1.1  reinoud 
    399  1.1  reinoud 
    400  1.1  reinoud /* XXX NOTHING from the system nodes should need to be written here */
    401  1.1  reinoud static void
    402  1.1  reinoud nilfs_unmount_base(struct nilfs_device *nilfsdev)
    403  1.1  reinoud {
    404  1.1  reinoud 	int error;
    405  1.1  reinoud 
    406  1.1  reinoud 	if (!nilfsdev)
    407  1.1  reinoud 		return;
    408  1.1  reinoud 
    409  1.1  reinoud 	/* remove all our information */
    410  1.1  reinoud 	error = vinvalbuf(nilfsdev->devvp, 0, FSCRED, curlwp, 0, 0);
    411  1.1  reinoud 	KASSERT(error == 0);
    412  1.1  reinoud 
    413  1.1  reinoud 	/* release the device's system nodes */
    414  1.1  reinoud 	nilfs_release_system_nodes(nilfsdev);
    415  1.1  reinoud 
    416  1.1  reinoud 	/* TODO writeout super_block? */
    417  1.1  reinoud }
    418  1.1  reinoud 
    419  1.1  reinoud 
    420  1.1  reinoud static int
    421  1.1  reinoud nilfs_mount_base(struct nilfs_device *nilfsdev,
    422  1.1  reinoud 		struct mount *mp, struct nilfs_args *args)
    423  1.1  reinoud {
    424  1.1  reinoud 	struct lwp *l = curlwp;
    425  1.1  reinoud 	uint64_t last_pseg, last_cno, last_seq;
    426  1.1  reinoud 	uint32_t log_blocksize;
    427  1.1  reinoud 	int error;
    428  1.1  reinoud 
    429  1.1  reinoud 	/* flush out any old buffers remaining from a previous use. */
    430  1.1  reinoud 	if ((error = vinvalbuf(nilfsdev->devvp, V_SAVE, l->l_cred, l, 0, 0)))
    431  1.1  reinoud 		return error;
    432  1.1  reinoud 
    433  1.1  reinoud 	/* read in our superblock */
    434  1.1  reinoud 	error = nilfs_read_superblock(nilfsdev);
    435  1.1  reinoud 	if (error) {
    436  1.1  reinoud 		printf("nilfs_mount: can't read in super block : %d\n", error);
    437  1.1  reinoud 		return error;
    438  1.1  reinoud 	}
    439  1.1  reinoud 
    440  1.1  reinoud 	/* get our blocksize */
    441  1.1  reinoud 	log_blocksize = nilfs_rw32(nilfsdev->super.s_log_block_size);
    442  1.1  reinoud 	nilfsdev->blocksize   = (uint64_t) 1 << (log_blocksize + 10);
    443  1.1  reinoud 	/* TODO check superblock's blocksize limits */
    444  1.1  reinoud 
    445  1.1  reinoud 	/* calculate dat structure parameters */
    446  1.1  reinoud 	nilfs_calc_mdt_consts(nilfsdev, &nilfsdev->dat_mdt,
    447  1.1  reinoud 			nilfs_rw16(nilfsdev->super.s_dat_entry_size));
    448  1.1  reinoud 	nilfs_calc_mdt_consts(nilfsdev, &nilfsdev->ifile_mdt,
    449  1.1  reinoud 			nilfs_rw16(nilfsdev->super.s_inode_size));
    450  1.1  reinoud 
    451  1.1  reinoud 	DPRINTF(VOLUMES, ("nilfs_mount: accepted super block\n"));
    452  1.1  reinoud 
    453  1.1  reinoud 	/* search for the super root and roll forward when needed */
    454  1.1  reinoud 	nilfs_search_super_root(nilfsdev);
    455  1.1  reinoud 
    456  1.1  reinoud 	nilfsdev->mount_state = nilfs_rw16(nilfsdev->super.s_state);
    457  1.1  reinoud 	if (nilfsdev->mount_state != NILFS_VALID_FS) {
    458  1.1  reinoud 		printf("FS is seriously damaged, needs repairing\n");
    459  1.1  reinoud 		printf("aborting mount\n");
    460  1.1  reinoud 		return EINVAL;
    461  1.1  reinoud 	}
    462  1.1  reinoud 
    463  1.1  reinoud 	/*
    464  1.1  reinoud 	 * FS should be ok now. The superblock and the last segsum could be
    465  1.1  reinoud 	 * updated from the repair so extract running values again.
    466  1.1  reinoud 	 */
    467  1.1  reinoud 	last_pseg = nilfs_rw64(nilfsdev->super.s_last_pseg); /*blknr */
    468  1.1  reinoud 	last_cno  = nilfs_rw64(nilfsdev->super.s_last_cno);
    469  1.1  reinoud 	last_seq  = nilfs_rw64(nilfsdev->super.s_last_seq);
    470  1.1  reinoud 
    471  1.1  reinoud 	nilfsdev->last_seg_seq = last_seq;
    472  1.1  reinoud 	nilfsdev->last_seg_num = nilfs_get_segnum_of_block(nilfsdev, last_pseg);
    473  1.1  reinoud 	nilfsdev->next_seg_num = nilfs_get_segnum_of_block(nilfsdev,
    474  1.1  reinoud 		nilfs_rw64(nilfsdev->last_segsum.ss_next));
    475  1.1  reinoud 	nilfsdev->last_cno     = last_cno;
    476  1.1  reinoud 
    477  1.1  reinoud 	DPRINTF(VOLUMES, ("nilfs_mount: accepted super root\n"));
    478  1.1  reinoud 
    479  1.1  reinoud 	/* create system vnodes for DAT, CP and SEGSUM */
    480  1.1  reinoud 	error = nilfs_create_system_nodes(nilfsdev);
    481  1.1  reinoud 	if (error)
    482  1.1  reinoud 		nilfs_unmount_base(nilfsdev);
    483  1.1  reinoud 	return error;
    484  1.1  reinoud }
    485  1.1  reinoud 
    486  1.1  reinoud 
    487  1.1  reinoud static void
    488  1.1  reinoud nilfs_unmount_device(struct nilfs_device *nilfsdev)
    489  1.1  reinoud {
    490  1.1  reinoud 	int error;
    491  1.1  reinoud 
    492  1.1  reinoud 	/* is there anything? */
    493  1.1  reinoud 	if (nilfsdev == NULL)
    494  1.1  reinoud 		return;
    495  1.1  reinoud 
    496  1.1  reinoud 	/* remove the device only if we're the last reference */
    497  1.1  reinoud 	nilfsdev->refcnt--;
    498  1.1  reinoud 	if (nilfsdev->refcnt >= 1)
    499  1.1  reinoud 		return;
    500  1.1  reinoud 
    501  1.1  reinoud 	/* unmount our base */
    502  1.1  reinoud 	nilfs_unmount_base(nilfsdev);
    503  1.1  reinoud 
    504  1.1  reinoud 	/* remove from our device list */
    505  1.1  reinoud 	SLIST_REMOVE(&nilfs_devices, nilfsdev, nilfs_device, next_device);
    506  1.1  reinoud 
    507  1.1  reinoud 	/* close device */
    508  1.1  reinoud 	DPRINTF(VOLUMES, ("closing device\n"));
    509  1.1  reinoud 
    510  1.1  reinoud 	/* remove our mount reference before closing device */
    511  1.1  reinoud 	nilfsdev->devvp->v_specmountpoint = NULL;
    512  1.1  reinoud 
    513  1.1  reinoud 	/* devvp is still locked by us */
    514  1.1  reinoud 	vn_lock(nilfsdev->devvp, LK_EXCLUSIVE | LK_RETRY);
    515  1.1  reinoud 	error = VOP_CLOSE(nilfsdev->devvp, FREAD | FWRITE, NOCRED);
    516  1.1  reinoud 	if (error)
    517  1.1  reinoud 		printf("Error during closure of device! error %d, "
    518  1.1  reinoud 		       "device might stay locked\n", error);
    519  1.1  reinoud 	DPRINTF(VOLUMES, ("device close ok\n"));
    520  1.1  reinoud 
    521  1.1  reinoud 	/* clear our mount reference and release device node */
    522  1.1  reinoud 	vput(nilfsdev->devvp);
    523  1.1  reinoud 
    524  1.1  reinoud 	/* free our device info */
    525  1.1  reinoud 	free(nilfsdev, M_NILFSMNT);
    526  1.1  reinoud }
    527  1.1  reinoud 
    528  1.1  reinoud 
    529  1.1  reinoud static int
    530  1.1  reinoud nilfs_check_mounts(struct nilfs_device *nilfsdev, struct mount *mp,
    531  1.1  reinoud 	struct nilfs_args *args)
    532  1.1  reinoud {
    533  1.1  reinoud 	struct nilfs_mount  *ump;
    534  1.1  reinoud 	uint64_t last_cno;
    535  1.1  reinoud 
    536  1.1  reinoud 	/* no double-mounting of the same checkpoint */
    537  1.1  reinoud 	STAILQ_FOREACH(ump, &nilfsdev->mounts, next_mount) {
    538  1.1  reinoud 		if (ump->mount_args.cpno == args->cpno)
    539  1.1  reinoud 			return EBUSY;
    540  1.1  reinoud 	}
    541  1.1  reinoud 
    542  1.1  reinoud 	/* allow readonly mounts without questioning here */
    543  1.1  reinoud 	if (mp->mnt_flag & MNT_RDONLY)
    544  1.1  reinoud 		return 0;
    545  1.1  reinoud 
    546  1.1  reinoud 	/* readwrite mount you want */
    547  1.1  reinoud 	STAILQ_FOREACH(ump, &nilfsdev->mounts, next_mount) {
    548  1.1  reinoud 		/* only one RW mount on this device! */
    549  1.1  reinoud 		if ((ump->vfs_mountp->mnt_flag & MNT_RDONLY)==0)
    550  1.1  reinoud 			return EROFS;
    551  1.1  reinoud 		/* RDONLY on last mountpoint is device busy */
    552  1.1  reinoud 		last_cno = nilfs_rw64(ump->nilfsdev->super.s_last_cno);
    553  1.1  reinoud 		if (ump->mount_args.cpno == last_cno)
    554  1.1  reinoud 			return EBUSY;
    555  1.1  reinoud 	}
    556  1.1  reinoud 
    557  1.1  reinoud 	/* OK for now */
    558  1.1  reinoud 	return 0;
    559  1.1  reinoud }
    560  1.1  reinoud 
    561  1.1  reinoud 
    562  1.1  reinoud static int
    563  1.1  reinoud nilfs_mount_device(struct vnode *devvp, struct mount *mp, struct nilfs_args *args,
    564  1.1  reinoud 	struct nilfs_device **nilfsdev_p)
    565  1.1  reinoud {
    566  1.1  reinoud 	struct partinfo dpart;
    567  1.1  reinoud 	struct nilfs_device *nilfsdev;
    568  1.1  reinoud 	struct lwp *l = curlwp;
    569  1.1  reinoud 	int openflags, accessmode, error;
    570  1.1  reinoud 
    571  1.1  reinoud 	DPRINTF(VOLUMES, ("Mounting NILFS device\n"));
    572  1.1  reinoud 
    573  1.1  reinoud 	/* lookup device in our nilfs_mountpoints */
    574  1.1  reinoud 	*nilfsdev_p = NULL;
    575  1.1  reinoud 	SLIST_FOREACH(nilfsdev, &nilfs_devices, next_device)
    576  1.1  reinoud 		if (nilfsdev->devvp == devvp)
    577  1.1  reinoud 			break;
    578  1.1  reinoud 
    579  1.1  reinoud 	if (nilfsdev) {
    580  1.1  reinoud 		DPRINTF(VOLUMES, ("device already mounted\n"));
    581  1.1  reinoud 		error = nilfs_check_mounts(nilfsdev, mp, args);
    582  1.1  reinoud 		if (error)
    583  1.1  reinoud 			return error;
    584  1.1  reinoud 		nilfsdev->refcnt++;
    585  1.1  reinoud 		*nilfsdev_p = nilfsdev;
    586  1.1  reinoud 		return 0;
    587  1.1  reinoud 	}
    588  1.1  reinoud 
    589  1.1  reinoud 	DPRINTF(VOLUMES, ("no previous mounts on this device, mounting device\n"));
    590  1.1  reinoud 
    591  1.1  reinoud 	/* check if its a block device specified */
    592  1.1  reinoud 	if (devvp->v_type != VBLK) {
    593  1.1  reinoud 		vrele(devvp);
    594  1.1  reinoud 		return ENOTBLK;
    595  1.1  reinoud 	}
    596  1.1  reinoud 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    597  1.1  reinoud 		vrele(devvp);
    598  1.1  reinoud 		return ENXIO;
    599  1.1  reinoud 	}
    600  1.1  reinoud 
    601  1.1  reinoud 	/*
    602  1.1  reinoud 	 * If mount by non-root, then verify that user has necessary
    603  1.1  reinoud 	 * permissions on the device.
    604  1.1  reinoud 	 */
    605  1.1  reinoud 	accessmode = VREAD;
    606  1.1  reinoud 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    607  1.1  reinoud 		accessmode |= VWRITE;
    608  1.1  reinoud 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    609  1.1  reinoud 	error = genfs_can_mount(devvp, accessmode, l->l_cred);
    610  1.1  reinoud 	VOP_UNLOCK(devvp, 0);
    611  1.1  reinoud 	if (error) {
    612  1.1  reinoud 		vrele(devvp);
    613  1.1  reinoud 		return error;
    614  1.1  reinoud 	}
    615  1.1  reinoud 
    616  1.1  reinoud 	/*
    617  1.1  reinoud 	 * Open device read-write; TODO how about upgrading later when needed?
    618  1.1  reinoud 	 */
    619  1.1  reinoud 	openflags = FREAD | FWRITE;
    620  1.1  reinoud 	error = VOP_OPEN(devvp, openflags, FSCRED);
    621  1.1  reinoud 	if (error) {
    622  1.1  reinoud 		vrele(devvp);
    623  1.1  reinoud 		return error;
    624  1.1  reinoud 	}
    625  1.1  reinoud 
    626  1.1  reinoud 	/* opened ok, try mounting */
    627  1.1  reinoud 	nilfsdev = malloc(sizeof(struct nilfs_device), M_NILFSMNT, M_WAITOK);
    628  1.1  reinoud 	KASSERT(nilfsdev);
    629  1.1  reinoud 
    630  1.1  reinoud 	/* initialise */
    631  1.1  reinoud 	memset(nilfsdev, 0, sizeof(struct nilfs_device));
    632  1.1  reinoud 	nilfsdev->refcnt        = 1;
    633  1.1  reinoud 	nilfsdev->devvp         = devvp;
    634  1.1  reinoud 	nilfsdev->uncomitted_bl = 0;
    635  1.1  reinoud 	cv_init(&nilfsdev->sync_cv, "nilfssync");
    636  1.1  reinoud 	STAILQ_INIT(&nilfsdev->mounts);
    637  1.1  reinoud 
    638  1.1  reinoud 	/* get our device's size */
    639  1.1  reinoud 	error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED);
    640  1.1  reinoud 	if (error) {
    641  1.1  reinoud 		/* remove all our information */
    642  1.1  reinoud 		nilfs_unmount_device(nilfsdev);
    643  1.1  reinoud 		return EINVAL;
    644  1.1  reinoud 	}
    645  1.1  reinoud 	nilfsdev->devsize = dpart.part->p_size * dpart.disklab->d_secsize;
    646  1.1  reinoud 
    647  1.1  reinoud 	/* register nilfs_device in list */
    648  1.1  reinoud 	SLIST_INSERT_HEAD(&nilfs_devices, nilfsdev, next_device);
    649  1.1  reinoud 
    650  1.1  reinoud 	/* connect to the head for most recent files XXX really pass mp and args? */
    651  1.1  reinoud 	error = nilfs_mount_base(nilfsdev, mp, args);
    652  1.1  reinoud 	if (error) {
    653  1.1  reinoud 		/* remove all our information */
    654  1.1  reinoud 		nilfs_unmount_device(nilfsdev);
    655  1.1  reinoud 		return EINVAL;
    656  1.1  reinoud 	}
    657  1.1  reinoud 
    658  1.1  reinoud 	*nilfsdev_p = nilfsdev;
    659  1.1  reinoud 	DPRINTF(VOLUMES, ("NILFS device mounted ok\n"));
    660  1.1  reinoud 
    661  1.1  reinoud 	return 0;
    662  1.1  reinoud }
    663  1.1  reinoud 
    664  1.1  reinoud 
    665  1.1  reinoud static int
    666  1.1  reinoud nilfs_mount_checkpoint(struct nilfs_mount *ump)
    667  1.1  reinoud {
    668  1.1  reinoud 	struct nilfs_cpfile_header *cphdr;
    669  1.1  reinoud 	struct nilfs_checkpoint *cp;
    670  1.1  reinoud 	struct nilfs_inode  ifile_inode;
    671  1.1  reinoud 	struct nilfs_node  *cp_node;
    672  1.1  reinoud 	struct buf *bp;
    673  1.1  reinoud 	uint64_t ncp, nsn, fcpno, blocknr, last_cno;
    674  1.1  reinoud 	uint32_t off, dlen;
    675  1.1  reinoud 	int cp_per_block, error;
    676  1.1  reinoud 
    677  1.1  reinoud 	DPRINTF(VOLUMES, ("mount_nilfs: trying to mount checkpoint number "
    678  1.1  reinoud 		"%"PRIu64"\n", ump->mount_args.cpno));
    679  1.1  reinoud 
    680  1.1  reinoud 	cp_node = ump->nilfsdev->cp_node;
    681  1.1  reinoud 
    682  1.1  reinoud 	/* get cpfile header from 1st block of cp file */
    683  1.1  reinoud 	error = nilfs_bread(cp_node, 0, NOCRED, 0, &bp);
    684  1.1  reinoud 	if (error)
    685  1.1  reinoud 		return error;
    686  1.1  reinoud 	cphdr = (struct nilfs_cpfile_header *) bp->b_data;
    687  1.1  reinoud 	ncp = nilfs_rw64(cphdr->ch_ncheckpoints);
    688  1.1  reinoud 	nsn = nilfs_rw64(cphdr->ch_nsnapshots);
    689  1.1  reinoud 
    690  1.1  reinoud 	brelse(bp, BC_AGE);
    691  1.1  reinoud 
    692  1.1  reinoud 	DPRINTF(VOLUMES, ("mount_nilfs: checkpoint header read in\n"));
    693  1.1  reinoud 	DPRINTF(VOLUMES, ("\tNumber of checkpoints %"PRIu64"\n", ncp));
    694  1.1  reinoud 	DPRINTF(VOLUMES, ("\tNumber of snapshots   %"PRIu64"\n", nsn));
    695  1.1  reinoud 
    696  1.1  reinoud 	/* read in our specified checkpoint */
    697  1.1  reinoud 	dlen = nilfs_rw16(ump->nilfsdev->super.s_checkpoint_size);
    698  1.1  reinoud 	cp_per_block = ump->nilfsdev->blocksize / dlen;
    699  1.1  reinoud 
    700  1.1  reinoud 	fcpno = ump->mount_args.cpno + NILFS_CPFILE_FIRST_CHECKPOINT_OFFSET -1;
    701  1.1  reinoud 	blocknr =  fcpno / cp_per_block;
    702  1.1  reinoud 	off     = (fcpno % cp_per_block) * dlen;
    703  1.1  reinoud 
    704  1.1  reinoud 	error = nilfs_bread(cp_node, blocknr, NOCRED, 0, &bp);
    705  1.1  reinoud 	if (error) {
    706  1.1  reinoud 		printf("mount_nilfs: couldn't read cp block %"PRIu64"\n",
    707  1.1  reinoud 			fcpno);
    708  1.1  reinoud 		return EINVAL;
    709  1.1  reinoud 	}
    710  1.1  reinoud 
    711  1.1  reinoud 	/* needs to be a valid checkpoint */
    712  1.1  reinoud 	cp = (struct nilfs_checkpoint *) ((uint8_t *) bp->b_data + off);
    713  1.1  reinoud 	if (cp->cp_flags & NILFS_CHECKPOINT_INVALID) {
    714  1.1  reinoud 		printf("mount_nilfs: checkpoint marked invalid\n");
    715  1.1  reinoud 		brelse(bp, BC_AGE);
    716  1.1  reinoud 		return EINVAL;
    717  1.1  reinoud 	}
    718  1.1  reinoud 
    719  1.1  reinoud 	/* is this really the checkpoint we want? */
    720  1.1  reinoud 	if (nilfs_rw64(cp->cp_cno) != ump->mount_args.cpno) {
    721  1.1  reinoud 		printf("mount_nilfs: checkpoint file corrupt? "
    722  1.1  reinoud 			"expected cpno %"PRIu64", found cpno %"PRIu64"\n",
    723  1.1  reinoud 			ump->mount_args.cpno, nilfs_rw64(cp->cp_cno));
    724  1.1  reinoud 		brelse(bp, BC_AGE);
    725  1.1  reinoud 		return EINVAL;
    726  1.1  reinoud 	}
    727  1.1  reinoud 
    728  1.1  reinoud 	/* check if its a snapshot ! */
    729  1.1  reinoud 	last_cno = nilfs_rw64(ump->nilfsdev->super.s_last_cno);
    730  1.1  reinoud 	if (ump->mount_args.cpno != last_cno) {
    731  1.1  reinoud 		/* only allow snapshots if not mounting on the last cp */
    732  1.1  reinoud 		if ((cp->cp_flags & NILFS_CHECKPOINT_SNAPSHOT) == 0) {
    733  1.1  reinoud 			printf( "mount_nilfs: checkpoint %"PRIu64" is not a "
    734  1.1  reinoud 				"snapshot\n", ump->mount_args.cpno);
    735  1.1  reinoud 			brelse(bp, BC_AGE);
    736  1.1  reinoud 			return EINVAL;
    737  1.1  reinoud 		}
    738  1.1  reinoud 	}
    739  1.1  reinoud 
    740  1.1  reinoud 	ifile_inode = cp->cp_ifile_inode;
    741  1.1  reinoud 	brelse(bp, BC_AGE);
    742  1.1  reinoud 
    743  1.1  reinoud 	/* get ifile inode */
    744  1.1  reinoud 	error = nilfs_get_node_raw(ump->nilfsdev, NULL, NILFS_IFILE_INO,
    745  1.1  reinoud 		&ifile_inode, &ump->ifile_node);
    746  1.1  reinoud 	if (error) {
    747  1.1  reinoud 		printf("mount_nilfs: can't read ifile node\n");
    748  1.1  reinoud 		return EINVAL;
    749  1.1  reinoud 	}
    750  1.1  reinoud 	NILFS_SET_SYSTEMFILE(ump->ifile_node->vnode);
    751  1.1  reinoud 
    752  1.1  reinoud 	/* get root node? */
    753  1.1  reinoud 
    754  1.1  reinoud 	return 0;
    755  1.1  reinoud }
    756  1.1  reinoud 
    757  1.1  reinoud 
    758  1.1  reinoud static int
    759  1.1  reinoud nilfs_stop_writing(struct nilfs_mount *ump)
    760  1.1  reinoud {
    761  1.1  reinoud 	/* readonly mounts won't write */
    762  1.1  reinoud 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
    763  1.1  reinoud 		return 0;
    764  1.1  reinoud 
    765  1.1  reinoud 	DPRINTF(CALL, ("nilfs_stop_writing called for RW mount\n"));
    766  1.1  reinoud 
    767  1.1  reinoud 	/* TODO writeout super_block? */
    768  1.1  reinoud 	/* XXX no support for writing yet anyway */
    769  1.1  reinoud 	return 0;
    770  1.1  reinoud }
    771  1.1  reinoud 
    772  1.1  reinoud 
    773  1.1  reinoud /* --------------------------------------------------------------------- */
    774  1.1  reinoud 
    775  1.1  reinoud 
    776  1.1  reinoud 
    777  1.1  reinoud #define MPFREE(a, lst) \
    778  1.1  reinoud 	if ((a)) free((a), lst);
    779  1.1  reinoud static void
    780  1.1  reinoud free_nilfs_mountinfo(struct mount *mp)
    781  1.1  reinoud {
    782  1.1  reinoud 	struct nilfs_mount *ump = VFSTONILFS(mp);
    783  1.1  reinoud 
    784  1.1  reinoud 	if (ump == NULL)
    785  1.1  reinoud 		return;
    786  1.1  reinoud 
    787  1.1  reinoud 	mutex_destroy(&ump->ihash_lock);
    788  1.1  reinoud 	mutex_destroy(&ump->get_node_lock);
    789  1.1  reinoud 	MPFREE(ump, M_NILFSMNT);
    790  1.1  reinoud }
    791  1.1  reinoud #undef MPFREE
    792  1.1  reinoud 
    793  1.1  reinoud int
    794  1.1  reinoud nilfs_mount(struct mount *mp, const char *path,
    795  1.1  reinoud 	  void *data, size_t *data_len)
    796  1.1  reinoud {
    797  1.1  reinoud 	struct nilfs_args   *args = data;
    798  1.1  reinoud 	struct nilfs_device *nilfsdev;
    799  1.1  reinoud 	struct nilfs_mount  *ump;
    800  1.1  reinoud 	struct vnode *devvp;
    801  1.1  reinoud 	int lst, error;
    802  1.1  reinoud 
    803  1.1  reinoud 	DPRINTF(VFSCALL, ("nilfs_mount called\n"));
    804  1.1  reinoud 
    805  1.1  reinoud 	if (*data_len < sizeof *args)
    806  1.1  reinoud 		return EINVAL;
    807  1.1  reinoud 
    808  1.1  reinoud 	if (mp->mnt_flag & MNT_GETARGS) {
    809  1.1  reinoud 		/* request for the mount arguments */
    810  1.1  reinoud 		ump = VFSTONILFS(mp);
    811  1.1  reinoud 		if (ump == NULL)
    812  1.1  reinoud 			return EINVAL;
    813  1.1  reinoud 		*args = ump->mount_args;
    814  1.1  reinoud 		*data_len = sizeof *args;
    815  1.1  reinoud 		return 0;
    816  1.1  reinoud 	}
    817  1.1  reinoud 
    818  1.1  reinoud 	/* check/translate struct version */
    819  1.1  reinoud 	if (args->version != 1) {
    820  1.1  reinoud 		printf("mount_nilfs: unrecognized argument structure version\n");
    821  1.1  reinoud 		return EINVAL;
    822  1.1  reinoud 	}
    823  1.1  reinoud 	/* TODO sanity checking other mount arguments */
    824  1.1  reinoud 
    825  1.1  reinoud 	/* handle request for updating mount parameters */
    826  1.1  reinoud 	if (mp->mnt_flag & MNT_UPDATE) {
    827  1.1  reinoud 		/* TODO can't update my mountpoint yet */
    828  1.1  reinoud 		return EOPNOTSUPP;
    829  1.1  reinoud 	}
    830  1.1  reinoud 
    831  1.1  reinoud 	/* lookup name to get its vnode */
    832  1.1  reinoud 	error = namei_simple_user(args->fspec, NSM_FOLLOW_NOEMULROOT, &devvp);
    833  1.1  reinoud 	if (error)
    834  1.1  reinoud 		return error;
    835  1.1  reinoud 
    836  1.1  reinoud #ifdef DEBUG
    837  1.1  reinoud 	if (nilfs_verbose & NILFS_DEBUG_VOLUMES)
    838  1.1  reinoud 		vprint("NILFS mount, trying to mount \n", devvp);
    839  1.1  reinoud #endif
    840  1.1  reinoud 
    841  1.1  reinoud 	error = nilfs_mount_device(devvp, mp, args, &nilfsdev);
    842  1.1  reinoud 	if (error)
    843  1.1  reinoud 		return error;
    844  1.1  reinoud 
    845  1.1  reinoud 	/*
    846  1.1  reinoud 	 * Create a nilfs_mount on the specified checkpoint. Note that only
    847  1.1  reinoud 	 * ONE RW mount point can exist and it needs to have the highest
    848  1.1  reinoud 	 * checkpoint nr. If mounting RW and its not on the last checkpoint we
    849  1.1  reinoud 	 * need to invalidate all checkpoints that follow!!! This is an
    850  1.1  reinoud 	 * advanced option.
    851  1.1  reinoud 	 */
    852  1.1  reinoud 
    853  1.1  reinoud 	/* setup basic mountpoint structure */
    854  1.1  reinoud 	mp->mnt_data = NULL;
    855  1.1  reinoud 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (uint32_t) devvp->v_rdev;
    856  1.1  reinoud 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NILFS);
    857  1.1  reinoud 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    858  1.1  reinoud 	mp->mnt_stat.f_namemax = NILFS_NAME_LEN;
    859  1.1  reinoud 	mp->mnt_flag  |= MNT_LOCAL;
    860  1.1  reinoud 
    861  1.1  reinoud 	/* XXX can't enable MPSAFE yet since genfs barfs on bad CV */
    862  1.1  reinoud 	// mp->mnt_iflag |= IMNT_MPSAFE;
    863  1.1  reinoud 
    864  1.1  reinoud 	/* set our dev and fs units */
    865  1.1  reinoud 	mp->mnt_dev_bshift = nilfs_rw32(nilfsdev->super.s_log_block_size) + 10;
    866  1.1  reinoud 	mp->mnt_fs_bshift  = mp->mnt_dev_bshift;
    867  1.1  reinoud 
    868  1.1  reinoud 	/* allocate nilfs part of mount structure; malloc always succeeds */
    869  1.1  reinoud 	ump = malloc(sizeof(struct nilfs_mount), M_NILFSMNT, M_WAITOK | M_ZERO);
    870  1.1  reinoud 
    871  1.1  reinoud 	/* init locks */
    872  1.1  reinoud 	mutex_init(&ump->ihash_lock, MUTEX_DEFAULT, IPL_NONE);
    873  1.1  reinoud 	mutex_init(&ump->get_node_lock, MUTEX_DEFAULT, IPL_NONE);
    874  1.1  reinoud 
    875  1.1  reinoud 	/* init our hash table for inode lookup */
    876  1.1  reinoud 	for (lst = 0; lst < NILFS_INODE_HASHSIZE; lst++) {
    877  1.1  reinoud 		LIST_INIT(&ump->nilfs_nodes[lst]);
    878  1.1  reinoud 	}
    879  1.1  reinoud 
    880  1.1  reinoud 	/* set up linkage */
    881  1.1  reinoud 	mp->mnt_data    =  ump;
    882  1.1  reinoud 	ump->vfs_mountp =  mp;
    883  1.1  reinoud 	ump->nilfsdev   =  nilfsdev;
    884  1.1  reinoud 
    885  1.1  reinoud #if 0
    886  1.1  reinoud #ifndef NILFS_READWRITE
    887  1.1  reinoud 	/* force read-only for now */
    888  1.1  reinoud 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    889  1.1  reinoud 		printf( "Enable kernel/module option NILFS_READWRITE for "
    890  1.1  reinoud 			"writing, downgrading access to read-only\n");
    891  1.1  reinoud 		mp->mnt_flag |= MNT_RDONLY;
    892  1.1  reinoud 	}
    893  1.1  reinoud #endif
    894  1.1  reinoud #endif
    895  1.1  reinoud 
    896  1.1  reinoud 	/* DONT register our nilfs mountpoint on our vfs mountpoint */
    897  1.1  reinoud 	devvp->v_specmountpoint = NULL;
    898  1.1  reinoud #if 0
    899  1.1  reinoud 	if (devvp->v_specmountpoint == NULL)
    900  1.1  reinoud 		devvp->v_specmountpoint = mp;
    901  1.1  reinoud 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    902  1.1  reinoud 		devvp->v_specmountpoint = mp;
    903  1.1  reinoud #endif
    904  1.1  reinoud 
    905  1.1  reinoud 	/* add our mountpoint */
    906  1.1  reinoud 	STAILQ_INSERT_TAIL(&nilfsdev->mounts, ump, next_mount);
    907  1.1  reinoud 
    908  1.1  reinoud 	/* get our selected checkpoint */
    909  1.1  reinoud 	if (args->cpno == 0)
    910  1.1  reinoud 		args->cpno = nilfsdev->last_cno;
    911  1.1  reinoud 	args->cpno = MIN(args->cpno, nilfsdev->last_cno);
    912  1.1  reinoud 
    913  1.1  reinoud 	/* setting up other parameters */
    914  1.1  reinoud 	ump->mount_args = *args;
    915  1.1  reinoud 	error = nilfs_mount_checkpoint(ump);
    916  1.1  reinoud 	if (error) {
    917  1.1  reinoud 		nilfs_unmount(mp, MNT_FORCE);
    918  1.1  reinoud 		return error;
    919  1.1  reinoud 	}
    920  1.1  reinoud 
    921  1.1  reinoud 	/* set VFS info */
    922  1.1  reinoud 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
    923  1.1  reinoud 			mp->mnt_op->vfs_name, mp, curlwp);
    924  1.1  reinoud 	if (error) {
    925  1.1  reinoud 		nilfs_unmount(mp, MNT_FORCE);
    926  1.1  reinoud 		return error;
    927  1.1  reinoud 	}
    928  1.1  reinoud 
    929  1.1  reinoud 	/* successfully mounted */
    930  1.1  reinoud 	DPRINTF(VOLUMES, ("nilfs_mount() successfull\n"));
    931  1.1  reinoud 
    932  1.1  reinoud 	return 0;
    933  1.1  reinoud }
    934  1.1  reinoud 
    935  1.1  reinoud /* --------------------------------------------------------------------- */
    936  1.1  reinoud 
    937  1.1  reinoud 
    938  1.1  reinoud /* remove our mountpoint and if its the last reference, remove our device */
    939  1.1  reinoud int
    940  1.1  reinoud nilfs_unmount(struct mount *mp, int mntflags)
    941  1.1  reinoud {
    942  1.1  reinoud 	struct nilfs_device *nilfsdev;
    943  1.1  reinoud 	struct nilfs_mount  *ump;
    944  1.1  reinoud 	int error, flags;
    945  1.1  reinoud 
    946  1.1  reinoud 	DPRINTF(VFSCALL, ("nilfs_umount called\n"));
    947  1.1  reinoud 
    948  1.1  reinoud 	ump = VFSTONILFS(mp);
    949  1.1  reinoud 	if (!ump)
    950  1.1  reinoud 		panic("NILFS unmount: empty ump\n");
    951  1.1  reinoud 	nilfsdev = ump->nilfsdev;
    952  1.1  reinoud 
    953  1.1  reinoud 	/*
    954  1.1  reinoud 	 * Flush all nodes associated to this mountpoint. By specifying
    955  1.1  reinoud 	 * SKIPSYSTEM we can skip vnodes marked with VV_SYSTEM. This hardly
    956  1.1  reinoud 	 * documented feature allows us to exempt certain files from being
    957  1.1  reinoud 	 * flushed.
    958  1.1  reinoud 	 */
    959  1.1  reinoud 	flags = (mntflags & MNT_FORCE) ? FORCECLOSE : 0;
    960  1.1  reinoud 	if ((error = vflush(mp, NULLVP, flags | SKIPSYSTEM)) != 0)
    961  1.1  reinoud 		return error;
    962  1.1  reinoud 
    963  1.1  reinoud 	/* if we're the write mount, we ought to close the writing session */
    964  1.1  reinoud 	error = nilfs_stop_writing(ump);
    965  1.1  reinoud 	if (error)
    966  1.1  reinoud 		return error;
    967  1.1  reinoud 
    968  1.1  reinoud 	if (ump->ifile_node)
    969  1.1  reinoud 		NILFS_UNSET_SYSTEMFILE(ump->ifile_node->vnode);
    970  1.1  reinoud 	nilfs_dispose_node(&ump->ifile_node);
    971  1.1  reinoud 
    972  1.1  reinoud 	/* remove our mount point */
    973  1.1  reinoud 	STAILQ_REMOVE(&nilfsdev->mounts, ump, nilfs_mount, next_mount);
    974  1.1  reinoud 	free_nilfs_mountinfo(mp);
    975  1.1  reinoud 
    976  1.1  reinoud 	/* free ump struct references */
    977  1.1  reinoud 	mp->mnt_data = NULL;
    978  1.1  reinoud 	mp->mnt_flag &= ~MNT_LOCAL;
    979  1.1  reinoud 
    980  1.1  reinoud 	/* unmount the device itself when we're the last one */
    981  1.1  reinoud 	nilfs_unmount_device(nilfsdev);
    982  1.1  reinoud 
    983  1.1  reinoud 	DPRINTF(VOLUMES, ("Fin unmount\n"));
    984  1.1  reinoud 	return error;
    985  1.1  reinoud }
    986  1.1  reinoud 
    987  1.1  reinoud /* --------------------------------------------------------------------- */
    988  1.1  reinoud 
    989  1.1  reinoud int
    990  1.1  reinoud nilfs_start(struct mount *mp, int flags)
    991  1.1  reinoud {
    992  1.1  reinoud 	/* do we have to do something here? */
    993  1.1  reinoud 	return 0;
    994  1.1  reinoud }
    995  1.1  reinoud 
    996  1.1  reinoud /* --------------------------------------------------------------------- */
    997  1.1  reinoud 
    998  1.1  reinoud int
    999  1.1  reinoud nilfs_root(struct mount *mp, struct vnode **vpp)
   1000  1.1  reinoud {
   1001  1.1  reinoud 	struct nilfs_mount *ump = VFSTONILFS(mp);
   1002  1.1  reinoud 	struct nilfs_node  *node;
   1003  1.1  reinoud 	int error;
   1004  1.1  reinoud 
   1005  1.1  reinoud 	DPRINTF(NODE, ("nilfs_root called\n"));
   1006  1.1  reinoud 
   1007  1.1  reinoud 	error = nilfs_get_node(ump, NILFS_ROOT_INO, &node);
   1008  1.1  reinoud 	if (node)  {
   1009  1.1  reinoud 		*vpp = node->vnode;
   1010  1.1  reinoud 		KASSERT(node->vnode->v_vflag & VV_ROOT);
   1011  1.1  reinoud 	}
   1012  1.1  reinoud 
   1013  1.1  reinoud 	DPRINTF(NODE, ("nilfs_root finished\n"));
   1014  1.1  reinoud 	return error;
   1015  1.1  reinoud }
   1016  1.1  reinoud 
   1017  1.1  reinoud /* --------------------------------------------------------------------- */
   1018  1.1  reinoud 
   1019  1.1  reinoud int
   1020  1.1  reinoud nilfs_statvfs(struct mount *mp, struct statvfs *sbp)
   1021  1.1  reinoud {
   1022  1.1  reinoud 	struct nilfs_mount *ump = VFSTONILFS(mp);
   1023  1.1  reinoud 	uint32_t blocksize;
   1024  1.1  reinoud 
   1025  1.1  reinoud 	DPRINTF(VFSCALL, ("nilfs_statvfs called\n"));
   1026  1.1  reinoud 
   1027  1.1  reinoud 	blocksize = ump->nilfsdev->blocksize;
   1028  1.1  reinoud 	sbp->f_flag   = mp->mnt_flag;
   1029  1.1  reinoud 	sbp->f_bsize  = blocksize;
   1030  1.1  reinoud 	sbp->f_frsize = blocksize;
   1031  1.1  reinoud 	sbp->f_iosize = blocksize;
   1032  1.1  reinoud 
   1033  1.1  reinoud 	copy_statvfs_info(sbp, mp);
   1034  1.1  reinoud 	return 0;
   1035  1.1  reinoud }
   1036  1.1  reinoud 
   1037  1.1  reinoud /* --------------------------------------------------------------------- */
   1038  1.1  reinoud 
   1039  1.1  reinoud int
   1040  1.1  reinoud nilfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
   1041  1.1  reinoud {
   1042  1.1  reinoud //	struct nilfs_mount *ump = VFSTONILFS(mp);
   1043  1.1  reinoud 
   1044  1.1  reinoud 	DPRINTF(VFSCALL, ("nilfs_sync called\n"));
   1045  1.1  reinoud 	/* if called when mounted readonly, just ignore */
   1046  1.1  reinoud 	if (mp->mnt_flag & MNT_RDONLY)
   1047  1.1  reinoud 		return 0;
   1048  1.1  reinoud 
   1049  1.1  reinoud 	DPRINTF(VFSCALL, ("end of nilfs_sync()\n"));
   1050  1.1  reinoud 
   1051  1.1  reinoud 	return 0;
   1052  1.1  reinoud }
   1053  1.1  reinoud 
   1054  1.1  reinoud /* --------------------------------------------------------------------- */
   1055  1.1  reinoud 
   1056  1.1  reinoud /*
   1057  1.1  reinoud  * Get vnode for the file system type specific file id ino for the fs. Its
   1058  1.1  reinoud  * used for reference to files by unique ID and for NFSv3.
   1059  1.1  reinoud  * (optional) TODO lookup why some sources state NFSv3
   1060  1.1  reinoud  */
   1061  1.1  reinoud int
   1062  1.1  reinoud nilfs_vget(struct mount *mp, ino_t ino,
   1063  1.1  reinoud     struct vnode **vpp)
   1064  1.1  reinoud {
   1065  1.1  reinoud 	DPRINTF(NOTIMPL, ("nilfs_vget called\n"));
   1066  1.1  reinoud 	return EOPNOTSUPP;
   1067  1.1  reinoud }
   1068  1.1  reinoud 
   1069  1.1  reinoud /* --------------------------------------------------------------------- */
   1070  1.1  reinoud 
   1071  1.1  reinoud /*
   1072  1.1  reinoud  * Lookup vnode for file handle specified
   1073  1.1  reinoud  */
   1074  1.1  reinoud int
   1075  1.1  reinoud nilfs_fhtovp(struct mount *mp, struct fid *fhp,
   1076  1.1  reinoud     struct vnode **vpp)
   1077  1.1  reinoud {
   1078  1.1  reinoud 	DPRINTF(NOTIMPL, ("nilfs_fhtovp called\n"));
   1079  1.1  reinoud 	return EOPNOTSUPP;
   1080  1.1  reinoud }
   1081  1.1  reinoud 
   1082  1.1  reinoud /* --------------------------------------------------------------------- */
   1083  1.1  reinoud 
   1084  1.1  reinoud /*
   1085  1.1  reinoud  * Create an unique file handle. Its structure is opaque and won't be used by
   1086  1.1  reinoud  * other subsystems. It should uniquely identify the file in the filingsystem
   1087  1.1  reinoud  * and enough information to know if a file has been removed and/or resources
   1088  1.1  reinoud  * have been recycled.
   1089  1.1  reinoud  */
   1090  1.1  reinoud int
   1091  1.1  reinoud nilfs_vptofh(struct vnode *vp, struct fid *fid,
   1092  1.1  reinoud     size_t *fh_size)
   1093  1.1  reinoud {
   1094  1.1  reinoud 	DPRINTF(NOTIMPL, ("nilfs_vptofh called\n"));
   1095  1.1  reinoud 	return EOPNOTSUPP;
   1096  1.1  reinoud }
   1097  1.1  reinoud 
   1098  1.1  reinoud /* --------------------------------------------------------------------- */
   1099  1.1  reinoud 
   1100  1.1  reinoud /*
   1101  1.1  reinoud  * Create a file system snapshot at the specified timestamp.
   1102  1.1  reinoud  */
   1103  1.1  reinoud int
   1104  1.1  reinoud nilfs_snapshot(struct mount *mp, struct vnode *vp,
   1105  1.1  reinoud     struct timespec *tm)
   1106  1.1  reinoud {
   1107  1.1  reinoud 	DPRINTF(NOTIMPL, ("nilfs_snapshot called\n"));
   1108  1.1  reinoud 	return EOPNOTSUPP;
   1109  1.1  reinoud }
   1110  1.1  reinoud 
   1111  1.1  reinoud /* --------------------------------------------------------------------- */
   1112