Merge 1.8.0~pre4 packaging into master
[pkg-k5-afs_openafs.git] / src / afs / SOLARIS / osi_vfsops.c
blob86e5f7803d5164ac5b595df48aa5d499a6049a5c
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 */
11 * osi_vfsops.c for SOLARIS
13 #include <afsconfig.h>
14 #include "afs/param.h"
17 #include "afs/sysincludes.h" /* Standard vendor system headers */
18 #include "afsincludes.h" /* Afs-based standard headers */
19 #include "afs/afs_stats.h" /* statistics stuff */
20 #include "h/modctl.h"
21 #include "h/syscall.h"
22 #if defined(AFS_SUN511_ENV)
23 #include <sys/vfs_opreg.h>
24 #endif
25 #include <sys/kobj.h>
27 #include <sys/mount.h>
29 struct vfs *afs_globalVFS = 0;
30 struct vcache *afs_globalVp = 0;
32 #if defined(AFS_SUN5_64BIT_ENV)
33 extern struct sysent sysent32[];
34 #endif
36 int afsfstype = 0;
38 int
39 afs_mount(struct vfs *afsp, struct vnode *amvp, struct mounta *uap,
40 afs_ucred_t *credp)
43 AFS_GLOCK();
45 AFS_STATCNT(afs_mount);
47 #if defined(AFS_SUN510_ENV)
48 if (secpolicy_fs_mount(credp, amvp, afsp) != 0) {
49 #else
50 if (!afs_osi_suser(credp)) {
51 #endif
52 AFS_GUNLOCK();
53 return (EPERM);
55 afsp->vfs_fstype = afsfstype;
57 if (afs_globalVFS) { /* Don't allow remounts. */
58 AFS_GUNLOCK();
59 return (EBUSY);
62 afs_globalVFS = afsp;
63 afsp->vfs_bsize = 8192;
64 afsp->vfs_fsid.val[0] = AFS_VFSMAGIC; /* magic */
65 afsp->vfs_fsid.val[1] = AFS_VFSFSID;
66 afsp->vfs_dev = AFS_VFSMAGIC;
68 AFS_GUNLOCK();
69 return 0;
72 static void
73 afs_freevfs(void)
76 afs_globalVFS = 0;
78 afs_shutdown();
81 int
82 afs_unmount(struct vfs *afsp, int flag, afs_ucred_t *credp)
84 struct vcache *rootvp = NULL;
86 AFS_GLOCK();
87 AFS_STATCNT(afs_unmount);
89 #if defined(AFS_SUN510_ENV)
90 if (secpolicy_fs_unmount(credp, afsp) != 0) {
91 #else
92 if (!afs_osi_suser(credp)) {
93 #endif
94 AFS_GUNLOCK();
95 return (EPERM);
98 if (flag & MS_FORCE) {
99 AFS_GUNLOCK();
100 return ENOTSUP;
103 /* We should have one reference from the caller, and one reference for the
104 * root vnode; any more and someone is still referencing something */
105 if (afsp->vfs_count > 2) {
106 AFS_GUNLOCK();
107 return EBUSY;
110 /* The root vnode should have one ref for the mount; any more, and someone
111 * else is using the root vnode */
112 if (afs_globalVp && VREFCOUNT_GT(afs_globalVp, 1)) {
113 AFS_GUNLOCK();
114 return EBUSY;
117 afsp->vfs_flag |= VFS_UNMOUNTED;
119 if (afs_globalVp) {
120 /* release the root vnode, which should be the last reference to us
121 * besides the caller of afs_unmount */
122 rootvp = afs_globalVp;
123 afs_globalVp = NULL;
124 AFS_RELE(rootvp);
127 AFS_GUNLOCK();
128 return 0;
131 void
132 gafs_freevfs(struct vfs *afsp)
134 AFS_GLOCK();
136 afs_freevfs();
138 AFS_GUNLOCK();
142 afs_root(struct vfs *afsp, struct vnode **avpp)
144 afs_int32 code = 0;
145 struct vrequest treq;
146 struct vcache *tvp = 0;
147 struct vcache *gvp;
148 struct proc *proc = ttoproc(curthread);
149 struct vnode *vp = afsp->vfs_vnodecovered;
150 int locked = 0;
152 /* Potential deadlock:
153 * afs_root is called with the Vnode's v_lock locked. Set VVFSLOCK
154 * and drop the v_lock if we need to make an RPC to complete this
155 * request. There used to be a deadlock on the global lock until
156 * we stopped calling iget while holding the global lock.
159 AFS_GLOCK();
161 AFS_STATCNT(afs_root);
163 again:
164 if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
165 tvp = afs_globalVp;
166 } else {
167 if (MUTEX_HELD(&vp->v_lock)) {
168 vp->v_flag |= VVFSLOCK;
169 locked = 1;
170 mutex_exit(&vp->v_lock);
173 if (afs_globalVp) {
174 gvp = afs_globalVp;
175 afs_globalVp = NULL;
176 afs_PutVCache(gvp);
179 if (!(code = afs_InitReq(&treq, proc->p_cred))
180 && !(code = afs_CheckInit())) {
181 tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
182 /* we really want this to stay around */
183 if (tvp) {
184 if (afs_globalVp) {
185 /* someone else got there before us! */
186 afs_PutVCache(tvp);
187 tvp = 0;
188 goto again;
190 afs_globalVp = tvp;
191 } else
192 code = EIO;
195 if (tvp) {
196 AFS_FAST_HOLD(tvp);
197 mutex_enter(&AFSTOV(tvp)->v_lock);
198 AFSTOV(tvp)->v_flag |= VROOT;
199 mutex_exit(&AFSTOV(tvp)->v_lock);
201 afs_globalVFS = afsp;
202 *avpp = AFSTOV(tvp);
205 afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, *avpp,
206 ICL_TYPE_INT32, code);
208 AFS_GUNLOCK();
209 if (locked) {
210 mutex_enter(&vp->v_lock);
211 vp->v_flag &= ~VVFSLOCK;
212 if (vp->v_flag & VVFSWAIT) {
213 vp->v_flag &= ~VVFSWAIT;
214 cv_broadcast(&vp->v_cv);
218 return code;
222 afs_statvfs(struct vfs *afsp, struct statvfs64 *abp)
224 AFS_GLOCK();
226 AFS_STATCNT(afs_statfs);
228 abp->f_frsize = 1024;
229 abp->f_bsize = afsp->vfs_bsize;
230 abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
231 abp->f_favail = abp->f_ffree = AFS_VFS_FAKEFREE;
232 abp->f_fsid = (AFS_VFSMAGIC << 16) || AFS_VFSFSID;
234 AFS_GUNLOCK();
235 return 0;
239 afs_sync(struct vfs *afsp, short flags, afs_ucred_t *credp)
241 return 0;
245 afs_vget(struct vfs *afsp, struct vnode **avcp, struct fid *fidp)
247 cred_t *credp = CRED();
248 struct vrequest treq;
249 int code;
251 AFS_GLOCK();
253 AFS_STATCNT(afs_vget);
255 *avcp = NULL;
256 if (!(code = afs_InitReq(&treq, credp))) {
257 code = afs_osi_vget((struct vcache **)avcp, fidp, &treq);
260 afs_Trace3(afs_iclSetp, CM_TRACE_VGET, ICL_TYPE_POINTER, *avcp,
261 ICL_TYPE_INT32, treq.uid, ICL_TYPE_FID, fidp);
262 code = afs_CheckCode(code, &treq, 42);
264 AFS_GUNLOCK();
265 return code;
268 /* This is only called by vfs_mount when afs is going to be mounted as root.
269 * Since we don't support diskless clients we shouldn't come here.
271 int afsmountroot = 0;
272 afs_mountroot(struct vfs *afsp, whymountroot_t why)
274 AFS_GLOCK();
275 AFS_STATCNT(afs_mountroot);
276 afsmountroot++;
277 AFS_GUNLOCK();
278 return EINVAL;
281 /* afs_swapvp is called to setup swapping over the net for diskless clients.
282 * Again not for us.
284 int afsswapvp = 0;
285 afs_swapvp(struct vfs *afsp, struct vnode **avpp, char *nm)
287 AFS_GLOCK();
288 AFS_STATCNT(afs_swapvp);
289 afsswapvp++;
290 AFS_GUNLOCK();
291 return EINVAL;
295 #if defined(AFS_SUN511_ENV)
296 /* The following list must always be NULL-terminated */
297 static const fs_operation_def_t afs_vfsops_template[] = {
298 VFSNAME_MOUNT, { .vfs_mount = afs_mount },
299 VFSNAME_UNMOUNT, { .vfs_unmount = afs_unmount },
300 VFSNAME_ROOT, { .vfs_root = afs_root },
301 VFSNAME_STATVFS, { .vfs_statvfs = afs_statvfs },
302 VFSNAME_SYNC, { .vfs_sync = afs_sync },
303 VFSNAME_VGET, { .vfs_vget = afs_vget },
304 VFSNAME_MOUNTROOT, { .vfs_mountroot = afs_mountroot },
305 VFSNAME_FREEVFS, { .vfs_freevfs = gafs_freevfs },
306 NULL, NULL
308 struct vfsops *afs_vfsopsp;
309 #elif defined(AFS_SUN510_ENV)
310 /* The following list must always be NULL-terminated */
311 const fs_operation_def_t afs_vfsops_template[] = {
312 VFSNAME_MOUNT, afs_mount,
313 VFSNAME_UNMOUNT, afs_unmount,
314 VFSNAME_ROOT, afs_root,
315 VFSNAME_STATVFS, afs_statvfs,
316 VFSNAME_SYNC, afs_sync,
317 VFSNAME_VGET, afs_vget,
318 VFSNAME_MOUNTROOT, afs_mountroot,
319 VFSNAME_FREEVFS, gafs_freevfs,
320 NULL, NULL
322 struct vfsops *afs_vfsopsp;
323 #else
324 struct vfsops Afs_vfsops = {
325 afs_mount,
326 afs_unmount,
327 afs_root,
328 afs_statvfs,
329 afs_sync,
330 afs_vget,
331 afs_mountroot,
332 afs_swapvp,
333 gafs_freevfs,
335 #endif
339 * afsinit - intialize VFS
341 int (*ufs_iallocp) ();
342 void (*ufs_iupdatp) ();
343 int (*ufs_igetp) ();
344 void (*ufs_itimes_nolockp) ();
346 int (*afs_orig_ioctl) (), (*afs_orig_ioctl32) ();
347 int (*afs_orig_setgroups) (), (*afs_orig_setgroups32) ();
349 #ifndef AFS_SUN510_ENV
350 struct ill_s *ill_g_headp = 0;
351 #endif
353 int afs_sinited = 0;
355 extern struct fs_operation_def afs_vnodeops_template[];
357 #if !defined(AFS_NONFSTRANS)
358 int (*nfs_rfsdisptab_v2) ();
359 int (*nfs_rfsdisptab_v3) ();
360 int (*nfs_acldisptab_v2) ();
361 int (*nfs_acldisptab_v3) ();
363 int (*nfs_checkauth) ();
364 #endif
366 extern Afs_syscall();
368 static void *
369 do_mod_lookup(const char * mod, const char * sym)
371 void * ptr;
373 ptr = modlookup(mod, sym);
374 if (ptr == NULL) {
375 afs_warn("modlookup failed for symbol '%s' in module '%s'\n",
376 sym, mod);
379 return ptr;
382 #ifdef AFS_SUN510_ENV
383 afsinit(int fstype, char *dummy)
384 #else
385 afsinit(struct vfssw *vfsswp, int fstype)
386 #endif
388 extern int afs_xioctl();
389 extern int afs_xsetgroups();
391 AFS_STATCNT(afsinit);
393 afs_orig_setgroups = sysent[SYS_setgroups].sy_callc;
394 afs_orig_ioctl = sysent[SYS_ioctl].sy_call;
395 sysent[SYS_setgroups].sy_callc = afs_xsetgroups;
396 sysent[SYS_ioctl].sy_call = afs_xioctl;
398 #if defined(AFS_SUN5_64BIT_ENV)
399 afs_orig_setgroups32 = sysent32[SYS_setgroups].sy_callc;
400 afs_orig_ioctl32 = sysent32[SYS_ioctl].sy_call;
401 sysent32[SYS_setgroups].sy_callc = afs_xsetgroups;
402 sysent32[SYS_ioctl].sy_call = afs_xioctl;
403 #endif /* AFS_SUN5_64BIT_ENV */
405 #ifdef AFS_SUN510_ENV
406 vfs_setfsops(fstype, afs_vfsops_template, &afs_vfsopsp);
407 afsfstype = fstype;
408 vn_make_ops("afs", afs_vnodeops_template, &afs_ops);
409 #else /* !AFS_SUN510_ENV */
410 vfsswp->vsw_vfsops = &Afs_vfsops;
411 afsfstype = fstype;
412 #endif /* !AFS_SUN510_ENV */
415 #if !defined(AFS_NONFSTRANS)
416 nfs_rfsdisptab_v2 = (int (*)()) do_mod_lookup("nfssrv", "rfsdisptab_v2");
417 if (nfs_rfsdisptab_v2 != NULL) {
418 nfs_acldisptab_v2 = (int (*)()) do_mod_lookup("nfssrv", "acldisptab_v2");
419 if (nfs_acldisptab_v2 != NULL) {
420 afs_xlatorinit_v2(nfs_rfsdisptab_v2, nfs_acldisptab_v2);
423 nfs_rfsdisptab_v3 = (int (*)()) do_mod_lookup("nfssrv", "rfsdisptab_v3");
424 if (nfs_rfsdisptab_v3 != NULL) {
425 nfs_acldisptab_v3 = (int (*)()) do_mod_lookup("nfssrv", "acldisptab_v3");
426 if (nfs_acldisptab_v3 != NULL) {
427 afs_xlatorinit_v3(nfs_rfsdisptab_v3, nfs_acldisptab_v3);
431 nfs_checkauth = (int (*)()) do_mod_lookup("nfssrv", "checkauth");
432 #endif /* !AFS_NONFSTRANS */
434 ufs_iallocp = (int (*)()) do_mod_lookup("ufs", "ufs_ialloc");
435 ufs_iupdatp = (void (*)()) do_mod_lookup("ufs", "ufs_iupdat");
436 ufs_igetp = (int (*)()) do_mod_lookup("ufs", "ufs_iget");
437 ufs_itimes_nolockp = (void (*)()) do_mod_lookup("ufs", "ufs_itimes_nolock");
439 if (!ufs_iallocp || !ufs_iupdatp || !ufs_itimes_nolockp || !ufs_igetp) {
440 afs_warn("AFS to UFS mapping cannot be fully initialised\n");
443 #if !defined(AFS_SUN510_ENV)
444 ill_g_headp = (struct ill_s *) do_mod_lookup("ip", "ill_g_head");
445 #endif /* !AFS_SUN510_ENV */
447 afs_sinited = 1;
448 return 0;
452 #ifdef AFS_SUN510_ENV
453 #ifdef AFS_SUN511_ENV
454 static vfsdef_t afs_vfsdef = {
455 VFSDEF_VERSION,
456 "afs",
457 afsinit,
458 VSW_STATS,
459 NULL
461 #else
462 static struct vfsdef_v3 afs_vfsdef = {
463 VFSDEF_VERSION,
464 "afs",
465 afsinit,
466 VSW_STATS
468 #endif
469 #else
470 static struct vfssw afs_vfw = {
471 "afs",
472 afsinit,
473 &Afs_vfsops,
476 #endif
478 #ifndef AFS_SUN511_ENV
479 static struct sysent afssysent = {
482 Afs_syscall
484 #endif /* AFS_SUN511_ENV */
487 * Info/Structs to link the afs module into the kernel
489 extern struct mod_ops mod_fsops;
491 static struct modlfs afsmodlfs = {
492 &mod_fsops,
493 "afs filesystem",
494 #ifdef AFS_SUN510_ENV
495 &afs_vfsdef
496 #else
497 &afs_vfw
498 #endif
501 #ifdef AFS_SUN511_ENV
503 extern struct modldrv afs_modldrv;
505 #else /* AFS_SUN511_ENV */
507 extern struct mod_ops mod_syscallops;
508 static struct modlsys afsmodlsys = {
509 &mod_syscallops,
510 "afs syscall interface",
511 &afssysent
514 /** The two structures afssysent32 and afsmodlsys32 are being added
515 * for supporting 32 bit syscalls. In Solaris 7 there are two system
516 * tables viz. sysent ans sysent32. 32 bit applications use sysent32.
517 * Since most of our user space binaries are going to be 32 bit
518 * we need to attach to sysent32 also. Note that the entry into AFS
519 * land still happens through Afs_syscall irrespective of whether we
520 * land here from sysent or sysent32
523 # if defined(AFS_SUN5_64BIT_ENV)
524 extern struct mod_ops mod_syscallops32;
526 static struct modlsys afsmodlsys32 = {
527 &mod_syscallops32,
528 "afs syscall interface(32 bit)",
529 &afssysent
531 # endif
532 #endif /* !AFS_SUN511_ENV */
535 static struct modlinkage afs_modlinkage = {
536 MODREV_1,
537 (void *)&afsmodlfs,
538 #ifdef AFS_SUN511_ENV
539 (void *)&afs_modldrv,
540 #else
541 (void *)&afsmodlsys,
542 # ifdef AFS_SUN5_64BIT_ENV
543 (void *)&afsmodlsys32,
544 # endif
545 #endif /* !AFS_SUN511_ENV */
546 NULL
549 static void
550 reset_sysent(void)
552 if (afs_sinited) {
553 sysent[SYS_setgroups].sy_callc = afs_orig_setgroups;
554 sysent[SYS_ioctl].sy_call = afs_orig_ioctl;
555 #if defined(AFS_SUN5_64BIT_ENV)
556 sysent32[SYS_setgroups].sy_callc = afs_orig_setgroups32;
557 sysent32[SYS_ioctl].sy_call = afs_orig_ioctl32;
558 #endif
562 /** This is the function that modload calls when loading the afs kernel
563 * extensions. The solaris modload program searches for the _init
564 * function in a module and calls it when modloading
567 _init()
569 char *sysn, *mod_getsysname();
570 int code;
571 extern char *sysbind;
572 extern struct bind *sb_hashtab[];
573 struct modctl *mp = 0;
575 if (afs_sinited)
576 return EBUSY;
578 if ((!(mp = mod_find_by_filename("fs", "ufs"))
579 && !(mp = mod_find_by_filename(NULL, "/kernel/fs/ufs"))
580 && !(mp = mod_find_by_filename(NULL, "sys/ufs"))) || (mp
581 && !mp->
582 mod_installed))
584 printf
585 ("ufs module must be loaded before loading afs; use modload /kernel/fs/ufs\n");
586 return (ENOSYS);
588 #ifndef AFS_NONFSTRANS
589 if ((!(mp = mod_find_by_filename("misc", "nfssrv"))
590 && !(mp = mod_find_by_filename(NULL, NFSSRV))
591 && !(mp = mod_find_by_filename(NULL, NFSSRV_V9))
592 && !(mp = mod_find_by_filename(NULL, NFSSRV_AMD64))) || (mp
593 && !mp->
594 mod_installed))
596 printf
597 ("misc/nfssrv module must be loaded before loading afs with nfs-xlator\n");
598 return (ENOSYS);
600 #endif /* !AFS_NONFSTRANS */
603 osi_Init(); /* initialize global lock, etc */
605 code = mod_install(&afs_modlinkage);
606 if (code) {
607 /* we failed to load, so make sure we don't leave behind any
608 * references to our syscall handlers */
609 reset_sysent();
611 return code;
614 _info(modp)
615 struct modinfo *modp;
617 int code;
619 code = mod_info(&afs_modlinkage, modp);
620 return code;
623 _fini()
625 int code;
627 if (afs_globalVFS)
628 return EBUSY;
630 reset_sysent();
631 code = mod_remove(&afs_modlinkage);
632 return code;