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