]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/vfs_vnops.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_vnops.c
CommitLineData
1c79356b 1/*
fe8ab488 2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
67 *
1c79356b 68 */
2d21ac55
A
69/*
70 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
71 * support for mandatory and extensible security protections. This notice
72 * is included in support of clause 2.2 (b) of the Apple Public License,
73 * Version 2.0.
74 */
1c79356b
A
75
76#include <sys/param.h>
91447636 77#include <sys/types.h>
1c79356b
A
78#include <sys/systm.h>
79#include <sys/kernel.h>
91447636 80#include <sys/file_internal.h>
1c79356b 81#include <sys/stat.h>
91447636
A
82#include <sys/proc_internal.h>
83#include <sys/kauth.h>
84#include <sys/mount_internal.h>
1c79356b 85#include <sys/namei.h>
91447636 86#include <sys/vnode_internal.h>
1c79356b
A
87#include <sys/ioctl.h>
88#include <sys/tty.h>
2d21ac55
A
89/* Temporary workaround for ubc.h until <rdar://4714366 is resolved */
90#define ubc_setcred ubc_setcred_deprecated
1c79356b 91#include <sys/ubc.h>
2d21ac55
A
92#undef ubc_setcred
93int ubc_setcred(struct vnode *, struct proc *);
9bccf70c
A
94#include <sys/conf.h>
95#include <sys/disk.h>
91447636
A
96#include <sys/fsevents.h>
97#include <sys/kdebug.h>
98#include <sys/xattr.h>
99#include <sys/ubc_internal.h>
100#include <sys/uio_internal.h>
0c530ab8 101#include <sys/resourcevar.h>
2d21ac55 102#include <sys/signalvar.h>
9bccf70c
A
103
104#include <vm/vm_kern.h>
91447636 105#include <vm/vm_map.h>
9bccf70c
A
106
107#include <miscfs/specfs/specdev.h>
b0d623f7 108#include <miscfs/fifofs/fifo.h>
9bccf70c 109
2d21ac55
A
110#if CONFIG_MACF
111#include <security/mac_framework.h>
112#endif
91447636 113
316670eb
A
114#if CONFIG_PROTECT
115#include <sys/cprotect.h>
116#endif
117
3e170ce0 118#include <IOKit/IOBSD.h>
91447636 119
2d21ac55
A
120static int vn_closefile(struct fileglob *fp, vfs_context_t ctx);
121static int vn_ioctl(struct fileproc *fp, u_long com, caddr_t data,
122 vfs_context_t ctx);
123static int vn_read(struct fileproc *fp, struct uio *uio, int flags,
124 vfs_context_t ctx);
125static int vn_write(struct fileproc *fp, struct uio *uio, int flags,
126 vfs_context_t ctx);
127static int vn_select( struct fileproc *fp, int which, void * wql,
128 vfs_context_t ctx);
129static int vn_kqfilt_add(struct fileproc *fp, struct knote *kn,
130 vfs_context_t ctx);
b0d623f7
A
131static void filt_vndetach(struct knote *kn);
132static int filt_vnode(struct knote *kn, long hint);
6d2010ae 133static int vn_open_auth_finish(vnode_t vp, int fmode, vfs_context_t ctx);
91447636 134#if 0
2d21ac55
A
135static int vn_kqfilt_remove(struct vnode *vp, uintptr_t ident,
136 vfs_context_t ctx);
91447636 137#endif
1c79356b 138
39236c6e
A
139const struct fileops vnops = {
140 DTYPE_VNODE,
141 vn_read,
142 vn_write,
143 vn_ioctl,
144 vn_select,
145 vn_closefile,
146 vn_kqfilt_add,
147 NULL
148};
1c79356b 149
b0d623f7
A
150struct filterops vnode_filtops = {
151 .f_isfd = 1,
152 .f_attach = NULL,
153 .f_detach = filt_vndetach,
154 .f_event = filt_vnode
155};
156
1c79356b
A
157/*
158 * Common code for vnode open operations.
91447636
A
159 * Check permissions, and call the VNOP_OPEN or VNOP_CREATE routine.
160 *
161 * XXX the profusion of interfaces here is probably a bad thing.
1c79356b 162 */
9bccf70c 163int
91447636 164vn_open(struct nameidata *ndp, int fmode, int cmode)
55e303ae 165{
91447636 166 return(vn_open_modflags(ndp, &fmode, cmode));
55e303ae
A
167}
168
91447636
A
169int
170vn_open_modflags(struct nameidata *ndp, int *fmodep, int cmode)
1c79356b 171{
91447636
A
172 struct vnode_attr va;
173
174 VATTR_INIT(&va);
175 VATTR_SET(&va, va_mode, cmode);
176
177 return(vn_open_auth(ndp, fmodep, &va));
178}
179
6d2010ae
A
180static int
181vn_open_auth_finish(vnode_t vp, int fmode, vfs_context_t ctx)
182{
183 int error;
184
185 if ((error = vnode_ref_ext(vp, fmode, 0)) != 0) {
186 goto bad;
187 }
188
4b17d6b6 189 /* Call out to allow 3rd party notification of open.
6d2010ae
A
190 * Ignore result of kauth_authorize_fileop call.
191 */
4b17d6b6
A
192#if CONFIG_MACF
193 mac_vnode_notify_open(ctx, vp, fmode);
194#endif
6d2010ae
A
195 kauth_authorize_fileop(vfs_context_ucred(ctx), KAUTH_FILEOP_OPEN,
196 (uintptr_t)vp, 0);
197
198 return 0;
199
200bad:
201 return error;
202
203}
204
205/*
206 * May do nameidone() to allow safely adding an FSEvent. Cue off of ni_dvp to
207 * determine whether that has happened.
208 */
209static int
210vn_open_auth_do_create(struct nameidata *ndp, struct vnode_attr *vap, int fmode, boolean_t *did_create, boolean_t *did_open, vfs_context_t ctx)
211{
212 uint32_t status = 0;
213 vnode_t dvp = ndp->ni_dvp;
214 int batched;
215 int error;
216 vnode_t vp;
217
218 batched = vnode_compound_open_available(ndp->ni_dvp);
219 *did_open = FALSE;
220
221 VATTR_SET(vap, va_type, VREG);
222 if (fmode & O_EXCL)
223 vap->va_vaflags |= VA_EXCLUSIVE;
224
225#if NAMEDRSRCFORK
226 if (ndp->ni_cnd.cn_flags & CN_WANTSRSRCFORK) {
227 if ((error = vn_authorize_create(dvp, &ndp->ni_cnd, vap, ctx, NULL)) != 0)
228 goto out;
229 if ((error = vnode_makenamedstream(dvp, &ndp->ni_vp, XATTR_RESOURCEFORK_NAME, 0, ctx)) != 0)
230 goto out;
231 *did_create = TRUE;
232 } else {
233#endif
234 if (!batched) {
235 if ((error = vn_authorize_create(dvp, &ndp->ni_cnd, vap, ctx, NULL)) != 0)
236 goto out;
237 }
238
239 error = vn_create(dvp, &ndp->ni_vp, ndp, vap, VN_CREATE_DOOPEN, fmode, &status, ctx);
240 if (error != 0) {
241 if (batched) {
242 *did_create = (status & COMPOUND_OPEN_STATUS_DID_CREATE) ? TRUE : FALSE;
243 } else {
244 *did_create = FALSE;
245 }
246
247 if (error == EKEEPLOOKING) {
248 if (*did_create) {
249 panic("EKEEPLOOKING, but we did a create?");
250 }
251 if (!batched) {
252 panic("EKEEPLOOKING from filesystem that doesn't support compound vnops?");
253 }
254 if ((ndp->ni_flag & NAMEI_CONTLOOKUP) == 0) {
255 panic("EKEEPLOOKING, but continue flag not set?");
256 }
257
258 /*
259 * Do NOT drop the dvp: we need everything to continue the lookup.
260 */
261 return error;
262 }
263 } else {
264 if (batched) {
265 *did_create = (status & COMPOUND_OPEN_STATUS_DID_CREATE) ? 1 : 0;
266 *did_open = TRUE;
267 } else {
268 *did_create = TRUE;
269 }
270 }
271#if NAMEDRSRCFORK
272 }
273#endif
274
6d2010ae 275 vp = ndp->ni_vp;
6d2010ae
A
276
277 if (*did_create) {
278 int update_flags = 0;
279
280 // Make sure the name & parent pointers are hooked up
281 if (vp->v_name == NULL)
282 update_flags |= VNODE_UPDATE_NAME;
283 if (vp->v_parent == NULLVP)
284 update_flags |= VNODE_UPDATE_PARENT;
285
286 if (update_flags)
287 vnode_update_identity(vp, dvp, ndp->ni_cnd.cn_nameptr, ndp->ni_cnd.cn_namelen, ndp->ni_cnd.cn_hash, update_flags);
288
289 vnode_put(dvp);
290 ndp->ni_dvp = NULLVP;
291
292#if CONFIG_FSE
293 if (need_fsevent(FSE_CREATE_FILE, vp)) {
294 add_fsevent(FSE_CREATE_FILE, ctx,
295 FSE_ARG_VNODE, vp,
296 FSE_ARG_DONE);
297 }
298#endif
299 }
300out:
301 if (ndp->ni_dvp != NULLVP) {
302 vnode_put(dvp);
303 ndp->ni_dvp = NULLVP;
304 }
305
306 return error;
307}
308
3e170ce0
A
309/*
310 * This is the number of times we'll loop in vn_open_auth without explicitly
311 * yielding the CPU when we determine we have to retry.
312 */
313#define RETRY_NO_YIELD_COUNT 5
314
0c530ab8
A
315/*
316 * Open a file with authorization, updating the contents of the structures
317 * pointed to by ndp, fmodep, and vap as necessary to perform the requested
318 * operation. This function is used for both opens of existing files, and
319 * creation of new files.
320 *
321 * Parameters: ndp The nami data pointer describing the
322 * file
323 * fmodep A pointer to an int containg the mode
324 * information to be used for the open
325 * vap A pointer to the vnode attribute
326 * descriptor to be used for the open
327 *
328 * Indirect: * Contents of the data structures pointed
329 * to by the parameters are modified as
330 * necessary to the requested operation.
331 *
332 * Returns: 0 Success
333 * !0 errno value
334 *
335 * Notes: The kauth_filesec_t in 'vap', if any, is in host byte order.
336 *
337 * The contents of '*ndp' will be modified, based on the other
338 * arguments to this function, and to return file and directory
339 * data necessary to satisfy the requested operation.
340 *
341 * If the file does not exist and we are creating it, then the
342 * O_TRUNC flag will be cleared in '*fmodep' to indicate to the
343 * caller that the file was not truncated.
344 *
345 * If the file exists and the O_EXCL flag was not specified, then
346 * the O_CREAT flag will be cleared in '*fmodep' to indicate to
347 * the caller that the existing file was merely opened rather
348 * than created.
349 *
350 * The contents of '*vap' will be modified as necessary to
351 * complete the operation, including setting of supported
352 * attribute, clearing of fields containing unsupported attributes
353 * in the request, if the request proceeds without them, etc..
354 *
355 * XXX: This function is too complicated in actings on its arguments
356 *
357 * XXX: We should enummerate the possible errno values here, and where
358 * in the code they originated.
359 */
91447636
A
360int
361vn_open_auth(struct nameidata *ndp, int *fmodep, struct vnode_attr *vap)
362{
363 struct vnode *vp;
364 struct vnode *dvp;
365 vfs_context_t ctx = ndp->ni_cnd.cn_context;
1c79356b 366 int error;
91447636 367 int fmode;
b0d623f7 368 uint32_t origcnflags;
6d2010ae
A
369 boolean_t did_create;
370 boolean_t did_open;
371 boolean_t need_vnop_open;
372 boolean_t batched;
373 boolean_t ref_failed;
3e170ce0 374 int nretries = 0;
1c79356b 375
91447636
A
376again:
377 vp = NULL;
378 dvp = NULL;
6d2010ae
A
379 batched = FALSE;
380 did_create = FALSE;
381 need_vnop_open = TRUE;
382 ref_failed = FALSE;
91447636 383 fmode = *fmodep;
b0d623f7 384 origcnflags = ndp->ni_cnd.cn_flags;
6d2010ae
A
385
386 /*
387 * O_CREAT
388 */
1c79356b 389 if (fmode & O_CREAT) {
0c530ab8
A
390 if ( (fmode & O_DIRECTORY) ) {
391 error = EINVAL;
392 goto out;
393 }
1c79356b 394 ndp->ni_cnd.cn_nameiop = CREATE;
6d2010ae
A
395#if CONFIG_TRIGGERS
396 ndp->ni_op = OP_LINK;
397#endif
b0d623f7 398 /* Inherit USEDVP, vnode_open() supported flags only */
fe8ab488 399 ndp->ni_cnd.cn_flags &= (USEDVP | NOCROSSMOUNT);
2d21ac55 400 ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF | AUDITVNPATH1;
6d2010ae 401 ndp->ni_flag = NAMEI_COMPOUNDOPEN;
2d21ac55
A
402#if NAMEDRSRCFORK
403 /* open calls are allowed for resource forks. */
404 ndp->ni_cnd.cn_flags |= CN_ALLOWRSRCFORK;
405#endif
b0d623f7 406 if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0 && (origcnflags & FOLLOW) != 0)
1c79356b 407 ndp->ni_cnd.cn_flags |= FOLLOW;
6d2010ae
A
408
409continue_create_lookup:
91447636
A
410 if ( (error = namei(ndp)) )
411 goto out;
6d2010ae 412
91447636
A
413 dvp = ndp->ni_dvp;
414 vp = ndp->ni_vp;
415
6d2010ae 416 batched = vnode_compound_open_available(dvp);
2d21ac55 417
6d2010ae
A
418 /* not found, create */
419 if (vp == NULL) {
420 /* must have attributes for a new file */
421 if (vap == NULL) {
fe8ab488 422 vnode_put(dvp);
6d2010ae
A
423 error = EINVAL;
424 goto out;
425 }
426 /*
427 * Attempt a create. For a system supporting compound VNOPs, we may
428 * find an existing file or create one; in either case, we will already
429 * have the file open and no VNOP_OPEN() will be needed.
430 */
431 error = vn_open_auth_do_create(ndp, vap, fmode, &did_create, &did_open, ctx);
91447636 432
6d2010ae 433 dvp = ndp->ni_dvp;
91447636
A
434 vp = ndp->ni_vp;
435
6d2010ae
A
436 /*
437 * Detected a node that the filesystem couldn't handle. Don't call
438 * nameidone() yet, because we need that path buffer.
439 */
440 if (error == EKEEPLOOKING) {
441 if (!batched) {
442 panic("EKEEPLOOKING from a filesystem that doesn't support compound VNOPs?");
91447636 443 }
6d2010ae 444 goto continue_create_lookup;
91447636 445 }
b0d623f7 446
6d2010ae 447 nameidone(ndp);
b0d623f7 448 if (dvp) {
6d2010ae 449 panic("Shouldn't have a dvp here.");
b0d623f7 450 }
91447636
A
451
452 if (error) {
453 /*
3e170ce0 454 * Check for a create race.
91447636 455 */
3e170ce0 456 if ((error == EEXIST) && !(fmode & O_EXCL)){
6d2010ae
A
457 if (vp)
458 vnode_put(vp);
91447636
A
459 goto again;
460 }
461 goto bad;
55e303ae 462 }
6d2010ae
A
463
464 need_vnop_open = !did_open;
316670eb
A
465 }
466 else {
6d2010ae
A
467 if (fmode & O_EXCL)
468 error = EEXIST;
469
470 /*
471 * We have a vnode. Use compound open if available
472 * or else fall through to "traditional" path. Note: can't
473 * do a compound open for root, because the parent belongs
474 * to a different FS.
475 */
476 if (error == 0 && batched && (vnode_mount(dvp) == vnode_mount(vp))) {
477 error = VNOP_COMPOUND_OPEN(dvp, &ndp->ni_vp, ndp, 0, fmode, NULL, NULL, ctx);
478
479 if (error == 0) {
480 vp = ndp->ni_vp;
481 need_vnop_open = FALSE;
482 } else if (error == EKEEPLOOKING) {
483 if ((ndp->ni_flag & NAMEI_CONTLOOKUP) == 0) {
484 panic("EKEEPLOOKING, but continue flag not set?");
485 }
486 goto continue_create_lookup;
487 }
488 }
91447636 489 nameidone(ndp);
91447636 490 vnode_put(dvp);
6d2010ae 491 ndp->ni_dvp = NULLVP;
91447636 492
6d2010ae 493 if (error) {
1c79356b
A
494 goto bad;
495 }
6d2010ae 496
1c79356b 497 fmode &= ~O_CREAT;
6d2010ae
A
498
499 /* Fall through */
1c79356b 500 }
39236c6e
A
501 }
502 else {
6d2010ae
A
503 /*
504 * Not O_CREAT
505 */
1c79356b 506 ndp->ni_cnd.cn_nameiop = LOOKUP;
b0d623f7 507 /* Inherit USEDVP, vnode_open() supported flags only */
fe8ab488 508 ndp->ni_cnd.cn_flags &= (USEDVP | NOCROSSMOUNT);
6d2010ae 509 ndp->ni_cnd.cn_flags |= FOLLOW | LOCKLEAF | AUDITVNPATH1 | WANTPARENT;
2d21ac55
A
510#if NAMEDRSRCFORK
511 /* open calls are allowed for resource forks. */
512 ndp->ni_cnd.cn_flags |= CN_ALLOWRSRCFORK;
513#endif
6d2010ae
A
514 ndp->ni_flag = NAMEI_COMPOUNDOPEN;
515
b0d623f7
A
516 /* preserve NOFOLLOW from vnode_open() */
517 if (fmode & O_NOFOLLOW || fmode & O_SYMLINK || (origcnflags & FOLLOW) == 0) {
6d2010ae 518 ndp->ni_cnd.cn_flags &= ~FOLLOW;
2d21ac55
A
519 }
520
6d2010ae
A
521 /* Do a lookup, possibly going directly to filesystem for compound operation */
522 do {
523 if ( (error = namei(ndp)) )
524 goto out;
525 vp = ndp->ni_vp;
526 dvp = ndp->ni_dvp;
527
528 /* Check for batched lookup-open */
529 batched = vnode_compound_open_available(dvp);
530 if (batched && ((vp == NULLVP) || (vnode_mount(dvp) == vnode_mount(vp)))) {
531 error = VNOP_COMPOUND_OPEN(dvp, &ndp->ni_vp, ndp, 0, fmode, NULL, NULL, ctx);
532 vp = ndp->ni_vp;
533 if (error == 0) {
534 need_vnop_open = FALSE;
535 } else if (error == EKEEPLOOKING) {
536 if ((ndp->ni_flag & NAMEI_CONTLOOKUP) == 0) {
537 panic("EKEEPLOOKING, but continue flag not set?");
538 }
539 }
540 }
541 } while (error == EKEEPLOOKING);
542
91447636 543 nameidone(ndp);
6d2010ae
A
544 vnode_put(dvp);
545 ndp->ni_dvp = NULLVP;
0c530ab8 546
6d2010ae 547 if (error) {
0c530ab8
A
548 goto bad;
549 }
1c79356b 550 }
2d21ac55 551
6d2010ae
A
552 /*
553 * By this point, nameidone() is called, dvp iocount is dropped,
554 * and dvp pointer is cleared.
555 */
556 if (ndp->ni_dvp != NULLVP) {
557 panic("Haven't cleaned up adequately in vn_open_auth()");
2d21ac55 558 }
9bccf70c 559
6d2010ae
A
560 /*
561 * Expect to use this code for filesystems without compound VNOPs, for the root
562 * of a filesystem, which can't be "looked up" in the sense of VNOP_LOOKUP(),
563 * and for shadow files, which do not live on the same filesystems as their "parents."
564 */
565 if (need_vnop_open) {
566 if (batched && !vnode_isvroot(vp) && !vnode_isnamedstream(vp)) {
567 panic("Why am I trying to use VNOP_OPEN() on anything other than the root or a named stream?");
1c79356b 568 }
91447636 569
6d2010ae
A
570 if (!did_create) {
571 error = vn_authorize_open_existing(vp, &ndp->ni_cnd, fmode, ctx, NULL);
572 if (error) {
573 goto bad;
2d21ac55
A
574 }
575 }
2d21ac55 576
316670eb 577#if CONFIG_PROTECT
3e170ce0
A
578 // If raw encrypted mode is requested, handle that here
579 if (VATTR_IS_ACTIVE (vap, va_dataprotect_flags)
580 && ISSET(vap->va_dataprotect_flags, VA_DP_RAWENCRYPTED)) {
581 fmode |= FENCRYPTED;
582 }
583 if (VATTR_IS_ACTIVE (vap, va_dataprotect_flags)
584 && ISSET(vap->va_dataprotect_flags, VA_DP_RAWUNENCRYPTED)) {
585 /* Don't allow unencrypted io request from user space unless entitled */
586 boolean_t entitled = FALSE;
587#if !SECURE_KERNEL
588 entitled = IOTaskHasEntitlement(current_task(), "com.apple.private.security.file-unencrypt-access");
589#endif
590 if (!entitled) {
591 error = EPERM;
316670eb
A
592 goto bad;
593 }
3e170ce0
A
594 fmode |= FUNENCRYPTED;
595 }
596
597 /*
598 * Perform any content protection access checks prior to calling
599 * into the filesystem.
600 */
601 error = cp_handle_open (vp, fmode);
602 if (error) {
603 goto bad;
316670eb
A
604 }
605#endif
606
6d2010ae
A
607 error = VNOP_OPEN(vp, fmode, ctx);
608 if (error) {
609 goto bad;
2d21ac55 610 }
6d2010ae
A
611 need_vnop_open = FALSE;
612 }
2d21ac55 613
6d2010ae
A
614 // if the vnode is tagged VOPENEVT and the current process
615 // has the P_CHECKOPENEVT flag set, then we or in the O_EVTONLY
616 // flag to the open mode so that this open won't count against
617 // the vnode when carbon delete() does a vnode_isinuse() to see
618 // if a file is currently in use. this allows spotlight
619 // importers to not interfere with carbon apps that depend on
620 // the no-delete-if-busy semantics of carbon delete().
621 //
622 if (!did_create && (vp->v_flag & VOPENEVT) && (current_proc()->p_flag & P_CHECKOPENEVT)) {
623 fmode |= O_EVTONLY;
1c79356b 624 }
0b4e3aa0 625
6d2010ae
A
626 /*
627 * Grab reference, etc.
628 */
629 error = vn_open_auth_finish(vp, fmode, ctx);
630 if (error) {
631 ref_failed = TRUE;
0b4e3aa0
A
632 goto bad;
633 }
91447636 634
6d2010ae
A
635 /* Compound VNOP open is responsible for doing the truncate */
636 if (batched || did_create)
637 fmode &= ~O_TRUNC;
0b4e3aa0 638
55e303ae 639 *fmodep = fmode;
1c79356b 640 return (0);
6d2010ae 641
1c79356b 642bad:
6d2010ae
A
643 /* Opened either explicitly or by a batched create */
644 if (!need_vnop_open) {
645 VNOP_CLOSE(vp, fmode, ctx);
646 }
647
55e303ae 648 ndp->ni_vp = NULL;
91447636 649 if (vp) {
c910b4d9 650#if NAMEDRSRCFORK
b0d623f7
A
651 /* Aggressively recycle shadow files if we error'd out during open() */
652 if ((vnode_isnamedstream(vp)) &&
653 (vp->v_parent != NULLVP) &&
654 (vnode_isshadow(vp))) {
655 vnode_recycle(vp);
c910b4d9
A
656 }
657#endif
658 vnode_put(vp);
91447636
A
659 /*
660 * Check for a race against unlink. We had a vnode
661 * but according to vnode_authorize or VNOP_OPEN it
662 * no longer exists.
935ed37a
A
663 *
664 * EREDRIVEOPEN: means that we were hit by the tty allocation race.
91447636 665 */
6d2010ae 666 if (((error == ENOENT) && (*fmodep & O_CREAT)) || (error == EREDRIVEOPEN) || ref_failed) {
3e170ce0
A
667 /*
668 * We'll retry here but it may be possible that we get
669 * into a retry "spin" inside the kernel and not allow
670 * threads, which need to run in order for the retry
671 * loop to end, to run. An example is an open of a
672 * terminal which is getting revoked and we spin here
673 * without yielding becasue namei and VNOP_OPEN are
674 * successful but vnode_ref fails. The revoke needs
675 * threads with an iocount to run but if spin here we
676 * may possibly be blcoking other threads from running.
677 *
678 * We start yielding the CPU after some number of
679 * retries for increasing durations. Note that this is
680 * still a loop without an exit condition.
681 */
682 nretries += 1;
683 if (nretries > RETRY_NO_YIELD_COUNT) {
684 /* Every hz/100 secs is 10 msecs ... */
685 tsleep(&nretries, PVFS, "vn_open_auth_retry",
686 MIN((nretries * (hz/100)), hz));
687 }
91447636
A
688 goto again;
689 }
690 }
6d2010ae 691
91447636 692out:
1c79356b
A
693 return (error);
694}
695
2d21ac55 696#if vn_access_DEPRECATED
1c79356b 697/*
91447636
A
698 * Authorize an action against a vnode. This has been the canonical way to
699 * ensure that the credential/process/etc. referenced by a vfs_context
700 * is granted the rights called out in 'mode' against the vnode 'vp'.
701 *
702 * Unfortunately, the use of VREAD/VWRITE/VEXEC makes it very difficult
703 * to add support for more rights. As such, this interface will be deprecated
704 * and callers will use vnode_authorize instead.
1c79356b 705 */
9bccf70c 706int
91447636 707vn_access(vnode_t vp, int mode, vfs_context_t context)
1c79356b 708{
91447636
A
709 kauth_action_t action;
710
711 action = 0;
712 if (mode & VREAD)
713 action |= KAUTH_VNODE_READ_DATA;
714 if (mode & VWRITE)
715 action |= KAUTH_VNODE_WRITE_DATA;
716 if (mode & VEXEC)
717 action |= KAUTH_VNODE_EXECUTE;
718
719 return(vnode_authorize(vp, NULL, action, context));
1c79356b 720}
2d21ac55 721#endif /* vn_access_DEPRECATED */
1c79356b
A
722
723/*
724 * Vnode close call
725 */
9bccf70c 726int
2d21ac55 727vn_close(struct vnode *vp, int flags, vfs_context_t ctx)
1c79356b
A
728{
729 int error;
39236c6e 730 int flusherror = 0;
1c79356b 731
2d21ac55 732#if NAMEDRSRCFORK
cf7d32b8 733 /* Sync data from resource fork shadow file if needed. */
2d21ac55
A
734 if ((vp->v_flag & VISNAMEDSTREAM) &&
735 (vp->v_parent != NULLVP) &&
b0d623f7 736 vnode_isshadow(vp)) {
2d21ac55 737 if (flags & FWASWRITTEN) {
39236c6e 738 flusherror = vnode_flushnamedstream(vp->v_parent, vp, ctx);
2d21ac55 739 }
2d21ac55
A
740 }
741#endif
593a1d5f
A
742
743 /* work around for foxhound */
b0d623f7 744 if (vnode_isspec(vp))
593a1d5f
A
745 (void)vnode_rele_ext(vp, flags, 0);
746
fe8ab488
A
747 /*
748 * On HFS, we flush when the last writer closes. We do this
749 * because resource fork vnodes hold a reference on data fork
750 * vnodes and that will prevent them from getting VNOP_INACTIVE
751 * which will delay when we flush cached data. In future, we
752 * might find it beneficial to do this for all file systems.
753 * Note that it's OK to access v_writecount without the lock
754 * in this context.
755 */
756 if (vp->v_tag == VT_HFS && (flags & FWRITE) && vp->v_writecount == 1)
757 VNOP_FSYNC(vp, MNT_NOWAIT, ctx);
758
2d21ac55 759 error = VNOP_CLOSE(vp, flags, ctx);
91447636 760
6d2010ae
A
761#if CONFIG_FSE
762 if (flags & FWASWRITTEN) {
763 if (need_fsevent(FSE_CONTENT_MODIFIED, vp)) {
764 add_fsevent(FSE_CONTENT_MODIFIED, ctx,
765 FSE_ARG_VNODE, vp,
766 FSE_ARG_DONE);
767 }
768 }
769#endif
770
b0d623f7 771 if (!vnode_isspec(vp))
593a1d5f
A
772 (void)vnode_rele_ext(vp, flags, 0);
773
39236c6e
A
774 if (flusherror) {
775 error = flusherror;
776 }
1c79356b
A
777 return (error);
778}
779
91447636
A
780static int
781vn_read_swapfile(
782 struct vnode *vp,
783 uio_t uio)
784{
91447636
A
785 int error;
786 off_t swap_count, this_count;
787 off_t file_end, read_end;
788 off_t prev_resid;
b0d623f7 789 char *my_swap_page;
91447636
A
790
791 /*
b0d623f7 792 * Reading from a swap file will get you zeroes.
91447636 793 */
b0d623f7
A
794
795 my_swap_page = NULL;
91447636
A
796 error = 0;
797 swap_count = uio_resid(uio);
798
799 file_end = ubc_getsize(vp);
800 read_end = uio->uio_offset + uio_resid(uio);
801 if (uio->uio_offset >= file_end) {
802 /* uio starts after end of file: nothing to read */
803 swap_count = 0;
804 } else if (read_end > file_end) {
805 /* uio extends beyond end of file: stop before that */
806 swap_count -= (read_end - file_end);
807 }
808
809 while (swap_count > 0) {
b0d623f7
A
810 if (my_swap_page == NULL) {
811 MALLOC(my_swap_page, char *, PAGE_SIZE,
812 M_TEMP, M_WAITOK);
813 memset(my_swap_page, '\0', PAGE_SIZE);
814 /* add an end-of-line to keep line counters happy */
815 my_swap_page[PAGE_SIZE-1] = '\n';
91447636 816 }
91447636
A
817 this_count = swap_count;
818 if (this_count > PAGE_SIZE) {
819 this_count = PAGE_SIZE;
820 }
821
822 prev_resid = uio_resid(uio);
b0d623f7 823 error = uiomove((caddr_t) my_swap_page,
91447636
A
824 this_count,
825 uio);
826 if (error) {
827 break;
828 }
829 swap_count -= (prev_resid - uio_resid(uio));
830 }
b0d623f7
A
831 if (my_swap_page != NULL) {
832 FREE(my_swap_page, M_TEMP);
833 my_swap_page = NULL;
834 }
91447636
A
835
836 return error;
837}
1c79356b
A
838/*
839 * Package up an I/O request on a vnode into a uio and do it.
840 */
9bccf70c 841int
91447636
A
842vn_rdwr(
843 enum uio_rw rw,
844 struct vnode *vp,
845 caddr_t base,
846 int len,
847 off_t offset,
848 enum uio_seg segflg,
849 int ioflg,
850 kauth_cred_t cred,
851 int *aresid,
2d21ac55 852 proc_t p)
1c79356b 853{
b0d623f7
A
854 int64_t resid;
855 int result;
856
857 result = vn_rdwr_64(rw,
91447636
A
858 vp,
859 (uint64_t)(uintptr_t)base,
860 (int64_t)len,
861 offset,
862 segflg,
863 ioflg,
864 cred,
b0d623f7 865 &resid,
91447636 866 p);
b0d623f7
A
867
868 /* "resid" should be bounded above by "len," which is an int */
869 if (aresid != NULL) {
870 *aresid = resid;
871 }
872
873 return result;
91447636
A
874}
875
876
877int
878vn_rdwr_64(
879 enum uio_rw rw,
880 struct vnode *vp,
881 uint64_t base,
882 int64_t len,
883 off_t offset,
884 enum uio_seg segflg,
885 int ioflg,
886 kauth_cred_t cred,
b0d623f7 887 int64_t *aresid,
2d21ac55 888 proc_t p)
91447636
A
889{
890 uio_t auio;
891 int spacetype;
892 struct vfs_context context;
1c79356b 893 int error=0;
91447636
A
894 char uio_buf[ UIO_SIZEOF(1) ];
895
2d21ac55 896 context.vc_thread = current_thread();
91447636 897 context.vc_ucred = cred;
1c79356b 898
91447636
A
899 if (UIO_SEG_IS_USER_SPACE(segflg)) {
900 spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32;
901 }
902 else {
903 spacetype = UIO_SYSSPACE;
904 }
905 auio = uio_createwithbuffer(1, offset, spacetype, rw,
906 &uio_buf[0], sizeof(uio_buf));
907 uio_addiov(auio, base, len);
908
2d21ac55
A
909#if CONFIG_MACF
910 /* XXXMAC
911 * IO_NOAUTH should be re-examined.
912 * Likely that mediation should be performed in caller.
913 */
914 if ((ioflg & IO_NOAUTH) == 0) {
915 /* passed cred is fp->f_cred */
916 if (rw == UIO_READ)
917 error = mac_vnode_check_read(&context, cred, vp);
918 else
919 error = mac_vnode_check_write(&context, cred, vp);
920 }
921#endif
922
923 if (error == 0) {
924 if (rw == UIO_READ) {
39236c6e 925 if (vnode_isswap(vp) && ((ioflg & IO_SWAP_DISPATCH) == 0)) {
2d21ac55
A
926 error = vn_read_swapfile(vp, auio);
927 } else {
928 error = VNOP_READ(vp, auio, ioflg, &context);
929 }
91447636 930 } else {
2d21ac55 931 error = VNOP_WRITE(vp, auio, ioflg, &context);
91447636 932 }
91447636 933 }
1c79356b
A
934
935 if (aresid)
91447636 936 *aresid = uio_resid(auio);
1c79356b 937 else
91447636 938 if (uio_resid(auio) && error == 0)
1c79356b 939 error = EIO;
1c79356b
A
940 return (error);
941}
942
fe8ab488
A
943static inline void
944vn_offset_lock(struct fileglob *fg)
945{
946 lck_mtx_lock_spin(&fg->fg_lock);
947 while (fg->fg_lflags & FG_OFF_LOCKED) {
948 fg->fg_lflags |= FG_OFF_LOCKWANT;
949 msleep(&fg->fg_lflags, &fg->fg_lock, PVFS | PSPIN,
950 "fg_offset_lock_wait", 0);
951 }
952 fg->fg_lflags |= FG_OFF_LOCKED;
953 lck_mtx_unlock(&fg->fg_lock);
954}
955
956static inline void
957vn_offset_unlock(struct fileglob *fg)
958{
959 int lock_wanted = 0;
960
961 lck_mtx_lock_spin(&fg->fg_lock);
962 if (fg->fg_lflags & FG_OFF_LOCKWANT) {
963 lock_wanted = 1;
964 }
965 fg->fg_lflags &= ~(FG_OFF_LOCKED | FG_OFF_LOCKWANT);
966 lck_mtx_unlock(&fg->fg_lock);
967 if (lock_wanted) {
968 wakeup(&fg->fg_lflags);
969 }
970}
971
1c79356b
A
972/*
973 * File table vnode read routine.
974 */
9bccf70c 975static int
2d21ac55 976vn_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx)
1c79356b 977{
9bccf70c 978 struct vnode *vp;
39236c6e
A
979 int error;
980 int ioflag;
1c79356b 981 off_t count;
fe8ab488 982 int offset_locked = 0;
9bccf70c 983
91447636
A
984 vp = (struct vnode *)fp->f_fglob->fg_data;
985 if ( (error = vnode_getwithref(vp)) ) {
986 return(error);
987 }
2d21ac55
A
988
989#if CONFIG_MACF
990 error = mac_vnode_check_read(ctx, vfs_context_ucred(ctx), vp);
991 if (error) {
992 (void)vnode_put(vp);
993 return (error);
994 }
995#endif
996
316670eb
A
997 /* This signals to VNOP handlers that this read came from a file table read */
998 ioflag = IO_SYSCALL_DISPATCH;
999
91447636 1000 if (fp->f_fglob->fg_flag & FNONBLOCK)
9bccf70c 1001 ioflag |= IO_NDELAY;
2d21ac55 1002 if ((fp->f_fglob->fg_flag & FNOCACHE) || vnode_isnocache(vp))
316670eb
A
1003 ioflag |= IO_NOCACHE;
1004 if (fp->f_fglob->fg_flag & FENCRYPTED) {
1005 ioflag |= IO_ENCRYPTED;
1006 }
3e170ce0
A
1007 if (fp->f_fglob->fg_flag & FUNENCRYPTED) {
1008 ioflag |= IO_SKIP_ENCRYPTION;
1009 }
1010 if (fp->f_fglob->fg_flag & O_EVTONLY) {
1011 ioflag |= IO_EVTONLY;
1012 }
2d21ac55 1013 if (fp->f_fglob->fg_flag & FNORDAHEAD)
316670eb 1014 ioflag |= IO_RAOFF;
91447636 1015
fe8ab488
A
1016 if ((flags & FOF_OFFSET) == 0) {
1017 if ((vnode_vtype(vp) == VREG) && !vnode_isswap(vp)) {
1018 vn_offset_lock(fp->f_fglob);
1019 offset_locked = 1;
1020 }
91447636 1021 uio->uio_offset = fp->f_fglob->fg_offset;
fe8ab488 1022 }
91447636
A
1023 count = uio_resid(uio);
1024
3e170ce0 1025 if (vnode_isswap(vp) && !(IO_SKIP_ENCRYPTION & ioflag)) {
91447636
A
1026 /* special case for swap files */
1027 error = vn_read_swapfile(vp, uio);
1028 } else {
2d21ac55 1029 error = VNOP_READ(vp, uio, ioflag, ctx);
9bccf70c 1030 }
fe8ab488 1031 if ((flags & FOF_OFFSET) == 0) {
91447636 1032 fp->f_fglob->fg_offset += count - uio_resid(uio);
fe8ab488
A
1033 if (offset_locked) {
1034 vn_offset_unlock(fp->f_fglob);
1035 offset_locked = 0;
1036 }
1037 }
91447636
A
1038
1039 (void)vnode_put(vp);
1c79356b
A
1040 return (error);
1041}
1042
1043
1044/*
1045 * File table vnode write routine.
1046 */
9bccf70c 1047static int
2d21ac55 1048vn_write(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx)
1c79356b 1049{
9bccf70c
A
1050 struct vnode *vp;
1051 int error, ioflag;
1c79356b 1052 off_t count;
2d21ac55
A
1053 int clippedsize = 0;
1054 int partialwrite=0;
1055 int residcount, oldcount;
fe8ab488 1056 int offset_locked = 0;
2d21ac55 1057 proc_t p = vfs_context_proc(ctx);
91447636 1058
91447636
A
1059 count = 0;
1060 vp = (struct vnode *)fp->f_fglob->fg_data;
1061 if ( (error = vnode_getwithref(vp)) ) {
1062 return(error);
1063 }
2d21ac55
A
1064
1065#if CONFIG_MACF
1066 error = mac_vnode_check_write(ctx, vfs_context_ucred(ctx), vp);
1067 if (error) {
1068 (void)vnode_put(vp);
1069 return (error);
1070 }
1071#endif
1072
39236c6e
A
1073 /*
1074 * IO_SYSCALL_DISPATCH signals to VNOP handlers that this write came from
1075 * a file table write
316670eb
A
1076 */
1077 ioflag = (IO_UNIT | IO_SYSCALL_DISPATCH);
1078
91447636 1079 if (vp->v_type == VREG && (fp->f_fglob->fg_flag & O_APPEND))
1c79356b 1080 ioflag |= IO_APPEND;
91447636 1081 if (fp->f_fglob->fg_flag & FNONBLOCK)
1c79356b 1082 ioflag |= IO_NDELAY;
2d21ac55
A
1083 if ((fp->f_fglob->fg_flag & FNOCACHE) || vnode_isnocache(vp))
1084 ioflag |= IO_NOCACHE;
6d2010ae
A
1085 if (fp->f_fglob->fg_flag & FNODIRECT)
1086 ioflag |= IO_NODIRECT;
316670eb
A
1087 if (fp->f_fglob->fg_flag & FSINGLE_WRITER)
1088 ioflag |= IO_SINGLE_WRITER;
3e170ce0
A
1089 if (fp->f_fglob->fg_flag & O_EVTONLY)
1090 ioflag |= IO_EVTONLY;
6d2010ae 1091
b0d623f7
A
1092 /*
1093 * Treat synchronous mounts and O_FSYNC on the fd as equivalent.
1094 *
1095 * XXX We treat O_DSYNC as O_FSYNC for now, since we can not delay
1096 * XXX the non-essential metadata without some additional VFS work;
1097 * XXX the intent at this point is to plumb the interface for it.
1098 */
1099 if ((fp->f_fglob->fg_flag & (O_FSYNC|O_DSYNC)) ||
1100 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) {
1c79356b 1101 ioflag |= IO_SYNC;
b0d623f7 1102 }
91447636 1103
9bccf70c 1104 if ((flags & FOF_OFFSET) == 0) {
fe8ab488
A
1105 if ((vnode_vtype(vp) == VREG) && !vnode_isswap(vp)) {
1106 vn_offset_lock(fp->f_fglob);
1107 offset_locked = 1;
1108 }
91447636
A
1109 uio->uio_offset = fp->f_fglob->fg_offset;
1110 count = uio_resid(uio);
9bccf70c 1111 }
2d21ac55
A
1112 if (((flags & FOF_OFFSET) == 0) &&
1113 vfs_context_proc(ctx) && (vp->v_type == VREG) &&
b0d623f7
A
1114 (((rlim_t)(uio->uio_offset + uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) ||
1115 ((rlim_t)uio_resid(uio) > (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset)))) {
2d21ac55
A
1116 /*
1117 * If the requested residual would cause us to go past the
1118 * administrative limit, then we need to adjust the residual
1119 * down to cause fewer bytes than requested to be written. If
1120 * we can't do that (e.g. the residual is already 1 byte),
1121 * then we fail the write with EFBIG.
1122 */
b0d623f7
A
1123 residcount = uio_resid(uio);
1124 if ((rlim_t)(uio->uio_offset + uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
1125 clippedsize = (uio->uio_offset + uio_resid(uio)) - p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
1126 } else if ((rlim_t)uio_resid(uio) > (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset)) {
2d21ac55
A
1127 clippedsize = (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset);
1128 }
1129 if (clippedsize >= residcount) {
1130 psignal(p, SIGXFSZ);
fe8ab488
A
1131 error = EFBIG;
1132 goto error_out;
2d21ac55
A
1133 }
1134 partialwrite = 1;
1135 uio_setresid(uio, residcount-clippedsize);
1136 }
1137 if ((flags & FOF_OFFSET) != 0) {
1138 /* for pwrite, append should be ignored */
1139 ioflag &= ~IO_APPEND;
1140 if (p && (vp->v_type == VREG) &&
1141 ((rlim_t)uio->uio_offset >= p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
91447636 1142 psignal(p, SIGXFSZ);
fe8ab488
A
1143 error = EFBIG;
1144 goto error_out;
9bccf70c 1145 }
2d21ac55 1146 if (p && (vp->v_type == VREG) &&
b0d623f7 1147 ((rlim_t)(uio->uio_offset + uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
2d21ac55 1148 //Debugger("vn_bwrite:overstepping the bounds");
b0d623f7
A
1149 residcount = uio_resid(uio);
1150 clippedsize = (uio->uio_offset + uio_resid(uio)) - p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
2d21ac55
A
1151 partialwrite = 1;
1152 uio_setresid(uio, residcount-clippedsize);
1153 }
1154 }
1155
1156 error = VNOP_WRITE(vp, uio, ioflag, ctx);
91447636 1157
2d21ac55
A
1158 if (partialwrite) {
1159 oldcount = uio_resid(uio);
1160 uio_setresid(uio, oldcount + clippedsize);
1161 }
1c79356b 1162
9bccf70c
A
1163 if ((flags & FOF_OFFSET) == 0) {
1164 if (ioflag & IO_APPEND)
91447636 1165 fp->f_fglob->fg_offset = uio->uio_offset;
9bccf70c 1166 else
91447636 1167 fp->f_fglob->fg_offset += count - uio_resid(uio);
fe8ab488
A
1168 if (offset_locked) {
1169 vn_offset_unlock(fp->f_fglob);
1170 offset_locked = 0;
1171 }
9bccf70c
A
1172 }
1173
1c79356b
A
1174 /*
1175 * Set the credentials on successful writes
1176 */
1177 if ((error == 0) && (vp->v_tag == VT_NFS) && (UBCINFOEXISTS(vp))) {
13fec989
A
1178 /*
1179 * When called from aio subsystem, we only have the proc from
1180 * which to get the credential, at this point, so use that
1181 * instead. This means aio functions are incompatible with
1182 * per-thread credentials (aio operations are proxied). We
1183 * can't easily correct the aio vs. settid race in this case
1184 * anyway, so we disallow it.
1185 */
1186 if ((flags & FOF_PCRED) == 0) {
1187 ubc_setthreadcred(vp, p, current_thread());
1188 } else {
1189 ubc_setcred(vp, p);
1190 }
1c79356b 1191 }
91447636 1192 (void)vnode_put(vp);
1c79356b 1193 return (error);
fe8ab488
A
1194
1195error_out:
1196 if (offset_locked) {
1197 vn_offset_unlock(fp->f_fglob);
1198 }
1199 (void)vnode_put(vp);
1200 return (error);
1c79356b
A
1201}
1202
1203/*
1204 * File table vnode stat routine.
2d21ac55
A
1205 *
1206 * Returns: 0 Success
1207 * EBADF
1208 * ENOMEM
1209 * vnode_getattr:???
1c79356b 1210 */
9bccf70c 1211int
2d21ac55 1212vn_stat_noauth(struct vnode *vp, void *sbptr, kauth_filesec_t *xsec, int isstat64, vfs_context_t ctx)
1c79356b 1213{
91447636 1214 struct vnode_attr va;
1c79356b
A
1215 int error;
1216 u_short mode;
91447636 1217 kauth_filesec_t fsec;
2d21ac55
A
1218 struct stat *sb = (struct stat *)0; /* warning avoidance ; protected by isstat64 */
1219 struct stat64 * sb64 = (struct stat64 *)0; /* warning avoidance ; protected by isstat64 */
1220
1221 if (isstat64 != 0)
1222 sb64 = (struct stat64 *)sbptr;
1223 else
1224 sb = (struct stat *)sbptr;
b0d623f7 1225 memset(&va, 0, sizeof(va));
91447636
A
1226 VATTR_INIT(&va);
1227 VATTR_WANTED(&va, va_fsid);
1228 VATTR_WANTED(&va, va_fileid);
1229 VATTR_WANTED(&va, va_mode);
1230 VATTR_WANTED(&va, va_type);
1231 VATTR_WANTED(&va, va_nlink);
1232 VATTR_WANTED(&va, va_uid);
1233 VATTR_WANTED(&va, va_gid);
1234 VATTR_WANTED(&va, va_rdev);
1235 VATTR_WANTED(&va, va_data_size);
1236 VATTR_WANTED(&va, va_access_time);
1237 VATTR_WANTED(&va, va_modify_time);
1238 VATTR_WANTED(&va, va_change_time);
2d21ac55 1239 VATTR_WANTED(&va, va_create_time);
91447636
A
1240 VATTR_WANTED(&va, va_flags);
1241 VATTR_WANTED(&va, va_gen);
1242 VATTR_WANTED(&va, va_iosize);
1243 /* lower layers will synthesise va_total_alloc from va_data_size if required */
1244 VATTR_WANTED(&va, va_total_alloc);
1245 if (xsec != NULL) {
1246 VATTR_WANTED(&va, va_uuuid);
1247 VATTR_WANTED(&va, va_guuid);
1248 VATTR_WANTED(&va, va_acl);
1249 }
1250 error = vnode_getattr(vp, &va, ctx);
1c79356b 1251 if (error)
91447636 1252 goto out;
1c79356b
A
1253 /*
1254 * Copy from vattr table
1255 */
2d21ac55
A
1256 if (isstat64 != 0) {
1257 sb64->st_dev = va.va_fsid;
1258 sb64->st_ino = (ino64_t)va.va_fileid;
1259
1260 } else {
1261 sb->st_dev = va.va_fsid;
1262 sb->st_ino = (ino_t)va.va_fileid;
1263 }
91447636 1264 mode = va.va_mode;
1c79356b
A
1265 switch (vp->v_type) {
1266 case VREG:
1267 mode |= S_IFREG;
1268 break;
1269 case VDIR:
1270 mode |= S_IFDIR;
1271 break;
1272 case VBLK:
1273 mode |= S_IFBLK;
1274 break;
1275 case VCHR:
1276 mode |= S_IFCHR;
1277 break;
1278 case VLNK:
1279 mode |= S_IFLNK;
1280 break;
1281 case VSOCK:
1282 mode |= S_IFSOCK;
1283 break;
1284 case VFIFO:
1285 mode |= S_IFIFO;
1286 break;
1287 default:
91447636
A
1288 error = EBADF;
1289 goto out;
1c79356b 1290 };
2d21ac55
A
1291 if (isstat64 != 0) {
1292 sb64->st_mode = mode;
1293 sb64->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? (u_int16_t)va.va_nlink : 1;
1294 sb64->st_uid = va.va_uid;
1295 sb64->st_gid = va.va_gid;
1296 sb64->st_rdev = va.va_rdev;
1297 sb64->st_size = va.va_data_size;
1298 sb64->st_atimespec = va.va_access_time;
1299 sb64->st_mtimespec = va.va_modify_time;
1300 sb64->st_ctimespec = va.va_change_time;
3e170ce0
A
1301 if (VATTR_IS_SUPPORTED(&va, va_create_time)) {
1302 sb64->st_birthtimespec = va.va_create_time;
1303 } else {
1304 sb64->st_birthtimespec.tv_sec = sb64->st_birthtimespec.tv_nsec = 0;
1305 }
2d21ac55
A
1306 sb64->st_blksize = va.va_iosize;
1307 sb64->st_flags = va.va_flags;
1308 sb64->st_blocks = roundup(va.va_total_alloc, 512) / 512;
1309 } else {
1310 sb->st_mode = mode;
1311 sb->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? (u_int16_t)va.va_nlink : 1;
1312 sb->st_uid = va.va_uid;
1313 sb->st_gid = va.va_gid;
1314 sb->st_rdev = va.va_rdev;
1315 sb->st_size = va.va_data_size;
1316 sb->st_atimespec = va.va_access_time;
1317 sb->st_mtimespec = va.va_modify_time;
1318 sb->st_ctimespec = va.va_change_time;
1319 sb->st_blksize = va.va_iosize;
1320 sb->st_flags = va.va_flags;
1321 sb->st_blocks = roundup(va.va_total_alloc, 512) / 512;
1322 }
91447636 1323
6d2010ae 1324 /* if we're interested in extended security data and we got an ACL */
91447636
A
1325 if (xsec != NULL) {
1326 if (!VATTR_IS_SUPPORTED(&va, va_acl) &&
1327 !VATTR_IS_SUPPORTED(&va, va_uuuid) &&
1328 !VATTR_IS_SUPPORTED(&va, va_guuid)) {
1329 *xsec = KAUTH_FILESEC_NONE;
1330 } else {
1331
1332 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
1333 fsec = kauth_filesec_alloc(va.va_acl->acl_entrycount);
1334 } else {
1335 fsec = kauth_filesec_alloc(0);
1336 }
1337 if (fsec == NULL) {
1338 error = ENOMEM;
1339 goto out;
1340 }
1341 fsec->fsec_magic = KAUTH_FILESEC_MAGIC;
1342 if (VATTR_IS_SUPPORTED(&va, va_uuuid)) {
1343 fsec->fsec_owner = va.va_uuuid;
1344 } else {
1345 fsec->fsec_owner = kauth_null_guid;
1346 }
1347 if (VATTR_IS_SUPPORTED(&va, va_guuid)) {
1348 fsec->fsec_group = va.va_guuid;
1349 } else {
1350 fsec->fsec_group = kauth_null_guid;
1351 }
1352 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
1353 bcopy(va.va_acl, &(fsec->fsec_acl), KAUTH_ACL_COPYSIZE(va.va_acl));
1354 } else {
1355 fsec->fsec_acl.acl_entrycount = KAUTH_FILESEC_NOACL;
1356 }
1357 *xsec = fsec;
1358 }
1359 }
1360
1c79356b 1361 /* Do not give the generation number out to unpriviledged users */
2d21ac55
A
1362 if (va.va_gen && !vfs_context_issuser(ctx)) {
1363 if (isstat64 != 0)
1364 sb64->st_gen = 0;
1365 else
1366 sb->st_gen = 0;
1367 } else {
1368 if (isstat64 != 0)
1369 sb64->st_gen = va.va_gen;
1370 else
1371 sb->st_gen = va.va_gen;
1372 }
91447636
A
1373
1374 error = 0;
1375out:
1376 if (VATTR_IS_SUPPORTED(&va, va_acl) && va.va_acl != NULL)
1377 kauth_acl_free(va.va_acl);
1378 return (error);
1379}
1380
1381int
2d21ac55 1382vn_stat(struct vnode *vp, void *sb, kauth_filesec_t *xsec, int isstat64, vfs_context_t ctx)
91447636
A
1383{
1384 int error;
1385
2d21ac55
A
1386#if CONFIG_MACF
1387 error = mac_vnode_check_stat(ctx, NOCRED, vp);
1388 if (error)
1389 return (error);
1390#endif
1391
91447636
A
1392 /* authorize */
1393 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY, ctx)) != 0)
1394 return(error);
1395
1396 /* actual stat */
2d21ac55 1397 return(vn_stat_noauth(vp, sb, xsec, isstat64, ctx));
1c79356b
A
1398}
1399
91447636 1400
1c79356b
A
1401/*
1402 * File table vnode ioctl routine.
1403 */
9bccf70c 1404static int
2d21ac55 1405vn_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx)
1c79356b 1406{
2d21ac55 1407 struct vnode *vp = ((struct vnode *)fp->f_fglob->fg_data);
91447636 1408 off_t file_size;
1c79356b 1409 int error;
fa4905b1 1410 struct vnode *ttyvp;
2d21ac55 1411 struct session * sessp;
9bccf70c 1412
91447636
A
1413 if ( (error = vnode_getwithref(vp)) ) {
1414 return(error);
1415 }
91447636 1416
2d21ac55
A
1417#if CONFIG_MACF
1418 error = mac_vnode_check_ioctl(ctx, vp, com);
1419 if (error)
1420 goto out;
1421#endif
1c79356b 1422
2d21ac55 1423 switch (vp->v_type) {
1c79356b
A
1424 case VREG:
1425 case VDIR:
1426 if (com == FIONREAD) {
2d21ac55 1427 if ((error = vnode_size(vp, &file_size, ctx)) != 0)
91447636
A
1428 goto out;
1429 *(int *)data = file_size - fp->f_fglob->fg_offset;
1430 goto out;
1431 }
1432 if (com == FIONBIO || com == FIOASYNC) { /* XXX */
1433 goto out;
1c79356b 1434 }
1c79356b
A
1435 /* fall into ... */
1436
1437 default:
91447636
A
1438 error = ENOTTY;
1439 goto out;
1c79356b
A
1440
1441 case VFIFO:
1442 case VCHR:
1443 case VBLK:
9bccf70c 1444
91447636
A
1445 /* Should not be able to set block size from user space */
1446 if (com == DKIOCSETBLOCKSIZE) {
1447 error = EPERM;
1448 goto out;
1449 }
1450
1451 if (com == FIODTYPE) {
1452 if (vp->v_type == VBLK) {
1453 if (major(vp->v_rdev) >= nblkdev) {
1454 error = ENXIO;
1455 goto out;
1456 }
39236c6e 1457 *(int *)data = bdevsw[major(vp->v_rdev)].d_type;
91447636
A
1458
1459 } else if (vp->v_type == VCHR) {
1460 if (major(vp->v_rdev) >= nchrdev) {
1461 error = ENXIO;
1462 goto out;
1463 }
39236c6e 1464 *(int *)data = cdevsw[major(vp->v_rdev)].d_type;
91447636
A
1465 } else {
1466 error = ENOTTY;
1467 goto out;
1468 }
1469 goto out;
1470 }
2d21ac55 1471 error = VNOP_IOCTL(vp, com, data, fp->f_fglob->fg_flag, ctx);
91447636
A
1472
1473 if (error == 0 && com == TIOCSCTTY) {
2d21ac55
A
1474 sessp = proc_session(vfs_context_proc(ctx));
1475
1476 session_lock(sessp);
1477 ttyvp = sessp->s_ttyvp;
1478 sessp->s_ttyvp = vp;
1479 sessp->s_ttyvid = vnode_vid(vp);
1480 session_unlock(sessp);
1481 session_rele(sessp);
91447636 1482 }
1c79356b 1483 }
91447636
A
1484out:
1485 (void)vnode_put(vp);
1486 return(error);
1c79356b
A
1487}
1488
1489/*
1490 * File table vnode select routine.
1491 */
9bccf70c 1492static int
2d21ac55 1493vn_select(struct fileproc *fp, int which, void *wql, __unused vfs_context_t ctx)
1c79356b 1494{
91447636
A
1495 int error;
1496 struct vnode * vp = (struct vnode *)fp->f_fglob->fg_data;
1497 struct vfs_context context;
1498
1499 if ( (error = vnode_getwithref(vp)) == 0 ) {
2d21ac55 1500 context.vc_thread = current_thread();
91447636
A
1501 context.vc_ucred = fp->f_fglob->fg_cred;
1502
2d21ac55
A
1503#if CONFIG_MACF
1504 /*
1505 * XXX We should use a per thread credential here; minimally,
1506 * XXX the process credential should have a persistent
1507 * XXX reference on it before being passed in here.
1508 */
1509 error = mac_vnode_check_select(ctx, vp, which);
1510 if (error == 0)
1511#endif
1512 error = VNOP_SELECT(vp, which, fp->f_fglob->fg_flag, wql, ctx);
1c79356b 1513
91447636
A
1514 (void)vnode_put(vp);
1515 }
1516 return(error);
1517
1c79356b
A
1518}
1519
1c79356b
A
1520/*
1521 * File table vnode close routine.
1522 */
9bccf70c 1523static int
2d21ac55 1524vn_closefile(struct fileglob *fg, vfs_context_t ctx)
1c79356b 1525{
3e170ce0 1526 struct vnode *vp = fg->fg_data;
91447636
A
1527 int error;
1528
1529 if ( (error = vnode_getwithref(vp)) == 0 ) {
3e170ce0
A
1530 if (FILEGLOB_DTYPE(fg) == DTYPE_VNODE &&
1531 ((fg->fg_flag & FHASLOCK) != 0 ||
1532 (fg->fg_lflags & FG_HAS_OFDLOCK) != 0)) {
1533 struct flock lf = {
1534 .l_whence = SEEK_SET,
1535 .l_start = 0,
1536 .l_len = 0,
1537 .l_type = F_UNLCK
1538 };
1539
1540 if ((fg->fg_flag & FHASLOCK) != 0)
1541 (void) VNOP_ADVLOCK(vp, (caddr_t)fg,
1542 F_UNLCK, &lf, F_FLOCK, ctx, NULL);
1543
1544 if ((fg->fg_lflags & FG_HAS_OFDLOCK) != 0)
1545 (void) VNOP_ADVLOCK(vp, (caddr_t)fg,
1546 F_UNLCK, &lf, F_OFD_LOCK, ctx, NULL);
2d21ac55
A
1547 }
1548 error = vn_close(vp, fg->fg_flag, ctx);
3e170ce0 1549 (void) vnode_put(vp);
91447636 1550 }
3e170ce0 1551 return (error);
91447636
A
1552}
1553
2d21ac55
A
1554/*
1555 * Returns: 0 Success
1556 * VNOP_PATHCONF:???
1557 */
91447636 1558int
b0d623f7 1559vn_pathconf(vnode_t vp, int name, int32_t *retval, vfs_context_t ctx)
91447636
A
1560{
1561 int error = 0;
6d2010ae 1562 struct vfs_attr vfa;
91447636
A
1563
1564 switch(name) {
1565 case _PC_EXTENDED_SECURITY_NP:
2d21ac55 1566 *retval = vfs_extendedsecurity(vnode_mount(vp)) ? 1 : 0;
91447636
A
1567 break;
1568 case _PC_AUTH_OPAQUE_NP:
1569 *retval = vfs_authopaque(vnode_mount(vp));
1570 break;
2d21ac55
A
1571 case _PC_2_SYMLINKS:
1572 *retval = 1; /* XXX NOTSUP on MSDOS, etc. */
1573 break;
1574 case _PC_ALLOC_SIZE_MIN:
1575 *retval = 1; /* XXX lie: 1 byte */
1576 break;
1577 case _PC_ASYNC_IO: /* unistd.h: _POSIX_ASYNCHRONUS_IO */
1578 *retval = 1; /* [AIO] option is supported */
1579 break;
1580 case _PC_PRIO_IO: /* unistd.h: _POSIX_PRIORITIZED_IO */
1581 *retval = 0; /* [PIO] option is not supported */
1582 break;
1583 case _PC_REC_INCR_XFER_SIZE:
1584 *retval = 4096; /* XXX go from MIN to MAX 4K at a time */
1585 break;
1586 case _PC_REC_MIN_XFER_SIZE:
1587 *retval = 4096; /* XXX recommend 4K minimum reads/writes */
1588 break;
1589 case _PC_REC_MAX_XFER_SIZE:
1590 *retval = 65536; /* XXX recommend 64K maximum reads/writes */
1591 break;
1592 case _PC_REC_XFER_ALIGN:
1593 *retval = 4096; /* XXX recommend page aligned buffers */
1594 break;
1595 case _PC_SYMLINK_MAX:
1596 *retval = 255; /* Minimum acceptable POSIX value */
1597 break;
1598 case _PC_SYNC_IO: /* unistd.h: _POSIX_SYNCHRONIZED_IO */
1599 *retval = 0; /* [SIO] option is not supported */
1600 break;
6d2010ae
A
1601 case _PC_XATTR_SIZE_BITS:
1602 /* The number of bits used to store maximum extended
1603 * attribute size in bytes. For example, if the maximum
1604 * attribute size supported by a file system is 128K, the
1605 * value returned will be 18. However a value 18 can mean
1606 * that the maximum attribute size can be anywhere from
1607 * (256KB - 1) to 128KB. As a special case, the resource
1608 * fork can have much larger size, and some file system
1609 * specific extended attributes can have smaller and preset
1610 * size; for example, Finder Info is always 32 bytes.
1611 */
1612 memset(&vfa, 0, sizeof(vfa));
1613 VFSATTR_INIT(&vfa);
1614 VFSATTR_WANTED(&vfa, f_capabilities);
1615 if (vfs_getattr(vnode_mount(vp), &vfa, ctx) == 0 &&
1616 (VFSATTR_IS_SUPPORTED(&vfa, f_capabilities)) &&
1617 (vfa.f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR) &&
1618 (vfa.f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR)) {
1619 /* Supports native extended attributes */
1620 error = VNOP_PATHCONF(vp, name, retval, ctx);
1621 } else {
1622 /* Number of bits used to represent the maximum size of
1623 * extended attribute stored in an Apple Double file.
1624 */
1625 *retval = AD_XATTR_SIZE_BITS;
1626 }
1627 break;
91447636
A
1628 default:
1629 error = VNOP_PATHCONF(vp, name, retval, ctx);
1630 break;
1631 }
1c79356b 1632
91447636 1633 return (error);
1c79356b 1634}
55e303ae
A
1635
1636static int
2d21ac55 1637vn_kqfilt_add(struct fileproc *fp, struct knote *kn, vfs_context_t ctx)
55e303ae 1638{
55e303ae 1639 int error;
b0d623f7 1640 struct vnode *vp;
55e303ae 1641
b0d623f7
A
1642 vp = (struct vnode *)fp->f_fglob->fg_data;
1643
1644 /*
1645 * Don't attach a knote to a dead vnode.
1646 */
1647 if ((error = vget_internal(vp, 0, VNODE_NODEAD)) == 0) {
1648 switch (kn->kn_filter) {
1649 case EVFILT_READ:
1650 case EVFILT_WRITE:
1651 if (vnode_isfifo(vp)) {
1652 /* We'll only watch FIFOs that use our fifofs */
1653 if (!(vp->v_fifoinfo && vp->v_fifoinfo->fi_readsock)) {
1654 error = ENOTSUP;
1655 }
1656
1657 } else if (!vnode_isreg(vp)) {
6d2010ae 1658 if (vnode_ischr(vp) &&
b0d623f7
A
1659 (error = spec_kqfilter(vp, kn)) == 0) {
1660 /* claimed by a special device */
1661 vnode_put(vp);
1662 return 0;
1663 }
1664
1665 error = EINVAL;
1666 }
1667 break;
1668 case EVFILT_VNODE:
1669 break;
1670 default:
1671 error = EINVAL;
1672 }
1673
1674 if (error) {
1675 vnode_put(vp);
1676 return error;
1677 }
2d21ac55
A
1678
1679#if CONFIG_MACF
1680 error = mac_vnode_check_kqfilter(ctx, fp->f_fglob->fg_cred, kn, vp);
1681 if (error) {
b0d623f7
A
1682 vnode_put(vp);
1683 return error;
2d21ac55
A
1684 }
1685#endif
91447636 1686
b0d623f7
A
1687 kn->kn_hook = (void*)vp;
1688 kn->kn_hookid = vnode_vid(vp);
1689 kn->kn_fop = &vnode_filtops;
91447636 1690
b0d623f7
A
1691 vnode_lock(vp);
1692 KNOTE_ATTACH(&vp->v_knotes, kn);
1693 vnode_unlock(vp);
1694
1695 /* Ask the filesystem to provide remove notifications, but ignore failure */
1696 VNOP_MONITOR(vp, 0, VNODE_MONITOR_BEGIN, (void*) kn, ctx);
1697
1698 vnode_put(vp);
91447636 1699 }
b0d623f7 1700
55e303ae
A
1701 return (error);
1702}
1703
b0d623f7
A
1704static void
1705filt_vndetach(struct knote *kn)
55e303ae 1706{
b0d623f7
A
1707 vfs_context_t ctx = vfs_context_current();
1708 struct vnode *vp;
1709 vp = (struct vnode *)kn->kn_hook;
1710 if (vnode_getwithvid(vp, kn->kn_hookid))
1711 return;
1712
1713 vnode_lock(vp);
1714 KNOTE_DETACH(&vp->v_knotes, kn);
1715 vnode_unlock(vp);
55e303ae 1716
b0d623f7
A
1717 /*
1718 * Tell a (generally networked) filesystem that we're no longer watching
1719 * If the FS wants to track contexts, it should still be using the one from
1720 * the VNODE_MONITOR_BEGIN.
1721 */
1722 VNOP_MONITOR(vp, 0, VNODE_MONITOR_END, (void*)kn, ctx);
1723 vnode_put(vp);
1724}
91447636 1725
91447636 1726
b0d623f7
A
1727/*
1728 * Used for EVFILT_READ
1729 *
1730 * Takes only VFIFO or VREG. vnode is locked. We handle the "poll" case
1731 * differently than the regular case for VREG files. If not in poll(),
1732 * then we need to know current fileproc offset for VREG.
1733 */
1734static intptr_t
1735vnode_readable_data_count(vnode_t vp, off_t current_offset, int ispoll)
1736{
1737 if (vnode_isfifo(vp)) {
39236c6e 1738#if FIFO
b0d623f7
A
1739 int cnt;
1740 int err = fifo_charcount(vp, &cnt);
1741 if (err == 0) {
1742 return (intptr_t)cnt;
39236c6e
A
1743 } else
1744#endif
1745 {
b0d623f7
A
1746 return (intptr_t)0;
1747 }
1748 } else if (vnode_isreg(vp)) {
1749 if (ispoll) {
1750 return (intptr_t)1;
1751 }
1752
1753 off_t amount;
1754 amount = vp->v_un.vu_ubcinfo->ui_size - current_offset;
1755 if (amount > (off_t)INTPTR_MAX) {
1756 return INTPTR_MAX;
1757 } else if (amount < (off_t)INTPTR_MIN) {
1758 return INTPTR_MIN;
1759 } else {
1760 return (intptr_t)amount;
1761 }
1762 } else {
1763 panic("Should never have an EVFILT_READ except for reg or fifo.");
1764 return 0;
91447636 1765 }
55e303ae 1766}
b0d623f7
A
1767
1768/*
1769 * Used for EVFILT_WRITE.
1770 *
1771 * For regular vnodes, we can always write (1). For named pipes,
1772 * see how much space there is in the buffer. Nothing else is covered.
1773 */
1774static intptr_t
1775vnode_writable_space_count(vnode_t vp)
1776{
1777 if (vnode_isfifo(vp)) {
39236c6e 1778#if FIFO
b0d623f7
A
1779 long spc;
1780 int err = fifo_freespace(vp, &spc);
1781 if (err == 0) {
1782 return (intptr_t)spc;
39236c6e
A
1783 } else
1784#endif
1785 {
b0d623f7
A
1786 return (intptr_t)0;
1787 }
1788 } else if (vnode_isreg(vp)) {
1789 return (intptr_t)1;
1790 } else {
1791 panic("Should never have an EVFILT_READ except for reg or fifo.");
1792 return 0;
1793 }
1794}
1795
1796/*
1797 * Determine whether this knote should be active
1798 *
1799 * This is kind of subtle.
1800 * --First, notice if the vnode has been revoked: in so, override hint
1801 * --EVFILT_READ knotes are checked no matter what the hint is
1802 * --Other knotes activate based on hint.
1803 * --If hint is revoke, set special flags and activate
1804 */
1805static int
1806filt_vnode(struct knote *kn, long hint)
1807{
6d2010ae 1808 vnode_t vp = (struct vnode *)kn->kn_hook;
b0d623f7 1809 int activate = 0;
6d2010ae 1810 long orig_hint = hint;
b0d623f7
A
1811
1812 if (0 == hint) {
6d2010ae
A
1813 vnode_lock(vp);
1814
1815 if (vnode_getiocount(vp, kn->kn_hookid, VNODE_NODEAD | VNODE_WITHID) != 0) {
1816 /* Is recycled */
b0d623f7 1817 hint = NOTE_REVOKE;
6d2010ae
A
1818 }
1819 } else {
1820 lck_mtx_assert(&vp->v_lock, LCK_MTX_ASSERT_OWNED);
1821 }
b0d623f7 1822
6d2010ae 1823 /* Special handling for vnodes that are in recycle or already gone */
b0d623f7
A
1824 if (NOTE_REVOKE == hint) {
1825 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1826 activate = 1;
1827
1828 if ((kn->kn_filter == EVFILT_VNODE) && (kn->kn_sfflags & NOTE_REVOKE)) {
1829 kn->kn_fflags |= NOTE_REVOKE;
1830 }
1831 } else {
1832 switch(kn->kn_filter) {
1833 case EVFILT_READ:
1834 kn->kn_data = vnode_readable_data_count(vp, kn->kn_fp->f_fglob->fg_offset, (kn->kn_flags & EV_POLL));
1835
1836 if (kn->kn_data != 0) {
1837 activate = 1;
1838 }
1839 break;
1840 case EVFILT_WRITE:
1841 kn->kn_data = vnode_writable_space_count(vp);
1842
1843 if (kn->kn_data != 0) {
1844 activate = 1;
1845 }
1846 break;
1847 case EVFILT_VNODE:
1848 /* Check events this note matches against the hint */
1849 if (kn->kn_sfflags & hint) {
1850 kn->kn_fflags |= hint; /* Set which event occurred */
1851 }
1852 if (kn->kn_fflags != 0) {
1853 activate = 1;
1854 }
1855 break;
1856 default:
1857 panic("Invalid knote filter on a vnode!\n");
1858 }
1859 }
1860
6d2010ae
A
1861 if (orig_hint == 0) {
1862 /*
1863 * Definitely need to unlock, may need to put
1864 */
1865 if (hint == 0) {
1866 vnode_put_locked(vp);
1867 }
1868 vnode_unlock(vp);
1869 }
1870
b0d623f7
A
1871 return (activate);
1872}