FBSD: close race in afs_root
[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 #ifdef AFS_FBSD70_ENV /* XXX 70? */
127     MNT_ILOCK(mp);
128     mp->mnt_flag &= ~MNT_LOCAL;
129     mp->mnt_kern_flag |= MNTK_MPSAFE; /* solid steel */
130 #endif
131     mp->mnt_stat.f_iosize = 8192;
132
133     if (path != NULL)
134         copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
135     else
136         bcopy("/afs", mp->mnt_stat.f_mntonname, size = 4);
137     memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
138     memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
139     strcpy(mp->mnt_stat.f_mntfromname, "AFS");
140     /* null terminated string "AFS" will fit, just leave it be. */
141     strcpy(mp->mnt_stat.f_fstypename, "afs");
142 #ifdef AFS_FBSD70_ENV
143     MNT_IUNLOCK(mp);
144 #endif
145     AFS_GUNLOCK();
146 #ifdef AFS_FBSD80_ENV
147     afs_statfs(mp, &mp->mnt_stat);
148 #else
149     afs_statfs(mp, &mp->mnt_stat, p);
150 #endif
151
152     return 0;
153 }
154
155 #ifdef AFS_FBSD53_ENV
156 int
157 #ifdef AFS_FBSD80_ENV
158 afs_mount(struct mount *mp)
159 #else
160 afs_mount(struct mount *mp, struct thread *td)
161 #endif
162 {
163 #ifdef AFS_FBSD80_ENV
164     return afs_omount(mp, NULL, NULL);
165 #else
166     return afs_omount(mp, NULL, NULL, td);
167 #endif
168 }
169 #endif
170
171 #ifdef AFS_FBSD60_ENV
172 static int
173 #ifdef AFS_FBSD80_ENV
174 afs_cmount(struct mntarg *ma, void *data, int flags)
175 #else
176 afs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
177 #endif
178 {
179     return kernel_mount(ma, flags);
180 }
181 #endif
182
183 int
184 #ifdef AFS_FBSD80_ENV
185 afs_unmount(struct mount *mp, int flags)
186 #else
187 afs_unmount(struct mount *mp, int flags, struct thread *p)
188 #endif
189 {
190     int error = 0;
191
192     /*
193      * Release any remaining vnodes on this mount point.
194      * The `1' means that we hold one extra reference on
195      * the root vnode (this is just a guess right now).
196      * This has to be done outside the global lock.
197      */
198 #if defined(AFS_FBSD80_ENV)
199     error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, curthread);
200 #elif defined(AFS_FBSD53_ENV)
201     error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, p);
202 #else
203     error = vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0);
204 #endif
205     if (error)
206         goto out;
207     AFS_GLOCK();
208     AFS_STATCNT(afs_unmount);
209     afs_globalVFS = 0;
210     afs_shutdown();
211     AFS_GUNLOCK();
212
213 out:
214     return error;
215 }
216
217 int
218 #if defined(AFS_FBSD80_ENV)
219 afs_root(struct mount *mp, int flags, struct vnode **vpp)
220 #elif defined(AFS_FBSD60_ENV)
221 afs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
222 #elif defined(AFS_FBSD53_ENV)
223 afs_root(struct mount *mp, struct vnode **vpp, struct thread *td)
224 #else
225 afs_root(struct mount *mp, struct vnode **vpp)
226 #endif
227 {
228     int error;
229     struct vrequest treq;
230     struct vcache *tvp = 0;
231     struct vcache *gvp;
232 #if !defined(AFS_FBSD53_ENV) || defined(AFS_FBSD80_ENV)
233     struct thread *td = curthread;
234 #endif
235     struct ucred *cr = osi_curcred();
236
237     AFS_GLOCK();
238     AFS_STATCNT(afs_root);
239     crhold(cr);
240 tryagain:
241     if (afs_globalVp && (afs_globalVp->f.states & CStatd)) {
242         tvp = afs_globalVp;
243         error = 0;
244     } else {
245         if (!(error = afs_InitReq(&treq, cr)) && !(error = afs_CheckInit())) {
246             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
247             /* we really want this to stay around */
248             if (tvp) {
249                 gvp = afs_globalVp;
250                 afs_globalVp = tvp;
251                 if (gvp) {
252                     afs_PutVCache(gvp);
253                     if (tvp != afs_globalVp) {
254                         /* someone raced us and won */
255                         afs_PutVCache(tvp);
256                         goto tryagain;
257                     }
258                 }
259             } else
260                 error = ENOENT;
261         }
262     }
263     if (tvp) {
264         struct vnode *vp = AFSTOV(tvp);
265
266         ASSERT_VI_UNLOCKED(vp, "afs_root");
267         AFS_GUNLOCK();
268         error = vget(vp, LK_EXCLUSIVE | LK_RETRY, td);
269         AFS_GLOCK();
270         /* we dropped the glock, so re-check everything it had serialized */
271         if (!afs_globalVp || !(afs_globalVp->f.states & CStatd) ||
272                 tvp != afs_globalVp) {
273             vput(vp);
274             afs_PutVCache(tvp);
275             goto tryagain;
276         }
277         if (error != 0)
278             goto tryagain;
279         /*
280          * I'm uncomfortable about this.  Shouldn't this happen at a
281          * higher level, and shouldn't we busy the top-level directory
282          * to prevent recycling?
283          */
284         vp->v_vflag |= VV_ROOT;
285
286         afs_globalVFS = mp;
287         *vpp = vp;
288     }
289
290     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, tvp ? AFSTOV(tvp) : NULL,
291                ICL_TYPE_INT32, error);
292     AFS_GUNLOCK();
293     crfree(cr);
294     return error;
295 }
296
297 int
298 #ifdef AFS_FBSD80_ENV
299 afs_statfs(struct mount *mp, struct statfs *abp)
300 #else
301 afs_statfs(struct mount *mp, struct statfs *abp, struct thread *p)
302 #endif
303 {
304     AFS_GLOCK();
305     AFS_STATCNT(afs_statfs);
306
307     abp->f_bsize = mp->vfs_bsize;
308     abp->f_iosize = mp->vfs_bsize;
309
310     /* Fake a high number below to satisfy programs that use the statfs call
311      * to make sure that there's enough space in the device partition before
312      * storing something there.
313      */
314     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
315         abp->f_ffree = 2000000;
316
317     abp->f_fsid.val[0] = mp->mnt_stat.f_fsid.val[0];
318     abp->f_fsid.val[1] = mp->mnt_stat.f_fsid.val[1];
319     if (abp != &mp->mnt_stat) {
320         abp->f_type = mp->mnt_vfc->vfc_typenum;
321         memcpy((caddr_t) & abp->f_mntonname[0],
322                (caddr_t) mp->mnt_stat.f_mntonname, MNAMELEN);
323         memcpy((caddr_t) & abp->f_mntfromname[0],
324                (caddr_t) mp->mnt_stat.f_mntfromname, MNAMELEN);
325     }
326
327     AFS_GUNLOCK();
328     return 0;
329 }
330
331 int
332 #if defined(AFS_FBSD80_ENV)
333 afs_sync(struct mount *mp, int waitfor)
334 #elif defined(AFS_FBSD60_ENV)
335 afs_sync(struct mount *mp, int waitfor, struct thread *td)
336 #else
337 afs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *p)
338 #endif
339 {
340     return 0;
341 }
342
343 #ifdef AFS_FBSD60_ENV
344 struct vfsops afs_vfsops = {
345         .vfs_init =             afs_init,
346         .vfs_mount =            afs_mount,
347         .vfs_cmount =           afs_cmount,
348         .vfs_root =             afs_root,
349         .vfs_statfs =           afs_statfs,
350         .vfs_sync =             afs_sync,
351         .vfs_uninit =           afs_uninit,
352         .vfs_unmount =          afs_unmount,
353         .vfs_sysctl =           vfs_stdsysctl,
354 };
355 #else
356 struct vfsops afs_vfsops = {
357 #ifdef AFS_FBSD53_ENV
358     afs_mount,
359 #endif
360     afs_omount,
361     afs_start,
362     afs_unmount,
363     afs_root,
364     vfs_stdquotactl,
365     afs_statfs,
366     afs_sync,
367     vfs_stdvget,
368     vfs_stdfhtovp,
369     vfs_stdcheckexp,
370     vfs_stdvptofh,
371     afs_init,
372     afs_uninit,
373     vfs_stdextattrctl,
374     vfs_stdsysctl,
375 };
376 #endif