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