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