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