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