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