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