Home | History | Annotate | Line # | Download | only in ultrix
ultrix_fs.c revision 1.6
      1 /*	$NetBSD: ultrix_fs.c,v 1.6 1996/10/13 00:46:55 christos 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 #ifdef needsmorethought	/* XXX */
    172 	/* tem->ufsd_bfree = sp->f_bfree; */	/* free 1k blocks */
    173 	/* tem->ufsd_bfree = sp->f_bavail; */	/* free 1k blocks */
    174 #endif
    175 
    176 	tem->ufsd_bfreen = sp->f_bavail;	/* blocks available to users */
    177 	tem->ufsd_pgthresh = 0;			/* not relevant */
    178 	tem->ufsd_uid = 0;			/* XXX kept where ?*/
    179 	tem->ufsd_dev = 0;			/* ?? */
    180 	tem->ufsd_exroot  = 0;			/* ?? */
    181 	strncpy(tem->ufsd_path, sp->f_mntonname, ULTRIX_MAXPATHLEN);
    182 	strncpy(tem->ufsd_devname, sp->f_mntfromname, ULTRIX_MAXPATHLEN);
    183 #if 0
    184 	/* In NetBSD-1.1, filesystem type is unused and always 0 */
    185 	printf("mntent: %s type %d\n", tem->ufsd_devname, tem->ufsd_fstype);
    186 	printf("mntent: %s tot %d free %d user%d\n",
    187 	 tem->ufsd_devname, sp->f_blocks, sp->f_bfree, sp->f_bavail);
    188 #endif
    189 }
    190 
    191 int
    192 ultrix_sys_getmnt(p, v, retval)
    193 	struct proc *p;
    194 	void *v;
    195 	int *retval;
    196 {
    197 	struct ultrix_sys_getmnt_args *uap = v;
    198 	struct mount *mp, *nmp;
    199 	struct statfs *sp;
    200 	struct ultrix_fs_data *sfsp;
    201 	char *path;
    202 	int mntflags;
    203 	int skip;
    204 	int start;
    205 	long count, maxcount;
    206 	int error = 0;
    207 
    208 	path = NULL;
    209 	error = 0;
    210 	maxcount = SCARG(uap, bufsize) / sizeof(struct ultrix_fs_data);
    211 	sfsp = SCARG(uap, buf);
    212 
    213 	if (SCARG(uap, mode) == ULTRIX_STAT_ONE ||
    214 	    SCARG(uap, mode) == ULTRIX_STAT_MANY)
    215 		mntflags = MNT_WAIT;
    216 	else
    217 		mntflags = MNT_NOWAIT;
    218 
    219 	if (SCARG(uap, mode) == ULTRIX_STAT_ONE || SCARG(uap, mode) == ULTRIX_NOSTAT_ONE) {
    220 		/*
    221 		 * Only get info on mountpoints that matches the path
    222 		 * provided.
    223 		 */
    224 		MALLOC(path, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
    225 		if ((error = copyinstr(SCARG(uap, path), path,
    226 				       MAXPATHLEN, NULL)) != 0)
    227 			goto bad;
    228 		maxcount = 1;
    229 	} else {
    230 		/*
    231 		 * Get info on any mountpoints, somewhat like readdir().
    232 		 * Find out how many mount list entries to skip, and skip
    233 		 * them.
    234 		 */
    235 		if ((error = copyin((caddr_t)SCARG(uap, start), &start,
    236 				    sizeof(*SCARG(uap, start))))  != 0)
    237 			goto bad;
    238 		for (skip = start, mp = mountlist.cqh_first;
    239 		    mp != (void*)&mountlist && skip-- > 0; mp = nmp)
    240 			nmp = mp->mnt_list.cqe_next;
    241 	}
    242 
    243 	for (count = 0, mp = mountlist.cqh_first;
    244 	    mp != (void*)&mountlist && count < maxcount; mp = nmp) {
    245 		nmp = mp->mnt_list.cqe_next;
    246 		if (sfsp != NULL && (mp->mnt_flag & MNT_MLOCK) == 0) {
    247 			struct ultrix_fs_data tem;
    248 			sp = &mp->mnt_stat;
    249 
    250 			/*
    251 			 * If requested, refresh the fsstat cache.
    252 			 */
    253 			if ((mntflags & MNT_WAIT) != 0 &&
    254 			    (error = VFS_STATFS(mp, sp, p)) != 0)
    255 				continue;
    256 
    257 			/*
    258 			 * XXX what does this do? -- cgd
    259 			 */
    260 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    261 			if (path == NULL ||
    262 			    strcmp(path, sp->f_mntonname) == 0) {
    263 				make_ultrix_mntent(sp, &tem);
    264 				if ((error = copyout((caddr_t)&tem, sfsp,
    265 						     sizeof(tem))) != 0)
    266 					goto bad;
    267 				sfsp++;
    268 				count++;
    269 			}
    270 		}
    271 	}
    272 
    273 	if (sfsp != NULL && count > maxcount)
    274 		*retval = maxcount;
    275 	else
    276 		*retval = count;
    277 
    278 bad:
    279 	if (path)
    280 		FREE(path, M_TEMP);
    281 	return (error);
    282 }
    283 
    284 
    285 
    286 /* Old-style inet sockaddr (no len field) as passed to Ultrix mount(2) */
    287 struct osockaddr_in {
    288 	short   sin_family;
    289 	u_short sin_port;
    290 	struct  in_addr sin_addr;
    291 	char    sin_zero[8];
    292 };
    293 
    294 
    295 /*
    296  * fstype-dependent structure passed to Ultrix mount(2) when
    297  * mounting NFS filesystems
    298  */
    299 struct	ultrix_nfs_args {
    300 	struct	osockaddr_in *addr;	/* file server address */
    301 	void	*fh;			/* file handle to be mounted */
    302 	int	flags;			/* flags */
    303 	int	wsize;			/* write size in bytes */
    304 	int	rsize;			/* read size in bytes */
    305 	int	timeo;			/* initial timeout in .1 secs */
    306 	int	retrans;		/* times to retry send */
    307 	char	*hostname;		/* server's hostname */
    308 	char	*optstr;		/* string of nfs mount options*/
    309 	int	gfs_flags;		/* gnode flags (ugh) */
    310 	int	pg_thresh;		/* paging threshold ? */
    311 };
    312 
    313 
    314 /*
    315  * fstype-dependent structure passed to Ultrix mount(2) when
    316  * mounting local (4.2bsd FFS) filesystems
    317  */
    318 struct ultrix_ufs_args {
    319 	u_long ufs_flags;		/* mount flags?*/
    320 	u_long ufs_pgthresh;		/* minimum file size to page */
    321 };
    322 
    323 int
    324 ultrix_sys_mount(p, v, retval)
    325 	struct proc *p;
    326 	void *v;
    327 	int *retval;
    328 {
    329 	struct ultrix_sys_mount_args *uap = v;
    330 
    331 	int error;
    332 	int otype = SCARG(uap, type);
    333 	extern char sigcode[], esigcode[];
    334 	char fsname[MFSNAMELEN];
    335 	char * fstype;
    336 	struct sys_mount_args nuap;
    337 
    338 #define	szsigcode	(esigcode - sigcode)
    339 	caddr_t usp = (caddr_t)ALIGN(PS_STRINGS - szsigcode - STACKGAPLEN);
    340 
    341 	bzero(&nuap, sizeof(nuap));
    342 	SCARG(&nuap, flags) = 0;
    343 
    344 	/*
    345 	 * Translate Ultrix integer mount codes for UFS and NFS to
    346 	 * NetBSD fstype strings.  Other Ultrix filesystem types
    347 	 *  (msdos, DEC ods-2) are not supported.
    348 	 */
    349 	if (otype == ULTRIX_FSTYPE_ULTRIX)
    350 		fstype = "ufs";
    351 	else if (otype == ULTRIX_FSTYPE_NFS)
    352 		fstype = "nfs";
    353 	else
    354 		return (EINVAL);
    355 
    356 	/* Translate the Ultrix mount-readonly option parameter */
    357 	if (SCARG(uap, rdonly))
    358 		SCARG(&nuap, flags) |= MNT_RDONLY;
    359 
    360 	/* Copy string-ified version of mount type back out to user space */
    361 	SCARG(&nuap, type) = (char *)usp;
    362 	if ((error = copyout(fstype, SCARG(&nuap, type),
    363 			    strlen(fstype)+1)) != 0) {
    364 		return (error);
    365 	}
    366 	usp += strlen(fstype)+1;
    367 
    368 #ifdef later
    369 	parse ultrix mount option string and set NetBSD flags
    370 #endif
    371 	SCARG(&nuap, path) = SCARG(uap, dir);
    372 
    373 	if (otype == ULTRIX_FSTYPE_ULTRIX) {
    374 		/* attempt to mount a native, rather than 4.2bsd, ffs */
    375 		struct ufs_args ua;
    376 
    377 		ua.fspec = SCARG(uap, special);
    378 		bzero(&ua.export, sizeof(ua.export));
    379 		SCARG(&nuap, data) = usp;
    380 
    381 		if ((error = copyout(&ua, SCARG(&nuap, data),
    382 				     sizeof ua)) !=0) {
    383 			return(error);
    384 		}
    385 		/*
    386 		 * Ultrix mount has no MNT_UPDATE flag.
    387 		 * Attempt to see if this is the root we're mounting,
    388 		 * and if so, set MNT_UPDATE so we can mount / read-write.
    389 		 */
    390 		fsname[0] = 0;
    391 		if ((error = copyinstr((caddr_t)SCARG(&nuap, path), fsname,
    392 				      sizeof fsname, (u_int*)0)) != 0)
    393 			return(error);
    394 		if (strcmp(fsname, "/") == 0) {
    395 			SCARG(&nuap, flags) |= MNT_UPDATE;
    396 			printf("COMPAT_ULTRIX: mount with MNT_UPDATE on %s\n",
    397 			    fsname);
    398 		}
    399 	} else if (otype == ULTRIX_FSTYPE_NFS) {
    400 		struct ultrix_nfs_args una;
    401 		struct nfs_args na;
    402 		struct osockaddr_in osa;
    403 		struct sockaddr_in *sap = (struct sockaddr_in *)& osa;
    404 
    405 		bzero(&osa, sizeof(osa));
    406 		bzero(&una, sizeof(una));
    407 		if ((error = copyin(SCARG(uap, data), &una, sizeof una)) !=0) {
    408 			return (error);
    409 		}
    410 		/*
    411 		 * This is the only syscall boundary the
    412 		 * address of the server passes, so do backwards
    413 		 * compatibility on 4.3style sockaddrs here.
    414 		 */
    415 		if ((error = copyin(una.addr, &osa, sizeof osa)) != 0) {
    416 			printf("ultrix_mount: nfs copyin osa\n");
    417 			return (error);
    418 		}
    419 		sap->sin_family = (u_char)osa.sin_family;
    420 		sap->sin_len = sizeof(*sap);
    421 		/* allocate space above caller's stack for nfs_args */
    422 		SCARG(&nuap, data) = usp;
    423 		usp +=  sizeof (na);
    424 		/* allocate space above caller's stack for server sockaddr */
    425 		na.version = NFS_ARGSVERSION;
    426 		na.addr = (struct sockaddr *)usp;
    427 		usp += sizeof(*sap);
    428 		na.addrlen = sap->sin_len;
    429 		na.sotype = SOCK_DGRAM;
    430 		na.proto = IPPROTO_UDP;
    431 		na.fh = una.fh;
    432 		na.fhsize = NFSX_V2FH;
    433 		na.flags = /*una.flags;*/ NFSMNT_NOCONN | NFSMNT_RESVPORT;
    434 		na.wsize = una.wsize;
    435 		na.rsize = una.rsize;
    436 		na.timeo = una.timeo;
    437 		na.retrans = una.retrans;
    438 		na.hostname = una.hostname;
    439 		if ((error = copyout(sap, na.addr, sizeof (*sap) )) != 0)
    440 			return (error);
    441 		if ((error = copyout(&na, SCARG(&nuap, data), sizeof na)) != 0)
    442 			return (error);
    443 	}
    444 	return (sys_mount(p, &nuap, retval));
    445 }
    446