]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/vfs_vnops.c
xnu-3789.60.24.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
00867663
A
570#if DEVELOPMENT || DEBUG
571 /*
572 * XXX VSWAP: Check for entitlements or special flag here
573 * so we can restrict access appropriately.
574 */
575#else /* DEVELOPMENT || DEBUG */
576
577 if (vnode_isswap(vp) && (fmode & (FWRITE | O_TRUNC)) && (ctx != vfs_context_kernel())) {
578 error = EPERM;
579 goto bad;
580 }
581#endif /* DEVELOPMENT || DEBUG */
582
6d2010ae
A
583 /*
584 * Expect to use this code for filesystems without compound VNOPs, for the root
585 * of a filesystem, which can't be "looked up" in the sense of VNOP_LOOKUP(),
586 * and for shadow files, which do not live on the same filesystems as their "parents."
587 */
588 if (need_vnop_open) {
589 if (batched && !vnode_isvroot(vp) && !vnode_isnamedstream(vp)) {
590 panic("Why am I trying to use VNOP_OPEN() on anything other than the root or a named stream?");
1c79356b 591 }
91447636 592
6d2010ae
A
593 if (!did_create) {
594 error = vn_authorize_open_existing(vp, &ndp->ni_cnd, fmode, ctx, NULL);
595 if (error) {
596 goto bad;
2d21ac55
A
597 }
598 }
2d21ac55 599
3e170ce0
A
600 if (VATTR_IS_ACTIVE (vap, va_dataprotect_flags)
601 && ISSET(vap->va_dataprotect_flags, VA_DP_RAWUNENCRYPTED)) {
602 /* Don't allow unencrypted io request from user space unless entitled */
603 boolean_t entitled = FALSE;
604#if !SECURE_KERNEL
605 entitled = IOTaskHasEntitlement(current_task(), "com.apple.private.security.file-unencrypt-access");
606#endif
607 if (!entitled) {
608 error = EPERM;
316670eb
A
609 goto bad;
610 }
3e170ce0
A
611 fmode |= FUNENCRYPTED;
612 }
613
6d2010ae
A
614 error = VNOP_OPEN(vp, fmode, ctx);
615 if (error) {
616 goto bad;
2d21ac55 617 }
6d2010ae
A
618 need_vnop_open = FALSE;
619 }
2d21ac55 620
6d2010ae
A
621 // if the vnode is tagged VOPENEVT and the current process
622 // has the P_CHECKOPENEVT flag set, then we or in the O_EVTONLY
623 // flag to the open mode so that this open won't count against
624 // the vnode when carbon delete() does a vnode_isinuse() to see
625 // if a file is currently in use. this allows spotlight
626 // importers to not interfere with carbon apps that depend on
627 // the no-delete-if-busy semantics of carbon delete().
628 //
629 if (!did_create && (vp->v_flag & VOPENEVT) && (current_proc()->p_flag & P_CHECKOPENEVT)) {
630 fmode |= O_EVTONLY;
1c79356b 631 }
0b4e3aa0 632
6d2010ae
A
633 /*
634 * Grab reference, etc.
635 */
636 error = vn_open_auth_finish(vp, fmode, ctx);
637 if (error) {
638 ref_failed = TRUE;
0b4e3aa0
A
639 goto bad;
640 }
91447636 641
6d2010ae
A
642 /* Compound VNOP open is responsible for doing the truncate */
643 if (batched || did_create)
644 fmode &= ~O_TRUNC;
0b4e3aa0 645
55e303ae 646 *fmodep = fmode;
1c79356b 647 return (0);
6d2010ae 648
1c79356b 649bad:
6d2010ae
A
650 /* Opened either explicitly or by a batched create */
651 if (!need_vnop_open) {
652 VNOP_CLOSE(vp, fmode, ctx);
653 }
654
55e303ae 655 ndp->ni_vp = NULL;
91447636 656 if (vp) {
c910b4d9 657#if NAMEDRSRCFORK
b0d623f7
A
658 /* Aggressively recycle shadow files if we error'd out during open() */
659 if ((vnode_isnamedstream(vp)) &&
660 (vp->v_parent != NULLVP) &&
661 (vnode_isshadow(vp))) {
662 vnode_recycle(vp);
c910b4d9
A
663 }
664#endif
665 vnode_put(vp);
91447636
A
666 /*
667 * Check for a race against unlink. We had a vnode
668 * but according to vnode_authorize or VNOP_OPEN it
669 * no longer exists.
935ed37a
A
670 *
671 * EREDRIVEOPEN: means that we were hit by the tty allocation race.
91447636 672 */
6d2010ae 673 if (((error == ENOENT) && (*fmodep & O_CREAT)) || (error == EREDRIVEOPEN) || ref_failed) {
3e170ce0
A
674 /*
675 * We'll retry here but it may be possible that we get
676 * into a retry "spin" inside the kernel and not allow
677 * threads, which need to run in order for the retry
678 * loop to end, to run. An example is an open of a
679 * terminal which is getting revoked and we spin here
680 * without yielding becasue namei and VNOP_OPEN are
681 * successful but vnode_ref fails. The revoke needs
682 * threads with an iocount to run but if spin here we
683 * may possibly be blcoking other threads from running.
684 *
685 * We start yielding the CPU after some number of
686 * retries for increasing durations. Note that this is
687 * still a loop without an exit condition.
688 */
689 nretries += 1;
690 if (nretries > RETRY_NO_YIELD_COUNT) {
691 /* Every hz/100 secs is 10 msecs ... */
692 tsleep(&nretries, PVFS, "vn_open_auth_retry",
693 MIN((nretries * (hz/100)), hz));
694 }
91447636
A
695 goto again;
696 }
697 }
6d2010ae 698
91447636 699out:
1c79356b
A
700 return (error);
701}
702
2d21ac55 703#if vn_access_DEPRECATED
1c79356b 704/*
91447636
A
705 * Authorize an action against a vnode. This has been the canonical way to
706 * ensure that the credential/process/etc. referenced by a vfs_context
707 * is granted the rights called out in 'mode' against the vnode 'vp'.
708 *
709 * Unfortunately, the use of VREAD/VWRITE/VEXEC makes it very difficult
710 * to add support for more rights. As such, this interface will be deprecated
711 * and callers will use vnode_authorize instead.
1c79356b 712 */
9bccf70c 713int
91447636 714vn_access(vnode_t vp, int mode, vfs_context_t context)
1c79356b 715{
91447636
A
716 kauth_action_t action;
717
718 action = 0;
719 if (mode & VREAD)
720 action |= KAUTH_VNODE_READ_DATA;
721 if (mode & VWRITE)
722 action |= KAUTH_VNODE_WRITE_DATA;
723 if (mode & VEXEC)
724 action |= KAUTH_VNODE_EXECUTE;
725
726 return(vnode_authorize(vp, NULL, action, context));
1c79356b 727}
2d21ac55 728#endif /* vn_access_DEPRECATED */
1c79356b
A
729
730/*
731 * Vnode close call
732 */
9bccf70c 733int
2d21ac55 734vn_close(struct vnode *vp, int flags, vfs_context_t ctx)
1c79356b
A
735{
736 int error;
39236c6e 737 int flusherror = 0;
1c79356b 738
2d21ac55 739#if NAMEDRSRCFORK
cf7d32b8 740 /* Sync data from resource fork shadow file if needed. */
2d21ac55
A
741 if ((vp->v_flag & VISNAMEDSTREAM) &&
742 (vp->v_parent != NULLVP) &&
b0d623f7 743 vnode_isshadow(vp)) {
2d21ac55 744 if (flags & FWASWRITTEN) {
39236c6e 745 flusherror = vnode_flushnamedstream(vp->v_parent, vp, ctx);
2d21ac55 746 }
2d21ac55
A
747 }
748#endif
593a1d5f
A
749
750 /* work around for foxhound */
b0d623f7 751 if (vnode_isspec(vp))
593a1d5f
A
752 (void)vnode_rele_ext(vp, flags, 0);
753
fe8ab488
A
754 /*
755 * On HFS, we flush when the last writer closes. We do this
756 * because resource fork vnodes hold a reference on data fork
757 * vnodes and that will prevent them from getting VNOP_INACTIVE
758 * which will delay when we flush cached data. In future, we
759 * might find it beneficial to do this for all file systems.
760 * Note that it's OK to access v_writecount without the lock
761 * in this context.
762 */
763 if (vp->v_tag == VT_HFS && (flags & FWRITE) && vp->v_writecount == 1)
764 VNOP_FSYNC(vp, MNT_NOWAIT, ctx);
765
2d21ac55 766 error = VNOP_CLOSE(vp, flags, ctx);
91447636 767
6d2010ae
A
768#if CONFIG_FSE
769 if (flags & FWASWRITTEN) {
770 if (need_fsevent(FSE_CONTENT_MODIFIED, vp)) {
771 add_fsevent(FSE_CONTENT_MODIFIED, ctx,
772 FSE_ARG_VNODE, vp,
773 FSE_ARG_DONE);
774 }
775 }
776#endif
777
b0d623f7 778 if (!vnode_isspec(vp))
593a1d5f
A
779 (void)vnode_rele_ext(vp, flags, 0);
780
39236c6e
A
781 if (flusherror) {
782 error = flusherror;
783 }
1c79356b
A
784 return (error);
785}
786
91447636
A
787static int
788vn_read_swapfile(
789 struct vnode *vp,
790 uio_t uio)
791{
91447636
A
792 int error;
793 off_t swap_count, this_count;
794 off_t file_end, read_end;
795 off_t prev_resid;
b0d623f7 796 char *my_swap_page;
91447636
A
797
798 /*
b0d623f7 799 * Reading from a swap file will get you zeroes.
91447636 800 */
b0d623f7
A
801
802 my_swap_page = NULL;
91447636
A
803 error = 0;
804 swap_count = uio_resid(uio);
805
806 file_end = ubc_getsize(vp);
807 read_end = uio->uio_offset + uio_resid(uio);
808 if (uio->uio_offset >= file_end) {
809 /* uio starts after end of file: nothing to read */
810 swap_count = 0;
811 } else if (read_end > file_end) {
812 /* uio extends beyond end of file: stop before that */
813 swap_count -= (read_end - file_end);
814 }
815
816 while (swap_count > 0) {
b0d623f7
A
817 if (my_swap_page == NULL) {
818 MALLOC(my_swap_page, char *, PAGE_SIZE,
819 M_TEMP, M_WAITOK);
820 memset(my_swap_page, '\0', PAGE_SIZE);
821 /* add an end-of-line to keep line counters happy */
822 my_swap_page[PAGE_SIZE-1] = '\n';
91447636 823 }
91447636
A
824 this_count = swap_count;
825 if (this_count > PAGE_SIZE) {
826 this_count = PAGE_SIZE;
827 }
828
829 prev_resid = uio_resid(uio);
b0d623f7 830 error = uiomove((caddr_t) my_swap_page,
91447636
A
831 this_count,
832 uio);
833 if (error) {
834 break;
835 }
836 swap_count -= (prev_resid - uio_resid(uio));
837 }
b0d623f7
A
838 if (my_swap_page != NULL) {
839 FREE(my_swap_page, M_TEMP);
840 my_swap_page = NULL;
841 }
91447636
A
842
843 return error;
844}
1c79356b
A
845/*
846 * Package up an I/O request on a vnode into a uio and do it.
847 */
9bccf70c 848int
91447636
A
849vn_rdwr(
850 enum uio_rw rw,
851 struct vnode *vp,
852 caddr_t base,
853 int len,
854 off_t offset,
855 enum uio_seg segflg,
856 int ioflg,
857 kauth_cred_t cred,
858 int *aresid,
2d21ac55 859 proc_t p)
1c79356b 860{
b0d623f7
A
861 int64_t resid;
862 int result;
863
864 result = vn_rdwr_64(rw,
91447636
A
865 vp,
866 (uint64_t)(uintptr_t)base,
867 (int64_t)len,
868 offset,
869 segflg,
870 ioflg,
871 cred,
b0d623f7 872 &resid,
91447636 873 p);
b0d623f7
A
874
875 /* "resid" should be bounded above by "len," which is an int */
876 if (aresid != NULL) {
877 *aresid = resid;
878 }
879
880 return result;
91447636
A
881}
882
883
884int
885vn_rdwr_64(
886 enum uio_rw rw,
887 struct vnode *vp,
888 uint64_t base,
889 int64_t len,
890 off_t offset,
891 enum uio_seg segflg,
892 int ioflg,
893 kauth_cred_t cred,
b0d623f7 894 int64_t *aresid,
2d21ac55 895 proc_t p)
91447636
A
896{
897 uio_t auio;
898 int spacetype;
899 struct vfs_context context;
1c79356b 900 int error=0;
91447636
A
901 char uio_buf[ UIO_SIZEOF(1) ];
902
2d21ac55 903 context.vc_thread = current_thread();
91447636 904 context.vc_ucred = cred;
1c79356b 905
91447636
A
906 if (UIO_SEG_IS_USER_SPACE(segflg)) {
907 spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32;
908 }
909 else {
910 spacetype = UIO_SYSSPACE;
911 }
912 auio = uio_createwithbuffer(1, offset, spacetype, rw,
913 &uio_buf[0], sizeof(uio_buf));
914 uio_addiov(auio, base, len);
915
2d21ac55
A
916#if CONFIG_MACF
917 /* XXXMAC
918 * IO_NOAUTH should be re-examined.
919 * Likely that mediation should be performed in caller.
920 */
921 if ((ioflg & IO_NOAUTH) == 0) {
922 /* passed cred is fp->f_cred */
923 if (rw == UIO_READ)
924 error = mac_vnode_check_read(&context, cred, vp);
925 else
926 error = mac_vnode_check_write(&context, cred, vp);
927 }
928#endif
929
930 if (error == 0) {
931 if (rw == UIO_READ) {
39236c6e 932 if (vnode_isswap(vp) && ((ioflg & IO_SWAP_DISPATCH) == 0)) {
2d21ac55
A
933 error = vn_read_swapfile(vp, auio);
934 } else {
935 error = VNOP_READ(vp, auio, ioflg, &context);
936 }
91447636 937 } else {
00867663
A
938
939#if DEVELOPMENT || DEBUG
940 /*
941 * XXX VSWAP: Check for entitlements or special flag here
942 * so we can restrict access appropriately.
943 */
2d21ac55 944 error = VNOP_WRITE(vp, auio, ioflg, &context);
00867663
A
945#else /* DEVELOPMENT || DEBUG */
946
947 if (vnode_isswap(vp) && ((ioflg & (IO_SWAP_DISPATCH | IO_SKIP_ENCRYPTION)) == 0)) {
948 error = EPERM;
949 } else {
950 error = VNOP_WRITE(vp, auio, ioflg, &context);
951 }
952#endif /* DEVELOPMENT || DEBUG */
91447636 953 }
91447636 954 }
1c79356b
A
955
956 if (aresid)
91447636 957 *aresid = uio_resid(auio);
1c79356b 958 else
91447636 959 if (uio_resid(auio) && error == 0)
1c79356b 960 error = EIO;
1c79356b
A
961 return (error);
962}
963
fe8ab488
A
964static inline void
965vn_offset_lock(struct fileglob *fg)
966{
967 lck_mtx_lock_spin(&fg->fg_lock);
968 while (fg->fg_lflags & FG_OFF_LOCKED) {
969 fg->fg_lflags |= FG_OFF_LOCKWANT;
970 msleep(&fg->fg_lflags, &fg->fg_lock, PVFS | PSPIN,
971 "fg_offset_lock_wait", 0);
972 }
973 fg->fg_lflags |= FG_OFF_LOCKED;
974 lck_mtx_unlock(&fg->fg_lock);
975}
976
977static inline void
978vn_offset_unlock(struct fileglob *fg)
979{
980 int lock_wanted = 0;
981
982 lck_mtx_lock_spin(&fg->fg_lock);
983 if (fg->fg_lflags & FG_OFF_LOCKWANT) {
984 lock_wanted = 1;
985 }
986 fg->fg_lflags &= ~(FG_OFF_LOCKED | FG_OFF_LOCKWANT);
987 lck_mtx_unlock(&fg->fg_lock);
988 if (lock_wanted) {
989 wakeup(&fg->fg_lflags);
990 }
991}
992
1c79356b
A
993/*
994 * File table vnode read routine.
995 */
9bccf70c 996static int
2d21ac55 997vn_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx)
1c79356b 998{
9bccf70c 999 struct vnode *vp;
39236c6e
A
1000 int error;
1001 int ioflag;
1c79356b 1002 off_t count;
fe8ab488 1003 int offset_locked = 0;
9bccf70c 1004
91447636
A
1005 vp = (struct vnode *)fp->f_fglob->fg_data;
1006 if ( (error = vnode_getwithref(vp)) ) {
1007 return(error);
1008 }
2d21ac55
A
1009
1010#if CONFIG_MACF
1011 error = mac_vnode_check_read(ctx, vfs_context_ucred(ctx), vp);
1012 if (error) {
1013 (void)vnode_put(vp);
1014 return (error);
1015 }
1016#endif
1017
316670eb
A
1018 /* This signals to VNOP handlers that this read came from a file table read */
1019 ioflag = IO_SYSCALL_DISPATCH;
1020
91447636 1021 if (fp->f_fglob->fg_flag & FNONBLOCK)
9bccf70c 1022 ioflag |= IO_NDELAY;
2d21ac55 1023 if ((fp->f_fglob->fg_flag & FNOCACHE) || vnode_isnocache(vp))
316670eb
A
1024 ioflag |= IO_NOCACHE;
1025 if (fp->f_fglob->fg_flag & FENCRYPTED) {
1026 ioflag |= IO_ENCRYPTED;
1027 }
3e170ce0
A
1028 if (fp->f_fglob->fg_flag & FUNENCRYPTED) {
1029 ioflag |= IO_SKIP_ENCRYPTION;
1030 }
1031 if (fp->f_fglob->fg_flag & O_EVTONLY) {
1032 ioflag |= IO_EVTONLY;
1033 }
2d21ac55 1034 if (fp->f_fglob->fg_flag & FNORDAHEAD)
316670eb 1035 ioflag |= IO_RAOFF;
91447636 1036
fe8ab488
A
1037 if ((flags & FOF_OFFSET) == 0) {
1038 if ((vnode_vtype(vp) == VREG) && !vnode_isswap(vp)) {
1039 vn_offset_lock(fp->f_fglob);
1040 offset_locked = 1;
1041 }
91447636 1042 uio->uio_offset = fp->f_fglob->fg_offset;
fe8ab488 1043 }
91447636
A
1044 count = uio_resid(uio);
1045
3e170ce0 1046 if (vnode_isswap(vp) && !(IO_SKIP_ENCRYPTION & ioflag)) {
00867663 1047
91447636
A
1048 /* special case for swap files */
1049 error = vn_read_swapfile(vp, uio);
1050 } else {
2d21ac55 1051 error = VNOP_READ(vp, uio, ioflag, ctx);
9bccf70c 1052 }
00867663 1053
fe8ab488 1054 if ((flags & FOF_OFFSET) == 0) {
91447636 1055 fp->f_fglob->fg_offset += count - uio_resid(uio);
fe8ab488
A
1056 if (offset_locked) {
1057 vn_offset_unlock(fp->f_fglob);
1058 offset_locked = 0;
1059 }
1060 }
91447636
A
1061
1062 (void)vnode_put(vp);
1c79356b
A
1063 return (error);
1064}
1065
1066
1067/*
1068 * File table vnode write routine.
1069 */
9bccf70c 1070static int
2d21ac55 1071vn_write(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx)
1c79356b 1072{
9bccf70c
A
1073 struct vnode *vp;
1074 int error, ioflag;
1c79356b 1075 off_t count;
2d21ac55
A
1076 int clippedsize = 0;
1077 int partialwrite=0;
1078 int residcount, oldcount;
fe8ab488 1079 int offset_locked = 0;
2d21ac55 1080 proc_t p = vfs_context_proc(ctx);
91447636 1081
91447636
A
1082 count = 0;
1083 vp = (struct vnode *)fp->f_fglob->fg_data;
1084 if ( (error = vnode_getwithref(vp)) ) {
1085 return(error);
1086 }
2d21ac55 1087
00867663
A
1088#if DEVELOPMENT || DEBUG
1089 /*
1090 * XXX VSWAP: Check for entitlements or special flag here
1091 * so we can restrict access appropriately.
1092 */
1093#else /* DEVELOPMENT || DEBUG */
1094
1095 if (vnode_isswap(vp)) {
1096 (void)vnode_put(vp);
1097 error = EPERM;
1098 return (error);
1099 }
1100#endif /* DEVELOPMENT || DEBUG */
1101
1102
2d21ac55
A
1103#if CONFIG_MACF
1104 error = mac_vnode_check_write(ctx, vfs_context_ucred(ctx), vp);
1105 if (error) {
1106 (void)vnode_put(vp);
1107 return (error);
1108 }
1109#endif
1110
39236c6e
A
1111 /*
1112 * IO_SYSCALL_DISPATCH signals to VNOP handlers that this write came from
1113 * a file table write
316670eb
A
1114 */
1115 ioflag = (IO_UNIT | IO_SYSCALL_DISPATCH);
1116
91447636 1117 if (vp->v_type == VREG && (fp->f_fglob->fg_flag & O_APPEND))
1c79356b 1118 ioflag |= IO_APPEND;
91447636 1119 if (fp->f_fglob->fg_flag & FNONBLOCK)
1c79356b 1120 ioflag |= IO_NDELAY;
2d21ac55
A
1121 if ((fp->f_fglob->fg_flag & FNOCACHE) || vnode_isnocache(vp))
1122 ioflag |= IO_NOCACHE;
6d2010ae
A
1123 if (fp->f_fglob->fg_flag & FNODIRECT)
1124 ioflag |= IO_NODIRECT;
316670eb
A
1125 if (fp->f_fglob->fg_flag & FSINGLE_WRITER)
1126 ioflag |= IO_SINGLE_WRITER;
3e170ce0
A
1127 if (fp->f_fglob->fg_flag & O_EVTONLY)
1128 ioflag |= IO_EVTONLY;
6d2010ae 1129
b0d623f7
A
1130 /*
1131 * Treat synchronous mounts and O_FSYNC on the fd as equivalent.
1132 *
1133 * XXX We treat O_DSYNC as O_FSYNC for now, since we can not delay
1134 * XXX the non-essential metadata without some additional VFS work;
1135 * XXX the intent at this point is to plumb the interface for it.
1136 */
1137 if ((fp->f_fglob->fg_flag & (O_FSYNC|O_DSYNC)) ||
1138 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))) {
1c79356b 1139 ioflag |= IO_SYNC;
b0d623f7 1140 }
91447636 1141
9bccf70c 1142 if ((flags & FOF_OFFSET) == 0) {
fe8ab488
A
1143 if ((vnode_vtype(vp) == VREG) && !vnode_isswap(vp)) {
1144 vn_offset_lock(fp->f_fglob);
1145 offset_locked = 1;
1146 }
91447636
A
1147 uio->uio_offset = fp->f_fglob->fg_offset;
1148 count = uio_resid(uio);
9bccf70c 1149 }
2d21ac55
A
1150 if (((flags & FOF_OFFSET) == 0) &&
1151 vfs_context_proc(ctx) && (vp->v_type == VREG) &&
b0d623f7
A
1152 (((rlim_t)(uio->uio_offset + uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) ||
1153 ((rlim_t)uio_resid(uio) > (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset)))) {
2d21ac55
A
1154 /*
1155 * If the requested residual would cause us to go past the
1156 * administrative limit, then we need to adjust the residual
1157 * down to cause fewer bytes than requested to be written. If
1158 * we can't do that (e.g. the residual is already 1 byte),
1159 * then we fail the write with EFBIG.
1160 */
b0d623f7
A
1161 residcount = uio_resid(uio);
1162 if ((rlim_t)(uio->uio_offset + uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
1163 clippedsize = (uio->uio_offset + uio_resid(uio)) - p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
1164 } else if ((rlim_t)uio_resid(uio) > (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset)) {
2d21ac55
A
1165 clippedsize = (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset);
1166 }
1167 if (clippedsize >= residcount) {
1168 psignal(p, SIGXFSZ);
fe8ab488
A
1169 error = EFBIG;
1170 goto error_out;
2d21ac55
A
1171 }
1172 partialwrite = 1;
1173 uio_setresid(uio, residcount-clippedsize);
1174 }
1175 if ((flags & FOF_OFFSET) != 0) {
1176 /* for pwrite, append should be ignored */
1177 ioflag &= ~IO_APPEND;
1178 if (p && (vp->v_type == VREG) &&
1179 ((rlim_t)uio->uio_offset >= p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
91447636 1180 psignal(p, SIGXFSZ);
fe8ab488
A
1181 error = EFBIG;
1182 goto error_out;
9bccf70c 1183 }
2d21ac55 1184 if (p && (vp->v_type == VREG) &&
b0d623f7 1185 ((rlim_t)(uio->uio_offset + uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
2d21ac55 1186 //Debugger("vn_bwrite:overstepping the bounds");
b0d623f7
A
1187 residcount = uio_resid(uio);
1188 clippedsize = (uio->uio_offset + uio_resid(uio)) - p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
2d21ac55
A
1189 partialwrite = 1;
1190 uio_setresid(uio, residcount-clippedsize);
1191 }
1192 }
1193
1194 error = VNOP_WRITE(vp, uio, ioflag, ctx);
91447636 1195
2d21ac55
A
1196 if (partialwrite) {
1197 oldcount = uio_resid(uio);
1198 uio_setresid(uio, oldcount + clippedsize);
1199 }
1c79356b 1200
9bccf70c
A
1201 if ((flags & FOF_OFFSET) == 0) {
1202 if (ioflag & IO_APPEND)
91447636 1203 fp->f_fglob->fg_offset = uio->uio_offset;
9bccf70c 1204 else
91447636 1205 fp->f_fglob->fg_offset += count - uio_resid(uio);
fe8ab488
A
1206 if (offset_locked) {
1207 vn_offset_unlock(fp->f_fglob);
1208 offset_locked = 0;
1209 }
9bccf70c
A
1210 }
1211
1c79356b
A
1212 /*
1213 * Set the credentials on successful writes
1214 */
1215 if ((error == 0) && (vp->v_tag == VT_NFS) && (UBCINFOEXISTS(vp))) {
13fec989
A
1216 /*
1217 * When called from aio subsystem, we only have the proc from
1218 * which to get the credential, at this point, so use that
1219 * instead. This means aio functions are incompatible with
1220 * per-thread credentials (aio operations are proxied). We
1221 * can't easily correct the aio vs. settid race in this case
1222 * anyway, so we disallow it.
1223 */
1224 if ((flags & FOF_PCRED) == 0) {
1225 ubc_setthreadcred(vp, p, current_thread());
1226 } else {
1227 ubc_setcred(vp, p);
1228 }
1c79356b 1229 }
91447636 1230 (void)vnode_put(vp);
1c79356b 1231 return (error);
fe8ab488
A
1232
1233error_out:
1234 if (offset_locked) {
1235 vn_offset_unlock(fp->f_fglob);
1236 }
1237 (void)vnode_put(vp);
1238 return (error);
1c79356b
A
1239}
1240
1241/*
1242 * File table vnode stat routine.
2d21ac55
A
1243 *
1244 * Returns: 0 Success
1245 * EBADF
1246 * ENOMEM
1247 * vnode_getattr:???
1c79356b 1248 */
9bccf70c 1249int
743345f9
A
1250vn_stat_noauth(struct vnode *vp, void *sbptr, kauth_filesec_t *xsec, int isstat64,
1251 vfs_context_t ctx, struct ucred *file_cred)
1c79356b 1252{
91447636 1253 struct vnode_attr va;
1c79356b
A
1254 int error;
1255 u_short mode;
91447636 1256 kauth_filesec_t fsec;
2d21ac55
A
1257 struct stat *sb = (struct stat *)0; /* warning avoidance ; protected by isstat64 */
1258 struct stat64 * sb64 = (struct stat64 *)0; /* warning avoidance ; protected by isstat64 */
1259
1260 if (isstat64 != 0)
1261 sb64 = (struct stat64 *)sbptr;
1262 else
1263 sb = (struct stat *)sbptr;
b0d623f7 1264 memset(&va, 0, sizeof(va));
91447636
A
1265 VATTR_INIT(&va);
1266 VATTR_WANTED(&va, va_fsid);
1267 VATTR_WANTED(&va, va_fileid);
1268 VATTR_WANTED(&va, va_mode);
1269 VATTR_WANTED(&va, va_type);
1270 VATTR_WANTED(&va, va_nlink);
1271 VATTR_WANTED(&va, va_uid);
1272 VATTR_WANTED(&va, va_gid);
1273 VATTR_WANTED(&va, va_rdev);
1274 VATTR_WANTED(&va, va_data_size);
1275 VATTR_WANTED(&va, va_access_time);
1276 VATTR_WANTED(&va, va_modify_time);
1277 VATTR_WANTED(&va, va_change_time);
2d21ac55 1278 VATTR_WANTED(&va, va_create_time);
91447636
A
1279 VATTR_WANTED(&va, va_flags);
1280 VATTR_WANTED(&va, va_gen);
1281 VATTR_WANTED(&va, va_iosize);
1282 /* lower layers will synthesise va_total_alloc from va_data_size if required */
1283 VATTR_WANTED(&va, va_total_alloc);
1284 if (xsec != NULL) {
1285 VATTR_WANTED(&va, va_uuuid);
1286 VATTR_WANTED(&va, va_guuid);
1287 VATTR_WANTED(&va, va_acl);
1288 }
1289 error = vnode_getattr(vp, &va, ctx);
1c79356b 1290 if (error)
91447636 1291 goto out;
743345f9
A
1292#if CONFIG_MACF
1293 /*
1294 * Give MAC polices a chance to reject or filter the attributes
1295 * returned by the filesystem. Note that MAC policies are consulted
1296 * *after* calling the filesystem because filesystems can return more
1297 * attributes than were requested so policies wouldn't be authoritative
1298 * is consulted beforehand. This also gives policies an opportunity
1299 * to change the values of attributes retrieved.
1300 */
1301 error = mac_vnode_check_getattr(ctx, file_cred, vp, &va);
1302 if (error)
1303 goto out;
1304#endif
1c79356b
A
1305 /*
1306 * Copy from vattr table
1307 */
2d21ac55
A
1308 if (isstat64 != 0) {
1309 sb64->st_dev = va.va_fsid;
1310 sb64->st_ino = (ino64_t)va.va_fileid;
1311
1312 } else {
1313 sb->st_dev = va.va_fsid;
1314 sb->st_ino = (ino_t)va.va_fileid;
1315 }
91447636 1316 mode = va.va_mode;
1c79356b
A
1317 switch (vp->v_type) {
1318 case VREG:
1319 mode |= S_IFREG;
1320 break;
1321 case VDIR:
1322 mode |= S_IFDIR;
1323 break;
1324 case VBLK:
1325 mode |= S_IFBLK;
1326 break;
1327 case VCHR:
1328 mode |= S_IFCHR;
1329 break;
1330 case VLNK:
1331 mode |= S_IFLNK;
1332 break;
1333 case VSOCK:
1334 mode |= S_IFSOCK;
1335 break;
1336 case VFIFO:
1337 mode |= S_IFIFO;
1338 break;
1339 default:
91447636
A
1340 error = EBADF;
1341 goto out;
1c79356b 1342 };
2d21ac55
A
1343 if (isstat64 != 0) {
1344 sb64->st_mode = mode;
1345 sb64->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? (u_int16_t)va.va_nlink : 1;
1346 sb64->st_uid = va.va_uid;
1347 sb64->st_gid = va.va_gid;
1348 sb64->st_rdev = va.va_rdev;
1349 sb64->st_size = va.va_data_size;
1350 sb64->st_atimespec = va.va_access_time;
1351 sb64->st_mtimespec = va.va_modify_time;
1352 sb64->st_ctimespec = va.va_change_time;
3e170ce0
A
1353 if (VATTR_IS_SUPPORTED(&va, va_create_time)) {
1354 sb64->st_birthtimespec = va.va_create_time;
1355 } else {
1356 sb64->st_birthtimespec.tv_sec = sb64->st_birthtimespec.tv_nsec = 0;
1357 }
2d21ac55
A
1358 sb64->st_blksize = va.va_iosize;
1359 sb64->st_flags = va.va_flags;
1360 sb64->st_blocks = roundup(va.va_total_alloc, 512) / 512;
1361 } else {
1362 sb->st_mode = mode;
1363 sb->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? (u_int16_t)va.va_nlink : 1;
1364 sb->st_uid = va.va_uid;
1365 sb->st_gid = va.va_gid;
1366 sb->st_rdev = va.va_rdev;
1367 sb->st_size = va.va_data_size;
1368 sb->st_atimespec = va.va_access_time;
1369 sb->st_mtimespec = va.va_modify_time;
1370 sb->st_ctimespec = va.va_change_time;
1371 sb->st_blksize = va.va_iosize;
1372 sb->st_flags = va.va_flags;
1373 sb->st_blocks = roundup(va.va_total_alloc, 512) / 512;
1374 }
91447636 1375
6d2010ae 1376 /* if we're interested in extended security data and we got an ACL */
91447636
A
1377 if (xsec != NULL) {
1378 if (!VATTR_IS_SUPPORTED(&va, va_acl) &&
1379 !VATTR_IS_SUPPORTED(&va, va_uuuid) &&
1380 !VATTR_IS_SUPPORTED(&va, va_guuid)) {
1381 *xsec = KAUTH_FILESEC_NONE;
1382 } else {
1383
1384 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
1385 fsec = kauth_filesec_alloc(va.va_acl->acl_entrycount);
1386 } else {
1387 fsec = kauth_filesec_alloc(0);
1388 }
1389 if (fsec == NULL) {
1390 error = ENOMEM;
1391 goto out;
1392 }
1393 fsec->fsec_magic = KAUTH_FILESEC_MAGIC;
1394 if (VATTR_IS_SUPPORTED(&va, va_uuuid)) {
1395 fsec->fsec_owner = va.va_uuuid;
1396 } else {
1397 fsec->fsec_owner = kauth_null_guid;
1398 }
1399 if (VATTR_IS_SUPPORTED(&va, va_guuid)) {
1400 fsec->fsec_group = va.va_guuid;
1401 } else {
1402 fsec->fsec_group = kauth_null_guid;
1403 }
1404 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
1405 bcopy(va.va_acl, &(fsec->fsec_acl), KAUTH_ACL_COPYSIZE(va.va_acl));
1406 } else {
1407 fsec->fsec_acl.acl_entrycount = KAUTH_FILESEC_NOACL;
1408 }
1409 *xsec = fsec;
1410 }
1411 }
1412
1c79356b 1413 /* Do not give the generation number out to unpriviledged users */
2d21ac55
A
1414 if (va.va_gen && !vfs_context_issuser(ctx)) {
1415 if (isstat64 != 0)
1416 sb64->st_gen = 0;
1417 else
1418 sb->st_gen = 0;
1419 } else {
1420 if (isstat64 != 0)
1421 sb64->st_gen = va.va_gen;
1422 else
1423 sb->st_gen = va.va_gen;
1424 }
91447636
A
1425
1426 error = 0;
1427out:
1428 if (VATTR_IS_SUPPORTED(&va, va_acl) && va.va_acl != NULL)
1429 kauth_acl_free(va.va_acl);
1430 return (error);
1431}
1432
1433int
2d21ac55 1434vn_stat(struct vnode *vp, void *sb, kauth_filesec_t *xsec, int isstat64, vfs_context_t ctx)
91447636
A
1435{
1436 int error;
1437
2d21ac55
A
1438#if CONFIG_MACF
1439 error = mac_vnode_check_stat(ctx, NOCRED, vp);
1440 if (error)
1441 return (error);
1442#endif
1443
91447636
A
1444 /* authorize */
1445 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY, ctx)) != 0)
1446 return(error);
1447
1448 /* actual stat */
743345f9 1449 return(vn_stat_noauth(vp, sb, xsec, isstat64, ctx, NOCRED));
1c79356b
A
1450}
1451
91447636 1452
1c79356b
A
1453/*
1454 * File table vnode ioctl routine.
1455 */
9bccf70c 1456static int
2d21ac55 1457vn_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx)
1c79356b 1458{
2d21ac55 1459 struct vnode *vp = ((struct vnode *)fp->f_fglob->fg_data);
91447636 1460 off_t file_size;
1c79356b 1461 int error;
fa4905b1 1462 struct vnode *ttyvp;
2d21ac55 1463 struct session * sessp;
9bccf70c 1464
91447636
A
1465 if ( (error = vnode_getwithref(vp)) ) {
1466 return(error);
1467 }
91447636 1468
2d21ac55
A
1469#if CONFIG_MACF
1470 error = mac_vnode_check_ioctl(ctx, vp, com);
1471 if (error)
1472 goto out;
1473#endif
1c79356b 1474
2d21ac55 1475 switch (vp->v_type) {
1c79356b
A
1476 case VREG:
1477 case VDIR:
1478 if (com == FIONREAD) {
2d21ac55 1479 if ((error = vnode_size(vp, &file_size, ctx)) != 0)
91447636
A
1480 goto out;
1481 *(int *)data = file_size - fp->f_fglob->fg_offset;
1482 goto out;
1483 }
1484 if (com == FIONBIO || com == FIOASYNC) { /* XXX */
1485 goto out;
1c79356b 1486 }
1c79356b
A
1487 /* fall into ... */
1488
1489 default:
91447636
A
1490 error = ENOTTY;
1491 goto out;
1c79356b
A
1492
1493 case VFIFO:
1494 case VCHR:
1495 case VBLK:
9bccf70c 1496
91447636
A
1497 /* Should not be able to set block size from user space */
1498 if (com == DKIOCSETBLOCKSIZE) {
1499 error = EPERM;
1500 goto out;
1501 }
1502
1503 if (com == FIODTYPE) {
1504 if (vp->v_type == VBLK) {
1505 if (major(vp->v_rdev) >= nblkdev) {
1506 error = ENXIO;
1507 goto out;
1508 }
39236c6e 1509 *(int *)data = bdevsw[major(vp->v_rdev)].d_type;
91447636
A
1510
1511 } else if (vp->v_type == VCHR) {
1512 if (major(vp->v_rdev) >= nchrdev) {
1513 error = ENXIO;
1514 goto out;
1515 }
39236c6e 1516 *(int *)data = cdevsw[major(vp->v_rdev)].d_type;
91447636
A
1517 } else {
1518 error = ENOTTY;
1519 goto out;
1520 }
1521 goto out;
1522 }
2d21ac55 1523 error = VNOP_IOCTL(vp, com, data, fp->f_fglob->fg_flag, ctx);
91447636
A
1524
1525 if (error == 0 && com == TIOCSCTTY) {
2d21ac55
A
1526 sessp = proc_session(vfs_context_proc(ctx));
1527
1528 session_lock(sessp);
1529 ttyvp = sessp->s_ttyvp;
1530 sessp->s_ttyvp = vp;
1531 sessp->s_ttyvid = vnode_vid(vp);
1532 session_unlock(sessp);
1533 session_rele(sessp);
91447636 1534 }
1c79356b 1535 }
91447636
A
1536out:
1537 (void)vnode_put(vp);
1538 return(error);
1c79356b
A
1539}
1540
1541/*
1542 * File table vnode select routine.
1543 */
9bccf70c 1544static int
2d21ac55 1545vn_select(struct fileproc *fp, int which, void *wql, __unused vfs_context_t ctx)
1c79356b 1546{
91447636
A
1547 int error;
1548 struct vnode * vp = (struct vnode *)fp->f_fglob->fg_data;
1549 struct vfs_context context;
1550
1551 if ( (error = vnode_getwithref(vp)) == 0 ) {
2d21ac55 1552 context.vc_thread = current_thread();
91447636
A
1553 context.vc_ucred = fp->f_fglob->fg_cred;
1554
2d21ac55
A
1555#if CONFIG_MACF
1556 /*
1557 * XXX We should use a per thread credential here; minimally,
1558 * XXX the process credential should have a persistent
1559 * XXX reference on it before being passed in here.
1560 */
1561 error = mac_vnode_check_select(ctx, vp, which);
1562 if (error == 0)
1563#endif
1564 error = VNOP_SELECT(vp, which, fp->f_fglob->fg_flag, wql, ctx);
1c79356b 1565
91447636
A
1566 (void)vnode_put(vp);
1567 }
1568 return(error);
1569
1c79356b
A
1570}
1571
1c79356b
A
1572/*
1573 * File table vnode close routine.
1574 */
9bccf70c 1575static int
2d21ac55 1576vn_closefile(struct fileglob *fg, vfs_context_t ctx)
1c79356b 1577{
3e170ce0 1578 struct vnode *vp = fg->fg_data;
91447636
A
1579 int error;
1580
1581 if ( (error = vnode_getwithref(vp)) == 0 ) {
3e170ce0
A
1582 if (FILEGLOB_DTYPE(fg) == DTYPE_VNODE &&
1583 ((fg->fg_flag & FHASLOCK) != 0 ||
1584 (fg->fg_lflags & FG_HAS_OFDLOCK) != 0)) {
1585 struct flock lf = {
1586 .l_whence = SEEK_SET,
1587 .l_start = 0,
1588 .l_len = 0,
1589 .l_type = F_UNLCK
1590 };
1591
1592 if ((fg->fg_flag & FHASLOCK) != 0)
1593 (void) VNOP_ADVLOCK(vp, (caddr_t)fg,
1594 F_UNLCK, &lf, F_FLOCK, ctx, NULL);
1595
1596 if ((fg->fg_lflags & FG_HAS_OFDLOCK) != 0)
1597 (void) VNOP_ADVLOCK(vp, (caddr_t)fg,
1598 F_UNLCK, &lf, F_OFD_LOCK, ctx, NULL);
2d21ac55
A
1599 }
1600 error = vn_close(vp, fg->fg_flag, ctx);
3e170ce0 1601 (void) vnode_put(vp);
91447636 1602 }
3e170ce0 1603 return (error);
91447636
A
1604}
1605
2d21ac55
A
1606/*
1607 * Returns: 0 Success
1608 * VNOP_PATHCONF:???
1609 */
91447636 1610int
b0d623f7 1611vn_pathconf(vnode_t vp, int name, int32_t *retval, vfs_context_t ctx)
91447636
A
1612{
1613 int error = 0;
6d2010ae 1614 struct vfs_attr vfa;
91447636
A
1615
1616 switch(name) {
1617 case _PC_EXTENDED_SECURITY_NP:
2d21ac55 1618 *retval = vfs_extendedsecurity(vnode_mount(vp)) ? 1 : 0;
91447636
A
1619 break;
1620 case _PC_AUTH_OPAQUE_NP:
1621 *retval = vfs_authopaque(vnode_mount(vp));
1622 break;
2d21ac55
A
1623 case _PC_2_SYMLINKS:
1624 *retval = 1; /* XXX NOTSUP on MSDOS, etc. */
1625 break;
1626 case _PC_ALLOC_SIZE_MIN:
1627 *retval = 1; /* XXX lie: 1 byte */
1628 break;
1629 case _PC_ASYNC_IO: /* unistd.h: _POSIX_ASYNCHRONUS_IO */
1630 *retval = 1; /* [AIO] option is supported */
1631 break;
1632 case _PC_PRIO_IO: /* unistd.h: _POSIX_PRIORITIZED_IO */
1633 *retval = 0; /* [PIO] option is not supported */
1634 break;
1635 case _PC_REC_INCR_XFER_SIZE:
1636 *retval = 4096; /* XXX go from MIN to MAX 4K at a time */
1637 break;
1638 case _PC_REC_MIN_XFER_SIZE:
1639 *retval = 4096; /* XXX recommend 4K minimum reads/writes */
1640 break;
1641 case _PC_REC_MAX_XFER_SIZE:
1642 *retval = 65536; /* XXX recommend 64K maximum reads/writes */
1643 break;
1644 case _PC_REC_XFER_ALIGN:
1645 *retval = 4096; /* XXX recommend page aligned buffers */
1646 break;
1647 case _PC_SYMLINK_MAX:
1648 *retval = 255; /* Minimum acceptable POSIX value */
1649 break;
1650 case _PC_SYNC_IO: /* unistd.h: _POSIX_SYNCHRONIZED_IO */
1651 *retval = 0; /* [SIO] option is not supported */
1652 break;
6d2010ae
A
1653 case _PC_XATTR_SIZE_BITS:
1654 /* The number of bits used to store maximum extended
1655 * attribute size in bytes. For example, if the maximum
1656 * attribute size supported by a file system is 128K, the
1657 * value returned will be 18. However a value 18 can mean
1658 * that the maximum attribute size can be anywhere from
1659 * (256KB - 1) to 128KB. As a special case, the resource
1660 * fork can have much larger size, and some file system
1661 * specific extended attributes can have smaller and preset
1662 * size; for example, Finder Info is always 32 bytes.
1663 */
1664 memset(&vfa, 0, sizeof(vfa));
1665 VFSATTR_INIT(&vfa);
1666 VFSATTR_WANTED(&vfa, f_capabilities);
1667 if (vfs_getattr(vnode_mount(vp), &vfa, ctx) == 0 &&
1668 (VFSATTR_IS_SUPPORTED(&vfa, f_capabilities)) &&
1669 (vfa.f_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR) &&
1670 (vfa.f_capabilities.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_EXTENDED_ATTR)) {
1671 /* Supports native extended attributes */
1672 error = VNOP_PATHCONF(vp, name, retval, ctx);
1673 } else {
1674 /* Number of bits used to represent the maximum size of
1675 * extended attribute stored in an Apple Double file.
1676 */
1677 *retval = AD_XATTR_SIZE_BITS;
1678 }
1679 break;
91447636
A
1680 default:
1681 error = VNOP_PATHCONF(vp, name, retval, ctx);
1682 break;
1683 }
1c79356b 1684
91447636 1685 return (error);
1c79356b 1686}
55e303ae
A
1687
1688static int
2d21ac55 1689vn_kqfilt_add(struct fileproc *fp, struct knote *kn, vfs_context_t ctx)
55e303ae 1690{
b0d623f7 1691 struct vnode *vp;
39037602
A
1692 int error = 0;
1693 int result = 0;
55e303ae 1694
b0d623f7
A
1695 vp = (struct vnode *)fp->f_fglob->fg_data;
1696
1697 /*
1698 * Don't attach a knote to a dead vnode.
1699 */
1700 if ((error = vget_internal(vp, 0, VNODE_NODEAD)) == 0) {
1701 switch (kn->kn_filter) {
1702 case EVFILT_READ:
1703 case EVFILT_WRITE:
1704 if (vnode_isfifo(vp)) {
1705 /* We'll only watch FIFOs that use our fifofs */
1706 if (!(vp->v_fifoinfo && vp->v_fifoinfo->fi_readsock)) {
1707 error = ENOTSUP;
1708 }
1709
1710 } else if (!vnode_isreg(vp)) {
39037602
A
1711 if (vnode_ischr(vp)) {
1712 result = spec_kqfilter(vp, kn);
1713 if ((kn->kn_flags & EV_ERROR) == 0) {
1714 /* claimed by a special device */
1715 vnode_put(vp);
1716 return result;
1717 }
b0d623f7 1718 }
b0d623f7
A
1719 error = EINVAL;
1720 }
1721 break;
1722 case EVFILT_VNODE:
1723 break;
1724 default:
1725 error = EINVAL;
1726 }
1727
39037602 1728 if (error == 0) {
2d21ac55
A
1729
1730#if CONFIG_MACF
39037602
A
1731 error = mac_vnode_check_kqfilter(ctx, fp->f_fglob->fg_cred, kn, vp);
1732 if (error) {
1733 vnode_put(vp);
1734 goto out;
1735 }
2d21ac55 1736#endif
91447636 1737
39037602
A
1738 kn->kn_hook = (void*)vp;
1739 kn->kn_hookid = vnode_vid(vp);
1740 kn->kn_filtid = EVFILTID_VN;
91447636 1741
39037602
A
1742 vnode_lock(vp);
1743 KNOTE_ATTACH(&vp->v_knotes, kn);
1744 result = filt_vnode_common(kn, vp, 0);
1745 vnode_unlock(vp);
b0d623f7 1746
39037602
A
1747 /*
1748 * Ask the filesystem to provide remove notifications,
1749 * but ignore failure
1750 */
1751 VNOP_MONITOR(vp, 0, VNODE_MONITOR_BEGIN, (void*) kn, ctx);
1752 }
b0d623f7
A
1753
1754 vnode_put(vp);
91447636 1755 }
b0d623f7 1756
39037602
A
1757 out:
1758 if (error) {
1759 kn->kn_flags = EV_ERROR;
1760 kn->kn_data = error;
1761 }
1762
1763 return result;
55e303ae
A
1764}
1765
b0d623f7
A
1766static void
1767filt_vndetach(struct knote *kn)
55e303ae 1768{
b0d623f7
A
1769 vfs_context_t ctx = vfs_context_current();
1770 struct vnode *vp;
1771 vp = (struct vnode *)kn->kn_hook;
1772 if (vnode_getwithvid(vp, kn->kn_hookid))
1773 return;
1774
1775 vnode_lock(vp);
1776 KNOTE_DETACH(&vp->v_knotes, kn);
1777 vnode_unlock(vp);
55e303ae 1778
b0d623f7
A
1779 /*
1780 * Tell a (generally networked) filesystem that we're no longer watching
1781 * If the FS wants to track contexts, it should still be using the one from
1782 * the VNODE_MONITOR_BEGIN.
1783 */
1784 VNOP_MONITOR(vp, 0, VNODE_MONITOR_END, (void*)kn, ctx);
1785 vnode_put(vp);
1786}
91447636 1787
91447636 1788
b0d623f7
A
1789/*
1790 * Used for EVFILT_READ
1791 *
1792 * Takes only VFIFO or VREG. vnode is locked. We handle the "poll" case
1793 * differently than the regular case for VREG files. If not in poll(),
1794 * then we need to know current fileproc offset for VREG.
1795 */
1796static intptr_t
1797vnode_readable_data_count(vnode_t vp, off_t current_offset, int ispoll)
1798{
1799 if (vnode_isfifo(vp)) {
39236c6e 1800#if FIFO
b0d623f7
A
1801 int cnt;
1802 int err = fifo_charcount(vp, &cnt);
1803 if (err == 0) {
1804 return (intptr_t)cnt;
39236c6e
A
1805 } else
1806#endif
1807 {
b0d623f7
A
1808 return (intptr_t)0;
1809 }
1810 } else if (vnode_isreg(vp)) {
1811 if (ispoll) {
1812 return (intptr_t)1;
1813 }
1814
1815 off_t amount;
1816 amount = vp->v_un.vu_ubcinfo->ui_size - current_offset;
1817 if (amount > (off_t)INTPTR_MAX) {
1818 return INTPTR_MAX;
1819 } else if (amount < (off_t)INTPTR_MIN) {
1820 return INTPTR_MIN;
1821 } else {
1822 return (intptr_t)amount;
1823 }
1824 } else {
1825 panic("Should never have an EVFILT_READ except for reg or fifo.");
1826 return 0;
91447636 1827 }
55e303ae 1828}
b0d623f7
A
1829
1830/*
1831 * Used for EVFILT_WRITE.
1832 *
1833 * For regular vnodes, we can always write (1). For named pipes,
1834 * see how much space there is in the buffer. Nothing else is covered.
1835 */
1836static intptr_t
1837vnode_writable_space_count(vnode_t vp)
1838{
1839 if (vnode_isfifo(vp)) {
39236c6e 1840#if FIFO
b0d623f7
A
1841 long spc;
1842 int err = fifo_freespace(vp, &spc);
1843 if (err == 0) {
1844 return (intptr_t)spc;
39236c6e
A
1845 } else
1846#endif
1847 {
b0d623f7
A
1848 return (intptr_t)0;
1849 }
1850 } else if (vnode_isreg(vp)) {
1851 return (intptr_t)1;
1852 } else {
1853 panic("Should never have an EVFILT_READ except for reg or fifo.");
1854 return 0;
1855 }
1856}
1857
1858/*
1859 * Determine whether this knote should be active
1860 *
1861 * This is kind of subtle.
1862 * --First, notice if the vnode has been revoked: in so, override hint
1863 * --EVFILT_READ knotes are checked no matter what the hint is
1864 * --Other knotes activate based on hint.
1865 * --If hint is revoke, set special flags and activate
1866 */
1867static int
39037602 1868filt_vnode_common(struct knote *kn, vnode_t vp, long hint)
b0d623f7 1869{
b0d623f7
A
1870 int activate = 0;
1871
39037602 1872 lck_mtx_assert(&vp->v_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7 1873
6d2010ae 1874 /* Special handling for vnodes that are in recycle or already gone */
b0d623f7
A
1875 if (NOTE_REVOKE == hint) {
1876 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1877 activate = 1;
1878
1879 if ((kn->kn_filter == EVFILT_VNODE) && (kn->kn_sfflags & NOTE_REVOKE)) {
1880 kn->kn_fflags |= NOTE_REVOKE;
1881 }
1882 } else {
1883 switch(kn->kn_filter) {
1884 case EVFILT_READ:
1885 kn->kn_data = vnode_readable_data_count(vp, kn->kn_fp->f_fglob->fg_offset, (kn->kn_flags & EV_POLL));
1886
1887 if (kn->kn_data != 0) {
1888 activate = 1;
1889 }
1890 break;
1891 case EVFILT_WRITE:
1892 kn->kn_data = vnode_writable_space_count(vp);
1893
1894 if (kn->kn_data != 0) {
1895 activate = 1;
1896 }
1897 break;
1898 case EVFILT_VNODE:
1899 /* Check events this note matches against the hint */
1900 if (kn->kn_sfflags & hint) {
1901 kn->kn_fflags |= hint; /* Set which event occurred */
1902 }
1903 if (kn->kn_fflags != 0) {
1904 activate = 1;
1905 }
1906 break;
1907 default:
1908 panic("Invalid knote filter on a vnode!\n");
1909 }
1910 }
39037602
A
1911 return (activate);
1912}
b0d623f7 1913
39037602
A
1914static int
1915filt_vnode(struct knote *kn, long hint)
1916{
1917 vnode_t vp = (struct vnode *)kn->kn_hook;
1918
1919 return filt_vnode_common(kn, vp, hint);
1920}
1921
1922static int
1923filt_vntouch(struct knote *kn, struct kevent_internal_s *kev)
1924{
1925 vnode_t vp = (struct vnode *)kn->kn_hook;
1926 int activate;
1927 int hint = 0;
1928
1929 vnode_lock(vp);
1930 if (vnode_getiocount(vp, kn->kn_hookid, VNODE_NODEAD | VNODE_WITHID) != 0) {
1931 /* is recycled */
1932 hint = NOTE_REVOKE;
1933 }
1934
1935 /* accept new input fflags mask */
1936 kn->kn_sfflags = kev->fflags;
1937 if ((kn->kn_status & KN_UDATA_SPECIFIC) == 0)
1938 kn->kn_udata = kev->udata;
1939
1940 activate = filt_vnode_common(kn, vp, hint);
1941
1942 if (hint == 0)
1943 vnode_put_locked(vp);
1944 vnode_unlock(vp);
1945
1946 return activate;
1947}
1948
1949static int
1950filt_vnprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev)
1951{
1952#pragma unused(data)
1953 vnode_t vp = (struct vnode *)kn->kn_hook;
1954 int activate;
1955 int hint = 0;
1956
1957 vnode_lock(vp);
1958 if (vnode_getiocount(vp, kn->kn_hookid, VNODE_NODEAD | VNODE_WITHID) != 0) {
1959 /* Is recycled */
1960 hint = NOTE_REVOKE;
1961 }
1962 activate = filt_vnode_common(kn, vp, hint);
1963 if (activate) {
1964 *kev = kn->kn_kevent;
1965 if (kn->kn_flags & EV_CLEAR) {
1966 kn->kn_data = 0;
1967 kn->kn_fflags = 0;
6d2010ae 1968 }
6d2010ae
A
1969 }
1970
39037602
A
1971 /* Definitely need to unlock, may need to put */
1972 if (hint == 0)
1973 vnode_put_locked(vp);
1974 vnode_unlock(vp);
1975
1976 return activate;
b0d623f7 1977}
39037602 1978