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