]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_vnops.c
xnu-792.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_LICENSE_HEADER_START@
5 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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 *
62 */
63
64 #include <sys/param.h>
65 #include <sys/types.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/file_internal.h>
69 #include <sys/stat.h>
70 #include <sys/proc_internal.h>
71 #include <sys/kauth.h>
72 #include <sys/mount_internal.h>
73 #include <sys/namei.h>
74 #include <sys/vnode_internal.h>
75 #include <sys/ioctl.h>
76 #include <sys/tty.h>
77 #include <sys/ubc.h>
78 #include <sys/conf.h>
79 #include <sys/disk.h>
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>
85
86 #include <vm/vm_kern.h>
87 #include <vm/vm_map.h>
88
89 #include <miscfs/specfs/specdev.h>
90
91
92
93 static int vn_closefile(struct fileglob *fp, struct proc *p);
94 static int vn_ioctl(struct fileproc *fp, u_long com, caddr_t data, struct proc *p);
95 static int vn_read(struct fileproc *fp, struct uio *uio,
96 kauth_cred_t cred, int flags, struct proc *p);
97 static int vn_write(struct fileproc *fp, struct uio *uio,
98 kauth_cred_t cred, int flags, struct proc *p);
99 static int vn_select( struct fileproc *fp, int which, void * wql, struct proc *p);
100 static int vn_kqfilt_add(struct fileproc *fp, struct knote *kn, struct proc *p);
101 #if 0
102 static int vn_kqfilt_remove(struct vnode *vp, uintptr_t ident, struct proc *p);
103 #endif
104
105 struct fileops vnops =
106 { vn_read, vn_write, vn_ioctl, vn_select, vn_closefile, vn_kqfilt_add, 0 };
107
108 /*
109 * Common code for vnode open operations.
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.
113 */
114 int
115 vn_open(struct nameidata *ndp, int fmode, int cmode)
116 {
117 return(vn_open_modflags(ndp, &fmode, cmode));
118 }
119
120 int
121 vn_open_modflags(struct nameidata *ndp, int *fmodep, int cmode)
122 {
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
131 int
132 vn_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;
137 int error;
138 int fmode;
139 kauth_action_t action;
140
141 again:
142 vp = NULL;
143 dvp = NULL;
144 fmode = *fmodep;
145 if (fmode & O_CREAT) {
146 ndp->ni_cnd.cn_nameiop = CREATE;
147 ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | AUDITVNPATH1;
148
149 if ((fmode & O_EXCL) == 0)
150 ndp->ni_cnd.cn_flags |= FOLLOW;
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);
169 if (fmode & O_EXCL)
170 vap->va_vaflags |= VA_EXCLUSIVE;
171
172 if ((error = vn_create(dvp, &ndp->ni_vp, &ndp->ni_cnd, vap, 0, ctx)) != 0)
173 goto badcreate;
174
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 */
200 badcreate:
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;
213 }
214 fmode &= ~O_TRUNC;
215 } else {
216 nameidone(ndp);
217 ndp->ni_dvp = NULL;
218 vnode_put(dvp);
219
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;
228 ndp->ni_cnd.cn_flags = FOLLOW | LOCKLEAF | AUDITVNPATH1;
229 if ( (error = namei(ndp)) )
230 goto out;
231 vp = ndp->ni_vp;
232 nameidone(ndp);
233 ndp->ni_dvp = NULL;
234 }
235 if (vp->v_type == VSOCK && vp->v_tag != VT_FDESC) {
236 error = EOPNOTSUPP; /* Operation not supported on socket */
237 goto bad;
238 }
239
240 #if DIAGNOSTIC
241 if (UBCINFOMISSING(vp))
242 panic("vn_open: ubc_info_init");
243 #endif /* DIAGNOSTIC */
244
245 /* authorize open of an existing file */
246 if ((fmode & O_CREAT) == 0) {
247
248 /* disallow write operations on directories */
249 if (vnode_isdir(vp) && (fmode & (FWRITE | O_TRUNC))) {
250 error = EISDIR;
251 goto bad;
252 }
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
263 }
264
265 if ( (error = VNOP_OPEN(vp, fmode, ctx)) ) {
266 goto bad;
267 }
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);
276
277 *fmodep = fmode;
278 return (0);
279 bad:
280 ndp->ni_vp = NULL;
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 }
292 out:
293 return (error);
294 }
295
296 /*
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.
304 */
305 #warning vn_access is deprecated
306 int
307 vn_access(vnode_t vp, int mode, vfs_context_t context)
308 {
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));
320 }
321
322 /*
323 * Vnode close call
324 */
325 int
326 vn_close(struct vnode *vp, int flags, kauth_cred_t cred, struct proc *p)
327 {
328 struct vfs_context context;
329 int error;
330
331 context.vc_proc = p;
332 context.vc_ucred = cred;
333
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);
339 }
340 }
341
342 error = VNOP_CLOSE(vp, flags, &context);
343 (void)vnode_rele_ext(vp, flags, 0);
344
345 return (error);
346 }
347
348 static int
349 vn_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 }
429 /*
430 * Package up an I/O request on a vnode into a uio and do it.
431 */
432 int
433 vn_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)
444 {
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
458 int
459 vn_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;
474 int error=0;
475 char uio_buf[ UIO_SIZEOF(1) ];
476
477 context.vc_proc = p;
478 context.vc_ucred = cred;
479
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 }
499
500 if (aresid)
501 // LP64todo - fix this
502 *aresid = uio_resid(auio);
503 else
504 if (uio_resid(auio) && error == 0)
505 error = EIO;
506 return (error);
507 }
508
509 /*
510 * File table vnode read routine.
511 */
512 static int
513 vn_read(struct fileproc *fp, struct uio *uio, kauth_cred_t cred,
514 int flags, struct proc *p)
515 {
516 struct vnode *vp;
517 int error, ioflag;
518 off_t count;
519 struct vfs_context context;
520
521 context.vc_proc = p;
522 context.vc_ucred = cred;
523
524 vp = (struct vnode *)fp->f_fglob->fg_data;
525 if ( (error = vnode_getwithref(vp)) ) {
526 return(error);
527 }
528 ioflag = 0;
529 if (fp->f_fglob->fg_flag & FNONBLOCK)
530 ioflag |= IO_NDELAY;
531
532 if ((flags & FOF_OFFSET) == 0)
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);
541 }
542 if ((flags & FOF_OFFSET) == 0)
543 fp->f_fglob->fg_offset += count - uio_resid(uio);
544
545 (void)vnode_put(vp);
546 return (error);
547 }
548
549
550 /*
551 * File table vnode write routine.
552 */
553 static int
554 vn_write(struct fileproc *fp, struct uio *uio, kauth_cred_t cred,
555 int flags, struct proc *p)
556 {
557 struct vnode *vp;
558 int error, ioflag;
559 off_t count;
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 }
569 ioflag = IO_UNIT;
570 if (vp->v_type == VREG && (fp->f_fglob->fg_flag & O_APPEND))
571 ioflag |= IO_APPEND;
572 if (fp->f_fglob->fg_flag & FNONBLOCK)
573 ioflag |= IO_NDELAY;
574 if ((fp->f_fglob->fg_flag & O_FSYNC) ||
575 (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
576 ioflag |= IO_SYNC;
577
578 if ((flags & FOF_OFFSET) == 0) {
579 uio->uio_offset = fp->f_fglob->fg_offset;
580 count = uio_resid(uio);
581 }
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);
588 }
589
590 error = VNOP_WRITE(vp, uio, ioflag, &context);
591
592 if ((flags & FOF_OFFSET) == 0) {
593 if (ioflag & IO_APPEND)
594 fp->f_fglob->fg_offset = uio->uio_offset;
595 else
596 fp->f_fglob->fg_offset += count - uio_resid(uio);
597 }
598
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 }
605 (void)vnode_put(vp);
606 return (error);
607 }
608
609 /*
610 * File table vnode stat routine.
611 */
612 int
613 vn_stat_noauth(struct vnode *vp, struct stat *sb, kauth_filesec_t *xsec, vfs_context_t ctx)
614 {
615 struct vnode_attr va;
616 int error;
617 u_short mode;
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);
644 if (error)
645 goto out;
646 /*
647 * Copy from vattr table
648 */
649 sb->st_dev = va.va_fsid;
650 sb->st_ino = (ino_t)va.va_fileid;
651 mode = va.va_mode;
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:
675 error = EBADF;
676 goto out;
677 };
678 sb->st_mode = mode;
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
728 /* Do not give the generation number out to unpriviledged users */
729 if (va.va_gen && !vfs_context_issuser(ctx))
730 sb->st_gen = 0;
731 else
732 sb->st_gen = va.va_gen;
733
734 error = 0;
735 out:
736 if (VATTR_IS_SUPPORTED(&va, va_acl) && va.va_acl != NULL)
737 kauth_acl_free(va.va_acl);
738 return (error);
739 }
740
741 int
742 vn_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));
752 }
753
754
755 /*
756 * File table vnode ioctl routine.
757 */
758 static int
759 vn_ioctl(fp, com, data, p)
760 struct fileproc *fp;
761 u_long com;
762 caddr_t data;
763 struct proc *p;
764 {
765 register struct vnode *vp = ((struct vnode *)fp->f_fglob->fg_data);
766 struct vfs_context context;
767 off_t file_size;
768 int error;
769 struct vnode *ttyvp;
770 int funnel_state;
771
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
778 switch (vp->v_type) {
779
780 case VREG:
781 case VDIR:
782 if (com == FIONREAD) {
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;
790 }
791 /* fall into ... */
792
793 default:
794 error = ENOTTY;
795 goto out;
796
797 case VFIFO:
798 case VCHR:
799 case VBLK:
800
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 }
840 }
841 out:
842 (void)vnode_put(vp);
843 return(error);
844 }
845
846 /*
847 * File table vnode select routine.
848 */
849 static int
850 vn_select(fp, which, wql, p)
851 struct fileproc *fp;
852 int which;
853 void * wql;
854 struct proc *p;
855 {
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);
865
866 (void)vnode_put(vp);
867 }
868 return(error);
869
870 }
871
872 /*
873 * Check that the vnode is still valid, and if so
874 * acquire requested lock.
875 */
876 int
877 vn_lock(__unused vnode_t vp, __unused int flags, __unused proc_t p)
878 {
879 return (0);
880 }
881
882 /*
883 * File table vnode close routine.
884 */
885 static int
886 vn_closefile(fg, p)
887 struct fileglob *fg;
888 struct proc *p;
889 {
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
901 int
902 vn_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 }
917
918 return (error);
919 }
920
921 static int
922 vn_kqfilt_add(fp, kn, p)
923 struct fileproc *fp;
924 struct knote *kn;
925 struct proc *p;
926 {
927 struct vnode *vp = (struct vnode *)fp->f_fglob->fg_data;
928 struct vfs_context context;
929 int error;
930 int funnel_state;
931
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 }
942 return (error);
943 }
944
945 #if 0
946 /* No one calls this yet. */
947 static int
948 vn_kqfilt_remove(vp, ident, p)
949 struct vnode *vp;
950 uintptr_t ident;
951 struct proc *p;
952 {
953 struct vfs_context context;
954 int error;
955 int funnel_state;
956
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 }
967 return (error);
968 }
969 #endif