winnt-make-mountingother-than-as-afs-work-20021104
[openafs.git] / src / afs / OBSD / osi_vfsops.c
1 /*
2 Copyright 1995 Massachusetts Institute of Technology.  All Rights
3 Reserved.
4
5 You are hereby granted a worldwide, irrevocable, paid-up, right and
6 license to use, execute, display, modify, copy and distribute MIT's
7 Modifications, provided that (i) you abide by the terms and conditions
8 of your Transarc AFS License Agreement, and (ii) you do not use the name
9 of MIT in any advertising or publicity without the prior written consent
10 of MIT.  MIT disclaims all liability for your use of MIT's
11 Modifications.  MIT's Modifications are provided "AS IS" WITHOUT
12 WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,
13 ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
14 NONINFRINGEMENT.
15 */
16
17 /*
18  * OpenBSD specific assistance routines & VFS ops
19  * Original NetBSD version for Transarc afs by John Kohl <jtk@MIT.EDU>
20  * OpenBSD version by Jim Rees <rees@umich.edu>
21  *
22  * $Id$
23  */
24
25 /*
26  * Some code cribbed from ffs_vfsops and other NetBSD sources, which
27  * are marked:
28  */
29 /*
30  * Copyright (c) 1989, 1991, 1993, 1994
31  *      The Regents of the University of California.  All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. All advertising materials mentioning features or use of this software
42  *    must display the following acknowledgement:
43  *      This product includes software developed by the University of
44  *      California, Berkeley and its contributors.
45  * 4. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  */
62
63 #include <afsconfig.h>
64 #include "afs/param.h"
65
66 RCSID("$Header$");
67
68 #include "afs/sysincludes.h"    /* Standard vendor system headers */
69 #include "afs/afsincludes.h"    /* Afs-based standard headers */
70 #include "afs/afs_stats.h" /* statistics */
71
72 #include <sys/conf.h>
73 #include <sys/exec.h>
74 #include <sys/lkm.h>
75 #include <sys/namei.h>
76 #include <sys/syscall.h>
77 #include <sys/syscallargs.h>
78
79 #define NBSD_DONTFOLLOW_LINK 0
80 #define NBSD_FOLLOW_LINK 1
81 static int lkmid = -1;
82 static int afs_badcall(struct proc *p, void *xx, register_t *yy);
83
84 char afs_NetBSD_osname[] = "OpenBSD";
85 struct osi_vfs *afs_globalVFS;
86 struct vcache *afs_globalVp;
87
88 int afs_quotactl();
89 int afs_fhtovp();
90 int afs_vptofh();
91 int afsinit();
92 int afs_start();
93 int afs_mount();
94 int afs_unmount();
95 int afs_root();
96 int afs_statfs();
97 int afs_sync();
98 int afs_vget();
99 int afs_sysctl();
100 int afs_checkexp();
101
102 struct vfsops afs_vfsops = {
103     afs_mount,
104     afs_start,
105     afs_unmount,
106     afs_root,
107     afs_quotactl,
108     afs_statfs,
109     afs_sync,
110     afs_vget,
111     afs_fhtovp,
112     afs_vptofh,
113     afsinit,
114     afs_sysctl,
115     afs_checkexp,
116 };
117
118 int
119 afs_nbsd_lookupname(char *fnamep,
120                     enum uio_seg segflg,
121                     int followlink,
122                     struct vnode **dirvpp,
123                     struct vnode **compvpp)
124 {
125     struct nameidata nd;
126     int niflag;
127     int error;
128
129     /*
130      * Lookup pathname "fnamep", returning parent directory in
131      * *dirvpp (if non-null) and leaf in *compvpp.  segflg says whether the
132      * pathname is user or system space.
133      */
134     /* XXX LOCKLEAF ? */
135     niflag =  (followlink == NBSD_FOLLOW_LINK) ? FOLLOW : NOFOLLOW;
136     if (dirvpp)
137         niflag |= WANTPARENT;           /* XXX LOCKPARENT? */
138     NDINIT(&nd, LOOKUP,
139            niflag,
140            segflg,
141            fnamep, osi_curproc());
142     if ((error = namei(&nd)))
143         return error;
144     *compvpp = nd.ni_vp;
145     if (dirvpp)
146         *dirvpp = nd.ni_dvp;
147     return error;
148 }
149
150 #if 0
151 int
152 afs_rdwr(struct vnode *vp, struct uio *uiop, enum uio_rw op,
153               int flags, struct AFS_UCRED *cred)
154 {
155     uiop->uio_rw = op;
156     if (op == UIO_READ)
157         return VOP_READ(vp, uiop, flags, cred);
158     if (op == UIO_WRITE)
159         return VOP_WRITE(vp, uiop, flags, cred);
160 #ifdef DIAGNOSTIC
161     panic("afs_rdwr mode");
162 #endif
163     return EINVAL;
164 }
165 #endif
166
167 int
168 afs_quotactl()
169 {
170     return EOPNOTSUPP;
171 }
172
173 int
174 afs_sysctl()
175 {
176     return EOPNOTSUPP;
177 }
178
179 int
180 afs_checkexp()
181 {
182         return EOPNOTSUPP;
183 }
184
185 int
186 afs_fhtovp(mp, fhp, vpp)
187 struct mount *mp;
188 struct fid *fhp;
189 struct vnode **vpp;
190 {
191
192     return (EINVAL);
193 }
194
195 int
196 afs_vptofh(vp, fhp)
197 struct vnode *vp;
198 struct fid *fhp;
199 {
200
201     return (EINVAL);
202 }
203
204 int
205 afs_start(mp, flags, p)
206 struct mount *mp;
207 int flags;
208 struct proc *p;
209 {
210     return (0);                         /* nothing to do. ? */
211 }
212
213 int
214 afs_mount(mp, path, data, ndp, p)
215 register struct mount *mp;
216 char *path;
217 caddr_t data;
218 struct nameidata *ndp;
219 struct proc *p;
220 {
221     /* ndp contains the mounted-from device.  Just ignore it.
222        we also don't care about our proc struct. */
223     int size;
224
225     if (mp->mnt_flag & MNT_UPDATE)
226         return EINVAL;
227
228     if (afs_globalVFS) {
229         /* Don't allow remounts */
230         return EBUSY;
231     }
232
233     AFS_GLOCK();
234     AFS_STATCNT(afs_mount);
235
236 #ifdef DISCONN
237     /* initialize the vcache entries before we start using them */
238
239     /* XXX find a better place for this if possible  */
240     init_vcache_entries();
241 #endif
242     afs_globalVFS = mp;
243     mp->osi_vfs_bsize = 8192;
244     mp->osi_vfs_fsid.val[0] = AFS_VFSMAGIC; /* magic */
245     mp->osi_vfs_fsid.val[1] = (int) AFS_VFSFSID; 
246
247     AFS_GUNLOCK();
248
249     (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN-1, &size);
250     bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
251     bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
252     strcpy(mp->mnt_stat.f_mntfromname, "AFS");
253     /* null terminated string "AFS" will fit, just leave it be. */
254     strcpy(mp->mnt_stat.f_fstypename, MOUNT_AFS);
255     (void) afs_statfs(mp, &mp->mnt_stat);
256
257     return 0;
258 }
259
260 int
261 afs_unmount(afsp, flags, p)
262 struct mount *afsp;
263 int flags;
264 struct proc *p;
265 {
266     int err;
267     extern int sys_ioctl(), sys_setgroups();
268
269     err = afs_unmount(afsp, flags);
270     if (err == 0) {
271         /* give up syscall entries for ioctl & setgroups, which we've stolen */
272         sysent[SYS_ioctl].sy_call = sys_ioctl;
273         sysent[SYS_setgroups].sy_call = sys_setgroups;
274         /* give up the stolen syscall entry */
275         sysent[AFS_SYSCALL].sy_narg = 0;
276         sysent[AFS_SYSCALL].sy_argsize = 0;
277         sysent[AFS_SYSCALL].sy_call = afs_badcall;
278         printf("AFS unmounted--use `/sbin/modunload -i %d' to unload before restarting AFS\n", lkmid);
279     }
280     return err;
281 }
282
283 static int
284 afs_badcall(struct proc *p, void *xx, register_t *yy)
285 {
286     return ENOSYS;
287 }
288
289 void
290 afs_nbsd_getnewvnode(struct vcache *tvc)
291 {
292     while (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &tvc->v)) {
293         /* no vnodes available, force an alloc (limits be damned)! */
294         desiredvnodes++;
295     }
296     tvc->v->v_data = (void *)tvc;
297 }
298
299 int
300 afs_root(struct mount *mp,
301               struct vnode **vpp)
302 {
303     int error;
304     error = afs_root(mp, vpp);
305     if (!error)
306         vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, curproc);                /* return it locked */
307     return error;
308 }
309
310 int
311 afs_statfs(struct osi_vfs *afsp, struct statfs *abp)
312 {
313     AFS_STATCNT(afs_statfs);
314 #ifdef  AFS_GLOBAL_SUNLOCK
315     mutex_enter(&afs_global_lock);
316 #endif
317     abp->f_bsize = afsp->osi_vfs_bsize;
318     /* Fake a high number below to satisfy programs that use the ustat (for AIX), or statfs (for the rest) call to make sure that there's enough space in the device partition before storing something there (like ed(1)) */
319     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files = abp->f_ffree  = 9000000; /* XXX */
320     abp->f_fsid.val[0] = AFS_VFSMAGIC; /* magic */
321     abp->f_fsid.val[1] = (int) AFS_VFSFSID;
322 #ifdef  AFS_GLOBAL_SUNLOCK
323     mutex_exit(&afs_global_lock);
324 #endif
325     return 0;
326 }
327
328 int
329 afs_sync(struct osi_vfs *afsp)
330 {
331     AFS_STATCNT(afs_sync);
332 #if defined(DISCONN) && !defined(AFS_OBSD_ENV)
333     /* Can't do this in OpenBSD 2.7, it faults when called from apm_suspend() */
334     store_dirty_vcaches();
335 #endif
336     return 0;
337 }
338
339 void
340 afs_nbsd_ref(struct vnode *vp)
341 {
342     if (vp->v_usecount == 0) {
343         vprint("holding unheld node", vp);
344         panic("afs_ref");
345     }
346     VREF(vp);
347 }
348
349 void
350 afs_nbsd_rele(struct vnode *vp)
351 {
352     if (vp->v_usecount <= 0) {
353         vprint("rele'ing unheld node", vp);
354         panic("afs_rele");
355     }
356     vrele(vp);
357 }
358
359 int
360 afs_vget(vp, lfl)
361 struct vnode *vp;
362 int lfl;
363 {
364     int error;
365
366     if (vp->v_usecount < 0) {
367         vprint("bad usecount", vp);
368         panic("afs_vget");
369     }
370     error = vget(vp, lfl, curproc);
371     if (!error)
372         insmntque(vp, afs_globalVFS);   /* take off free list */
373     return error;
374 }
375
376 extern struct vfsops afs_vfsops;
377 extern struct vnodeopv_desc afs_vnodeop_opv_desc;
378
379 static struct vfsconf afs_vfsconf = {
380     &afs_vfsops,
381     "afs",
382     0,
383     0,
384     0,
385     NULL,
386     NULL,
387 };
388
389 MOD_VFS("afs", 0, &afs_vfsconf);
390
391 static char afsgenmem[] = "afsgenmem";
392 static char afsfidmem[] = "afsfidmem";
393 static char afsbhdrmem[] = "afsbhdrmem";
394 static char afsbfrmem[] = "afsbfrmem";
395
396 int
397 afsinit()
398 {
399     extern int afs3_syscall(), afs_xioctl(), Afs_xsetgroups(), afs_xflock();
400
401     sysent[AFS_SYSCALL].sy_call = afs3_syscall;
402     sysent[AFS_SYSCALL].sy_narg = 6;
403     sysent[AFS_SYSCALL].sy_argsize = 6*sizeof(long);
404     sysent[54].sy_call = afs_xioctl;
405     sysent[80].sy_call = Afs_xsetgroups;
406
407     return 0;
408 }
409
410 int
411 afs_vfs_load(struct lkm_table *lkmtp,
412              int cmd)
413 {
414     extern char *memname[];
415
416     vfs_opv_init_explicit(&afs_vnodeop_opv_desc);
417     vfs_opv_init_default(&afs_vnodeop_opv_desc);
418     if (memname[M_AFSGENERIC] == NULL)
419         memname[M_AFSGENERIC] = afsgenmem;
420     if (memname[M_AFSFID] == NULL)
421         memname[M_AFSFID] = afsfidmem;
422     if (memname[M_AFSBUFHDR] == NULL)
423         memname[M_AFSBUFHDR] = afsbhdrmem;
424     if (memname[M_AFSBUFFER] == NULL)
425         memname[M_AFSBUFFER] = afsbfrmem;
426     lkmid = lkmtp->id;
427     printf("OpenAFS ($Revision$) lkm loaded\n");
428     return 0;
429 }
430
431 int
432 afs_vfs_unload(struct lkm_table *lktmp,
433              int cmd)
434 {
435     extern char *memname[];
436     extern int sys_lkmnosys();
437
438     if (afs_globalVp)
439         return EBUSY;
440     if (sysent[SYS_ioctl].sy_call != sys_ioctl)
441         return EBUSY;
442
443     if (memname[M_AFSGENERIC] == afsgenmem)
444         memname[M_AFSGENERIC] = NULL;
445     if (memname[M_AFSFID] == afsfidmem)
446         memname[M_AFSFID] = NULL;
447     if (memname[M_AFSBUFHDR] == afsbhdrmem)
448         memname[M_AFSBUFHDR] = NULL;
449     if (memname[M_AFSBUFFER] == afsbfrmem)
450         memname[M_AFSBUFFER] = NULL;
451
452     sysent[AFS_SYSCALL].sy_call = sys_lkmnosys;
453     printf("OpenAFS unloaded\n");
454     return 0;
455 }
456
457
458
459 int
460 afsmodload(struct lkm_table *lkmtp,
461            int cmd,
462            int ver)
463 {
464     extern int sys_lkmnosys();
465
466     if (cmd == LKM_E_LOAD) {
467         if (strcmp(ostype,afs_NetBSD_osname)) {
468             printf("This is %s version %s\n", ostype, osrelease);
469             printf("This version of AFS is only for %s\n",
470                    afs_NetBSD_osname);
471 /*          return EPROGMISMATCH;*/
472         }
473         if (sysent[AFS_SYSCALL].sy_call != sys_lkmnosys) {
474             printf("AFS must be loaded with syscall %d assigned to sys_lkmnosys\nIs AFS already loaded?\n",
475                    AFS_SYSCALL);
476             return EINVAL;
477         }
478     }
479     DISPATCH(lkmtp,cmd,ver,afs_vfs_load,afs_vfs_unload,lkm_nofunc);
480 }