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