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