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