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