Home | History | Annotate | Line # | Download | only in ultrix
ultrix_fs.c revision 1.3
      1 /*	$NetBSD: ultrix_fs.c,v 1.3 1996/02/19 15:41:39 pk Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995
      5  *	Jonathan Stone (hereinafter referred to as the author)
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/malloc.h>
     34 #include <sys/exec.h>
     35 #include <sys/namei.h>
     36 #include <sys/mount.h>
     37 #include <net/if.h>
     38 #include <netinet/in.h>
     39 
     40 #include <nfs/rpcv2.h>
     41 #include <nfs/nfsproto.h>
     42 #include <nfs/nfs.h>
     43 
     44 #include <sys/syscallargs.h>
     45 #include <compat/ultrix/ultrix_syscallargs.h>
     46 
     47 #include <vm/vm.h>
     48 
     49 #define	ULTRIX_MAXPATHLEN	1024
     50 
     51 /**
     52  ** Ultrix filesystem operations: mount(), getmnt().
     53  ** These are included purely so one can place an (ECOFF or ELF)
     54  ** NetBSD/pmax kernel in an Ultrix root filesystem, boot it,
     55  ** and over-write the Ultrix root parition with NetBSD binaries.
     56  **/
     57 
     58 /*
     59  * Ultrix file system data structure, as modified by
     60  * Ultrix getmntent(). This  structure is padded to 2560 bytes, for
     61  * compatiblity with the size the Ultrix kernel and user apps expect.
     62  */
     63 struct ultrix_fs_data {
     64 	u_int32_t	ufsd_flags;	/* how mounted */
     65 	u_int32_t	ufsd_mtsize;	/* max transfer size in bytes */
     66 	u_int32_t	ufsd_otsize;	/* optimal transfer size in bytes */
     67 	u_int32_t	ufsd_bsize;	/* fs block size (bytes) for vm code */
     68 	u_int32_t	ufsd_fstype;	/* see ../h/fs_types.h  */
     69 	u_int32_t	ufsd_gtot;	/* total number of gnodes */
     70 	u_int32_t	ufsd_gfree;	/* # of free gnodes */
     71 	u_int32_t	ufsd_btot;	/* total number of 1K blocks */
     72 	u_int32_t	ufsd_bfree;	/* # of free 1K blocks */
     73 	u_int32_t	ufsd_bfreen;	/* user consumable 1K blocks */
     74 	u_int32_t	ufsd_pgthresh;	/* min size in bytes before paging*/
     75 	int32_t		ufsd_uid;	/* uid that mounted me */
     76 	int16_t		ufsd_dev;	/* major/minor of fs */
     77 	int16_t		ufsd_exroot;	/* root mapping from exports */
     78 	char		ufsd_devname[ULTRIX_MAXPATHLEN + 4]; /* name of dev */
     79 	char		ufsd_path[ULTRIX_MAXPATHLEN + 4]; /* name of mnt point */
     80 	u_int32_t	ufsd_nupdate;	/* number of writes */
     81 	u_int32_t	ufsd_pad[112];	/* pad to 2560 bytes. */
     82 };
     83 
     84 /*
     85  * Get statistics on mounted filesystems.
     86  */
     87 #if 0
     88 struct ultrix_getmnt_args {
     89 	int32_t *start;
     90 	struct ultrix_fs_data *buf;
     91 	int32_t bufsize;
     92 	int32_t mode;
     93 	char *path;
     94 };
     95 
     96 #endif
     97 /*
     98  * Ultrix getmnt() flags.
     99  * The operation getmnt() should perform is incoded in the flag
    100  * argument.  There are two independent attributes.
    101  *
    102  * ULTRIX_NOSTAT_xxx will never hang, but it may not return
    103  * up-to-date statistics. (For NFS clients, it returns whatever is
    104  * in the cache.) ULTRIX_STAT_xxx returns up-to-date info but may
    105  * hang (e.g., on dead NFS servers).
    106  *
    107  * ULTRIX_xxSTAT_ONE returns statistics on just one filesystem, determined
    108  * by the parth argument.  ULTRIX_xxSTAT_MANY ignores the path argument and
    109  * returns info on as many  filesystems fit in the structure.
    110  * the start argument, which should be zero on the first call,
    111  * can be used to iterate over all filesystems.
    112  *
    113  */
    114 #define	ULTRIX_NOSTAT_MANY	1
    115 #define	ULTRIX_STAT_MANY	2
    116 #define	ULTRIX_STAT_ONE		3
    117 #define	ULTRIX_NOSTAT_ONE	4
    118 
    119 /*
    120  * Ultrix gnode-layer  filesystem codes.
    121  */
    122 #define ULTRIX_FSTYPE_UNKNOWN	0x0
    123 #define ULTRIX_FSTYPE_ULTRIX	0x1	/*  Ultrix UFS: basically 4.2bsd FFS */
    124 #define ULTRIX_FSTYPE_NFS	0x5	/*  NFS v2 */
    125 
    126 /*
    127  * Ultrix mount(2) options
    128  */
    129 #define ULTRIX_NM_RONLY    0x0001  /* mount read-only */
    130 #define ULTRIX_NM_SOFT     0x0002  /* soft mount (hard is default) */
    131 #define ULTRIX_NM_WSIZE    0x0004  /* set write size */
    132 #define ULTRIX_NM_RSIZE    0x0008  /* set read size */
    133 #define ULTRIX_NM_TIMEO    0x0010  /* set initial timeout */
    134 #define ULTRIX_NM_RETRANS  0x0020  /* set number of request retrys */
    135 #define ULTRIX_NM_HOSTNAME 0x0040  /* set hostname for error printf */
    136 #define ULTRIX_NM_PGTHRESH 0x0080  /* set page threshold for exec */
    137 #define ULTRIX_NM_INT      0x0100  /* allow hard mount keyboard interrupts */
    138 #define ULTRIX_NM_NOAC     0x0200  /* don't cache attributes */
    139 
    140 
    141 /*
    142  * Construct an Ultrix getmnt() ultrix_fs_data from the native NetBSD
    143  * struct statfs.
    144  */
    145 static void
    146 make_ultrix_mntent(sp, tem)
    147 	register struct statfs *sp;
    148 	register struct ultrix_fs_data *tem;
    149 {
    150 
    151 	bzero(tem, sizeof (*tem));
    152 
    153 	tem->ufsd_flags = sp->f_flags;		/* XXX translate */
    154 	tem->ufsd_mtsize = sp->f_bsize;		/* XXX max transfer size */
    155 	tem->ufsd_otsize = sp->f_iosize;
    156 	tem->ufsd_bsize = sp->f_bsize;
    157 	/*
    158 	 * Translate file system type. NetBSD/1.1 has f_type zero,
    159 	 * and uses an fstype string instead.
    160 	 * For now, map types not in Ultrix (kernfs, null, procfs...)
    161 	 * to UFS, since Ultrix mout will try and call mount_unknown
    162 	 * for ULTRIX_FSTYPE_UNKNOWN, but lacks a mount_unknown binary.
    163 	 */
    164 	tem->ufsd_fstype = ULTRIX_FSTYPE_NFS;
    165 	if (strcmp(sp->f_fstypename, "ffs") == 0)
    166 		tem->ufsd_fstype = ULTRIX_FSTYPE_ULTRIX;
    167 
    168 	tem->ufsd_gtot = sp->f_files;		/* total "gnodes" */
    169 	tem->ufsd_gfree = sp->f_ffree;		/* free "gnodes"/
    170 	tem->ufsd_btot = sp->f_blocks;		/* total 1k blocks */
    171 	/*tem->ufsd_bfree = sp->f_bfree;	/* free 1k blocks */
    172 	/*tem->ufsd_bfree = sp->f_bavail;	/* free 1k blocks */
    173 	tem->ufsd_bfreen = sp->f_bavail;	/* blocks available to users */
    174 	tem->ufsd_pgthresh = 0;			/* not relevant */
    175 	tem->ufsd_uid = 0;			/* XXX kept where ?*/
    176 	tem->ufsd_dev = 0;			/* ?? */
    177 	tem->ufsd_exroot  = 0;			/* ?? */
    178 	strncpy(tem->ufsd_path, sp->f_mntonname, ULTRIX_MAXPATHLEN);
    179 	strncpy(tem->ufsd_devname, sp->f_mntfromname, ULTRIX_MAXPATHLEN);
    180 #if 0
    181 	/* In NetBSD-1.1, filesystem type is unused and always 0 */
    182 	printf("mntent: %s type %d\n", tem->ufsd_devname, tem->ufsd_fstype);
    183 	printf("mntent: %s tot %d free %d user%d\n",
    184 	 tem->ufsd_devname, sp->f_blocks, sp->f_bfree, sp->f_bavail);
    185 #endif
    186 }
    187 
    188 int
    189 ultrix_sys_getmnt(p, v, retval)
    190 	struct proc *p;
    191 	void *v;
    192 	int *retval;
    193 {
    194 	struct ultrix_sys_getmnt_args *uap = v;
    195 	struct mount *mp, *nmp;
    196 	struct statfs *sp;
    197 	struct ultrix_fs_data *sfsp;
    198 	char *path;
    199 	int mntflags;
    200 	int skip;
    201 	int start;
    202 	long count, maxcount;
    203 	int error = 0;
    204 
    205 	path = NULL;
    206 	error = 0;
    207 	maxcount = SCARG(uap, bufsize) / sizeof(struct ultrix_fs_data);
    208 	sfsp = SCARG(uap, buf);
    209 
    210 	if (SCARG(uap, mode) == ULTRIX_STAT_ONE ||
    211 	    SCARG(uap, mode) == ULTRIX_STAT_MANY)
    212 		mntflags = MNT_WAIT;
    213 	else
    214 		mntflags = MNT_NOWAIT;
    215 
    216 	if (SCARG(uap, mode) == ULTRIX_STAT_ONE || SCARG(uap, mode) == ULTRIX_NOSTAT_ONE) {
    217 		/*
    218 		 * Only get info on mountpoints that matches the path
    219 		 * provided.
    220 		 */
    221 		MALLOC(path, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
    222 		if (error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL))
    223 			goto bad;
    224 		maxcount = 1;
    225 	} else {
    226 		/*
    227 		 * Get info on any mountpoints, somewhat like readdir().
    228 		 * Find out how many mount list entries to skip, and skip
    229 		 * them.
    230 		 */
    231 		if (error =
    232 		    copyin((caddr_t)SCARG(uap, start), &start,
    233 					  sizeof(*SCARG(uap, start))))
    234 			goto bad;
    235 		for (skip = start, mp = mountlist.cqh_first;
    236 		    mp != (void*)&mountlist && skip-- > 0; mp = nmp)
    237 			nmp = mp->mnt_list.cqe_next;
    238 	}
    239 
    240 	for (count = 0, mp = mountlist.cqh_first;
    241 	    mp != (void*)&mountlist && count < maxcount; mp = nmp) {
    242 		nmp = mp->mnt_list.cqe_next;
    243 		if (sfsp != NULL && (mp->mnt_flag & MNT_MLOCK) == 0) {
    244 			struct ultrix_fs_data tem;
    245 			sp = &mp->mnt_stat;
    246 
    247 			/*
    248 			 * If requested, refresh the fsstat cache.
    249 			 */
    250 			if ((mntflags & MNT_WAIT) != 0 &&
    251 			    (error = VFS_STATFS(mp, sp, p)) != 0)
    252 				continue;
    253 
    254 			/*
    255 			 * XXX what does this do? -- cgd
    256 			 */
    257 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    258 			if (path == NULL ||
    259 			    strcmp(path, sp->f_mntonname) == 0) {
    260 				make_ultrix_mntent(sp, &tem);
    261 				if (error =
    262 				    copyout((caddr_t)&tem, sfsp, sizeof(tem)))
    263 					goto bad;
    264 				sfsp++;
    265 				count++;
    266 			}
    267 		}
    268 	}
    269 
    270 	if (sfsp != NULL && count > maxcount)
    271 		*retval = maxcount;
    272 	else
    273 		*retval = count;
    274 
    275 bad:
    276 	if (path)
    277 		FREE(path, M_TEMP);
    278 	return (error);
    279 }
    280 
    281 
    282 
    283 /* Old-style inet sockaddr (no len field) as passed to Ultrix mount(2) */
    284 struct osockaddr_in {
    285 	short   sin_family;
    286 	u_short sin_port;
    287 	struct  in_addr sin_addr;
    288 	char    sin_zero[8];
    289 };
    290 
    291 
    292 /*
    293  * fstype-dependent structure passed to Ultrix mount(2) when
    294  * mounting NFS filesystems
    295  */
    296 struct	ultrix_nfs_args {
    297 	struct	osockaddr_in *addr;	/* file server address */
    298 	void	*fh;			/* file handle to be mounted */
    299 	int	flags;			/* flags */
    300 	int	wsize;			/* write size in bytes */
    301 	int	rsize;			/* read size in bytes */
    302 	int	timeo;			/* initial timeout in .1 secs */
    303 	int	retrans;		/* times to retry send */
    304 	char	*hostname;		/* server's hostname */
    305 	char	*optstr;		/* string of nfs mount options*/
    306 	int	gfs_flags;		/* gnode flags (ugh) */
    307 	int	pg_thresh;		/* paging threshold ? */
    308 };
    309 
    310 
    311 /*
    312  * fstype-dependent structure passed to Ultrix mount(2) when
    313  * mounting local (4.2bsd FFS) filesystems
    314  */
    315 struct ultrix_ufs_args {
    316 	u_long ufs_flags;		/* mount flags?*/
    317 	u_long ufs_pgthresh;		/* minimum file size to page */
    318 };
    319 
    320 int
    321 ultrix_sys_mount(p, v, retval)
    322 	struct proc *p;
    323 	void *v;
    324 	int *retval;
    325 {
    326 	struct ultrix_sys_mount_args *uap = v;
    327 
    328 	int error;
    329 	int otype = SCARG(uap, type);
    330 	extern char sigcode[], esigcode[];
    331 	char fsname[MFSNAMELEN];
    332 	char * fstype;
    333 	struct sys_mount_args nuap;
    334 
    335 #define	szsigcode	(esigcode - sigcode)
    336 	caddr_t usp = (caddr_t)ALIGN(PS_STRINGS - szsigcode - STACKGAPLEN);
    337 
    338 	bzero(&nuap, sizeof(nuap));
    339 	SCARG(&nuap, flags) = 0;
    340 
    341 	/*
    342 	 * Translate Ultrix integer mount codes for UFS and NFS to
    343 	 * NetBSD fstype strings.  Other Ultrix filesystem types
    344 	 *  (msdos, DEC ods-2) are not supported.
    345 	 */
    346 	if (otype == ULTRIX_FSTYPE_ULTRIX)
    347 		fstype = "ufs";
    348 	else if (otype == ULTRIX_FSTYPE_NFS)
    349 		fstype = "nfs";
    350 	else
    351 		return (EINVAL);
    352 
    353 	/* Translate the Ultrix mount-readonly option parameter */
    354 	if (SCARG(uap, rdonly))
    355 		SCARG(&nuap, flags) |= MNT_RDONLY;
    356 
    357 	/* Copy string-ified version of mount type back out to user space */
    358 	SCARG(&nuap, type) = (char *)usp;
    359 	if (error = copyout(fstype, SCARG(&nuap, type), strlen(fstype)+1)) {
    360 		return (error);
    361 	}
    362 	usp += strlen(fstype)+1;
    363 
    364 #ifdef later
    365 	parse ultrix mount option string and set NetBSD flags
    366 #endif
    367 	SCARG(&nuap, path) = SCARG(uap, dir);
    368 
    369 	if (otype == ULTRIX_FSTYPE_ULTRIX) {
    370 		/* attempt to mount a native, rather than 4.2bsd, ffs */
    371 		struct ufs_args ua;
    372 		struct nameidata nd;
    373 
    374 		ua.fspec = SCARG(uap, special);
    375 		bzero(&ua.export, sizeof(ua.export));
    376 		SCARG(&nuap, data) = usp;
    377 
    378 		if (error = copyout(&ua, SCARG(&nuap, data), sizeof ua)) {
    379 			return(error);
    380 		}
    381 		/*
    382 		 * Ultrix mount has no MNT_UPDATE flag.
    383 		 * Attempt to see if this is the root we're mounting,
    384 		 * and if so, set MNT_UPDATE so we can mount / read-write.
    385 		 */
    386 		fsname[0] = 0;
    387 		if (error = copyinstr((caddr_t)SCARG(&nuap, path), fsname,
    388 				      sizeof fsname, (u_int*)0))
    389 			return(error);
    390 		if (strcmp(fsname, "/") == 0) {
    391 			SCARG(&nuap, flags) |= MNT_UPDATE;
    392 			printf("COMPAT_ULTRIX: mount with MNT_UPDATE on %s\n",
    393 			       fsname);
    394 		}
    395 	} else if (otype == ULTRIX_FSTYPE_NFS) {
    396 		struct ultrix_nfs_args una;
    397 		struct nfs_args na;
    398 		struct osockaddr_in osa;
    399 		struct sockaddr_in *sap = (struct sockaddr_in *)& osa;
    400 
    401 		bzero(&osa, sizeof(osa));
    402 		bzero(&una, sizeof(una));
    403 		if (error = copyin(SCARG(uap, data), &una, sizeof una)) {
    404 			return (error);
    405 		}
    406 		/*
    407 		 * This is the only syscall boundary the
    408 		 * address of the server passes, so do backwards
    409 		 * compatibility on 4.3style sockaddrs here.
    410 		 */
    411 		if (error = copyin(una.addr, &osa, sizeof osa)) {
    412 			printf("ultrix_mount: nfs copyin osa\n");
    413 			return (error);
    414 		}
    415 		sap->sin_family = (u_char)osa.sin_family;
    416 		sap->sin_len = sizeof(*sap);
    417 		/* allocate space above caller's stack for nfs_args */
    418 		SCARG(&nuap, data) = usp;
    419 		usp +=  sizeof (na);
    420 		/* allocate space above caller's stack for server sockaddr */
    421 		na.version = NFS_ARGSVERSION;
    422 		na.addr = (struct sockaddr *)usp;
    423 		usp += sizeof(*sap);
    424 		na.addrlen = sap->sin_len;
    425 		na.sotype = SOCK_DGRAM;
    426 		na.proto = IPPROTO_UDP;
    427 		na.fh = una.fh;
    428 		na.fhsize = NFSX_V2FH;
    429 		na.flags = /*una.flags;*/ NFSMNT_NOCONN | NFSMNT_RESVPORT;
    430 		na.wsize = una.wsize;
    431 		na.rsize = una.rsize;
    432 		na.timeo = una.timeo;
    433 		na.retrans = una.retrans;
    434 		na.hostname = una.hostname;
    435 		if (error = copyout(sap, na.addr, sizeof (*sap) ))
    436 			return (error);
    437 		if (error = copyout(&na, SCARG(&nuap, data), sizeof na))
    438 			return (error);
    439 	}
    440 	return (sys_mount(p, &nuap, retval));
    441 }
    442