puffs_vnops.c revision 1.166 1 /* $NetBSD: puffs_vnops.c,v 1.166 2012/04/18 00:42:50 manu Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Google Summer of Code program and the Ulla Tuominen Foundation.
8 * The Google SoC project was mentored by Bill Studenmund.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: puffs_vnops.c,v 1.166 2012/04/18 00:42:50 manu Exp $");
34
35 #include <sys/param.h>
36 #include <sys/buf.h>
37 #include <sys/lockf.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/namei.h>
41 #include <sys/vnode.h>
42 #include <sys/proc.h>
43 #include <sys/kernel.h> /* For hz, hardclock_ticks */
44
45 #include <uvm/uvm.h>
46
47 #include <fs/puffs/puffs_msgif.h>
48 #include <fs/puffs/puffs_sys.h>
49
50 #include <miscfs/fifofs/fifo.h>
51 #include <miscfs/genfs/genfs.h>
52 #include <miscfs/specfs/specdev.h>
53
54 int puffs_vnop_lookup(void *);
55 int puffs_vnop_create(void *);
56 int puffs_vnop_access(void *);
57 int puffs_vnop_mknod(void *);
58 int puffs_vnop_open(void *);
59 int puffs_vnop_close(void *);
60 int puffs_vnop_getattr(void *);
61 int puffs_vnop_setattr(void *);
62 int puffs_vnop_reclaim(void *);
63 int puffs_vnop_readdir(void *);
64 int puffs_vnop_poll(void *);
65 int puffs_vnop_fsync(void *);
66 int puffs_vnop_seek(void *);
67 int puffs_vnop_remove(void *);
68 int puffs_vnop_mkdir(void *);
69 int puffs_vnop_rmdir(void *);
70 int puffs_vnop_link(void *);
71 int puffs_vnop_readlink(void *);
72 int puffs_vnop_symlink(void *);
73 int puffs_vnop_rename(void *);
74 int puffs_vnop_read(void *);
75 int puffs_vnop_write(void *);
76 int puffs_vnop_fcntl(void *);
77 int puffs_vnop_ioctl(void *);
78 int puffs_vnop_inactive(void *);
79 int puffs_vnop_print(void *);
80 int puffs_vnop_pathconf(void *);
81 int puffs_vnop_advlock(void *);
82 int puffs_vnop_strategy(void *);
83 int puffs_vnop_bmap(void *);
84 int puffs_vnop_mmap(void *);
85 int puffs_vnop_getpages(void *);
86 int puffs_vnop_abortop(void *);
87 int puffs_vnop_getextattr(void *);
88 int puffs_vnop_setextattr(void *);
89 int puffs_vnop_listextattr(void *);
90 int puffs_vnop_deleteextattr(void *);
91
92 int puffs_vnop_spec_read(void *);
93 int puffs_vnop_spec_write(void *);
94 int puffs_vnop_fifo_read(void *);
95 int puffs_vnop_fifo_write(void *);
96
97 int puffs_vnop_checkop(void *);
98
99 #define puffs_vnop_lock genfs_lock
100 #define puffs_vnop_unlock genfs_unlock
101 #define puffs_vnop_islocked genfs_islocked
102
103 int (**puffs_vnodeop_p)(void *);
104 const struct vnodeopv_entry_desc puffs_vnodeop_entries[] = {
105 { &vop_default_desc, vn_default_error },
106 { &vop_lookup_desc, puffs_vnop_lookup }, /* REAL lookup */
107 { &vop_create_desc, puffs_vnop_checkop }, /* create */
108 { &vop_mknod_desc, puffs_vnop_checkop }, /* mknod */
109 { &vop_open_desc, puffs_vnop_open }, /* REAL open */
110 { &vop_close_desc, puffs_vnop_checkop }, /* close */
111 { &vop_access_desc, puffs_vnop_access }, /* REAL access */
112 { &vop_getattr_desc, puffs_vnop_checkop }, /* getattr */
113 { &vop_setattr_desc, puffs_vnop_checkop }, /* setattr */
114 { &vop_read_desc, puffs_vnop_checkop }, /* read */
115 { &vop_write_desc, puffs_vnop_checkop }, /* write */
116 { &vop_fsync_desc, puffs_vnop_fsync }, /* REAL fsync */
117 { &vop_seek_desc, puffs_vnop_checkop }, /* seek */
118 { &vop_remove_desc, puffs_vnop_checkop }, /* remove */
119 { &vop_link_desc, puffs_vnop_checkop }, /* link */
120 { &vop_rename_desc, puffs_vnop_checkop }, /* rename */
121 { &vop_mkdir_desc, puffs_vnop_checkop }, /* mkdir */
122 { &vop_rmdir_desc, puffs_vnop_checkop }, /* rmdir */
123 { &vop_symlink_desc, puffs_vnop_checkop }, /* symlink */
124 { &vop_readdir_desc, puffs_vnop_checkop }, /* readdir */
125 { &vop_readlink_desc, puffs_vnop_checkop }, /* readlink */
126 { &vop_getpages_desc, puffs_vnop_checkop }, /* getpages */
127 { &vop_putpages_desc, genfs_putpages }, /* REAL putpages */
128 { &vop_pathconf_desc, puffs_vnop_checkop }, /* pathconf */
129 { &vop_advlock_desc, puffs_vnop_advlock }, /* advlock */
130 { &vop_strategy_desc, puffs_vnop_strategy }, /* REAL strategy */
131 { &vop_revoke_desc, genfs_revoke }, /* REAL revoke */
132 { &vop_abortop_desc, puffs_vnop_abortop }, /* REAL abortop */
133 { &vop_inactive_desc, puffs_vnop_inactive }, /* REAL inactive */
134 { &vop_reclaim_desc, puffs_vnop_reclaim }, /* REAL reclaim */
135 { &vop_lock_desc, puffs_vnop_lock }, /* REAL lock */
136 { &vop_unlock_desc, puffs_vnop_unlock }, /* REAL unlock */
137 { &vop_bmap_desc, puffs_vnop_bmap }, /* REAL bmap */
138 { &vop_print_desc, puffs_vnop_print }, /* REAL print */
139 { &vop_islocked_desc, puffs_vnop_islocked }, /* REAL islocked */
140 { &vop_bwrite_desc, genfs_nullop }, /* REAL bwrite */
141 { &vop_mmap_desc, puffs_vnop_mmap }, /* REAL mmap */
142 { &vop_poll_desc, puffs_vnop_poll }, /* REAL poll */
143 { &vop_getextattr_desc, puffs_vnop_getextattr }, /* getextattr */
144 { &vop_setextattr_desc, puffs_vnop_setextattr }, /* setextattr */
145 { &vop_listextattr_desc, puffs_vnop_listextattr }, /* listextattr */
146 { &vop_deleteextattr_desc, puffs_vnop_deleteextattr },/* deleteextattr */
147 #if 0
148 { &vop_openextattr_desc, puffs_vnop_checkop }, /* openextattr */
149 { &vop_closeextattr_desc, puffs_vnop_checkop }, /* closeextattr */
150 #endif
151 { &vop_kqfilter_desc, genfs_eopnotsupp }, /* kqfilter XXX */
152 { NULL, NULL }
153 };
154 const struct vnodeopv_desc puffs_vnodeop_opv_desc =
155 { &puffs_vnodeop_p, puffs_vnodeop_entries };
156
157
158 int (**puffs_specop_p)(void *);
159 const struct vnodeopv_entry_desc puffs_specop_entries[] = {
160 { &vop_default_desc, vn_default_error },
161 { &vop_lookup_desc, spec_lookup }, /* lookup, ENOTDIR */
162 { &vop_create_desc, spec_create }, /* genfs_badop */
163 { &vop_mknod_desc, spec_mknod }, /* genfs_badop */
164 { &vop_open_desc, spec_open }, /* spec_open */
165 { &vop_close_desc, spec_close }, /* spec_close */
166 { &vop_access_desc, puffs_vnop_checkop }, /* access */
167 { &vop_getattr_desc, puffs_vnop_checkop }, /* getattr */
168 { &vop_setattr_desc, puffs_vnop_checkop }, /* setattr */
169 { &vop_read_desc, puffs_vnop_spec_read }, /* update, read */
170 { &vop_write_desc, puffs_vnop_spec_write }, /* update, write */
171 { &vop_ioctl_desc, spec_ioctl }, /* spec_ioctl */
172 { &vop_fcntl_desc, genfs_fcntl }, /* dummy */
173 { &vop_poll_desc, spec_poll }, /* spec_poll */
174 { &vop_kqfilter_desc, spec_kqfilter }, /* spec_kqfilter */
175 { &vop_revoke_desc, spec_revoke }, /* genfs_revoke */
176 { &vop_mmap_desc, spec_mmap }, /* spec_mmap */
177 { &vop_fsync_desc, spec_fsync }, /* vflushbuf */
178 { &vop_seek_desc, spec_seek }, /* genfs_nullop */
179 { &vop_remove_desc, spec_remove }, /* genfs_badop */
180 { &vop_link_desc, spec_link }, /* genfs_badop */
181 { &vop_rename_desc, spec_rename }, /* genfs_badop */
182 { &vop_mkdir_desc, spec_mkdir }, /* genfs_badop */
183 { &vop_rmdir_desc, spec_rmdir }, /* genfs_badop */
184 { &vop_symlink_desc, spec_symlink }, /* genfs_badop */
185 { &vop_readdir_desc, spec_readdir }, /* genfs_badop */
186 { &vop_readlink_desc, spec_readlink }, /* genfs_badop */
187 { &vop_abortop_desc, spec_abortop }, /* genfs_badop */
188 { &vop_inactive_desc, puffs_vnop_inactive }, /* REAL inactive */
189 { &vop_reclaim_desc, puffs_vnop_reclaim }, /* REAL reclaim */
190 { &vop_lock_desc, puffs_vnop_lock }, /* REAL lock */
191 { &vop_unlock_desc, puffs_vnop_unlock }, /* REAL unlock */
192 { &vop_bmap_desc, spec_bmap }, /* dummy */
193 { &vop_strategy_desc, spec_strategy }, /* dev strategy */
194 { &vop_print_desc, puffs_vnop_print }, /* REAL print */
195 { &vop_islocked_desc, puffs_vnop_islocked }, /* REAL islocked */
196 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
197 { &vop_advlock_desc, spec_advlock }, /* lf_advlock */
198 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
199 { &vop_getpages_desc, spec_getpages }, /* genfs_getpages */
200 { &vop_putpages_desc, spec_putpages }, /* genfs_putpages */
201 { &vop_getextattr_desc, puffs_vnop_checkop }, /* getextattr */
202 { &vop_setextattr_desc, puffs_vnop_checkop }, /* setextattr */
203 { &vop_listextattr_desc, puffs_vnop_checkop }, /* listextattr */
204 { &vop_deleteextattr_desc, puffs_vnop_checkop },/* deleteextattr */
205 #if 0
206 { &vop_openextattr_desc, _openextattr }, /* openextattr */
207 { &vop_closeextattr_desc, _closeextattr }, /* closeextattr */
208 #endif
209 { NULL, NULL }
210 };
211 const struct vnodeopv_desc puffs_specop_opv_desc =
212 { &puffs_specop_p, puffs_specop_entries };
213
214
215 int (**puffs_fifoop_p)(void *);
216 const struct vnodeopv_entry_desc puffs_fifoop_entries[] = {
217 { &vop_default_desc, vn_default_error },
218 { &vop_lookup_desc, vn_fifo_bypass }, /* lookup, ENOTDIR */
219 { &vop_create_desc, vn_fifo_bypass }, /* genfs_badop */
220 { &vop_mknod_desc, vn_fifo_bypass }, /* genfs_badop */
221 { &vop_open_desc, vn_fifo_bypass }, /* open */
222 { &vop_close_desc, vn_fifo_bypass }, /* close */
223 { &vop_access_desc, puffs_vnop_checkop }, /* access */
224 { &vop_getattr_desc, puffs_vnop_checkop }, /* getattr */
225 { &vop_setattr_desc, puffs_vnop_checkop }, /* setattr */
226 { &vop_read_desc, puffs_vnop_fifo_read }, /* read, update */
227 { &vop_write_desc, puffs_vnop_fifo_write }, /* write, update */
228 { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
229 { &vop_fcntl_desc, genfs_fcntl }, /* dummy */
230 { &vop_poll_desc, vn_fifo_bypass }, /* poll */
231 { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
232 { &vop_revoke_desc, vn_fifo_bypass }, /* genfs_revoke */
233 { &vop_mmap_desc, vn_fifo_bypass }, /* genfs_badop */
234 { &vop_fsync_desc, vn_fifo_bypass }, /* genfs_nullop*/
235 { &vop_seek_desc, vn_fifo_bypass }, /* genfs_badop */
236 { &vop_remove_desc, vn_fifo_bypass }, /* genfs_badop */
237 { &vop_link_desc, vn_fifo_bypass }, /* genfs_badop */
238 { &vop_rename_desc, vn_fifo_bypass }, /* genfs_badop */
239 { &vop_mkdir_desc, vn_fifo_bypass }, /* genfs_badop */
240 { &vop_rmdir_desc, vn_fifo_bypass }, /* genfs_badop */
241 { &vop_symlink_desc, vn_fifo_bypass }, /* genfs_badop */
242 { &vop_readdir_desc, vn_fifo_bypass }, /* genfs_badop */
243 { &vop_readlink_desc, vn_fifo_bypass }, /* genfs_badop */
244 { &vop_abortop_desc, vn_fifo_bypass }, /* genfs_badop */
245 { &vop_inactive_desc, puffs_vnop_inactive }, /* REAL inactive */
246 { &vop_reclaim_desc, puffs_vnop_reclaim }, /* REAL reclaim */
247 { &vop_lock_desc, puffs_vnop_lock }, /* REAL lock */
248 { &vop_unlock_desc, puffs_vnop_unlock }, /* REAL unlock */
249 { &vop_bmap_desc, vn_fifo_bypass }, /* dummy */
250 { &vop_strategy_desc, vn_fifo_bypass }, /* genfs_badop */
251 { &vop_print_desc, puffs_vnop_print }, /* REAL print */
252 { &vop_islocked_desc, puffs_vnop_islocked }, /* REAL islocked */
253 { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
254 { &vop_advlock_desc, vn_fifo_bypass }, /* genfs_einval */
255 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
256 { &vop_putpages_desc, vn_fifo_bypass }, /* genfs_null_putpages*/
257 #if 0
258 { &vop_openextattr_desc, _openextattr }, /* openextattr */
259 { &vop_closeextattr_desc, _closeextattr }, /* closeextattr */
260 #endif
261 { &vop_getextattr_desc, puffs_vnop_checkop }, /* getextattr */
262 { &vop_setextattr_desc, puffs_vnop_checkop }, /* setextattr */
263 { &vop_listextattr_desc, puffs_vnop_checkop }, /* listextattr */
264 { &vop_deleteextattr_desc, puffs_vnop_checkop }, /* deleteextattr */
265 { NULL, NULL }
266 };
267 const struct vnodeopv_desc puffs_fifoop_opv_desc =
268 { &puffs_fifoop_p, puffs_fifoop_entries };
269
270
271 /* "real" vnode operations */
272 int (**puffs_msgop_p)(void *);
273 const struct vnodeopv_entry_desc puffs_msgop_entries[] = {
274 { &vop_default_desc, vn_default_error },
275 { &vop_create_desc, puffs_vnop_create }, /* create */
276 { &vop_mknod_desc, puffs_vnop_mknod }, /* mknod */
277 { &vop_open_desc, puffs_vnop_open }, /* open */
278 { &vop_close_desc, puffs_vnop_close }, /* close */
279 { &vop_access_desc, puffs_vnop_access }, /* access */
280 { &vop_getattr_desc, puffs_vnop_getattr }, /* getattr */
281 { &vop_setattr_desc, puffs_vnop_setattr }, /* setattr */
282 { &vop_read_desc, puffs_vnop_read }, /* read */
283 { &vop_write_desc, puffs_vnop_write }, /* write */
284 { &vop_seek_desc, puffs_vnop_seek }, /* seek */
285 { &vop_remove_desc, puffs_vnop_remove }, /* remove */
286 { &vop_link_desc, puffs_vnop_link }, /* link */
287 { &vop_rename_desc, puffs_vnop_rename }, /* rename */
288 { &vop_mkdir_desc, puffs_vnop_mkdir }, /* mkdir */
289 { &vop_rmdir_desc, puffs_vnop_rmdir }, /* rmdir */
290 { &vop_symlink_desc, puffs_vnop_symlink }, /* symlink */
291 { &vop_readdir_desc, puffs_vnop_readdir }, /* readdir */
292 { &vop_readlink_desc, puffs_vnop_readlink }, /* readlink */
293 { &vop_print_desc, puffs_vnop_print }, /* print */
294 { &vop_islocked_desc, puffs_vnop_islocked }, /* islocked */
295 { &vop_pathconf_desc, puffs_vnop_pathconf }, /* pathconf */
296 { &vop_getpages_desc, puffs_vnop_getpages }, /* getpages */
297 { NULL, NULL }
298 };
299 const struct vnodeopv_desc puffs_msgop_opv_desc =
300 { &puffs_msgop_p, puffs_msgop_entries };
301
302 /*
303 * for dosetattr / update_va
304 */
305 #define SETATTR_CHSIZE 0x01
306 #define SETATTR_ASYNC 0x02
307
308 #define ERROUT(err) \
309 do { \
310 error = err; \
311 goto out; \
312 } while (/*CONSTCOND*/0)
313
314 /*
315 * This is a generic vnode operation handler. It checks if the necessary
316 * operations for the called vnode operation are implemented by userspace
317 * and either returns a dummy return value or proceeds to call the real
318 * vnode operation from puffs_msgop_v.
319 *
320 * XXX: this should described elsewhere and autogenerated, the complexity
321 * of the vnode operations vectors and their interrelationships is also
322 * getting a bit out of hand. Another problem is that we need this same
323 * information in the fs server code, so keeping the two in sync manually
324 * is not a viable (long term) plan.
325 */
326
327 /* not supported, handle locking protocol */
328 #define CHECKOP_NOTSUPP(op) \
329 case VOP_##op##_DESCOFFSET: \
330 if (pmp->pmp_vnopmask[PUFFS_VN_##op] == 0) \
331 return genfs_eopnotsupp(v); \
332 break
333
334 /* always succeed, no locking */
335 #define CHECKOP_SUCCESS(op) \
336 case VOP_##op##_DESCOFFSET: \
337 if (pmp->pmp_vnopmask[PUFFS_VN_##op] == 0) \
338 return 0; \
339 break
340
341 int
342 puffs_vnop_checkop(void *v)
343 {
344 struct vop_generic_args /* {
345 struct vnodeop_desc *a_desc;
346 spooky mystery contents;
347 } */ *ap = v;
348 struct vnodeop_desc *desc = ap->a_desc;
349 struct puffs_mount *pmp;
350 struct vnode *vp;
351 int offset, rv;
352
353 offset = ap->a_desc->vdesc_vp_offsets[0];
354 #ifdef DIAGNOSTIC
355 if (offset == VDESC_NO_OFFSET)
356 panic("puffs_checkop: no vnode, why did you call me?");
357 #endif
358 vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap);
359 pmp = MPTOPUFFSMP(vp->v_mount);
360
361 DPRINTF_VERBOSE(("checkop call %s (%d), vp %p\n",
362 ap->a_desc->vdesc_name, ap->a_desc->vdesc_offset, vp));
363
364 if (!ALLOPS(pmp)) {
365 switch (desc->vdesc_offset) {
366 CHECKOP_NOTSUPP(CREATE);
367 CHECKOP_NOTSUPP(MKNOD);
368 CHECKOP_NOTSUPP(GETATTR);
369 CHECKOP_NOTSUPP(SETATTR);
370 CHECKOP_NOTSUPP(READ);
371 CHECKOP_NOTSUPP(WRITE);
372 CHECKOP_NOTSUPP(FCNTL);
373 CHECKOP_NOTSUPP(IOCTL);
374 CHECKOP_NOTSUPP(REMOVE);
375 CHECKOP_NOTSUPP(LINK);
376 CHECKOP_NOTSUPP(RENAME);
377 CHECKOP_NOTSUPP(MKDIR);
378 CHECKOP_NOTSUPP(RMDIR);
379 CHECKOP_NOTSUPP(SYMLINK);
380 CHECKOP_NOTSUPP(READDIR);
381 CHECKOP_NOTSUPP(READLINK);
382 CHECKOP_NOTSUPP(PRINT);
383 CHECKOP_NOTSUPP(PATHCONF);
384 CHECKOP_NOTSUPP(GETEXTATTR);
385 CHECKOP_NOTSUPP(SETEXTATTR);
386 CHECKOP_NOTSUPP(LISTEXTATTR);
387 CHECKOP_NOTSUPP(DELETEEXTATTR);
388
389 CHECKOP_SUCCESS(ACCESS);
390 CHECKOP_SUCCESS(CLOSE);
391 CHECKOP_SUCCESS(SEEK);
392
393 case VOP_GETPAGES_DESCOFFSET:
394 if (!EXISTSOP(pmp, READ))
395 return genfs_eopnotsupp(v);
396 break;
397
398 default:
399 panic("puffs_checkop: unhandled vnop %d",
400 desc->vdesc_offset);
401 }
402 }
403
404 rv = VOCALL(puffs_msgop_p, ap->a_desc->vdesc_offset, v);
405
406 DPRINTF_VERBOSE(("checkop return %s (%d), vp %p: %d\n",
407 ap->a_desc->vdesc_name, ap->a_desc->vdesc_offset, vp, rv));
408
409 return rv;
410 }
411
412 static int callremove(struct puffs_mount *, puffs_cookie_t, puffs_cookie_t,
413 struct componentname *);
414 static int callrmdir(struct puffs_mount *, puffs_cookie_t, puffs_cookie_t,
415 struct componentname *);
416 static void callinactive(struct puffs_mount *, puffs_cookie_t, int);
417 static void callreclaim(struct puffs_mount *, puffs_cookie_t);
418 static int flushvncache(struct vnode *, off_t, off_t, bool);
419 static void update_va(struct vnode *, struct vattr *, struct vattr *,
420 struct timespec *, struct timespec *, int);
421
422
423 #define PUFFS_ABORT_LOOKUP 1
424 #define PUFFS_ABORT_CREATE 2
425 #define PUFFS_ABORT_MKNOD 3
426 #define PUFFS_ABORT_MKDIR 4
427 #define PUFFS_ABORT_SYMLINK 5
428
429 /*
430 * Press the pani^Wabort button! Kernel resource allocation failed.
431 */
432 static void
433 puffs_abortbutton(struct puffs_mount *pmp, int what,
434 puffs_cookie_t dck, puffs_cookie_t ck, struct componentname *cnp)
435 {
436
437 switch (what) {
438 case PUFFS_ABORT_CREATE:
439 case PUFFS_ABORT_MKNOD:
440 case PUFFS_ABORT_SYMLINK:
441 callremove(pmp, dck, ck, cnp);
442 break;
443 case PUFFS_ABORT_MKDIR:
444 callrmdir(pmp, dck, ck, cnp);
445 break;
446 }
447
448 callinactive(pmp, ck, 0);
449 callreclaim(pmp, ck);
450 }
451
452 /*
453 * Begin vnode operations.
454 *
455 * A word from the keymaster about locks: generally we don't want
456 * to use the vnode locks at all: it creates an ugly dependency between
457 * the userlandia file server and the kernel. But we'll play along with
458 * the kernel vnode locks for now. However, even currently we attempt
459 * to release locks as early as possible. This is possible for some
460 * operations which a) don't need a locked vnode after the userspace op
461 * and b) return with the vnode unlocked. Theoretically we could
462 * unlock-do op-lock for others and order the graph in userspace, but I
463 * don't want to think of the consequences for the time being.
464 */
465
466 #define TTL_TO_TIMEOUT(ts) \
467 (hardclock_ticks + (ts->tv_sec * hz) + (ts->tv_nsec * hz / 1000000000))
468 #define TTL_VALID(ts) \
469 ((ts != NULL) && !((ts->tv_sec == 0) && (ts->tv_nsec == 0)))
470 #define TIMED_OUT(expire) \
471 ((int)((unsigned int)hardclock_ticks - (unsigned int)expire) > 0)
472 int
473 puffs_vnop_lookup(void *v)
474 {
475 struct vop_lookup_args /* {
476 const struct vnodeop_desc *a_desc;
477 struct vnode *a_dvp;
478 struct vnode **a_vpp;
479 struct componentname *a_cnp;
480 } */ *ap = v;
481 PUFFS_MSG_VARS(vn, lookup);
482 struct puffs_mount *pmp;
483 struct componentname *cnp;
484 struct vnode *vp, *dvp, *cvp;
485 struct puffs_node *dpn, *cpn;
486 int isdot;
487 int error;
488
489 pmp = MPTOPUFFSMP(ap->a_dvp->v_mount);
490 cnp = ap->a_cnp;
491 dvp = ap->a_dvp;
492 cvp = NULL;
493 cpn = NULL;
494 *ap->a_vpp = NULL;
495
496 /* r/o fs? we check create later to handle EEXIST */
497 if ((cnp->cn_flags & ISLASTCN)
498 && (dvp->v_mount->mnt_flag & MNT_RDONLY)
499 && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
500 return EROFS;
501
502 isdot = cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.';
503
504 DPRINTF(("puffs_lookup: \"%s\", parent vnode %p, op: %x\n",
505 cnp->cn_nameptr, dvp, cnp->cn_nameiop));
506
507 /*
508 * Check if someone fed it into the cache
509 */
510 if (PUFFS_USE_NAMECACHE(pmp)) {
511 error = cache_lookup(dvp, ap->a_vpp, cnp);
512
513 if ((error == 0) && PUFFS_USE_FS_TTL(pmp)) {
514
515 cvp = *ap->a_vpp;
516 cpn = VPTOPP(cvp);
517 if (TIMED_OUT(cpn->pn_cn_timeout)) {
518 cache_purge1(cvp, NULL, PURGE_CHILDREN);
519
520 /*
521 * cached vnode (cvp) is still locked
522 * so that we can reuse it upon a new
523 * successful lookup.
524 */
525 *ap->a_vpp = NULL;
526 error = -1;
527 }
528 }
529
530 /*
531 * Do not use negative caching, since the filesystem
532 * provides no TTL for it.
533 */
534 if ((error == ENOENT) && PUFFS_USE_FS_TTL(pmp))
535 error = -1;
536
537 if (error >= 0)
538 return error;
539 }
540
541 if (isdot) {
542 /* deal with rename lookup semantics */
543 if (cnp->cn_nameiop == RENAME && (cnp->cn_flags & ISLASTCN))
544 return EISDIR;
545
546 if (cvp != NULL)
547 vput(cvp);
548
549 vp = ap->a_dvp;
550 vref(vp);
551 *ap->a_vpp = vp;
552 return 0;
553 }
554
555 if (cvp != NULL)
556 mutex_enter(&cpn->pn_sizemtx);
557
558 PUFFS_MSG_ALLOC(vn, lookup);
559 puffs_makecn(&lookup_msg->pvnr_cn, &lookup_msg->pvnr_cn_cred,
560 cnp, PUFFS_USE_FULLPNBUF(pmp));
561
562 if (cnp->cn_flags & ISDOTDOT)
563 VOP_UNLOCK(dvp);
564
565 puffs_msg_setinfo(park_lookup, PUFFSOP_VN,
566 PUFFS_VN_LOOKUP, VPTOPNC(dvp));
567 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_lookup, dvp->v_data, NULL, error);
568 DPRINTF(("puffs_lookup: return of the userspace, part %d\n", error));
569
570 /*
571 * In case of error, there is no new vnode to play with, so be
572 * happy with the NULL value given to vpp in the beginning.
573 * Also, check if this really was an error or the target was not
574 * present. Either treat it as a non-error for CREATE/RENAME or
575 * enter the component into the negative name cache (if desired).
576 */
577 if (error) {
578 error = checkerr(pmp, error, __func__);
579 if (error == ENOENT) {
580 /* don't allow to create files on r/o fs */
581 if ((dvp->v_mount->mnt_flag & MNT_RDONLY)
582 && cnp->cn_nameiop == CREATE) {
583 error = EROFS;
584
585 /* adjust values if we are creating */
586 } else if ((cnp->cn_flags & ISLASTCN)
587 && (cnp->cn_nameiop == CREATE
588 || cnp->cn_nameiop == RENAME)) {
589 error = EJUSTRETURN;
590
591 /* save negative cache entry */
592 } else {
593 if ((cnp->cn_flags & MAKEENTRY)
594 && PUFFS_USE_NAMECACHE(pmp)
595 && !PUFFS_USE_FS_TTL(pmp))
596 cache_enter(dvp, NULL, cnp);
597 }
598 }
599 goto out;
600 }
601
602 /*
603 * Check that we don't get our parent node back, that would cause
604 * a pretty obvious deadlock.
605 */
606 dpn = dvp->v_data;
607 if (lookup_msg->pvnr_newnode == dpn->pn_cookie) {
608 puffs_senderr(pmp, PUFFS_ERR_LOOKUP, EINVAL,
609 "lookup produced parent cookie", lookup_msg->pvnr_newnode);
610 error = EPROTO;
611 goto out;
612 }
613
614 /*
615 * On successfull relookup, do not create a new node.
616 */
617 if (cvp != NULL) {
618 vp = cvp;
619 } else {
620 error = puffs_cookie2vnode(pmp, lookup_msg->pvnr_newnode,
621 1, 1, &vp);
622
623 if (error == PUFFS_NOSUCHCOOKIE) {
624 error = puffs_getvnode(dvp->v_mount,
625 lookup_msg->pvnr_newnode, lookup_msg->pvnr_vtype,
626 lookup_msg->pvnr_size, lookup_msg->pvnr_rdev, &vp);
627 if (error) {
628 puffs_abortbutton(pmp, PUFFS_ABORT_LOOKUP,
629 VPTOPNC(dvp), lookup_msg->pvnr_newnode,
630 ap->a_cnp);
631 goto out;
632 }
633
634 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
635 } else if (error) {
636 puffs_abortbutton(pmp, PUFFS_ABORT_LOOKUP, VPTOPNC(dvp),
637 lookup_msg->pvnr_newnode, ap->a_cnp);
638 goto out;
639 }
640 }
641
642 /*
643 * Update cache and TTL
644 */
645 if (PUFFS_USE_FS_TTL(pmp)) {
646 struct timespec *va_ttl = &lookup_msg->pvnr_va_ttl;
647 struct timespec *cn_ttl = &lookup_msg->pvnr_cn_ttl;
648 update_va(vp, NULL, &lookup_msg->pvnr_va,
649 va_ttl, cn_ttl, SETATTR_CHSIZE);
650 }
651
652 *ap->a_vpp = vp;
653
654 if ((cnp->cn_flags & MAKEENTRY) != 0 && PUFFS_USE_NAMECACHE(pmp))
655 cache_enter(dvp, vp, cnp);
656
657 /* XXX */
658 if ((lookup_msg->pvnr_cn.pkcn_flags & REQUIREDIR) == 0)
659 cnp->cn_flags &= ~REQUIREDIR;
660 if (lookup_msg->pvnr_cn.pkcn_consume)
661 cnp->cn_consume = MIN(lookup_msg->pvnr_cn.pkcn_consume,
662 strlen(cnp->cn_nameptr) - cnp->cn_namelen);
663
664 out:
665 if (cvp != NULL) {
666 mutex_exit(&cpn->pn_sizemtx);
667 /*
668 * We had a cached vnode but new lookup failed,
669 * unlock it and let it die now.
670 */
671 if (error != 0)
672 vput(cvp);
673 }
674
675 if (cnp->cn_flags & ISDOTDOT)
676 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
677
678 DPRINTF(("puffs_lookup: returning %d %p\n", error, *ap->a_vpp));
679 PUFFS_MSG_RELEASE(lookup);
680 return error;
681 }
682
683 #define REFPN_AND_UNLOCKVP(a, b) \
684 do { \
685 mutex_enter(&b->pn_mtx); \
686 puffs_referencenode(b); \
687 mutex_exit(&b->pn_mtx); \
688 VOP_UNLOCK(a); \
689 } while (/*CONSTCOND*/0)
690
691 #define REFPN(b) \
692 do { \
693 mutex_enter(&b->pn_mtx); \
694 puffs_referencenode(b); \
695 mutex_exit(&b->pn_mtx); \
696 } while (/*CONSTCOND*/0)
697
698 #define RELEPN_AND_VP(a, b) \
699 do { \
700 puffs_releasenode(b); \
701 vrele(a); \
702 } while (/*CONSTCOND*/0)
703
704 int
705 puffs_vnop_create(void *v)
706 {
707 struct vop_create_args /* {
708 const struct vnodeop_desc *a_desc;
709 struct vnode *a_dvp;
710 struct vnode **a_vpp;
711 struct componentname *a_cnp;
712 struct vattr *a_vap;
713 } */ *ap = v;
714 PUFFS_MSG_VARS(vn, create);
715 struct vnode *dvp = ap->a_dvp;
716 struct puffs_node *dpn = VPTOPP(dvp);
717 struct componentname *cnp = ap->a_cnp;
718 struct mount *mp = dvp->v_mount;
719 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
720 int error;
721
722 DPRINTF(("puffs_create: dvp %p, cnp: %s\n",
723 dvp, ap->a_cnp->cn_nameptr));
724
725 PUFFS_MSG_ALLOC(vn, create);
726 puffs_makecn(&create_msg->pvnr_cn, &create_msg->pvnr_cn_cred,
727 cnp, PUFFS_USE_FULLPNBUF(pmp));
728 create_msg->pvnr_va = *ap->a_vap;
729 puffs_msg_setinfo(park_create, PUFFSOP_VN,
730 PUFFS_VN_CREATE, VPTOPNC(dvp));
731 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_create, dvp->v_data, NULL, error);
732
733 error = checkerr(pmp, error, __func__);
734 if (error)
735 goto out;
736
737 error = puffs_newnode(mp, dvp, ap->a_vpp,
738 create_msg->pvnr_newnode, cnp, ap->a_vap->va_type, 0);
739 if (error) {
740 puffs_abortbutton(pmp, PUFFS_ABORT_CREATE, dpn->pn_cookie,
741 create_msg->pvnr_newnode, cnp);
742 goto out;
743 }
744
745 if (PUFFS_USE_FS_TTL(pmp)) {
746 struct timespec *va_ttl = &create_msg->pvnr_va_ttl;
747 struct timespec *cn_ttl = &create_msg->pvnr_cn_ttl;
748 struct vattr *rvap = &create_msg->pvnr_va;
749
750 update_va(*ap->a_vpp, NULL, rvap,
751 va_ttl, cn_ttl, SETATTR_CHSIZE);
752 }
753
754 out:
755 vput(dvp);
756
757 DPRINTF(("puffs_create: return %d\n", error));
758 PUFFS_MSG_RELEASE(create);
759 return error;
760 }
761
762 int
763 puffs_vnop_mknod(void *v)
764 {
765 struct vop_mknod_args /* {
766 const struct vnodeop_desc *a_desc;
767 struct vnode *a_dvp;
768 struct vnode **a_vpp;
769 struct componentname *a_cnp;
770 struct vattr *a_vap;
771 } */ *ap = v;
772 PUFFS_MSG_VARS(vn, mknod);
773 struct vnode *dvp = ap->a_dvp;
774 struct puffs_node *dpn = VPTOPP(dvp);
775 struct componentname *cnp = ap->a_cnp;
776 struct mount *mp = dvp->v_mount;
777 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
778 int error;
779
780 PUFFS_MSG_ALLOC(vn, mknod);
781 puffs_makecn(&mknod_msg->pvnr_cn, &mknod_msg->pvnr_cn_cred,
782 cnp, PUFFS_USE_FULLPNBUF(pmp));
783 mknod_msg->pvnr_va = *ap->a_vap;
784 puffs_msg_setinfo(park_mknod, PUFFSOP_VN,
785 PUFFS_VN_MKNOD, VPTOPNC(dvp));
786
787 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mknod, dvp->v_data, NULL, error);
788
789 error = checkerr(pmp, error, __func__);
790 if (error)
791 goto out;
792
793 error = puffs_newnode(mp, dvp, ap->a_vpp,
794 mknod_msg->pvnr_newnode, cnp, ap->a_vap->va_type,
795 ap->a_vap->va_rdev);
796 if (error) {
797 puffs_abortbutton(pmp, PUFFS_ABORT_MKNOD, dpn->pn_cookie,
798 mknod_msg->pvnr_newnode, cnp);
799 goto out;
800 }
801
802 if (PUFFS_USE_FS_TTL(pmp)) {
803 struct timespec *va_ttl = &mknod_msg->pvnr_va_ttl;
804 struct timespec *cn_ttl = &mknod_msg->pvnr_cn_ttl;
805 struct vattr *rvap = &mknod_msg->pvnr_va;
806
807 update_va(*ap->a_vpp, NULL, rvap,
808 va_ttl, cn_ttl, SETATTR_CHSIZE);
809 }
810
811 out:
812 vput(dvp);
813 PUFFS_MSG_RELEASE(mknod);
814 return error;
815 }
816
817 int
818 puffs_vnop_open(void *v)
819 {
820 struct vop_open_args /* {
821 const struct vnodeop_desc *a_desc;
822 struct vnode *a_vp;
823 int a_mode;
824 kauth_cred_t a_cred;
825 } */ *ap = v;
826 PUFFS_MSG_VARS(vn, open);
827 struct vnode *vp = ap->a_vp;
828 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
829 int mode = ap->a_mode;
830 int error;
831
832 DPRINTF(("puffs_open: vp %p, mode 0x%x\n", vp, mode));
833
834 if (vp->v_type == VREG && mode & FWRITE && !EXISTSOP(pmp, WRITE))
835 ERROUT(EROFS);
836
837 if (!EXISTSOP(pmp, OPEN))
838 ERROUT(0);
839
840 PUFFS_MSG_ALLOC(vn, open);
841 open_msg->pvnr_mode = mode;
842 puffs_credcvt(&open_msg->pvnr_cred, ap->a_cred);
843 puffs_msg_setinfo(park_open, PUFFSOP_VN,
844 PUFFS_VN_OPEN, VPTOPNC(vp));
845
846 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_open, vp->v_data, NULL, error);
847 error = checkerr(pmp, error, __func__);
848
849 out:
850 DPRINTF(("puffs_open: returning %d\n", error));
851 PUFFS_MSG_RELEASE(open);
852 return error;
853 }
854
855 int
856 puffs_vnop_close(void *v)
857 {
858 struct vop_close_args /* {
859 const struct vnodeop_desc *a_desc;
860 struct vnode *a_vp;
861 int a_fflag;
862 kauth_cred_t a_cred;
863 } */ *ap = v;
864 PUFFS_MSG_VARS(vn, close);
865 struct vnode *vp = ap->a_vp;
866 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
867
868 PUFFS_MSG_ALLOC(vn, close);
869 puffs_msg_setfaf(park_close);
870 close_msg->pvnr_fflag = ap->a_fflag;
871 puffs_credcvt(&close_msg->pvnr_cred, ap->a_cred);
872 puffs_msg_setinfo(park_close, PUFFSOP_VN,
873 PUFFS_VN_CLOSE, VPTOPNC(vp));
874
875 puffs_msg_enqueue(pmp, park_close);
876 PUFFS_MSG_RELEASE(close);
877 return 0;
878 }
879
880 int
881 puffs_vnop_access(void *v)
882 {
883 struct vop_access_args /* {
884 const struct vnodeop_desc *a_desc;
885 struct vnode *a_vp;
886 int a_mode;
887 kauth_cred_t a_cred;
888 } */ *ap = v;
889 PUFFS_MSG_VARS(vn, access);
890 struct vnode *vp = ap->a_vp;
891 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
892 int mode = ap->a_mode;
893 int error;
894
895 if (mode & VWRITE) {
896 switch (vp->v_type) {
897 case VDIR:
898 case VLNK:
899 case VREG:
900 if ((vp->v_mount->mnt_flag & MNT_RDONLY)
901 || !EXISTSOP(pmp, WRITE))
902 return EROFS;
903 break;
904 default:
905 break;
906 }
907 }
908
909 if (!EXISTSOP(pmp, ACCESS))
910 return 0;
911
912 PUFFS_MSG_ALLOC(vn, access);
913 access_msg->pvnr_mode = ap->a_mode;
914 puffs_credcvt(&access_msg->pvnr_cred, ap->a_cred);
915 puffs_msg_setinfo(park_access, PUFFSOP_VN,
916 PUFFS_VN_ACCESS, VPTOPNC(vp));
917
918 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_access, vp->v_data, NULL, error);
919 error = checkerr(pmp, error, __func__);
920 PUFFS_MSG_RELEASE(access);
921
922 return error;
923 }
924
925 static void
926 update_va(struct vnode *vp, struct vattr *vap, struct vattr *rvap,
927 struct timespec *va_ttl, struct timespec *cn_ttl, int flags)
928 {
929 struct puffs_node *pn = VPTOPP(vp);
930
931 if (TTL_VALID(cn_ttl))
932 pn->pn_cn_timeout = TTL_TO_TIMEOUT(cn_ttl);
933
934 /*
935 * Don't listen to the file server regarding special device
936 * size info, the file server doesn't know anything about them.
937 */
938 if (vp->v_type == VBLK || vp->v_type == VCHR)
939 rvap->va_size = vp->v_size;
940
941 /* Ditto for blocksize (ufs comment: this doesn't belong here) */
942 if (vp->v_type == VBLK)
943 rvap->va_blocksize = BLKDEV_IOSIZE;
944 else if (vp->v_type == VCHR)
945 rvap->va_blocksize = MAXBSIZE;
946
947 if (vap != NULL) {
948 (void) memcpy(vap, rvap, sizeof(struct vattr));
949 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
950
951 if (pn->pn_stat & PNODE_METACACHE_ATIME)
952 vap->va_atime = pn->pn_mc_atime;
953 if (pn->pn_stat & PNODE_METACACHE_CTIME)
954 vap->va_ctime = pn->pn_mc_ctime;
955 if (pn->pn_stat & PNODE_METACACHE_MTIME)
956 vap->va_mtime = pn->pn_mc_mtime;
957 if (pn->pn_stat & PNODE_METACACHE_SIZE)
958 vap->va_size = pn->pn_mc_size;
959 }
960
961 if (!(pn->pn_stat & PNODE_METACACHE_SIZE) && (flags & SETATTR_CHSIZE)) {
962 if (rvap->va_size != VNOVAL
963 && vp->v_type != VBLK && vp->v_type != VCHR) {
964 uvm_vnp_setsize(vp, rvap->va_size);
965 pn->pn_serversize = rvap->va_size;
966 }
967 }
968
969 if ((va_ttl != NULL) && TTL_VALID(va_ttl)) {
970 if (pn->pn_va_cache == NULL)
971 pn->pn_va_cache = pool_get(&puffs_vapool, PR_WAITOK);
972
973 (void)memcpy(pn->pn_va_cache, rvap, sizeof(*rvap));
974
975 pn->pn_va_timeout = TTL_TO_TIMEOUT(va_ttl);
976 }
977 }
978
979 int
980 puffs_vnop_getattr(void *v)
981 {
982 struct vop_getattr_args /* {
983 const struct vnodeop_desc *a_desc;
984 struct vnode *a_vp;
985 struct vattr *a_vap;
986 kauth_cred_t a_cred;
987 } */ *ap = v;
988 PUFFS_MSG_VARS(vn, getattr);
989 struct vnode *vp = ap->a_vp;
990 struct mount *mp = vp->v_mount;
991 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
992 struct vattr *vap, *rvap;
993 struct puffs_node *pn = VPTOPP(vp);
994 struct timespec *va_ttl = NULL;
995 int error = 0;
996
997 /*
998 * A lock is required so that we do not race with
999 * setattr, write and fsync when changing vp->v_size.
1000 * This is critical, since setting a stall smaler value
1001 * triggers a file truncate in uvm_vnp_setsize(), which
1002 * most of the time means data corruption (a chunk of
1003 * data is replaced by zeroes). This can be removed if
1004 * we decide one day that VOP_GETATTR must operate on
1005 * a locked vnode.
1006 *
1007 * XXX Should be useless now that VOP_GETATTR has been
1008 * fixed to always require a shared lock at least.
1009 */
1010 mutex_enter(&pn->pn_sizemtx);
1011
1012 REFPN(pn);
1013 vap = ap->a_vap;
1014
1015 if (PUFFS_USE_FS_TTL(pmp)) {
1016 if (!TIMED_OUT(pn->pn_va_timeout)) {
1017 update_va(vp, vap, pn->pn_va_cache,
1018 NULL, NULL, SETATTR_CHSIZE);
1019 goto out2;
1020 }
1021 }
1022
1023 PUFFS_MSG_ALLOC(vn, getattr);
1024 vattr_null(&getattr_msg->pvnr_va);
1025 puffs_credcvt(&getattr_msg->pvnr_cred, ap->a_cred);
1026 puffs_msg_setinfo(park_getattr, PUFFSOP_VN,
1027 PUFFS_VN_GETATTR, VPTOPNC(vp));
1028
1029 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_getattr, vp->v_data, NULL, error);
1030 error = checkerr(pmp, error, __func__);
1031 if (error)
1032 goto out;
1033
1034 rvap = &getattr_msg->pvnr_va;
1035
1036 if (PUFFS_USE_FS_TTL(pmp))
1037 va_ttl = &getattr_msg->pvnr_va_ttl;
1038
1039 update_va(vp, vap, rvap, va_ttl, NULL, SETATTR_CHSIZE);
1040
1041 out:
1042 PUFFS_MSG_RELEASE(getattr);
1043
1044 out2:
1045 puffs_releasenode(pn);
1046
1047 mutex_exit(&pn->pn_sizemtx);
1048
1049 return error;
1050 }
1051
1052 static int
1053 dosetattr(struct vnode *vp, struct vattr *vap, kauth_cred_t cred, int flags)
1054 {
1055 PUFFS_MSG_VARS(vn, setattr);
1056 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
1057 struct puffs_node *pn = vp->v_data;
1058 int error = 0;
1059
1060 KASSERT(!(flags & SETATTR_CHSIZE) || mutex_owned(&pn->pn_sizemtx));
1061
1062 if ((vp->v_mount->mnt_flag & MNT_RDONLY) &&
1063 (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL
1064 || vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL
1065 || vap->va_mode != (mode_t)VNOVAL))
1066 return EROFS;
1067
1068 if ((vp->v_mount->mnt_flag & MNT_RDONLY)
1069 && vp->v_type == VREG && vap->va_size != VNOVAL)
1070 return EROFS;
1071
1072 /*
1073 * Flush metacache first. If we are called with some explicit
1074 * parameters, treat them as information overriding metacache
1075 * information.
1076 */
1077 if (pn->pn_stat & PNODE_METACACHE_MASK) {
1078 if ((pn->pn_stat & PNODE_METACACHE_ATIME)
1079 && vap->va_atime.tv_sec == VNOVAL)
1080 vap->va_atime = pn->pn_mc_atime;
1081 if ((pn->pn_stat & PNODE_METACACHE_CTIME)
1082 && vap->va_ctime.tv_sec == VNOVAL)
1083 vap->va_ctime = pn->pn_mc_ctime;
1084 if ((pn->pn_stat & PNODE_METACACHE_MTIME)
1085 && vap->va_mtime.tv_sec == VNOVAL)
1086 vap->va_mtime = pn->pn_mc_mtime;
1087 if ((pn->pn_stat & PNODE_METACACHE_SIZE)
1088 && vap->va_size == VNOVAL)
1089 vap->va_size = pn->pn_mc_size;
1090
1091 pn->pn_stat &= ~PNODE_METACACHE_MASK;
1092 }
1093
1094 /*
1095 * Flush attribute cache so that another thread do
1096 * not get a stale value during the operation.
1097 */
1098 if (PUFFS_USE_FS_TTL(pmp))
1099 pn->pn_va_timeout = 0;
1100
1101 PUFFS_MSG_ALLOC(vn, setattr);
1102 (void)memcpy(&setattr_msg->pvnr_va, vap, sizeof(struct vattr));
1103 puffs_credcvt(&setattr_msg->pvnr_cred, cred);
1104 puffs_msg_setinfo(park_setattr, PUFFSOP_VN,
1105 PUFFS_VN_SETATTR, VPTOPNC(vp));
1106 if (flags & SETATTR_ASYNC)
1107 puffs_msg_setfaf(park_setattr);
1108
1109 puffs_msg_enqueue(pmp, park_setattr);
1110 if ((flags & SETATTR_ASYNC) == 0)
1111 error = puffs_msg_wait2(pmp, park_setattr, vp->v_data, NULL);
1112
1113 if ((error == 0) && PUFFS_USE_FS_TTL(pmp)) {
1114 struct timespec *va_ttl = &setattr_msg->pvnr_va_ttl;
1115 struct vattr *rvap = &setattr_msg->pvnr_va;
1116
1117 update_va(vp, NULL, rvap, va_ttl, NULL, flags);
1118 }
1119
1120 PUFFS_MSG_RELEASE(setattr);
1121 if ((flags & SETATTR_ASYNC) == 0) {
1122 error = checkerr(pmp, error, __func__);
1123 if (error)
1124 return error;
1125 } else {
1126 error = 0;
1127 }
1128
1129 if (vap->va_size != VNOVAL) {
1130 pn->pn_serversize = vap->va_size;
1131 if (flags & SETATTR_CHSIZE)
1132 uvm_vnp_setsize(vp, vap->va_size);
1133 }
1134
1135 return 0;
1136 }
1137
1138 int
1139 puffs_vnop_setattr(void *v)
1140 {
1141 struct vop_getattr_args /* {
1142 const struct vnodeop_desc *a_desc;
1143 struct vnode *a_vp;
1144 struct vattr *a_vap;
1145 kauth_cred_t a_cred;
1146 } */ *ap = v;
1147 struct puffs_node *pn = ap->a_vp->v_data;
1148 int error;
1149
1150 mutex_enter(&pn->pn_sizemtx);
1151 error = dosetattr(ap->a_vp, ap->a_vap, ap->a_cred, SETATTR_CHSIZE);
1152 mutex_exit(&pn->pn_sizemtx);
1153
1154 return error;
1155 }
1156
1157 static __inline int
1158 doinact(struct puffs_mount *pmp, int iaflag)
1159 {
1160
1161 if (EXISTSOP(pmp, INACTIVE))
1162 if (pmp->pmp_flags & PUFFS_KFLAG_IAONDEMAND)
1163 if (iaflag || ALLOPS(pmp))
1164 return 1;
1165 else
1166 return 0;
1167 else
1168 return 1;
1169 else
1170 return 0;
1171 }
1172
1173 static void
1174 callinactive(struct puffs_mount *pmp, puffs_cookie_t ck, int iaflag)
1175 {
1176 int error;
1177 PUFFS_MSG_VARS(vn, inactive);
1178
1179 if (doinact(pmp, iaflag)) {
1180 PUFFS_MSG_ALLOC(vn, inactive);
1181 puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
1182 PUFFS_VN_INACTIVE, ck);
1183
1184 PUFFS_MSG_ENQUEUEWAIT(pmp, park_inactive, error);
1185 PUFFS_MSG_RELEASE(inactive);
1186 }
1187 }
1188
1189 /* XXX: callinactive can't setback */
1190 int
1191 puffs_vnop_inactive(void *v)
1192 {
1193 struct vop_inactive_args /* {
1194 const struct vnodeop_desc *a_desc;
1195 struct vnode *a_vp;
1196 } */ *ap = v;
1197 PUFFS_MSG_VARS(vn, inactive);
1198 struct vnode *vp = ap->a_vp;
1199 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
1200 struct puffs_node *pnode;
1201 int error;
1202
1203 pnode = vp->v_data;
1204 mutex_enter(&pnode->pn_sizemtx);
1205
1206 if (doinact(pmp, pnode->pn_stat & PNODE_DOINACT)) {
1207 flushvncache(vp, 0, 0, false);
1208 PUFFS_MSG_ALLOC(vn, inactive);
1209 puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
1210 PUFFS_VN_INACTIVE, VPTOPNC(vp));
1211
1212 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_inactive, vp->v_data,
1213 NULL, error);
1214 PUFFS_MSG_RELEASE(inactive);
1215 }
1216 pnode->pn_stat &= ~PNODE_DOINACT;
1217
1218 /*
1219 * file server thinks it's gone? then don't be afraid care,
1220 * node's life was already all it would ever be
1221 */
1222 if (pnode->pn_stat & PNODE_NOREFS) {
1223 pnode->pn_stat |= PNODE_DYING;
1224 *ap->a_recycle = true;
1225 }
1226
1227 mutex_exit(&pnode->pn_sizemtx);
1228 VOP_UNLOCK(vp);
1229
1230 return 0;
1231 }
1232
1233 static void
1234 callreclaim(struct puffs_mount *pmp, puffs_cookie_t ck)
1235 {
1236 PUFFS_MSG_VARS(vn, reclaim);
1237
1238 if (!EXISTSOP(pmp, RECLAIM))
1239 return;
1240
1241 PUFFS_MSG_ALLOC(vn, reclaim);
1242 puffs_msg_setfaf(park_reclaim);
1243 puffs_msg_setinfo(park_reclaim, PUFFSOP_VN, PUFFS_VN_RECLAIM, ck);
1244
1245 puffs_msg_enqueue(pmp, park_reclaim);
1246 PUFFS_MSG_RELEASE(reclaim);
1247 }
1248
1249 /*
1250 * always FAF, we don't really care if the server wants to fail to
1251 * reclaim the node or not
1252 */
1253 int
1254 puffs_vnop_reclaim(void *v)
1255 {
1256 struct vop_reclaim_args /* {
1257 const struct vnodeop_desc *a_desc;
1258 struct vnode *a_vp;
1259 } */ *ap = v;
1260 struct vnode *vp = ap->a_vp;
1261 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
1262 struct puffs_node *pnode = vp->v_data;
1263 bool notifyserver = true;
1264
1265 /*
1266 * first things first: check if someone is trying to reclaim the
1267 * root vnode. do not allow that to travel to userspace.
1268 * Note that we don't need to take the lock similarly to
1269 * puffs_root(), since there is only one of us.
1270 */
1271 if (vp->v_vflag & VV_ROOT) {
1272 mutex_enter(&pmp->pmp_lock);
1273 KASSERT(pmp->pmp_root != NULL);
1274 pmp->pmp_root = NULL;
1275 mutex_exit(&pmp->pmp_lock);
1276 notifyserver = false;
1277 }
1278
1279 /*
1280 * purge info from kernel before issueing FAF, since we
1281 * don't really know when we'll get around to it after
1282 * that and someone might race us into node creation
1283 */
1284 mutex_enter(&pmp->pmp_lock);
1285 LIST_REMOVE(pnode, pn_hashent);
1286 mutex_exit(&pmp->pmp_lock);
1287
1288 if (notifyserver)
1289 callreclaim(MPTOPUFFSMP(vp->v_mount), VPTOPNC(vp));
1290
1291 puffs_putvnode(vp);
1292 vp->v_data = NULL;
1293
1294 return 0;
1295 }
1296
1297 #define CSIZE sizeof(**ap->a_cookies)
1298 int
1299 puffs_vnop_readdir(void *v)
1300 {
1301 struct vop_readdir_args /* {
1302 const struct vnodeop_desc *a_desc;
1303 struct vnode *a_vp;
1304 struct uio *a_uio;
1305 kauth_cred_t a_cred;
1306 int *a_eofflag;
1307 off_t **a_cookies;
1308 int *a_ncookies;
1309 } */ *ap = v;
1310 PUFFS_MSG_VARS(vn, readdir);
1311 struct vnode *vp = ap->a_vp;
1312 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
1313 size_t argsize, tomove, cookiemem, cookiesmax;
1314 struct uio *uio = ap->a_uio;
1315 size_t howmuch, resid;
1316 int error;
1317
1318 /*
1319 * ok, so we need: resid + cookiemem = maxreq
1320 * => resid + cookiesize * (resid/minsize) = maxreq
1321 * => resid + cookiesize/minsize * resid = maxreq
1322 * => (cookiesize/minsize + 1) * resid = maxreq
1323 * => resid = maxreq / (cookiesize/minsize + 1)
1324 *
1325 * Since cookiesize <= minsize and we're not very big on floats,
1326 * we approximate that to be 1. Therefore:
1327 *
1328 * resid = maxreq / 2;
1329 *
1330 * Well, at least we didn't have to use differential equations
1331 * or the Gram-Schmidt process.
1332 *
1333 * (yes, I'm very afraid of this)
1334 */
1335 KASSERT(CSIZE <= _DIRENT_MINSIZE((struct dirent *)0));
1336
1337 if (ap->a_cookies) {
1338 KASSERT(ap->a_ncookies != NULL);
1339 if (pmp->pmp_args.pa_fhsize == 0)
1340 return EOPNOTSUPP;
1341 resid = PUFFS_TOMOVE(uio->uio_resid, pmp) / 2;
1342 cookiesmax = resid/_DIRENT_MINSIZE((struct dirent *)0);
1343 cookiemem = ALIGN(cookiesmax*CSIZE); /* play safe */
1344 } else {
1345 resid = PUFFS_TOMOVE(uio->uio_resid, pmp);
1346 cookiesmax = 0;
1347 cookiemem = 0;
1348 }
1349
1350 argsize = sizeof(struct puffs_vnmsg_readdir);
1351 tomove = resid + cookiemem;
1352 puffs_msgmem_alloc(argsize + tomove, &park_readdir,
1353 (void *)&readdir_msg, 1);
1354
1355 puffs_credcvt(&readdir_msg->pvnr_cred, ap->a_cred);
1356 readdir_msg->pvnr_offset = uio->uio_offset;
1357 readdir_msg->pvnr_resid = resid;
1358 readdir_msg->pvnr_ncookies = cookiesmax;
1359 readdir_msg->pvnr_eofflag = 0;
1360 readdir_msg->pvnr_dentoff = cookiemem;
1361 puffs_msg_setinfo(park_readdir, PUFFSOP_VN,
1362 PUFFS_VN_READDIR, VPTOPNC(vp));
1363 puffs_msg_setdelta(park_readdir, tomove);
1364
1365 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_readdir, vp->v_data, NULL, error);
1366 error = checkerr(pmp, error, __func__);
1367 if (error)
1368 goto out;
1369
1370 /* userspace is cheating? */
1371 if (readdir_msg->pvnr_resid > resid) {
1372 puffs_senderr(pmp, PUFFS_ERR_READDIR, E2BIG,
1373 "resid grew", VPTOPNC(vp));
1374 ERROUT(EPROTO);
1375 }
1376 if (readdir_msg->pvnr_ncookies > cookiesmax) {
1377 puffs_senderr(pmp, PUFFS_ERR_READDIR, E2BIG,
1378 "too many cookies", VPTOPNC(vp));
1379 ERROUT(EPROTO);
1380 }
1381
1382 /* check eof */
1383 if (readdir_msg->pvnr_eofflag)
1384 *ap->a_eofflag = 1;
1385
1386 /* bouncy-wouncy with the directory data */
1387 howmuch = resid - readdir_msg->pvnr_resid;
1388
1389 /* force eof if no data was returned (getcwd() needs this) */
1390 if (howmuch == 0) {
1391 *ap->a_eofflag = 1;
1392 goto out;
1393 }
1394
1395 error = uiomove(readdir_msg->pvnr_data + cookiemem, howmuch, uio);
1396 if (error)
1397 goto out;
1398
1399 /* provide cookies to caller if so desired */
1400 if (ap->a_cookies) {
1401 KASSERT(curlwp != uvm.pagedaemon_lwp);
1402 *ap->a_cookies = malloc(readdir_msg->pvnr_ncookies*CSIZE,
1403 M_TEMP, M_WAITOK);
1404 *ap->a_ncookies = readdir_msg->pvnr_ncookies;
1405 memcpy(*ap->a_cookies, readdir_msg->pvnr_data,
1406 *ap->a_ncookies*CSIZE);
1407 }
1408
1409 /* next readdir starts here */
1410 uio->uio_offset = readdir_msg->pvnr_offset;
1411
1412 out:
1413 puffs_msgmem_release(park_readdir);
1414 return error;
1415 }
1416 #undef CSIZE
1417
1418 /*
1419 * poll works by consuming the bitmask in pn_revents. If there are
1420 * events available, poll returns immediately. If not, it issues a
1421 * poll to userspace, selrecords itself and returns with no available
1422 * events. When the file server returns, it executes puffs_parkdone_poll(),
1423 * where available events are added to the bitmask. selnotify() is
1424 * then also executed by that function causing us to enter here again
1425 * and hopefully find the missing bits (unless someone got them first,
1426 * in which case it starts all over again).
1427 */
1428 int
1429 puffs_vnop_poll(void *v)
1430 {
1431 struct vop_poll_args /* {
1432 const struct vnodeop_desc *a_desc;
1433 struct vnode *a_vp;
1434 int a_events;
1435 } */ *ap = v;
1436 PUFFS_MSG_VARS(vn, poll);
1437 struct vnode *vp = ap->a_vp;
1438 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
1439 struct puffs_node *pn = vp->v_data;
1440 int events, error;
1441
1442 if (EXISTSOP(pmp, POLL)) {
1443 mutex_enter(&pn->pn_mtx);
1444 events = pn->pn_revents & ap->a_events;
1445 if (events & ap->a_events) {
1446 pn->pn_revents &= ~ap->a_events;
1447 mutex_exit(&pn->pn_mtx);
1448
1449 return events;
1450 } else {
1451 puffs_referencenode(pn);
1452 mutex_exit(&pn->pn_mtx);
1453
1454 PUFFS_MSG_ALLOC(vn, poll);
1455 poll_msg->pvnr_events = ap->a_events;
1456 puffs_msg_setinfo(park_poll, PUFFSOP_VN,
1457 PUFFS_VN_POLL, VPTOPNC(vp));
1458 puffs_msg_setcall(park_poll, puffs_parkdone_poll, pn);
1459 selrecord(curlwp, &pn->pn_sel);
1460
1461 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_poll, vp->v_data,
1462 NULL, error);
1463 PUFFS_MSG_RELEASE(poll);
1464
1465 return 0;
1466 }
1467 } else {
1468 return genfs_poll(v);
1469 }
1470 }
1471
1472 static int
1473 flushvncache(struct vnode *vp, off_t offlo, off_t offhi, bool wait)
1474 {
1475 struct puffs_node *pn = VPTOPP(vp);
1476 struct vattr va;
1477 int pflags, error;
1478
1479 /* flush out information from our metacache, see vop_setattr */
1480 if (pn->pn_stat & PNODE_METACACHE_MASK
1481 && (pn->pn_stat & PNODE_DYING) == 0) {
1482 vattr_null(&va);
1483 error = dosetattr(vp, &va, FSCRED,
1484 SETATTR_CHSIZE | (wait ? 0 : SETATTR_ASYNC));
1485 if (error)
1486 return error;
1487 }
1488
1489 /*
1490 * flush pages to avoid being overly dirty
1491 */
1492 pflags = PGO_CLEANIT;
1493 if (wait)
1494 pflags |= PGO_SYNCIO;
1495
1496 mutex_enter(vp->v_interlock);
1497 return VOP_PUTPAGES(vp, trunc_page(offlo), round_page(offhi), pflags);
1498 }
1499
1500 int
1501 puffs_vnop_fsync(void *v)
1502 {
1503 struct vop_fsync_args /* {
1504 const struct vnodeop_desc *a_desc;
1505 struct vnode *a_vp;
1506 kauth_cred_t a_cred;
1507 int a_flags;
1508 off_t a_offlo;
1509 off_t a_offhi;
1510 } */ *ap = v;
1511 PUFFS_MSG_VARS(vn, fsync);
1512 struct vnode *vp;
1513 struct puffs_node *pn;
1514 struct puffs_mount *pmp;
1515 int error, dofaf;
1516
1517 vp = ap->a_vp;
1518 KASSERT(vp != NULL);
1519 pn = VPTOPP(vp);
1520 KASSERT(pn != NULL);
1521 pmp = MPTOPUFFSMP(vp->v_mount);
1522 if (ap->a_flags & FSYNC_WAIT) {
1523 mutex_enter(&pn->pn_sizemtx);
1524 } else {
1525 if (mutex_tryenter(&pn->pn_sizemtx) == 0)
1526 return EDEADLK;
1527 }
1528
1529 error = flushvncache(vp, ap->a_offlo, ap->a_offhi,
1530 (ap->a_flags & FSYNC_WAIT) == FSYNC_WAIT);
1531 if (error)
1532 goto out;
1533
1534 /*
1535 * HELLO! We exit already here if the user server does not
1536 * support fsync OR if we should call fsync for a node which
1537 * has references neither in the kernel or the fs server.
1538 * Otherwise we continue to issue fsync() forward.
1539 */
1540 error = 0;
1541 if (!EXISTSOP(pmp, FSYNC) || (pn->pn_stat & PNODE_DYING))
1542 goto out;
1543
1544 dofaf = (ap->a_flags & FSYNC_WAIT) == 0 || ap->a_flags == FSYNC_LAZY;
1545 /*
1546 * We abuse VXLOCK to mean "vnode is going to die", so we issue
1547 * only FAFs for those. Otherwise there's a danger of deadlock,
1548 * since the execution context here might be the user server
1549 * doing some operation on another fs, which in turn caused a
1550 * vnode to be reclaimed from the freelist for this fs.
1551 */
1552 if (dofaf == 0) {
1553 mutex_enter(vp->v_interlock);
1554 if (vp->v_iflag & VI_XLOCK)
1555 dofaf = 1;
1556 mutex_exit(vp->v_interlock);
1557 }
1558
1559 PUFFS_MSG_ALLOC(vn, fsync);
1560 if (dofaf)
1561 puffs_msg_setfaf(park_fsync);
1562
1563 puffs_credcvt(&fsync_msg->pvnr_cred, ap->a_cred);
1564 fsync_msg->pvnr_flags = ap->a_flags;
1565 fsync_msg->pvnr_offlo = ap->a_offlo;
1566 fsync_msg->pvnr_offhi = ap->a_offhi;
1567 puffs_msg_setinfo(park_fsync, PUFFSOP_VN,
1568 PUFFS_VN_FSYNC, VPTOPNC(vp));
1569
1570 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_fsync, vp->v_data, NULL, error);
1571 PUFFS_MSG_RELEASE(fsync);
1572
1573 error = checkerr(pmp, error, __func__);
1574
1575 out:
1576 mutex_exit(&pn->pn_sizemtx);
1577 return error;
1578 }
1579
1580 int
1581 puffs_vnop_seek(void *v)
1582 {
1583 struct vop_seek_args /* {
1584 const struct vnodeop_desc *a_desc;
1585 struct vnode *a_vp;
1586 off_t a_oldoff;
1587 off_t a_newoff;
1588 kauth_cred_t a_cred;
1589 } */ *ap = v;
1590 PUFFS_MSG_VARS(vn, seek);
1591 struct vnode *vp = ap->a_vp;
1592 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
1593 int error;
1594
1595 PUFFS_MSG_ALLOC(vn, seek);
1596 seek_msg->pvnr_oldoff = ap->a_oldoff;
1597 seek_msg->pvnr_newoff = ap->a_newoff;
1598 puffs_credcvt(&seek_msg->pvnr_cred, ap->a_cred);
1599 puffs_msg_setinfo(park_seek, PUFFSOP_VN,
1600 PUFFS_VN_SEEK, VPTOPNC(vp));
1601
1602 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_seek, vp->v_data, NULL, error);
1603 PUFFS_MSG_RELEASE(seek);
1604 return checkerr(pmp, error, __func__);
1605 }
1606
1607 static int
1608 callremove(struct puffs_mount *pmp, puffs_cookie_t dck, puffs_cookie_t ck,
1609 struct componentname *cnp)
1610 {
1611 PUFFS_MSG_VARS(vn, remove);
1612 int error;
1613
1614 PUFFS_MSG_ALLOC(vn, remove);
1615 remove_msg->pvnr_cookie_targ = ck;
1616 puffs_makecn(&remove_msg->pvnr_cn, &remove_msg->pvnr_cn_cred,
1617 cnp, PUFFS_USE_FULLPNBUF(pmp));
1618 puffs_msg_setinfo(park_remove, PUFFSOP_VN, PUFFS_VN_REMOVE, dck);
1619
1620 PUFFS_MSG_ENQUEUEWAIT(pmp, park_remove, error);
1621 PUFFS_MSG_RELEASE(remove);
1622
1623 return checkerr(pmp, error, __func__);
1624 }
1625
1626 /*
1627 * XXX: can't use callremove now because can't catch setbacks with
1628 * it due to lack of a pnode argument.
1629 */
1630 int
1631 puffs_vnop_remove(void *v)
1632 {
1633 struct vop_remove_args /* {
1634 const struct vnodeop_desc *a_desc;
1635 struct vnode *a_dvp;
1636 struct vnode *a_vp;
1637 struct componentname *a_cnp;
1638 } */ *ap = v;
1639 PUFFS_MSG_VARS(vn, remove);
1640 struct vnode *dvp = ap->a_dvp;
1641 struct vnode *vp = ap->a_vp;
1642 struct puffs_node *dpn = VPTOPP(dvp);
1643 struct puffs_node *pn = VPTOPP(vp);
1644 struct componentname *cnp = ap->a_cnp;
1645 struct mount *mp = dvp->v_mount;
1646 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
1647 int error;
1648
1649 PUFFS_MSG_ALLOC(vn, remove);
1650 remove_msg->pvnr_cookie_targ = VPTOPNC(vp);
1651 puffs_makecn(&remove_msg->pvnr_cn, &remove_msg->pvnr_cn_cred,
1652 cnp, PUFFS_USE_FULLPNBUF(pmp));
1653 puffs_msg_setinfo(park_remove, PUFFSOP_VN,
1654 PUFFS_VN_REMOVE, VPTOPNC(dvp));
1655
1656 puffs_msg_enqueue(pmp, park_remove);
1657 REFPN_AND_UNLOCKVP(dvp, dpn);
1658 if (dvp == vp)
1659 REFPN(pn);
1660 else
1661 REFPN_AND_UNLOCKVP(vp, pn);
1662 error = puffs_msg_wait2(pmp, park_remove, dpn, pn);
1663
1664 PUFFS_MSG_RELEASE(remove);
1665
1666 RELEPN_AND_VP(dvp, dpn);
1667 RELEPN_AND_VP(vp, pn);
1668
1669 error = checkerr(pmp, error, __func__);
1670 return error;
1671 }
1672
1673 int
1674 puffs_vnop_mkdir(void *v)
1675 {
1676 struct vop_mkdir_args /* {
1677 const struct vnodeop_desc *a_desc;
1678 struct vnode *a_dvp;
1679 struct vnode **a_vpp;
1680 struct componentname *a_cnp;
1681 struct vattr *a_vap;
1682 } */ *ap = v;
1683 PUFFS_MSG_VARS(vn, mkdir);
1684 struct vnode *dvp = ap->a_dvp;
1685 struct puffs_node *dpn = VPTOPP(dvp);
1686 struct componentname *cnp = ap->a_cnp;
1687 struct mount *mp = dvp->v_mount;
1688 struct puffs_mount *pmp = MPTOPUFFSMP(mp);
1689 int error;
1690
1691 PUFFS_MSG_ALLOC(vn, mkdir);
1692 puffs_makecn(&mkdir_msg->pvnr_cn, &mkdir_msg->pvnr_cn_cred,
1693 cnp, PUFFS_USE_FULLPNBUF(pmp));
1694 mkdir_msg->pvnr_va = *ap->a_vap;
1695 puffs_msg_setinfo(park_mkdir, PUFFSOP_VN,
1696 PUFFS_VN_MKDIR, VPTOPNC(dvp));
1697
1698 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mkdir, dvp->v_data, NULL, error);
1699
1700 error = checkerr(pmp, error, __func__);
1701 if (error)
1702 goto out;
1703
1704 error = puffs_newnode(mp, dvp, ap->a_vpp,
1705 mkdir_msg->pvnr_newnode, cnp, VDIR, 0);
1706 if (error) {
1707 puffs_abortbutton(pmp, PUFFS_ABORT_MKDIR, dpn->pn_cookie,
1708 mkdir_msg->pvnr_newnode, cnp);
1709 goto out;
1710 }
1711
1712 if (PUFFS_USE_FS_TTL(pmp)) {
1713 struct timespec *va_ttl = &mkdir_msg->pvnr_va_ttl;
1714 struct timespec *cn_ttl = &mkdir_msg->pvnr_cn_ttl;
1715 struct vattr *rvap = &mkdir_msg->pvnr_va;
1716
1717 update_va(*ap->a_vpp, NULL, rvap,
1718 va_ttl, cn_ttl, SETATTR_CHSIZE);
1719 }
1720
1721 out:
1722 vput(dvp);
1723 PUFFS_MSG_RELEASE(mkdir);
1724 return error;
1725 }
1726
1727 static int
1728 callrmdir(struct puffs_mount *pmp, puffs_cookie_t dck, puffs_cookie_t ck,
1729 struct componentname *cnp)
1730 {
1731 PUFFS_MSG_VARS(vn, rmdir);
1732 int error;
1733
1734 PUFFS_MSG_ALLOC(vn, rmdir);
1735 rmdir_msg->pvnr_cookie_targ = ck;
1736 puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
1737 cnp, PUFFS_USE_FULLPNBUF(pmp));
1738 puffs_msg_setinfo(park_rmdir, PUFFSOP_VN, PUFFS_VN_RMDIR, dck);
1739
1740 PUFFS_MSG_ENQUEUEWAIT(pmp, park_rmdir, error);
1741 PUFFS_MSG_RELEASE(rmdir);
1742
1743 return checkerr(pmp, error, __func__);
1744 }
1745
1746 int
1747 puffs_vnop_rmdir(void *v)
1748 {
1749 struct vop_rmdir_args /* {
1750 const struct vnodeop_desc *a_desc;
1751 struct vnode *a_dvp;
1752 struct vnode *a_vp;
1753 struct componentname *a_cnp;
1754 } */ *ap = v;
1755 PUFFS_MSG_VARS(vn, rmdir);
1756 struct vnode *dvp = ap->a_dvp;
1757 struct vnode *vp = ap->a_vp;
1758 struct puffs_node *dpn = VPTOPP(dvp);
1759 struct puffs_node *pn = VPTOPP(vp);
1760 struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
1761 struct componentname *cnp = ap->a_cnp;
1762 int error;
1763
1764 PUFFS_MSG_ALLOC(vn, rmdir);
1765 rmdir_msg->pvnr_cookie_targ = VPTOPNC(vp);
1766 puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
1767 cnp, PUFFS_USE_FULLPNBUF(pmp));
1768 puffs_msg_setinfo(park_rmdir, PUFFSOP_VN,
1769 PUFFS_VN_RMDIR, VPTOPNC(dvp));
1770
1771 puffs_msg_enqueue(pmp, park_rmdir);
1772 REFPN_AND_UNLOCKVP(dvp, dpn);
1773 REFPN_AND_UNLOCKVP(vp, pn);
1774 error = puffs_msg_wait2(pmp, park_rmdir, dpn, pn);
1775
1776 PUFFS_MSG_RELEASE(rmdir);
1777
1778 /* XXX: some call cache_purge() *for both vnodes* here, investigate */
1779 RELEPN_AND_VP(dvp, dpn);
1780 RELEPN_AND_VP(vp, pn);
1781
1782 return error;
1783 }
1784
1785 int
1786 puffs_vnop_link(void *v)
1787 {
1788 struct vop_link_args /* {
1789 const struct vnodeop_desc *a_desc;
1790 struct vnode *a_dvp;
1791 struct vnode *a_vp;
1792 struct componentname *a_cnp;
1793 } */ *ap = v;
1794 PUFFS_MSG_VARS(vn, link);
1795 struct vnode *dvp = ap->a_dvp;
1796 struct vnode *vp = ap->a_vp;
1797 struct puffs_node *dpn = VPTOPP(dvp);
1798 struct puffs_node *pn = VPTOPP(vp);
1799 struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
1800 struct componentname *cnp = ap->a_cnp;
1801 int error;
1802
1803 PUFFS_MSG_ALLOC(vn, link);
1804 link_msg->pvnr_cookie_targ = VPTOPNC(vp);
1805 puffs_makecn(&link_msg->pvnr_cn, &link_msg->pvnr_cn_cred,
1806 cnp, PUFFS_USE_FULLPNBUF(pmp));
1807 puffs_msg_setinfo(park_link, PUFFSOP_VN,
1808 PUFFS_VN_LINK, VPTOPNC(dvp));
1809
1810 puffs_msg_enqueue(pmp, park_link);
1811 REFPN_AND_UNLOCKVP(dvp, dpn);
1812 REFPN(pn);
1813 error = puffs_msg_wait2(pmp, park_link, dpn, pn);
1814
1815 PUFFS_MSG_RELEASE(link);
1816
1817 error = checkerr(pmp, error, __func__);
1818
1819 /*
1820 * XXX: stay in touch with the cache. I don't like this, but
1821 * don't have a better solution either. See also puffs_rename().
1822 */
1823 if (error == 0)
1824 puffs_updatenode(pn, PUFFS_UPDATECTIME, 0);
1825
1826 RELEPN_AND_VP(dvp, dpn);
1827 puffs_releasenode(pn);
1828
1829 return error;
1830 }
1831
1832 int
1833 puffs_vnop_symlink(void *v)
1834 {
1835 struct vop_symlink_args /* {
1836 const struct vnodeop_desc *a_desc;
1837 struct vnode *a_dvp;
1838 struct vnode **a_vpp;
1839 struct componentname *a_cnp;
1840 struct vattr *a_vap;
1841 char *a_target;
1842 } */ *ap = v;
1843 PUFFS_MSG_VARS(vn, symlink);
1844 struct vnode *dvp = ap->a_dvp;
1845 struct puffs_node *dpn = VPTOPP(dvp);
1846 struct mount *mp = dvp->v_mount;
1847 struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
1848 struct componentname *cnp = ap->a_cnp;
1849 int error;
1850
1851 *ap->a_vpp = NULL;
1852
1853 PUFFS_MSG_ALLOC(vn, symlink);
1854 puffs_makecn(&symlink_msg->pvnr_cn, &symlink_msg->pvnr_cn_cred,
1855 cnp, PUFFS_USE_FULLPNBUF(pmp));
1856 symlink_msg->pvnr_va = *ap->a_vap;
1857 (void)strlcpy(symlink_msg->pvnr_link, ap->a_target,
1858 sizeof(symlink_msg->pvnr_link));
1859 puffs_msg_setinfo(park_symlink, PUFFSOP_VN,
1860 PUFFS_VN_SYMLINK, VPTOPNC(dvp));
1861
1862 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_symlink, dvp->v_data, NULL, error);
1863
1864 error = checkerr(pmp, error, __func__);
1865 if (error)
1866 goto out;
1867
1868 error = puffs_newnode(mp, dvp, ap->a_vpp,
1869 symlink_msg->pvnr_newnode, cnp, VLNK, 0);
1870 if (error) {
1871 puffs_abortbutton(pmp, PUFFS_ABORT_SYMLINK, dpn->pn_cookie,
1872 symlink_msg->pvnr_newnode, cnp);
1873 goto out;
1874 }
1875
1876 if (PUFFS_USE_FS_TTL(pmp)) {
1877 struct timespec *va_ttl = &symlink_msg->pvnr_va_ttl;
1878 struct timespec *cn_ttl = &symlink_msg->pvnr_cn_ttl;
1879 struct vattr *rvap = &symlink_msg->pvnr_va;
1880
1881 update_va(*ap->a_vpp, NULL, rvap,
1882 va_ttl, cn_ttl, SETATTR_CHSIZE);
1883 }
1884
1885 out:
1886 vput(dvp);
1887 PUFFS_MSG_RELEASE(symlink);
1888
1889 return error;
1890 }
1891
1892 int
1893 puffs_vnop_readlink(void *v)
1894 {
1895 struct vop_readlink_args /* {
1896 const struct vnodeop_desc *a_desc;
1897 struct vnode *a_vp;
1898 struct uio *a_uio;
1899 kauth_cred_t a_cred;
1900 } */ *ap = v;
1901 PUFFS_MSG_VARS(vn, readlink);
1902 struct vnode *vp = ap->a_vp;
1903 struct puffs_mount *pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
1904 size_t linklen;
1905 int error;
1906
1907 PUFFS_MSG_ALLOC(vn, readlink);
1908 puffs_credcvt(&readlink_msg->pvnr_cred, ap->a_cred);
1909 linklen = sizeof(readlink_msg->pvnr_link);
1910 readlink_msg->pvnr_linklen = linklen;
1911 puffs_msg_setinfo(park_readlink, PUFFSOP_VN,
1912 PUFFS_VN_READLINK, VPTOPNC(vp));
1913
1914 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_readlink, vp->v_data, NULL, error);
1915 error = checkerr(pmp, error, __func__);
1916 if (error)
1917 goto out;
1918
1919 /* bad bad user file server */
1920 if (readlink_msg->pvnr_linklen > linklen) {
1921 puffs_senderr(pmp, PUFFS_ERR_READLINK, E2BIG,
1922 "linklen too big", VPTOPNC(ap->a_vp));
1923 error = EPROTO;
1924 goto out;
1925 }
1926
1927 error = uiomove(&readlink_msg->pvnr_link, readlink_msg->pvnr_linklen,
1928 ap->a_uio);
1929 out:
1930 PUFFS_MSG_RELEASE(readlink);
1931 return error;
1932 }
1933
1934 int
1935 puffs_vnop_rename(void *v)
1936 {
1937 struct vop_rename_args /* {
1938 const struct vnodeop_desc *a_desc;
1939 struct vnode *a_fdvp;
1940 struct vnode *a_fvp;
1941 struct componentname *a_fcnp;
1942 struct vnode *a_tdvp;
1943 struct vnode *a_tvp;
1944 struct componentname *a_tcnp;
1945 } */ *ap = v;
1946 PUFFS_MSG_VARS(vn, rename);
1947 struct vnode *fdvp = ap->a_fdvp, *fvp = ap->a_fvp;
1948 struct vnode *tdvp = ap->a_tdvp, *tvp = ap->a_tvp;
1949 struct puffs_node *fpn = ap->a_fvp->v_data;
1950 struct puffs_mount *pmp = MPTOPUFFSMP(fdvp->v_mount);
1951 int error;
1952 bool doabort = true;
1953
1954 if ((fvp->v_mount != tdvp->v_mount) ||
1955 (tvp && (fvp->v_mount != tvp->v_mount))) {
1956 ERROUT(EXDEV);
1957 }
1958
1959 PUFFS_MSG_ALLOC(vn, rename);
1960 rename_msg->pvnr_cookie_src = VPTOPNC(fvp);
1961 rename_msg->pvnr_cookie_targdir = VPTOPNC(tdvp);
1962 if (tvp)
1963 rename_msg->pvnr_cookie_targ = VPTOPNC(tvp);
1964 else
1965 rename_msg->pvnr_cookie_targ = NULL;
1966 puffs_makecn(&rename_msg->pvnr_cn_src, &rename_msg->pvnr_cn_src_cred,
1967 ap->a_fcnp, PUFFS_USE_FULLPNBUF(pmp));
1968 puffs_makecn(&rename_msg->pvnr_cn_targ, &rename_msg->pvnr_cn_targ_cred,
1969 ap->a_tcnp, PUFFS_USE_FULLPNBUF(pmp));
1970 puffs_msg_setinfo(park_rename, PUFFSOP_VN,
1971 PUFFS_VN_RENAME, VPTOPNC(fdvp));
1972
1973 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rename, fdvp->v_data, NULL, error);
1974 doabort = false;
1975 PUFFS_MSG_RELEASE(rename);
1976 error = checkerr(pmp, error, __func__);
1977
1978 /*
1979 * XXX: stay in touch with the cache. I don't like this, but
1980 * don't have a better solution either. See also puffs_link().
1981 */
1982 if (error == 0)
1983 puffs_updatenode(fpn, PUFFS_UPDATECTIME, 0);
1984
1985 out:
1986 if (doabort)
1987 VOP_ABORTOP(tdvp, ap->a_tcnp);
1988 if (tvp != NULL)
1989 vput(tvp);
1990 if (tdvp == tvp)
1991 vrele(tdvp);
1992 else
1993 vput(tdvp);
1994
1995 if (doabort)
1996 VOP_ABORTOP(fdvp, ap->a_fcnp);
1997 vrele(fdvp);
1998 vrele(fvp);
1999
2000 return error;
2001 }
2002
2003 #define RWARGS(cont, iofl, move, offset, creds) \
2004 (cont)->pvnr_ioflag = (iofl); \
2005 (cont)->pvnr_resid = (move); \
2006 (cont)->pvnr_offset = (offset); \
2007 puffs_credcvt(&(cont)->pvnr_cred, creds)
2008
2009 int
2010 puffs_vnop_read(void *v)
2011 {
2012 struct vop_read_args /* {
2013 const struct vnodeop_desc *a_desc;
2014 struct vnode *a_vp;
2015 struct uio *a_uio;
2016 int a_ioflag;
2017 kauth_cred_t a_cred;
2018 } */ *ap = v;
2019 PUFFS_MSG_VARS(vn, read);
2020 struct vnode *vp = ap->a_vp;
2021 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2022 struct uio *uio = ap->a_uio;
2023 size_t tomove, argsize;
2024 vsize_t bytelen;
2025 int error;
2026
2027 read_msg = NULL;
2028 error = 0;
2029
2030 /* std sanity */
2031 if (uio->uio_resid == 0)
2032 return 0;
2033 if (uio->uio_offset < 0)
2034 return EINVAL;
2035
2036 if (vp->v_type == VREG && PUFFS_USE_PAGECACHE(pmp)) {
2037 const int advice = IO_ADV_DECODE(ap->a_ioflag);
2038
2039 while (uio->uio_resid > 0) {
2040 if (vp->v_size <= uio->uio_offset) {
2041 break;
2042 }
2043 bytelen = MIN(uio->uio_resid,
2044 vp->v_size - uio->uio_offset);
2045 if (bytelen == 0)
2046 break;
2047
2048 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
2049 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
2050 if (error)
2051 break;
2052 }
2053
2054 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
2055 puffs_updatenode(VPTOPP(vp), PUFFS_UPDATEATIME, 0);
2056 } else {
2057 /*
2058 * in case it's not a regular file or we're operating
2059 * uncached, do read in the old-fashioned style,
2060 * i.e. explicit read operations
2061 */
2062
2063 tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
2064 argsize = sizeof(struct puffs_vnmsg_read);
2065 puffs_msgmem_alloc(argsize + tomove, &park_read,
2066 (void *)&read_msg, 1);
2067
2068 error = 0;
2069 while (uio->uio_resid > 0) {
2070 tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
2071 memset(read_msg, 0, argsize); /* XXX: touser KASSERT */
2072 RWARGS(read_msg, ap->a_ioflag, tomove,
2073 uio->uio_offset, ap->a_cred);
2074 puffs_msg_setinfo(park_read, PUFFSOP_VN,
2075 PUFFS_VN_READ, VPTOPNC(vp));
2076 puffs_msg_setdelta(park_read, tomove);
2077
2078 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_read, vp->v_data,
2079 NULL, error);
2080 error = checkerr(pmp, error, __func__);
2081 if (error)
2082 break;
2083
2084 if (read_msg->pvnr_resid > tomove) {
2085 puffs_senderr(pmp, PUFFS_ERR_READ,
2086 E2BIG, "resid grew", VPTOPNC(ap->a_vp));
2087 error = EPROTO;
2088 break;
2089 }
2090
2091 error = uiomove(read_msg->pvnr_data,
2092 tomove - read_msg->pvnr_resid, uio);
2093
2094 /*
2095 * in case the file is out of juice, resid from
2096 * userspace is != 0. and the error-case is
2097 * quite obvious
2098 */
2099 if (error || read_msg->pvnr_resid)
2100 break;
2101 }
2102
2103 puffs_msgmem_release(park_read);
2104 }
2105
2106 return error;
2107 }
2108
2109 /*
2110 * XXX: in case of a failure, this leaves uio in a bad state.
2111 * We could theoretically copy the uio and iovecs and "replay"
2112 * them the right amount after the userspace trip, but don't
2113 * bother for now.
2114 */
2115 int
2116 puffs_vnop_write(void *v)
2117 {
2118 struct vop_write_args /* {
2119 const struct vnodeop_desc *a_desc;
2120 struct vnode *a_vp;
2121 struct uio *a_uio;
2122 int a_ioflag;
2123 kauth_cred_t a_cred;
2124 } */ *ap = v;
2125 PUFFS_MSG_VARS(vn, write);
2126 struct vnode *vp = ap->a_vp;
2127 struct puffs_node *pn = VPTOPP(vp);
2128 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2129 struct uio *uio = ap->a_uio;
2130 size_t tomove, argsize;
2131 off_t oldoff, newoff, origoff;
2132 vsize_t bytelen;
2133 int error, uflags;
2134 int ubcflags;
2135
2136 error = uflags = 0;
2137 write_msg = NULL;
2138
2139 mutex_enter(&pn->pn_sizemtx);
2140
2141 if (vp->v_type == VREG && PUFFS_USE_PAGECACHE(pmp)) {
2142 ubcflags = UBC_WRITE | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp);
2143
2144 /*
2145 * userspace *should* be allowed to control this,
2146 * but with UBC it's a bit unclear how to handle it
2147 */
2148 if (ap->a_ioflag & IO_APPEND)
2149 uio->uio_offset = vp->v_size;
2150
2151 origoff = uio->uio_offset;
2152 while (uio->uio_resid > 0) {
2153 if (vp->v_mount->mnt_flag & MNT_RELATIME)
2154 uflags |= PUFFS_UPDATEATIME;
2155 uflags |= PUFFS_UPDATECTIME;
2156 uflags |= PUFFS_UPDATEMTIME;
2157 oldoff = uio->uio_offset;
2158 bytelen = uio->uio_resid;
2159
2160 newoff = oldoff + bytelen;
2161 if (vp->v_size < newoff) {
2162 uvm_vnp_setwritesize(vp, newoff);
2163 }
2164 error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
2165 UVM_ADV_RANDOM, ubcflags);
2166
2167 /*
2168 * In case of a ubc_uiomove() error,
2169 * opt to not extend the file at all and
2170 * return an error. Otherwise, if we attempt
2171 * to clear the memory we couldn't fault to,
2172 * we might generate a kernel page fault.
2173 */
2174 if (vp->v_size < newoff) {
2175 if (error == 0) {
2176 uflags |= PUFFS_UPDATESIZE;
2177 uvm_vnp_setsize(vp, newoff);
2178 } else {
2179 uvm_vnp_setwritesize(vp, vp->v_size);
2180 }
2181 }
2182 if (error)
2183 break;
2184
2185 /*
2186 * If we're writing large files, flush to file server
2187 * every 64k. Otherwise we can very easily exhaust
2188 * kernel and user memory, as the file server cannot
2189 * really keep up with our writing speed.
2190 *
2191 * Note: this does *NOT* honor MNT_ASYNC, because
2192 * that gives userland too much say in the kernel.
2193 */
2194 if (oldoff >> 16 != uio->uio_offset >> 16) {
2195 mutex_enter(vp->v_interlock);
2196 error = VOP_PUTPAGES(vp, oldoff & ~0xffff,
2197 uio->uio_offset & ~0xffff,
2198 PGO_CLEANIT | PGO_SYNCIO);
2199 if (error)
2200 break;
2201 }
2202 }
2203
2204 /* synchronous I/O? */
2205 if (error == 0 && ap->a_ioflag & IO_SYNC) {
2206 mutex_enter(vp->v_interlock);
2207 error = VOP_PUTPAGES(vp, trunc_page(origoff),
2208 round_page(uio->uio_offset),
2209 PGO_CLEANIT | PGO_SYNCIO);
2210
2211 /* write through page cache? */
2212 } else if (error == 0 && pmp->pmp_flags & PUFFS_KFLAG_WTCACHE) {
2213 mutex_enter(vp->v_interlock);
2214 error = VOP_PUTPAGES(vp, trunc_page(origoff),
2215 round_page(uio->uio_offset), PGO_CLEANIT);
2216 }
2217
2218 puffs_updatenode(VPTOPP(vp), uflags, vp->v_size);
2219 } else {
2220 /* tomove is non-increasing */
2221 tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
2222 argsize = sizeof(struct puffs_vnmsg_write) + tomove;
2223 puffs_msgmem_alloc(argsize, &park_write, (void *)&write_msg,1);
2224
2225 while (uio->uio_resid > 0) {
2226 /* move data to buffer */
2227 tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
2228 memset(write_msg, 0, argsize); /* XXX: touser KASSERT */
2229 RWARGS(write_msg, ap->a_ioflag, tomove,
2230 uio->uio_offset, ap->a_cred);
2231 error = uiomove(write_msg->pvnr_data, tomove, uio);
2232 if (error)
2233 break;
2234
2235 /* move buffer to userspace */
2236 puffs_msg_setinfo(park_write, PUFFSOP_VN,
2237 PUFFS_VN_WRITE, VPTOPNC(vp));
2238 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_write, vp->v_data,
2239 NULL, error);
2240 error = checkerr(pmp, error, __func__);
2241 if (error)
2242 break;
2243
2244 if (write_msg->pvnr_resid > tomove) {
2245 puffs_senderr(pmp, PUFFS_ERR_WRITE,
2246 E2BIG, "resid grew", VPTOPNC(ap->a_vp));
2247 error = EPROTO;
2248 break;
2249 }
2250
2251 /* adjust file size */
2252 if (vp->v_size < uio->uio_offset)
2253 uvm_vnp_setsize(vp, uio->uio_offset);
2254
2255 /* didn't move everything? bad userspace. bail */
2256 if (write_msg->pvnr_resid != 0) {
2257 error = EIO;
2258 break;
2259 }
2260 }
2261 puffs_msgmem_release(park_write);
2262 }
2263
2264 mutex_exit(&pn->pn_sizemtx);
2265 return error;
2266 }
2267
2268 int
2269 puffs_vnop_print(void *v)
2270 {
2271 struct vop_print_args /* {
2272 struct vnode *a_vp;
2273 } */ *ap = v;
2274 PUFFS_MSG_VARS(vn, print);
2275 struct vnode *vp = ap->a_vp;
2276 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2277 struct puffs_node *pn = vp->v_data;
2278 int error;
2279
2280 /* kernel portion */
2281 printf("tag VT_PUFFS, vnode %p, puffs node: %p,\n"
2282 "\tuserspace cookie: %p", vp, pn, pn->pn_cookie);
2283 if (vp->v_type == VFIFO)
2284 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
2285 printf("\n");
2286
2287 /* userspace portion */
2288 if (EXISTSOP(pmp, PRINT)) {
2289 PUFFS_MSG_ALLOC(vn, print);
2290 puffs_msg_setinfo(park_print, PUFFSOP_VN,
2291 PUFFS_VN_PRINT, VPTOPNC(vp));
2292 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_print, vp->v_data,
2293 NULL, error);
2294 PUFFS_MSG_RELEASE(print);
2295 }
2296
2297 return 0;
2298 }
2299
2300 int
2301 puffs_vnop_pathconf(void *v)
2302 {
2303 struct vop_pathconf_args /* {
2304 const struct vnodeop_desc *a_desc;
2305 struct vnode *a_vp;
2306 int a_name;
2307 register_t *a_retval;
2308 } */ *ap = v;
2309 PUFFS_MSG_VARS(vn, pathconf);
2310 struct vnode *vp = ap->a_vp;
2311 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2312 int error;
2313
2314 PUFFS_MSG_ALLOC(vn, pathconf);
2315 pathconf_msg->pvnr_name = ap->a_name;
2316 puffs_msg_setinfo(park_pathconf, PUFFSOP_VN,
2317 PUFFS_VN_PATHCONF, VPTOPNC(vp));
2318 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_pathconf, vp->v_data, NULL, error);
2319 error = checkerr(pmp, error, __func__);
2320 if (!error)
2321 *ap->a_retval = pathconf_msg->pvnr_retval;
2322 PUFFS_MSG_RELEASE(pathconf);
2323
2324 return error;
2325 }
2326
2327 int
2328 puffs_vnop_advlock(void *v)
2329 {
2330 struct vop_advlock_args /* {
2331 const struct vnodeop_desc *a_desc;
2332 struct vnode *a_vp;
2333 void *a_id;
2334 int a_op;
2335 struct flock *a_fl;
2336 int a_flags;
2337 } */ *ap = v;
2338 PUFFS_MSG_VARS(vn, advlock);
2339 struct vnode *vp = ap->a_vp;
2340 struct puffs_node *pn = VPTOPP(vp);
2341 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2342 int error;
2343
2344 if (!EXISTSOP(pmp, ADVLOCK))
2345 return lf_advlock(ap, &pn->pn_lockf, vp->v_size);
2346
2347 PUFFS_MSG_ALLOC(vn, advlock);
2348 (void)memcpy(&advlock_msg->pvnr_fl, ap->a_fl,
2349 sizeof(advlock_msg->pvnr_fl));
2350 advlock_msg->pvnr_id = ap->a_id;
2351 advlock_msg->pvnr_op = ap->a_op;
2352 advlock_msg->pvnr_flags = ap->a_flags;
2353 puffs_msg_setinfo(park_advlock, PUFFSOP_VN,
2354 PUFFS_VN_ADVLOCK, VPTOPNC(vp));
2355 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_advlock, vp->v_data, NULL, error);
2356 error = checkerr(pmp, error, __func__);
2357 PUFFS_MSG_RELEASE(advlock);
2358
2359 return error;
2360 }
2361
2362 int
2363 puffs_vnop_abortop(void *v)
2364 {
2365 struct vop_abortop_args /* {
2366 struct vnode *a_dvp;
2367 struct componentname *a_cnp;
2368 }; */ *ap = v;
2369 PUFFS_MSG_VARS(vn, abortop);
2370 struct vnode *dvp = ap->a_dvp;
2371 struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
2372 struct componentname *cnp = ap->a_cnp;
2373
2374 if (EXISTSOP(pmp, ABORTOP)) {
2375 PUFFS_MSG_ALLOC(vn, abortop);
2376 puffs_makecn(&abortop_msg->pvnr_cn, &abortop_msg->pvnr_cn_cred,
2377 cnp, PUFFS_USE_FULLPNBUF(pmp));
2378 puffs_msg_setfaf(park_abortop);
2379 puffs_msg_setinfo(park_abortop, PUFFSOP_VN,
2380 PUFFS_VN_ABORTOP, VPTOPNC(dvp));
2381
2382 puffs_msg_enqueue(pmp, park_abortop);
2383 PUFFS_MSG_RELEASE(abortop);
2384 }
2385
2386 return genfs_abortop(v);
2387 }
2388
2389 #define BIOASYNC(bp) (bp->b_flags & B_ASYNC)
2390
2391 /*
2392 * This maps itself to PUFFS_VN_READ/WRITE for data transfer.
2393 */
2394 int
2395 puffs_vnop_strategy(void *v)
2396 {
2397 struct vop_strategy_args /* {
2398 const struct vnodeop_desc *a_desc;
2399 struct vnode *a_vp;
2400 struct buf *a_bp;
2401 } */ *ap = v;
2402 PUFFS_MSG_VARS(vn, rw);
2403 struct vnode *vp = ap->a_vp;
2404 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2405 struct puffs_node *pn;
2406 struct buf *bp;
2407 size_t argsize;
2408 size_t tomove, moved;
2409 int error, dofaf, cansleep, dobiodone;
2410
2411 pmp = MPTOPUFFSMP(vp->v_mount);
2412 bp = ap->a_bp;
2413 error = 0;
2414 dofaf = 0;
2415 cansleep = 0;
2416 pn = VPTOPP(vp);
2417 park_rw = NULL; /* explicit */
2418 dobiodone = 1;
2419
2420 if ((BUF_ISREAD(bp) && !EXISTSOP(pmp, READ))
2421 || (BUF_ISWRITE(bp) && !EXISTSOP(pmp, WRITE)))
2422 ERROUT(EOPNOTSUPP);
2423
2424 /*
2425 * Short-circuit optimization: don't flush buffer in between
2426 * VOP_INACTIVE and VOP_RECLAIM in case the node has no references.
2427 */
2428 if (pn->pn_stat & PNODE_DYING) {
2429 KASSERT(BUF_ISWRITE(bp));
2430 bp->b_resid = 0;
2431 goto out;
2432 }
2433
2434 #ifdef DIAGNOSTIC
2435 if (bp->b_bcount > pmp->pmp_msg_maxsize - PUFFS_MSGSTRUCT_MAX)
2436 panic("puffs_strategy: wildly inappropriate buf bcount %d",
2437 bp->b_bcount);
2438 #endif
2439
2440 /*
2441 * See explanation for the necessity of a FAF in puffs_fsync.
2442 *
2443 * Also, do FAF in case we're suspending.
2444 * See puffs_vfsops.c:pageflush()
2445 */
2446 if (BUF_ISWRITE(bp)) {
2447 mutex_enter(vp->v_interlock);
2448 if (vp->v_iflag & VI_XLOCK)
2449 dofaf = 1;
2450 if (pn->pn_stat & PNODE_FAF)
2451 dofaf = 1;
2452 mutex_exit(vp->v_interlock);
2453 }
2454
2455 cansleep = (curlwp == uvm.pagedaemon_lwp || dofaf) ? 0 : 1;
2456
2457 KASSERT(curlwp != uvm.pagedaemon_lwp || dofaf || BIOASYNC(bp));
2458
2459 /* allocate transport structure */
2460 tomove = PUFFS_TOMOVE(bp->b_bcount, pmp);
2461 argsize = sizeof(struct puffs_vnmsg_rw);
2462 error = puffs_msgmem_alloc(argsize + tomove, &park_rw,
2463 (void *)&rw_msg, cansleep);
2464 if (error)
2465 goto out;
2466 RWARGS(rw_msg, 0, tomove, bp->b_blkno << DEV_BSHIFT, FSCRED);
2467
2468 /* 2x2 cases: read/write, faf/nofaf */
2469 if (BUF_ISREAD(bp)) {
2470 puffs_msg_setinfo(park_rw, PUFFSOP_VN,
2471 PUFFS_VN_READ, VPTOPNC(vp));
2472 puffs_msg_setdelta(park_rw, tomove);
2473 if (BIOASYNC(bp)) {
2474 puffs_msg_setcall(park_rw,
2475 puffs_parkdone_asyncbioread, bp);
2476 puffs_msg_enqueue(pmp, park_rw);
2477 dobiodone = 0;
2478 } else {
2479 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data,
2480 NULL, error);
2481 error = checkerr(pmp, error, __func__);
2482 if (error)
2483 goto out;
2484
2485 if (rw_msg->pvnr_resid > tomove) {
2486 puffs_senderr(pmp, PUFFS_ERR_READ,
2487 E2BIG, "resid grew", VPTOPNC(vp));
2488 ERROUT(EPROTO);
2489 }
2490
2491 moved = tomove - rw_msg->pvnr_resid;
2492
2493 (void)memcpy(bp->b_data, rw_msg->pvnr_data, moved);
2494 bp->b_resid = bp->b_bcount - moved;
2495 }
2496 } else {
2497 puffs_msg_setinfo(park_rw, PUFFSOP_VN,
2498 PUFFS_VN_WRITE, VPTOPNC(vp));
2499 /*
2500 * make pages read-only before we write them if we want
2501 * write caching info
2502 */
2503 if (PUFFS_WCACHEINFO(pmp)) {
2504 struct uvm_object *uobj = &vp->v_uobj;
2505 int npages = (bp->b_bcount + PAGE_SIZE-1) >> PAGE_SHIFT;
2506 struct vm_page *vmp;
2507 int i;
2508
2509 for (i = 0; i < npages; i++) {
2510 vmp= uvm_pageratop((vaddr_t)bp->b_data
2511 + (i << PAGE_SHIFT));
2512 DPRINTF(("puffs_strategy: write-protecting "
2513 "vp %p page %p, offset %" PRId64"\n",
2514 vp, vmp, vmp->offset));
2515 mutex_enter(uobj->vmobjlock);
2516 vmp->flags |= PG_RDONLY;
2517 pmap_page_protect(vmp, VM_PROT_READ);
2518 mutex_exit(uobj->vmobjlock);
2519 }
2520 }
2521
2522 (void)memcpy(&rw_msg->pvnr_data, bp->b_data, tomove);
2523 if (dofaf) {
2524 puffs_msg_setfaf(park_rw);
2525 } else if (BIOASYNC(bp)) {
2526 puffs_msg_setcall(park_rw,
2527 puffs_parkdone_asyncbiowrite, bp);
2528 dobiodone = 0;
2529 }
2530
2531 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data, NULL, error);
2532
2533 if (dobiodone == 0)
2534 goto out;
2535
2536 /*
2537 * XXXXXXXX: wrong, but kernel can't survive strategy
2538 * failure currently. Here, have one more X: X.
2539 */
2540 if (error != ENOMEM)
2541 error = 0;
2542
2543 error = checkerr(pmp, error, __func__);
2544 if (error)
2545 goto out;
2546
2547 if (rw_msg->pvnr_resid > tomove) {
2548 puffs_senderr(pmp, PUFFS_ERR_WRITE,
2549 E2BIG, "resid grew", VPTOPNC(vp));
2550 ERROUT(EPROTO);
2551 }
2552
2553 /*
2554 * FAF moved everything. Frankly, we don't
2555 * really have a choice.
2556 */
2557 if (dofaf && error == 0)
2558 moved = tomove;
2559 else
2560 moved = tomove - rw_msg->pvnr_resid;
2561
2562 bp->b_resid = bp->b_bcount - moved;
2563 if (bp->b_resid != 0) {
2564 ERROUT(EIO);
2565 }
2566 }
2567
2568 out:
2569 if (park_rw)
2570 puffs_msgmem_release(park_rw);
2571
2572 if (error)
2573 bp->b_error = error;
2574
2575 if (error || dobiodone)
2576 biodone(bp);
2577
2578 return error;
2579 }
2580
2581 int
2582 puffs_vnop_mmap(void *v)
2583 {
2584 struct vop_mmap_args /* {
2585 const struct vnodeop_desc *a_desc;
2586 struct vnode *a_vp;
2587 vm_prot_t a_prot;
2588 kauth_cred_t a_cred;
2589 } */ *ap = v;
2590 PUFFS_MSG_VARS(vn, mmap);
2591 struct vnode *vp = ap->a_vp;
2592 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2593 int error;
2594
2595 if (!PUFFS_USE_PAGECACHE(pmp))
2596 return genfs_eopnotsupp(v);
2597
2598 if (EXISTSOP(pmp, MMAP)) {
2599 PUFFS_MSG_ALLOC(vn, mmap);
2600 mmap_msg->pvnr_prot = ap->a_prot;
2601 puffs_credcvt(&mmap_msg->pvnr_cred, ap->a_cred);
2602 puffs_msg_setinfo(park_mmap, PUFFSOP_VN,
2603 PUFFS_VN_MMAP, VPTOPNC(vp));
2604
2605 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mmap, vp->v_data, NULL, error);
2606 error = checkerr(pmp, error, __func__);
2607 PUFFS_MSG_RELEASE(mmap);
2608 } else {
2609 error = genfs_mmap(v);
2610 }
2611
2612 return error;
2613 }
2614
2615
2616 /*
2617 * The rest don't get a free trip to userspace and back, they
2618 * have to stay within the kernel.
2619 */
2620
2621 /*
2622 * bmap doesn't really make any sense for puffs, so just 1:1 map it.
2623 * well, maybe somehow, somewhere, some day ....
2624 */
2625 int
2626 puffs_vnop_bmap(void *v)
2627 {
2628 struct vop_bmap_args /* {
2629 const struct vnodeop_desc *a_desc;
2630 struct vnode *a_vp;
2631 daddr_t a_bn;
2632 struct vnode **a_vpp;
2633 daddr_t *a_bnp;
2634 int *a_runp;
2635 } */ *ap = v;
2636 struct puffs_mount *pmp;
2637
2638 pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
2639
2640 if (ap->a_vpp)
2641 *ap->a_vpp = ap->a_vp;
2642 if (ap->a_bnp)
2643 *ap->a_bnp = ap->a_bn;
2644 if (ap->a_runp)
2645 *ap->a_runp
2646 = (PUFFS_TOMOVE(pmp->pmp_msg_maxsize, pmp)>>DEV_BSHIFT) - 1;
2647
2648 return 0;
2649 }
2650
2651 /*
2652 * Handle getpages faults in puffs. We let genfs_getpages() do most
2653 * of the dirty work, but we come in this route to do accounting tasks.
2654 * If the user server has specified functions for cache notifications
2655 * about reads and/or writes, we record which type of operation we got,
2656 * for which page range, and proceed to issue a FAF notification to the
2657 * server about it.
2658 */
2659 int
2660 puffs_vnop_getpages(void *v)
2661 {
2662 struct vop_getpages_args /* {
2663 const struct vnodeop_desc *a_desc;
2664 struct vnode *a_vp;
2665 voff_t a_offset;
2666 struct vm_page **a_m;
2667 int *a_count;
2668 int a_centeridx;
2669 vm_prot_t a_access_type;
2670 int a_advice;
2671 int a_flags;
2672 } */ *ap = v;
2673 struct puffs_mount *pmp;
2674 struct puffs_node *pn;
2675 struct vnode *vp;
2676 struct vm_page **pgs;
2677 struct puffs_cacheinfo *pcinfo = NULL;
2678 struct puffs_cacherun *pcrun;
2679 void *parkmem = NULL;
2680 size_t runsizes;
2681 int i, npages, si, streakon;
2682 int error, locked, write;
2683
2684 pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
2685 npages = *ap->a_count;
2686 pgs = ap->a_m;
2687 vp = ap->a_vp;
2688 pn = vp->v_data;
2689 locked = (ap->a_flags & PGO_LOCKED) != 0;
2690 write = (ap->a_access_type & VM_PROT_WRITE) != 0;
2691
2692 /* ccg xnaht - gets Wuninitialized wrong */
2693 pcrun = NULL;
2694 runsizes = 0;
2695
2696 /*
2697 * Check that we aren't trying to fault in pages which our file
2698 * server doesn't know about. This happens if we extend a file by
2699 * skipping some pages and later try to fault in pages which
2700 * are between pn_serversize and vp_size. This check optimizes
2701 * away the common case where a file is being extended.
2702 */
2703 if (ap->a_offset >= pn->pn_serversize && ap->a_offset < vp->v_size) {
2704 struct vattr va;
2705
2706 /* try again later when we can block */
2707 if (locked)
2708 ERROUT(EBUSY);
2709
2710 mutex_exit(vp->v_interlock);
2711 vattr_null(&va);
2712 va.va_size = vp->v_size;
2713 error = dosetattr(vp, &va, FSCRED, 0);
2714 if (error)
2715 ERROUT(error);
2716 mutex_enter(vp->v_interlock);
2717 }
2718
2719 if (write && PUFFS_WCACHEINFO(pmp)) {
2720 #ifdef notnowjohn
2721 /* allocate worst-case memory */
2722 runsizes = ((npages / 2) + 1) * sizeof(struct puffs_cacherun);
2723 KASSERT(curlwp != uvm.pagedaemon_lwp || locked);
2724 pcinfo = kmem_zalloc(sizeof(struct puffs_cacheinfo) + runsize,
2725 locked ? KM_NOSLEEP : KM_SLEEP);
2726
2727 /*
2728 * can't block if we're locked and can't mess up caching
2729 * information for fs server. so come back later, please
2730 */
2731 if (pcinfo == NULL)
2732 ERROUT(ENOMEM);
2733
2734 parkmem = puffs_park_alloc(locked == 0);
2735 if (parkmem == NULL)
2736 ERROUT(ENOMEM);
2737
2738 pcrun = pcinfo->pcache_runs;
2739 #else
2740 (void)parkmem;
2741 #endif
2742 }
2743
2744 error = genfs_getpages(v);
2745 if (error)
2746 goto out;
2747
2748 if (PUFFS_WCACHEINFO(pmp) == 0)
2749 goto out;
2750
2751 /*
2752 * Let's see whose fault it was and inform the user server of
2753 * possibly read/written pages. Map pages from read faults
2754 * strictly read-only, since otherwise we might miss info on
2755 * when the page is actually write-faulted to.
2756 */
2757 if (!locked)
2758 mutex_enter(vp->v_uobj.vmobjlock);
2759 for (i = 0, si = 0, streakon = 0; i < npages; i++) {
2760 if (pgs[i] == NULL || pgs[i] == PGO_DONTCARE) {
2761 if (streakon && write) {
2762 streakon = 0;
2763 pcrun[si].pcache_runend
2764 = trunc_page(pgs[i]->offset) + PAGE_MASK;
2765 si++;
2766 }
2767 continue;
2768 }
2769 if (streakon == 0 && write) {
2770 streakon = 1;
2771 pcrun[si].pcache_runstart = pgs[i]->offset;
2772 }
2773
2774 if (!write)
2775 pgs[i]->flags |= PG_RDONLY;
2776 }
2777 /* was the last page part of our streak? */
2778 if (streakon) {
2779 pcrun[si].pcache_runend
2780 = trunc_page(pgs[i-1]->offset) + PAGE_MASK;
2781 si++;
2782 }
2783 if (!locked)
2784 mutex_exit(vp->v_uobj.vmobjlock);
2785
2786 KASSERT(si <= (npages / 2) + 1);
2787
2788 #ifdef notnowjohn
2789 /* send results to userspace */
2790 if (write)
2791 puffs_cacheop(pmp, parkmem, pcinfo,
2792 sizeof(struct puffs_cacheinfo) + runsizes, VPTOPNC(vp));
2793 #endif
2794
2795 out:
2796 if (error) {
2797 if (pcinfo != NULL)
2798 kmem_free(pcinfo,
2799 sizeof(struct puffs_cacheinfo) + runsizes);
2800 #ifdef notnowjohn
2801 if (parkmem != NULL)
2802 puffs_park_release(parkmem, 1);
2803 #endif
2804 }
2805
2806 return error;
2807 }
2808
2809 /*
2810 * Extended attribute support.
2811 */
2812
2813 int
2814 puffs_vnop_getextattr(void *v)
2815 {
2816 struct vop_getextattr_args /*
2817 struct vnode *a_vp;
2818 int a_attrnamespace;
2819 const char *a_name;
2820 struct uio *a_uio;
2821 size_t *a_size;
2822 kauth_cred_t a_cred;
2823 }; */ *ap = v;
2824 PUFFS_MSG_VARS(vn, getextattr);
2825 struct vnode *vp = ap->a_vp;
2826 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2827 int attrnamespace = ap->a_attrnamespace;
2828 const char *name = ap->a_name;
2829 struct uio *uio = ap->a_uio;
2830 size_t *sizep = ap->a_size;
2831 size_t tomove, resid;
2832 int error;
2833
2834 if (uio)
2835 resid = uio->uio_resid;
2836 else
2837 resid = 0;
2838
2839 tomove = PUFFS_TOMOVE(resid, pmp);
2840 if (tomove != resid) {
2841 error = E2BIG;
2842 goto out;
2843 }
2844
2845 puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_getextattr) + tomove,
2846 &park_getextattr, (void *)&getextattr_msg, 1);
2847
2848 getextattr_msg->pvnr_attrnamespace = attrnamespace;
2849 strlcpy(getextattr_msg->pvnr_attrname, name,
2850 sizeof(getextattr_msg->pvnr_attrname));
2851 puffs_credcvt(&getextattr_msg->pvnr_cred, ap->a_cred);
2852 if (sizep)
2853 getextattr_msg->pvnr_datasize = 1;
2854 getextattr_msg->pvnr_resid = tomove;
2855
2856 puffs_msg_setinfo(park_getextattr,
2857 PUFFSOP_VN, PUFFS_VN_GETEXTATTR, VPTOPNC(vp));
2858 puffs_msg_setdelta(park_getextattr, tomove);
2859 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_getextattr, vp->v_data, NULL, error);
2860
2861 error = checkerr(pmp, error, __func__);
2862 if (error)
2863 goto out;
2864
2865 resid = getextattr_msg->pvnr_resid;
2866 if (resid > tomove) {
2867 puffs_senderr(pmp, PUFFS_ERR_GETEXTATTR, E2BIG,
2868 "resid grew", VPTOPNC(vp));
2869 error = EPROTO;
2870 goto out;
2871 }
2872
2873 if (sizep)
2874 *sizep = getextattr_msg->pvnr_datasize;
2875 if (uio)
2876 error = uiomove(getextattr_msg->pvnr_data, tomove - resid, uio);
2877
2878 out:
2879 PUFFS_MSG_RELEASE(getextattr);
2880 return error;
2881 }
2882
2883 int
2884 puffs_vnop_setextattr(void *v)
2885 {
2886 struct vop_setextattr_args /* {
2887 struct vnode *a_vp;
2888 int a_attrnamespace;
2889 const char *a_name;
2890 struct uio *a_uio;
2891 kauth_cred_t a_cred;
2892 }; */ *ap = v;
2893 PUFFS_MSG_VARS(vn, setextattr);
2894 struct vnode *vp = ap->a_vp;
2895 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2896 int attrnamespace = ap->a_attrnamespace;
2897 const char *name = ap->a_name;
2898 struct uio *uio = ap->a_uio;
2899 size_t tomove, resid;
2900 int error;
2901
2902 if (uio)
2903 resid = uio->uio_resid;
2904 else
2905 resid = 0;
2906
2907 tomove = PUFFS_TOMOVE(resid, pmp);
2908 if (tomove != resid) {
2909 error = E2BIG;
2910 goto out;
2911 }
2912
2913 puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_setextattr) + tomove,
2914 &park_setextattr, (void *)&setextattr_msg, 1);
2915
2916 setextattr_msg->pvnr_attrnamespace = attrnamespace;
2917 strlcpy(setextattr_msg->pvnr_attrname, name,
2918 sizeof(setextattr_msg->pvnr_attrname));
2919 puffs_credcvt(&setextattr_msg->pvnr_cred, ap->a_cred);
2920 setextattr_msg->pvnr_resid = tomove;
2921
2922 if (uio) {
2923 error = uiomove(setextattr_msg->pvnr_data, tomove, uio);
2924 if (error)
2925 goto out;
2926 }
2927
2928 puffs_msg_setinfo(park_setextattr,
2929 PUFFSOP_VN, PUFFS_VN_SETEXTATTR, VPTOPNC(vp));
2930 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_setextattr, vp->v_data, NULL, error);
2931
2932 error = checkerr(pmp, error, __func__);
2933 if (error)
2934 goto out;
2935
2936 if (setextattr_msg->pvnr_resid != 0)
2937 error = EIO;
2938
2939 out:
2940 PUFFS_MSG_RELEASE(setextattr);
2941
2942 return error;
2943 }
2944
2945 int
2946 puffs_vnop_listextattr(void *v)
2947 {
2948 struct vop_listextattr_args /* {
2949 struct vnode *a_vp;
2950 int a_attrnamespace;
2951 struct uio *a_uio;
2952 size_t *a_size;
2953 int a_flag,
2954 kauth_cred_t a_cred;
2955 }; */ *ap = v;
2956 PUFFS_MSG_VARS(vn, listextattr);
2957 struct vnode *vp = ap->a_vp;
2958 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
2959 int attrnamespace = ap->a_attrnamespace;
2960 struct uio *uio = ap->a_uio;
2961 size_t *sizep = ap->a_size;
2962 int flag = ap->a_flag;
2963 size_t tomove, resid;
2964 int error;
2965
2966 if (uio)
2967 resid = uio->uio_resid;
2968 else
2969 resid = 0;
2970
2971 tomove = PUFFS_TOMOVE(resid, pmp);
2972 if (tomove != resid) {
2973 error = E2BIG;
2974 goto out;
2975 }
2976
2977 puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_listextattr) + tomove,
2978 &park_listextattr, (void *)&listextattr_msg, 1);
2979
2980 listextattr_msg->pvnr_attrnamespace = attrnamespace;
2981 listextattr_msg->pvnr_flag = flag;
2982 puffs_credcvt(&listextattr_msg->pvnr_cred, ap->a_cred);
2983 listextattr_msg->pvnr_resid = tomove;
2984 if (sizep)
2985 listextattr_msg->pvnr_datasize = 1;
2986
2987 puffs_msg_setinfo(park_listextattr,
2988 PUFFSOP_VN, PUFFS_VN_LISTEXTATTR, VPTOPNC(vp));
2989 puffs_msg_setdelta(park_listextattr, tomove);
2990 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_listextattr, vp->v_data, NULL, error);
2991
2992 error = checkerr(pmp, error, __func__);
2993 if (error)
2994 goto out;
2995
2996 resid = listextattr_msg->pvnr_resid;
2997 if (resid > tomove) {
2998 puffs_senderr(pmp, PUFFS_ERR_LISTEXTATTR, E2BIG,
2999 "resid grew", VPTOPNC(vp));
3000 error = EPROTO;
3001 goto out;
3002 }
3003
3004 if (sizep)
3005 *sizep = listextattr_msg->pvnr_datasize;
3006 if (uio)
3007 error = uiomove(listextattr_msg->pvnr_data, tomove-resid, uio);
3008
3009 out:
3010 PUFFS_MSG_RELEASE(listextattr);
3011 return error;
3012 }
3013
3014 int
3015 puffs_vnop_deleteextattr(void *v)
3016 {
3017 struct vop_deleteextattr_args /* {
3018 struct vnode *a_vp;
3019 int a_attrnamespace;
3020 const char *a_name;
3021 kauth_cred_t a_cred;
3022 }; */ *ap = v;
3023 PUFFS_MSG_VARS(vn, deleteextattr);
3024 struct vnode *vp = ap->a_vp;
3025 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
3026 int attrnamespace = ap->a_attrnamespace;
3027 const char *name = ap->a_name;
3028 int error;
3029
3030 PUFFS_MSG_ALLOC(vn, deleteextattr);
3031 deleteextattr_msg->pvnr_attrnamespace = attrnamespace;
3032 strlcpy(deleteextattr_msg->pvnr_attrname, name,
3033 sizeof(deleteextattr_msg->pvnr_attrname));
3034 puffs_credcvt(&deleteextattr_msg->pvnr_cred, ap->a_cred);
3035
3036 puffs_msg_setinfo(park_deleteextattr,
3037 PUFFSOP_VN, PUFFS_VN_DELETEEXTATTR, VPTOPNC(vp));
3038 PUFFS_MSG_ENQUEUEWAIT2(pmp, park_deleteextattr,
3039 vp->v_data, NULL, error);
3040
3041 error = checkerr(pmp, error, __func__);
3042
3043 PUFFS_MSG_RELEASE(deleteextattr);
3044 return error;
3045 }
3046
3047 /*
3048 * spec & fifo. These call the miscfs spec and fifo vectors, but issue
3049 * FAF update information for the puffs node first.
3050 */
3051 int
3052 puffs_vnop_spec_read(void *v)
3053 {
3054 struct vop_read_args /* {
3055 const struct vnodeop_desc *a_desc;
3056 struct vnode *a_vp;
3057 struct uio *a_uio;
3058 int a_ioflag;
3059 kauth_cred_t a_cred;
3060 } */ *ap = v;
3061
3062 puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
3063 return VOCALL(spec_vnodeop_p, VOFFSET(vop_read), v);
3064 }
3065
3066 int
3067 puffs_vnop_spec_write(void *v)
3068 {
3069 struct vop_write_args /* {
3070 const struct vnodeop_desc *a_desc;
3071 struct vnode *a_vp;
3072 struct uio *a_uio;
3073 int a_ioflag;
3074 kauth_cred_t a_cred;
3075 } */ *ap = v;
3076
3077 puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
3078 return VOCALL(spec_vnodeop_p, VOFFSET(vop_write), v);
3079 }
3080
3081 int
3082 puffs_vnop_fifo_read(void *v)
3083 {
3084 struct vop_read_args /* {
3085 const struct vnodeop_desc *a_desc;
3086 struct vnode *a_vp;
3087 struct uio *a_uio;
3088 int a_ioflag;
3089 kauth_cred_t a_cred;
3090 } */ *ap = v;
3091
3092 puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
3093 return VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), v);
3094 }
3095
3096 int
3097 puffs_vnop_fifo_write(void *v)
3098 {
3099 struct vop_write_args /* {
3100 const struct vnodeop_desc *a_desc;
3101 struct vnode *a_vp;
3102 struct uio *a_uio;
3103 int a_ioflag;
3104 kauth_cred_t a_cred;
3105 } */ *ap = v;
3106
3107 puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
3108 return VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), v);
3109 }
3110