d8dabd1c207ca34201ab572209b0fa916920517c
[openafs.git] / src / afs / FBSD / osi_vfsops.c
1 #include <afsconfig.h>
2 #include <afs/param.h>
3
4
5 #include <afs/sysincludes.h>    /* Standard vendor system headers */
6 #include <afsincludes.h>        /* Afs-based standard headers */
7 #include <afs/afs_stats.h>      /* statistics */
8 #include <sys/malloc.h>
9 #include <sys/namei.h>
10 #include <sys/conf.h>
11 #include <sys/module.h>
12 #include <sys/sysproto.h>
13 #include <sys/syscall.h>
14 #include <sys/sysent.h>
15
16 struct vcache *afs_globalVp = NULL;
17 struct mount *afs_globalVFS = NULL;
18 int afs_pbuf_freecnt = -1;
19
20 extern int Afs_xsetgroups();
21 extern int afs_xioctl();
22
23 #if !defined(AFS_FBSD90_ENV) && !defined(AFS_FBSD82_ENV)
24 static sy_call_t *old_handler;
25 #else
26 static struct sysent old_sysent;
27
28 static struct sysent afs_sysent = {
29     5,                  /* int sy_narg */
30     (sy_call_t *) afs3_syscall, /* sy_call_t *sy_call */
31 #ifdef AFS_FBSD60_ENV
32     AUE_NULL,           /* au_event_t sy_auevent */
33 #ifdef AFS_FBSD70_ENV
34     NULL,               /* systrace_args_funt_t sy_systrace_args_func */
35     0,                  /* u_int32_t sy_entry */
36     0,                  /* u_int32_t sy_return */
37 #ifdef AFS_FBSD90_ENV
38     0,                  /* u_int32_t sy_flags */
39     0                   /* u_int32_t sy_thrcnt */
40 #endif
41 #endif
42 #endif /* FBSD60 */
43 };
44 #endif /* FBSD90 */
45
46 int
47 afs_init(struct vfsconf *vfc)
48 {
49     int code;
50     int offset = AFS_SYSCALL;
51 #if defined(AFS_FBSD90_ENV) || defined(AFS_FBSD82_ENV)
52 # if defined(FBSD_SYSCALL_REGISTER_FOUR_ARGS)
53     code = syscall_register(&offset, &afs_sysent, &old_sysent, 0);
54 # else
55     code = syscall_register(&offset, &afs_sysent, &old_sysent);
56 # endif
57     if (code) {
58         printf("AFS_SYSCALL in use, error %i. aborting\n", code);
59         return code;
60     }
61 #else
62     if (sysent[AFS_SYSCALL].sy_call != (sy_call_t *)nosys
63         && sysent[AFS_SYSCALL].sy_call != (sy_call_t *)lkmnosys) {
64         printf("AFS_SYSCALL in use. aborting\n");
65         return EBUSY;
66     }
67 #endif
68     osi_Init();
69     afs_pbuf_freecnt = nswbuf / 2 + 1;
70 #if !defined(AFS_FBSD90_ENV) && !defined(AFS_FBSD82_ENV)
71     old_handler = sysent[AFS_SYSCALL].sy_call;
72     sysent[AFS_SYSCALL].sy_call = afs3_syscall;
73     sysent[AFS_SYSCALL].sy_narg = 5;
74 #endif
75     return 0;
76 }
77
78 int
79 afs_uninit(struct vfsconf *vfc)
80 {
81 #if defined(AFS_FBSD90_ENV) || defined(AFS_FBSD82_ENV)
82     int offset = AFS_SYSCALL;
83 #endif
84
85     if (afs_globalVFS)
86         return EBUSY;
87 #if defined(AFS_FBSD90_ENV) || defined(AFS_FBSD82_ENV)
88     syscall_deregister(&offset, &old_sysent);
89 #else
90     sysent[AFS_SYSCALL].sy_narg = 0;
91     sysent[AFS_SYSCALL].sy_call = old_handler;
92 #endif
93     return 0;
94 }
95
96 int
97 afs_start(struct mount *mp, int flags, struct thread *p)
98 {
99     return (0);                 /* nothing to do. ? */
100 }
101
102 int
103 #if defined(AFS_FBSD80_ENV)
104 afs_omount(struct mount *mp, char *path, caddr_t data)
105 #elif defined(AFS_FBSD53_ENV)
106 afs_omount(struct mount *mp, char *path, caddr_t data, struct thread *p)
107 #else
108 afs_omount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
109         struct thread *p)
110 #endif
111 {
112     /* ndp contains the mounted-from device.  Just ignore it.
113      * we also don't care about our thread struct. */
114     size_t size;
115
116     if (mp->mnt_flag & MNT_UPDATE)
117         return EINVAL;
118
119     AFS_GLOCK();
120     AFS_STATCNT(afs_mount);
121
122     if (afs_globalVFS) {        /* Don't allow remounts. */
123         AFS_GUNLOCK();
124         return EBUSY;
125     }
126
127     afs_globalVFS = mp;
128     mp->vfs_bsize = 8192;
129     vfs_getnewfsid(mp);
130     /*
131      * This is kind of ugly, as the interlock has grown to encompass
132      * more fields over time and there's not a good way to group the
133      * code without duplication.
134      */
135 #ifdef AFS_FBSD62_ENV
136     MNT_ILOCK(mp);
137 #endif
138     mp->mnt_flag &= ~MNT_LOCAL;
139 #if defined(AFS_FBSD61_ENV) && !defined(AFS_FBSD62_ENV)
140     MNT_ILOCK(mp);
141 #endif
142 #if __FreeBSD_version < 1000021
143     mp->mnt_kern_flag |= MNTK_MPSAFE; /* solid steel */
144 #endif
145 #ifndef AFS_FBSD61_ENV
146     MNT_ILOCK(mp);
147 #endif
148     /*
149      * XXX mnt_stat "is considered stable as long as a ref is held".
150      * We should check that we hold the only ref.
151      */
152     mp->mnt_stat.f_iosize = 8192;
153
154     if (path != NULL)
155         copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
156     else
157         bcopy("/afs", mp->mnt_stat.f_mntonname, size = 4);
158     memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
159     memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
160     strcpy(mp->mnt_stat.f_mntfromname, "AFS");
161     /* null terminated string "AFS" will fit, just leave it be. */
162     strcpy(mp->mnt_stat.f_fstypename, "afs");
163     MNT_IUNLOCK(mp);
164     AFS_GUNLOCK();
165 #ifdef AFS_FBSD80_ENV
166     afs_statfs(mp, &mp->mnt_stat);
167 #else
168     afs_statfs(mp, &mp->mnt_stat, p);
169 #endif
170
171     return 0;
172 }
173
174 #ifdef AFS_FBSD53_ENV
175 int
176 #ifdef AFS_FBSD80_ENV
177 afs_mount(struct mount *mp)
178 #else
179 afs_mount(struct mount *mp, struct thread *td)
180 #endif
181 {
182 #ifdef AFS_FBSD80_ENV
183     return afs_omount(mp, NULL, NULL);
184 #else
185     return afs_omount(mp, NULL, NULL, td);
186 #endif
187 }
188 #endif
189
190 #ifdef AFS_FBSD60_ENV
191 static int
192 #if (__FreeBSD_version >= 900503 && __FreeBSD_version < 1000000) || __FreeBSD_version >= 1000004
193 afs_cmount(struct mntarg *ma, void *data, uint64_t flags)
194 #elif defined(AFS_FBSD80_ENV)
195 afs_cmount(struct mntarg *ma, void *data, int flags)
196 #else
197 afs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
198 #endif
199 {
200     return kernel_mount(ma, flags);
201 }
202 #endif
203
204 int
205 #ifdef AFS_FBSD80_ENV
206 afs_unmount(struct mount *mp, int flags)
207 #else
208 afs_unmount(struct mount *mp, int flags, struct thread *p)
209 #endif
210 {
211     int error = 0;
212
213     AFS_GLOCK();
214     if (afs_globalVp &&
215         ((flags & MNT_FORCE) || !VREFCOUNT_GT(afs_globalVp, 1))) {
216         /* Put back afs_root's ref */
217         struct vcache *gvp = afs_globalVp;
218         afs_globalVp = NULL;
219         afs_PutVCache(gvp);
220     }
221     if (afs_globalVp)
222         error = EBUSY;
223     AFS_GUNLOCK();
224
225     if (!error) {
226         /*
227          * Release any remaining vnodes on this mount point. The second
228          * argument is how many refs we hold on the root vnode. Since we
229          * released our reference to the root vnode up above, give 0.
230          */
231 #if defined(AFS_FBSD80_ENV)
232         error = vflush(mp, 0, (flags & MNT_FORCE) ? FORCECLOSE : 0, curthread);
233 #elif defined(AFS_FBSD53_ENV)
234         error = vflush(mp, 0, (flags & MNT_FORCE) ? FORCECLOSE : 0, p);
235 #else
236         error = vflush(mp, 0, (flags & MNT_FORCE) ? FORCECLOSE : 0);
237 #endif
238     }
239     if (error)
240         goto out;
241     AFS_GLOCK();
242     AFS_STATCNT(afs_unmount);
243     afs_globalVFS = 0;
244     afs_shutdown();
245     AFS_GUNLOCK();
246
247 out:
248     return error;
249 }
250
251 int
252 #if defined(AFS_FBSD80_ENV)
253 afs_root(struct mount *mp, int flags, struct vnode **vpp)
254 #elif defined(AFS_FBSD60_ENV)
255 afs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
256 #elif defined(AFS_FBSD53_ENV)
257 afs_root(struct mount *mp, struct vnode **vpp, struct thread *td)
258 #else
259 afs_root(struct mount *mp, struct vnode **vpp)
260 #endif
261 {
262     int error;
263     struct vrequest treq;
264     struct vcache *tvp = 0;
265     struct vcache *gvp;
266 #if !defined(AFS_FBSD53_ENV) || defined(AFS_FBSD80_ENV)
267     struct thread *td = curthread;
268 #endif
269     struct ucred *cr = osi_curcred();
270
271     AFS_GLOCK();
272     AFS_STATCNT(afs_root);
273     crhold(cr);
274 tryagain:
275     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
276         tvp = afs_globalVp;
277         error = 0;
278     } else {
279         if (!(error = afs_InitReq(&treq, cr)) && !(error = afs_CheckInit())) {
280             tvp = afs_GetVCache(&afs_rootFid, &treq);
281             /* we really want this to stay around */
282             if (tvp) {
283                 gvp = afs_globalVp;
284                 afs_globalVp = tvp;
285                 if (gvp) {
286                     afs_PutVCache(gvp);
287                     if (tvp != afs_globalVp) {
288                         /* someone raced us and won */
289                         afs_PutVCache(tvp);
290                         goto tryagain;
291                     }
292                 }
293             } else
294                 error = EIO;
295         }
296     }
297     if (tvp) {
298         struct vnode *vp = AFSTOV(tvp);
299
300         ASSERT_VI_UNLOCKED(vp, "afs_root");
301         AFS_GUNLOCK();
302         error = vget(vp, LK_EXCLUSIVE | LK_RETRY, td);
303         AFS_GLOCK();
304         /* we dropped the glock, so re-check everything it had serialized */
305         if (!afs_globalVp || !(afs_globalVp->f.states & CStatd) ||
306                 tvp != afs_globalVp) {
307             vput(vp);
308             afs_PutVCache(tvp);
309             goto tryagain;
310         }
311         if (error != 0)
312             goto tryagain;
313         /*
314          * I'm uncomfortable about this.  Shouldn't this happen at a
315          * higher level, and shouldn't we busy the top-level directory
316          * to prevent recycling?
317          */
318         vp->v_vflag |= VV_ROOT;
319
320         afs_globalVFS = mp;
321         *vpp = vp;
322     }
323
324     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, tvp ? AFSTOV(tvp) : NULL,
325                ICL_TYPE_INT32, error);
326     AFS_GUNLOCK();
327     crfree(cr);
328     return error;
329 }
330
331 int
332 #ifdef AFS_FBSD80_ENV
333 afs_statfs(struct mount *mp, struct statfs *abp)
334 #else
335 afs_statfs(struct mount *mp, struct statfs *abp, struct thread *p)
336 #endif
337 {
338     AFS_GLOCK();
339     AFS_STATCNT(afs_statfs);
340
341     abp->f_bsize = mp->vfs_bsize;
342     abp->f_iosize = mp->vfs_bsize;
343
344     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
345         abp->f_ffree = AFS_VFS_FAKEFREE;
346
347     abp->f_fsid.val[0] = mp->mnt_stat.f_fsid.val[0];
348     abp->f_fsid.val[1] = mp->mnt_stat.f_fsid.val[1];
349     if (abp != &mp->mnt_stat) {
350         abp->f_type = mp->mnt_vfc->vfc_typenum;
351         memcpy((caddr_t) & abp->f_mntonname[0],
352                (caddr_t) mp->mnt_stat.f_mntonname, MNAMELEN);
353         memcpy((caddr_t) & abp->f_mntfromname[0],
354                (caddr_t) mp->mnt_stat.f_mntfromname, MNAMELEN);
355     }
356
357     AFS_GUNLOCK();
358     return 0;
359 }
360
361 int
362 #if defined(AFS_FBSD80_ENV)
363 afs_sync(struct mount *mp, int waitfor)
364 #elif defined(AFS_FBSD60_ENV)
365 afs_sync(struct mount *mp, int waitfor, struct thread *td)
366 #else
367 afs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *p)
368 #endif
369 {
370     return 0;
371 }
372
373 #ifdef AFS_FBSD60_ENV
374 struct vfsops afs_vfsops = {
375         .vfs_init =             afs_init,
376         .vfs_mount =            afs_mount,
377         .vfs_cmount =           afs_cmount,
378         .vfs_root =             afs_root,
379         .vfs_statfs =           afs_statfs,
380         .vfs_sync =             afs_sync,
381         .vfs_uninit =           afs_uninit,
382         .vfs_unmount =          afs_unmount,
383         .vfs_sysctl =           vfs_stdsysctl,
384 };
385 #else
386 struct vfsops afs_vfsops = {
387 #ifdef AFS_FBSD53_ENV
388     afs_mount,
389 #endif
390     afs_omount,
391     afs_start,
392     afs_unmount,
393     afs_root,
394     vfs_stdquotactl,
395     afs_statfs,
396     afs_sync,
397     vfs_stdvget,
398     vfs_stdfhtovp,
399     vfs_stdcheckexp,
400     vfs_stdvptofh,
401     afs_init,
402     afs_uninit,
403     vfs_stdextattrctl,
404     vfs_stdsysctl,
405 };
406 #endif