]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/vfs_vnops.c
xnu-792.6.22.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_vnops.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
e5568f75
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23/*
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)vfs_vnops.c 8.14 (Berkeley) 6/15/95
61 *
1c79356b
A
62 */
63
64#include <sys/param.h>
91447636 65#include <sys/types.h>
1c79356b
A
66#include <sys/systm.h>
67#include <sys/kernel.h>
91447636 68#include <sys/file_internal.h>
1c79356b 69#include <sys/stat.h>
91447636
A
70#include <sys/proc_internal.h>
71#include <sys/kauth.h>
72#include <sys/mount_internal.h>
1c79356b 73#include <sys/namei.h>
91447636 74#include <sys/vnode_internal.h>
1c79356b
A
75#include <sys/ioctl.h>
76#include <sys/tty.h>
77#include <sys/ubc.h>
9bccf70c
A
78#include <sys/conf.h>
79#include <sys/disk.h>
91447636
A
80#include <sys/fsevents.h>
81#include <sys/kdebug.h>
82#include <sys/xattr.h>
83#include <sys/ubc_internal.h>
84#include <sys/uio_internal.h>
9bccf70c
A
85
86#include <vm/vm_kern.h>
91447636 87#include <vm/vm_map.h>
9bccf70c
A
88
89#include <miscfs/specfs/specdev.h>
90
91447636
A
91
92
93static int vn_closefile(struct fileglob *fp, struct proc *p);
94static int vn_ioctl(struct fileproc *fp, u_long com, caddr_t data, struct proc *p);
95static int vn_read(struct fileproc *fp, struct uio *uio,
96 kauth_cred_t cred, int flags, struct proc *p);
97static int vn_write(struct fileproc *fp, struct uio *uio,
98 kauth_cred_t cred, int flags, struct proc *p);
99static int vn_select( struct fileproc *fp, int which, void * wql, struct proc *p);
100static int vn_kqfilt_add(struct fileproc *fp, struct knote *kn, struct proc *p);
101#if 0
102static int vn_kqfilt_remove(struct vnode *vp, uintptr_t ident, struct proc *p);
103#endif
1c79356b
A
104
105struct fileops vnops =
91447636 106 { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile, vn_kqfilt_add, 0 };
1c79356b
A
107
108/*
109 * Common code for vnode open operations.
91447636
A
110 * Check permissions, and call the VNOP_OPEN or VNOP_CREATE routine.
111 *
112 * XXX the profusion of interfaces here is probably a bad thing.
1c79356b 113 */
9bccf70c 114int
91447636 115vn_open(struct nameidata *ndp, int fmode, int cmode)
55e303ae 116{
91447636 117 return(vn_open_modflags(ndp, &fmode, cmode));
55e303ae
A
118}
119
91447636
A
120int
121vn_open_modflags(struct nameidata *ndp, int *fmodep, int cmode)
1c79356b 122{
91447636
A
123 struct vnode_attr va;
124
125 VATTR_INIT(&va);
126 VATTR_SET(&va, va_mode, cmode);
127
128 return(vn_open_auth(ndp, fmodep, &va));
129}
130
131int
132vn_open_auth(struct nameidata *ndp, int *fmodep, struct vnode_attr *vap)
133{
134 struct vnode *vp;
135 struct vnode *dvp;
136 vfs_context_t ctx = ndp->ni_cnd.cn_context;
1c79356b 137 int error;
91447636
A
138 int fmode;
139 kauth_action_t action;
1c79356b 140
91447636
A
141again:
142 vp = NULL;
143 dvp = NULL;
144 fmode = *fmodep;
1c79356b
A
145 if (fmode & O_CREAT) {
146 ndp->ni_cnd.cn_nameiop = CREATE;
55e303ae 147 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | AUDITVNPATH1;
91447636 148
1c79356b
A
149 if ((fmode & O_EXCL) == 0)
150 ndp->ni_cnd.cn_flags |= FOLLOW;
91447636
A
151 if ( (error = namei(ndp)) )
152 goto out;
153 dvp = ndp->ni_dvp;
154 vp = ndp->ni_vp;
155
156 /* not found, create */
157 if (vp == NULL) {
158 /* must have attributes for a new file */
159 if (vap == NULL) {
160 error = EINVAL;
161 goto badcreate;
162 }
163
164 /* authorize before creating */
165 if ((error = vnode_authorize(dvp, NULL, KAUTH_VNODE_ADD_FILE, ctx)) != 0)
166 goto badcreate;
167
168 VATTR_SET(vap, va_type, VREG);
1c79356b
A
169 if (fmode & O_EXCL)
170 vap->va_vaflags |= VA_EXCLUSIVE;
91447636
A
171
172 if ((error = vn_create(dvp, &ndp->ni_vp, &ndp->ni_cnd, vap, 0, ctx)) != 0)
173 goto badcreate;
55e303ae 174
91447636
A
175 vp = ndp->ni_vp;
176
177 if (vp) {
178 int update_flags = 0;
179
180 // Make sure the name & parent pointers are hooked up
181 if (vp->v_name == NULL)
182 update_flags |= VNODE_UPDATE_NAME;
183 if (vp->v_parent == NULLVP)
184 update_flags |= VNODE_UPDATE_PARENT;
185
186 if (update_flags)
187 vnode_update_identity(vp, dvp, ndp->ni_cnd.cn_nameptr, ndp->ni_cnd.cn_namelen, ndp->ni_cnd.cn_hash, update_flags);
188
189 if (need_fsevent(FSE_CREATE_FILE, vp)) {
190 add_fsevent(FSE_CREATE_FILE, ctx,
191 FSE_ARG_VNODE, vp,
192 FSE_ARG_DONE);
193 }
194 }
195 /*
196 * nameidone has to happen before we vnode_put(dvp)
197 * and clear the ni_dvp field, since it may need
198 * to release the fs_nodelock on the dvp
199 */
200badcreate:
201 nameidone(ndp);
202 ndp->ni_dvp = NULL;
203 vnode_put(dvp);
204
205 if (error) {
206 /*
207 * Check for a creation race.
208 */
209 if ((error == EEXIST) && !(fmode & O_EXCL)) {
210 goto again;
211 }
212 goto bad;
55e303ae 213 }
91447636 214 fmode &= ~O_TRUNC;
1c79356b 215 } else {
91447636 216 nameidone(ndp);
1c79356b 217 ndp->ni_dvp = NULL;
91447636
A
218 vnode_put(dvp);
219
1c79356b
A
220 if (fmode & O_EXCL) {
221 error = EEXIST;
222 goto bad;
223 }
224 fmode &= ~O_CREAT;
225 }
226 } else {
227 ndp->ni_cnd.cn_nameiop = LOOKUP;
55e303ae 228 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | AUDITVNPATH1;
91447636
A
229 if ( (error = namei(ndp)) )
230 goto out;
1c79356b 231 vp = ndp->ni_vp;
91447636
A
232 nameidone(ndp);
233 ndp->ni_dvp = NULL;
1c79356b 234 }
91447636
A
235 if (vp->v_type == VSOCK && vp->v_tag != VT_FDESC) {
236 error = EOPNOTSUPP; /* Operation not supported on socket */
1c79356b
A
237 goto bad;
238 }
9bccf70c
A
239
240#if DIAGNOSTIC
241 if (UBCINFOMISSING(vp))
242 panic("vn_open: ubc_info_init");
243#endif /* DIAGNOSTIC */
244
91447636 245 /* authorize open of an existing file */
1c79356b 246 if ((fmode & O_CREAT) == 0) {
91447636
A
247
248 /* disallow write operations on directories */
249 if (vnode_isdir(vp) && (fmode & (FWRITE | O_TRUNC))) {
250 error = EISDIR;
251 goto bad;
1c79356b 252 }
91447636
A
253
254 /* compute action to be authorized */
255 action = 0;
256 if (fmode & FREAD)
257 action |= KAUTH_VNODE_READ_DATA;
258 if (fmode & (FWRITE | O_TRUNC))
259 action |= KAUTH_VNODE_WRITE_DATA;
260 if ((error = vnode_authorize(vp, NULL, action, ctx)) != 0)
261 goto bad;
262
1c79356b 263 }
0b4e3aa0 264
91447636 265 if ( (error = VNOP_OPEN(vp, fmode, ctx)) ) {
0b4e3aa0
A
266 goto bad;
267 }
91447636
A
268 if ( (error = vnode_ref_ext(vp, fmode)) )
269 goto bad;
270
271 /* call out to allow 3rd party notification of open.
272 * Ignore result of kauth_authorize_fileop call.
273 */
274 kauth_authorize_fileop(vfs_context_ucred(ctx), KAUTH_FILEOP_OPEN,
275 (uintptr_t)vp, 0);
0b4e3aa0 276
55e303ae 277 *fmodep = fmode;
1c79356b
A
278 return (0);
279bad:
55e303ae 280 ndp->ni_vp = NULL;
91447636
A
281 if (vp) {
282 vnode_put(vp);
283 /*
284 * Check for a race against unlink. We had a vnode
285 * but according to vnode_authorize or VNOP_OPEN it
286 * no longer exists.
287 */
288 if ((error == ENOENT) && (*fmodep & O_CREAT)) {
289 goto again;
290 }
291 }
292out:
1c79356b
A
293 return (error);
294}
295
296/*
91447636
A
297 * Authorize an action against a vnode. This has been the canonical way to
298 * ensure that the credential/process/etc. referenced by a vfs_context
299 * is granted the rights called out in 'mode' against the vnode 'vp'.
300 *
301 * Unfortunately, the use of VREAD/VWRITE/VEXEC makes it very difficult
302 * to add support for more rights. As such, this interface will be deprecated
303 * and callers will use vnode_authorize instead.
1c79356b 304 */
91447636 305#warning vn_access is deprecated
9bccf70c 306int
91447636 307vn_access(vnode_t vp, int mode, vfs_context_t context)
1c79356b 308{
91447636
A
309 kauth_action_t action;
310
311 action = 0;
312 if (mode & VREAD)
313 action |= KAUTH_VNODE_READ_DATA;
314 if (mode & VWRITE)
315 action |= KAUTH_VNODE_WRITE_DATA;
316 if (mode & VEXEC)
317 action |= KAUTH_VNODE_EXECUTE;
318
319 return(vnode_authorize(vp, NULL, action, context));
1c79356b
A
320}
321
322/*
323 * Vnode close call
324 */
9bccf70c 325int
91447636 326vn_close(struct vnode *vp, int flags, kauth_cred_t cred, struct proc *p)
1c79356b 327{
91447636 328 struct vfs_context context;
1c79356b 329 int error;
1c79356b 330
91447636
A
331 context.vc_proc = p;
332 context.vc_ucred = cred;
55e303ae 333
91447636
A
334 if (flags & FWASWRITTEN) {
335 if (need_fsevent(FSE_CONTENT_MODIFIED, vp)) {
336 add_fsevent(FSE_CONTENT_MODIFIED, &context,
337 FSE_ARG_VNODE, vp,
338 FSE_ARG_DONE);
55e303ae
A
339 }
340 }
341
91447636
A
342 error = VNOP_CLOSE(vp, flags, &context);
343 (void)vnode_rele_ext(vp, flags, 0);
344
1c79356b
A
345 return (error);
346}
347
91447636
A
348static int
349vn_read_swapfile(
350 struct vnode *vp,
351 uio_t uio)
352{
353 static char *swap_read_zero_page = NULL;
354 int error;
355 off_t swap_count, this_count;
356 off_t file_end, read_end;
357 off_t prev_resid;
358
359 /*
360 * Reading from a swap file will get you all zeroes.
361 */
362 error = 0;
363 swap_count = uio_resid(uio);
364
365 file_end = ubc_getsize(vp);
366 read_end = uio->uio_offset + uio_resid(uio);
367 if (uio->uio_offset >= file_end) {
368 /* uio starts after end of file: nothing to read */
369 swap_count = 0;
370 } else if (read_end > file_end) {
371 /* uio extends beyond end of file: stop before that */
372 swap_count -= (read_end - file_end);
373 }
374
375 while (swap_count > 0) {
376 if (swap_read_zero_page == NULL) {
377 char *my_zero_page;
378 int funnel_state;
379
380 /*
381 * Take kernel funnel so that only one thread
382 * sets up "swap_read_zero_page".
383 */
384 funnel_state = thread_funnel_set(kernel_flock, TRUE);
385
386 if (swap_read_zero_page == NULL) {
387 MALLOC(my_zero_page, char *, PAGE_SIZE,
388 M_TEMP, M_WAITOK);
389 memset(my_zero_page, '?', PAGE_SIZE);
390 /*
391 * Adding a newline character here
392 * and there prevents "less(1)", for
393 * example, from getting too confused
394 * about a file with one really really
395 * long line.
396 */
397 my_zero_page[PAGE_SIZE-1] = '\n';
398 if (swap_read_zero_page == NULL) {
399 swap_read_zero_page = my_zero_page;
400 } else {
401 FREE(my_zero_page, M_TEMP);
402 }
403 } else {
404 /*
405 * Someone else raced us here and won;
406 * just use their page.
407 */
408 }
409 thread_funnel_set(kernel_flock, funnel_state);
410 }
411
412 this_count = swap_count;
413 if (this_count > PAGE_SIZE) {
414 this_count = PAGE_SIZE;
415 }
416
417 prev_resid = uio_resid(uio);
418 error = uiomove((caddr_t) swap_read_zero_page,
419 this_count,
420 uio);
421 if (error) {
422 break;
423 }
424 swap_count -= (prev_resid - uio_resid(uio));
425 }
426
427 return error;
428}
1c79356b
A
429/*
430 * Package up an I/O request on a vnode into a uio and do it.
431 */
9bccf70c 432int
91447636
A
433vn_rdwr(
434 enum uio_rw rw,
435 struct vnode *vp,
436 caddr_t base,
437 int len,
438 off_t offset,
439 enum uio_seg segflg,
440 int ioflg,
441 kauth_cred_t cred,
442 int *aresid,
443 struct proc *p)
1c79356b 444{
91447636
A
445 return vn_rdwr_64(rw,
446 vp,
447 (uint64_t)(uintptr_t)base,
448 (int64_t)len,
449 offset,
450 segflg,
451 ioflg,
452 cred,
453 aresid,
454 p);
455}
456
457
458int
459vn_rdwr_64(
460 enum uio_rw rw,
461 struct vnode *vp,
462 uint64_t base,
463 int64_t len,
464 off_t offset,
465 enum uio_seg segflg,
466 int ioflg,
467 kauth_cred_t cred,
468 int *aresid,
469 struct proc *p)
470{
471 uio_t auio;
472 int spacetype;
473 struct vfs_context context;
1c79356b 474 int error=0;
91447636
A
475 char uio_buf[ UIO_SIZEOF(1) ];
476
477 context.vc_proc = p;
478 context.vc_ucred = cred;
1c79356b 479
91447636
A
480 if (UIO_SEG_IS_USER_SPACE(segflg)) {
481 spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32;
482 }
483 else {
484 spacetype = UIO_SYSSPACE;
485 }
486 auio = uio_createwithbuffer(1, offset, spacetype, rw,
487 &uio_buf[0], sizeof(uio_buf));
488 uio_addiov(auio, base, len);
489
490 if (rw == UIO_READ) {
491 if (vp->v_flag & VSWAP) {
492 error = vn_read_swapfile(vp, auio);
493 } else {
494 error = VNOP_READ(vp, auio, ioflg, &context);
495 }
496 } else {
497 error = VNOP_WRITE(vp, auio, ioflg, &context);
498 }
1c79356b
A
499
500 if (aresid)
91447636
A
501 // LP64todo - fix this
502 *aresid = uio_resid(auio);
1c79356b 503 else
91447636 504 if (uio_resid(auio) && error == 0)
1c79356b 505 error = EIO;
1c79356b
A
506 return (error);
507}
508
509/*
510 * File table vnode read routine.
511 */
9bccf70c 512static int
91447636
A
513vn_read(struct fileproc *fp, struct uio *uio, kauth_cred_t cred,
514 int flags, struct proc *p)
1c79356b 515{
9bccf70c
A
516 struct vnode *vp;
517 int error, ioflag;
1c79356b 518 off_t count;
91447636 519 struct vfs_context context;
1c79356b 520
91447636
A
521 context.vc_proc = p;
522 context.vc_ucred = cred;
9bccf70c 523
91447636
A
524 vp = (struct vnode *)fp->f_fglob->fg_data;
525 if ( (error = vnode_getwithref(vp)) ) {
526 return(error);
527 }
9bccf70c 528 ioflag = 0;
91447636 529 if (fp->f_fglob->fg_flag & FNONBLOCK)
9bccf70c 530 ioflag |= IO_NDELAY;
91447636 531
9bccf70c 532 if ((flags & FOF_OFFSET) == 0)
91447636
A
533 uio->uio_offset = fp->f_fglob->fg_offset;
534 count = uio_resid(uio);
535
536 if (vp->v_flag & VSWAP) {
537 /* special case for swap files */
538 error = vn_read_swapfile(vp, uio);
539 } else {
540 error = VNOP_READ(vp, uio, ioflag, &context);
9bccf70c 541 }
9bccf70c 542 if ((flags & FOF_OFFSET) == 0)
91447636
A
543 fp->f_fglob->fg_offset += count - uio_resid(uio);
544
545 (void)vnode_put(vp);
1c79356b
A
546 return (error);
547}
548
549
550/*
551 * File table vnode write routine.
552 */
9bccf70c 553static int
91447636
A
554vn_write(struct fileproc *fp, struct uio *uio, kauth_cred_t cred,
555 int flags, struct proc *p)
1c79356b 556{
9bccf70c
A
557 struct vnode *vp;
558 int error, ioflag;
1c79356b 559 off_t count;
91447636
A
560 struct vfs_context context;
561
562 context.vc_proc = p;
563 context.vc_ucred = cred;
564 count = 0;
565 vp = (struct vnode *)fp->f_fglob->fg_data;
566 if ( (error = vnode_getwithref(vp)) ) {
567 return(error);
568 }
9bccf70c 569 ioflag = IO_UNIT;
91447636 570 if (vp->v_type == VREG && (fp->f_fglob->fg_flag & O_APPEND))
1c79356b 571 ioflag |= IO_APPEND;
91447636 572 if (fp->f_fglob->fg_flag & FNONBLOCK)
1c79356b 573 ioflag |= IO_NDELAY;
91447636 574 if ((fp->f_fglob->fg_flag & O_FSYNC) ||
1c79356b
A
575 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
576 ioflag |= IO_SYNC;
91447636 577
9bccf70c 578 if ((flags & FOF_OFFSET) == 0) {
91447636
A
579 uio->uio_offset = fp->f_fglob->fg_offset;
580 count = uio_resid(uio);
9bccf70c 581 }
91447636
A
582 if (p && (vp->v_type == VREG) &&
583 (((uio->uio_offset + uio_uio_resid(uio)) > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) ||
584 (uio_uio_resid(uio) > (p->p_rlimit[RLIMIT_FSIZE].rlim_cur - uio->uio_offset)))) {
585 psignal(p, SIGXFSZ);
586 vnode_put(vp);
587 return (EFBIG);
9bccf70c 588 }
91447636
A
589
590 error = VNOP_WRITE(vp, uio, ioflag, &context);
1c79356b 591
9bccf70c
A
592 if ((flags & FOF_OFFSET) == 0) {
593 if (ioflag & IO_APPEND)
91447636 594 fp->f_fglob->fg_offset = uio->uio_offset;
9bccf70c 595 else
91447636 596 fp->f_fglob->fg_offset += count - uio_resid(uio);
9bccf70c
A
597 }
598
1c79356b
A
599 /*
600 * Set the credentials on successful writes
601 */
602 if ((error == 0) && (vp->v_tag == VT_NFS) && (UBCINFOEXISTS(vp))) {
603 ubc_setcred(vp, p);
604 }
91447636 605 (void)vnode_put(vp);
1c79356b
A
606 return (error);
607}
608
609/*
610 * File table vnode stat routine.
611 */
9bccf70c 612int
91447636 613vn_stat_noauth(struct vnode *vp, struct stat *sb, kauth_filesec_t *xsec, vfs_context_t ctx)
1c79356b 614{
91447636 615 struct vnode_attr va;
1c79356b
A
616 int error;
617 u_short mode;
91447636
A
618 kauth_filesec_t fsec;
619
620 VATTR_INIT(&va);
621 VATTR_WANTED(&va, va_fsid);
622 VATTR_WANTED(&va, va_fileid);
623 VATTR_WANTED(&va, va_mode);
624 VATTR_WANTED(&va, va_type);
625 VATTR_WANTED(&va, va_nlink);
626 VATTR_WANTED(&va, va_uid);
627 VATTR_WANTED(&va, va_gid);
628 VATTR_WANTED(&va, va_rdev);
629 VATTR_WANTED(&va, va_data_size);
630 VATTR_WANTED(&va, va_access_time);
631 VATTR_WANTED(&va, va_modify_time);
632 VATTR_WANTED(&va, va_change_time);
633 VATTR_WANTED(&va, va_flags);
634 VATTR_WANTED(&va, va_gen);
635 VATTR_WANTED(&va, va_iosize);
636 /* lower layers will synthesise va_total_alloc from va_data_size if required */
637 VATTR_WANTED(&va, va_total_alloc);
638 if (xsec != NULL) {
639 VATTR_WANTED(&va, va_uuuid);
640 VATTR_WANTED(&va, va_guuid);
641 VATTR_WANTED(&va, va_acl);
642 }
643 error = vnode_getattr(vp, &va, ctx);
1c79356b 644 if (error)
91447636 645 goto out;
1c79356b
A
646 /*
647 * Copy from vattr table
648 */
91447636
A
649 sb->st_dev = va.va_fsid;
650 sb->st_ino = (ino_t)va.va_fileid;
651 mode = va.va_mode;
1c79356b
A
652 switch (vp->v_type) {
653 case VREG:
654 mode |= S_IFREG;
655 break;
656 case VDIR:
657 mode |= S_IFDIR;
658 break;
659 case VBLK:
660 mode |= S_IFBLK;
661 break;
662 case VCHR:
663 mode |= S_IFCHR;
664 break;
665 case VLNK:
666 mode |= S_IFLNK;
667 break;
668 case VSOCK:
669 mode |= S_IFSOCK;
670 break;
671 case VFIFO:
672 mode |= S_IFIFO;
673 break;
674 default:
91447636
A
675 error = EBADF;
676 goto out;
1c79356b
A
677 };
678 sb->st_mode = mode;
91447636
A
679 sb->st_nlink = VATTR_IS_SUPPORTED(&va, va_nlink) ? (u_int16_t)va.va_nlink : 1;
680 sb->st_uid = va.va_uid;
681 sb->st_gid = va.va_gid;
682 sb->st_rdev = va.va_rdev;
683 sb->st_size = va.va_data_size;
684 sb->st_atimespec = va.va_access_time;
685 sb->st_mtimespec = va.va_modify_time;
686 sb->st_ctimespec = va.va_change_time;
687 sb->st_blksize = va.va_iosize;
688 sb->st_flags = va.va_flags;
689 sb->st_blocks = roundup(va.va_total_alloc, 512) / 512;
690
691 /* if we're interested in exended security data and we got an ACL */
692 if (xsec != NULL) {
693 if (!VATTR_IS_SUPPORTED(&va, va_acl) &&
694 !VATTR_IS_SUPPORTED(&va, va_uuuid) &&
695 !VATTR_IS_SUPPORTED(&va, va_guuid)) {
696 *xsec = KAUTH_FILESEC_NONE;
697 } else {
698
699 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
700 fsec = kauth_filesec_alloc(va.va_acl->acl_entrycount);
701 } else {
702 fsec = kauth_filesec_alloc(0);
703 }
704 if (fsec == NULL) {
705 error = ENOMEM;
706 goto out;
707 }
708 fsec->fsec_magic = KAUTH_FILESEC_MAGIC;
709 if (VATTR_IS_SUPPORTED(&va, va_uuuid)) {
710 fsec->fsec_owner = va.va_uuuid;
711 } else {
712 fsec->fsec_owner = kauth_null_guid;
713 }
714 if (VATTR_IS_SUPPORTED(&va, va_guuid)) {
715 fsec->fsec_group = va.va_guuid;
716 } else {
717 fsec->fsec_group = kauth_null_guid;
718 }
719 if (VATTR_IS_SUPPORTED(&va, va_acl) && (va.va_acl != NULL)) {
720 bcopy(va.va_acl, &(fsec->fsec_acl), KAUTH_ACL_COPYSIZE(va.va_acl));
721 } else {
722 fsec->fsec_acl.acl_entrycount = KAUTH_FILESEC_NOACL;
723 }
724 *xsec = fsec;
725 }
726 }
727
1c79356b 728 /* Do not give the generation number out to unpriviledged users */
91447636 729 if (va.va_gen && !vfs_context_issuser(ctx))
1c79356b
A
730 sb->st_gen = 0;
731 else
91447636
A
732 sb->st_gen = va.va_gen;
733
734 error = 0;
735out:
736 if (VATTR_IS_SUPPORTED(&va, va_acl) && va.va_acl != NULL)
737 kauth_acl_free(va.va_acl);
738 return (error);
739}
740
741int
742vn_stat(struct vnode *vp, struct stat *sb, kauth_filesec_t *xsec, vfs_context_t ctx)
743{
744 int error;
745
746 /* authorize */
747 if ((error = vnode_authorize(vp, NULL, KAUTH_VNODE_READ_ATTRIBUTES | KAUTH_VNODE_READ_SECURITY, ctx)) != 0)
748 return(error);
749
750 /* actual stat */
751 return(vn_stat_noauth(vp, sb, xsec, ctx));
1c79356b
A
752}
753
91447636 754
1c79356b
A
755/*
756 * File table vnode ioctl routine.
757 */
9bccf70c 758static int
1c79356b 759vn_ioctl(fp, com, data, p)
91447636 760 struct fileproc *fp;
1c79356b
A
761 u_long com;
762 caddr_t data;
763 struct proc *p;
764{
91447636
A
765 register struct vnode *vp = ((struct vnode *)fp->f_fglob->fg_data);
766 struct vfs_context context;
767 off_t file_size;
1c79356b 768 int error;
fa4905b1 769 struct vnode *ttyvp;
91447636 770 int funnel_state;
9bccf70c 771
91447636
A
772 if ( (error = vnode_getwithref(vp)) ) {
773 return(error);
774 }
775 context.vc_proc = p;
776 context.vc_ucred = p->p_ucred; /* XXX kauth_cred_get() ??? */
777
1c79356b
A
778 switch (vp->v_type) {
779
780 case VREG:
781 case VDIR:
782 if (com == FIONREAD) {
91447636
A
783 if ((error = vnode_size(vp, &file_size, &context)) != 0)
784 goto out;
785 *(int *)data = file_size - fp->f_fglob->fg_offset;
786 goto out;
787 }
788 if (com == FIONBIO || com == FIOASYNC) { /* XXX */
789 goto out;
1c79356b 790 }
1c79356b
A
791 /* fall into ... */
792
793 default:
91447636
A
794 error = ENOTTY;
795 goto out;
1c79356b
A
796
797 case VFIFO:
798 case VCHR:
799 case VBLK:
9bccf70c 800
91447636
A
801 /* Should not be able to set block size from user space */
802 if (com == DKIOCSETBLOCKSIZE) {
803 error = EPERM;
804 goto out;
805 }
806
807 if (com == FIODTYPE) {
808 if (vp->v_type == VBLK) {
809 if (major(vp->v_rdev) >= nblkdev) {
810 error = ENXIO;
811 goto out;
812 }
813 *(int *)data = bdevsw[major(vp->v_rdev)].d_type;
814
815 } else if (vp->v_type == VCHR) {
816 if (major(vp->v_rdev) >= nchrdev) {
817 error = ENXIO;
818 goto out;
819 }
820 *(int *)data = cdevsw[major(vp->v_rdev)].d_type;
821 } else {
822 error = ENOTTY;
823 goto out;
824 }
825 goto out;
826 }
827 error = VNOP_IOCTL(vp, com, data, fp->f_fglob->fg_flag, &context);
828
829 if (error == 0 && com == TIOCSCTTY) {
830 vnode_ref(vp);
831
832 funnel_state = thread_funnel_set(kernel_flock, TRUE);
833 ttyvp = p->p_session->s_ttyvp;
834 p->p_session->s_ttyvp = vp;
835 thread_funnel_set(kernel_flock, funnel_state);
836
837 if (ttyvp)
838 vnode_rele(ttyvp);
839 }
1c79356b 840 }
91447636
A
841out:
842 (void)vnode_put(vp);
843 return(error);
1c79356b
A
844}
845
846/*
847 * File table vnode select routine.
848 */
9bccf70c 849static int
0b4e3aa0 850vn_select(fp, which, wql, p)
91447636 851 struct fileproc *fp;
1c79356b 852 int which;
0b4e3aa0 853 void * wql;
1c79356b
A
854 struct proc *p;
855{
91447636
A
856 int error;
857 struct vnode * vp = (struct vnode *)fp->f_fglob->fg_data;
858 struct vfs_context context;
859
860 if ( (error = vnode_getwithref(vp)) == 0 ) {
861 context.vc_proc = p;
862 context.vc_ucred = fp->f_fglob->fg_cred;
863
864 error = VNOP_SELECT(vp, which, fp->f_fglob->fg_flag, wql, &context);
1c79356b 865
91447636
A
866 (void)vnode_put(vp);
867 }
868 return(error);
869
1c79356b
A
870}
871
872/*
873 * Check that the vnode is still valid, and if so
874 * acquire requested lock.
875 */
876int
91447636 877vn_lock(__unused vnode_t vp, __unused int flags, __unused proc_t p)
1c79356b 878{
91447636 879 return (0);
1c79356b
A
880}
881
882/*
883 * File table vnode close routine.
884 */
9bccf70c 885static int
91447636
A
886vn_closefile(fg, p)
887 struct fileglob *fg;
1c79356b
A
888 struct proc *p;
889{
91447636
A
890 struct vnode *vp = (struct vnode *)fg->fg_data;
891 int error;
892
893 if ( (error = vnode_getwithref(vp)) == 0 ) {
894 error = vn_close(vp, fg->fg_flag, fg->fg_cred, p);
895
896 (void)vnode_put(vp);
897 }
898 return(error);
899}
900
901int
902vn_pathconf(vnode_t vp, int name, register_t *retval, vfs_context_t ctx)
903{
904 int error = 0;
905
906 switch(name) {
907 case _PC_EXTENDED_SECURITY_NP:
908 *retval = vfs_extendedsecurity(vnode_mount(vp));
909 break;
910 case _PC_AUTH_OPAQUE_NP:
911 *retval = vfs_authopaque(vnode_mount(vp));
912 break;
913 default:
914 error = VNOP_PATHCONF(vp, name, retval, ctx);
915 break;
916 }
1c79356b 917
91447636 918 return (error);
1c79356b 919}
55e303ae
A
920
921static int
922vn_kqfilt_add(fp, kn, p)
91447636 923 struct fileproc *fp;
55e303ae
A
924 struct knote *kn;
925 struct proc *p;
926{
91447636
A
927 struct vnode *vp = (struct vnode *)fp->f_fglob->fg_data;
928 struct vfs_context context;
55e303ae 929 int error;
91447636 930 int funnel_state;
55e303ae 931
91447636
A
932 if ( (error = vnode_getwithref(vp)) == 0 ) {
933 context.vc_proc = p;
934 context.vc_ucred = p->p_ucred; /* XXX kauth_cred_get() ??? */
935
936 funnel_state = thread_funnel_set(kernel_flock, TRUE);
937 error = VNOP_KQFILT_ADD(vp, kn, &context);
938 thread_funnel_set(kernel_flock, funnel_state);
939
940 (void)vnode_put(vp);
941 }
55e303ae
A
942 return (error);
943}
944
91447636
A
945#if 0
946/* No one calls this yet. */
55e303ae
A
947static int
948vn_kqfilt_remove(vp, ident, p)
949 struct vnode *vp;
950 uintptr_t ident;
951 struct proc *p;
952{
91447636 953 struct vfs_context context;
55e303ae 954 int error;
91447636 955 int funnel_state;
55e303ae 956
91447636
A
957 if ( (error = vnode_getwithref(vp)) == 0 ) {
958 context.vc_proc = p;
959 context.vc_ucred = p->p_ucred; /* XXX kauth_cred_get() ??? */
960
961 funnel_state = thread_funnel_set(kernel_flock, TRUE);
962 error = VNOP_KQFILT_REMOVE(vp, ident, &context);
963 thread_funnel_set(kernel_flock, funnel_state);
964
965 (void)vnode_put(vp);
966 }
55e303ae
A
967 return (error);
968}
91447636 969#endif