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