solaris8-dont-read-binding-file-20030513
[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 RCSID("$Header$");
17
18 #include "afs/sysincludes.h"    /* Standard vendor system headers */
19 #include "afsincludes.h"        /* Afs-based standard headers */
20 #include "afs/afs_stats.h"   /* statistics stuff */
21 #include "h/modctl.h"
22 #include "h/syscall.h"
23 #include <sys/kobj.h>
24
25
26
27 struct vfs *afs_globalVFS = 0;
28 struct vcache *afs_globalVp = 0;
29
30 #if defined(AFS_SUN57_64BIT_ENV)
31 extern struct sysent sysent32[];
32 #endif
33
34 int afsfstype = 0;
35
36 int afs_mount(struct vfs *afsp, struct vnode *amvp, struct mounta *uap,
37           struct AFS_UCRED *credp)
38 {
39
40     AFS_GLOCK();
41
42     AFS_STATCNT(afs_mount);
43
44     if (!suser(credp)) {
45         AFS_GUNLOCK();
46         return EPERM;
47     }
48     afsp->vfs_fstype = afsfstype;
49
50     if (afs_globalVFS) { /* Don't allow remounts. */
51         AFS_GUNLOCK();
52         return EBUSY;
53     }
54
55     afs_globalVFS = afsp;
56     afsp->vfs_bsize = 8192;
57     afsp->vfs_fsid.val[0] = AFS_VFSMAGIC; /* magic */
58     afsp->vfs_fsid.val[1] = AFS_VFSFSID; 
59     afsp->vfs_dev = AFS_VFSMAGIC;
60
61     AFS_GUNLOCK();
62     return 0;
63 }
64
65 #if defined(AFS_SUN58_ENV)
66 int afs_unmount (struct vfs *afsp, int flag, struct AFS_UCRED *credp)
67 #else
68 int afs_unmount (struct vfs *afsp, struct AFS_UCRED *credp)
69 #endif
70 {
71     AFS_GLOCK();
72     AFS_STATCNT(afs_unmount);
73
74     if (!suser(credp)) {
75         AFS_GUNLOCK();
76         return EPERM;
77     }
78     afs_globalVFS = 0;
79     afs_shutdown();
80
81     AFS_GUNLOCK();
82     return 0;
83 }
84
85 int afs_root (struct vfs *afsp, struct vnode **avpp)
86 {
87     register afs_int32 code = 0;
88     struct vrequest treq;
89     register struct vcache *tvp=0;
90     struct proc *proc = ttoproc(curthread);
91     struct vnode *vp = afsp->vfs_vnodecovered;
92     int locked = 0;
93
94     /* Potential deadlock:
95      * afs_root is called with the Vnode's v_lock locked. Set VVFSLOCK
96      * and drop the v_lock if we need to make an RPC to complete this
97      * request. There used to be a deadlock on the global lock until
98      * we stopped calling iget while holding the global lock.
99      */
100
101     AFS_GLOCK();
102
103     AFS_STATCNT(afs_root);
104
105     if (afs_globalVp && (afs_globalVp->states & CStatd)) {
106         tvp = afs_globalVp;
107     } else {
108         if (MUTEX_HELD(&vp->v_lock)) {
109             vp->v_flag |= VVFSLOCK;
110             locked = 1;
111             mutex_exit(&vp->v_lock);
112         }
113
114         if (afs_globalVp) {
115             afs_PutVCache(afs_globalVp);
116             afs_globalVp = NULL;
117         }
118
119         if (!(code = afs_InitReq(&treq, proc->p_cred)) &&
120             !(code = afs_CheckInit())) {
121             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
122             /* we really want this to stay around */
123             if (tvp) {
124                 afs_globalVp = tvp;
125             } else
126                 code = ENOENT;
127         }
128     }
129     if (tvp) {
130         VN_HOLD(AFSTOV(tvp));
131         mutex_enter(&AFSTOV(tvp)->v_lock);
132         AFSTOV(tvp)->v_flag |= VROOT;
133         mutex_exit(&AFSTOV(tvp)->v_lock);
134
135         afs_globalVFS = afsp;
136         *avpp = AFSTOV(tvp);
137     }
138
139     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, *avpp,
140                ICL_TYPE_INT32, code);
141
142     AFS_GUNLOCK();
143     if (locked) {
144         mutex_enter(&vp->v_lock);
145         vp->v_flag &= ~VVFSLOCK;
146         if (vp->v_flag & VVFSWAIT) {
147             vp->v_flag &= ~VVFSWAIT;
148             cv_broadcast(&vp->v_cv);
149         }
150     }
151
152     return code;
153 }
154
155 #ifdef AFS_SUN56_ENV
156 int afs_statvfs(struct vfs *afsp, struct statvfs64 *abp)
157 #else
158 int afs_statvfs(struct vfs *afsp, struct statvfs *abp)
159 #endif
160 {
161     AFS_GLOCK();
162
163     AFS_STATCNT(afs_statfs);
164
165     abp->f_frsize = 1024;
166     abp->f_favail =  9000000;
167     abp->f_bsize = afsp->vfs_bsize;
168     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
169         abp->f_ffree  = 9000000;
170     abp->f_fsid = (AFS_VFSMAGIC << 16) || AFS_VFSFSID;
171
172     AFS_GUNLOCK();
173     return 0;
174 }
175
176 int afs_sync(struct vfs *afsp, short flags, struct AFS_UCRED *credp)
177 {
178     return 0;
179 }
180
181 int afs_vget(struct vfs *afsp, struct vnode **avcp, struct fid *fidp)
182 {
183     cred_t *credp = CRED();
184     struct vrequest treq;
185     int code;
186
187     AFS_GLOCK();
188
189     AFS_STATCNT(afs_vget);
190
191     *avcp = NULL;
192     if (!(code = afs_InitReq(&treq, credp))) {
193         code = afs_osi_vget((struct vcache**)avcp, fidp, &treq);
194     }
195
196     afs_Trace3(afs_iclSetp, CM_TRACE_VGET, ICL_TYPE_POINTER, *avcp,
197                ICL_TYPE_INT32, treq.uid, ICL_TYPE_FID, fidp);
198     code = afs_CheckCode(code, &treq, 42);
199
200     AFS_GUNLOCK();
201     return code;
202 }
203
204 /* This is only called by vfs_mount when afs is going to be mounted as root.
205  * Since we don't support diskless clients we shouldn't come here.
206  */
207 int afsmountroot=0;
208 afs_mountroot(struct vfs *afsp, whymountroot_t why)
209 {
210     AFS_GLOCK();
211     AFS_STATCNT(afs_mountroot);
212     afsmountroot++;
213     AFS_GUNLOCK();
214     return EINVAL;
215 }
216
217 /* afs_swapvp is called to setup swapping over the net for diskless clients.
218  * Again not for us.
219  */
220 int afsswapvp=0;
221 afs_swapvp(struct vfs *afsp, struct vnode **avpp, char *nm)
222 {
223     AFS_GLOCK();
224     AFS_STATCNT(afs_swapvp);
225     afsswapvp++;
226     AFS_GUNLOCK();
227     return EINVAL;
228 }
229
230
231 struct vfsops Afs_vfsops = {
232     afs_mount,
233     afs_unmount,
234     afs_root,
235     afs_statvfs,
236     afs_sync,
237     afs_vget,
238     afs_mountroot,
239     afs_swapvp,
240 #if defined(AFS_SUN58_ENV)
241     fs_freevfs,
242 #endif
243 };
244
245
246 /*
247  * afsinit - intialize VFS
248  */
249 int (*ufs_iallocp)();
250 void (*ufs_iupdatp)();
251 int (*ufs_igetp)();
252 void (*ufs_itimes_nolockp)();
253
254 int (*afs_orig_ioctl)(), (*afs_orig_ioctl32)();
255 int (*afs_orig_setgroups)(), (*afs_orig_setgroups32)();
256
257 struct streamtab *udp_infop = 0;
258 struct ill_s *ill_g_headp = 0;
259
260 int afs_sinited = 0;
261
262 #if     !defined(AFS_NONFSTRANS)
263 int (*nfs_rfsdisptab_v2)();
264 int (*nfs_rfsdisptab_v3)();
265 int (*nfs_acldisptab_v2)();
266 int (*nfs_acldisptab_v3)();
267
268 int (*nfs_checkauth)();
269 #endif
270
271 extern Afs_syscall();
272
273 afsinit(struct vfssw *vfsswp, int fstype)
274 {
275     extern int afs_xioctl();
276     extern int afs_xsetgroups();
277
278     AFS_STATCNT(afsinit);
279
280     afs_orig_setgroups = sysent[SYS_setgroups].sy_callc;
281     afs_orig_ioctl = sysent[SYS_ioctl].sy_call;
282     sysent[SYS_setgroups].sy_callc = afs_xsetgroups;
283     sysent[SYS_ioctl].sy_call = afs_xioctl;
284
285 #if defined(AFS_SUN57_64BIT_ENV)
286     afs_orig_setgroups32 = sysent32[SYS_setgroups].sy_callc;
287     afs_orig_ioctl32 = sysent32[SYS_ioctl].sy_call;
288     sysent32[SYS_setgroups].sy_callc = afs_xsetgroups;
289     sysent32[SYS_ioctl].sy_call = afs_xioctl;
290 #endif
291
292     vfsswp->vsw_vfsops = &Afs_vfsops;
293     afsfstype = fstype;
294
295
296 #if     !defined(AFS_NONFSTRANS)
297     nfs_rfsdisptab_v2 = (int (*)()) modlookup("nfssrv", "rfsdisptab_v2");
298     if ( !nfs_rfsdisptab_v2 ) {
299         afs_warn("warning : rfsdisptab_v2 NOT FOUND\n");
300     }
301     if (nfs_rfsdisptab_v2) {
302         nfs_acldisptab_v2 = (int (*)()) modlookup("nfssrv", "acldisptab_v2");
303         if ( !nfs_acldisptab_v2 ) {
304             afs_warn("warning : acldisptab_v2 NOT FOUND\n");
305         }
306         else {
307             afs_xlatorinit_v2(nfs_rfsdisptab_v2, nfs_acldisptab_v2);
308         }
309     }
310     nfs_rfsdisptab_v3 = (int (*)()) modlookup("nfssrv", "rfsdisptab_v3");
311     if ( !nfs_rfsdisptab_v3 ) {
312         afs_warn("warning : rfsdisptab_v3 NOT FOUND\n");
313     }
314     if (nfs_rfsdisptab_v3) {
315         nfs_acldisptab_v3 = (int (*)()) modlookup("nfssrv", "acldisptab_v3");
316         if ( !nfs_acldisptab_v3 ) {
317             afs_warn("warning : acldisptab_v3 NOT FOUND\n");
318         }
319         else {
320             afs_xlatorinit_v3(nfs_rfsdisptab_v3, nfs_acldisptab_v3);
321         }
322     }
323
324     nfs_checkauth = (int (*)()) modlookup("nfssrv", "checkauth");
325     if ( !nfs_checkauth ) afs_warn("nfs_checkauth not initialised");
326 #endif
327     ufs_iallocp = (int (*)()) modlookup("ufs", "ufs_ialloc");    
328     ufs_iupdatp = (void (*)()) modlookup("ufs", "ufs_iupdat");
329     ufs_igetp = (int (*)()) modlookup("ufs", "ufs_iget");    
330     ufs_itimes_nolockp = (void (*)()) modlookup("ufs", "ufs_itimes_nolock");
331     udp_infop = (struct streamtab *) modlookup("udp", "udpinfo");    
332     ill_g_headp = (struct ill_s *) modlookup("ip", "ill_g_head");    
333
334     if ( !ufs_iallocp || !ufs_iupdatp || !ufs_itimes_nolockp ||
335          !ufs_igetp || !udp_infop || !ill_g_headp )
336         afs_warn("AFS to UFS mapping cannot be fully initialised\n");
337
338     afs_sinited = 1;
339     return 0;
340
341 }
342
343 static struct vfssw afs_vfw = {
344     "afs",
345     afsinit,
346     &Afs_vfsops,
347     0
348     };
349
350 static struct sysent afssysent = {
351     6,
352     0,
353     Afs_syscall
354     };
355
356 /* inter-module dependencies */
357 char _depends_on[] = "drv/ip drv/udp strmod/rpcmod";
358
359 /*
360  * Info/Structs to link the afs module into the kernel
361  */
362 extern struct mod_ops mod_fsops;
363 extern struct mod_ops mod_syscallops;
364
365 static struct modlfs afsmodlfs = {
366     &mod_fsops,
367     "afs filesystem",
368     &afs_vfw
369     };
370
371 static struct modlsys afsmodlsys = {
372     &mod_syscallops,
373     "afs syscall interface",
374     &afssysent
375     };
376
377 /** The two structures afssysent32 and afsmodlsys32 are being added
378   * for supporting 32 bit syscalls. In Solaris 7 there are two system
379   * tables viz. sysent ans sysent32. 32 bit applications use sysent32.
380   * Since most of our user space binaries are going to be 32 bit
381   * we need to attach to sysent32 also. Note that the entry into AFS
382   * land still happens through Afs_syscall irrespective of whether we
383   * land here from sysent or sysent32
384   */
385
386 #if defined(AFS_SUN57_64BIT_ENV)
387 extern struct mod_ops mod_syscallops32;
388
389 static struct modlsys afsmodlsys32 = {
390     &mod_syscallops32,
391     "afs syscall interface(32 bit)",
392     &afssysent
393 };
394 #endif
395
396
397 static struct modlinkage afs_modlinkage = {
398     MODREV_1,
399     (void *)&afsmodlsys,
400 #ifdef AFS_SUN57_64BIT_ENV
401     (void *)&afsmodlsys32,
402 #endif
403     (void *)&afsmodlfs,
404     NULL
405     };
406
407 /** This is the function that modload calls when loading the afs kernel
408   * extensions. The solaris modload program searches for the _init
409   * function in a module and calls it when modloading
410   */
411
412 _init()
413 {
414     char *sysn, *mod_getsysname();
415     int code;
416     extern char *sysbind;
417     extern struct bind *sb_hashtab[];
418     struct modctl *mp = 0;
419
420     if (afs_sinited)
421         return EBUSY;
422
423     if ((!(mp = mod_find_by_filename("fs", "ufs")) && 
424         !(mp = mod_find_by_filename(NULL, "/kernel/fs/ufs")) &&
425         !(mp = mod_find_by_filename(NULL, "sys/ufs"))) ||
426         (mp && !mp->mod_installed)) {
427         printf("ufs module must be loaded before loading afs; use modload /kernel/fs/ufs\n");
428         return (ENOSYS);
429     }
430 #ifndef AFS_NONFSTRANS
431 #if     defined(AFS_SUN55_ENV)
432     if ((!(mp = mod_find_by_filename("misc", "nfssrv")) &&
433          !(mp = mod_find_by_filename(NULL, NFSSRV)) &&
434          !(mp = mod_find_by_filename(NULL, NFSSRV_V9))) || 
435         (mp && !mp->mod_installed)) {
436         printf("misc/nfssrv module must be loaded before loading afs with nfs-xlator\n");
437         return (ENOSYS);
438     }
439 #else /* !AFS_SUN55_ENV */
440 #if     defined(AFS_SUN52_ENV)
441     if ((!(mp = mod_find_by_filename("fs", "nfs")) && 
442         !(mp = mod_find_by_filename(NULL, "/kernel/fs/nfs")) &&
443         !(mp = mod_find_by_filename(NULL, "sys/nfs"))) ||
444         (mp && !mp->mod_installed)) {
445         printf("fs/nfs module must be loaded before loading afs with nfs-xlator\n");
446         return (ENOSYS);
447     }
448 #endif /* AFS_SUN52_ENV */
449 #endif /* AFS_SUN55_ENV */
450 #endif /* !AFS_NONFSTRANS */
451 #if !defined(AFS_SUN58_ENV)
452     /* 
453      * Re-read the /etc/name_to_sysnum file to make sure afs isn't added after
454      * reboot.  Ideally we would like to call modctl_read_sysbinding_file() but
455      * unfortunately in Solaris 2.2 it became a local function so we have to do
456      * the read_binding_file() direct call with the appropriate text file and
457      * system call hashtable.  make_syscallname actually copies "afs" to the
458      * proper slot entry and we also actually have to properly initialize the
459      * global sysent[AFS_SYSCALL] entry!
460      */
461 #ifdef  AFS_SUN53_ENV
462 #ifndef SYSBINDFILE
463 #define SYSBINDFILE     "/etc/name_to_sysnum"
464 #endif /* SYSBINDFILE */
465     read_binding_file(SYSBINDFILE, sb_hashtab);
466 #else /* !AFS_SUN53_ENV */
467     read_binding_file(sysbind, sb_hashtab);
468 #endif /* AFS_SUN53_ENV */
469     make_syscallname("afs", AFS_SYSCALL);
470
471     if (sysent[AFS_SYSCALL].sy_call == nosys) {
472         if ((sysn = mod_getsysname(AFS_SYSCALL)) != NULL) {
473             sysent[AFS_SYSCALL].sy_lock =
474                 (krwlock_t *) kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
475             rw_init(sysent[AFS_SYSCALL].sy_lock, "afs_syscall",
476 #ifdef AFS_SUN57_ENV
477                     RW_DEFAULT, NULL);
478 #else /* !AFS_SUN57_ENV */
479                         RW_DEFAULT, DEFAULT_WT);
480 #endif /* AFS_SUN57_ENV */
481         }
482     }
483 #endif /* !AFS_SUN58_ENV */
484
485     osi_Init();                         /* initialize global lock, etc */
486
487     code = mod_install(&afs_modlinkage);
488     return code;
489 }
490
491 _info(modp)
492     struct modinfo *modp;
493 {
494     int code;
495
496     code = mod_info(&afs_modlinkage, modp);
497     return code;
498 }
499
500 _fini()
501 {
502     int code;
503
504     if (afs_globalVFS)
505         return EBUSY;
506
507     if (afs_sinited) {
508         sysent[SYS_setgroups].sy_callc = afs_orig_setgroups;
509         sysent[SYS_ioctl].sy_call = afs_orig_ioctl;
510 #if defined(AFS_SUN57_64BIT_ENV)
511         sysent32[SYS_setgroups].sy_callc = afs_orig_setgroups32;
512         sysent32[SYS_ioctl].sy_call = afs_orig_ioctl32;
513 #endif
514     }
515     code = mod_remove(&afs_modlinkage);
516     return code;
517 }