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