Sync with cat.c from netbsd-8
[minix3.git] / sys / ufs / lfs / ulfs_vnops.c
blobd26e29e7e0013c2fa09080d928de874d37a5e3a8
1 /* $NetBSD: ulfs_vnops.c,v 1.34 2015/09/21 01:24:23 dholland Exp $ */
2 /* from NetBSD: ufs_vnops.c,v 1.213 2013/06/08 05:47:02 kardel Exp */
4 /*-
5 * Copyright (c) 2008 The NetBSD Foundation, Inc.
6 * All rights reserved.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Wasabi Systems, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1982, 1986, 1989, 1993, 1995
35 * The Regents of the University of California. All rights reserved.
36 * (c) UNIX System Laboratories, Inc.
37 * All or some portions of this file are derived from material licensed
38 * to the University of California by American Telephone and Telegraph
39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 * the permission of UNIX System Laboratories, Inc.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
66 * @(#)ufs_vnops.c 8.28 (Berkeley) 7/31/95
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: ulfs_vnops.c,v 1.34 2015/09/21 01:24:23 dholland Exp $");
72 #if defined(_KERNEL_OPT)
73 #include "opt_lfs.h"
74 #include "opt_quota.h"
75 #endif
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/namei.h>
80 #include <sys/resourcevar.h>
81 #include <sys/kernel.h>
82 #include <sys/file.h>
83 #include <sys/stat.h>
84 #include <sys/buf.h>
85 #include <sys/proc.h>
86 #include <sys/mount.h>
87 #include <sys/vnode.h>
88 #include <sys/kmem.h>
89 #include <sys/malloc.h>
90 #include <sys/dirent.h>
91 #include <sys/lockf.h>
92 #include <sys/kauth.h>
93 #include <sys/wapbl.h>
94 #include <sys/fstrans.h>
96 #include <miscfs/specfs/specdev.h>
97 #include <miscfs/fifofs/fifo.h>
98 #include <miscfs/genfs/genfs.h>
100 #include <ufs/lfs/lfs_extern.h>
101 #include <ufs/lfs/lfs.h>
102 #include <ufs/lfs/lfs_accessors.h>
104 #include <ufs/lfs/ulfs_inode.h>
105 #include <ufs/lfs/ulfsmount.h>
106 #include <ufs/lfs/ulfs_bswap.h>
107 #include <ufs/lfs/ulfs_extern.h>
108 #ifdef LFS_DIRHASH
109 #include <ufs/lfs/ulfs_dirhash.h>
110 #endif
112 #include <uvm/uvm.h>
114 static int ulfs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *);
115 static int ulfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
116 struct lwp *);
119 * Open called.
121 * Nothing to do.
123 /* ARGSUSED */
125 ulfs_open(void *v)
127 struct vop_open_args /* {
128 struct vnode *a_vp;
129 int a_mode;
130 kauth_cred_t a_cred;
131 } */ *ap = v;
134 * Files marked append-only must be opened for appending.
136 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
137 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
138 return (EPERM);
139 return (0);
142 static int
143 ulfs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode,
144 kauth_cred_t cred)
146 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
147 int error;
148 #endif
151 * Disallow write attempts on read-only file systems;
152 * unless the file is a socket, fifo, or a block or
153 * character device resident on the file system.
155 if (mode & VWRITE) {
156 switch (vp->v_type) {
157 case VDIR:
158 case VLNK:
159 case VREG:
160 if (vp->v_mount->mnt_flag & MNT_RDONLY)
161 return (EROFS);
162 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
163 fstrans_start(vp->v_mount, FSTRANS_SHARED);
164 error = lfs_chkdq(ip, 0, cred, 0);
165 fstrans_done(vp->v_mount);
166 if (error != 0)
167 return error;
168 #endif
169 break;
170 case VBAD:
171 case VBLK:
172 case VCHR:
173 case VSOCK:
174 case VFIFO:
175 case VNON:
176 default:
177 break;
181 /* If it is a snapshot, nobody gets access to it. */
182 if ((ip->i_flags & SF_SNAPSHOT))
183 return (EPERM);
184 /* If immutable bit set, nobody gets to write it. */
185 if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE))
186 return (EPERM);
188 return 0;
191 static int
192 ulfs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode,
193 kauth_cred_t cred)
196 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type,
197 ip->i_mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type,
198 ip->i_mode & ALLPERMS, ip->i_uid, ip->i_gid, mode, cred));
202 ulfs_access(void *v)
204 struct vop_access_args /* {
205 struct vnode *a_vp;
206 int a_mode;
207 kauth_cred_t a_cred;
208 } */ *ap = v;
209 struct vnode *vp;
210 struct inode *ip;
211 mode_t mode;
212 int error;
214 vp = ap->a_vp;
215 ip = VTOI(vp);
216 mode = ap->a_mode;
218 error = ulfs_check_possible(vp, ip, mode, ap->a_cred);
219 if (error)
220 return error;
222 error = ulfs_check_permitted(vp, ip, mode, ap->a_cred);
224 return error;
228 * Set attribute vnode op. called from several syscalls
231 ulfs_setattr(void *v)
233 struct vop_setattr_args /* {
234 struct vnode *a_vp;
235 struct vattr *a_vap;
236 kauth_cred_t a_cred;
237 } */ *ap = v;
238 struct vattr *vap;
239 struct vnode *vp;
240 struct inode *ip;
241 struct lfs *fs;
242 kauth_cred_t cred;
243 struct lwp *l;
244 int error;
245 kauth_action_t action;
246 bool changing_sysflags;
248 vap = ap->a_vap;
249 vp = ap->a_vp;
250 ip = VTOI(vp);
251 fs = ip->i_lfs;
252 cred = ap->a_cred;
253 l = curlwp;
254 action = KAUTH_VNODE_WRITE_FLAGS;
255 changing_sysflags = false;
258 * Check for unsettable attributes.
260 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
261 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
262 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
263 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
264 return (EINVAL);
267 fstrans_start(vp->v_mount, FSTRANS_SHARED);
269 if (vap->va_flags != VNOVAL) {
270 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
271 error = EROFS;
272 goto out;
275 /* Snapshot flag cannot be set or cleared */
276 if ((vap->va_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) !=
277 (ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))) {
278 error = EPERM;
279 goto out;
282 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
283 action |= KAUTH_VNODE_HAS_SYSFLAGS;
286 if ((vap->va_flags & SF_SETTABLE) != (ip->i_flags & SF_SETTABLE)) {
287 action |= KAUTH_VNODE_WRITE_SYSFLAGS;
288 changing_sysflags = true;
291 error = kauth_authorize_vnode(cred, action, vp, NULL,
292 genfs_can_chflags(cred, vp->v_type, ip->i_uid,
293 changing_sysflags));
294 if (error)
295 goto out;
297 if (changing_sysflags) {
298 ip->i_flags = vap->va_flags;
299 DIP_ASSIGN(ip, flags, ip->i_flags);
300 } else {
301 ip->i_flags &= SF_SETTABLE;
302 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
303 DIP_ASSIGN(ip, flags, ip->i_flags);
305 ip->i_flag |= IN_CHANGE;
306 if (vap->va_flags & (IMMUTABLE | APPEND)) {
307 error = 0;
308 goto out;
311 if (ip->i_flags & (IMMUTABLE | APPEND)) {
312 error = EPERM;
313 goto out;
316 * Go through the fields and update iff not VNOVAL.
318 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
319 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
320 error = EROFS;
321 goto out;
323 error = ulfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
324 if (error)
325 goto out;
327 if (vap->va_size != VNOVAL) {
329 * Disallow write attempts on read-only file systems;
330 * unless the file is a socket, fifo, or a block or
331 * character device resident on the file system.
333 switch (vp->v_type) {
334 case VDIR:
335 error = EISDIR;
336 goto out;
337 case VCHR:
338 case VBLK:
339 case VFIFO:
340 break;
341 case VREG:
342 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
343 error = EROFS;
344 goto out;
346 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
347 error = EPERM;
348 goto out;
350 error = lfs_truncate(vp, vap->va_size, 0, cred);
351 if (error)
352 goto out;
353 break;
354 default:
355 error = EOPNOTSUPP;
356 goto out;
359 ip = VTOI(vp);
360 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
361 vap->va_birthtime.tv_sec != VNOVAL) {
362 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
363 error = EROFS;
364 goto out;
366 if ((ip->i_flags & SF_SNAPSHOT) != 0) {
367 error = EPERM;
368 goto out;
370 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
371 NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, cred));
372 if (error)
373 goto out;
374 if (vap->va_atime.tv_sec != VNOVAL)
375 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
376 ip->i_flag |= IN_ACCESS;
377 if (vap->va_mtime.tv_sec != VNOVAL) {
378 ip->i_flag |= IN_CHANGE | IN_UPDATE;
379 if (vp->v_mount->mnt_flag & MNT_RELATIME)
380 ip->i_flag |= IN_ACCESS;
382 if (vap->va_birthtime.tv_sec != VNOVAL) {
383 lfs_dino_setbirthtime(fs, ip->i_din,
384 &vap->va_birthtime);
386 error = lfs_update(vp, &vap->va_atime, &vap->va_mtime, 0);
387 if (error)
388 goto out;
390 error = 0;
391 if (vap->va_mode != (mode_t)VNOVAL) {
392 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
393 error = EROFS;
394 goto out;
396 if ((ip->i_flags & SF_SNAPSHOT) != 0 &&
397 (vap->va_mode & (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP |
398 S_IXOTH | S_IWOTH))) {
399 error = EPERM;
400 goto out;
402 error = ulfs_chmod(vp, (int)vap->va_mode, cred, l);
404 VN_KNOTE(vp, NOTE_ATTRIB);
405 out:
406 fstrans_done(vp->v_mount);
407 return (error);
411 * Change the mode on a file.
412 * Inode must be locked before calling.
414 static int
415 ulfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
417 struct inode *ip;
418 int error;
420 ip = VTOI(vp);
422 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
423 NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid, ip->i_gid, mode));
424 if (error)
425 return (error);
427 fstrans_start(vp->v_mount, FSTRANS_SHARED);
428 ip->i_mode &= ~ALLPERMS;
429 ip->i_mode |= (mode & ALLPERMS);
430 ip->i_flag |= IN_CHANGE;
431 DIP_ASSIGN(ip, mode, ip->i_mode);
432 fstrans_done(vp->v_mount);
433 return (0);
437 * Perform chown operation on inode ip;
438 * inode must be locked prior to call.
440 static int
441 ulfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
442 struct lwp *l)
444 struct inode *ip;
445 int error = 0;
446 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
447 uid_t ouid;
448 gid_t ogid;
449 int64_t change;
450 #endif
451 ip = VTOI(vp);
452 error = 0;
454 if (uid == (uid_t)VNOVAL)
455 uid = ip->i_uid;
456 if (gid == (gid_t)VNOVAL)
457 gid = ip->i_gid;
459 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
460 NULL, genfs_can_chown(cred, ip->i_uid, ip->i_gid, uid, gid));
461 if (error)
462 return (error);
464 fstrans_start(vp->v_mount, FSTRANS_SHARED);
465 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
466 ogid = ip->i_gid;
467 ouid = ip->i_uid;
468 change = DIP(ip, blocks);
469 (void) lfs_chkdq(ip, -change, cred, 0);
470 (void) lfs_chkiq(ip, -1, cred, 0);
471 #endif
472 ip->i_gid = gid;
473 DIP_ASSIGN(ip, gid, gid);
474 ip->i_uid = uid;
475 DIP_ASSIGN(ip, uid, uid);
476 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
477 if ((error = lfs_chkdq(ip, change, cred, 0)) == 0) {
478 if ((error = lfs_chkiq(ip, 1, cred, 0)) == 0)
479 goto good;
480 else
481 (void) lfs_chkdq(ip, -change, cred, FORCE);
483 ip->i_gid = ogid;
484 DIP_ASSIGN(ip, gid, ogid);
485 ip->i_uid = ouid;
486 DIP_ASSIGN(ip, uid, ouid);
487 (void) lfs_chkdq(ip, change, cred, FORCE);
488 (void) lfs_chkiq(ip, 1, cred, FORCE);
489 fstrans_done(vp->v_mount);
490 return (error);
491 good:
492 #endif /* LFS_QUOTA || LFS_QUOTA2 */
493 ip->i_flag |= IN_CHANGE;
494 fstrans_done(vp->v_mount);
495 return (0);
499 ulfs_remove(void *v)
501 struct vop_remove_args /* {
502 struct vnode *a_dvp;
503 struct vnode *a_vp;
504 struct componentname *a_cnp;
505 } */ *ap = v;
506 struct vnode *vp, *dvp;
507 struct inode *ip;
508 struct mount *mp;
509 int error;
510 struct ulfs_lookup_results *ulr;
512 vp = ap->a_vp;
513 dvp = ap->a_dvp;
514 ip = VTOI(vp);
515 mp = dvp->v_mount;
516 KASSERT(mp == vp->v_mount); /* XXX Not stable without lock. */
518 /* XXX should handle this material another way */
519 ulr = &VTOI(dvp)->i_crap;
520 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
522 fstrans_start(mp, FSTRANS_SHARED);
523 if (vp->v_type == VDIR || (ip->i_flags & (IMMUTABLE | APPEND)) ||
524 (VTOI(dvp)->i_flags & APPEND))
525 error = EPERM;
526 else {
527 error = ulfs_dirremove(dvp, ulr,
528 ip, ap->a_cnp->cn_flags, 0);
530 VN_KNOTE(vp, NOTE_DELETE);
531 VN_KNOTE(dvp, NOTE_WRITE);
532 if (dvp == vp)
533 vrele(vp);
534 else
535 vput(vp);
536 vput(dvp);
537 fstrans_done(mp);
538 return (error);
542 * ulfs_link: create hard link.
545 ulfs_link(void *v)
547 struct vop_link_v2_args /* {
548 struct vnode *a_dvp;
549 struct vnode *a_vp;
550 struct componentname *a_cnp;
551 } */ *ap = v;
552 struct vnode *dvp = ap->a_dvp;
553 struct vnode *vp = ap->a_vp;
554 struct componentname *cnp = ap->a_cnp;
555 struct mount *mp = dvp->v_mount;
556 struct inode *ip;
557 int error;
558 struct ulfs_lookup_results *ulr;
560 KASSERT(dvp != vp);
561 KASSERT(vp->v_type != VDIR);
562 KASSERT(mp == vp->v_mount); /* XXX Not stable without lock. */
564 /* XXX should handle this material another way */
565 ulr = &VTOI(dvp)->i_crap;
566 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
568 fstrans_start(mp, FSTRANS_SHARED);
569 error = vn_lock(vp, LK_EXCLUSIVE);
570 if (error) {
571 VOP_ABORTOP(dvp, cnp);
572 goto out2;
574 ip = VTOI(vp);
575 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
576 VOP_ABORTOP(dvp, cnp);
577 error = EMLINK;
578 goto out1;
580 if (ip->i_flags & (IMMUTABLE | APPEND)) {
581 VOP_ABORTOP(dvp, cnp);
582 error = EPERM;
583 goto out1;
585 ip->i_nlink++;
586 DIP_ASSIGN(ip, nlink, ip->i_nlink);
587 ip->i_flag |= IN_CHANGE;
588 error = lfs_update(vp, NULL, NULL, UPDATE_DIROP);
589 if (!error) {
590 error = ulfs_direnter(dvp, ulr, vp,
591 cnp, ip->i_number, LFS_IFTODT(ip->i_mode), NULL);
593 if (error) {
594 ip->i_nlink--;
595 DIP_ASSIGN(ip, nlink, ip->i_nlink);
596 ip->i_flag |= IN_CHANGE;
598 out1:
599 VOP_UNLOCK(vp);
600 out2:
601 VN_KNOTE(vp, NOTE_LINK);
602 VN_KNOTE(dvp, NOTE_WRITE);
603 fstrans_done(mp);
604 return (error);
608 * whiteout vnode call
611 ulfs_whiteout(void *v)
613 struct vop_whiteout_args /* {
614 struct vnode *a_dvp;
615 struct componentname *a_cnp;
616 int a_flags;
617 } */ *ap = v;
618 struct vnode *dvp = ap->a_dvp;
619 struct componentname *cnp = ap->a_cnp;
620 int error;
621 struct ulfsmount *ump = VFSTOULFS(dvp->v_mount);
622 struct lfs *fs = ump->um_lfs;
623 struct ulfs_lookup_results *ulr;
625 /* XXX should handle this material another way */
626 ulr = &VTOI(dvp)->i_crap;
627 ULFS_CHECK_CRAPCOUNTER(VTOI(dvp));
629 error = 0;
630 switch (ap->a_flags) {
631 case LOOKUP:
632 /* 4.4 format directories support whiteout operations */
633 if (fs->um_maxsymlinklen > 0)
634 return (0);
635 return (EOPNOTSUPP);
637 case CREATE:
638 /* create a new directory whiteout */
639 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
640 #ifdef DIAGNOSTIC
641 if (fs->um_maxsymlinklen <= 0)
642 panic("ulfs_whiteout: old format filesystem");
643 #endif
645 error = ulfs_direnter(dvp, ulr, NULL,
646 cnp, ULFS_WINO, LFS_DT_WHT, NULL);
647 break;
649 case DELETE:
650 /* remove an existing directory whiteout */
651 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
652 #ifdef DIAGNOSTIC
653 if (fs->um_maxsymlinklen <= 0)
654 panic("ulfs_whiteout: old format filesystem");
655 #endif
657 cnp->cn_flags &= ~DOWHITEOUT;
658 error = ulfs_dirremove(dvp, ulr, NULL, cnp->cn_flags, 0);
659 break;
660 default:
661 panic("ulfs_whiteout: unknown op");
662 /* NOTREACHED */
664 fstrans_done(dvp->v_mount);
665 return (error);
669 ulfs_rmdir(void *v)
671 struct vop_rmdir_args /* {
672 struct vnode *a_dvp;
673 struct vnode *a_vp;
674 struct componentname *a_cnp;
675 } */ *ap = v;
676 struct vnode *vp, *dvp;
677 struct componentname *cnp;
678 struct inode *ip, *dp;
679 int error;
680 struct ulfs_lookup_results *ulr;
682 vp = ap->a_vp;
683 dvp = ap->a_dvp;
684 cnp = ap->a_cnp;
685 ip = VTOI(vp);
686 dp = VTOI(dvp);
688 /* XXX should handle this material another way */
689 ulr = &dp->i_crap;
690 ULFS_CHECK_CRAPCOUNTER(dp);
693 * No rmdir "." or of mounted directories please.
695 if (dp == ip || vp->v_mountedhere != NULL) {
696 if (dp == ip)
697 vrele(dvp);
698 else
699 vput(dvp);
700 vput(vp);
701 return (EINVAL);
704 fstrans_start(dvp->v_mount, FSTRANS_SHARED);
707 * Do not remove a directory that is in the process of being renamed.
708 * Verify that the directory is empty (and valid). (Rmdir ".." won't
709 * be valid since ".." will contain a reference to the current
710 * directory and thus be non-empty.)
712 error = 0;
713 if (ip->i_nlink != 2 ||
714 !ulfs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
715 error = ENOTEMPTY;
716 goto out;
718 if ((dp->i_flags & APPEND) ||
719 (ip->i_flags & (IMMUTABLE | APPEND))) {
720 error = EPERM;
721 goto out;
724 * Delete reference to directory before purging
725 * inode. If we crash in between, the directory
726 * will be reattached to lost+found,
728 error = ulfs_dirremove(dvp, ulr, ip, cnp->cn_flags, 1);
729 if (error) {
730 goto out;
732 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
733 cache_purge(dvp);
735 * Truncate inode. The only stuff left in the directory is "." and
736 * "..". The "." reference is inconsequential since we're quashing
737 * it.
739 dp->i_nlink--;
740 DIP_ASSIGN(dp, nlink, dp->i_nlink);
741 dp->i_flag |= IN_CHANGE;
742 ip->i_nlink--;
743 DIP_ASSIGN(ip, nlink, ip->i_nlink);
744 ip->i_flag |= IN_CHANGE;
745 error = lfs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
746 cache_purge(vp);
747 #ifdef LFS_DIRHASH
748 if (ip->i_dirhash != NULL)
749 ulfsdirhash_free(ip);
750 #endif
751 out:
752 VN_KNOTE(vp, NOTE_DELETE);
753 vput(vp);
754 fstrans_done(dvp->v_mount);
755 vput(dvp);
756 return (error);
760 * Vnode op for reading directories.
762 * This routine handles converting from the on-disk directory format
763 * "struct lfs_direct" to the in-memory format "struct dirent" as well as
764 * byte swapping the entries if necessary.
767 ulfs_readdir(void *v)
769 struct vop_readdir_args /* {
770 struct vnode *a_vp;
771 struct uio *a_uio;
772 kauth_cred_t a_cred;
773 int *a_eofflag;
774 off_t **a_cookies;
775 int *ncookies;
776 } */ *ap = v;
777 struct vnode *vp = ap->a_vp;
778 LFS_DIRHEADER *cdp, *ecdp;
779 struct dirent *ndp;
780 char *cdbuf, *ndbuf, *endp;
781 struct uio auio, *uio;
782 struct iovec aiov;
783 int error;
784 size_t count, ccount, rcount, cdbufsz, ndbufsz;
785 off_t off, *ccp;
786 off_t startoff;
787 size_t skipbytes;
788 struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
789 struct lfs *fs = ump->um_lfs;
790 uio = ap->a_uio;
791 count = uio->uio_resid;
792 rcount = count - ((uio->uio_offset + count) & (fs->um_dirblksiz - 1));
794 if (rcount < LFS_DIRECTSIZ(fs, 0) || count < _DIRENT_MINSIZE(ndp))
795 return EINVAL;
797 startoff = uio->uio_offset & ~(fs->um_dirblksiz - 1);
798 skipbytes = uio->uio_offset - startoff;
799 rcount += skipbytes;
801 auio.uio_iov = &aiov;
802 auio.uio_iovcnt = 1;
803 auio.uio_offset = startoff;
804 auio.uio_resid = rcount;
805 UIO_SETUP_SYSSPACE(&auio);
806 auio.uio_rw = UIO_READ;
807 cdbufsz = rcount;
808 cdbuf = kmem_alloc(cdbufsz, KM_SLEEP);
809 aiov.iov_base = cdbuf;
810 aiov.iov_len = rcount;
811 error = VOP_READ(vp, &auio, 0, ap->a_cred);
812 if (error != 0) {
813 kmem_free(cdbuf, cdbufsz);
814 return error;
817 rcount -= auio.uio_resid;
819 cdp = (LFS_DIRHEADER *)(void *)cdbuf;
820 ecdp = (LFS_DIRHEADER *)(void *)&cdbuf[rcount];
822 ndbufsz = count;
823 ndbuf = kmem_alloc(ndbufsz, KM_SLEEP);
824 ndp = (struct dirent *)(void *)ndbuf;
825 endp = &ndbuf[count];
827 off = uio->uio_offset;
828 if (ap->a_cookies) {
829 ccount = rcount / _DIRENT_RECLEN(ndp, 1);
830 ccp = *(ap->a_cookies) = malloc(ccount * sizeof(*ccp),
831 M_TEMP, M_WAITOK);
832 } else {
833 /* XXX: GCC */
834 ccount = 0;
835 ccp = NULL;
838 while (cdp < ecdp) {
839 if (skipbytes > 0) {
840 if (lfs_dir_getreclen(fs, cdp) <= skipbytes) {
841 skipbytes -= lfs_dir_getreclen(fs, cdp);
842 cdp = LFS_NEXTDIR(fs, cdp);
843 continue;
846 * invalid cookie.
848 error = EINVAL;
849 goto out;
851 if (lfs_dir_getreclen(fs, cdp) == 0) {
852 struct dirent *ondp = ndp;
853 ndp->d_reclen = _DIRENT_MINSIZE(ndp);
854 ndp = _DIRENT_NEXT(ndp);
855 ondp->d_reclen = 0;
856 cdp = ecdp;
857 break;
859 ndp->d_type = lfs_dir_gettype(fs, cdp);
860 ndp->d_namlen = lfs_dir_getnamlen(fs, cdp);
861 ndp->d_reclen = _DIRENT_RECLEN(ndp, ndp->d_namlen);
862 if ((char *)(void *)ndp + ndp->d_reclen +
863 _DIRENT_MINSIZE(ndp) > endp)
864 break;
865 ndp->d_fileno = lfs_dir_getino(fs, cdp);
866 (void)memcpy(ndp->d_name, lfs_dir_nameptr(fs, cdp),
867 ndp->d_namlen);
868 memset(&ndp->d_name[ndp->d_namlen], 0,
869 ndp->d_reclen - _DIRENT_NAMEOFF(ndp) - ndp->d_namlen);
870 off += lfs_dir_getreclen(fs, cdp);
871 if (ap->a_cookies) {
872 KASSERT(ccp - *(ap->a_cookies) < ccount);
873 *(ccp++) = off;
875 ndp = _DIRENT_NEXT(ndp);
876 cdp = LFS_NEXTDIR(fs, cdp);
879 count = ((char *)(void *)ndp - ndbuf);
880 error = uiomove(ndbuf, count, uio);
881 out:
882 if (ap->a_cookies) {
883 if (error) {
884 free(*(ap->a_cookies), M_TEMP);
885 *(ap->a_cookies) = NULL;
886 *(ap->a_ncookies) = 0;
887 } else {
888 *ap->a_ncookies = ccp - *(ap->a_cookies);
891 uio->uio_offset = off;
892 kmem_free(ndbuf, ndbufsz);
893 kmem_free(cdbuf, cdbufsz);
894 *ap->a_eofflag = VTOI(vp)->i_size <= uio->uio_offset;
895 return error;
899 * Return target name of a symbolic link
902 ulfs_readlink(void *v)
904 struct vop_readlink_args /* {
905 struct vnode *a_vp;
906 struct uio *a_uio;
907 kauth_cred_t a_cred;
908 } */ *ap = v;
909 struct vnode *vp = ap->a_vp;
910 struct inode *ip = VTOI(vp);
911 struct ulfsmount *ump = VFSTOULFS(vp->v_mount);
912 struct lfs *fs = ump->um_lfs;
913 int isize;
915 isize = ip->i_size;
916 if (isize < fs->um_maxsymlinklen ||
917 (fs->um_maxsymlinklen == 0 && DIP(ip, blocks) == 0)) {
918 uiomove((char *)SHORTLINK(ip), isize, ap->a_uio);
919 return (0);
921 return (lfs_bufrd(vp, ap->a_uio, 0, ap->a_cred));
925 * Print out the contents of an inode.
928 ulfs_print(void *v)
930 struct vop_print_args /* {
931 struct vnode *a_vp;
932 } */ *ap = v;
933 struct vnode *vp;
934 struct inode *ip;
936 vp = ap->a_vp;
937 ip = VTOI(vp);
938 printf("tag VT_ULFS, ino %llu, on dev %llu, %llu",
939 (unsigned long long)ip->i_number,
940 (unsigned long long)major(ip->i_dev),
941 (unsigned long long)minor(ip->i_dev));
942 printf(" flags 0x%x, nlink %d\n",
943 ip->i_flag, ip->i_nlink);
944 printf("\tmode 0%o, owner %d, group %d, size %qd",
945 ip->i_mode, ip->i_uid, ip->i_gid,
946 (long long)ip->i_size);
947 if (vp->v_type == VFIFO)
948 VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
949 printf("\n");
950 return (0);
954 * Read wrapper for special devices.
957 ulfsspec_read(void *v)
959 struct vop_read_args /* {
960 struct vnode *a_vp;
961 struct uio *a_uio;
962 int a_ioflag;
963 kauth_cred_t a_cred;
964 } */ *ap = v;
967 * Set access flag.
969 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
970 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
971 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_read), ap));
975 * Write wrapper for special devices.
978 ulfsspec_write(void *v)
980 struct vop_write_args /* {
981 struct vnode *a_vp;
982 struct uio *a_uio;
983 int a_ioflag;
984 kauth_cred_t a_cred;
985 } */ *ap = v;
988 * Set update and change flags.
990 if ((ap->a_vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)
991 VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
992 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_write), ap));
996 * Read wrapper for fifo's
999 ulfsfifo_read(void *v)
1001 struct vop_read_args /* {
1002 struct vnode *a_vp;
1003 struct uio *a_uio;
1004 int a_ioflag;
1005 kauth_cred_t a_cred;
1006 } */ *ap = v;
1009 * Set access flag.
1011 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1012 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_read), ap));
1016 * Write wrapper for fifo's.
1019 ulfsfifo_write(void *v)
1021 struct vop_write_args /* {
1022 struct vnode *a_vp;
1023 struct uio *a_uio;
1024 int a_ioflag;
1025 kauth_cred_t a_cred;
1026 } */ *ap = v;
1029 * Set update and change flags.
1031 VTOI(ap->a_vp)->i_flag |= IN_MODIFY;
1032 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_write), ap));
1036 * Return POSIX pathconf information applicable to ulfs filesystems.
1039 ulfs_pathconf(void *v)
1041 struct vop_pathconf_args /* {
1042 struct vnode *a_vp;
1043 int a_name;
1044 register_t *a_retval;
1045 } */ *ap = v;
1047 switch (ap->a_name) {
1048 case _PC_LINK_MAX:
1049 *ap->a_retval = LINK_MAX;
1050 return (0);
1051 case _PC_NAME_MAX:
1052 *ap->a_retval = LFS_MAXNAMLEN;
1053 return (0);
1054 case _PC_PATH_MAX:
1055 *ap->a_retval = PATH_MAX;
1056 return (0);
1057 case _PC_PIPE_BUF:
1058 *ap->a_retval = PIPE_BUF;
1059 return (0);
1060 case _PC_CHOWN_RESTRICTED:
1061 *ap->a_retval = 1;
1062 return (0);
1063 case _PC_NO_TRUNC:
1064 *ap->a_retval = 1;
1065 return (0);
1066 case _PC_SYNC_IO:
1067 *ap->a_retval = 1;
1068 return (0);
1069 case _PC_FILESIZEBITS:
1070 *ap->a_retval = 42;
1071 return (0);
1072 case _PC_SYMLINK_MAX:
1073 *ap->a_retval = MAXPATHLEN;
1074 return (0);
1075 case _PC_2_SYMLINKS:
1076 *ap->a_retval = 1;
1077 return (0);
1078 default:
1079 return (EINVAL);
1081 /* NOTREACHED */
1085 * Advisory record locking support
1088 ulfs_advlock(void *v)
1090 struct vop_advlock_args /* {
1091 struct vnode *a_vp;
1092 void * a_id;
1093 int a_op;
1094 struct flock *a_fl;
1095 int a_flags;
1096 } */ *ap = v;
1097 struct inode *ip;
1099 ip = VTOI(ap->a_vp);
1100 return lf_advlock(ap, &ip->i_lockf, ip->i_size);
1104 * Initialize the vnode associated with a new inode, handle aliased
1105 * vnodes.
1107 void
1108 ulfs_vinit(struct mount *mntp, int (**specops)(void *), int (**fifoops)(void *),
1109 struct vnode **vpp)
1111 struct timeval tv;
1112 struct inode *ip;
1113 struct vnode *vp;
1114 dev_t rdev;
1115 struct ulfsmount *ump;
1117 vp = *vpp;
1118 ip = VTOI(vp);
1119 switch(vp->v_type = IFTOVT(ip->i_mode)) {
1120 case VCHR:
1121 case VBLK:
1122 vp->v_op = specops;
1123 ump = ip->i_ump;
1124 // XXX clean this up
1125 if (ump->um_fstype == ULFS1)
1126 rdev = (dev_t)ulfs_rw32(ip->i_din->u_32.di_rdev,
1127 ULFS_MPNEEDSWAP(ump->um_lfs));
1128 else
1129 rdev = (dev_t)ulfs_rw64(ip->i_din->u_64.di_rdev,
1130 ULFS_MPNEEDSWAP(ump->um_lfs));
1131 spec_node_init(vp, rdev);
1132 break;
1133 case VFIFO:
1134 vp->v_op = fifoops;
1135 break;
1136 case VNON:
1137 case VBAD:
1138 case VSOCK:
1139 case VLNK:
1140 case VDIR:
1141 case VREG:
1142 break;
1144 if (ip->i_number == ULFS_ROOTINO)
1145 vp->v_vflag |= VV_ROOT;
1147 * Initialize modrev times
1149 getmicrouptime(&tv);
1150 ip->i_modrev = (uint64_t)(uint)tv.tv_sec << 32
1151 | tv.tv_usec * 4294u;
1152 *vpp = vp;
1156 * Allocate a new inode.
1159 ulfs_makeinode(struct vattr *vap, struct vnode *dvp,
1160 const struct ulfs_lookup_results *ulr,
1161 struct vnode **vpp, struct componentname *cnp)
1163 struct inode *ip;
1164 struct vnode *tvp;
1165 int error;
1167 error = vcache_new(dvp->v_mount, dvp, vap, cnp->cn_cred, &tvp);
1168 if (error)
1169 return error;
1170 error = vn_lock(tvp, LK_EXCLUSIVE);
1171 if (error) {
1172 vrele(tvp);
1173 return error;
1175 lfs_mark_vnode(tvp);
1176 *vpp = tvp;
1177 ip = VTOI(tvp);
1178 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1179 ip->i_nlink = 1;
1180 DIP_ASSIGN(ip, nlink, 1);
1182 /* Authorize setting SGID if needed. */
1183 if (ip->i_mode & ISGID) {
1184 error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY,
1185 tvp, NULL, genfs_can_chmod(tvp->v_type, cnp->cn_cred, ip->i_uid,
1186 ip->i_gid, MAKEIMODE(vap->va_type, vap->va_mode)));
1187 if (error) {
1188 ip->i_mode &= ~ISGID;
1189 DIP_ASSIGN(ip, mode, ip->i_mode);
1193 if (cnp->cn_flags & ISWHITEOUT) {
1194 ip->i_flags |= UF_OPAQUE;
1195 DIP_ASSIGN(ip, flags, ip->i_flags);
1199 * Make sure inode goes to disk before directory entry.
1201 if ((error = lfs_update(tvp, NULL, NULL, UPDATE_DIROP)) != 0)
1202 goto bad;
1203 error = ulfs_direnter(dvp, ulr, tvp,
1204 cnp, ip->i_number, LFS_IFTODT(ip->i_mode), NULL);
1205 if (error)
1206 goto bad;
1207 *vpp = tvp;
1208 return (0);
1210 bad:
1212 * Write error occurred trying to update the inode
1213 * or the directory so must deallocate the inode.
1215 ip->i_nlink = 0;
1216 DIP_ASSIGN(ip, nlink, 0);
1217 ip->i_flag |= IN_CHANGE;
1218 /* If IN_ADIROP, account for it */
1219 lfs_unmark_vnode(tvp);
1220 vput(tvp);
1221 return (error);
1225 * Allocate len bytes at offset off.
1228 ulfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
1229 kauth_cred_t cred)
1231 struct inode *ip = VTOI(vp);
1232 int error, delta, bshift, bsize;
1233 UVMHIST_FUNC("ulfs_gop_alloc"); UVMHIST_CALLED(ubchist);
1235 error = 0;
1236 bshift = vp->v_mount->mnt_fs_bshift;
1237 bsize = 1 << bshift;
1239 delta = off & (bsize - 1);
1240 off -= delta;
1241 len += delta;
1243 while (len > 0) {
1244 bsize = MIN(bsize, len);
1246 error = lfs_balloc(vp, off, bsize, cred, flags, NULL);
1247 if (error) {
1248 goto out;
1252 * increase file size now, lfs_balloc() requires that
1253 * EOF be up-to-date before each call.
1256 if (ip->i_size < off + bsize) {
1257 UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
1258 vp, ip->i_size, off + bsize, 0);
1259 ip->i_size = off + bsize;
1260 DIP_ASSIGN(ip, size, ip->i_size);
1263 off += bsize;
1264 len -= bsize;
1267 out:
1268 return error;
1271 void
1272 ulfs_gop_markupdate(struct vnode *vp, int flags)
1274 u_int32_t mask = 0;
1276 if ((flags & GOP_UPDATE_ACCESSED) != 0) {
1277 mask = IN_ACCESS;
1279 if ((flags & GOP_UPDATE_MODIFIED) != 0) {
1280 if (vp->v_type == VREG) {
1281 mask |= IN_CHANGE | IN_UPDATE;
1282 } else {
1283 mask |= IN_MODIFY;
1286 if (mask) {
1287 struct inode *ip = VTOI(vp);
1289 ip->i_flag |= mask;
1294 ulfs_bufio(enum uio_rw rw, struct vnode *vp, void *buf, size_t len, off_t off,
1295 int ioflg, kauth_cred_t cred, size_t *aresid, struct lwp *l)
1297 struct iovec iov;
1298 struct uio uio;
1299 int error;
1301 KASSERT(ISSET(ioflg, IO_NODELOCKED));
1302 KASSERT(VOP_ISLOCKED(vp));
1303 KASSERT(rw != UIO_WRITE || VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1305 iov.iov_base = buf;
1306 iov.iov_len = len;
1307 uio.uio_iov = &iov;
1308 uio.uio_iovcnt = 1;
1309 uio.uio_resid = len;
1310 uio.uio_offset = off;
1311 uio.uio_rw = rw;
1312 UIO_SETUP_SYSSPACE(&uio);
1314 switch (rw) {
1315 case UIO_READ:
1316 error = lfs_bufrd(vp, &uio, ioflg, cred);
1317 break;
1318 case UIO_WRITE:
1319 error = lfs_bufwr(vp, &uio, ioflg, cred);
1320 break;
1321 default:
1322 panic("invalid uio rw: %d", (int)rw);
1325 if (aresid)
1326 *aresid = uio.uio_resid;
1327 else if (uio.uio_resid && error == 0)
1328 error = EIO;
1330 KASSERT(VOP_ISLOCKED(vp));
1331 KASSERT(rw != UIO_WRITE || VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
1332 return error;