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