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