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