xref: /netbsd-src/sys/compat/ultrix/ultrix_fs.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: ultrix_fs.c,v 1.29 2004/04/21 07:05:07 simonb 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.29 2004/04/21 07:05:07 simonb 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(struct statvfs *, struct ultrix_fs_data *);
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(struct statvfs *sp, struct ultrix_fs_data *tem)
162 {
163 
164 	memset(tem, 0, sizeof (*tem));
165 
166 	tem->ufsd_flags = sp->f_flag;		/* XXX translate */
167 	tem->ufsd_mtsize = sp->f_bsize;		/* XXX max transfer size */
168 	tem->ufsd_otsize = sp->f_iosize;
169 	tem->ufsd_bsize = sp->f_bsize;
170 	/*
171 	 * Translate file system type. NetBSD/1.1 has f_type zero,
172 	 * and uses an fstype string instead.
173 	 * For now, map types not in Ultrix (kernfs, null, procfs...)
174 	 * to UFS, since Ultrix mout will try and call mount_unknown
175 	 * for ULTRIX_FSTYPE_UNKNOWN, but lacks a mount_unknown binary.
176 	 */
177 	tem->ufsd_fstype = ULTRIX_FSTYPE_NFS;
178 	if (strcmp(sp->f_fstypename, "ffs") == 0)
179 		tem->ufsd_fstype = ULTRIX_FSTYPE_ULTRIX;
180 
181 	tem->ufsd_gtot = sp->f_files;		/* total "gnodes" */
182 	tem->ufsd_gfree = sp->f_ffree;		/* free "gnodes" */
183 	tem->ufsd_btot = sp->f_blocks;		/* total 1k blocks */
184 #ifdef needsmorethought	/* XXX */
185 	/* tem->ufsd_bfree = sp->f_bfree; */	/* free 1k blocks */
186 	/* tem->ufsd_bfree = sp->f_bavail; */	/* free 1k blocks */
187 #endif
188 
189 	tem->ufsd_bfreen = sp->f_bavail;	/* blocks available to users */
190 	tem->ufsd_pgthresh = 0;			/* not relevant */
191 	tem->ufsd_uid = 0;			/* XXX kept where ?*/
192 	tem->ufsd_dev = 0;			/* ?? */
193 	tem->ufsd_exroot  = 0;			/* ?? */
194 	strncpy(tem->ufsd_path, sp->f_mntonname, ULTRIX_MAXPATHLEN);
195 	strncpy(tem->ufsd_devname, sp->f_mntfromname, ULTRIX_MAXPATHLEN);
196 #if 0
197 	/* In NetBSD-1.1, filesystem type is unused and always 0 */
198 	printf("mntent: %s type %d\n", tem->ufsd_devname, tem->ufsd_fstype);
199 	printf("mntent: %s tot %d free %d user%d\n",
200 	 tem->ufsd_devname, sp->f_blocks, sp->f_bfree, sp->f_bavail);
201 #endif
202 }
203 
204 int
205 ultrix_sys_getmnt(struct lwp *l, void *v, register_t *retval)
206 {
207 	struct ultrix_sys_getmnt_args *uap = v;
208 	struct proc *p = l->l_proc;
209 	struct mount *mp, *nmp;
210 	struct statvfs *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_STATVFS(mp, sp, p)) != 0)
273 				continue;
274 
275 			/*
276 			 * XXX what does this do? -- cgd
277 			 */
278 			sp->f_flag = 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(struct lwp *l, void *v, register_t *retval)
347 {
348 	struct ultrix_sys_mount_args *uap = v;
349 	struct proc *p = l->l_proc;
350 	int error;
351 	int otype = SCARG(uap, type);
352 	char fsname[MFSNAMELEN];
353 	char * fstype;
354 	struct sys_mount_args nuap;
355 	char *native_fstype;
356 
357 	caddr_t usp = stackgap_init(p, 0);
358 
359 	memset(&nuap, 0, sizeof(nuap));
360 	SCARG(&nuap, flags) = 0;
361 
362 	/*
363 	 * Translate Ultrix integer mount codes for UFS and NFS to
364 	 * NetBSD fstype strings.  Other Ultrix filesystem types
365 	 *  (msdos, DEC ods-2) are not supported.
366 	 * Copy resulting string out to userspace (on stack)
367 	 * so we can pass it to the native mount syscall.
368 	 */
369 	if (otype == ULTRIX_FSTYPE_ULTRIX)
370 		fstype = "ufs";
371 	else if (otype == ULTRIX_FSTYPE_NFS)
372 		fstype = "nfs";
373 	else
374 		return (EINVAL);
375 
376 	/* Translate the Ultrix mount-readonly option parameter */
377 	if (SCARG(uap, rdonly))
378 		SCARG(&nuap, flags) |= MNT_RDONLY;
379 
380 	/* Copy string-ified version of mount type back out to user space */
381 	native_fstype = (char *)usp;
382 	SCARG(&nuap, type) = native_fstype;
383 	if ((error = copyout(fstype, native_fstype,
384 			    strlen(fstype)+1)) != 0) {
385 		return (error);
386 	}
387 	usp += strlen(fstype)+1;
388 
389 #ifdef later
390 	parse ultrix mount option string and set NetBSD flags
391 #endif
392 	SCARG(&nuap, path) = SCARG(uap, dir);
393 
394 	/*
395 	 * Translate fstype-dependent mount options from
396 	 * Ultrix format to native.
397 	 */
398 	if (otype == ULTRIX_FSTYPE_ULTRIX) {
399 		/* attempt to mount a native, rather than 4.2bsd, ffs */
400 		struct ufs_args ua;
401 
402 		ua.fspec = SCARG(uap, special);
403 		memset(&ua.export, 0, sizeof(ua.export));
404 		SCARG(&nuap, data) = usp;
405 
406 		if ((error = copyout(&ua, SCARG(&nuap, data),
407 				     sizeof ua)) !=0) {
408 			return(error);
409 		}
410 		/*
411 		 * Ultrix mount has no MNT_UPDATE flag.
412 		 * Attempt to see if this is the root we're mounting,
413 		 * and if so, set MNT_UPDATE so we can mount / read-write.
414 		 */
415 		fsname[0] = 0;
416 		if ((error = copyinstr((caddr_t)SCARG(&nuap, path), fsname,
417 				      sizeof fsname, NULL)) != 0)
418 			return(error);
419 		if (strcmp(fsname, "/") == 0) {
420 			SCARG(&nuap, flags) |= MNT_UPDATE;
421 			printf("COMPAT_ULTRIX: mount with MNT_UPDATE on %s\n",
422 			    fsname);
423 		}
424 	} else if (otype == ULTRIX_FSTYPE_NFS) {
425 		struct ultrix_nfs_args una;
426 		struct nfs_args na;
427 		struct osockaddr_in osa;
428 		struct sockaddr_in *sap = (struct sockaddr_in *)& osa;
429 
430 		memset(&osa, 0, sizeof(osa));
431 		memset(&una, 0, sizeof(una));
432 		if ((error = copyin(SCARG(uap, data), &una, sizeof una)) !=0) {
433 			return (error);
434 		}
435 		/*
436 		 * This is the only syscall boundary the
437 		 * address of the server passes, so do backwards
438 		 * compatibility on 4.3style sockaddrs here.
439 		 */
440 		if ((error = copyin(una.addr, &osa, sizeof osa)) != 0) {
441 			printf("ultrix_mount: nfs copyin osa\n");
442 			return (error);
443 		}
444 		sap->sin_family = (u_char)osa.sin_family;
445 		sap->sin_len = sizeof(*sap);
446 		/* allocate space above caller's stack for nfs_args */
447 		SCARG(&nuap, data) = usp;
448 		usp +=  sizeof (na);
449 		/* allocate space above caller's stack for server sockaddr */
450 		na.version = NFS_ARGSVERSION;
451 		na.addr = (struct sockaddr *)usp;
452 		usp += sizeof(*sap);
453 		na.addrlen = sap->sin_len;
454 		na.sotype = SOCK_DGRAM;
455 		na.proto = IPPROTO_UDP;
456 		na.fh = una.fh;
457 		na.fhsize = NFSX_V2FH;
458 		na.flags = /*una.flags;*/ NFSMNT_NOCONN | NFSMNT_RESVPORT;
459 		na.wsize = una.wsize;
460 		na.rsize = una.rsize;
461 		na.timeo = una.timeo;
462 		na.retrans = una.retrans;
463 		na.hostname = una.hostname;
464 		if ((error = copyout(sap, na.addr, sizeof (*sap) )) != 0)
465 			return (error);
466 		if ((error = copyout(&na, SCARG(&nuap, data), sizeof na)) != 0)
467 			return (error);
468 	}
469 	return (sys_mount(l, &nuap, retval));
470 }
471