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