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