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