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