]>
Commit | Line | Data |
---|---|---|
f427ee49 A |
1 | /* |
2 | * Copyright (c) 2019 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | /*- | |
25 | * Portions Copyright (c) 1992, 1993 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * This code is derived from software contributed to Berkeley by | |
29 | * John Heidemann of the UCLA Ficus project. | |
30 | * | |
31 | * Redistribution and use in source and binary forms, with or without | |
32 | * modification, are permitted provided that the following conditions | |
33 | * are met: | |
34 | * 1. Redistributions of source code must retain the above copyright | |
35 | * notice, this list of conditions and the following disclaimer. | |
36 | * 2. Redistributions in binary form must reproduce the above copyright | |
37 | * notice, this list of conditions and the following disclaimer in the | |
38 | * documentation and/or other materials provided with the distribution. | |
39 | * 4. Neither the name of the University nor the names of its contributors | |
40 | * may be used to endorse or promote products derived from this software | |
41 | * without specific prior written permission. | |
42 | * | |
43 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
53 | * SUCH DAMAGE. | |
54 | * | |
55 | * @(#)null_vnops.c 8.6 (Berkeley) 5/27/95 | |
56 | * | |
57 | * Ancestors: | |
58 | * @(#)lofs_vnops.c 1.2 (Berkeley) 6/18/92 | |
59 | * ...and... | |
60 | * @(#)null_vnodeops.c 1.20 92/07/07 UCLA Ficus project | |
61 | * | |
62 | * $FreeBSD$ | |
63 | */ | |
64 | ||
65 | #include <sys/param.h> | |
66 | #include <sys/systm.h> | |
67 | #include <sys/conf.h> | |
68 | #include <sys/kernel.h> | |
69 | #include <sys/lock.h> | |
70 | #include <sys/malloc.h> | |
71 | #include <sys/mount.h> | |
72 | #include <sys/mount_internal.h> | |
73 | #include <sys/namei.h> | |
74 | #include <sys/sysctl.h> | |
75 | #include <sys/vnode.h> | |
76 | #include <sys/vnode_internal.h> | |
77 | #include <sys/xattr.h> | |
78 | #include <sys/ubc.h> | |
79 | #include <sys/types.h> | |
80 | #include <sys/dirent.h> | |
81 | ||
82 | #include "bindfs.h" | |
83 | ||
84 | #define BIND_ROOT_INO 2 | |
85 | ||
86 | vop_t * bindfs_vnodeop_p = NULL; | |
87 | ||
88 | static int | |
89 | bindfs_default(__unused struct vnop_generic_args * args) | |
90 | { | |
91 | return ENOTSUP; | |
92 | } | |
93 | ||
94 | static int | |
95 | bindfs_getattr(struct vnop_getattr_args * args) | |
96 | { | |
97 | int error; | |
98 | BINDFSDEBUG("%s %p\n", __FUNCTION__, args->a_vp); | |
99 | ||
100 | struct vnode * lowervp = BINDVPTOLOWERVP(args->a_vp); | |
101 | ||
102 | error = vnode_getwithref(lowervp); | |
103 | if (error == 0) { | |
104 | error = VNOP_GETATTR(lowervp, args->a_vap, args->a_context); | |
105 | vnode_put(lowervp); | |
106 | ||
107 | if (error == 0) { | |
108 | if (VATTR_IS_ACTIVE(args->a_vap, va_fsid)) { | |
109 | /* fix up fsid so it doesn't say the underlying fs*/ | |
110 | VATTR_RETURN(args->a_vap, va_fsid, vfs_statfs(vnode_mount(args->a_vp))->f_fsid.val[0]); | |
111 | } | |
112 | if (VATTR_IS_ACTIVE(args->a_vap, va_fsid64)) { | |
113 | /* fix up fsid so it doesn't say the underlying fs*/ | |
114 | VATTR_RETURN(args->a_vap, va_fsid64, vfs_statfs(vnode_mount(args->a_vp))->f_fsid); | |
115 | } | |
116 | struct vnode * parent = vnode_parent(args->a_vp); | |
117 | if (vnode_isvroot(args->a_vp)) { | |
118 | // We can use the lower answers for most questions about the root vnode but need to fix up a few things | |
119 | if (VATTR_IS_ACTIVE(args->a_vap, va_fileid)) { | |
120 | VATTR_RETURN(args->a_vap, va_fileid, BIND_ROOT_INO); | |
121 | } | |
122 | if (VATTR_IS_ACTIVE(args->a_vap, va_linkid)) { | |
123 | VATTR_RETURN(args->a_vap, va_linkid, BIND_ROOT_INO); | |
124 | } | |
125 | if (VATTR_IS_ACTIVE(args->a_vap, va_parentid)) { | |
126 | // The parent of the root is itself | |
127 | VATTR_RETURN(args->a_vap, va_parentid, BIND_ROOT_INO); | |
128 | } | |
129 | } else if (parent != NULL && vnode_isvroot(parent)) { | |
130 | if (VATTR_IS_ACTIVE(args->a_vap, va_parentid)) { | |
131 | // This vnode's parent is the root. | |
132 | VATTR_RETURN(args->a_vap, va_parentid, BIND_ROOT_INO); | |
133 | } | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | return error; | |
139 | } | |
140 | ||
141 | static int | |
142 | bindfs_open(struct vnop_open_args * args) | |
143 | { | |
144 | int error; | |
145 | struct vnode *vp, *lvp; | |
146 | ||
147 | BINDFSDEBUG("%s %p\n", __FUNCTION__, args->a_vp); | |
148 | ||
149 | vp = args->a_vp; | |
150 | lvp = BINDVPTOLOWERVP(vp); | |
151 | error = vnode_getwithref(lvp); | |
152 | if (error == 0) { | |
153 | error = VNOP_OPEN(lvp, args->a_mode, args->a_context); | |
154 | vnode_put(lvp); | |
155 | } | |
156 | ||
157 | return error; | |
158 | } | |
159 | ||
160 | static int | |
161 | bindfs_close(struct vnop_close_args * args) | |
162 | { | |
163 | int error; | |
164 | struct vnode *vp, *lvp; | |
165 | ||
166 | BINDFSDEBUG("%s %p\n", __FUNCTION__, args->a_vp); | |
167 | ||
168 | vp = args->a_vp; | |
169 | lvp = BINDVPTOLOWERVP(vp); | |
170 | ||
171 | error = vnode_getwithref(lvp); | |
172 | if (error == 0) { | |
173 | error = VNOP_CLOSE(lvp, args->a_fflag, args->a_context); | |
174 | vnode_put(lvp); | |
175 | } | |
176 | return error; | |
177 | } | |
178 | ||
179 | /* | |
180 | * We have to carry on the locking protocol on the bind layer vnodes | |
181 | * as we progress through the tree. We also have to enforce read-only | |
182 | * if this layer is mounted read-only. | |
183 | */ | |
184 | static int | |
185 | bind_lookup(struct vnop_lookup_args * ap) | |
186 | { | |
187 | struct componentname * cnp = ap->a_cnp; | |
188 | struct vnode * dvp = ap->a_dvp; | |
189 | struct vnode *vp, *ldvp, *lvp; | |
190 | struct mount * mp; | |
191 | struct bind_mount * bind_mp; | |
192 | int error; | |
193 | ||
194 | BINDFSDEBUG("%s parent: %p component: %.*s\n", __FUNCTION__, ap->a_dvp, cnp->cn_namelen, cnp->cn_nameptr); | |
195 | ||
196 | mp = vnode_mount(dvp); | |
197 | /* rename and delete are not allowed. this is a read only file system */ | |
198 | if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME || cnp->cn_nameiop == CREATE) { | |
199 | return EROFS; | |
200 | } | |
201 | bind_mp = MOUNTTOBINDMOUNT(mp); | |
202 | ||
203 | // . and .. handling | |
204 | if (cnp->cn_nameptr[0] == '.') { | |
205 | if (cnp->cn_namelen == 1) { | |
206 | vp = dvp; | |
207 | } else if (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.') { | |
208 | vp = (vnode_isvroot(dvp)) ? dvp : vnode_parent(dvp); | |
209 | } else { | |
210 | goto notdot; | |
211 | } | |
212 | ||
213 | error = vp ? vnode_get(vp) : ENOENT; | |
214 | ||
215 | if (error == 0) { | |
216 | *ap->a_vpp = vp; | |
217 | } | |
218 | ||
219 | return error; | |
220 | } | |
221 | ||
222 | notdot: | |
223 | ldvp = BINDVPTOLOWERVP(dvp); | |
224 | vp = lvp = NULL; | |
225 | ||
226 | /* | |
227 | * Hold ldvp. The reference on it, owned by dvp, is lost in | |
228 | * case of dvp reclamation. | |
229 | */ | |
230 | error = vnode_getwithref(ldvp); | |
231 | if (error) { | |
232 | return error; | |
233 | } | |
234 | ||
235 | error = VNOP_LOOKUP(ldvp, &lvp, cnp, ap->a_context); | |
236 | ||
237 | vnode_put(ldvp); | |
238 | ||
239 | if ((error == 0 || error == EJUSTRETURN) && lvp != NULL) { | |
240 | if (ldvp == lvp) { | |
241 | vp = dvp; | |
242 | error = vnode_get(vp); | |
243 | } else { | |
244 | error = bind_nodeget(mp, lvp, dvp, &vp, cnp, 0); | |
245 | } | |
246 | if (error == 0) { | |
247 | *ap->a_vpp = vp; | |
248 | } | |
c3c9b80d A |
249 | /* if we got lvp, drop the iocount from VNOP_LOOKUP */ |
250 | if (lvp != NULL) { | |
251 | vnode_put(lvp); | |
252 | } | |
f427ee49 A |
253 | } |
254 | ||
255 | return error; | |
256 | } | |
257 | ||
258 | /* | |
259 | * Don't think this needs to do anything | |
260 | */ | |
261 | static int | |
262 | bind_inactive(__unused struct vnop_inactive_args * ap) | |
263 | { | |
264 | BINDFSDEBUG("%s %p\n", __FUNCTION__, ap->a_vp); | |
265 | ||
266 | return 0; | |
267 | } | |
268 | ||
269 | static int | |
270 | bind_reclaim(struct vnop_reclaim_args * ap) | |
271 | { | |
272 | struct vnode * vp; | |
273 | struct bind_node * xp; | |
274 | struct vnode * lowervp; | |
275 | ||
276 | BINDFSDEBUG("%s %p\n", __FUNCTION__, ap->a_vp); | |
277 | ||
278 | vp = ap->a_vp; | |
279 | ||
280 | xp = VTOBIND(vp); | |
281 | lowervp = xp->bind_lowervp; | |
282 | ||
283 | vnode_removefsref(vp); | |
284 | ||
285 | bind_hashrem(xp); | |
286 | vnode_getwithref(lowervp); | |
287 | vnode_rele(lowervp); | |
288 | vnode_put(lowervp); | |
289 | ||
290 | cache_purge(vp); | |
291 | vnode_clearfsnode(vp); | |
292 | ||
293 | FREE(xp, M_TEMP); | |
294 | ||
295 | return 0; | |
296 | } | |
297 | ||
298 | /* Get dirent length padded to 4 byte alignment */ | |
299 | #define DIRENT_LEN(namelen) \ | |
300 | ((sizeof(struct dirent) + (namelen + 1) - (__DARWIN_MAXNAMLEN + 1) + 3) & ~3) | |
301 | ||
302 | /* Get the end of this dirent */ | |
303 | #define DIRENT_END(dep) \ | |
304 | (((char *)(dep)) + (dep)->d_reclen - 1) | |
305 | ||
306 | static int | |
307 | bindfs_readdir(struct vnop_readdir_args * ap) | |
308 | { | |
309 | struct vnode *vp, *lvp, *dvp; | |
310 | int error; | |
311 | uio_t uio = ap->a_uio; | |
312 | ||
313 | BINDFSDEBUG("%s %p\n", __FUNCTION__, ap->a_vp); | |
314 | /* assumption is that any vp that comes through here had to go through lookup | |
315 | */ | |
316 | ||
317 | if (ap->a_flags & (VNODE_READDIR_EXTENDED | VNODE_READDIR_REQSEEKOFF)) { | |
318 | return EINVAL; | |
319 | } | |
320 | ||
321 | vp = ap->a_vp; | |
322 | dvp = vnode_parent(vp); | |
323 | lvp = BINDVPTOLOWERVP(vp); | |
324 | error = vnode_getwithref(lvp); | |
325 | if (error != 0) { | |
326 | goto lb_end; | |
327 | } | |
328 | ||
329 | if (vnode_isvroot(vp) || (dvp != NULL && vnode_isvroot(dvp))) { | |
330 | size_t bufsize; | |
331 | void * bufptr; | |
332 | uio_t auio; | |
333 | struct dirent *dep; | |
334 | size_t bytesread; | |
335 | bufsize = 3 * MIN((user_size_t)uio_resid(uio), 87371u) / 8; | |
c3c9b80d | 336 | bufptr = kheap_alloc(KHEAP_TEMP, bufsize, Z_WAITOK); |
f427ee49 A |
337 | if (bufptr == NULL) { |
338 | return ENOMEM; | |
339 | } | |
340 | auio = uio_create(1, 0, UIO_SYSSPACE, UIO_READ); | |
341 | uio_addiov(auio, (uintptr_t)bufptr, bufsize); | |
342 | uio_setoffset(auio, uio_offset(uio)); | |
343 | error = VNOP_READDIR(lvp, auio, ap->a_flags, ap->a_eofflag, ap->a_numdirent, ap->a_context); | |
344 | vnode_put(lvp); | |
345 | if (error != 0) { | |
346 | goto lb_end; | |
347 | } | |
348 | ||
349 | dep = (struct dirent *)bufptr; | |
350 | bytesread = bufsize - uio_resid(auio); | |
351 | while (error == 0 && (char *)dep < ((char *)bufptr + bytesread)) { | |
352 | if (DIRENT_END(dep) > ((char *)bufptr + bytesread) || | |
353 | DIRENT_LEN(dep->d_namlen) > dep->d_reclen) { | |
354 | printf("%s: %s: Bad dirent received from directory %s\n", __func__, | |
355 | vfs_statfs(vnode_mount(vp))->f_mntonname, | |
356 | vp->v_name ? vp->v_name : "<unknown>"); | |
357 | error = EIO; | |
358 | break; | |
359 | } | |
360 | if (dep->d_name[0] == '.') { | |
361 | /* re-write the inode number for the mount root */ | |
362 | /* if vp is the mount root then . = 2 and .. = 2 */ | |
363 | /* if the parent of vp is the mount root then .. = 2 */ | |
364 | if ((vnode_isvroot(vp) && dep->d_namlen == 1) || | |
365 | (dep->d_namlen == 2 && dep->d_name[1] == '.')) { | |
366 | dep->d_ino = BIND_ROOT_INO; | |
367 | } | |
368 | } | |
369 | /* Copy entry64 to user's buffer. */ | |
370 | error = uiomove((caddr_t)dep, dep->d_reclen, uio); | |
371 | /* Move to next entry. */ | |
372 | dep = (struct dirent *)((char *)dep + dep->d_reclen); | |
373 | } | |
374 | /* Update the real offset using the offset we got from VNOP_READDIR. */ | |
375 | if (error == 0) { | |
376 | uio_setoffset(uio, uio_offset(auio)); | |
377 | } | |
378 | uio_free(auio); | |
c3c9b80d | 379 | kheap_free(KHEAP_TEMP, bufptr, bufsize); |
f427ee49 A |
380 | } else { |
381 | error = VNOP_READDIR(lvp, ap->a_uio, ap->a_flags, ap->a_eofflag, ap->a_numdirent, ap->a_context); | |
382 | vnode_put(lvp); | |
383 | } | |
384 | ||
385 | lb_end: | |
386 | return error; | |
387 | } | |
388 | ||
389 | static int | |
390 | bindfs_readlink(struct vnop_readlink_args * ap) | |
391 | { | |
392 | BINDFSDEBUG("%s %p\n", __FUNCTION__, ap->a_vp); | |
393 | int error; | |
394 | struct vnode *vp, *lvp; | |
395 | ||
396 | vp = ap->a_vp; | |
397 | lvp = BINDVPTOLOWERVP(vp); | |
398 | ||
399 | error = vnode_getwithref(lvp); | |
400 | if (error == 0) { | |
401 | error = VNOP_READLINK(lvp, ap->a_uio, ap->a_context); | |
402 | vnode_put(lvp); | |
403 | ||
404 | if (error) { | |
405 | printf("bindfs: readlink failed: %d\n", error); | |
406 | } | |
407 | } | |
408 | ||
409 | return error; | |
410 | } | |
411 | ||
412 | static int | |
413 | bindfs_pathconf(__unused struct vnop_pathconf_args * args) | |
414 | { | |
415 | BINDFSDEBUG("%s %p\n", __FUNCTION__, args->a_vp); | |
416 | return EINVAL; | |
417 | } | |
418 | ||
419 | static int | |
420 | bindfs_fsync(__unused struct vnop_fsync_args * args) | |
421 | { | |
422 | BINDFSDEBUG("%s %p\n", __FUNCTION__, args->a_vp); | |
423 | return 0; | |
424 | } | |
425 | ||
426 | static int | |
427 | bindfs_mmap(struct vnop_mmap_args * args) | |
428 | { | |
429 | int error; | |
430 | struct vnode *vp, *lvp; | |
431 | ||
432 | BINDFSDEBUG("%s %p\n", __FUNCTION__, args->a_vp); | |
433 | ||
434 | vp = args->a_vp; | |
435 | lvp = BINDVPTOLOWERVP(vp); | |
436 | error = vnode_getwithref(lvp); | |
437 | if (error == 0) { | |
438 | error = VNOP_MMAP(lvp, args->a_fflags, args->a_context); | |
439 | vnode_put(lvp); | |
440 | } | |
441 | ||
442 | return error; | |
443 | } | |
444 | ||
445 | static int | |
446 | bindfs_mnomap(struct vnop_mnomap_args * args) | |
447 | { | |
448 | int error; | |
449 | struct vnode *vp, *lvp; | |
450 | ||
451 | BINDFSDEBUG("%s %p\n", __FUNCTION__, args->a_vp); | |
452 | ||
453 | vp = args->a_vp; | |
454 | lvp = BINDVPTOLOWERVP(vp); | |
455 | error = vnode_getwithref(lvp); | |
456 | if (error == 0) { | |
457 | error = VNOP_MNOMAP(lvp, args->a_context); | |
458 | vnode_put(lvp); | |
459 | } | |
460 | ||
461 | return error; | |
462 | } | |
463 | ||
464 | static int | |
465 | bindfs_getxattr(struct vnop_getxattr_args * args) | |
466 | { | |
467 | int error; | |
468 | struct vnode *vp, *lvp; | |
469 | ||
470 | BINDFSDEBUG("%s %p\n", __FUNCTION__, args->a_vp); | |
471 | ||
472 | vp = args->a_vp; | |
473 | lvp = BINDVPTOLOWERVP(vp); | |
474 | error = vnode_getwithref(lvp); | |
475 | if (error == 0) { | |
476 | error = VNOP_GETXATTR(lvp, args->a_name, args->a_uio, args->a_size, args->a_options, args->a_context); | |
477 | vnode_put(lvp); | |
478 | } | |
479 | ||
480 | return error; | |
481 | } | |
482 | ||
483 | static int | |
484 | bindfs_listxattr(struct vnop_listxattr_args * args) | |
485 | { | |
486 | int error; | |
487 | struct vnode *vp, *lvp; | |
488 | ||
489 | BINDFSDEBUG("%s %p\n", __FUNCTION__, args->a_vp); | |
490 | ||
491 | vp = args->a_vp; | |
492 | lvp = BINDVPTOLOWERVP(vp); | |
493 | error = vnode_getwithref(lvp); | |
494 | if (error == 0) { | |
495 | error = VNOP_LISTXATTR(lvp, args->a_uio, args->a_size, args->a_options, args->a_context); | |
496 | vnode_put(lvp); | |
497 | } | |
498 | ||
499 | return error; | |
500 | } | |
501 | ||
502 | /* relies on v1 paging */ | |
503 | static int | |
504 | bindfs_pagein(struct vnop_pagein_args * ap) | |
505 | { | |
506 | int error = EIO; | |
507 | struct vnode *vp, *lvp; | |
508 | ||
509 | BINDFSDEBUG("%s %p\n", __FUNCTION__, ap->a_vp); | |
510 | ||
511 | vp = ap->a_vp; | |
512 | lvp = BINDVPTOLOWERVP(vp); | |
513 | ||
514 | if (vnode_vtype(vp) != VREG) { | |
515 | return ENOTSUP; | |
516 | } | |
517 | ||
518 | /* | |
519 | * Ask VM/UBC/VFS to do our bidding | |
520 | */ | |
521 | if (vnode_getwithvid(lvp, BINDVPTOLOWERVID(vp)) == 0) { | |
522 | vm_offset_t ioaddr; | |
523 | uio_t auio; | |
524 | kern_return_t kret; | |
525 | off_t bytes_to_commit; | |
526 | off_t lowersize; | |
527 | upl_t upl = ap->a_pl; | |
528 | user_ssize_t bytes_remaining = 0; | |
529 | ||
530 | auio = uio_create(1, ap->a_f_offset, UIO_SYSSPACE, UIO_READ); | |
531 | if (auio == NULL) { | |
532 | error = EIO; | |
533 | goto exit_no_unmap; | |
534 | } | |
535 | ||
536 | kret = ubc_upl_map(upl, &ioaddr); | |
537 | if (KERN_SUCCESS != kret) { | |
538 | panic("bindfs_pagein: ubc_upl_map() failed with (%d)", kret); | |
539 | } | |
540 | ||
541 | ioaddr += ap->a_pl_offset; | |
542 | ||
543 | error = uio_addiov(auio, (user_addr_t)ioaddr, ap->a_size); | |
544 | if (error) { | |
545 | goto exit; | |
546 | } | |
547 | ||
548 | lowersize = ubc_getsize(lvp); | |
549 | if (lowersize != ubc_getsize(vp)) { | |
550 | (void)ubc_setsize(vp, lowersize); /* ignore failures, nothing can be done */ | |
551 | } | |
552 | ||
553 | error = VNOP_READ(lvp, auio, ((ap->a_flags & UPL_IOSYNC) ? IO_SYNC : 0), ap->a_context); | |
554 | ||
555 | bytes_remaining = uio_resid(auio); | |
556 | if (bytes_remaining > 0 && bytes_remaining <= (user_ssize_t)ap->a_size) { | |
557 | /* zero bytes that weren't read in to the upl */ | |
558 | bzero((void*)((uintptr_t)(ioaddr + ap->a_size - bytes_remaining)), (size_t) bytes_remaining); | |
559 | } | |
560 | ||
561 | exit: | |
562 | kret = ubc_upl_unmap(upl); | |
563 | if (KERN_SUCCESS != kret) { | |
564 | panic("bindfs_pagein: ubc_upl_unmap() failed with (%d)", kret); | |
565 | } | |
566 | ||
567 | if (auio != NULL) { | |
568 | uio_free(auio); | |
569 | } | |
570 | ||
571 | exit_no_unmap: | |
572 | if ((ap->a_flags & UPL_NOCOMMIT) == 0) { | |
573 | if (!error && (bytes_remaining >= 0) && (bytes_remaining <= (user_ssize_t)ap->a_size)) { | |
574 | /* only commit what was read in (page aligned)*/ | |
575 | bytes_to_commit = ap->a_size - bytes_remaining; | |
576 | if (bytes_to_commit) { | |
577 | /* need to make sure bytes_to_commit and byte_remaining are page aligned before calling ubc_upl_commit_range*/ | |
578 | if (bytes_to_commit & PAGE_MASK) { | |
579 | bytes_to_commit = (bytes_to_commit & (~PAGE_MASK)) + (PAGE_MASK + 1); | |
580 | assert(bytes_to_commit <= (off_t)ap->a_size); | |
581 | ||
582 | bytes_remaining = ap->a_size - bytes_to_commit; | |
583 | } | |
584 | ubc_upl_commit_range(upl, ap->a_pl_offset, (upl_size_t)bytes_to_commit, UPL_COMMIT_FREE_ON_EMPTY); | |
585 | } | |
586 | ||
587 | /* abort anything thats left */ | |
588 | if (bytes_remaining) { | |
589 | ubc_upl_abort_range(upl, ap->a_pl_offset + (upl_offset_t)bytes_to_commit, (upl_size_t)bytes_remaining, UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY); | |
590 | } | |
591 | } else { | |
592 | ubc_upl_abort_range(upl, ap->a_pl_offset, (upl_size_t)ap->a_size, UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY); | |
593 | } | |
594 | } | |
595 | vnode_put(lvp); | |
596 | } else if ((ap->a_flags & UPL_NOCOMMIT) == 0) { | |
597 | ubc_upl_abort_range(ap->a_pl, ap->a_pl_offset, (upl_size_t)ap->a_size, UPL_ABORT_ERROR | UPL_ABORT_FREE_ON_EMPTY); | |
598 | } | |
599 | return error; | |
600 | } | |
601 | ||
602 | static int | |
603 | bindfs_read(struct vnop_read_args * ap) | |
604 | { | |
605 | int error = EIO; | |
606 | ||
607 | struct vnode *vp, *lvp; | |
608 | ||
609 | BINDFSDEBUG("%s %p\n", __FUNCTION__, ap->a_vp); | |
610 | ||
611 | vp = ap->a_vp; | |
612 | lvp = BINDVPTOLOWERVP(vp); | |
613 | ||
614 | /* | |
615 | * First some house keeping | |
616 | */ | |
617 | if (vnode_getwithvid(lvp, BINDVPTOLOWERVID(vp)) == 0) { | |
618 | if (!vnode_isreg(lvp) && !vnode_islnk(lvp)) { | |
619 | error = EPERM; | |
620 | goto end; | |
621 | } | |
622 | ||
623 | if (uio_resid(ap->a_uio) == 0) { | |
624 | error = 0; | |
625 | goto end; | |
626 | } | |
627 | ||
628 | /* | |
629 | * Now ask VM/UBC/VFS to do our bidding | |
630 | */ | |
631 | ||
632 | error = VNOP_READ(lvp, ap->a_uio, ap->a_ioflag, ap->a_context); | |
633 | if (error) { | |
634 | printf("bindfs: VNOP_READ failed: %d\n", error); | |
635 | } | |
636 | end: | |
637 | vnode_put(lvp); | |
638 | } | |
639 | return error; | |
640 | } | |
641 | ||
642 | /* | |
643 | * Global vfs data structures | |
644 | */ | |
645 | ||
646 | static const struct vnodeopv_entry_desc bindfs_vnodeop_entries[] = { | |
647 | {.opve_op = &vnop_default_desc, .opve_impl = (vop_t)bindfs_default}, /* default */ | |
648 | {.opve_op = &vnop_getattr_desc, .opve_impl = (vop_t)bindfs_getattr}, /* getattr */ | |
649 | {.opve_op = &vnop_open_desc, .opve_impl = (vop_t)bindfs_open}, /* open */ | |
650 | {.opve_op = &vnop_close_desc, .opve_impl = (vop_t)bindfs_close}, /* close */ | |
651 | {.opve_op = &vnop_inactive_desc, .opve_impl = (vop_t)bind_inactive}, /* inactive */ | |
652 | {.opve_op = &vnop_reclaim_desc, .opve_impl = (vop_t)bind_reclaim}, /* reclaim */ | |
653 | {.opve_op = &vnop_lookup_desc, .opve_impl = (vop_t)bind_lookup}, /* lookup */ | |
654 | {.opve_op = &vnop_readdir_desc, .opve_impl = (vop_t)bindfs_readdir}, /* readdir */ | |
655 | {.opve_op = &vnop_readlink_desc, .opve_impl = (vop_t)bindfs_readlink}, /* readlink */ | |
656 | {.opve_op = &vnop_pathconf_desc, .opve_impl = (vop_t)bindfs_pathconf}, /* pathconf */ | |
657 | {.opve_op = &vnop_fsync_desc, .opve_impl = (vop_t)bindfs_fsync}, /* fsync */ | |
658 | {.opve_op = &vnop_mmap_desc, .opve_impl = (vop_t)bindfs_mmap}, /* mmap */ | |
659 | {.opve_op = &vnop_mnomap_desc, .opve_impl = (vop_t)bindfs_mnomap}, /* mnomap */ | |
660 | {.opve_op = &vnop_getxattr_desc, .opve_impl = (vop_t)bindfs_getxattr}, /* getxattr */ | |
661 | {.opve_op = &vnop_pagein_desc, .opve_impl = (vop_t)bindfs_pagein}, /* pagein */ | |
662 | {.opve_op = &vnop_read_desc, .opve_impl = (vop_t)bindfs_read}, /* read */ | |
663 | {.opve_op = &vnop_listxattr_desc, .opve_impl = (vop_t)bindfs_listxattr}, /* listxattr */ | |
664 | {.opve_op = NULL, .opve_impl = NULL}, | |
665 | }; | |
666 | ||
667 | const struct vnodeopv_desc bindfs_vnodeop_opv_desc = {.opv_desc_vector_p = &bindfs_vnodeop_p, .opv_desc_ops = bindfs_vnodeop_entries}; | |
668 | ||
669 | //BINDFS Specific helper function | |
670 | ||
671 | int | |
672 | bindfs_getbackingvnode(vnode_t in_vp, vnode_t* out_vpp) | |
673 | { | |
674 | int result = EINVAL; | |
675 | ||
676 | if (out_vpp == NULL || in_vp == NULL) { | |
677 | goto end; | |
678 | } | |
679 | ||
680 | struct vfsstatfs * sp = NULL; | |
681 | mount_t mp = vnode_mount(in_vp); | |
682 | ||
683 | sp = vfs_statfs(mp); | |
684 | //If this isn't a bindfs vnode or it is but it's a special vnode | |
685 | if (strcmp(sp->f_fstypename, "bindfs") != 0) { | |
686 | *out_vpp = NULLVP; | |
687 | result = ENOENT; | |
688 | goto end; | |
689 | } | |
690 | ||
691 | vnode_t lvp = BINDVPTOLOWERVP(in_vp); | |
692 | if ((result = vnode_getwithvid(lvp, BINDVPTOLOWERVID(in_vp)))) { | |
693 | goto end; | |
694 | } | |
695 | ||
696 | *out_vpp = lvp; | |
697 | ||
698 | end: | |
699 | return result; | |
700 | } |