DEVEL15-freebsd-70-client-20080826
[openafs.git] / src / afs / FBSD / osi_vfsops.c
1 #include <afsconfig.h>
2 #include <afs/param.h>
3
4 RCSID
5     ("$Header$");
6
7 #include <afs/sysincludes.h>    /* Standard vendor system headers */
8 #include <afsincludes.h>        /* Afs-based standard headers */
9 #include <afs/afs_stats.h>      /* statistics */
10 #include <sys/malloc.h>
11 #include <sys/namei.h>
12 #include <sys/conf.h>
13 #include <sys/module.h>
14 #include <sys/sysproto.h>
15 #include <sys/syscall.h>
16 #include <sys/sysent.h>
17
18 struct vcache *afs_globalVp = NULL;
19 struct mount *afs_globalVFS = NULL;
20 int afs_pbuf_freecnt = -1;
21
22 #ifdef AFS_FBSD50_ENV
23 #define THREAD_OR_PROC struct thread *p
24 #else
25 #define THREAD_OR_PROC struct proc *p
26 #endif
27
28 extern int afs3_syscall();
29 extern int Afs_xsetgroups();
30 extern int afs_xioctl();
31
32 static sy_call_t *old_handler;
33
34
35 int
36 afs_init(struct vfsconf *vfc)
37 {
38     if (sysent[AFS_SYSCALL].sy_call != nosys
39         && sysent[AFS_SYSCALL].sy_call != lkmnosys) {
40         printf("AFS_SYSCALL in use. aborting\n");
41         return EBUSY;
42     }
43     osi_Init();
44     afs_pbuf_freecnt = nswbuf / 2 + 1;
45 #if 0
46     sysent[SYS_setgroups].sy_call = Afs_xsetgroups;
47     sysent[SYS_ioctl].sy_call = afs_xioctl;
48 #endif
49     old_handler = sysent[AFS_SYSCALL].sy_call;
50     sysent[AFS_SYSCALL].sy_call = afs3_syscall;
51     sysent[AFS_SYSCALL].sy_narg = 5;
52     return 0;
53 }
54
55 int
56 afs_uninit(struct vfsconf *vfc)
57 {
58     if (afs_globalVFS)
59         return EBUSY;
60 #if 0
61     sysent[SYS_ioctl].sy_call = ioctl;
62     sysent[SYS_setgroups].sy_call = setgroups;
63 #endif
64     sysent[AFS_SYSCALL].sy_narg = 0;
65     sysent[AFS_SYSCALL].sy_call = old_handler;
66     return 0;
67 }
68
69 int
70 afs_start(struct mount *mp, int flags, THREAD_OR_PROC)
71 {
72     return (0);                 /* nothing to do. ? */
73 }
74
75 int
76 #ifdef AFS_FBSD53_ENV
77 afs_omount(struct mount *mp, char *path, caddr_t data, struct thread *p)
78 #else
79 afs_omount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
80         THREAD_OR_PROC)
81 #endif
82 {
83     /* ndp contains the mounted-from device.  Just ignore it.
84      * we also don't care about our proc struct. */
85     size_t size;
86
87     if (mp->mnt_flag & MNT_UPDATE)
88         return EINVAL;
89
90     AFS_GLOCK();
91     AFS_STATCNT(afs_mount);
92
93     if (afs_globalVFS) {        /* Don't allow remounts. */
94         AFS_GUNLOCK();
95         return EBUSY;
96     }
97
98     afs_globalVFS = mp;
99     mp->vfs_bsize = 8192;
100     vfs_getnewfsid(mp);
101 #ifdef AFS_FBSD70_ENV /* XXX 70? */
102     MNT_ILOCK(mp);
103     mp->mnt_flag &= ~MNT_LOCAL;
104     mp->mnt_kern_flag |= MNTK_MPSAFE; /* solid steel */
105 #endif
106     mp->mnt_stat.f_iosize = 8192;
107
108     if (path != NULL)
109         copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
110     else
111         bcopy("/afs", mp->mnt_stat.f_mntonname, size = 4);
112     memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
113     memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
114     strcpy(mp->mnt_stat.f_mntfromname, "AFS");
115     /* null terminated string "AFS" will fit, just leave it be. */
116     strcpy(mp->mnt_stat.f_fstypename, "afs");
117 #ifdef AFS_FBSD70_ENV
118     MNT_IUNLOCK(mp);
119 #endif
120     AFS_GUNLOCK();
121     afs_statfs(mp, &mp->mnt_stat, p);
122
123     return 0;
124 }
125
126 #ifdef AFS_FBSD53_ENV
127 int
128 afs_mount(struct mount *mp, struct thread *td)
129 {
130     return afs_omount(mp, NULL, NULL, td);
131 }
132 #endif
133
134 #ifdef AFS_FBSD60_ENV
135 static int
136 afs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
137 {
138     return kernel_mount(ma, flags);
139 }
140 #endif
141
142 int
143 afs_unmount(struct mount *mp, int flags, THREAD_OR_PROC)
144 {
145
146     /*
147      * Release any remaining vnodes on this mount point.
148      * The `1' means that we hold one extra reference on
149      * the root vnode (this is just a guess right now).
150      * This has to be done outside the global lock.
151      */
152 #if defined(AFS_FBSD80_ENV)
153   /* do nothing */
154 #elif defined(AFS_FBSD53_ENV)
155     vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, p);
156 #else
157     vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0);
158 #endif
159     AFS_GLOCK();
160     AFS_STATCNT(afs_unmount);
161     afs_globalVFS = 0;
162     afs_shutdown();
163     AFS_GUNLOCK();
164
165     return 0;
166 }
167
168 int
169 #if defined(AFS_FBSD60_ENV)
170 afs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
171 #elif defined(AFS_FBSD53_ENV)
172 afs_root(struct mount *mp, struct vnode **vpp, struct thread *td)
173 #else
174 afs_root(struct mount *mp, struct vnode **vpp)
175 #endif
176 {
177     int error;
178     struct vrequest treq;
179     register struct vcache *tvp = 0;
180 #ifdef AFS_FBSD50_ENV
181 #ifndef AFS_FBSD53_ENV
182     struct thread *td = curthread;
183 #endif
184     struct ucred *cr = td->td_ucred;
185 #else
186     struct proc *p = curproc;
187     struct ucred *cr = p->p_cred->pc_ucred;
188 #endif
189
190     AFS_GLOCK();
191     AFS_STATCNT(afs_root);
192     crhold(cr);
193     if (afs_globalVp && (afs_globalVp->states & CStatd)) {
194         tvp = afs_globalVp;
195         error = 0;
196     } else {
197 tryagain:
198 #ifndef AFS_FBSD80_ENV
199         if (afs_globalVp) {
200             afs_PutVCache(afs_globalVp);
201             /* vrele() needed here or not? */
202             afs_globalVp = NULL;
203         }
204 #endif
205         if (!(error = afs_InitReq(&treq, cr)) && !(error = afs_CheckInit())) {
206             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
207             /* we really want this to stay around */
208             if (tvp)
209                 afs_globalVp = tvp;
210             else
211                 error = ENOENT;
212         }
213     }
214     if (tvp) {
215         struct vnode *vp = AFSTOV(tvp);
216
217 #ifdef AFS_FBSD50_ENV
218         ASSERT_VI_UNLOCKED(vp, "afs_root");
219 #endif
220         AFS_GUNLOCK();
221         /*
222          * I'm uncomfortable about this.  Shouldn't this happen at a
223          * higher level, and shouldn't we busy the top-level directory
224          * to prevent recycling?
225          */
226 #ifdef AFS_FBSD50_ENV
227         error = vget(vp, LK_EXCLUSIVE | LK_RETRY, td);
228         vp->v_vflag |= VV_ROOT;
229 #else
230         error = vget(vp, LK_EXCLUSIVE | LK_RETRY, p);
231         vp->v_flag |= VROOT;
232 #endif
233         AFS_GLOCK();
234         if (error != 0)
235                 goto tryagain;
236
237         afs_globalVFS = mp;
238         *vpp = vp;
239     }
240
241     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, tvp ? AFSTOV(tvp) : NULL,
242                ICL_TYPE_INT32, error);
243     AFS_GUNLOCK();
244     crfree(cr);
245     return error;
246 }
247
248 int
249 afs_statfs(struct mount *mp, struct statfs *abp, THREAD_OR_PROC)
250 {
251     AFS_GLOCK();
252     AFS_STATCNT(afs_statfs);
253
254     abp->f_bsize = mp->vfs_bsize;
255     abp->f_iosize = mp->vfs_bsize;
256
257     /* Fake a high number below to satisfy programs that use the statfs call
258      * to make sure that there's enough space in the device partition before
259      * storing something there.
260      */
261     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
262         abp->f_ffree = 2000000;
263
264     abp->f_fsid.val[0] = mp->mnt_stat.f_fsid.val[0];
265     abp->f_fsid.val[1] = mp->mnt_stat.f_fsid.val[1];
266     if (abp != &mp->mnt_stat) {
267         abp->f_type = mp->mnt_vfc->vfc_typenum;
268         memcpy((caddr_t) & abp->f_mntonname[0],
269                (caddr_t) mp->mnt_stat.f_mntonname, MNAMELEN);
270         memcpy((caddr_t) & abp->f_mntfromname[0],
271                (caddr_t) mp->mnt_stat.f_mntfromname, MNAMELEN);
272     }
273
274     AFS_GUNLOCK();
275     return 0;
276 }
277
278 int
279 #ifdef AFS_FBSD60_ENV
280 afs_sync(struct mount *mp, int waitfor, struct thread *td)
281 #else
282 afs_sync(struct mount *mp, int waitfor, struct ucred *cred, THREAD_OR_PROC)
283 #endif
284 {
285     return 0;
286 }
287
288 #ifdef AFS_FBSD60_ENV
289 struct vfsops afs_vfsops = {
290         .vfs_init =             afs_init,
291         .vfs_mount =            afs_mount,
292         .vfs_cmount =           afs_cmount,
293         .vfs_root =             afs_root,
294         .vfs_statfs =           afs_statfs,
295         .vfs_sync =             afs_sync,
296         .vfs_uninit =           afs_uninit,
297         .vfs_unmount =          afs_unmount,
298         .vfs_sysctl =           vfs_stdsysctl,
299 };
300 #else
301 struct vfsops afs_vfsops = {
302 #ifdef AFS_FBSD53_ENV
303     afs_mount,
304 #endif
305     afs_omount,
306     afs_start,
307     afs_unmount,
308     afs_root,
309     vfs_stdquotactl,
310     afs_statfs,
311     afs_sync,
312     vfs_stdvget,
313     vfs_stdfhtovp,
314     vfs_stdcheckexp,
315     vfs_stdvptofh,
316     afs_init,
317     afs_uninit,
318     vfs_stdextattrctl,
319 #ifdef AFS_FBSD50_ENV
320     vfs_stdsysctl,
321 #endif
322 };
323 #endif