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