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