ultrix_fs.c revision 1.9 1 /* $NetBSD: ultrix_fs.c,v 1.9 1997/04/06 23:26:52 jonathan Exp $ */
2
3 /*
4 * Copyright (c) 1995
5 * Jonathan Stone (hereinafter referred to as the author)
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 /*
148 * Construct an Ultrix getmnt() ultrix_fs_data from the native NetBSD
149 * struct statfs.
150 */
151 static void
152 make_ultrix_mntent(sp, tem)
153 register struct statfs *sp;
154 register struct ultrix_fs_data *tem;
155 {
156
157 bzero(tem, sizeof (*tem));
158
159 tem->ufsd_flags = sp->f_flags; /* XXX translate */
160 tem->ufsd_mtsize = sp->f_bsize; /* XXX max transfer size */
161 tem->ufsd_otsize = sp->f_iosize;
162 tem->ufsd_bsize = sp->f_bsize;
163 /*
164 * Translate file system type. NetBSD/1.1 has f_type zero,
165 * and uses an fstype string instead.
166 * For now, map types not in Ultrix (kernfs, null, procfs...)
167 * to UFS, since Ultrix mout will try and call mount_unknown
168 * for ULTRIX_FSTYPE_UNKNOWN, but lacks a mount_unknown binary.
169 */
170 tem->ufsd_fstype = ULTRIX_FSTYPE_NFS;
171 if (strcmp(sp->f_fstypename, "ffs") == 0)
172 tem->ufsd_fstype = ULTRIX_FSTYPE_ULTRIX;
173
174 tem->ufsd_gtot = sp->f_files; /* total "gnodes" */
175 tem->ufsd_gfree = sp->f_ffree; /* free "gnodes" */
176 tem->ufsd_btot = sp->f_blocks; /* total 1k blocks */
177 #ifdef needsmorethought /* XXX */
178 /* tem->ufsd_bfree = sp->f_bfree; */ /* free 1k blocks */
179 /* tem->ufsd_bfree = sp->f_bavail; */ /* free 1k blocks */
180 #endif
181
182 tem->ufsd_bfreen = sp->f_bavail; /* blocks available to users */
183 tem->ufsd_pgthresh = 0; /* not relevant */
184 tem->ufsd_uid = 0; /* XXX kept where ?*/
185 tem->ufsd_dev = 0; /* ?? */
186 tem->ufsd_exroot = 0; /* ?? */
187 strncpy(tem->ufsd_path, sp->f_mntonname, ULTRIX_MAXPATHLEN);
188 strncpy(tem->ufsd_devname, sp->f_mntfromname, ULTRIX_MAXPATHLEN);
189 #if 0
190 /* In NetBSD-1.1, filesystem type is unused and always 0 */
191 printf("mntent: %s type %d\n", tem->ufsd_devname, tem->ufsd_fstype);
192 printf("mntent: %s tot %d free %d user%d\n",
193 tem->ufsd_devname, sp->f_blocks, sp->f_bfree, sp->f_bavail);
194 #endif
195 }
196
197 int
198 ultrix_sys_getmnt(p, v, retval)
199 struct proc *p;
200 void *v;
201 int *retval;
202 {
203 struct ultrix_sys_getmnt_args *uap = v;
204 struct mount *mp, *nmp;
205 struct statfs *sp;
206 struct ultrix_fs_data *sfsp;
207 char *path;
208 int mntflags;
209 int skip;
210 int start;
211 long count, maxcount;
212 int error = 0;
213
214 path = NULL;
215 error = 0;
216 maxcount = SCARG(uap, bufsize) / sizeof(struct ultrix_fs_data);
217 sfsp = SCARG(uap, buf);
218
219 if (SCARG(uap, mode) == ULTRIX_STAT_ONE ||
220 SCARG(uap, mode) == ULTRIX_STAT_MANY)
221 mntflags = MNT_WAIT;
222 else
223 mntflags = MNT_NOWAIT;
224
225 if (SCARG(uap, mode) == ULTRIX_STAT_ONE || SCARG(uap, mode) == ULTRIX_NOSTAT_ONE) {
226 /*
227 * Only get info on mountpoints that matches the path
228 * provided.
229 */
230 MALLOC(path, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
231 if ((error = copyinstr(SCARG(uap, path), path,
232 MAXPATHLEN, NULL)) != 0)
233 goto bad;
234 maxcount = 1;
235 } else {
236 /*
237 * Get info on any mountpoints, somewhat like readdir().
238 * Find out how many mount list entries to skip, and skip
239 * them.
240 */
241 if ((error = copyin((caddr_t)SCARG(uap, start), &start,
242 sizeof(*SCARG(uap, start)))) != 0)
243 goto bad;
244 for (skip = start, mp = mountlist.cqh_first;
245 mp != (void*)&mountlist && skip-- > 0; mp = nmp)
246 nmp = mp->mnt_list.cqe_next;
247 }
248
249 for (count = 0, mp = mountlist.cqh_first;
250 mp != (void*)&mountlist && count < maxcount; mp = nmp) {
251 nmp = mp->mnt_list.cqe_next;
252 if (sfsp != NULL && (mp->mnt_flag & MNT_MLOCK) == 0) {
253 struct ultrix_fs_data tem;
254 sp = &mp->mnt_stat;
255
256 /*
257 * If requested, refresh the fsstat cache.
258 */
259 if ((mntflags & MNT_WAIT) != 0 &&
260 (error = VFS_STATFS(mp, sp, p)) != 0)
261 continue;
262
263 /*
264 * XXX what does this do? -- cgd
265 */
266 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
267 if (path == NULL ||
268 strcmp(path, sp->f_mntonname) == 0) {
269 make_ultrix_mntent(sp, &tem);
270 if ((error = copyout((caddr_t)&tem, sfsp,
271 sizeof(tem))) != 0)
272 goto bad;
273 sfsp++;
274 count++;
275 }
276 }
277 }
278
279 if (sfsp != NULL && count > maxcount)
280 *retval = maxcount;
281 else
282 *retval = count;
283
284 bad:
285 if (path)
286 FREE(path, M_TEMP);
287 return (error);
288 }
289
290
291
292 /* Old-style inet sockaddr (no len field) as passed to Ultrix mount(2) */
293 struct osockaddr_in {
294 short sin_family;
295 u_short sin_port;
296 struct in_addr sin_addr;
297 char sin_zero[8];
298 };
299
300
301 /*
302 * fstype-dependent structure passed to Ultrix mount(2) when
303 * mounting NFS filesystems
304 */
305 struct ultrix_nfs_args {
306 struct osockaddr_in *addr; /* file server address */
307 void *fh; /* file handle to be mounted */
308 int flags; /* flags */
309 int wsize; /* write size in bytes */
310 int rsize; /* read size in bytes */
311 int timeo; /* initial timeout in .1 secs */
312 int retrans; /* times to retry send */
313 char *hostname; /* server's hostname */
314 char *optstr; /* string of nfs mount options*/
315 int gfs_flags; /* gnode flags (ugh) */
316 int pg_thresh; /* paging threshold ? */
317 };
318
319
320 /*
321 * fstype-dependent structure passed to Ultrix mount(2) when
322 * mounting local (4.2bsd FFS) filesystems
323 */
324 struct ultrix_ufs_args {
325 u_long ufs_flags; /* mount flags?*/
326 u_long ufs_pgthresh; /* minimum file size to page */
327 };
328
329 int
330 ultrix_sys_mount(p, v, retval)
331 struct proc *p;
332 void *v;
333 int *retval;
334 {
335 struct ultrix_sys_mount_args *uap = v;
336
337 int error;
338 int otype = SCARG(uap, type);
339 extern char sigcode[], esigcode[];
340 char fsname[MFSNAMELEN];
341 char * fstype;
342 struct sys_mount_args nuap;
343 char *native_fstype;
344
345 caddr_t usp = stackgap_init(p->p_emul);
346
347 bzero(&nuap, sizeof(nuap));
348 SCARG(&nuap, flags) = 0;
349
350 /*
351 * Translate Ultrix integer mount codes for UFS and NFS to
352 * NetBSD fstype strings. Other Ultrix filesystem types
353 * (msdos, DEC ods-2) are not supported.
354 * Copy resulting string out to userspace (on stack)
355 * so we can pass it to the native mount syscall.
356 */
357 if (otype == ULTRIX_FSTYPE_ULTRIX)
358 fstype = "ufs";
359 else if (otype == ULTRIX_FSTYPE_NFS)
360 fstype = "nfs";
361 else
362 return (EINVAL);
363
364 /* Translate the Ultrix mount-readonly option parameter */
365 if (SCARG(uap, rdonly))
366 SCARG(&nuap, flags) |= MNT_RDONLY;
367
368 /* Copy string-ified version of mount type back out to user space */
369 native_fstype = (char *)usp;
370 SCARG(&nuap, type) = native_fstype;
371 if ((error = copyout(fstype, native_fstype,
372 strlen(fstype)+1)) != 0) {
373 return (error);
374 }
375 usp += strlen(fstype)+1;
376
377 #ifdef later
378 parse ultrix mount option string and set NetBSD flags
379 #endif
380 SCARG(&nuap, path) = SCARG(uap, dir);
381
382 /*
383 * Translate fstype-dependent mount options from
384 * Ultrix format to native.
385 */
386 if (otype == ULTRIX_FSTYPE_ULTRIX) {
387 /* attempt to mount a native, rather than 4.2bsd, ffs */
388 struct ufs_args ua;
389
390 ua.fspec = SCARG(uap, special);
391 bzero(&ua.export, sizeof(ua.export));
392 SCARG(&nuap, data) = usp;
393
394 if ((error = copyout(&ua, SCARG(&nuap, data),
395 sizeof ua)) !=0) {
396 return(error);
397 }
398 /*
399 * Ultrix mount has no MNT_UPDATE flag.
400 * Attempt to see if this is the root we're mounting,
401 * and if so, set MNT_UPDATE so we can mount / read-write.
402 */
403 fsname[0] = 0;
404 if ((error = copyinstr((caddr_t)SCARG(&nuap, path), fsname,
405 sizeof fsname, (u_int*)0)) != 0)
406 return(error);
407 if (strcmp(fsname, "/") == 0) {
408 SCARG(&nuap, flags) |= MNT_UPDATE;
409 printf("COMPAT_ULTRIX: mount with MNT_UPDATE on %s\n",
410 fsname);
411 }
412 } else if (otype == ULTRIX_FSTYPE_NFS) {
413 struct ultrix_nfs_args una;
414 struct nfs_args na;
415 struct osockaddr_in osa;
416 struct sockaddr_in *sap = (struct sockaddr_in *)& osa;
417
418 bzero(&osa, sizeof(osa));
419 bzero(&una, sizeof(una));
420 if ((error = copyin(SCARG(uap, data), &una, sizeof una)) !=0) {
421 return (error);
422 }
423 /*
424 * This is the only syscall boundary the
425 * address of the server passes, so do backwards
426 * compatibility on 4.3style sockaddrs here.
427 */
428 if ((error = copyin(una.addr, &osa, sizeof osa)) != 0) {
429 printf("ultrix_mount: nfs copyin osa\n");
430 return (error);
431 }
432 sap->sin_family = (u_char)osa.sin_family;
433 sap->sin_len = sizeof(*sap);
434 /* allocate space above caller's stack for nfs_args */
435 SCARG(&nuap, data) = usp;
436 usp += sizeof (na);
437 /* allocate space above caller's stack for server sockaddr */
438 na.version = NFS_ARGSVERSION;
439 na.addr = (struct sockaddr *)usp;
440 usp += sizeof(*sap);
441 na.addrlen = sap->sin_len;
442 na.sotype = SOCK_DGRAM;
443 na.proto = IPPROTO_UDP;
444 na.fh = una.fh;
445 na.fhsize = NFSX_V2FH;
446 na.flags = /*una.flags;*/ NFSMNT_NOCONN | NFSMNT_RESVPORT;
447 na.wsize = una.wsize;
448 na.rsize = una.rsize;
449 na.timeo = una.timeo;
450 na.retrans = una.retrans;
451 na.hostname = una.hostname;
452 if ((error = copyout(sap, na.addr, sizeof (*sap) )) != 0)
453 return (error);
454 if ((error = copyout(&na, SCARG(&nuap, data), sizeof na)) != 0)
455 return (error);
456 }
457 return (sys_mount(p, &nuap, retval));
458 }
459