18138c50c8fffe5b00c7131632b7412dfdbb1fa0
[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 #ifdef AFS_FBSD53_ENV
76 int
77 afs_mount(struct mount *mp, struct thread *td)
78 {
79     int afs_omount(struct mount *mp, char *path, caddr_t data, struct thread *p);
80
81     return afs_omount(mp, NULL, NULL, td);
82 }
83 #endif
84
85 int
86 #ifdef AFS_FBSD53_ENV
87 afs_omount(struct mount *mp, char *path, caddr_t data, struct thread *p)
88 #else
89 afs_omount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
90         THREAD_OR_PROC)
91 #endif
92 {
93     /* ndp contains the mounted-from device.  Just ignore it.
94      * we also don't care about our proc struct. */
95     size_t size;
96
97     if (mp->mnt_flag & MNT_UPDATE)
98         return EINVAL;
99
100     AFS_GLOCK();
101     AFS_STATCNT(afs_mount);
102
103     if (afs_globalVFS) {        /* Don't allow remounts. */
104         AFS_GUNLOCK();
105         return EBUSY;
106     }
107
108     afs_globalVFS = mp;
109     mp->vfs_bsize = 8192;
110     vfs_getnewfsid(mp);
111     mp->mnt_stat.f_iosize = 8192;
112
113     if (path)
114         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
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     AFS_GUNLOCK();
121     (void)afs_statfs(mp, &mp->mnt_stat, p);
122     return 0;
123 }
124
125 #ifdef AFS_FBSD60_ENV
126 static int
127 afs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
128 {
129     return kernel_mount(ma, flags);
130 }
131 #endif
132
133 int
134 afs_unmount(struct mount *mp, int flags, THREAD_OR_PROC)
135 {
136
137     /*
138      * Release any remaining vnodes on this mount point.
139      * The `1' means that we hold one extra reference on
140      * the root vnode (this is just a guess right now).
141      * This has to be done outside the global lock.
142      */
143 #ifdef AFS_FBSD53_ENV
144     vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0, p);
145 #else
146     vflush(mp, 1, (flags & MNT_FORCE) ? FORCECLOSE : 0);
147 #endif
148     AFS_GLOCK();
149     AFS_STATCNT(afs_unmount);
150     afs_globalVFS = 0;
151     afs_shutdown();
152     AFS_GUNLOCK();
153
154     return 0;
155 }
156
157 int
158 #ifdef AFS_FBSD53_ENV
159 afs_root(struct mount *mp, struct vnode **vpp, struct thread *td)
160 #else
161 afs_root(struct mount *mp, struct vnode **vpp)
162 #endif
163 {
164     int error;
165     struct vrequest treq;
166     register struct vcache *tvp = 0;
167 #ifdef AFS_FBSD50_ENV
168 #ifndef AFS_FBSD53_ENV
169     struct thread *td = curthread;
170 #endif
171     struct ucred *cr = td->td_ucred;
172 #else
173     struct proc *p = curproc;
174     struct ucred *cr = p->p_cred->pc_ucred;
175 #endif
176
177     AFS_GLOCK();
178     AFS_STATCNT(afs_root);
179     crhold(cr);
180     if (afs_globalVp && (afs_globalVp->states & CStatd)) {
181         tvp = afs_globalVp;
182         error = 0;
183     } else {
184 tryagain:
185         if (afs_globalVp) {
186             afs_PutVCache(afs_globalVp);
187             /* vrele() needed here or not? */
188             afs_globalVp = NULL;
189         }
190
191         if (!(error = afs_InitReq(&treq, cr)) && !(error = afs_CheckInit())) {
192             tvp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
193             /* we really want this to stay around */
194             if (tvp)
195                 afs_globalVp = tvp;
196             else
197                 error = ENOENT;
198         }
199     }
200     if (tvp) {
201         struct vnode *vp = AFSTOV(tvp);
202
203 #ifdef AFS_FBSD50_ENV
204         ASSERT_VI_UNLOCKED(vp, "afs_root");
205 #endif
206         AFS_GUNLOCK();
207         /*
208          * I'm uncomfortable about this.  Shouldn't this happen at a
209          * higher level, and shouldn't we busy the top-level directory
210          * to prevent recycling?
211          */
212 #ifdef AFS_FBSD50_ENV
213         error = vget(vp, LK_EXCLUSIVE | LK_RETRY, td);
214         vp->v_vflag |= VV_ROOT;
215 #else
216         error = vget(vp, LK_EXCLUSIVE | LK_RETRY, p);
217         vp->v_flag |= VROOT;
218 #endif
219         AFS_GLOCK();
220         if (error != 0)
221                 goto tryagain;
222
223         afs_globalVFS = mp;
224         *vpp = vp;
225     }
226
227     afs_Trace2(afs_iclSetp, CM_TRACE_VFSROOT, ICL_TYPE_POINTER, *vpp,
228                ICL_TYPE_INT32, error);
229     AFS_GUNLOCK();
230     crfree(cr);
231     return error;
232 }
233
234 int
235 afs_statfs(struct mount *mp, struct statfs *abp, THREAD_OR_PROC)
236 {
237     AFS_GLOCK();
238     AFS_STATCNT(afs_statfs);
239
240     abp->f_bsize = mp->vfs_bsize;
241     abp->f_iosize = mp->vfs_bsize;
242
243     /* Fake a high number below to satisfy programs that use the statfs call
244      * to make sure that there's enough space in the device partition before
245      * storing something there.
246      */
247     abp->f_blocks = abp->f_bfree = abp->f_bavail = abp->f_files =
248         abp->f_ffree = 2000000;
249
250     abp->f_fsid.val[0] = mp->mnt_stat.f_fsid.val[0];
251     abp->f_fsid.val[1] = mp->mnt_stat.f_fsid.val[1];
252     if (abp != &mp->mnt_stat) {
253         abp->f_type = mp->mnt_vfc->vfc_typenum;
254         memcpy((caddr_t) & abp->f_mntonname[0],
255                (caddr_t) mp->mnt_stat.f_mntonname, MNAMELEN);
256         memcpy((caddr_t) & abp->f_mntfromname[0],
257                (caddr_t) mp->mnt_stat.f_mntfromname, MNAMELEN);
258     }
259
260     AFS_GUNLOCK();
261     return 0;
262 }
263
264 int
265 #ifdef AFS_FBSD60_ENV
266 afs_sync(struct mount *mp, int waitfor, struct thread *td)
267 #else
268 afs_sync(struct mount *mp, int waitfor, struct ucred *cred, THREAD_OR_PROC)
269 #endif
270 {
271     return 0;
272 }
273
274 #ifdef AFS_FBSD60_ENV
275 struct vfsops afs_vfsops = {
276         .vfs_init =             afs_init,
277         .vfs_mount =            afs_mount,
278         .vfs_cmount =           afs_cmount,
279         .vfs_root =             afs_root,
280         .vfs_statfs =           afs_statfs,
281         .vfs_sync =             afs_sync,
282         .vfs_uninit =           afs_uninit,
283         .vfs_unmount =          afs_unmount,
284         .vfs_sysctl =           vfs_stdsysctl,
285 };
286 #else
287 struct vfsops afs_vfsops = {
288 #ifdef AFS_FBSD53_ENV
289     afs_mount,
290 #endif
291     afs_omount,
292     afs_start,
293     afs_unmount,
294     afs_root,
295     vfs_stdquotactl,
296     afs_statfs,
297     afs_sync,
298     vfs_stdvget,
299     vfs_stdfhtovp,
300     vfs_stdcheckexp,
301     vfs_stdvptofh,
302     afs_init,
303     afs_uninit,
304     vfs_stdextattrctl,
305 #ifdef AFS_FBSD50_ENV
306     vfs_stdsysctl,
307 #endif
308 };
309 #endif