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