8a30878843aedb07dbd33efdcb8957f90be112d3
[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 "../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         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, (afs_int32 *)0,
122                                 (struct vcache*)0, WRITE_LOCK);
123             /* we really want this to stay around */
124             if (tvp) {
125                 afs_globalVp = tvp;
126             } else
127                 code = ENOENT;
128         }
129     }
130     if (tvp) {
131         VN_HOLD(AFSTOV(tvp));
132         mutex_enter(&AFSTOV(tvp)->v_lock);
133         AFSTOV(tvp)->v_flag |= VROOT;
134         mutex_exit(&AFSTOV(tvp)->v_lock);
135
136         afs_globalVFS = afsp;
137         *avpp = AFSTOV(tvp);
138     }
139
140     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, *avpp,
141                ICL_TYPE_INT32, code);
142
143     AFS_GUNLOCK();
144     if (locked) {
145         mutex_enter(&vp->v_lock);
146         vp->v_flag &= ~VVFSLOCK;
147         if (vp->v_flag & VVFSWAIT) {
148             vp->v_flag &= ~VVFSWAIT;
149             cv_broadcast(&vp->v_cv);
150         }
151     }
152
153     return code;
154 }
155
156 #ifdef AFS_SUN56_ENV
157 int afs_statvfs(struct vfs *afsp, struct statvfs64 *abp)
158 #else
159 int afs_statvfs(struct vfs *afsp, struct statvfs *abp)
160 #endif
161 {
162     AFS_GLOCK();
163
164     AFS_STATCNT(afs_statfs);
165
166     abp->f_frsize = 1024;
167     abp->f_favail =  9000000;
168     abp->f_bsize = afsp->vfs_bsize;
169     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
170         abp->f_ffree  = 9000000;
171     abp->f_fsid = (AFS_VFSMAGIC << 16) || AFS_VFSFSID;
172
173     AFS_GUNLOCK();
174     return 0;
175 }
176
177 int afs_sync(struct vfs *afsp, short flags, struct AFS_UCRED *credp)
178 {
179     return 0;
180 }
181
182 int afs_vget(struct vfs *afsp, struct vnode **avcp, struct fid *fidp)
183 {
184     cred_t *credp = CRED();
185     struct vrequest treq;
186     int code;
187
188     AFS_GLOCK();
189
190     AFS_STATCNT(afs_vget);
191
192     *avcp = NULL;
193     if (!(code = afs_InitReq(&treq, credp))) {
194         code = afs_osi_vget((struct vcache**)avcp, fidp, &treq);
195     }
196
197     afs_Trace3(afs_iclSetp, CM_TRACE_VGET, ICL_TYPE_POINTER, *avcp,
198                ICL_TYPE_INT32, treq.uid, ICL_TYPE_FID, fidp);
199     code = afs_CheckCode(code, &treq, 42);
200
201     AFS_GUNLOCK();
202     return code;
203 }
204
205 /* This is only called by vfs_mount when afs is going to be mounted as root.
206  * Since we don't support diskless clients we shouldn't come here.
207  */
208 int afsmountroot=0;
209 afs_mountroot(struct vfs *afsp, whymountroot_t why)
210 {
211     AFS_GLOCK();
212     AFS_STATCNT(afs_mountroot);
213     afsmountroot++;
214     AFS_GUNLOCK();
215     return EINVAL;
216 }
217
218 /* afs_swapvp is called to setup swapping over the net for diskless clients.
219  * Again not for us.
220  */
221 int afsswapvp=0;
222 afs_swapvp(struct vfs *afsp, struct vnode **avpp, char *nm)
223 {
224     AFS_GLOCK();
225     AFS_STATCNT(afs_swapvp);
226     afsswapvp++;
227     AFS_GUNLOCK();
228     return EINVAL;
229 }
230
231
232 struct vfsops Afs_vfsops = {
233     afs_mount,
234     afs_unmount,
235     afs_root,
236     afs_statvfs,
237     afs_sync,
238     afs_vget,
239     afs_mountroot,
240     afs_swapvp,
241 #if defined(AFS_SUN58_ENV)
242     fs_freevfs,
243 #endif
244 };
245
246
247 /*
248  * afsinit - intialize VFS
249  */
250 int (*ufs_iallocp)();
251 void (*ufs_iupdatp)();
252 int (*ufs_igetp)();
253 void (*ufs_itimes_nolockp)();
254
255 int (*afs_orig_ioctl)(), (*afs_orig_ioctl32)();
256 int (*afs_orig_setgroups)(), (*afs_orig_setgroups32)();
257
258 struct streamtab *udp_infop = 0;
259 struct ill_s *ill_g_headp = 0;
260
261 int afs_sinited = 0;
262
263 #if     !defined(AFS_NONFSTRANS)
264 int (*nfs_rfsdisptab_v2)();
265 int (*nfs_rfsdisptab_v3)();
266 int (*nfs_acldisptab_v2)();
267 int (*nfs_acldisptab_v3)();
268
269 int (*nfs_checkauth)();
270 #endif
271
272 extern Afs_syscall();
273
274 afsinit(struct vfssw *vfsswp, int fstype)
275 {
276     extern int afs_xioctl();
277     extern int afs_xsetgroups();
278
279     AFS_STATCNT(afsinit);
280
281     afs_orig_setgroups = sysent[SYS_setgroups].sy_callc;
282     afs_orig_ioctl = sysent[SYS_ioctl].sy_call;
283     sysent[SYS_setgroups].sy_callc = afs_xsetgroups;
284     sysent[SYS_ioctl].sy_call = afs_xioctl;
285
286 #if defined(AFS_SUN57_64BIT_ENV)
287     afs_orig_setgroups32 = sysent32[SYS_setgroups].sy_callc;
288     afs_orig_ioctl32 = sysent32[SYS_ioctl].sy_call;
289     sysent32[SYS_setgroups].sy_callc = afs_xsetgroups;
290     sysent32[SYS_ioctl].sy_call = afs_xioctl;
291 #endif
292
293     vfsswp->vsw_vfsops = &Afs_vfsops;
294     afsfstype = fstype;
295
296
297 #if     !defined(AFS_NONFSTRANS)
298     nfs_rfsdisptab_v2 = (int (*)()) modlookup("nfssrv", "rfsdisptab_v2");
299     if ( !nfs_rfsdisptab_v2 ) {
300         afs_warn("warning : rfsdisptab_v2 NOT FOUND\n");
301     }
302     if (nfs_rfsdisptab_v2) {
303         nfs_acldisptab_v2 = (int (*)()) modlookup("nfssrv", "acldisptab_v2");
304         if ( !nfs_acldisptab_v2 ) {
305             afs_warn("warning : acldisptab_v2 NOT FOUND\n");
306         }
307         else {
308             afs_xlatorinit_v2(nfs_rfsdisptab_v2, nfs_acldisptab_v2);
309         }
310     }
311     nfs_rfsdisptab_v3 = (int (*)()) modlookup("nfssrv", "rfsdisptab_v3");
312     if ( !nfs_rfsdisptab_v3 ) {
313         afs_warn("warning : rfsdisptab_v3 NOT FOUND\n");
314     }
315     if (nfs_rfsdisptab_v3) {
316         nfs_acldisptab_v3 = (int (*)()) modlookup("nfssrv", "acldisptab_v3");
317         if ( !nfs_acldisptab_v3 ) {
318             afs_warn("warning : acldisptab_v3 NOT FOUND\n");
319         }
320         else {
321             afs_xlatorinit_v3(nfs_rfsdisptab_v3, nfs_acldisptab_v3);
322         }
323     }
324
325     nfs_checkauth = (int (*)()) modlookup("nfssrv", "checkauth");
326     if ( !nfs_checkauth ) afs_warn("nfs_checkauth not initialised");
327 #endif
328     ufs_iallocp = (int (*)()) modlookup("ufs", "ufs_ialloc");    
329     ufs_iupdatp = (void (*)()) modlookup("ufs", "ufs_iupdat");
330     ufs_igetp = (int (*)()) modlookup("ufs", "ufs_iget");    
331     ufs_itimes_nolockp = (void (*)()) modlookup("ufs", "ufs_itimes_nolock");
332     udp_infop = (struct streamtab *) modlookup("udp", "udpinfo");    
333     ill_g_headp = (struct ill_s *) modlookup("ip", "ill_g_head");    
334
335     if ( !ufs_iallocp || !ufs_iupdatp || !ufs_itimes_nolockp ||
336          !ufs_igetp || !udp_infop || !ill_g_headp )
337         afs_warn("AFS to UFS mapping cannot be fully initialised\n");
338
339     afs_sinited = 1;
340     return 0;
341
342 }
343
344 static struct vfssw afs_vfw = {
345     "afs",
346     afsinit,
347     &Afs_vfsops,
348     0
349     };
350
351 static struct sysent afssysent = {
352     6,
353     0,
354     Afs_syscall
355     };
356
357 /* inter-module dependencies */
358 char _depends_on[] = "drv/ip drv/udp strmod/rpcmod";
359
360 /*
361  * Info/Structs to link the afs module into the kernel
362  */
363 extern struct mod_ops mod_fsops;
364 extern struct mod_ops mod_syscallops;
365
366 static struct modlfs afsmodlfs = {
367     &mod_fsops,
368     "afs filesystem",
369     &afs_vfw
370     };
371
372 static struct modlsys afsmodlsys = {
373     &mod_syscallops,
374     "afs syscall interface",
375     &afssysent
376     };
377
378 /** The two structures afssysent32 and afsmodlsys32 are being added
379   * for supporting 32 bit syscalls. In Solaris 7 there are two system
380   * tables viz. sysent ans sysent32. 32 bit applications use sysent32.
381   * Since most of our user space binaries are going to be 32 bit
382   * we need to attach to sysent32 also. Note that the entry into AFS
383   * land still happens through Afs_syscall irrespective of whether we
384   * land here from sysent or sysent32
385   */
386
387 #if defined(AFS_SUN57_64BIT_ENV)
388 extern struct mod_ops mod_syscallops32;
389
390 static struct modlsys afsmodlsys32 = {
391     &mod_syscallops32,
392     "afs syscall interface(32 bit)",
393     &afssysent
394 };
395 #endif
396
397
398 static struct modlinkage afs_modlinkage = {
399     MODREV_1,
400     (void *)&afsmodlsys,
401 #ifdef AFS_SUN57_64BIT_ENV
402     (void *)&afsmodlsys32,
403 #endif
404     (void *)&afsmodlfs,
405     NULL
406     };
407
408 /** This is the function that modload calls when loading the afs kernel
409   * extensions. The solaris modload program searches for the _init
410   * function in a module and calls it when modloading
411   */
412
413 _init()
414 {
415     char *sysn, *mod_getsysname();
416     int code;
417     extern char *sysbind;
418     extern struct bind *sb_hashtab[];
419     struct modctl *mp = 0;
420
421     if (afs_sinited)
422         return EBUSY;
423
424     if ((!(mp = mod_find_by_filename("fs", "ufs")) && 
425         !(mp = mod_find_by_filename(NULL, "/kernel/fs/ufs")) &&
426         !(mp = mod_find_by_filename(NULL, "sys/ufs"))) ||
427         (mp && !mp->mod_installed)) {
428         printf("ufs module must be loaded before loading afs; use modload /kernel/fs/ufs\n");
429         return (ENOSYS);
430     }
431 #ifndef AFS_NONFSTRANS
432 #if     defined(AFS_SUN55_ENV)
433     if ((!(mp = mod_find_by_filename("misc", "nfssrv")) &&
434          !(mp = mod_find_by_filename(NULL, NFSSRV)) &&
435          !(mp = mod_find_by_filename(NULL, NFSSRV_V9))) || 
436         (mp && !mp->mod_installed)) {
437         printf("misc/nfssrv module must be loaded before loading afs with nfs-xlator\n");
438         return (ENOSYS);
439     }
440 #else
441 #if     defined(AFS_SUN52_ENV)
442     if ((!(mp = mod_find_by_filename("fs", "nfs")) && 
443         !(mp = mod_find_by_filename(NULL, "/kernel/fs/nfs")) &&
444         !(mp = mod_find_by_filename(NULL, "sys/nfs"))) ||
445         (mp && !mp->mod_installed)) {
446         printf("fs/nfs module must be loaded before loading afs with nfs-xlator\n");
447         return (ENOSYS);
448     }
449 #endif
450 #endif
451 #endif
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
465     read_binding_file(SYSBINDFILE, sb_hashtab);
466 #else
467     read_binding_file(sysbind, sb_hashtab);
468 #endif
469 #if !defined(AFS_SUN58_ENV)
470     make_syscallname("afs", AFS_SYSCALL);
471 #endif
472
473     if (sysent[AFS_SYSCALL].sy_call == nosys) {
474         if ((sysn = mod_getsysname(AFS_SYSCALL)) != NULL) {
475             sysent[AFS_SYSCALL].sy_lock =
476                 (krwlock_t *) kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
477             rw_init(sysent[AFS_SYSCALL].sy_lock, "afs_syscall",
478 #ifdef AFS_SUN57_ENV
479                     RW_DEFAULT, NULL);
480 #else
481                         RW_DEFAULT, DEFAULT_WT);
482 #endif  
483         }
484     }
485
486     osi_Init();                         /* initialize global lock, etc */
487
488     code = mod_install(&afs_modlinkage);
489     return code;
490 }
491
492 _info(modp)
493     struct modinfo *modp;
494 {
495     int code;
496
497     code = mod_info(&afs_modlinkage, modp);
498     return code;
499 }
500
501 _fini()
502 {
503     int code;
504
505     if (afs_globalVFS)
506         return EBUSY;
507
508     if (afs_sinited) {
509         sysent[SYS_setgroups].sy_callc = afs_orig_setgroups;
510         sysent[SYS_ioctl].sy_call = afs_orig_ioctl;
511 #if defined(AFS_SUN57_64BIT_ENV)
512         sysent32[SYS_setgroups].sy_callc = afs_orig_setgroups32;
513         sysent32[SYS_ioctl].sy_call = afs_orig_ioctl32;
514 #endif
515     }
516     code = mod_remove(&afs_modlinkage);
517     return code;
518 }