proc->thread stage 3.5: Add an IO_CORE flag so coda doesn't have to dig
[dragonfly.git] / sys / vfs / ufs / ufs_vnops.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
39 * $FreeBSD: src/sys/ufs/ufs/ufs_vnops.c,v 1.131.2.8 2003/01/02 17:26:19 bde Exp $
41c20dac 40 * $DragonFly: src/sys/vfs/ufs/ufs_vnops.c,v 1.3 2003/06/23 17:55:51 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_quota.h"
44#include "opt_suiddir.h"
45#include "opt_ufs.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/namei.h>
50#include <sys/kernel.h>
51#include <sys/fcntl.h>
52#include <sys/stat.h>
53#include <sys/buf.h>
54#include <sys/proc.h>
55#include <sys/mount.h>
56#include <sys/unistd.h>
57#include <sys/vnode.h>
58#include <sys/malloc.h>
59#include <sys/dirent.h>
60#include <sys/lockf.h>
61#include <sys/event.h>
62#include <sys/conf.h>
63
64#include <sys/file.h> /* XXX */
65
66#include <vm/vm.h>
67#include <vm/vm_extern.h>
68
69#include <miscfs/fifofs/fifo.h>
70
71#include <ufs/ufs/quota.h>
72#include <ufs/ufs/inode.h>
73#include <ufs/ufs/dir.h>
74#include <ufs/ufs/ufsmount.h>
75#include <ufs/ufs/ufs_extern.h>
76#ifdef UFS_DIRHASH
77#include <ufs/ufs/dirhash.h>
78#endif
79
80static int ufs_access __P((struct vop_access_args *));
81static int ufs_advlock __P((struct vop_advlock_args *));
82static int ufs_chmod __P((struct vnode *, int, struct ucred *, struct proc *));
83static int ufs_chown __P((struct vnode *, uid_t, gid_t, struct ucred *, struct proc *));
84static int ufs_close __P((struct vop_close_args *));
85static int ufs_create __P((struct vop_create_args *));
86static int ufs_getattr __P((struct vop_getattr_args *));
87static int ufs_link __P((struct vop_link_args *));
88static int ufs_makeinode __P((int mode, struct vnode *, struct vnode **, struct componentname *));
89static int ufs_missingop __P((struct vop_generic_args *ap));
90static int ufs_mkdir __P((struct vop_mkdir_args *));
91static int ufs_mknod __P((struct vop_mknod_args *));
92static int ufs_mmap __P((struct vop_mmap_args *));
93static int ufs_open __P((struct vop_open_args *));
94static int ufs_pathconf __P((struct vop_pathconf_args *));
95static int ufs_print __P((struct vop_print_args *));
96static int ufs_readdir __P((struct vop_readdir_args *));
97static int ufs_readlink __P((struct vop_readlink_args *));
98static int ufs_remove __P((struct vop_remove_args *));
99static int ufs_rename __P((struct vop_rename_args *));
100static int ufs_rmdir __P((struct vop_rmdir_args *));
101static int ufs_setattr __P((struct vop_setattr_args *));
102static int ufs_strategy __P((struct vop_strategy_args *));
103static int ufs_symlink __P((struct vop_symlink_args *));
104static int ufs_whiteout __P((struct vop_whiteout_args *));
105static int ufsfifo_close __P((struct vop_close_args *));
106static int ufsfifo_kqfilter __P((struct vop_kqfilter_args *));
107static int ufsfifo_read __P((struct vop_read_args *));
108static int ufsfifo_write __P((struct vop_write_args *));
109static int ufsspec_close __P((struct vop_close_args *));
110static int ufsspec_read __P((struct vop_read_args *));
111static int ufsspec_write __P((struct vop_write_args *));
112static int filt_ufsread __P((struct knote *kn, long hint));
113static int filt_ufswrite __P((struct knote *kn, long hint));
114static int filt_ufsvnode __P((struct knote *kn, long hint));
115static void filt_ufsdetach __P((struct knote *kn));
116static int ufs_kqfilter __P((struct vop_kqfilter_args *ap));
117
118union _qcvt {
119 int64_t qcvt;
120 int32_t val[2];
121};
122#define SETHIGH(q, h) { \
123 union _qcvt tmp; \
124 tmp.qcvt = (q); \
125 tmp.val[_QUAD_HIGHWORD] = (h); \
126 (q) = tmp.qcvt; \
127}
128#define SETLOW(q, l) { \
129 union _qcvt tmp; \
130 tmp.qcvt = (q); \
131 tmp.val[_QUAD_LOWWORD] = (l); \
132 (q) = tmp.qcvt; \
133}
134#define VN_KNOTE(vp, b) \
135 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
136
137/*
138 * A virgin directory (no blushing please).
139 */
140static struct dirtemplate mastertemplate = {
141 0, 12, DT_DIR, 1, ".",
142 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
143};
144static struct odirtemplate omastertemplate = {
145 0, 12, 1, ".",
146 0, DIRBLKSIZ - 12, 2, ".."
147};
148
149void
150ufs_itimes(vp)
151 struct vnode *vp;
152{
153 struct inode *ip;
154 struct timespec ts;
155
156 ip = VTOI(vp);
157 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
158 return;
159 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
160 ip->i_flag |= IN_LAZYMOD;
161 else
162 ip->i_flag |= IN_MODIFIED;
163 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
164 vfs_timestamp(&ts);
165 if (ip->i_flag & IN_ACCESS) {
166 ip->i_atime = ts.tv_sec;
167 ip->i_atimensec = ts.tv_nsec;
168 }
169 if (ip->i_flag & IN_UPDATE) {
170 ip->i_mtime = ts.tv_sec;
171 ip->i_mtimensec = ts.tv_nsec;
172 ip->i_modrev++;
173 }
174 if (ip->i_flag & IN_CHANGE) {
175 ip->i_ctime = ts.tv_sec;
176 ip->i_ctimensec = ts.tv_nsec;
177 }
178 }
179 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
180}
181
182/*
183 * Create a regular file
184 */
185int
186ufs_create(ap)
187 struct vop_create_args /* {
188 struct vnode *a_dvp;
189 struct vnode **a_vpp;
190 struct componentname *a_cnp;
191 struct vattr *a_vap;
192 } */ *ap;
193{
194 int error;
195
196 error =
197 ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
198 ap->a_dvp, ap->a_vpp, ap->a_cnp);
199 if (error)
200 return (error);
201 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
202 return (0);
203}
204
205/*
206 * Mknod vnode call
207 */
208/* ARGSUSED */
209int
210ufs_mknod(ap)
211 struct vop_mknod_args /* {
212 struct vnode *a_dvp;
213 struct vnode **a_vpp;
214 struct componentname *a_cnp;
215 struct vattr *a_vap;
216 } */ *ap;
217{
218 struct vattr *vap = ap->a_vap;
219 struct vnode **vpp = ap->a_vpp;
220 struct inode *ip;
221 ino_t ino;
222 int error;
223
224 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
225 ap->a_dvp, vpp, ap->a_cnp);
226 if (error)
227 return (error);
228 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
229 ip = VTOI(*vpp);
230 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
231 if (vap->va_rdev != VNOVAL) {
232 /*
233 * Want to be able to use this to make badblock
234 * inodes, so don't truncate the dev number.
235 */
236 ip->i_rdev = vap->va_rdev;
237 }
238 /*
239 * Remove inode, then reload it through VFS_VGET so it is
240 * checked to see if it is an alias of an existing entry in
241 * the inode cache.
242 */
243 vput(*vpp);
244 (*vpp)->v_type = VNON;
245 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
246 vgone(*vpp);
247 error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp);
248 if (error) {
249 *vpp = NULL;
250 return (error);
251 }
252 return (0);
253}
254
255/*
256 * Open called.
257 *
258 * Nothing to do.
259 */
260/* ARGSUSED */
261int
262ufs_open(ap)
263 struct vop_open_args /* {
264 struct vnode *a_vp;
265 int a_mode;
266 struct ucred *a_cred;
267 struct proc *a_p;
268 } */ *ap;
269{
270
271 /*
272 * Files marked append-only must be opened for appending.
273 */
274 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
275 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
276 return (EPERM);
277 return (0);
278}
279
280/*
281 * Close called.
282 *
283 * Update the times on the inode.
284 */
285/* ARGSUSED */
286int
287ufs_close(ap)
288 struct vop_close_args /* {
289 struct vnode *a_vp;
290 int a_fflag;
291 struct ucred *a_cred;
292 struct proc *a_p;
293 } */ *ap;
294{
295 register struct vnode *vp = ap->a_vp;
296
297 simple_lock(&vp->v_interlock);
298 if (vp->v_usecount > 1)
299 ufs_itimes(vp);
300 simple_unlock(&vp->v_interlock);
301 return (0);
302}
303
304int
305ufs_access(ap)
306 struct vop_access_args /* {
307 struct vnode *a_vp;
308 int a_mode;
309 struct ucred *a_cred;
310 struct proc *a_p;
311 } */ *ap;
312{
313 struct vnode *vp = ap->a_vp;
314 struct inode *ip = VTOI(vp);
315 struct ucred *cred = ap->a_cred;
316 mode_t mask, mode = ap->a_mode;
317 register gid_t *gp;
318 int i;
319#ifdef QUOTA
320 int error;
321#endif
322
323 /*
324 * Disallow write attempts on read-only file systems;
325 * unless the file is a socket, fifo, or a block or
326 * character device resident on the file system.
327 */
328 if (mode & VWRITE) {
329 switch (vp->v_type) {
330 case VDIR:
331 case VLNK:
332 case VREG:
333 if (vp->v_mount->mnt_flag & MNT_RDONLY)
334 return (EROFS);
335#ifdef QUOTA
336 if ((error = getinoquota(ip)) != 0)
337 return (error);
338#endif
339 break;
340 default:
341 break;
342 }
343 }
344
345 /* If immutable bit set, nobody gets to write it. */
346 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
347 return (EPERM);
348
349 /* Otherwise, user id 0 always gets access. */
350 if (cred->cr_uid == 0)
351 return (0);
352
353 mask = 0;
354
355 /* Otherwise, check the owner. */
356 if (cred->cr_uid == ip->i_uid) {
357 if (mode & VEXEC)
358 mask |= S_IXUSR;
359 if (mode & VREAD)
360 mask |= S_IRUSR;
361 if (mode & VWRITE)
362 mask |= S_IWUSR;
363 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
364 }
365
366 /* Otherwise, check the groups. */
367 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
368 if (ip->i_gid == *gp) {
369 if (mode & VEXEC)
370 mask |= S_IXGRP;
371 if (mode & VREAD)
372 mask |= S_IRGRP;
373 if (mode & VWRITE)
374 mask |= S_IWGRP;
375 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
376 }
377
378 /* Otherwise, check everyone else. */
379 if (mode & VEXEC)
380 mask |= S_IXOTH;
381 if (mode & VREAD)
382 mask |= S_IROTH;
383 if (mode & VWRITE)
384 mask |= S_IWOTH;
385 return ((ip->i_mode & mask) == mask ? 0 : EACCES);
386}
387
388/* ARGSUSED */
389int
390ufs_getattr(ap)
391 struct vop_getattr_args /* {
392 struct vnode *a_vp;
393 struct vattr *a_vap;
394 struct ucred *a_cred;
395 struct proc *a_p;
396 } */ *ap;
397{
398 register struct vnode *vp = ap->a_vp;
399 register struct inode *ip = VTOI(vp);
400 register struct vattr *vap = ap->a_vap;
401
402 ufs_itimes(vp);
403 /*
404 * Copy from inode table
405 */
406 vap->va_fsid = dev2udev(ip->i_dev);
407 vap->va_fileid = ip->i_number;
408 vap->va_mode = ip->i_mode & ~IFMT;
409 vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ?
410 ip->i_effnlink : ip->i_nlink;
411 vap->va_uid = ip->i_uid;
412 vap->va_gid = ip->i_gid;
413 vap->va_rdev = ip->i_rdev;
414 vap->va_size = ip->i_din.di_size;
415 vap->va_atime.tv_sec = ip->i_atime;
416 vap->va_atime.tv_nsec = ip->i_atimensec;
417 vap->va_mtime.tv_sec = ip->i_mtime;
418 vap->va_mtime.tv_nsec = ip->i_mtimensec;
419 vap->va_ctime.tv_sec = ip->i_ctime;
420 vap->va_ctime.tv_nsec = ip->i_ctimensec;
421 vap->va_flags = ip->i_flags;
422 vap->va_gen = ip->i_gen;
423 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
424 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
425 vap->va_type = IFTOVT(ip->i_mode);
426 vap->va_filerev = ip->i_modrev;
427 return (0);
428}
429
430/*
431 * Set attribute vnode op. called from several syscalls
432 */
433int
434ufs_setattr(ap)
435 struct vop_setattr_args /* {
436 struct vnode *a_vp;
437 struct vattr *a_vap;
438 struct ucred *a_cred;
439 struct proc *a_p;
440 } */ *ap;
441{
442 struct vattr *vap = ap->a_vap;
443 struct vnode *vp = ap->a_vp;
444 struct inode *ip = VTOI(vp);
445 struct ucred *cred = ap->a_cred;
446 struct proc *p = ap->a_p;
447 int error;
448
449 /*
450 * Check for unsettable attributes.
451 */
452 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
453 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
454 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
455 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
456 return (EINVAL);
457 }
458 if (vap->va_flags != VNOVAL) {
459 if (vp->v_mount->mnt_flag & MNT_RDONLY)
460 return (EROFS);
461 if (cred->cr_uid != ip->i_uid &&
41c20dac 462 (error = suser_xxx(cred, PRISON_ROOT)))
984263bc 463 return (error);
41c20dac 464 if ((cred->cr_uid == 0) && (cred->cr_prison == NULL)) {
984263bc
MD
465 if ((ip->i_flags
466 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
467 securelevel > 0)
468 return (EPERM);
469 ip->i_flags = vap->va_flags;
470 } else {
471 if (ip->i_flags
472 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
473 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
474 return (EPERM);
475 ip->i_flags &= SF_SETTABLE;
476 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
477 }
478 ip->i_flag |= IN_CHANGE;
479 if (vap->va_flags & (IMMUTABLE | APPEND))
480 return (0);
481 }
482 if (ip->i_flags & (IMMUTABLE | APPEND))
483 return (EPERM);
484 /*
485 * Go through the fields and update iff not VNOVAL.
486 */
487 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
488 if (vp->v_mount->mnt_flag & MNT_RDONLY)
489 return (EROFS);
490 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, p)) != 0)
491 return (error);
492 }
493 if (vap->va_size != VNOVAL) {
494 /*
495 * Disallow write attempts on read-only file systems;
496 * unless the file is a socket, fifo, or a block or
497 * character device resident on the file system.
498 */
499 switch (vp->v_type) {
500 case VDIR:
501 return (EISDIR);
502 case VLNK:
503 case VREG:
504 if (vp->v_mount->mnt_flag & MNT_RDONLY)
505 return (EROFS);
506 break;
507 default:
508 break;
509 }
510 if ((error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, p)) != 0)
511 return (error);
512 }
513 ip = VTOI(vp);
514 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
515 if (vp->v_mount->mnt_flag & MNT_RDONLY)
516 return (EROFS);
517 if (cred->cr_uid != ip->i_uid &&
41c20dac 518 (error = suser_xxx(cred, PRISON_ROOT)) &&
984263bc
MD
519 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
520 (error = VOP_ACCESS(vp, VWRITE, cred, p))))
521 return (error);
522 if (vap->va_atime.tv_sec != VNOVAL)
523 ip->i_flag |= IN_ACCESS;
524 if (vap->va_mtime.tv_sec != VNOVAL)
525 ip->i_flag |= IN_CHANGE | IN_UPDATE;
526 ufs_itimes(vp);
527 if (vap->va_atime.tv_sec != VNOVAL) {
528 ip->i_atime = vap->va_atime.tv_sec;
529 ip->i_atimensec = vap->va_atime.tv_nsec;
530 }
531 if (vap->va_mtime.tv_sec != VNOVAL) {
532 ip->i_mtime = vap->va_mtime.tv_sec;
533 ip->i_mtimensec = vap->va_mtime.tv_nsec;
534 }
535 error = UFS_UPDATE(vp, 0);
536 if (error)
537 return (error);
538 }
539 error = 0;
540 if (vap->va_mode != (mode_t)VNOVAL) {
541 if (vp->v_mount->mnt_flag & MNT_RDONLY)
542 return (EROFS);
543 error = ufs_chmod(vp, (int)vap->va_mode, cred, p);
544 }
545 VN_KNOTE(vp, NOTE_ATTRIB);
546 return (error);
547}
548
549/*
550 * Change the mode on a file.
551 * Inode must be locked before calling.
552 */
553static int
554ufs_chmod(vp, mode, cred, p)
555 register struct vnode *vp;
556 register int mode;
557 register struct ucred *cred;
558 struct proc *p;
559{
560 register struct inode *ip = VTOI(vp);
561 int error;
562
563 if (cred->cr_uid != ip->i_uid) {
41c20dac 564 error = suser_xxx(cred, PRISON_ROOT);
984263bc
MD
565 if (error)
566 return (error);
567 }
568 if (cred->cr_uid) {
569 if (vp->v_type != VDIR && (mode & S_ISTXT))
570 return (EFTYPE);
571 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
572 return (EPERM);
573 }
574 ip->i_mode &= ~ALLPERMS;
575 ip->i_mode |= (mode & ALLPERMS);
576 ip->i_flag |= IN_CHANGE;
577 return (0);
578}
579
580/*
581 * Perform chown operation on inode ip;
582 * inode must be locked prior to call.
583 */
584static int
585ufs_chown(vp, uid, gid, cred, p)
586 register struct vnode *vp;
587 uid_t uid;
588 gid_t gid;
589 struct ucred *cred;
590 struct proc *p;
591{
592 register struct inode *ip = VTOI(vp);
593 uid_t ouid;
594 gid_t ogid;
595 int error = 0;
596#ifdef QUOTA
597 register int i;
598 long change;
599#endif
600
601 if (uid == (uid_t)VNOVAL)
602 uid = ip->i_uid;
603 if (gid == (gid_t)VNOVAL)
604 gid = ip->i_gid;
605 /*
606 * If we don't own the file, are trying to change the owner
607 * of the file, or are not a member of the target group,
608 * the caller must be superuser or the call fails.
609 */
610 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
611 (gid != ip->i_gid && !groupmember((gid_t)gid, cred))) &&
41c20dac 612 (error = suser_xxx(cred, PRISON_ROOT)))
984263bc
MD
613 return (error);
614 ogid = ip->i_gid;
615 ouid = ip->i_uid;
616#ifdef QUOTA
617 if ((error = getinoquota(ip)) != 0)
618 return (error);
619 if (ouid == uid) {
620 dqrele(vp, ip->i_dquot[USRQUOTA]);
621 ip->i_dquot[USRQUOTA] = NODQUOT;
622 }
623 if (ogid == gid) {
624 dqrele(vp, ip->i_dquot[GRPQUOTA]);
625 ip->i_dquot[GRPQUOTA] = NODQUOT;
626 }
627 change = ip->i_blocks;
628 (void) chkdq(ip, -change, cred, CHOWN);
629 (void) chkiq(ip, -1, cred, CHOWN);
630 for (i = 0; i < MAXQUOTAS; i++) {
631 dqrele(vp, ip->i_dquot[i]);
632 ip->i_dquot[i] = NODQUOT;
633 }
634#endif
635 ip->i_gid = gid;
636 ip->i_uid = uid;
637#ifdef QUOTA
638 if ((error = getinoquota(ip)) == 0) {
639 if (ouid == uid) {
640 dqrele(vp, ip->i_dquot[USRQUOTA]);
641 ip->i_dquot[USRQUOTA] = NODQUOT;
642 }
643 if (ogid == gid) {
644 dqrele(vp, ip->i_dquot[GRPQUOTA]);
645 ip->i_dquot[GRPQUOTA] = NODQUOT;
646 }
647 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
648 if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
649 goto good;
650 else
651 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
652 }
653 for (i = 0; i < MAXQUOTAS; i++) {
654 dqrele(vp, ip->i_dquot[i]);
655 ip->i_dquot[i] = NODQUOT;
656 }
657 }
658 ip->i_gid = ogid;
659 ip->i_uid = ouid;
660 if (getinoquota(ip) == 0) {
661 if (ouid == uid) {
662 dqrele(vp, ip->i_dquot[USRQUOTA]);
663 ip->i_dquot[USRQUOTA] = NODQUOT;
664 }
665 if (ogid == gid) {
666 dqrele(vp, ip->i_dquot[GRPQUOTA]);
667 ip->i_dquot[GRPQUOTA] = NODQUOT;
668 }
669 (void) chkdq(ip, change, cred, FORCE|CHOWN);
670 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
671 (void) getinoquota(ip);
672 }
673 return (error);
674good:
675 if (getinoquota(ip))
676 panic("ufs_chown: lost quota");
677#endif /* QUOTA */
678 ip->i_flag |= IN_CHANGE;
679 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
680 ip->i_mode &= ~(ISUID | ISGID);
681 return (0);
682}
683
684/*
685 * Mmap a file
686 *
687 * NB Currently unsupported.
688 */
689/* ARGSUSED */
690int
691ufs_mmap(ap)
692 struct vop_mmap_args /* {
693 struct vnode *a_vp;
694 int a_fflags;
695 struct ucred *a_cred;
696 struct proc *a_p;
697 } */ *ap;
698{
699
700 return (EINVAL);
701}
702
703int
704ufs_remove(ap)
705 struct vop_remove_args /* {
706 struct vnode *a_dvp;
707 struct vnode *a_vp;
708 struct componentname *a_cnp;
709 } */ *ap;
710{
711 struct inode *ip;
712 struct vnode *vp = ap->a_vp;
713 struct vnode *dvp = ap->a_dvp;
714 int error;
715
716 ip = VTOI(vp);
717 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
718 (VTOI(dvp)->i_flags & APPEND)) {
719 error = EPERM;
720 goto out;
721 }
722 error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
723 VN_KNOTE(vp, NOTE_DELETE);
724 VN_KNOTE(dvp, NOTE_WRITE);
725out:
726 return (error);
727}
728
729/*
730 * link vnode call
731 */
732int
733ufs_link(ap)
734 struct vop_link_args /* {
735 struct vnode *a_tdvp;
736 struct vnode *a_vp;
737 struct componentname *a_cnp;
738 } */ *ap;
739{
740 struct vnode *vp = ap->a_vp;
741 struct vnode *tdvp = ap->a_tdvp;
742 struct componentname *cnp = ap->a_cnp;
743 struct proc *p = cnp->cn_proc;
744 struct inode *ip;
745 struct direct newdir;
746 int error;
747
748#ifdef DIAGNOSTIC
749 if ((cnp->cn_flags & HASBUF) == 0)
750 panic("ufs_link: no name");
751#endif
752 if (tdvp->v_mount != vp->v_mount) {
753 error = EXDEV;
754 goto out2;
755 }
756 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, p))) {
757 goto out2;
758 }
759 ip = VTOI(vp);
760 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
761 error = EMLINK;
762 goto out1;
763 }
764 if (ip->i_flags & (IMMUTABLE | APPEND)) {
765 error = EPERM;
766 goto out1;
767 }
768 ip->i_effnlink++;
769 ip->i_nlink++;
770 ip->i_flag |= IN_CHANGE;
771 if (DOINGSOFTDEP(vp))
772 softdep_change_linkcnt(ip);
773 error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
774 if (!error) {
775 ufs_makedirentry(ip, cnp, &newdir);
776 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
777 }
778
779 if (error) {
780 ip->i_effnlink--;
781 ip->i_nlink--;
782 ip->i_flag |= IN_CHANGE;
783 if (DOINGSOFTDEP(vp))
784 softdep_change_linkcnt(ip);
785 }
786out1:
787 if (tdvp != vp)
788 VOP_UNLOCK(vp, 0, p);
789out2:
790 VN_KNOTE(vp, NOTE_LINK);
791 VN_KNOTE(tdvp, NOTE_WRITE);
792 return (error);
793}
794
795/*
796 * whiteout vnode call
797 */
798int
799ufs_whiteout(ap)
800 struct vop_whiteout_args /* {
801 struct vnode *a_dvp;
802 struct componentname *a_cnp;
803 int a_flags;
804 } */ *ap;
805{
806 struct vnode *dvp = ap->a_dvp;
807 struct componentname *cnp = ap->a_cnp;
808 struct direct newdir;
809 int error = 0;
810
811 switch (ap->a_flags) {
812 case LOOKUP:
813 /* 4.4 format directories support whiteout operations */
814 if (dvp->v_mount->mnt_maxsymlinklen > 0)
815 return (0);
816 return (EOPNOTSUPP);
817
818 case CREATE:
819 /* create a new directory whiteout */
820#ifdef DIAGNOSTIC
821 if ((cnp->cn_flags & SAVENAME) == 0)
822 panic("ufs_whiteout: missing name");
823 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
824 panic("ufs_whiteout: old format filesystem");
825#endif
826
827 newdir.d_ino = WINO;
828 newdir.d_namlen = cnp->cn_namelen;
829 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
830 newdir.d_type = DT_WHT;
831 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
832 break;
833
834 case DELETE:
835 /* remove an existing directory whiteout */
836#ifdef DIAGNOSTIC
837 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
838 panic("ufs_whiteout: old format filesystem");
839#endif
840
841 cnp->cn_flags &= ~DOWHITEOUT;
842 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
843 break;
844 default:
845 panic("ufs_whiteout: unknown op");
846 }
847 return (error);
848}
849
850/*
851 * Rename system call.
852 * rename("foo", "bar");
853 * is essentially
854 * unlink("bar");
855 * link("foo", "bar");
856 * unlink("foo");
857 * but ``atomically''. Can't do full commit without saving state in the
858 * inode on disk which isn't feasible at this time. Best we can do is
859 * always guarantee the target exists.
860 *
861 * Basic algorithm is:
862 *
863 * 1) Bump link count on source while we're linking it to the
864 * target. This also ensure the inode won't be deleted out
865 * from underneath us while we work (it may be truncated by
866 * a concurrent `trunc' or `open' for creation).
867 * 2) Link source to destination. If destination already exists,
868 * delete it first.
869 * 3) Unlink source reference to inode if still around. If a
870 * directory was moved and the parent of the destination
871 * is different from the source, patch the ".." entry in the
872 * directory.
873 */
874int
875ufs_rename(ap)
876 struct vop_rename_args /* {
877 struct vnode *a_fdvp;
878 struct vnode *a_fvp;
879 struct componentname *a_fcnp;
880 struct vnode *a_tdvp;
881 struct vnode *a_tvp;
882 struct componentname *a_tcnp;
883 } */ *ap;
884{
885 struct vnode *tvp = ap->a_tvp;
886 register struct vnode *tdvp = ap->a_tdvp;
887 struct vnode *fvp = ap->a_fvp;
888 struct vnode *fdvp = ap->a_fdvp;
889 struct componentname *tcnp = ap->a_tcnp;
890 struct componentname *fcnp = ap->a_fcnp;
891 struct proc *p = fcnp->cn_proc;
892 struct inode *ip, *xp, *dp;
893 struct direct newdir;
894 int doingdirectory = 0, oldparent = 0, newparent = 0;
895 int error = 0, ioflag;
896
897#ifdef DIAGNOSTIC
898 if ((tcnp->cn_flags & HASBUF) == 0 ||
899 (fcnp->cn_flags & HASBUF) == 0)
900 panic("ufs_rename: no name");
901#endif
902 /*
903 * Check for cross-device rename.
904 */
905 if ((fvp->v_mount != tdvp->v_mount) ||
906 (tvp && (fvp->v_mount != tvp->v_mount))) {
907 error = EXDEV;
908abortit:
909 if (tdvp == tvp)
910 vrele(tdvp);
911 else
912 vput(tdvp);
913 if (tvp)
914 vput(tvp);
915 vrele(fdvp);
916 vrele(fvp);
917 return (error);
918 }
919
920 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
921 (VTOI(tdvp)->i_flags & APPEND))) {
922 error = EPERM;
923 goto abortit;
924 }
925
926 /*
927 * Renaming a file to itself has no effect. The upper layers should
928 * not call us in that case. Temporarily just warn if they do.
929 */
930 if (fvp == tvp) {
931 printf("ufs_rename: fvp == tvp (can't happen)\n");
932 error = 0;
933 goto abortit;
934 }
935
936 if ((error = vn_lock(fvp, LK_EXCLUSIVE, p)) != 0)
937 goto abortit;
938 dp = VTOI(fdvp);
939 ip = VTOI(fvp);
940 if (ip->i_nlink >= LINK_MAX) {
941 VOP_UNLOCK(fvp, 0, p);
942 error = EMLINK;
943 goto abortit;
944 }
945 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
946 || (dp->i_flags & APPEND)) {
947 VOP_UNLOCK(fvp, 0, p);
948 error = EPERM;
949 goto abortit;
950 }
951 if ((ip->i_mode & IFMT) == IFDIR) {
952 /*
953 * Avoid ".", "..", and aliases of "." for obvious reasons.
954 */
955 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
956 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
957 (ip->i_flag & IN_RENAME)) {
958 VOP_UNLOCK(fvp, 0, p);
959 error = EINVAL;
960 goto abortit;
961 }
962 ip->i_flag |= IN_RENAME;
963 oldparent = dp->i_number;
964 doingdirectory = 1;
965 }
966 VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */
967 vrele(fdvp);
968
969 /*
970 * When the target exists, both the directory
971 * and target vnodes are returned locked.
972 */
973 dp = VTOI(tdvp);
974 xp = NULL;
975 if (tvp)
976 xp = VTOI(tvp);
977
978 /*
979 * 1) Bump link count while we're moving stuff
980 * around. If we crash somewhere before
981 * completing our work, the link count
982 * may be wrong, but correctable.
983 */
984 ip->i_effnlink++;
985 ip->i_nlink++;
986 ip->i_flag |= IN_CHANGE;
987 if (DOINGSOFTDEP(fvp))
988 softdep_change_linkcnt(ip);
989 if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
990 DOINGASYNC(fvp)))) != 0) {
991 VOP_UNLOCK(fvp, 0, p);
992 goto bad;
993 }
994
995 /*
996 * If ".." must be changed (ie the directory gets a new
997 * parent) then the source directory must not be in the
998 * directory heirarchy above the target, as this would
999 * orphan everything below the source directory. Also
1000 * the user must have write permission in the source so
1001 * as to be able to change "..". We must repeat the call
1002 * to namei, as the parent directory is unlocked by the
1003 * call to checkpath().
1004 */
1005 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
1006 VOP_UNLOCK(fvp, 0, p);
1007 if (oldparent != dp->i_number)
1008 newparent = dp->i_number;
1009 if (doingdirectory && newparent) {
1010 if (error) /* write access check above */
1011 goto bad;
1012 if (xp != NULL)
1013 vput(tvp);
1014 error = ufs_checkpath(ip, dp, tcnp->cn_cred);
1015 if (error)
1016 goto out;
1017 if ((tcnp->cn_flags & SAVESTART) == 0)
1018 panic("ufs_rename: lost to startdir");
1019 VREF(tdvp);
1020 error = relookup(tdvp, &tvp, tcnp);
1021 if (error)
1022 goto out;
1023 vrele(tdvp);
1024 dp = VTOI(tdvp);
1025 xp = NULL;
1026 if (tvp)
1027 xp = VTOI(tvp);
1028 }
1029 /*
1030 * 2) If target doesn't exist, link the target
1031 * to the source and unlink the source.
1032 * Otherwise, rewrite the target directory
1033 * entry to reference the source inode and
1034 * expunge the original entry's existence.
1035 */
1036 if (xp == NULL) {
1037 if (dp->i_dev != ip->i_dev)
1038 panic("ufs_rename: EXDEV");
1039 /*
1040 * Account for ".." in new directory.
1041 * When source and destination have the same
1042 * parent we don't fool with the link count.
1043 */
1044 if (doingdirectory && newparent) {
1045 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1046 error = EMLINK;
1047 goto bad;
1048 }
1049 dp->i_effnlink++;
1050 dp->i_nlink++;
1051 dp->i_flag |= IN_CHANGE;
1052 if (DOINGSOFTDEP(tdvp))
1053 softdep_change_linkcnt(dp);
1054 error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
1055 DOINGASYNC(tdvp)));
1056 if (error)
1057 goto bad;
1058 }
1059 ufs_makedirentry(ip, tcnp, &newdir);
1060 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
1061 if (error) {
1062 if (doingdirectory && newparent) {
1063 dp->i_effnlink--;
1064 dp->i_nlink--;
1065 dp->i_flag |= IN_CHANGE;
1066 if (DOINGSOFTDEP(tdvp))
1067 softdep_change_linkcnt(dp);
1068 (void)UFS_UPDATE(tdvp, 1);
1069 }
1070 goto bad;
1071 }
1072 VN_KNOTE(tdvp, NOTE_WRITE);
1073 vput(tdvp);
1074 } else {
1075 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1076 panic("ufs_rename: EXDEV");
1077 /*
1078 * Short circuit rename(foo, foo).
1079 */
1080 if (xp->i_number == ip->i_number)
1081 panic("ufs_rename: same file");
1082 /*
1083 * If the parent directory is "sticky", then the user must
1084 * own the parent directory, or the destination of the rename,
1085 * otherwise the destination may not be changed (except by
1086 * root). This implements append-only directories.
1087 */
1088 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
1089 tcnp->cn_cred->cr_uid != dp->i_uid &&
1090 xp->i_uid != tcnp->cn_cred->cr_uid) {
1091 error = EPERM;
1092 goto bad;
1093 }
1094 /*
1095 * Target must be empty if a directory and have no links
1096 * to it. Also, ensure source and target are compatible
1097 * (both directories, or both not directories).
1098 */
1099 if ((xp->i_mode&IFMT) == IFDIR) {
1100 if ((xp->i_effnlink > 2) ||
1101 !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
1102 error = ENOTEMPTY;
1103 goto bad;
1104 }
1105 if (!doingdirectory) {
1106 error = ENOTDIR;
1107 goto bad;
1108 }
1109 cache_purge(tdvp);
1110 } else if (doingdirectory) {
1111 error = EISDIR;
1112 goto bad;
1113 }
1114 error = ufs_dirrewrite(dp, xp, ip->i_number,
1115 IFTODT(ip->i_mode),
1116 (doingdirectory && newparent) ? newparent : doingdirectory);
1117 if (error)
1118 goto bad;
1119 if (doingdirectory) {
1120 if (!newparent) {
1121 dp->i_effnlink--;
1122 if (DOINGSOFTDEP(tdvp))
1123 softdep_change_linkcnt(dp);
1124 }
1125 xp->i_effnlink--;
1126 if (DOINGSOFTDEP(tvp))
1127 softdep_change_linkcnt(xp);
1128 }
1129 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
1130 /*
1131 * Truncate inode. The only stuff left in the directory
1132 * is "." and "..". The "." reference is inconsequential
1133 * since we are quashing it. We have removed the "."
1134 * reference and the reference in the parent directory,
1135 * but there may be other hard links. The soft
1136 * dependency code will arrange to do these operations
1137 * after the parent directory entry has been deleted on
1138 * disk, so when running with that code we avoid doing
1139 * them now.
1140 */
1141 if (!newparent) {
1142 dp->i_nlink--;
1143 dp->i_flag |= IN_CHANGE;
1144 }
1145 xp->i_nlink--;
1146 xp->i_flag |= IN_CHANGE;
1147 ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC;
1148 if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
1149 tcnp->cn_cred, tcnp->cn_proc)) != 0)
1150 goto bad;
1151 }
1152 VN_KNOTE(tdvp, NOTE_WRITE);
1153 vput(tdvp);
1154 VN_KNOTE(tvp, NOTE_DELETE);
1155 vput(tvp);
1156 xp = NULL;
1157 }
1158
1159 /*
1160 * 3) Unlink the source.
1161 */
1162 fcnp->cn_flags &= ~MODMASK;
1163 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1164 if ((fcnp->cn_flags & SAVESTART) == 0)
1165 panic("ufs_rename: lost from startdir");
1166 VREF(fdvp);
1167 error = relookup(fdvp, &fvp, fcnp);
1168 if (error == 0)
1169 vrele(fdvp);
1170 if (fvp != NULL) {
1171 xp = VTOI(fvp);
1172 dp = VTOI(fdvp);
1173 } else {
1174 /*
1175 * From name has disappeared.
1176 */
1177 if (doingdirectory)
1178 panic("ufs_rename: lost dir entry");
1179 vrele(ap->a_fvp);
1180 return (0);
1181 }
1182 /*
1183 * Ensure that the directory entry still exists and has not
1184 * changed while the new name has been entered. If the source is
1185 * a file then the entry may have been unlinked or renamed. In
1186 * either case there is no further work to be done. If the source
1187 * is a directory then it cannot have been rmdir'ed; the IN_RENAME
1188 * flag ensures that it cannot be moved by another rename or removed
1189 * by a rmdir.
1190 */
1191 if (xp != ip) {
1192 if (doingdirectory)
1193 panic("ufs_rename: lost dir entry");
1194 } else {
1195 /*
1196 * If the source is a directory with a
1197 * new parent, the link count of the old
1198 * parent directory must be decremented
1199 * and ".." set to point to the new parent.
1200 */
1201 if (doingdirectory && newparent) {
1202 xp->i_offset = mastertemplate.dot_reclen;
1203 ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
1204 cache_purge(fdvp);
1205 }
1206 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
1207 xp->i_flag &= ~IN_RENAME;
1208 }
1209 VN_KNOTE(fvp, NOTE_RENAME);
1210 if (dp)
1211 vput(fdvp);
1212 if (xp)
1213 vput(fvp);
1214 vrele(ap->a_fvp);
1215 return (error);
1216
1217bad:
1218 if (xp)
1219 vput(ITOV(xp));
1220 vput(ITOV(dp));
1221out:
1222 if (doingdirectory)
1223 ip->i_flag &= ~IN_RENAME;
1224 if (vn_lock(fvp, LK_EXCLUSIVE, p) == 0) {
1225 ip->i_effnlink--;
1226 ip->i_nlink--;
1227 ip->i_flag |= IN_CHANGE;
1228 ip->i_flag &= ~IN_RENAME;
1229 if (DOINGSOFTDEP(fvp))
1230 softdep_change_linkcnt(ip);
1231 vput(fvp);
1232 } else
1233 vrele(fvp);
1234 return (error);
1235}
1236
1237/*
1238 * Mkdir system call
1239 */
1240int
1241ufs_mkdir(ap)
1242 struct vop_mkdir_args /* {
1243 struct vnode *a_dvp;
1244 struct vnode **a_vpp;
1245 struct componentname *a_cnp;
1246 struct vattr *a_vap;
1247 } */ *ap;
1248{
1249 register struct vnode *dvp = ap->a_dvp;
1250 register struct vattr *vap = ap->a_vap;
1251 register struct componentname *cnp = ap->a_cnp;
1252 register struct inode *ip, *dp;
1253 struct vnode *tvp;
1254 struct buf *bp;
1255 struct dirtemplate dirtemplate, *dtp;
1256 struct direct newdir;
1257 int error, dmode;
1258 long blkoff;
1259
1260#ifdef DIAGNOSTIC
1261 if ((cnp->cn_flags & HASBUF) == 0)
1262 panic("ufs_mkdir: no name");
1263#endif
1264 dp = VTOI(dvp);
1265 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1266 error = EMLINK;
1267 goto out;
1268 }
1269 dmode = vap->va_mode & 0777;
1270 dmode |= IFDIR;
1271 /*
1272 * Must simulate part of ufs_makeinode here to acquire the inode,
1273 * but not have it entered in the parent directory. The entry is
1274 * made later after writing "." and ".." entries.
1275 */
1276 error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
1277 if (error)
1278 goto out;
1279 ip = VTOI(tvp);
1280 ip->i_gid = dp->i_gid;
1281#ifdef SUIDDIR
1282 {
1283#ifdef QUOTA
1284 struct ucred ucred, *ucp;
1285 ucp = cnp->cn_cred;
1286#endif
1287 /*
1288 * If we are hacking owners here, (only do this where told to)
1289 * and we are not giving it TO root, (would subvert quotas)
1290 * then go ahead and give it to the other user.
1291 * The new directory also inherits the SUID bit.
1292 * If user's UID and dir UID are the same,
1293 * 'give it away' so that the SUID is still forced on.
1294 */
1295 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1296 (dp->i_mode & ISUID) && dp->i_uid) {
1297 dmode |= ISUID;
1298 ip->i_uid = dp->i_uid;
1299#ifdef QUOTA
1300 if (dp->i_uid != cnp->cn_cred->cr_uid) {
1301 /*
1302 * Make sure the correct user gets charged
1303 * for the space.
1304 * Make a dummy credential for the victim.
1305 * XXX This seems to never be accessed out of
1306 * our context so a stack variable is ok.
1307 */
1308 ucred.cr_ref = 1;
1309 ucred.cr_uid = ip->i_uid;
1310 ucred.cr_ngroups = 1;
1311 ucred.cr_groups[0] = dp->i_gid;
1312 ucp = &ucred;
1313 }
1314#endif
1315 } else
1316 ip->i_uid = cnp->cn_cred->cr_uid;
1317#ifdef QUOTA
1318 if ((error = getinoquota(ip)) ||
1319 (error = chkiq(ip, 1, ucp, 0))) {
1320 UFS_VFREE(tvp, ip->i_number, dmode);
1321 vput(tvp);
1322 return (error);
1323 }
1324#endif
1325 }
1326#else /* !SUIDDIR */
1327 ip->i_uid = cnp->cn_cred->cr_uid;
1328#ifdef QUOTA
1329 if ((error = getinoquota(ip)) ||
1330 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
1331 UFS_VFREE(tvp, ip->i_number, dmode);
1332 vput(tvp);
1333 return (error);
1334 }
1335#endif
1336#endif /* !SUIDDIR */
1337 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1338 ip->i_mode = dmode;
1339 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1340 ip->i_effnlink = 2;
1341 ip->i_nlink = 2;
1342 if (DOINGSOFTDEP(tvp))
1343 softdep_change_linkcnt(ip);
1344 if (cnp->cn_flags & ISWHITEOUT)
1345 ip->i_flags |= UF_OPAQUE;
1346
1347 /*
1348 * Bump link count in parent directory to reflect work done below.
1349 * Should be done before reference is created so cleanup is
1350 * possible if we crash.
1351 */
1352 dp->i_effnlink++;
1353 dp->i_nlink++;
1354 dp->i_flag |= IN_CHANGE;
1355 if (DOINGSOFTDEP(dvp))
1356 softdep_change_linkcnt(dp);
1357 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
1358 if (error)
1359 goto bad;
1360
1361 /*
1362 * Initialize directory with "." and ".." from static template.
1363 */
1364 if (dvp->v_mount->mnt_maxsymlinklen > 0
1365 )
1366 dtp = &mastertemplate;
1367 else
1368 dtp = (struct dirtemplate *)&omastertemplate;
1369 dirtemplate = *dtp;
1370 dirtemplate.dot_ino = ip->i_number;
1371 dirtemplate.dotdot_ino = dp->i_number;
1372 if ((error = VOP_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
1373 B_CLRBUF, &bp)) != 0)
1374 goto bad;
1375 ip->i_size = DIRBLKSIZ;
1376 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1377 vnode_pager_setsize(tvp, (u_long)ip->i_size);
1378 bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
1379 if (DOINGSOFTDEP(tvp)) {
1380 /*
1381 * Ensure that the entire newly allocated block is a
1382 * valid directory so that future growth within the
1383 * block does not have to ensure that the block is
1384 * written before the inode.
1385 */
1386 blkoff = DIRBLKSIZ;
1387 while (blkoff < bp->b_bcount) {
1388 ((struct direct *)
1389 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
1390 blkoff += DIRBLKSIZ;
1391 }
1392 }
1393 if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
1394 DOINGASYNC(tvp)))) != 0) {
1395 (void)VOP_BWRITE(bp->b_vp, bp);
1396 goto bad;
1397 }
1398 /*
1399 * Directory set up, now install its entry in the parent directory.
1400 *
1401 * If we are not doing soft dependencies, then we must write out the
1402 * buffer containing the new directory body before entering the new
1403 * name in the parent. If we are doing soft dependencies, then the
1404 * buffer containing the new directory body will be passed to and
1405 * released in the soft dependency code after the code has attached
1406 * an appropriate ordering dependency to the buffer which ensures that
1407 * the buffer is written before the new name is written in the parent.
1408 */
1409 if (DOINGASYNC(dvp))
1410 bdwrite(bp);
1411 else if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp->b_vp, bp))))
1412 goto bad;
1413 ufs_makedirentry(ip, cnp, &newdir);
1414 error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
1415
1416bad:
1417 if (error == 0) {
1418 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1419 *ap->a_vpp = tvp;
1420 } else {
1421 dp->i_effnlink--;
1422 dp->i_nlink--;
1423 dp->i_flag |= IN_CHANGE;
1424 if (DOINGSOFTDEP(dvp))
1425 softdep_change_linkcnt(dp);
1426 /*
1427 * No need to do an explicit VOP_TRUNCATE here, vrele will
1428 * do this for us because we set the link count to 0.
1429 */
1430 ip->i_effnlink = 0;
1431 ip->i_nlink = 0;
1432 ip->i_flag |= IN_CHANGE;
1433 if (DOINGSOFTDEP(tvp))
1434 softdep_change_linkcnt(ip);
1435 vput(tvp);
1436 }
1437out:
1438 return (error);
1439}
1440
1441/*
1442 * Rmdir system call.
1443 */
1444int
1445ufs_rmdir(ap)
1446 struct vop_rmdir_args /* {
1447 struct vnode *a_dvp;
1448 struct vnode *a_vp;
1449 struct componentname *a_cnp;
1450 } */ *ap;
1451{
1452 struct vnode *vp = ap->a_vp;
1453 struct vnode *dvp = ap->a_dvp;
1454 struct componentname *cnp = ap->a_cnp;
1455 struct inode *ip, *dp;
1456 int error, ioflag;
1457
1458 ip = VTOI(vp);
1459 dp = VTOI(dvp);
1460
1461 /*
1462 * Do not remove a directory that is in the process of being renamed.
1463 * Verify the directory is empty (and valid). Rmdir ".." will not be
1464 * valid since ".." will contain a reference to the current directory
1465 * and thus be non-empty. Do not allow the removal of mounted on
1466 * directories (this can happen when an NFS exported filesystem
1467 * tries to remove a locally mounted on directory).
1468 */
1469 error = 0;
1470 if (ip->i_flag & IN_RENAME) {
1471 error = EINVAL;
1472 goto out;
1473 }
1474 if (ip->i_effnlink != 2 ||
1475 !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1476 error = ENOTEMPTY;
1477 goto out;
1478 }
1479 if ((dp->i_flags & APPEND)
1480 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1481 error = EPERM;
1482 goto out;
1483 }
1484 if (vp->v_mountedhere != 0) {
1485 error = EINVAL;
1486 goto out;
1487 }
1488 /*
1489 * Delete reference to directory before purging
1490 * inode. If we crash in between, the directory
1491 * will be reattached to lost+found,
1492 */
1493 dp->i_effnlink--;
1494 ip->i_effnlink--;
1495 if (DOINGSOFTDEP(vp)) {
1496 softdep_change_linkcnt(dp);
1497 softdep_change_linkcnt(ip);
1498 }
1499 error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
1500 if (error) {
1501 dp->i_effnlink++;
1502 ip->i_effnlink++;
1503 if (DOINGSOFTDEP(vp)) {
1504 softdep_change_linkcnt(dp);
1505 softdep_change_linkcnt(ip);
1506 }
1507 goto out;
1508 }
1509 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1510 cache_purge(dvp);
1511 /*
1512 * Truncate inode. The only stuff left in the directory is "." and
1513 * "..". The "." reference is inconsequential since we are quashing
1514 * it. The soft dependency code will arrange to do these operations
1515 * after the parent directory entry has been deleted on disk, so
1516 * when running with that code we avoid doing them now.
1517 */
1518 if (!DOINGSOFTDEP(vp)) {
1519 dp->i_nlink--;
1520 dp->i_flag |= IN_CHANGE;
1521 ip->i_nlink--;
1522 ip->i_flag |= IN_CHANGE;
1523 ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC;
1524 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
1525 cnp->cn_proc);
1526 }
1527 cache_purge(vp);
1528#ifdef UFS_DIRHASH
1529 /* Kill any active hash; i_effnlink == 0, so it will not come back. */
1530 if (ip->i_dirhash != NULL)
1531 ufsdirhash_free(ip);
1532#endif
1533out:
1534 VN_KNOTE(vp, NOTE_DELETE);
1535 return (error);
1536}
1537
1538/*
1539 * symlink -- make a symbolic link
1540 */
1541int
1542ufs_symlink(ap)
1543 struct vop_symlink_args /* {
1544 struct vnode *a_dvp;
1545 struct vnode **a_vpp;
1546 struct componentname *a_cnp;
1547 struct vattr *a_vap;
1548 char *a_target;
1549 } */ *ap;
1550{
1551 register struct vnode *vp, **vpp = ap->a_vpp;
1552 register struct inode *ip;
1553 int len, error;
1554
1555 error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1556 vpp, ap->a_cnp);
1557 if (error)
1558 return (error);
1559 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1560 vp = *vpp;
1561 len = strlen(ap->a_target);
1562 if (len < vp->v_mount->mnt_maxsymlinklen) {
1563 ip = VTOI(vp);
1564 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1565 ip->i_size = len;
1566 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1567 } else
1568 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1569 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0,
1570 (struct proc *)0);
1571 if (error)
1572 vput(vp);
1573 return (error);
1574}
1575
1576/*
1577 * Vnode op for reading directories.
1578 *
1579 * The routine below assumes that the on-disk format of a directory
1580 * is the same as that defined by <sys/dirent.h>. If the on-disk
1581 * format changes, then it will be necessary to do a conversion
1582 * from the on-disk format that read returns to the format defined
1583 * by <sys/dirent.h>.
1584 */
1585int
1586ufs_readdir(ap)
1587 struct vop_readdir_args /* {
1588 struct vnode *a_vp;
1589 struct uio *a_uio;
1590 struct ucred *a_cred;
1591 int *a_eofflag;
1592 int *ncookies;
1593 u_long **a_cookies;
1594 } */ *ap;
1595{
1596 register struct uio *uio = ap->a_uio;
1597 int error;
1598 size_t count, lost;
1599 off_t off;
1600
1601 if (ap->a_ncookies != NULL)
1602 /*
1603 * Ensure that the block is aligned. The caller can use
1604 * the cookies to determine where in the block to start.
1605 */
1606 uio->uio_offset &= ~(DIRBLKSIZ - 1);
1607 off = uio->uio_offset;
1608 count = uio->uio_resid;
1609 /* Make sure we don't return partial entries. */
1610 if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
1611 return (EINVAL);
1612 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
1613 lost = uio->uio_resid - count;
1614 uio->uio_resid = count;
1615 uio->uio_iov->iov_len = count;
1616# if (BYTE_ORDER == LITTLE_ENDIAN)
1617 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
1618 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1619 } else {
1620 struct dirent *dp, *edp;
1621 struct uio auio;
1622 struct iovec aiov;
1623 caddr_t dirbuf;
1624 int readcnt;
1625 u_char tmp;
1626
1627 auio = *uio;
1628 auio.uio_iov = &aiov;
1629 auio.uio_iovcnt = 1;
1630 auio.uio_segflg = UIO_SYSSPACE;
1631 aiov.iov_len = count;
1632 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
1633 aiov.iov_base = dirbuf;
1634 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
1635 if (error == 0) {
1636 readcnt = count - auio.uio_resid;
1637 edp = (struct dirent *)&dirbuf[readcnt];
1638 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
1639 tmp = dp->d_namlen;
1640 dp->d_namlen = dp->d_type;
1641 dp->d_type = tmp;
1642 if (dp->d_reclen > 0) {
1643 dp = (struct dirent *)
1644 ((char *)dp + dp->d_reclen);
1645 } else {
1646 error = EIO;
1647 break;
1648 }
1649 }
1650 if (dp >= edp)
1651 error = uiomove(dirbuf, readcnt, uio);
1652 }
1653 FREE(dirbuf, M_TEMP);
1654 }
1655# else
1656 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
1657# endif
1658 if (!error && ap->a_ncookies != NULL) {
1659 struct dirent* dpStart;
1660 struct dirent* dpEnd;
1661 struct dirent* dp;
1662 int ncookies;
1663 u_long *cookies;
1664 u_long *cookiep;
1665
1666 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
1667 panic("ufs_readdir: unexpected uio from NFS server");
1668 dpStart = (struct dirent *)
1669 (uio->uio_iov->iov_base - (uio->uio_offset - off));
1670 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
1671 for (dp = dpStart, ncookies = 0;
1672 dp < dpEnd;
1673 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
1674 ncookies++;
1675 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1676 M_WAITOK);
1677 for (dp = dpStart, cookiep = cookies;
1678 dp < dpEnd;
1679 dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
1680 off += dp->d_reclen;
1681 *cookiep++ = (u_long) off;
1682 }
1683 *ap->a_ncookies = ncookies;
1684 *ap->a_cookies = cookies;
1685 }
1686 uio->uio_resid += lost;
1687 if (ap->a_eofflag)
1688 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
1689 return (error);
1690}
1691
1692/*
1693 * Return target name of a symbolic link
1694 */
1695int
1696ufs_readlink(ap)
1697 struct vop_readlink_args /* {
1698 struct vnode *a_vp;
1699 struct uio *a_uio;
1700 struct ucred *a_cred;
1701 } */ *ap;
1702{
1703 register struct vnode *vp = ap->a_vp;
1704 register struct inode *ip = VTOI(vp);
1705 int isize;
1706
1707 isize = ip->i_size;
1708 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1709 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1710 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1711 return (0);
1712 }
1713 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1714}
1715
1716/*
1717 * Calculate the logical to physical mapping if not done already,
1718 * then call the device strategy routine.
1719 *
1720 * In order to be able to swap to a file, the VOP_BMAP operation may not
1721 * deadlock on memory. See ufs_bmap() for details.
1722 */
1723int
1724ufs_strategy(ap)
1725 struct vop_strategy_args /* {
1726 struct vnode *a_vp;
1727 struct buf *a_bp;
1728 } */ *ap;
1729{
1730 register struct buf *bp = ap->a_bp;
1731 register struct vnode *vp = ap->a_vp;
1732 register struct inode *ip;
1733 int error;
1734
1735 ip = VTOI(vp);
1736 if (vp->v_type == VBLK || vp->v_type == VCHR)
1737 panic("ufs_strategy: spec");
1738 if (bp->b_blkno == bp->b_lblkno) {
1739 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1740 if (error) {
1741 bp->b_error = error;
1742 bp->b_flags |= B_ERROR;
1743 biodone(bp);
1744 return (error);
1745 }
1746 if ((long)bp->b_blkno == -1)
1747 vfs_bio_clrbuf(bp);
1748 }
1749 if ((long)bp->b_blkno == -1) {
1750 biodone(bp);
1751 return (0);
1752 }
1753 vp = ip->i_devvp;
1754 bp->b_dev = vp->v_rdev;
1755 VOP_STRATEGY(vp, bp);
1756 return (0);
1757}
1758
1759/*
1760 * Print out the contents of an inode.
1761 */
1762int
1763ufs_print(ap)
1764 struct vop_print_args /* {
1765 struct vnode *a_vp;
1766 } */ *ap;
1767{
1768 register struct vnode *vp = ap->a_vp;
1769 register struct inode *ip = VTOI(vp);
1770
1771 printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)",
1772 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1773 minor(ip->i_dev));
1774 if (vp->v_type == VFIFO)
1775 fifo_printinfo(vp);
1776 lockmgr_printinfo(&ip->i_lock);
1777 printf("\n");
1778 return (0);
1779}
1780
1781/*
1782 * Read wrapper for special devices.
1783 */
1784int
1785ufsspec_read(ap)
1786 struct vop_read_args /* {
1787 struct vnode *a_vp;
1788 struct uio *a_uio;
1789 int a_ioflag;
1790 struct ucred *a_cred;
1791 } */ *ap;
1792{
1793 int error, resid;
1794 struct inode *ip;
1795 struct uio *uio;
1796
1797 uio = ap->a_uio;
1798 resid = uio->uio_resid;
1799 error = VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap);
1800 /*
1801 * The inode may have been revoked during the call, so it must not
1802 * be accessed blindly here or in the other wrapper functions.
1803 */
1804 ip = VTOI(ap->a_vp);
1805 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1806 ip->i_flag |= IN_ACCESS;
1807 return (error);
1808}
1809
1810/*
1811 * Write wrapper for special devices.
1812 */
1813int
1814ufsspec_write(ap)
1815 struct vop_write_args /* {
1816 struct vnode *a_vp;
1817 struct uio *a_uio;
1818 int a_ioflag;
1819 struct ucred *a_cred;
1820 } */ *ap;
1821{
1822 int error, resid;
1823 struct inode *ip;
1824 struct uio *uio;
1825
1826 uio = ap->a_uio;
1827 resid = uio->uio_resid;
1828 error = VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap);
1829 ip = VTOI(ap->a_vp);
1830 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1831 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1832 return (error);
1833}
1834
1835/*
1836 * Close wrapper for special devices.
1837 *
1838 * Update the times on the inode then do device close.
1839 */
1840int
1841ufsspec_close(ap)
1842 struct vop_close_args /* {
1843 struct vnode *a_vp;
1844 int a_fflag;
1845 struct ucred *a_cred;
1846 struct proc *a_p;
1847 } */ *ap;
1848{
1849 struct vnode *vp = ap->a_vp;
1850
1851 simple_lock(&vp->v_interlock);
1852 if (vp->v_usecount > 1)
1853 ufs_itimes(vp);
1854 simple_unlock(&vp->v_interlock);
1855 return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
1856}
1857
1858/*
1859 * Read wrapper for fifos.
1860 */
1861int
1862ufsfifo_read(ap)
1863 struct vop_read_args /* {
1864 struct vnode *a_vp;
1865 struct uio *a_uio;
1866 int a_ioflag;
1867 struct ucred *a_cred;
1868 } */ *ap;
1869{
1870 int error, resid;
1871 struct inode *ip;
1872 struct uio *uio;
1873
1874 uio = ap->a_uio;
1875 resid = uio->uio_resid;
1876 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap);
1877 ip = VTOI(ap->a_vp);
1878 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1879 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1880 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1881 return (error);
1882}
1883
1884/*
1885 * Write wrapper for fifos.
1886 */
1887int
1888ufsfifo_write(ap)
1889 struct vop_write_args /* {
1890 struct vnode *a_vp;
1891 struct uio *a_uio;
1892 int a_ioflag;
1893 struct ucred *a_cred;
1894 } */ *ap;
1895{
1896 int error, resid;
1897 struct inode *ip;
1898 struct uio *uio;
1899
1900 uio = ap->a_uio;
1901 resid = uio->uio_resid;
1902 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap);
1903 ip = VTOI(ap->a_vp);
1904 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1905 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1906 return (error);
1907}
1908
1909/*
1910 * Close wrapper for fifos.
1911 *
1912 * Update the times on the inode then do device close.
1913 */
1914int
1915ufsfifo_close(ap)
1916 struct vop_close_args /* {
1917 struct vnode *a_vp;
1918 int a_fflag;
1919 struct ucred *a_cred;
1920 struct proc *a_p;
1921 } */ *ap;
1922{
1923 struct vnode *vp = ap->a_vp;
1924
1925 simple_lock(&vp->v_interlock);
1926 if (vp->v_usecount > 1)
1927 ufs_itimes(vp);
1928 simple_unlock(&vp->v_interlock);
1929 return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
1930}
1931
1932/*
1933 * Kqfilter wrapper for fifos.
1934 *
1935 * Fall through to ufs kqfilter routines if needed
1936 */
1937int
1938ufsfifo_kqfilter(ap)
1939 struct vop_kqfilter_args *ap;
1940{
1941 int error;
1942
1943 error = VOCALL(fifo_vnodeop_p, VOFFSET(vop_kqfilter), ap);
1944 if (error)
1945 error = ufs_kqfilter(ap);
1946 return (error);
1947}
1948
1949/*
1950 * Return POSIX pathconf information applicable to ufs filesystems.
1951 */
1952int
1953ufs_pathconf(ap)
1954 struct vop_pathconf_args /* {
1955 struct vnode *a_vp;
1956 int a_name;
1957 int *a_retval;
1958 } */ *ap;
1959{
1960
1961 switch (ap->a_name) {
1962 case _PC_LINK_MAX:
1963 *ap->a_retval = LINK_MAX;
1964 return (0);
1965 case _PC_NAME_MAX:
1966 *ap->a_retval = NAME_MAX;
1967 return (0);
1968 case _PC_PATH_MAX:
1969 *ap->a_retval = PATH_MAX;
1970 return (0);
1971 case _PC_PIPE_BUF:
1972 *ap->a_retval = PIPE_BUF;
1973 return (0);
1974 case _PC_CHOWN_RESTRICTED:
1975 *ap->a_retval = 1;
1976 return (0);
1977 case _PC_NO_TRUNC:
1978 *ap->a_retval = 1;
1979 return (0);
1980 default:
1981 return (EINVAL);
1982 }
1983 /* NOTREACHED */
1984}
1985
1986/*
1987 * Advisory record locking support
1988 */
1989int
1990ufs_advlock(ap)
1991 struct vop_advlock_args /* {
1992 struct vnode *a_vp;
1993 caddr_t a_id;
1994 int a_op;
1995 struct flock *a_fl;
1996 int a_flags;
1997 } */ *ap;
1998{
1999 register struct inode *ip = VTOI(ap->a_vp);
2000
2001 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
2002}
2003
2004/*
2005 * Initialize the vnode associated with a new inode, handle aliased
2006 * vnodes.
2007 */
2008int
2009ufs_vinit(mntp, specops, fifoops, vpp)
2010 struct mount *mntp;
2011 vop_t **specops;
2012 vop_t **fifoops;
2013 struct vnode **vpp;
2014{
2015 struct inode *ip;
2016 struct vnode *vp;
2017 struct timeval tv;
2018
2019 vp = *vpp;
2020 ip = VTOI(vp);
2021 switch(vp->v_type = IFTOVT(ip->i_mode)) {
2022 case VCHR:
2023 case VBLK:
2024 vp->v_op = specops;
2025 addaliasu(vp, ip->i_rdev);
2026 break;
2027 case VFIFO:
2028 vp->v_op = fifoops;
2029 break;
2030 default:
2031 break;
2032
2033 }
2034 if (ip->i_number == ROOTINO)
2035 vp->v_flag |= VROOT;
2036 /*
2037 * Initialize modrev times
2038 */
2039 getmicrouptime(&tv);
2040 SETHIGH(ip->i_modrev, tv.tv_sec);
2041 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
2042 *vpp = vp;
2043 return (0);
2044}
2045
2046/*
2047 * Allocate a new inode.
2048 */
2049int
2050ufs_makeinode(mode, dvp, vpp, cnp)
2051 int mode;
2052 struct vnode *dvp;
2053 struct vnode **vpp;
2054 struct componentname *cnp;
2055{
2056 register struct inode *ip, *pdir;
2057 struct direct newdir;
2058 struct vnode *tvp;
2059 int error;
2060
2061 pdir = VTOI(dvp);
2062#ifdef DIAGNOSTIC
2063 if ((cnp->cn_flags & HASBUF) == 0)
2064 panic("ufs_makeinode: no name");
2065#endif
2066 *vpp = NULL;
2067 if ((mode & IFMT) == 0)
2068 mode |= IFREG;
2069
2070 error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
2071 if (error)
2072 return (error);
2073 ip = VTOI(tvp);
2074 ip->i_gid = pdir->i_gid;
2075#ifdef SUIDDIR
2076 {
2077#ifdef QUOTA
2078 struct ucred ucred, *ucp;
2079 ucp = cnp->cn_cred;
2080#endif
2081 /*
2082 * If we are not the owner of the directory,
2083 * and we are hacking owners here, (only do this where told to)
2084 * and we are not giving it TO root, (would subvert quotas)
2085 * then go ahead and give it to the other user.
2086 * Note that this drops off the execute bits for security.
2087 */
2088 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
2089 (pdir->i_mode & ISUID) &&
2090 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
2091 ip->i_uid = pdir->i_uid;
2092 mode &= ~07111;
2093#ifdef QUOTA
2094 /*
2095 * Make sure the correct user gets charged
2096 * for the space.
2097 * Quickly knock up a dummy credential for the victim.
2098 * XXX This seems to never be accessed out of our
2099 * context so a stack variable is ok.
2100 */
2101 ucred.cr_ref = 1;
2102 ucred.cr_uid = ip->i_uid;
2103 ucred.cr_ngroups = 1;
2104 ucred.cr_groups[0] = pdir->i_gid;
2105 ucp = &ucred;
2106#endif
2107 } else
2108 ip->i_uid = cnp->cn_cred->cr_uid;
2109
2110#ifdef QUOTA
2111 if ((error = getinoquota(ip)) ||
2112 (error = chkiq(ip, 1, ucp, 0))) {
2113 UFS_VFREE(tvp, ip->i_number, mode);
2114 vput(tvp);
2115 return (error);
2116 }
2117#endif
2118 }
2119#else /* !SUIDDIR */
2120 ip->i_uid = cnp->cn_cred->cr_uid;
2121#ifdef QUOTA
2122 if ((error = getinoquota(ip)) ||
2123 (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
2124 UFS_VFREE(tvp, ip->i_number, mode);
2125 vput(tvp);
2126 return (error);
2127 }
2128#endif
2129#endif /* !SUIDDIR */
2130 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
2131 ip->i_mode = mode;
2132 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
2133 ip->i_effnlink = 1;
2134 ip->i_nlink = 1;
2135 if (DOINGSOFTDEP(tvp))
2136 softdep_change_linkcnt(ip);
2137 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
41c20dac 2138 suser_xxx(cnp->cn_cred, 0)) {
984263bc 2139 ip->i_mode &= ~ISGID;
41c20dac 2140 }
984263bc
MD
2141
2142 if (cnp->cn_flags & ISWHITEOUT)
2143 ip->i_flags |= UF_OPAQUE;
2144
2145 /*
2146 * Make sure inode goes to disk before directory entry.
2147 */
2148 error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
2149 if (error)
2150 goto bad;
2151 ufs_makedirentry(ip, cnp, &newdir);
2152 error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
2153 if (error)
2154 goto bad;
2155 *vpp = tvp;
2156 return (0);
2157
2158bad:
2159 /*
2160 * Write error occurred trying to update the inode
2161 * or the directory so must deallocate the inode.
2162 */
2163 ip->i_effnlink = 0;
2164 ip->i_nlink = 0;
2165 ip->i_flag |= IN_CHANGE;
2166 if (DOINGSOFTDEP(tvp))
2167 softdep_change_linkcnt(ip);
2168 vput(tvp);
2169 return (error);
2170}
2171
2172static int
2173ufs_missingop(ap)
2174 struct vop_generic_args *ap;
2175{
2176
2177 panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name);
2178 return (EOPNOTSUPP);
2179}
2180
2181static struct filterops ufsread_filtops =
2182 { 1, NULL, filt_ufsdetach, filt_ufsread };
2183static struct filterops ufswrite_filtops =
2184 { 1, NULL, filt_ufsdetach, filt_ufswrite };
2185static struct filterops ufsvnode_filtops =
2186 { 1, NULL, filt_ufsdetach, filt_ufsvnode };
2187
2188static int
2189ufs_kqfilter(ap)
2190 struct vop_kqfilter_args /* {
2191 struct vnode *a_vp;
2192 struct knote *a_kn;
2193 } */ *ap;
2194{
2195 struct vnode *vp = ap->a_vp;
2196 struct knote *kn = ap->a_kn;
2197
2198 switch (kn->kn_filter) {
2199 case EVFILT_READ:
2200 kn->kn_fop = &ufsread_filtops;
2201 break;
2202 case EVFILT_WRITE:
2203 kn->kn_fop = &ufswrite_filtops;
2204 break;
2205 case EVFILT_VNODE:
2206 kn->kn_fop = &ufsvnode_filtops;
2207 break;
2208 default:
2209 return (1);
2210 }
2211
2212 kn->kn_hook = (caddr_t)vp;
2213
2214 simple_lock(&vp->v_pollinfo.vpi_lock);
2215 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
2216 simple_unlock(&vp->v_pollinfo.vpi_lock);
2217
2218 return (0);
2219}
2220
2221static void
2222filt_ufsdetach(struct knote *kn)
2223{
2224 struct vnode *vp = (struct vnode *)kn->kn_hook;
2225
2226 simple_lock(&vp->v_pollinfo.vpi_lock);
2227 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
2228 kn, knote, kn_selnext);
2229 simple_unlock(&vp->v_pollinfo.vpi_lock);
2230}
2231
2232/*ARGSUSED*/
2233static int
2234filt_ufsread(struct knote *kn, long hint)
2235{
2236 struct vnode *vp = (struct vnode *)kn->kn_hook;
2237 struct inode *ip = VTOI(vp);
2238
2239 /*
2240 * filesystem is gone, so set the EOF flag and schedule
2241 * the knote for deletion.
2242 */
2243 if (hint == NOTE_REVOKE) {
2244 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2245 return (1);
2246 }
2247
2248 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2249 return (kn->kn_data != 0);
2250}
2251
2252/*ARGSUSED*/
2253static int
2254filt_ufswrite(struct knote *kn, long hint)
2255{
2256
2257 /*
2258 * filesystem is gone, so set the EOF flag and schedule
2259 * the knote for deletion.
2260 */
2261 if (hint == NOTE_REVOKE)
2262 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2263
2264 kn->kn_data = 0;
2265 return (1);
2266}
2267
2268static int
2269filt_ufsvnode(struct knote *kn, long hint)
2270{
2271
2272 if (kn->kn_sfflags & hint)
2273 kn->kn_fflags |= hint;
2274 if (hint == NOTE_REVOKE) {
2275 kn->kn_flags |= EV_EOF;
2276 return (1);
2277 }
2278 return (kn->kn_fflags != 0);
2279}
2280
2281/* Global vfs data structures for ufs. */
2282static vop_t **ufs_vnodeop_p;
2283static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = {
2284 { &vop_default_desc, (vop_t *) vop_defaultop },
2285 { &vop_fsync_desc, (vop_t *) ufs_missingop },
2286 { &vop_read_desc, (vop_t *) ufs_missingop },
2287 { &vop_reallocblks_desc, (vop_t *) ufs_missingop },
2288 { &vop_write_desc, (vop_t *) ufs_missingop },
2289 { &vop_access_desc, (vop_t *) ufs_access },
2290 { &vop_advlock_desc, (vop_t *) ufs_advlock },
2291 { &vop_bmap_desc, (vop_t *) ufs_bmap },
2292 { &vop_cachedlookup_desc, (vop_t *) ufs_lookup },
2293 { &vop_close_desc, (vop_t *) ufs_close },
2294 { &vop_create_desc, (vop_t *) ufs_create },
2295 { &vop_getattr_desc, (vop_t *) ufs_getattr },
2296 { &vop_inactive_desc, (vop_t *) ufs_inactive },
2297 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
2298 { &vop_link_desc, (vop_t *) ufs_link },
2299 { &vop_lock_desc, (vop_t *) vop_stdlock },
2300 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup },
2301 { &vop_mkdir_desc, (vop_t *) ufs_mkdir },
2302 { &vop_mknod_desc, (vop_t *) ufs_mknod },
2303 { &vop_mmap_desc, (vop_t *) ufs_mmap },
2304 { &vop_open_desc, (vop_t *) ufs_open },
2305 { &vop_pathconf_desc, (vop_t *) ufs_pathconf },
2306 { &vop_poll_desc, (vop_t *) vop_stdpoll },
2307 { &vop_kqfilter_desc, (vop_t *) ufs_kqfilter },
2308 { &vop_print_desc, (vop_t *) ufs_print },
2309 { &vop_readdir_desc, (vop_t *) ufs_readdir },
2310 { &vop_readlink_desc, (vop_t *) ufs_readlink },
2311 { &vop_reclaim_desc, (vop_t *) ufs_reclaim },
2312 { &vop_remove_desc, (vop_t *) ufs_remove },
2313 { &vop_rename_desc, (vop_t *) ufs_rename },
2314 { &vop_rmdir_desc, (vop_t *) ufs_rmdir },
2315 { &vop_setattr_desc, (vop_t *) ufs_setattr },
2316 { &vop_strategy_desc, (vop_t *) ufs_strategy },
2317 { &vop_symlink_desc, (vop_t *) ufs_symlink },
2318 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
2319 { &vop_whiteout_desc, (vop_t *) ufs_whiteout },
2320 { NULL, NULL }
2321};
2322static struct vnodeopv_desc ufs_vnodeop_opv_desc =
2323 { &ufs_vnodeop_p, ufs_vnodeop_entries };
2324
2325static vop_t **ufs_specop_p;
2326static struct vnodeopv_entry_desc ufs_specop_entries[] = {
2327 { &vop_default_desc, (vop_t *) spec_vnoperate },
2328 { &vop_fsync_desc, (vop_t *) ufs_missingop },
2329 { &vop_access_desc, (vop_t *) ufs_access },
2330 { &vop_close_desc, (vop_t *) ufsspec_close },
2331 { &vop_getattr_desc, (vop_t *) ufs_getattr },
2332 { &vop_inactive_desc, (vop_t *) ufs_inactive },
2333 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
2334 { &vop_lock_desc, (vop_t *) vop_stdlock },
2335 { &vop_print_desc, (vop_t *) ufs_print },
2336 { &vop_read_desc, (vop_t *) ufsspec_read },
2337 { &vop_reclaim_desc, (vop_t *) ufs_reclaim },
2338 { &vop_setattr_desc, (vop_t *) ufs_setattr },
2339 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
2340 { &vop_write_desc, (vop_t *) ufsspec_write },
2341 { NULL, NULL }
2342};
2343static struct vnodeopv_desc ufs_specop_opv_desc =
2344 { &ufs_specop_p, ufs_specop_entries };
2345
2346static vop_t **ufs_fifoop_p;
2347static struct vnodeopv_entry_desc ufs_fifoop_entries[] = {
2348 { &vop_default_desc, (vop_t *) fifo_vnoperate },
2349 { &vop_fsync_desc, (vop_t *) ufs_missingop },
2350 { &vop_access_desc, (vop_t *) ufs_access },
2351 { &vop_close_desc, (vop_t *) ufsfifo_close },
2352 { &vop_getattr_desc, (vop_t *) ufs_getattr },
2353 { &vop_inactive_desc, (vop_t *) ufs_inactive },
2354 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
2355 { &vop_kqfilter_desc, (vop_t *) ufsfifo_kqfilter },
2356 { &vop_lock_desc, (vop_t *) vop_stdlock },
2357 { &vop_print_desc, (vop_t *) ufs_print },
2358 { &vop_read_desc, (vop_t *) ufsfifo_read },
2359 { &vop_reclaim_desc, (vop_t *) ufs_reclaim },
2360 { &vop_setattr_desc, (vop_t *) ufs_setattr },
2361 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
2362 { &vop_write_desc, (vop_t *) ufsfifo_write },
2363 { NULL, NULL }
2364};
2365static struct vnodeopv_desc ufs_fifoop_opv_desc =
2366 { &ufs_fifoop_p, ufs_fifoop_entries };
2367
2368VNODEOP_SET(ufs_vnodeop_opv_desc);
2369VNODEOP_SET(ufs_specop_opv_desc);
2370VNODEOP_SET(ufs_fifoop_opv_desc);
2371
2372int
2373ufs_vnoperate(ap)
2374 struct vop_generic_args /* {
2375 struct vnodeop_desc *a_desc;
2376 } */ *ap;
2377{
2378 return (VOCALL(ufs_vnodeop_p, ap->a_desc->vdesc_offset, ap));
2379}
2380
2381int
2382ufs_vnoperatefifo(ap)
2383 struct vop_generic_args /* {
2384 struct vnodeop_desc *a_desc;
2385 } */ *ap;
2386{
2387 return (VOCALL(ufs_fifoop_p, ap->a_desc->vdesc_offset, ap));
2388}
2389
2390int
2391ufs_vnoperatespec(ap)
2392 struct vop_generic_args /* {
2393 struct vnodeop_desc *a_desc;
2394 } */ *ap;
2395{
2396 return (VOCALL(ufs_specop_p, ap->a_desc->vdesc_offset, ap));
2397}