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