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