puffs_vfsops.c revision 1.28.6.7 1 1.28.6.7 ad /* $NetBSD: puffs_vfsops.c,v 1.28.6.7 2007/06/17 21:31:10 ad Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2005, 2006 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by the
7 1.1 pooka * Google Summer of Code program and the Ulla Tuominen Foundation.
8 1.1 pooka * The Google SoC project was mentored by Bill Studenmund.
9 1.1 pooka *
10 1.1 pooka * Redistribution and use in source and binary forms, with or without
11 1.1 pooka * modification, are permitted provided that the following conditions
12 1.1 pooka * are met:
13 1.1 pooka * 1. Redistributions of source code must retain the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer.
15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pooka * notice, this list of conditions and the following disclaimer in the
17 1.1 pooka * documentation and/or other materials provided with the distribution.
18 1.1 pooka *
19 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 pooka * SUCH DAMAGE.
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka #include <sys/cdefs.h>
33 1.28.6.7 ad __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.28.6.7 2007/06/17 21:31:10 ad Exp $");
34 1.1 pooka
35 1.1 pooka #include <sys/param.h>
36 1.1 pooka #include <sys/mount.h>
37 1.1 pooka #include <sys/malloc.h>
38 1.1 pooka #include <sys/extattr.h>
39 1.1 pooka #include <sys/queue.h>
40 1.1 pooka #include <sys/vnode.h>
41 1.1 pooka #include <sys/dirent.h>
42 1.1 pooka #include <sys/kauth.h>
43 1.26 pooka #include <sys/fstrans.h>
44 1.28.6.3 ad #include <sys/proc.h>
45 1.1 pooka
46 1.1 pooka #include <lib/libkern/libkern.h>
47 1.1 pooka
48 1.1 pooka #include <fs/puffs/puffs_msgif.h>
49 1.1 pooka #include <fs/puffs/puffs_sys.h>
50 1.1 pooka
51 1.28.6.5 ad #include <nfs/nfsproto.h> /* for fh sizes */
52 1.28.6.5 ad
53 1.1 pooka VFS_PROTOS(puffs);
54 1.1 pooka
55 1.28.6.4 ad MALLOC_JUSTDEFINE(M_PUFFS, "puffs", "Pass-to-Userspace Framework File System");
56 1.1 pooka
57 1.22 pooka #ifndef PUFFS_PNODEBUCKETS
58 1.22 pooka #define PUFFS_PNODEBUCKETS 256
59 1.22 pooka #endif
60 1.22 pooka #ifndef PUFFS_MAXPNODEBUCKETS
61 1.28.6.5 ad #define PUFFS_MAXPNODEBUCKETS 8192
62 1.22 pooka #endif
63 1.28.6.5 ad int puffs_pnodebuckets_default = PUFFS_PNODEBUCKETS;
64 1.28.6.5 ad int puffs_maxpnodebuckets = PUFFS_MAXPNODEBUCKETS;
65 1.22 pooka
66 1.1 pooka int
67 1.1 pooka puffs_mount(struct mount *mp, const char *path, void *data,
68 1.1 pooka struct nameidata *ndp, struct lwp *l)
69 1.1 pooka {
70 1.22 pooka struct puffs_mount *pmp = NULL;
71 1.28.6.5 ad struct puffs_kargs *args;
72 1.17 pooka char namebuf[PUFFSNAMESIZE+sizeof(PUFFS_NAMEPREFIX)+1]; /* spooky */
73 1.22 pooka int error = 0, i;
74 1.1 pooka
75 1.1 pooka if (mp->mnt_flag & MNT_GETARGS) {
76 1.1 pooka pmp = MPTOPUFFSMP(mp);
77 1.28.6.5 ad return copyout(&pmp->pmp_args,data,sizeof(struct puffs_kargs));
78 1.1 pooka }
79 1.1 pooka
80 1.1 pooka /* update is not supported currently */
81 1.1 pooka if (mp->mnt_flag & MNT_UPDATE)
82 1.1 pooka return EOPNOTSUPP;
83 1.1 pooka
84 1.1 pooka /*
85 1.1 pooka * We need the file system name
86 1.1 pooka */
87 1.1 pooka if (!data)
88 1.1 pooka return EINVAL;
89 1.1 pooka
90 1.28.6.5 ad MALLOC(args, struct puffs_kargs *, sizeof(struct puffs_kargs),
91 1.17 pooka M_PUFFS, M_WAITOK);
92 1.17 pooka
93 1.28.6.5 ad error = copyin(data, args, sizeof(struct puffs_kargs));
94 1.1 pooka if (error)
95 1.17 pooka goto out;
96 1.17 pooka
97 1.17 pooka /* devel phase */
98 1.17 pooka if (args->pa_vers != (PUFFSVERSION | PUFFSDEVELVERS)) {
99 1.17 pooka printf("puffs_mount: development version mismatch\n");
100 1.17 pooka error = EINVAL;
101 1.17 pooka goto out;
102 1.17 pooka }
103 1.1 pooka
104 1.8 pooka /* nuke spy bits */
105 1.17 pooka args->pa_flags &= PUFFS_KFLAG_MASK;
106 1.8 pooka
107 1.28.6.5 ad /* sanitize file handle length */
108 1.28.6.5 ad if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) {
109 1.28.6.5 ad printf("puffs_mount: handle size %zu too large\n",
110 1.28.6.5 ad args->pa_fhsize);
111 1.28.6.5 ad error = EINVAL;
112 1.28.6.5 ad goto out;
113 1.28.6.5 ad }
114 1.28.6.5 ad /* sanity check file handle max sizes */
115 1.28.6.5 ad if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) {
116 1.28.6.5 ad size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize);
117 1.28.6.5 ad
118 1.28.6.5 ad if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) {
119 1.28.6.5 ad if (NFSX_FHTOOBIG_P(kfhsize, 0)) {
120 1.28.6.5 ad printf("puffs_mount: fhsize larger than "
121 1.28.6.5 ad "NFSv2 max %d\n",
122 1.28.6.5 ad PUFFS_FROMFHSIZE(NFSX_V2FH));
123 1.28.6.5 ad error = EINVAL;
124 1.28.6.5 ad goto out;
125 1.28.6.5 ad }
126 1.28.6.5 ad }
127 1.28.6.5 ad
128 1.28.6.5 ad if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) {
129 1.28.6.5 ad if (NFSX_FHTOOBIG_P(kfhsize, 1)) {
130 1.28.6.5 ad printf("puffs_mount: fhsize larger than "
131 1.28.6.5 ad "NFSv3 max %d\n",
132 1.28.6.5 ad PUFFS_FROMFHSIZE(NFSX_V3FHMAX));
133 1.28.6.5 ad error = EINVAL;
134 1.28.6.5 ad goto out;
135 1.28.6.5 ad }
136 1.28.6.5 ad }
137 1.28.6.5 ad }
138 1.28.6.5 ad
139 1.1 pooka /* build real name */
140 1.17 pooka (void)strlcpy(namebuf, PUFFS_NAMEPREFIX, sizeof(namebuf));
141 1.17 pooka (void)strlcat(namebuf, args->pa_name, sizeof(namebuf));
142 1.1 pooka
143 1.1 pooka /* inform user server if it got the max request size it wanted */
144 1.17 pooka if (args->pa_maxreqlen == 0 || args->pa_maxreqlen > PUFFS_REQ_MAXSIZE)
145 1.17 pooka args->pa_maxreqlen = PUFFS_REQ_MAXSIZE;
146 1.17 pooka else if (args->pa_maxreqlen < PUFFS_REQSTRUCT_MAX)
147 1.17 pooka args->pa_maxreqlen = PUFFS_REQSTRUCT_MAX;
148 1.17 pooka (void)strlcpy(args->pa_name, namebuf, sizeof(args->pa_name));
149 1.1 pooka
150 1.28.6.5 ad if (args->pa_nhashbuckets == 0)
151 1.28.6.5 ad args->pa_nhashbuckets = puffs_pnodebuckets_default;
152 1.28.6.5 ad if (args->pa_nhashbuckets < 1)
153 1.28.6.5 ad args->pa_nhashbuckets = 1;
154 1.28.6.5 ad if (args->pa_nhashbuckets > PUFFS_MAXPNODEBUCKETS) {
155 1.28.6.5 ad args->pa_nhashbuckets = puffs_maxpnodebuckets;
156 1.28.6.5 ad printf("puffs_mount: using %d hash buckets. "
157 1.28.6.5 ad "adjust puffs_maxpnodebuckets for more\n",
158 1.28.6.5 ad puffs_maxpnodebuckets);
159 1.28.6.5 ad }
160 1.28.6.5 ad
161 1.28.6.5 ad error = copyout(args, data, sizeof(struct puffs_kargs));
162 1.1 pooka if (error)
163 1.17 pooka goto out;
164 1.1 pooka
165 1.1 pooka error = set_statvfs_info(path, UIO_USERSPACE, namebuf,
166 1.1 pooka UIO_SYSSPACE, mp, l);
167 1.1 pooka if (error)
168 1.17 pooka goto out;
169 1.10 pooka mp->mnt_stat.f_iosize = DEV_BSIZE;
170 1.1 pooka
171 1.28.6.5 ad /*
172 1.28.6.5 ad * We can't handle the VFS_STATVFS() mount_domount() does
173 1.28.6.5 ad * after VFS_MOUNT() because we'd deadlock, so handle it
174 1.28.6.5 ad * here already.
175 1.28.6.5 ad */
176 1.28.6.5 ad copy_statvfs_info(&args->pa_svfsb, mp);
177 1.28.6.5 ad (void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat));
178 1.28.6.5 ad
179 1.1 pooka MALLOC(pmp, struct puffs_mount *, sizeof(struct puffs_mount),
180 1.1 pooka M_PUFFS, M_WAITOK | M_ZERO);
181 1.1 pooka
182 1.6 pooka mp->mnt_fs_bshift = DEV_BSHIFT;
183 1.6 pooka mp->mnt_dev_bshift = DEV_BSHIFT;
184 1.6 pooka mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
185 1.1 pooka mp->mnt_data = pmp;
186 1.26 pooka mp->mnt_iflag |= IMNT_HAS_TRANS;
187 1.6 pooka
188 1.1 pooka pmp->pmp_status = PUFFSTAT_MOUNTING;
189 1.1 pooka pmp->pmp_nextreq = 0;
190 1.1 pooka pmp->pmp_mp = mp;
191 1.17 pooka pmp->pmp_req_maxsize = args->pa_maxreqlen;
192 1.17 pooka pmp->pmp_args = *args;
193 1.1 pooka
194 1.28.6.5 ad pmp->pmp_npnodehash = args->pa_nhashbuckets;
195 1.22 pooka pmp->pmp_pnodehash = malloc
196 1.22 pooka (sizeof(struct puffs_pnode_hashlist *) * pmp->pmp_npnodehash,
197 1.22 pooka M_PUFFS, M_WAITOK);
198 1.22 pooka for (i = 0; i < pmp->pmp_npnodehash; i++)
199 1.22 pooka LIST_INIT(&pmp->pmp_pnodehash[i]);
200 1.22 pooka
201 1.1 pooka /*
202 1.1 pooka * Inform the fileops processing code that we have a mountpoint.
203 1.1 pooka * If it doesn't know about anyone with our pid/fd having the
204 1.1 pooka * device open, punt
205 1.1 pooka */
206 1.17 pooka if (puffs_setpmp(l->l_proc->p_pid, args->pa_fd, pmp)) {
207 1.17 pooka error = ENOENT;
208 1.17 pooka goto out;
209 1.1 pooka }
210 1.1 pooka
211 1.28.6.5 ad /* XXX: check parameters */
212 1.28.6.5 ad pmp->pmp_root_cookie = args->pa_root_cookie;
213 1.28.6.5 ad pmp->pmp_root_vtype = args->pa_root_vtype;
214 1.28.6.5 ad pmp->pmp_root_vsize = args->pa_root_vsize;
215 1.28.6.5 ad pmp->pmp_root_rdev = args->pa_root_rdev;
216 1.28.6.5 ad
217 1.28.6.4 ad mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
218 1.28.6.4 ad cv_init(&pmp->pmp_req_waiter_cv, "puffsget");
219 1.28.6.5 ad cv_init(&pmp->pmp_refcount_cv, "puffsref");
220 1.28.6.4 ad cv_init(&pmp->pmp_unmounting_cv, "puffsum");
221 1.1 pooka TAILQ_INIT(&pmp->pmp_req_touser);
222 1.1 pooka TAILQ_INIT(&pmp->pmp_req_replywait);
223 1.1 pooka TAILQ_INIT(&pmp->pmp_req_sizepark);
224 1.1 pooka
225 1.1 pooka DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
226 1.1 pooka mp, MPTOPUFFSMP(mp)));
227 1.1 pooka
228 1.1 pooka vfs_getnewfsid(mp);
229 1.1 pooka
230 1.17 pooka out:
231 1.22 pooka if (error && pmp && pmp->pmp_pnodehash)
232 1.22 pooka free(pmp->pmp_pnodehash, M_PUFFS);
233 1.22 pooka if (error && pmp)
234 1.22 pooka FREE(pmp, M_PUFFS);
235 1.17 pooka FREE(args, M_PUFFS);
236 1.17 pooka return error;
237 1.1 pooka }
238 1.1 pooka
239 1.1 pooka int
240 1.28.6.5 ad puffs_start(struct mount *mp, int flags, struct lwp *l)
241 1.1 pooka {
242 1.28.6.5 ad struct puffs_mount *pmp = MPTOPUFFSMP(mp);
243 1.1 pooka
244 1.28.6.5 ad KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
245 1.1 pooka pmp->pmp_status = PUFFSTAT_RUNNING;
246 1.1 pooka
247 1.1 pooka return 0;
248 1.1 pooka }
249 1.1 pooka
250 1.1 pooka int
251 1.1 pooka puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
252 1.1 pooka {
253 1.1 pooka struct puffs_mount *pmp;
254 1.1 pooka int error, force;
255 1.1 pooka
256 1.1 pooka PUFFS_VFSREQ(unmount);
257 1.1 pooka
258 1.1 pooka error = 0;
259 1.1 pooka force = mntflags & MNT_FORCE;
260 1.1 pooka pmp = MPTOPUFFSMP(mp);
261 1.1 pooka
262 1.1 pooka DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
263 1.1 pooka "status 0x%x\n", pmp->pmp_status));
264 1.1 pooka
265 1.1 pooka /*
266 1.1 pooka * flush all the vnodes. VOP_RECLAIM() takes care that the
267 1.1 pooka * root vnode does not get flushed until unmount. The
268 1.1 pooka * userspace root node cookie is stored in the mount
269 1.1 pooka * structure, so we can always re-instantiate a root vnode,
270 1.1 pooka * should userspace unmount decide it doesn't want to
271 1.1 pooka * cooperate.
272 1.1 pooka */
273 1.1 pooka error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
274 1.1 pooka if (error)
275 1.1 pooka goto out;
276 1.1 pooka
277 1.1 pooka /*
278 1.1 pooka * If we are not DYING, we should ask userspace's opinion
279 1.1 pooka * about the situation
280 1.1 pooka */
281 1.28.6.4 ad mutex_enter(&pmp->pmp_lock);
282 1.1 pooka if (pmp->pmp_status != PUFFSTAT_DYING) {
283 1.16 pooka pmp->pmp_unmounting = 1;
284 1.28.6.4 ad mutex_exit(&pmp->pmp_lock);
285 1.16 pooka
286 1.1 pooka unmount_arg.pvfsr_flags = mntflags;
287 1.1 pooka unmount_arg.pvfsr_pid = puffs_lwp2pid(l);
288 1.16 pooka
289 1.1 pooka error = puffs_vfstouser(pmp, PUFFS_VFS_UNMOUNT,
290 1.1 pooka &unmount_arg, sizeof(unmount_arg));
291 1.16 pooka DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
292 1.16 pooka
293 1.28.6.4 ad mutex_enter(&pmp->pmp_lock);
294 1.16 pooka pmp->pmp_unmounting = 0;
295 1.28.6.4 ad cv_broadcast(&pmp->pmp_unmounting_cv);
296 1.1 pooka }
297 1.1 pooka
298 1.1 pooka /*
299 1.1 pooka * if userspace cooperated or we really need to die,
300 1.1 pooka * screw what userland thinks and just die.
301 1.1 pooka */
302 1.1 pooka if (error == 0 || force) {
303 1.26 pooka /* tell waiters & other resources to go unwait themselves */
304 1.26 pooka puffs_userdead(pmp);
305 1.1 pooka puffs_nukebypmp(pmp);
306 1.26 pooka
307 1.26 pooka /*
308 1.28.6.5 ad * Wait until there are no more users for the mount resource.
309 1.28.6.5 ad * Notice that this is hooked against transport_close
310 1.28.6.5 ad * and return from touser. In an ideal world, it would
311 1.28.6.5 ad * be hooked against final return from all operations.
312 1.28.6.5 ad * But currently it works well enough, since nobody
313 1.28.6.5 ad * does weird blocking voodoo after return from touser().
314 1.26 pooka */
315 1.28.6.5 ad while (pmp->pmp_refcount != 0)
316 1.28.6.5 ad cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
317 1.28.6.4 ad mutex_exit(&pmp->pmp_lock);
318 1.26 pooka
319 1.26 pooka /* free resources now that we hopefully have no waiters left */
320 1.28.6.4 ad cv_destroy(&pmp->pmp_unmounting_cv);
321 1.28.6.5 ad cv_destroy(&pmp->pmp_refcount_cv);
322 1.28.6.5 ad cv_destroy(&pmp->pmp_req_waiter_cv);
323 1.28.6.4 ad mutex_destroy(&pmp->pmp_lock);
324 1.28.6.4 ad
325 1.22 pooka free(pmp->pmp_pnodehash, M_PUFFS);
326 1.1 pooka FREE(pmp, M_PUFFS);
327 1.1 pooka error = 0;
328 1.16 pooka } else {
329 1.28.6.4 ad mutex_exit(&pmp->pmp_lock);
330 1.1 pooka }
331 1.1 pooka
332 1.1 pooka out:
333 1.2 pooka DPRINTF(("puffs_unmount: return %d\n", error));
334 1.1 pooka return error;
335 1.1 pooka }
336 1.1 pooka
337 1.1 pooka /*
338 1.1 pooka * This doesn't need to travel to userspace
339 1.1 pooka */
340 1.1 pooka int
341 1.1 pooka puffs_root(struct mount *mp, struct vnode **vpp)
342 1.1 pooka {
343 1.1 pooka struct puffs_mount *pmp;
344 1.1 pooka struct puffs_node *pn;
345 1.1 pooka struct vnode *vp;
346 1.1 pooka
347 1.1 pooka pmp = MPTOPUFFSMP(mp);
348 1.1 pooka
349 1.1 pooka /*
350 1.1 pooka * pmp_lock must be held if vref()'ing or vrele()'ing the
351 1.20 pooka * root vnode. the latter is controlled by puffs_inactive().
352 1.1 pooka */
353 1.28.6.4 ad mutex_enter(&pmp->pmp_lock);
354 1.1 pooka vp = pmp->pmp_root;
355 1.1 pooka if (vp) {
356 1.28.6.3 ad mutex_enter(&vp->v_interlock);
357 1.28.6.4 ad mutex_exit(&pmp->pmp_lock);
358 1.1 pooka pn = VPTOPP(vp);
359 1.25 pooka if (vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK))
360 1.20 pooka goto grabnew;
361 1.1 pooka *vpp = vp;
362 1.1 pooka return 0;
363 1.25 pooka } else
364 1.28.6.4 ad mutex_exit(&pmp->pmp_lock);
365 1.25 pooka
366 1.25 pooka /* XXX: this is wrong, so FIXME */
367 1.1 pooka grabnew:
368 1.1 pooka
369 1.1 pooka /*
370 1.1 pooka * So, didn't have the magic root vnode available.
371 1.1 pooka * No matter, grab another an stuff it with the cookie.
372 1.1 pooka */
373 1.28.6.5 ad if (puffs_getvnode(mp, pmp->pmp_root_cookie, pmp->pmp_root_vtype,
374 1.28.6.5 ad pmp->pmp_root_vsize, pmp->pmp_root_rdev, &vp))
375 1.1 pooka panic("sloppy programming");
376 1.1 pooka
377 1.28.6.4 ad mutex_enter(&pmp->pmp_lock);
378 1.1 pooka /*
379 1.1 pooka * check if by mysterious force someone else created a root
380 1.1 pooka * vnode while we were executing.
381 1.1 pooka */
382 1.1 pooka if (pmp->pmp_root) {
383 1.1 pooka vref(pmp->pmp_root);
384 1.28.6.4 ad mutex_exit(&pmp->pmp_lock);
385 1.1 pooka puffs_putvnode(vp);
386 1.1 pooka vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
387 1.1 pooka *vpp = pmp->pmp_root;
388 1.1 pooka return 0;
389 1.1 pooka }
390 1.1 pooka
391 1.1 pooka /* store cache */
392 1.28.6.7 ad vp->v_vflag = VV_ROOT;
393 1.28.6.7 ad vp->v_iflag = 0;
394 1.1 pooka pmp->pmp_root = vp;
395 1.28.6.4 ad mutex_exit(&pmp->pmp_lock);
396 1.1 pooka
397 1.1 pooka vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
398 1.1 pooka
399 1.1 pooka *vpp = vp;
400 1.1 pooka return 0;
401 1.1 pooka }
402 1.1 pooka
403 1.1 pooka int
404 1.1 pooka puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
405 1.1 pooka {
406 1.1 pooka struct puffs_vfsreq_statvfs *statvfs_arg; /* too big for stack */
407 1.1 pooka struct puffs_mount *pmp;
408 1.1 pooka int error = 0;
409 1.1 pooka
410 1.1 pooka pmp = MPTOPUFFSMP(mp);
411 1.1 pooka
412 1.1 pooka /*
413 1.1 pooka * If we are mounting, it means that the userspace counterpart
414 1.1 pooka * is calling mount(2), but mount(2) also calls statvfs. So
415 1.1 pooka * requesting statvfs from userspace would mean a deadlock.
416 1.1 pooka * Compensate.
417 1.1 pooka */
418 1.1 pooka if (pmp->pmp_status == PUFFSTAT_MOUNTING)
419 1.1 pooka return EINPROGRESS;
420 1.1 pooka
421 1.1 pooka /* too big for stack */
422 1.1 pooka MALLOC(statvfs_arg, struct puffs_vfsreq_statvfs *,
423 1.1 pooka sizeof(struct puffs_vfsreq_statvfs), M_PUFFS, M_WAITOK | M_ZERO);
424 1.1 pooka statvfs_arg->pvfsr_pid = puffs_lwp2pid(l);
425 1.1 pooka
426 1.1 pooka error = puffs_vfstouser(pmp, PUFFS_VFS_STATVFS,
427 1.1 pooka statvfs_arg, sizeof(*statvfs_arg));
428 1.11 pooka statvfs_arg->pvfsr_sb.f_iosize = DEV_BSIZE;
429 1.10 pooka
430 1.1 pooka /*
431 1.1 pooka * Try to produce a sensible result even in the event
432 1.1 pooka * of userspace error.
433 1.1 pooka *
434 1.1 pooka * XXX: cache the copy in non-error case
435 1.1 pooka */
436 1.1 pooka if (!error) {
437 1.1 pooka copy_statvfs_info(&statvfs_arg->pvfsr_sb, mp);
438 1.1 pooka (void)memcpy(sbp, &statvfs_arg->pvfsr_sb,
439 1.1 pooka sizeof(struct statvfs));
440 1.1 pooka } else {
441 1.1 pooka copy_statvfs_info(sbp, mp);
442 1.1 pooka }
443 1.1 pooka
444 1.1 pooka FREE(statvfs_arg, M_PUFFS);
445 1.14 pooka return error;
446 1.1 pooka }
447 1.1 pooka
448 1.26 pooka static int
449 1.28.6.4 ad pageflush(struct mount *mp, kauth_cred_t cred,
450 1.28.6.4 ad int waitfor, int suspending, struct lwp *l)
451 1.1 pooka {
452 1.26 pooka struct puffs_node *pn;
453 1.18 pooka struct vnode *vp, *nvp;
454 1.28.6.4 ad int error, rv;
455 1.1 pooka
456 1.26 pooka KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
457 1.26 pooka KASSERT((suspending == 0)
458 1.26 pooka || (fstrans_is_owner(mp)
459 1.27 hannken && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
460 1.1 pooka
461 1.18 pooka error = 0;
462 1.18 pooka
463 1.18 pooka /*
464 1.24 pooka * Sync all cached data from regular vnodes (which are not
465 1.24 pooka * currently locked, see below). After this we call VFS_SYNC
466 1.24 pooka * for the fs server, which should handle data and metadata for
467 1.24 pooka * all the nodes it knows to exist.
468 1.18 pooka */
469 1.28.6.2 ad mutex_enter(&mntvnode_lock);
470 1.18 pooka loop:
471 1.18 pooka for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
472 1.18 pooka /* check if we're on the right list */
473 1.18 pooka if (vp->v_mount != mp)
474 1.18 pooka goto loop;
475 1.18 pooka
476 1.28.6.2 ad mutex_enter(&vp->v_interlock);
477 1.26 pooka pn = VPTOPP(vp);
478 1.18 pooka nvp = TAILQ_NEXT(vp, v_mntvnodes);
479 1.18 pooka
480 1.19 pooka if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
481 1.28.6.2 ad mutex_exit(&vp->v_interlock);
482 1.18 pooka continue;
483 1.18 pooka }
484 1.18 pooka
485 1.28.6.2 ad mutex_exit(&mntvnode_lock);
486 1.21 pooka
487 1.21 pooka /*
488 1.21 pooka * Here we try to get a reference to the vnode and to
489 1.21 pooka * lock it. This is mostly cargo-culted, but I will
490 1.21 pooka * offer an explanation to why I believe this might
491 1.21 pooka * actually do the right thing.
492 1.21 pooka *
493 1.21 pooka * If the vnode is a goner, we quite obviously don't need
494 1.21 pooka * to sync it.
495 1.21 pooka *
496 1.21 pooka * If the vnode was busy, we don't need to sync it because
497 1.21 pooka * this is never called with MNT_WAIT except from
498 1.21 pooka * dounmount(), when we are wait-flushing all the dirty
499 1.21 pooka * vnodes through other routes in any case. So there,
500 1.21 pooka * sync() doesn't actually sync. Happy now?
501 1.26 pooka *
502 1.26 pooka * NOTE: if we're suspending, vget() does NOT lock.
503 1.26 pooka * See puffs_lock() for details.
504 1.21 pooka */
505 1.18 pooka rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
506 1.18 pooka if (rv) {
507 1.28.6.2 ad mutex_enter(&mntvnode_lock);
508 1.18 pooka if (rv == ENOENT)
509 1.18 pooka goto loop;
510 1.18 pooka continue;
511 1.18 pooka }
512 1.18 pooka
513 1.26 pooka /*
514 1.26 pooka * Thread information to puffs_strategy() through the
515 1.26 pooka * pnode flags: we want to issue the putpages operations
516 1.26 pooka * as FAF if we're suspending, since it's very probable
517 1.26 pooka * that our execution context is that of the userspace
518 1.26 pooka * daemon. We can do this because:
519 1.26 pooka * + we send the "going to suspend" prior to this part
520 1.26 pooka * + if any of the writes fails in userspace, it's the
521 1.26 pooka * file system server's problem to decide if this was a
522 1.26 pooka * failed snapshot when it gets the "snapshot complete"
523 1.26 pooka * notification.
524 1.26 pooka * + if any of the writes fail in the kernel already, we
525 1.26 pooka * immediately fail *and* notify the user server of
526 1.26 pooka * failure.
527 1.26 pooka *
528 1.26 pooka * We also do FAFs if we're called from the syncer. This
529 1.26 pooka * is just general optimization for trickle sync: no need
530 1.26 pooka * to really guarantee that the stuff ended on backing
531 1.26 pooka * storage.
532 1.26 pooka * TODO: Maybe also hint the user server of this twist?
533 1.26 pooka */
534 1.28.6.4 ad if (suspending || waitfor == MNT_LAZY) {
535 1.28.6.4 ad mutex_enter(&vp->v_interlock);
536 1.26 pooka pn->pn_stat |= PNODE_SUSPEND;
537 1.28.6.4 ad mutex_exit(&vp->v_interlock);
538 1.28.6.4 ad }
539 1.28.6.4 ad rv = VOP_FSYNC(vp, cred, waitfor, 0, 0, l);
540 1.26 pooka if (suspending || waitfor == MNT_LAZY) {
541 1.28.6.2 ad mutex_enter(&vp->v_interlock);
542 1.26 pooka pn->pn_stat &= ~PNODE_SUSPEND;
543 1.28.6.2 ad mutex_exit(&vp->v_interlock);
544 1.26 pooka }
545 1.21 pooka if (rv)
546 1.18 pooka error = rv;
547 1.18 pooka vput(vp);
548 1.28.6.2 ad mutex_enter(&mntvnode_lock);
549 1.18 pooka }
550 1.28.6.2 ad mutex_exit(&mntvnode_lock);
551 1.18 pooka
552 1.26 pooka return error;
553 1.26 pooka }
554 1.26 pooka
555 1.26 pooka int
556 1.26 pooka puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
557 1.26 pooka struct lwp *l)
558 1.26 pooka {
559 1.26 pooka int error, rv;
560 1.26 pooka
561 1.26 pooka PUFFS_VFSREQ(sync);
562 1.26 pooka
563 1.28.6.4 ad error = pageflush(mp, cred, waitfor, 0, l);
564 1.26 pooka
565 1.18 pooka /* sync fs */
566 1.1 pooka sync_arg.pvfsr_waitfor = waitfor;
567 1.1 pooka puffs_credcvt(&sync_arg.pvfsr_cred, cred);
568 1.1 pooka sync_arg.pvfsr_pid = puffs_lwp2pid(l);
569 1.1 pooka
570 1.18 pooka rv = puffs_vfstouser(MPTOPUFFSMP(mp), PUFFS_VFS_SYNC,
571 1.1 pooka &sync_arg, sizeof(sync_arg));
572 1.18 pooka if (rv)
573 1.18 pooka error = rv;
574 1.1 pooka
575 1.1 pooka return error;
576 1.1 pooka }
577 1.1 pooka
578 1.1 pooka int
579 1.28.6.5 ad puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
580 1.1 pooka {
581 1.28.6.5 ad struct puffs_mount *pmp = MPTOPUFFSMP(mp);
582 1.28.6.5 ad struct puffs_vfsreq_fhtonode *fhtonode_argp;
583 1.28.6.5 ad struct vnode *vp;
584 1.28.6.5 ad size_t argsize;
585 1.28.6.5 ad int error;
586 1.1 pooka
587 1.28.6.5 ad if (pmp->pmp_args.pa_fhsize == 0)
588 1.28.6.5 ad return EOPNOTSUPP;
589 1.1 pooka
590 1.28.6.5 ad if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
591 1.28.6.5 ad if (pmp->pmp_args.pa_fhsize < PUFFS_FROMFHSIZE(fhp->fid_len))
592 1.28.6.5 ad return EINVAL;
593 1.28.6.5 ad } else {
594 1.28.6.5 ad if (pmp->pmp_args.pa_fhsize != PUFFS_FROMFHSIZE(fhp->fid_len))
595 1.28.6.5 ad return EINVAL;
596 1.28.6.5 ad }
597 1.1 pooka
598 1.28.6.5 ad argsize = sizeof(struct puffs_vfsreq_fhtonode)
599 1.28.6.5 ad + PUFFS_FROMFHSIZE(fhp->fid_len);
600 1.28.6.5 ad fhtonode_argp = malloc(argsize, M_PUFFS, M_ZERO | M_WAITOK);
601 1.28.6.5 ad fhtonode_argp->pvfsr_dsize = PUFFS_FROMFHSIZE(fhp->fid_len);
602 1.28.6.5 ad memcpy(fhtonode_argp->pvfsr_data, fhp->fid_data,
603 1.28.6.5 ad PUFFS_FROMFHSIZE(fhp->fid_len));
604 1.28.6.5 ad
605 1.28.6.5 ad error = puffs_vfstouser(pmp, PUFFS_VFS_FHTOVP, fhtonode_argp, argsize);
606 1.28.6.5 ad if (error)
607 1.28.6.5 ad goto out;
608 1.28.6.5 ad
609 1.28.6.5 ad vp = puffs_pnode2vnode(pmp, fhtonode_argp->pvfsr_fhcookie, 1);
610 1.28.6.5 ad DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
611 1.28.6.5 ad fhtonode_argp->pvfsr_fhcookie, vp));
612 1.28.6.5 ad if (!vp) {
613 1.28.6.5 ad error = puffs_getvnode(mp, fhtonode_argp->pvfsr_fhcookie,
614 1.28.6.5 ad fhtonode_argp->pvfsr_vtype, fhtonode_argp->pvfsr_size,
615 1.28.6.5 ad fhtonode_argp->pvfsr_rdev, &vp);
616 1.28.6.5 ad if (error)
617 1.28.6.5 ad goto out;
618 1.28.6.5 ad vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
619 1.28.6.5 ad }
620 1.28.6.5 ad
621 1.28.6.5 ad *vpp = vp;
622 1.28.6.5 ad out:
623 1.28.6.5 ad free(fhtonode_argp, M_PUFFS);
624 1.28.6.5 ad return error;
625 1.1 pooka }
626 1.1 pooka
627 1.1 pooka int
628 1.28.6.5 ad puffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
629 1.1 pooka {
630 1.28.6.5 ad struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
631 1.28.6.5 ad struct puffs_vfsreq_nodetofh *nodetofh_argp;
632 1.28.6.5 ad size_t argsize;
633 1.28.6.5 ad int error;
634 1.1 pooka
635 1.28.6.5 ad if (pmp->pmp_args.pa_fhsize == 0)
636 1.28.6.5 ad return EOPNOTSUPP;
637 1.28.6.5 ad
638 1.28.6.5 ad /* if file handles are static length, we can return immediately */
639 1.28.6.5 ad if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
640 1.28.6.5 ad && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
641 1.28.6.5 ad *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
642 1.28.6.5 ad return E2BIG;
643 1.28.6.5 ad }
644 1.28.6.5 ad
645 1.28.6.5 ad argsize = sizeof(struct puffs_vfsreq_nodetofh)
646 1.28.6.5 ad + PUFFS_FROMFHSIZE(*fh_size);
647 1.28.6.5 ad nodetofh_argp = malloc(argsize, M_PUFFS, M_ZERO | M_WAITOK);
648 1.28.6.5 ad nodetofh_argp->pvfsr_fhcookie = VPTOPNC(vp);
649 1.28.6.5 ad nodetofh_argp->pvfsr_dsize = PUFFS_FROMFHSIZE(*fh_size);
650 1.28.6.5 ad
651 1.28.6.5 ad error = puffs_vfstouser(pmp, PUFFS_VFS_VPTOFH, nodetofh_argp, argsize);
652 1.28.6.5 ad if (error) {
653 1.28.6.5 ad if (error == E2BIG)
654 1.28.6.5 ad *fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
655 1.28.6.5 ad goto out;
656 1.28.6.5 ad }
657 1.28.6.5 ad
658 1.28.6.5 ad if (PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize) > FHANDLE_SIZE_MAX) {
659 1.28.6.5 ad /* XXX: wrong direction */
660 1.28.6.5 ad error = EINVAL;
661 1.28.6.5 ad goto out;
662 1.28.6.5 ad }
663 1.28.6.5 ad
664 1.28.6.5 ad if (*fh_size < PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize)) {
665 1.28.6.5 ad *fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
666 1.28.6.5 ad error = E2BIG;
667 1.28.6.5 ad goto out;
668 1.28.6.5 ad }
669 1.28.6.5 ad if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
670 1.28.6.5 ad *fh_size = PUFFS_TOFHSIZE(nodetofh_argp->pvfsr_dsize);
671 1.28.6.5 ad } else {
672 1.28.6.5 ad *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
673 1.28.6.5 ad }
674 1.28.6.5 ad
675 1.28.6.5 ad if (fhp) {
676 1.28.6.5 ad fhp->fid_len = *fh_size;
677 1.28.6.5 ad memcpy(fhp->fid_data,
678 1.28.6.5 ad nodetofh_argp->pvfsr_data, nodetofh_argp->pvfsr_dsize);
679 1.28.6.5 ad }
680 1.28.6.5 ad
681 1.28.6.5 ad out:
682 1.28.6.5 ad free(nodetofh_argp, M_PUFFS);
683 1.28.6.5 ad return error;
684 1.1 pooka }
685 1.1 pooka
686 1.1 pooka void
687 1.1 pooka puffs_init()
688 1.1 pooka {
689 1.1 pooka
690 1.5 pooka malloc_type_attach(M_PUFFS);
691 1.5 pooka
692 1.28.6.4 ad pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
693 1.28.6.4 ad "puffpnpl", &pool_allocator_nointr, IPL_NONE);
694 1.28.6.4 ad puffs_transport_init();
695 1.28.6.4 ad puffs_msgif_init();
696 1.1 pooka }
697 1.1 pooka
698 1.1 pooka void
699 1.1 pooka puffs_done()
700 1.1 pooka {
701 1.1 pooka
702 1.28.6.4 ad puffs_msgif_destroy();
703 1.28.6.4 ad puffs_transport_destroy();
704 1.5 pooka pool_destroy(&puffs_pnpool);
705 1.5 pooka
706 1.28.6.4 ad malloc_type_detach(M_PUFFS);
707 1.1 pooka }
708 1.1 pooka
709 1.1 pooka int
710 1.1 pooka puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
711 1.1 pooka {
712 1.1 pooka
713 1.1 pooka return EOPNOTSUPP;
714 1.1 pooka }
715 1.1 pooka
716 1.26 pooka int
717 1.26 pooka puffs_suspendctl(struct mount *mp, int cmd)
718 1.26 pooka {
719 1.26 pooka struct puffs_mount *pmp;
720 1.26 pooka int error;
721 1.26 pooka
722 1.26 pooka pmp = MPTOPUFFSMP(mp);
723 1.26 pooka switch (cmd) {
724 1.26 pooka case SUSPEND_SUSPEND:
725 1.26 pooka DPRINTF(("puffs_suspendctl: suspending\n"));
726 1.27 hannken if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
727 1.26 pooka break;
728 1.26 pooka puffs_suspendtouser(pmp, PUFFS_SUSPEND_START);
729 1.26 pooka
730 1.28.6.4 ad error = pageflush(mp, FSCRED, 0, 1, curlwp);
731 1.26 pooka if (error == 0)
732 1.27 hannken error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
733 1.26 pooka
734 1.26 pooka if (error != 0) {
735 1.26 pooka puffs_suspendtouser(pmp, PUFFS_SUSPEND_ERROR);
736 1.27 hannken (void) fstrans_setstate(mp, FSTRANS_NORMAL);
737 1.26 pooka break;
738 1.26 pooka }
739 1.26 pooka
740 1.26 pooka puffs_suspendtouser(pmp, PUFFS_SUSPEND_SUSPENDED);
741 1.26 pooka
742 1.26 pooka break;
743 1.26 pooka
744 1.26 pooka case SUSPEND_RESUME:
745 1.26 pooka DPRINTF(("puffs_suspendctl: resume\n"));
746 1.26 pooka error = 0;
747 1.27 hannken (void) fstrans_setstate(mp, FSTRANS_NORMAL);
748 1.26 pooka puffs_suspendtouser(pmp, PUFFS_SUSPEND_RESUME);
749 1.26 pooka break;
750 1.26 pooka
751 1.26 pooka default:
752 1.26 pooka error = EINVAL;
753 1.26 pooka break;
754 1.26 pooka }
755 1.26 pooka
756 1.26 pooka DPRINTF(("puffs_suspendctl: return %d\n", error));
757 1.26 pooka return error;
758 1.26 pooka }
759 1.26 pooka
760 1.1 pooka const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
761 1.1 pooka &puffs_vnodeop_opv_desc,
762 1.3 pooka &puffs_specop_opv_desc,
763 1.4 pooka &puffs_fifoop_opv_desc,
764 1.12 pooka &puffs_msgop_opv_desc,
765 1.1 pooka NULL,
766 1.1 pooka };
767 1.1 pooka
768 1.1 pooka struct vfsops puffs_vfsops = {
769 1.1 pooka MOUNT_PUFFS,
770 1.1 pooka puffs_mount, /* mount */
771 1.1 pooka puffs_start, /* start */
772 1.1 pooka puffs_unmount, /* unmount */
773 1.1 pooka puffs_root, /* root */
774 1.28.6.5 ad (void *)eopnotsupp, /* quotactl */
775 1.1 pooka puffs_statvfs, /* statvfs */
776 1.1 pooka puffs_sync, /* sync */
777 1.28.6.5 ad (void *)eopnotsupp, /* vget */
778 1.28.6.5 ad puffs_fhtovp, /* fhtovp */
779 1.28.6.5 ad puffs_vptofh, /* vptofh */
780 1.1 pooka puffs_init, /* init */
781 1.1 pooka NULL, /* reinit */
782 1.1 pooka puffs_done, /* done */
783 1.1 pooka NULL, /* mountroot */
784 1.1 pooka puffs_snapshot, /* snapshot */
785 1.1 pooka vfs_stdextattrctl, /* extattrctl */
786 1.26 pooka puffs_suspendctl, /* suspendctl */
787 1.1 pooka puffs_vnodeopv_descs, /* vnodeops */
788 1.1 pooka 0, /* refcount */
789 1.1 pooka { NULL, NULL }
790 1.1 pooka };
791 1.1 pooka VFS_ATTACH(puffs_vfsops);
792