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