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