ebb5012f9003c477c2b920163634fc0ef163f326
[openafs.git] / src / afs / SOLARIS / osi_vfsops.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * osi_vfsops.c for SOLARIS
12  */
13 #include <afsconfig.h>
14 #include "afs/param.h"
15
16
17 #include "afs/sysincludes.h"    /* Standard vendor system headers */
18 #include "afsincludes.h"        /* Afs-based standard headers */
19 #include "afs/afs_stats.h"      /* statistics stuff */
20 #include "h/modctl.h"
21 #include "h/syscall.h"
22 #if defined(AFS_SUN511_ENV)
23 #include <sys/vfs_opreg.h>
24 #endif
25 #include <sys/kobj.h>
26
27 #include <sys/mount.h>
28
29 struct vfs *afs_globalVFS = 0;
30 struct vcache *afs_globalVp = 0;
31
32 #if defined(AFS_SUN5_64BIT_ENV)
33 extern struct sysent sysent32[];
34 #endif
35
36 int afsfstype = 0;
37
38 int
39 afs_mount(struct vfs *afsp, struct vnode *amvp, struct mounta *uap,
40           afs_ucred_t *credp)
41 {
42
43     AFS_GLOCK();
44
45     AFS_STATCNT(afs_mount);
46
47 #if defined(AFS_SUN510_ENV)
48     if (secpolicy_fs_mount(credp, amvp, afsp) != 0) {
49 #else
50     if (!afs_osi_suser(credp)) {
51 #endif
52         AFS_GUNLOCK();
53         return (EPERM);
54     }
55     afsp->vfs_fstype = afsfstype;
56
57     if (afs_globalVFS) {        /* Don't allow remounts. */
58         AFS_GUNLOCK();
59         return (EBUSY);
60     }
61
62     afs_globalVFS = afsp;
63     afsp->vfs_bsize = 8192;
64     afsp->vfs_fsid.val[0] = AFS_VFSMAGIC;       /* magic */
65     afsp->vfs_fsid.val[1] = AFS_VFSFSID;
66     afsp->vfs_dev = AFS_VFSMAGIC;
67
68     AFS_GUNLOCK();
69     return 0;
70 }
71
72 static void
73 afs_freevfs(void)
74 {
75     int i;
76     struct vcache *vc, *nvc;
77     extern struct vcache *afs_vhashT[VCSIZE];
78
79     afs_globalVFS = 0;
80
81     afs_shutdown();
82 }
83
84 int
85 afs_unmount(struct vfs *afsp, int flag, afs_ucred_t *credp)
86 {
87     struct vcache *rootvp = NULL;
88
89     AFS_GLOCK();
90     AFS_STATCNT(afs_unmount);
91
92 #if defined(AFS_SUN510_ENV)
93     if (secpolicy_fs_unmount(credp, afsp) != 0) {
94 #else
95     if (!afs_osi_suser(credp)) {
96 #endif
97         AFS_GUNLOCK();
98         return (EPERM);
99     }
100
101     if (flag & MS_FORCE) {
102         AFS_GUNLOCK();
103         return ENOTSUP;
104     }
105
106     /* We should have one reference from the caller, and one reference for the
107      * root vnode; any more and someone is still referencing something */
108     if (afsp->vfs_count > 2) {
109         AFS_GUNLOCK();
110         return EBUSY;
111     }
112
113     /* The root vnode should have one ref for the mount; any more, and someone
114      * else is using the root vnode */
115     if (afs_globalVp && VREFCOUNT_GT(afs_globalVp, 1)) {
116         AFS_GUNLOCK();
117         return EBUSY;
118     }
119
120     afsp->vfs_flag |= VFS_UNMOUNTED;
121
122     if (afs_globalVp) {
123         /* release the root vnode, which should be the last reference to us
124          * besides the caller of afs_unmount */
125         rootvp = afs_globalVp;
126         afs_globalVp = NULL;
127         AFS_RELE(rootvp);
128     }
129
130     AFS_GUNLOCK();
131     return 0;
132 }
133
134 void
135 gafs_freevfs(struct vfs *afsp)
136 {
137     AFS_GLOCK();
138
139     afs_freevfs();
140
141     AFS_GUNLOCK();
142 }
143
144 int
145 afs_root(struct vfs *afsp, struct vnode **avpp)
146 {
147     afs_int32 code = 0;
148     struct vrequest treq;
149     struct vcache *tvp = 0;
150     struct vcache *gvp;
151     struct proc *proc = ttoproc(curthread);
152     struct vnode *vp = afsp->vfs_vnodecovered;
153     int locked = 0;
154
155     /* Potential deadlock:
156      * afs_root is called with the Vnode's v_lock locked. Set VVFSLOCK
157      * and drop the v_lock if we need to make an RPC to complete this
158      * request. There used to be a deadlock on the global lock until
159      * we stopped calling iget while holding the global lock.
160      */
161
162     AFS_GLOCK();
163
164     AFS_STATCNT(afs_root);
165
166 again:
167     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
168         tvp = afs_globalVp;
169     } else {
170         if (MUTEX_HELD(&vp->v_lock)) {
171             vp->v_flag |= VVFSLOCK;
172             locked = 1;
173             mutex_exit(&vp->v_lock);
174         }
175
176         if (afs_globalVp) {
177             gvp = afs_globalVp;
178             afs_globalVp = NULL;
179             afs_PutVCache(gvp);
180         }
181
182         if (!(code = afs_InitReq(&treq, proc->p_cred))
183             && !(code = afs_CheckInit())) {
184             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
185             /* we really want this to stay around */
186             if (tvp) {
187                 if (afs_globalVp) {
188                     /* someone else got there before us! */
189                     afs_PutVCache(tvp);
190                     tvp = 0;
191                     goto again;
192                 }
193                 afs_globalVp = tvp;
194             } else
195                 code = ENOENT;
196         }
197     }
198     if (tvp) {
199         AFS_FAST_HOLD(tvp);
200         mutex_enter(&AFSTOV(tvp)->v_lock);
201         AFSTOV(tvp)->v_flag |= VROOT;
202         mutex_exit(&AFSTOV(tvp)->v_lock);
203
204         afs_globalVFS = afsp;
205         *avpp = AFSTOV(tvp);
206     }
207
208     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, *avpp,
209                ICL_TYPE_INT32, code);
210
211     AFS_GUNLOCK();
212     if (locked) {
213         mutex_enter(&vp->v_lock);
214         vp->v_flag &= ~VVFSLOCK;
215         if (vp->v_flag & VVFSWAIT) {
216             vp->v_flag &= ~VVFSWAIT;
217             cv_broadcast(&vp->v_cv);
218         }
219     }
220
221     return code;
222 }
223
224 int
225 afs_statvfs(struct vfs *afsp, struct statvfs64 *abp)
226 {
227     AFS_GLOCK();
228
229     AFS_STATCNT(afs_statfs);
230
231     abp->f_frsize = 1024;
232     abp->f_favail = 9000000;
233     abp->f_bsize = afsp->vfs_bsize;
234     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
235         abp->f_ffree = 9000000;
236     abp->f_fsid = (AFS_VFSMAGIC << 16) || AFS_VFSFSID;
237
238     AFS_GUNLOCK();
239     return 0;
240 }
241
242 int
243 afs_sync(struct vfs *afsp, short flags, afs_ucred_t *credp)
244 {
245     return 0;
246 }
247
248 int
249 afs_vget(struct vfs *afsp, struct vnode **avcp, struct fid *fidp)
250 {
251     cred_t *credp = CRED();
252     struct vrequest treq;
253     int code;
254
255     AFS_GLOCK();
256
257     AFS_STATCNT(afs_vget);
258
259     *avcp = NULL;
260     if (!(code = afs_InitReq(&treq, credp))) {
261         code = afs_osi_vget((struct vcache **)avcp, fidp, &treq);
262     }
263
264     afs_Trace3(afs_iclSetp, CM_TRACE_VGET, ICL_TYPE_POINTER, *avcp,
265                ICL_TYPE_INT32, treq.uid, ICL_TYPE_FID, fidp);
266     code = afs_CheckCode(code, &treq, 42);
267
268     AFS_GUNLOCK();
269     return code;
270 }
271
272 /* This is only called by vfs_mount when afs is going to be mounted as root.
273  * Since we don't support diskless clients we shouldn't come here.
274  */
275 int afsmountroot = 0;
276 afs_mountroot(struct vfs *afsp, whymountroot_t why)
277 {
278     AFS_GLOCK();
279     AFS_STATCNT(afs_mountroot);
280     afsmountroot++;
281     AFS_GUNLOCK();
282     return EINVAL;
283 }
284
285 /* afs_swapvp is called to setup swapping over the net for diskless clients.
286  * Again not for us.
287  */
288 int afsswapvp = 0;
289 afs_swapvp(struct vfs *afsp, struct vnode **avpp, char *nm)
290 {
291     AFS_GLOCK();
292     AFS_STATCNT(afs_swapvp);
293     afsswapvp++;
294     AFS_GUNLOCK();
295     return EINVAL;
296 }
297
298
299 #if defined(AFS_SUN511_ENV)
300 /* The following list must always be NULL-terminated */
301 static const fs_operation_def_t afs_vfsops_template[] = {
302     VFSNAME_MOUNT,              { .vfs_mount = afs_mount },
303     VFSNAME_UNMOUNT,            { .vfs_unmount = afs_unmount },
304     VFSNAME_ROOT,               { .vfs_root = afs_root },
305     VFSNAME_STATVFS,            { .vfs_statvfs = afs_statvfs },
306     VFSNAME_SYNC,               { .vfs_sync = afs_sync },
307     VFSNAME_VGET,               { .vfs_vget = afs_vget },
308     VFSNAME_MOUNTROOT,          { .vfs_mountroot = afs_mountroot },
309     VFSNAME_FREEVFS,            { .vfs_freevfs = gafs_freevfs },
310     NULL,                       NULL
311 };
312 struct vfsops *afs_vfsopsp;
313 #elif defined(AFS_SUN510_ENV)
314 /* The following list must always be NULL-terminated */
315 const fs_operation_def_t afs_vfsops_template[] = {
316     VFSNAME_MOUNT,              afs_mount,
317     VFSNAME_UNMOUNT,            afs_unmount,
318     VFSNAME_ROOT,               afs_root,
319     VFSNAME_STATVFS,            afs_statvfs,
320     VFSNAME_SYNC,               afs_sync,
321     VFSNAME_VGET,               afs_vget,
322     VFSNAME_MOUNTROOT,          afs_mountroot,
323     VFSNAME_FREEVFS,            gafs_freevfs,
324     NULL,                       NULL
325 };
326 struct vfsops *afs_vfsopsp;
327 #else
328 struct vfsops Afs_vfsops = {
329     afs_mount,
330     afs_unmount,
331     afs_root,
332     afs_statvfs,
333     afs_sync,
334     afs_vget,
335     afs_mountroot,
336     afs_swapvp,
337     gafs_freevfs,
338 };
339 #endif
340
341
342 /*
343  * afsinit - intialize VFS
344  */
345 int (*ufs_iallocp) ();
346 void (*ufs_iupdatp) ();
347 int (*ufs_igetp) ();
348 void (*ufs_itimes_nolockp) ();
349
350 int (*afs_orig_ioctl) (), (*afs_orig_ioctl32) ();
351 int (*afs_orig_setgroups) (), (*afs_orig_setgroups32) ();
352
353 #ifndef AFS_SUN510_ENV
354 struct ill_s *ill_g_headp = 0;
355 #endif
356
357 int afs_sinited = 0;
358
359 extern struct fs_operation_def afs_vnodeops_template[];
360
361 #if     !defined(AFS_NONFSTRANS)
362 int (*nfs_rfsdisptab_v2) ();
363 int (*nfs_rfsdisptab_v3) ();
364 int (*nfs_acldisptab_v2) ();
365 int (*nfs_acldisptab_v3) ();
366
367 int (*nfs_checkauth) ();
368 #endif
369
370 extern Afs_syscall();
371
372 static void *
373 do_mod_lookup(const char * mod, const char * sym)
374 {
375     void * ptr;
376
377     ptr = modlookup(mod, sym);
378     if (ptr == NULL) {
379         afs_warn("modlookup failed for symbol '%s' in module '%s'\n",
380                  sym, mod);
381     }
382
383     return ptr;
384 }
385
386 #ifdef AFS_SUN510_ENV
387 afsinit(int fstype, char *dummy)
388 #else
389 afsinit(struct vfssw *vfsswp, int fstype)
390 #endif
391 {
392     extern int afs_xioctl();
393     extern int afs_xsetgroups();
394
395     AFS_STATCNT(afsinit);
396
397     afs_orig_setgroups = sysent[SYS_setgroups].sy_callc;
398     afs_orig_ioctl = sysent[SYS_ioctl].sy_call;
399     sysent[SYS_setgroups].sy_callc = afs_xsetgroups;
400     sysent[SYS_ioctl].sy_call = afs_xioctl;
401
402 #if defined(AFS_SUN5_64BIT_ENV)
403     afs_orig_setgroups32 = sysent32[SYS_setgroups].sy_callc;
404     afs_orig_ioctl32 = sysent32[SYS_ioctl].sy_call;
405     sysent32[SYS_setgroups].sy_callc = afs_xsetgroups;
406     sysent32[SYS_ioctl].sy_call = afs_xioctl;
407 #endif /* AFS_SUN5_64BIT_ENV */
408
409 #ifdef AFS_SUN510_ENV
410     vfs_setfsops(fstype, afs_vfsops_template, &afs_vfsopsp);
411     afsfstype = fstype;
412     vn_make_ops("afs", afs_vnodeops_template, &afs_ops);
413 #else /* !AFS_SUN510_ENV */
414     vfsswp->vsw_vfsops = &Afs_vfsops;
415     afsfstype = fstype;
416 #endif /* !AFS_SUN510_ENV */
417
418
419 #if !defined(AFS_NONFSTRANS)
420     nfs_rfsdisptab_v2 = (int (*)()) do_mod_lookup("nfssrv", "rfsdisptab_v2");
421     if (nfs_rfsdisptab_v2 != NULL) {
422         nfs_acldisptab_v2 = (int (*)()) do_mod_lookup("nfssrv", "acldisptab_v2");
423         if (nfs_acldisptab_v2 != NULL) {
424             afs_xlatorinit_v2(nfs_rfsdisptab_v2, nfs_acldisptab_v2);
425         }
426     }
427     nfs_rfsdisptab_v3 = (int (*)()) do_mod_lookup("nfssrv", "rfsdisptab_v3");
428     if (nfs_rfsdisptab_v3 != NULL) {
429         nfs_acldisptab_v3 = (int (*)()) do_mod_lookup("nfssrv", "acldisptab_v3");
430         if (nfs_acldisptab_v3 != NULL) {
431             afs_xlatorinit_v3(nfs_rfsdisptab_v3, nfs_acldisptab_v3);
432         }
433     }
434
435     nfs_checkauth = (int (*)()) do_mod_lookup("nfssrv", "checkauth");
436 #endif /* !AFS_NONFSTRANS */
437
438     ufs_iallocp = (int (*)()) do_mod_lookup("ufs", "ufs_ialloc");
439     ufs_iupdatp = (void (*)()) do_mod_lookup("ufs", "ufs_iupdat");
440     ufs_igetp = (int (*)()) do_mod_lookup("ufs", "ufs_iget");
441     ufs_itimes_nolockp = (void (*)()) do_mod_lookup("ufs", "ufs_itimes_nolock");
442
443     if (!ufs_iallocp || !ufs_iupdatp || !ufs_itimes_nolockp || !ufs_igetp) {
444         afs_warn("AFS to UFS mapping cannot be fully initialised\n");
445     }
446
447 #if !defined(AFS_SUN510_ENV)
448     ill_g_headp = (struct ill_s *) do_mod_lookup("ip", "ill_g_head");
449 #endif /* !AFS_SUN510_ENV */
450
451     afs_sinited = 1;
452     return 0;
453
454 }
455
456 #ifdef AFS_SUN510_ENV
457 #ifdef AFS_SUN511_ENV
458 static vfsdef_t afs_vfsdef = {
459     VFSDEF_VERSION,
460     "afs",
461     afsinit,
462     VSW_STATS,
463     NULL
464 };
465 #else
466 static struct vfsdef_v3 afs_vfsdef = {
467     VFSDEF_VERSION,
468     "afs",
469     afsinit,
470     VSW_STATS
471 };
472 #endif
473 #else
474 static struct vfssw afs_vfw = {
475     "afs",
476     afsinit,
477     &Afs_vfsops,
478     0
479 };
480 #endif
481
482 #ifndef AFS_SUN511_ENV
483 static struct sysent afssysent = {
484     6,
485     0,
486     Afs_syscall
487 };
488 #endif /* AFS_SUN511_ENV */
489
490 /* inter-module dependencies */
491 char _depends_on[] = 
492 #if AFS_SUN510_ENV
493         "drv/ip drv/udp strmod/rpcmod fs/ufs";
494 #else
495         "drv/ip drv/udp strmod/rpcmod";
496 #endif
497
498 /*
499  * Info/Structs to link the afs module into the kernel
500  */
501 extern struct mod_ops mod_fsops;
502
503 static struct modlfs afsmodlfs = {
504     &mod_fsops,
505     "afs filesystem",
506 #ifdef AFS_SUN510_ENV
507     &afs_vfsdef
508 #else
509     &afs_vfw
510 #endif
511 };
512
513 #ifdef AFS_SUN511_ENV
514
515 extern struct modldrv afs_modldrv;
516
517 #else /* AFS_SUN511_ENV */
518
519 extern struct mod_ops mod_syscallops;
520 static struct modlsys afsmodlsys = {
521     &mod_syscallops,
522     "afs syscall interface",
523     &afssysent
524 };
525
526 /** The two structures afssysent32 and afsmodlsys32 are being added
527   * for supporting 32 bit syscalls. In Solaris 7 there are two system
528   * tables viz. sysent ans sysent32. 32 bit applications use sysent32.
529   * Since most of our user space binaries are going to be 32 bit
530   * we need to attach to sysent32 also. Note that the entry into AFS
531   * land still happens through Afs_syscall irrespective of whether we
532   * land here from sysent or sysent32
533   */
534
535 # if defined(AFS_SUN5_64BIT_ENV)
536 extern struct mod_ops mod_syscallops32;
537
538 static struct modlsys afsmodlsys32 = {
539     &mod_syscallops32,
540     "afs syscall interface(32 bit)",
541     &afssysent
542 };
543 # endif
544 #endif /* !AFS_SUN511_ENV */
545
546
547 static struct modlinkage afs_modlinkage = {
548     MODREV_1,
549     (void *)&afsmodlfs,
550 #ifdef AFS_SUN511_ENV
551     (void *)&afs_modldrv,
552 #else
553     (void *)&afsmodlsys,
554 # ifdef AFS_SUN5_64BIT_ENV
555     (void *)&afsmodlsys32,
556 # endif
557 #endif /* !AFS_SUN511_ENV */
558     NULL
559 };
560
561 static void
562 reset_sysent(void)
563 {
564     if (afs_sinited) {
565         sysent[SYS_setgroups].sy_callc = afs_orig_setgroups;
566         sysent[SYS_ioctl].sy_call = afs_orig_ioctl;
567 #if defined(AFS_SUN5_64BIT_ENV)
568         sysent32[SYS_setgroups].sy_callc = afs_orig_setgroups32;
569         sysent32[SYS_ioctl].sy_call = afs_orig_ioctl32;
570 #endif
571     }
572 }
573
574 /** This is the function that modload calls when loading the afs kernel
575   * extensions. The solaris modload program searches for the _init
576   * function in a module and calls it when modloading
577   */
578
579 _init()
580 {
581     char *sysn, *mod_getsysname();
582     int code;
583     extern char *sysbind;
584     extern struct bind *sb_hashtab[];
585     struct modctl *mp = 0;
586
587     if (afs_sinited)
588         return EBUSY;
589
590     if ((!(mp = mod_find_by_filename("fs", "ufs"))
591          && !(mp = mod_find_by_filename(NULL, "/kernel/fs/ufs"))
592          && !(mp = mod_find_by_filename(NULL, "sys/ufs"))) || (mp
593                                                                && !mp->
594                                                                mod_installed))
595     {
596         printf
597             ("ufs module must be loaded before loading afs; use modload /kernel/fs/ufs\n");
598         return (ENOSYS);
599     }
600 #ifndef AFS_NONFSTRANS
601     if ((!(mp = mod_find_by_filename("misc", "nfssrv"))
602          && !(mp = mod_find_by_filename(NULL, NFSSRV))
603          && !(mp = mod_find_by_filename(NULL, NFSSRV_V9))
604          && !(mp = mod_find_by_filename(NULL, NFSSRV_AMD64))) || (mp
605                                                                && !mp->
606                                                                mod_installed))
607     {
608         printf
609             ("misc/nfssrv module must be loaded before loading afs with nfs-xlator\n");
610         return (ENOSYS);
611     }
612 #endif /* !AFS_NONFSTRANS */
613
614
615     osi_Init();                 /* initialize global lock, etc */
616
617     code = mod_install(&afs_modlinkage);
618     if (code) {
619         /* we failed to load, so make sure we don't leave behind any
620          * references to our syscall handlers */
621         reset_sysent();
622     }
623     return code;
624 }
625
626 _info(modp)
627      struct modinfo *modp;
628 {
629     int code;
630
631     code = mod_info(&afs_modlinkage, modp);
632     return code;
633 }
634
635 _fini()
636 {
637     int code;
638
639     if (afs_globalVFS)
640         return EBUSY;
641
642     reset_sysent();
643     code = mod_remove(&afs_modlinkage);
644     return code;
645 }