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