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