1*aa47c49aSsemarie /* $OpenBSD: ext2fs_vnops.c,v 1.94 2024/05/13 11:17:41 semarie Exp $ */
21414b0faSart /* $NetBSD: ext2fs_vnops.c,v 1.1 1997/06/11 09:34:09 bouyer Exp $ */
35ac2d602Sdownsj
45ac2d602Sdownsj /*
51f3ff51cSdownsj * Copyright (c) 1997 Manuel Bouyer.
65ac2d602Sdownsj * Copyright (c) 1982, 1986, 1989, 1993
75ac2d602Sdownsj * The Regents of the University of California. All rights reserved.
85ac2d602Sdownsj * (c) UNIX System Laboratories, Inc.
95ac2d602Sdownsj * All or some portions of this file are derived from material licensed
105ac2d602Sdownsj * to the University of California by American Telephone and Telegraph
115ac2d602Sdownsj * Co. or Unix System Laboratories, Inc. and are reproduced herein with
125ac2d602Sdownsj * the permission of UNIX System Laboratories, Inc.
135ac2d602Sdownsj *
145ac2d602Sdownsj * Redistribution and use in source and binary forms, with or without
155ac2d602Sdownsj * modification, are permitted provided that the following conditions
165ac2d602Sdownsj * are met:
175ac2d602Sdownsj * 1. Redistributions of source code must retain the above copyright
185ac2d602Sdownsj * notice, this list of conditions and the following disclaimer.
195ac2d602Sdownsj * 2. Redistributions in binary form must reproduce the above copyright
205ac2d602Sdownsj * notice, this list of conditions and the following disclaimer in the
215ac2d602Sdownsj * documentation and/or other materials provided with the distribution.
2229295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
235ac2d602Sdownsj * may be used to endorse or promote products derived from this software
245ac2d602Sdownsj * without specific prior written permission.
255ac2d602Sdownsj *
265ac2d602Sdownsj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
275ac2d602Sdownsj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
285ac2d602Sdownsj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
295ac2d602Sdownsj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
305ac2d602Sdownsj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
315ac2d602Sdownsj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
325ac2d602Sdownsj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
335ac2d602Sdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
345ac2d602Sdownsj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
355ac2d602Sdownsj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
365ac2d602Sdownsj * SUCH DAMAGE.
375ac2d602Sdownsj *
385ac2d602Sdownsj * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94
391f3ff51cSdownsj * Modified for ext2fs by Manuel Bouyer.
405ac2d602Sdownsj */
415ac2d602Sdownsj
425ac2d602Sdownsj #include <sys/param.h>
435ac2d602Sdownsj #include <sys/systm.h>
445ac2d602Sdownsj #include <sys/resourcevar.h>
455ac2d602Sdownsj #include <sys/kernel.h>
46c0cd3489Sguenther #include <sys/fcntl.h>
475ac2d602Sdownsj #include <sys/stat.h>
485ac2d602Sdownsj #include <sys/proc.h>
495ac2d602Sdownsj #include <sys/conf.h>
505ac2d602Sdownsj #include <sys/mount.h>
515ac2d602Sdownsj #include <sys/namei.h>
525ac2d602Sdownsj #include <sys/vnode.h>
535ac2d602Sdownsj #include <sys/lockf.h>
545ac2d602Sdownsj #include <sys/malloc.h>
55f5ee6277Sjasoni #include <sys/pool.h>
565ac2d602Sdownsj #include <sys/signalvar.h>
57544451c3Sderaadt #include <sys/specdev.h>
58782ebdf8Stedu #include <sys/unistd.h>
595ac2d602Sdownsj
605ac2d602Sdownsj #include <miscfs/fifofs/fifo.h>
615ac2d602Sdownsj
625ac2d602Sdownsj #include <ufs/ufs/quota.h>
635ac2d602Sdownsj #include <ufs/ufs/inode.h>
645ac2d602Sdownsj #include <ufs/ufs/ufs_extern.h>
655ac2d602Sdownsj #include <ufs/ufs/ufsmount.h>
665ac2d602Sdownsj
675ac2d602Sdownsj #include <ufs/ext2fs/ext2fs.h>
685ac2d602Sdownsj #include <ufs/ext2fs/ext2fs_extern.h>
695ac2d602Sdownsj #include <ufs/ext2fs/ext2fs_dir.h>
705ac2d602Sdownsj
71669a8cbbSmiod #include <uvm/uvm_extern.h>
72669a8cbbSmiod
73db7aa982Smpi static int ext2fs_chmod(struct vnode *, mode_t, struct ucred *);
74db7aa982Smpi static int ext2fs_chown(struct vnode *, uid_t, gid_t, struct ucred *);
755ac2d602Sdownsj
765ac2d602Sdownsj /*
775ac2d602Sdownsj * Create a regular file
785ac2d602Sdownsj */
795ac2d602Sdownsj int
ext2fs_create(void * v)805f64cd9cSjasper ext2fs_create(void *v)
815ac2d602Sdownsj {
8299bc9d31Sderaadt struct vop_create_args *ap = v;
839d1c6d70Svisa
844dd4d774Svisa return (ext2fs_makeinode(MAKEIMODE(ap->a_vap->va_type,
854dd4d774Svisa ap->a_vap->va_mode), ap->a_dvp, ap->a_vpp, ap->a_cnp));
865ac2d602Sdownsj }
875ac2d602Sdownsj
885ac2d602Sdownsj /*
895ac2d602Sdownsj * Mknod vnode call
905ac2d602Sdownsj */
915ac2d602Sdownsj int
ext2fs_mknod(void * v)925f64cd9cSjasper ext2fs_mknod(void *v)
935ac2d602Sdownsj {
9499bc9d31Sderaadt struct vop_mknod_args *ap = v;
955f64cd9cSjasper struct vattr *vap = ap->a_vap;
965f64cd9cSjasper struct vnode **vpp = ap->a_vpp;
975f64cd9cSjasper struct inode *ip;
985ac2d602Sdownsj int error;
995ac2d602Sdownsj
1009d1c6d70Svisa error = ext2fs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
1019d1c6d70Svisa ap->a_dvp, vpp, ap->a_cnp);
1029d1c6d70Svisa if (error != 0)
1035ac2d602Sdownsj return (error);
1045ac2d602Sdownsj ip = VTOI(*vpp);
1055ac2d602Sdownsj ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1065ac2d602Sdownsj if (vap->va_rdev != VNOVAL) {
1075ac2d602Sdownsj /*
1085ac2d602Sdownsj * Want to be able to use this to make badblock
1095ac2d602Sdownsj * inodes, so don't truncate the dev number.
1105ac2d602Sdownsj */
111f7dbefaaSpelikan ip->i_e2din->e2di_rdev = htole32(vap->va_rdev);
1125ac2d602Sdownsj }
1135ac2d602Sdownsj /*
1145ac2d602Sdownsj * Remove inode so that it will be reloaded by VFS_VGET and
1155ac2d602Sdownsj * checked to see if it is an alias of an existing entry in
1165ac2d602Sdownsj * the inode cache.
1175ac2d602Sdownsj */
1185ac2d602Sdownsj vput(*vpp);
1195ac2d602Sdownsj (*vpp)->v_type = VNON;
1205ac2d602Sdownsj vgone(*vpp);
121dfc6b8b2Stedu *vpp = NULL;
1225ac2d602Sdownsj return (0);
1235ac2d602Sdownsj }
1245ac2d602Sdownsj
1255ac2d602Sdownsj /*
1265ac2d602Sdownsj * Open called.
1275ac2d602Sdownsj *
1285ac2d602Sdownsj * Just check the APPEND flag.
1295ac2d602Sdownsj */
1305ac2d602Sdownsj int
ext2fs_open(void * v)1315f64cd9cSjasper ext2fs_open(void *v)
1325ac2d602Sdownsj {
13399bc9d31Sderaadt struct vop_open_args *ap = v;
1345ac2d602Sdownsj
1355ac2d602Sdownsj /*
1365ac2d602Sdownsj * Files marked append-only must be opened for appending.
1375ac2d602Sdownsj */
1385ac2d602Sdownsj if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) &&
1395ac2d602Sdownsj (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
1405ac2d602Sdownsj return (EPERM);
1415ac2d602Sdownsj return (0);
1425ac2d602Sdownsj }
1435ac2d602Sdownsj
1445ac2d602Sdownsj int
ext2fs_access(void * v)1455f64cd9cSjasper ext2fs_access(void *v)
1465ac2d602Sdownsj {
14799bc9d31Sderaadt struct vop_access_args *ap = v;
1485f64cd9cSjasper struct vnode *vp = ap->a_vp;
1495f64cd9cSjasper struct inode *ip = VTOI(vp);
1505ac2d602Sdownsj mode_t mode = ap->a_mode;
1515ac2d602Sdownsj
1525ac2d602Sdownsj /* If immutable bit set, nobody gets to write it. */
1535ac2d602Sdownsj if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE))
1545ac2d602Sdownsj return (EPERM);
1555ac2d602Sdownsj
156141c07a8Smillert return (vaccess(vp->v_type, ip->i_e2fs_mode, ip->i_e2fs_uid,
157141c07a8Smillert ip->i_e2fs_gid, mode, ap->a_cred));
1585ac2d602Sdownsj }
1595ac2d602Sdownsj
1605ac2d602Sdownsj int
ext2fs_getattr(void * v)1615f64cd9cSjasper ext2fs_getattr(void *v)
1625ac2d602Sdownsj {
16399bc9d31Sderaadt struct vop_getattr_args *ap = v;
1645f64cd9cSjasper struct vnode *vp = ap->a_vp;
1655f64cd9cSjasper struct inode *ip = VTOI(vp);
1665f64cd9cSjasper struct vattr *vap = ap->a_vap;
1675ac2d602Sdownsj
168d4fc1c49Sguenther EXT2FS_ITIMES(ip);
1695ac2d602Sdownsj /*
1705ac2d602Sdownsj * Copy from inode table
1715ac2d602Sdownsj */
1725ac2d602Sdownsj vap->va_fsid = ip->i_dev;
1735ac2d602Sdownsj vap->va_fileid = ip->i_number;
1741f3ff51cSdownsj vap->va_mode = ip->i_e2fs_mode & ALLPERMS;
1755ac2d602Sdownsj vap->va_nlink = ip->i_e2fs_nlink;
1765ac2d602Sdownsj vap->va_uid = ip->i_e2fs_uid;
1775ac2d602Sdownsj vap->va_gid = ip->i_e2fs_gid;
178f7dbefaaSpelikan vap->va_rdev = (dev_t) letoh32(ip->i_e2din->e2di_rdev);
1794dcbbbb0Sniallo vap->va_size = ext2fs_size(ip);
1805ac2d602Sdownsj vap->va_atime.tv_sec = ip->i_e2fs_atime;
1815ac2d602Sdownsj vap->va_atime.tv_nsec = 0;
1825ac2d602Sdownsj vap->va_mtime.tv_sec = ip->i_e2fs_mtime;
1835ac2d602Sdownsj vap->va_mtime.tv_nsec = 0;
1845ac2d602Sdownsj vap->va_ctime.tv_sec = ip->i_e2fs_ctime;
1855ac2d602Sdownsj vap->va_ctime.tv_nsec = 0;
1865ac2d602Sdownsj #ifdef EXT2FS_SYSTEM_FLAGS
1875ac2d602Sdownsj vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0;
1885ac2d602Sdownsj vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
1895ac2d602Sdownsj #else
1905ac2d602Sdownsj vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0;
1915ac2d602Sdownsj vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0;
1925ac2d602Sdownsj #endif
1935ac2d602Sdownsj vap->va_gen = ip->i_e2fs_gen;
1945ac2d602Sdownsj /* this doesn't belong here */
1955ac2d602Sdownsj if (vp->v_type == VBLK)
1965ac2d602Sdownsj vap->va_blocksize = BLKDEV_IOSIZE;
1975ac2d602Sdownsj else if (vp->v_type == VCHR)
1985ac2d602Sdownsj vap->va_blocksize = MAXBSIZE;
1995ac2d602Sdownsj else
2005ac2d602Sdownsj vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
201f5ee6277Sjasoni vap->va_bytes = dbtob((u_quad_t)ip->i_e2fs_nblock);
2025ac2d602Sdownsj vap->va_type = vp->v_type;
2035ac2d602Sdownsj vap->va_filerev = ip->i_modrev;
2045ac2d602Sdownsj return (0);
2055ac2d602Sdownsj }
2065ac2d602Sdownsj
2075ac2d602Sdownsj /*
2085ac2d602Sdownsj * Set attribute vnode op. called from several syscalls
2095ac2d602Sdownsj */
2105ac2d602Sdownsj int
ext2fs_setattr(void * v)2115f64cd9cSjasper ext2fs_setattr(void *v)
2125ac2d602Sdownsj {
21399bc9d31Sderaadt struct vop_setattr_args *ap = v;
2145f64cd9cSjasper struct vattr *vap = ap->a_vap;
2155f64cd9cSjasper struct vnode *vp = ap->a_vp;
2165f64cd9cSjasper struct inode *ip = VTOI(vp);
2175f64cd9cSjasper struct ucred *cred = ap->a_cred;
2185ac2d602Sdownsj int error;
2195ac2d602Sdownsj
2205ac2d602Sdownsj /*
2215ac2d602Sdownsj * Check for unsettable attributes.
2225ac2d602Sdownsj */
2235ac2d602Sdownsj if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
2245ac2d602Sdownsj (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
2255ac2d602Sdownsj (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
2265ac2d602Sdownsj ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
2275ac2d602Sdownsj return (EINVAL);
2285ac2d602Sdownsj }
2295ac2d602Sdownsj if (vap->va_flags != VNOVAL) {
230f5ee6277Sjasoni if (vp->v_mount->mnt_flag & MNT_RDONLY)
231f5ee6277Sjasoni return (EROFS);
2325ac2d602Sdownsj if (cred->cr_uid != ip->i_e2fs_uid &&
233ad392958Stedu (error = suser_ucred(cred)))
2345ac2d602Sdownsj return (error);
2355ac2d602Sdownsj #ifdef EXT2FS_SYSTEM_FLAGS
2365ac2d602Sdownsj if (cred->cr_uid == 0) {
237f5ee6277Sjasoni if ((ip->i_e2fs_flags &
238f5ee6277Sjasoni (EXT2_APPEND | EXT2_IMMUTABLE)) && securelevel > 0)
2395ac2d602Sdownsj return (EPERM);
2405ac2d602Sdownsj ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
241f5ee6277Sjasoni ip->i_e2fs_flags |=
242f5ee6277Sjasoni (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 |
2435ac2d602Sdownsj (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
2445ac2d602Sdownsj } else {
2455ac2d602Sdownsj return (EPERM);
2465ac2d602Sdownsj }
2475ac2d602Sdownsj #else
2485ac2d602Sdownsj ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
249f5ee6277Sjasoni ip->i_e2fs_flags |=
250f5ee6277Sjasoni (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 |
2515ac2d602Sdownsj (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
2525ac2d602Sdownsj #endif
2535ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
2545ac2d602Sdownsj if (vap->va_flags & (IMMUTABLE | APPEND))
2555ac2d602Sdownsj return (0);
2565ac2d602Sdownsj }
2575ac2d602Sdownsj if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE))
2585ac2d602Sdownsj return (EPERM);
2595ac2d602Sdownsj /*
2605ac2d602Sdownsj * Go through the fields and update iff not VNOVAL.
2615ac2d602Sdownsj */
2625ac2d602Sdownsj if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
263f5ee6277Sjasoni if (vp->v_mount->mnt_flag & MNT_RDONLY)
264f5ee6277Sjasoni return (EROFS);
265db7aa982Smpi error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred);
2665ac2d602Sdownsj if (error)
2675ac2d602Sdownsj return (error);
2685ac2d602Sdownsj }
2695ac2d602Sdownsj if (vap->va_size != VNOVAL) {
270f5ee6277Sjasoni /*
271f5ee6277Sjasoni * Disallow write attempts on read-only file systems;
272f5ee6277Sjasoni * unless the file is a socket, fifo, or a block or
273f5ee6277Sjasoni * character device resident on the file system.
274f5ee6277Sjasoni */
275f5ee6277Sjasoni switch (vp->v_type) {
276f5ee6277Sjasoni case VDIR:
2775ac2d602Sdownsj return (EISDIR);
278f5ee6277Sjasoni case VLNK:
279f5ee6277Sjasoni case VREG:
280f5ee6277Sjasoni if (vp->v_mount->mnt_flag & MNT_RDONLY)
281f5ee6277Sjasoni return (EROFS);
282f5ee6277Sjasoni default:
283f5ee6277Sjasoni break;
284f5ee6277Sjasoni }
285b080ad39Scsapuntz error = ext2fs_truncate(ip, vap->va_size, 0, cred);
2865ac2d602Sdownsj if (error)
2875ac2d602Sdownsj return (error);
2885ac2d602Sdownsj }
2894707cbe3Sguenther if ((vap->va_vaflags & VA_UTIMES_CHANGE) ||
2904707cbe3Sguenther vap->va_atime.tv_nsec != VNOVAL ||
2914707cbe3Sguenther vap->va_mtime.tv_nsec != VNOVAL) {
292f5ee6277Sjasoni if (vp->v_mount->mnt_flag & MNT_RDONLY)
293f5ee6277Sjasoni return (EROFS);
2945ac2d602Sdownsj if (cred->cr_uid != ip->i_e2fs_uid &&
295ad392958Stedu (error = suser_ucred(cred)) &&
2965ac2d602Sdownsj ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
297db7aa982Smpi (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_p))))
2985ac2d602Sdownsj return (error);
2994707cbe3Sguenther if (vap->va_mtime.tv_nsec != VNOVAL)
3005ac2d602Sdownsj ip->i_flag |= IN_CHANGE | IN_UPDATE;
3014707cbe3Sguenther else if (vap->va_vaflags & VA_UTIMES_CHANGE)
3024707cbe3Sguenther ip->i_flag |= IN_CHANGE;
3034707cbe3Sguenther if (vap->va_atime.tv_nsec != VNOVAL) {
3045fdfb110Smillert if (!(vp->v_mount->mnt_flag & MNT_NOATIME) ||
3055fdfb110Smillert (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
3065fdfb110Smillert ip->i_flag |= IN_ACCESS;
3075fdfb110Smillert }
308d4fc1c49Sguenther EXT2FS_ITIMES(ip);
3094707cbe3Sguenther if (vap->va_mtime.tv_nsec != VNOVAL)
310d4fc1c49Sguenther ip->i_e2fs_mtime = vap->va_mtime.tv_sec;
3114707cbe3Sguenther if (vap->va_atime.tv_nsec != VNOVAL)
312d4fc1c49Sguenther ip->i_e2fs_atime = vap->va_atime.tv_sec;
313d4fc1c49Sguenther error = ext2fs_update(ip, 1);
3145ac2d602Sdownsj if (error)
3155ac2d602Sdownsj return (error);
3165ac2d602Sdownsj }
3175ac2d602Sdownsj error = 0;
318f5ee6277Sjasoni if (vap->va_mode != (mode_t)VNOVAL) {
319f5ee6277Sjasoni if (vp->v_mount->mnt_flag & MNT_RDONLY)
320f5ee6277Sjasoni return (EROFS);
321db7aa982Smpi error = ext2fs_chmod(vp, vap->va_mode, cred);
322f5ee6277Sjasoni }
3235ac2d602Sdownsj return (error);
3245ac2d602Sdownsj }
3255ac2d602Sdownsj
3265ac2d602Sdownsj /*
3275ac2d602Sdownsj * Change the mode on a file.
3285ac2d602Sdownsj * Inode must be locked before calling.
3295ac2d602Sdownsj */
3305ac2d602Sdownsj static int
ext2fs_chmod(struct vnode * vp,mode_t mode,struct ucred * cred)331db7aa982Smpi ext2fs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred)
3325ac2d602Sdownsj {
3335f64cd9cSjasper struct inode *ip = VTOI(vp);
3345ac2d602Sdownsj int error;
3355ac2d602Sdownsj
336ad392958Stedu if (cred->cr_uid != ip->i_e2fs_uid && (error = suser_ucred(cred)))
3375ac2d602Sdownsj return (error);
3385ac2d602Sdownsj if (cred->cr_uid) {
3395ac2d602Sdownsj if (vp->v_type != VDIR && (mode & S_ISTXT))
3405ac2d602Sdownsj return (EFTYPE);
3415ac2d602Sdownsj if (!groupmember(ip->i_e2fs_gid, cred) && (mode & ISGID))
3425ac2d602Sdownsj return (EPERM);
3435ac2d602Sdownsj }
3445ac2d602Sdownsj ip->i_e2fs_mode &= ~ALLPERMS;
3455ac2d602Sdownsj ip->i_e2fs_mode |= (mode & ALLPERMS);
3465ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
3471414b0faSart if ((vp->v_flag & VTEXT) && (ip->i_e2fs_mode & S_ISTXT) == 0)
3481414b0faSart (void) uvm_vnp_uncache(vp);
3495ac2d602Sdownsj return (0);
3505ac2d602Sdownsj }
3515ac2d602Sdownsj
3525ac2d602Sdownsj /*
3535ac2d602Sdownsj * Perform chown operation on inode ip;
3545ac2d602Sdownsj * inode must be locked prior to call.
3555ac2d602Sdownsj */
3565ac2d602Sdownsj static int
ext2fs_chown(struct vnode * vp,uid_t uid,gid_t gid,struct ucred * cred)357db7aa982Smpi ext2fs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
3585ac2d602Sdownsj {
3595f64cd9cSjasper struct inode *ip = VTOI(vp);
3605ac2d602Sdownsj uid_t ouid;
3615ac2d602Sdownsj gid_t ogid;
3625ac2d602Sdownsj int error = 0;
3635ac2d602Sdownsj
3645ac2d602Sdownsj if (uid == (uid_t)VNOVAL)
3655ac2d602Sdownsj uid = ip->i_e2fs_uid;
3665ac2d602Sdownsj if (gid == (gid_t)VNOVAL)
3675ac2d602Sdownsj gid = ip->i_e2fs_gid;
3685ac2d602Sdownsj /*
3695ac2d602Sdownsj * If we don't own the file, are trying to change the owner
3705ac2d602Sdownsj * of the file, or are not a member of the target group,
3715ac2d602Sdownsj * the caller must be superuser or the call fails.
3725ac2d602Sdownsj */
3735ac2d602Sdownsj if ((cred->cr_uid != ip->i_e2fs_uid || uid != ip->i_e2fs_uid ||
3745604a4c2Sguenther (gid != ip->i_e2fs_gid && !groupmember(gid, cred))) &&
375ad392958Stedu (error = suser_ucred(cred)))
3765ac2d602Sdownsj return (error);
3775ac2d602Sdownsj ogid = ip->i_e2fs_gid;
3785ac2d602Sdownsj ouid = ip->i_e2fs_uid;
3795ac2d602Sdownsj
3805ac2d602Sdownsj ip->i_e2fs_gid = gid;
3815ac2d602Sdownsj ip->i_e2fs_uid = uid;
3825ac2d602Sdownsj if (ouid != uid || ogid != gid)
3835ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
3845ac2d602Sdownsj if (ouid != uid && cred->cr_uid != 0)
3855ac2d602Sdownsj ip->i_e2fs_mode &= ~ISUID;
3865ac2d602Sdownsj if (ogid != gid && cred->cr_uid != 0)
3875ac2d602Sdownsj ip->i_e2fs_mode &= ~ISGID;
3885ac2d602Sdownsj return (0);
3895ac2d602Sdownsj }
3905ac2d602Sdownsj
3915ac2d602Sdownsj int
ext2fs_remove(void * v)3925f64cd9cSjasper ext2fs_remove(void *v)
3935ac2d602Sdownsj {
39499bc9d31Sderaadt struct vop_remove_args *ap = v;
3955f64cd9cSjasper struct inode *ip;
3965f64cd9cSjasper struct vnode *vp = ap->a_vp;
3975f64cd9cSjasper struct vnode *dvp = ap->a_dvp;
3985ac2d602Sdownsj int error;
3995ac2d602Sdownsj
4005ac2d602Sdownsj ip = VTOI(vp);
401d56d3b9eSderaadt if (vp->v_type == VDIR ||
402d56d3b9eSderaadt (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
4035ac2d602Sdownsj (VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
4045ac2d602Sdownsj error = EPERM;
4055ac2d602Sdownsj goto out;
4065ac2d602Sdownsj }
4075ac2d602Sdownsj error = ext2fs_dirremove(dvp, ap->a_cnp);
4085ac2d602Sdownsj if (error == 0) {
4095ac2d602Sdownsj ip->i_e2fs_nlink--;
4105ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
4115ac2d602Sdownsj }
4125ac2d602Sdownsj out:
4135ac2d602Sdownsj return (error);
4145ac2d602Sdownsj }
4155ac2d602Sdownsj
4165ac2d602Sdownsj /*
4175ac2d602Sdownsj * link vnode call
4185ac2d602Sdownsj */
4195ac2d602Sdownsj int
ext2fs_link(void * v)4205f64cd9cSjasper ext2fs_link(void *v)
4215ac2d602Sdownsj {
42299bc9d31Sderaadt struct vop_link_args *ap = v;
4235f64cd9cSjasper struct vnode *dvp = ap->a_dvp;
4245f64cd9cSjasper struct vnode *vp = ap->a_vp;
4255f64cd9cSjasper struct componentname *cnp = ap->a_cnp;
4265f64cd9cSjasper struct inode *ip;
4275ac2d602Sdownsj int error;
4285ac2d602Sdownsj
4295ac2d602Sdownsj #ifdef DIAGNOSTIC
4305ac2d602Sdownsj if ((cnp->cn_flags & HASBUF) == 0)
4315ac2d602Sdownsj panic("ext2fs_link: no name");
4325ac2d602Sdownsj #endif
4336e880534Svisa if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
4345ac2d602Sdownsj VOP_ABORTOP(dvp, cnp);
4355ac2d602Sdownsj goto out2;
4365ac2d602Sdownsj }
4375ac2d602Sdownsj ip = VTOI(vp);
4385ac2d602Sdownsj if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
4395ac2d602Sdownsj VOP_ABORTOP(dvp, cnp);
4405ac2d602Sdownsj error = EMLINK;
4415ac2d602Sdownsj goto out1;
4425ac2d602Sdownsj }
4435ac2d602Sdownsj if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
4445ac2d602Sdownsj VOP_ABORTOP(dvp, cnp);
4455ac2d602Sdownsj error = EPERM;
4465ac2d602Sdownsj goto out1;
4475ac2d602Sdownsj }
4485ac2d602Sdownsj ip->i_e2fs_nlink++;
4495ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
450d4fc1c49Sguenther error = ext2fs_update(ip, 1);
4515ac2d602Sdownsj if (!error)
4525ac2d602Sdownsj error = ext2fs_direnter(ip, dvp, cnp);
4535ac2d602Sdownsj if (error) {
4545ac2d602Sdownsj ip->i_e2fs_nlink--;
4555ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
4565ac2d602Sdownsj }
457dbe27ba0Stedu pool_put(&namei_pool, cnp->cn_pnbuf);
4585ac2d602Sdownsj out1:
4595ac2d602Sdownsj if (dvp != vp)
46036bb23f1Svisa VOP_UNLOCK(vp);
4615ac2d602Sdownsj out2:
4625ac2d602Sdownsj vput(dvp);
4635ac2d602Sdownsj return (error);
4645ac2d602Sdownsj }
4655ac2d602Sdownsj
4665ac2d602Sdownsj /*
4675ac2d602Sdownsj * Rename system call.
4685ac2d602Sdownsj * rename("foo", "bar");
4695ac2d602Sdownsj * is essentially
4705ac2d602Sdownsj * unlink("bar");
4715ac2d602Sdownsj * link("foo", "bar");
4725ac2d602Sdownsj * unlink("foo");
4735ac2d602Sdownsj * but ``atomically''. Can't do full commit without saving state in the
4745ac2d602Sdownsj * inode on disk which isn't feasible at this time. Best we can do is
4755ac2d602Sdownsj * always guarantee the target exists.
4765ac2d602Sdownsj *
4775ac2d602Sdownsj * Basic algorithm is:
4785ac2d602Sdownsj *
4795ac2d602Sdownsj * 1) Bump link count on source while we're linking it to the
4805ac2d602Sdownsj * target. This also ensure the inode won't be deleted out
4815ac2d602Sdownsj * from underneath us while we work (it may be truncated by
4825ac2d602Sdownsj * a concurrent `trunc' or `open' for creation).
4835ac2d602Sdownsj * 2) Link source to destination. If destination already exists,
4845ac2d602Sdownsj * delete it first.
4855ac2d602Sdownsj * 3) Unlink source reference to inode if still around. If a
4865ac2d602Sdownsj * directory was moved and the parent of the destination
4875ac2d602Sdownsj * is different from the source, patch the ".." entry in the
4885ac2d602Sdownsj * directory.
4895ac2d602Sdownsj */
4905ac2d602Sdownsj int
ext2fs_rename(void * v)4915f64cd9cSjasper ext2fs_rename(void *v)
4925ac2d602Sdownsj {
49399bc9d31Sderaadt struct vop_rename_args *ap = v;
4945ac2d602Sdownsj struct vnode *tvp = ap->a_tvp;
4955f64cd9cSjasper struct vnode *tdvp = ap->a_tdvp;
4965ac2d602Sdownsj struct vnode *fvp = ap->a_fvp;
4975f64cd9cSjasper struct vnode *fdvp = ap->a_fdvp;
4985f64cd9cSjasper struct componentname *tcnp = ap->a_tcnp;
4995f64cd9cSjasper struct componentname *fcnp = ap->a_fcnp;
5005f64cd9cSjasper struct inode *ip, *xp, *dp;
5015ac2d602Sdownsj struct ext2fs_dirtemplate dirbuf;
5025f64cd9cSjasper /* struct timespec ts; */
5035ac2d602Sdownsj int doingdirectory = 0, oldparent = 0, newparent = 0;
5045ac2d602Sdownsj int error = 0;
5055ac2d602Sdownsj u_char namlen;
5065ac2d602Sdownsj
5075ac2d602Sdownsj #ifdef DIAGNOSTIC
5085ac2d602Sdownsj if ((tcnp->cn_flags & HASBUF) == 0 ||
5095ac2d602Sdownsj (fcnp->cn_flags & HASBUF) == 0)
5105ac2d602Sdownsj panic("ext2fs_rename: no name");
5115ac2d602Sdownsj #endif
5125ac2d602Sdownsj /*
5135ac2d602Sdownsj * Check for cross-device rename.
5145ac2d602Sdownsj */
5155ac2d602Sdownsj if ((fvp->v_mount != tdvp->v_mount) ||
5165ac2d602Sdownsj (tvp && (fvp->v_mount != tvp->v_mount))) {
5175ac2d602Sdownsj error = EXDEV;
5185ac2d602Sdownsj abortit:
5195ac2d602Sdownsj VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
5205ac2d602Sdownsj if (tdvp == tvp)
5215ac2d602Sdownsj vrele(tdvp);
5225ac2d602Sdownsj else
5235ac2d602Sdownsj vput(tdvp);
5245ac2d602Sdownsj if (tvp)
5255ac2d602Sdownsj vput(tvp);
5265ac2d602Sdownsj VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
5275ac2d602Sdownsj vrele(fdvp);
5285ac2d602Sdownsj vrele(fvp);
5295ac2d602Sdownsj return (error);
5305ac2d602Sdownsj }
5315ac2d602Sdownsj
5325ac2d602Sdownsj /*
5335ac2d602Sdownsj * Check if just deleting a link name.
5345ac2d602Sdownsj */
5355ac2d602Sdownsj if (tvp && ((VTOI(tvp)->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
5365ac2d602Sdownsj (VTOI(tdvp)->i_e2fs_flags & EXT2_APPEND))) {
5375ac2d602Sdownsj error = EPERM;
5385ac2d602Sdownsj goto abortit;
5395ac2d602Sdownsj }
5405ac2d602Sdownsj if (fvp == tvp) {
5415ac2d602Sdownsj if (fvp->v_type == VDIR) {
5425ac2d602Sdownsj error = EINVAL;
5435ac2d602Sdownsj goto abortit;
5445ac2d602Sdownsj }
5455ac2d602Sdownsj
5465ac2d602Sdownsj /* Release destination completely. */
5475ac2d602Sdownsj VOP_ABORTOP(tdvp, tcnp);
5485ac2d602Sdownsj vput(tdvp);
5495ac2d602Sdownsj vput(tvp);
5505ac2d602Sdownsj
5515ac2d602Sdownsj /* Delete source. */
5525ac2d602Sdownsj vrele(fdvp);
5535ac2d602Sdownsj vrele(fvp);
5545ac2d602Sdownsj fcnp->cn_flags &= ~MODMASK;
5555ac2d602Sdownsj fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
5565ac2d602Sdownsj if ((fcnp->cn_flags & SAVESTART) == 0)
5575ac2d602Sdownsj panic("ext2fs_rename: lost from startdir");
5585ac2d602Sdownsj fcnp->cn_nameiop = DELETE;
5599baecc3aSthib (void) vfs_relookup(fdvp, &fvp, fcnp);
5605ac2d602Sdownsj return (VOP_REMOVE(fdvp, fvp, fcnp));
5615ac2d602Sdownsj }
5626e880534Svisa if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
5635ac2d602Sdownsj goto abortit;
5645ac2d602Sdownsj dp = VTOI(fdvp);
5655ac2d602Sdownsj ip = VTOI(fvp);
5664592dbefSmillert if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
56736bb23f1Svisa VOP_UNLOCK(fvp);
5684592dbefSmillert error = EMLINK;
5694592dbefSmillert goto abortit;
5704592dbefSmillert }
5715ac2d602Sdownsj if ((ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
5725ac2d602Sdownsj (dp->i_e2fs_flags & EXT2_APPEND)) {
57336bb23f1Svisa VOP_UNLOCK(fvp);
5745ac2d602Sdownsj error = EPERM;
5755ac2d602Sdownsj goto abortit;
5765ac2d602Sdownsj }
5775ac2d602Sdownsj if ((ip->i_e2fs_mode & IFMT) == IFDIR) {
5785ac2d602Sdownsj error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
5795ac2d602Sdownsj if (!error && tvp)
580f5ee6277Sjasoni error = VOP_ACCESS(tvp, VWRITE, tcnp->cn_cred,
581f5ee6277Sjasoni tcnp->cn_proc);
5825ac2d602Sdownsj if (error) {
58336bb23f1Svisa VOP_UNLOCK(fvp);
5845ac2d602Sdownsj error = EACCES;
5855ac2d602Sdownsj goto abortit;
5865ac2d602Sdownsj }
5875ac2d602Sdownsj /*
5885ac2d602Sdownsj * Avoid ".", "..", and aliases of "." for obvious reasons.
5895ac2d602Sdownsj */
5905ac2d602Sdownsj if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
591d56d3b9eSderaadt dp == ip ||
592d56d3b9eSderaadt (fcnp->cn_flags&ISDOTDOT) ||
593d56d3b9eSderaadt (tcnp->cn_flags & ISDOTDOT) ||
5945ac2d602Sdownsj (ip->i_flag & IN_RENAME)) {
59536bb23f1Svisa VOP_UNLOCK(fvp);
5965ac2d602Sdownsj error = EINVAL;
5975ac2d602Sdownsj goto abortit;
5985ac2d602Sdownsj }
5995ac2d602Sdownsj ip->i_flag |= IN_RENAME;
6005ac2d602Sdownsj oldparent = dp->i_number;
6015ac2d602Sdownsj doingdirectory++;
6025ac2d602Sdownsj }
6035ac2d602Sdownsj vrele(fdvp);
6045ac2d602Sdownsj
6055ac2d602Sdownsj /*
6065ac2d602Sdownsj * When the target exists, both the directory
6075ac2d602Sdownsj * and target vnodes are returned locked.
6085ac2d602Sdownsj */
6095ac2d602Sdownsj dp = VTOI(tdvp);
6105ac2d602Sdownsj xp = NULL;
6115ac2d602Sdownsj if (tvp)
6125ac2d602Sdownsj xp = VTOI(tvp);
6135ac2d602Sdownsj
6145ac2d602Sdownsj /*
6155ac2d602Sdownsj * 1) Bump link count while we're moving stuff
6165ac2d602Sdownsj * around. If we crash somewhere before
6175ac2d602Sdownsj * completing our work, the link count
6185ac2d602Sdownsj * may be wrong, but correctable.
6195ac2d602Sdownsj */
6205ac2d602Sdownsj ip->i_e2fs_nlink++;
6215ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
622d4fc1c49Sguenther if ((error = ext2fs_update(ip, 1)) != 0) {
62336bb23f1Svisa VOP_UNLOCK(fvp);
6245ac2d602Sdownsj goto bad;
6255ac2d602Sdownsj }
6265ac2d602Sdownsj
6275ac2d602Sdownsj /*
6285ac2d602Sdownsj * If ".." must be changed (ie the directory gets a new
6295ac2d602Sdownsj * parent) then the source directory must not be in the
6302f4b598dStedu * directory hierarchy above the target, as this would
6315ac2d602Sdownsj * orphan everything below the source directory. Also
6325ac2d602Sdownsj * the user must have write permission in the source so
6335ac2d602Sdownsj * as to be able to change "..". We must repeat the call
6345ac2d602Sdownsj * to namei, as the parent directory is unlocked by the
6355ac2d602Sdownsj * call to checkpath().
6365ac2d602Sdownsj */
6375ac2d602Sdownsj error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
63836bb23f1Svisa VOP_UNLOCK(fvp);
6395ac2d602Sdownsj if (oldparent != dp->i_number)
6405ac2d602Sdownsj newparent = dp->i_number;
6415ac2d602Sdownsj if (doingdirectory && newparent) {
6425ac2d602Sdownsj if (error) /* write access check above */
6435ac2d602Sdownsj goto bad;
6445ac2d602Sdownsj if (xp != NULL)
6455ac2d602Sdownsj vput(tvp);
6465ac2d602Sdownsj error = ext2fs_checkpath(ip, dp, tcnp->cn_cred);
6475ac2d602Sdownsj if (error != 0)
6485ac2d602Sdownsj goto out;
6495ac2d602Sdownsj if ((tcnp->cn_flags & SAVESTART) == 0)
6505ac2d602Sdownsj panic("ext2fs_rename: lost to startdir");
6519baecc3aSthib if ((error = vfs_relookup(tdvp, &tvp, tcnp)) != 0)
6525ac2d602Sdownsj goto out;
6535ac2d602Sdownsj dp = VTOI(tdvp);
6545ac2d602Sdownsj xp = NULL;
6555ac2d602Sdownsj if (tvp)
6565ac2d602Sdownsj xp = VTOI(tvp);
6575ac2d602Sdownsj }
6585ac2d602Sdownsj /*
6595ac2d602Sdownsj * 2) If target doesn't exist, link the target
6605ac2d602Sdownsj * to the source and unlink the source.
6615ac2d602Sdownsj * Otherwise, rewrite the target directory
6625ac2d602Sdownsj * entry to reference the source inode and
6635ac2d602Sdownsj * expunge the original entry's existence.
6645ac2d602Sdownsj */
6655ac2d602Sdownsj if (xp == NULL) {
6665ac2d602Sdownsj if (dp->i_dev != ip->i_dev)
6675ac2d602Sdownsj panic("rename: EXDEV");
6685ac2d602Sdownsj /*
6695ac2d602Sdownsj * Account for ".." in new directory.
6705ac2d602Sdownsj * When source and destination have the same
6715ac2d602Sdownsj * parent we don't fool with the link count.
6725ac2d602Sdownsj */
6735ac2d602Sdownsj if (doingdirectory && newparent) {
6745ac2d602Sdownsj if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
6755ac2d602Sdownsj error = EMLINK;
6765ac2d602Sdownsj goto bad;
6775ac2d602Sdownsj }
6785ac2d602Sdownsj dp->i_e2fs_nlink++;
6795ac2d602Sdownsj dp->i_flag |= IN_CHANGE;
680d4fc1c49Sguenther if ((error = ext2fs_update(dp, 1)) != 0)
6815ac2d602Sdownsj goto bad;
6825ac2d602Sdownsj }
6835ac2d602Sdownsj error = ext2fs_direnter(ip, tdvp, tcnp);
6845ac2d602Sdownsj if (error != 0) {
6855ac2d602Sdownsj if (doingdirectory && newparent) {
6865ac2d602Sdownsj dp->i_e2fs_nlink--;
6875ac2d602Sdownsj dp->i_flag |= IN_CHANGE;
688d4fc1c49Sguenther (void)ext2fs_update(dp, 1);
6895ac2d602Sdownsj }
6905ac2d602Sdownsj goto bad;
6915ac2d602Sdownsj }
6925ac2d602Sdownsj vput(tdvp);
6935ac2d602Sdownsj } else {
6945ac2d602Sdownsj if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
6955ac2d602Sdownsj panic("rename: EXDEV");
6965ac2d602Sdownsj /*
6975ac2d602Sdownsj * Short circuit rename(foo, foo).
6985ac2d602Sdownsj */
6995ac2d602Sdownsj if (xp->i_number == ip->i_number)
7005ac2d602Sdownsj panic("rename: same file");
7015ac2d602Sdownsj /*
7025ac2d602Sdownsj * If the parent directory is "sticky", then the user must
7035ac2d602Sdownsj * own the parent directory, or the destination of the rename,
7045ac2d602Sdownsj * otherwise the destination may not be changed (except by
7055ac2d602Sdownsj * root). This implements append-only directories.
7065ac2d602Sdownsj */
7075ac2d602Sdownsj if ((dp->i_e2fs_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
7085ac2d602Sdownsj tcnp->cn_cred->cr_uid != dp->i_e2fs_uid &&
7095ac2d602Sdownsj xp->i_e2fs_uid != tcnp->cn_cred->cr_uid) {
7105ac2d602Sdownsj error = EPERM;
7115ac2d602Sdownsj goto bad;
7125ac2d602Sdownsj }
7135ac2d602Sdownsj /*
7145ac2d602Sdownsj * Target must be empty if a directory and have no links
7155ac2d602Sdownsj * to it. Also, ensure source and target are compatible
7165ac2d602Sdownsj * (both directories, or both not directories).
7175ac2d602Sdownsj */
7185ac2d602Sdownsj if ((xp->i_e2fs_mode & IFMT) == IFDIR) {
7195ac2d602Sdownsj if (!ext2fs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
7205ac2d602Sdownsj xp->i_e2fs_nlink > 2) {
7215ac2d602Sdownsj error = ENOTEMPTY;
7225ac2d602Sdownsj goto bad;
7235ac2d602Sdownsj }
7245ac2d602Sdownsj if (!doingdirectory) {
7255ac2d602Sdownsj error = ENOTDIR;
7265ac2d602Sdownsj goto bad;
7275ac2d602Sdownsj }
7285ac2d602Sdownsj cache_purge(tdvp);
7295ac2d602Sdownsj } else if (doingdirectory) {
7305ac2d602Sdownsj error = EISDIR;
7315ac2d602Sdownsj goto bad;
7325ac2d602Sdownsj }
7335ac2d602Sdownsj error = ext2fs_dirrewrite(dp, ip, tcnp);
7345ac2d602Sdownsj if (error != 0)
7355ac2d602Sdownsj goto bad;
7365ac2d602Sdownsj /*
7375ac2d602Sdownsj * If the target directory is in the same
7385ac2d602Sdownsj * directory as the source directory,
7395ac2d602Sdownsj * decrement the link count on the parent
7405ac2d602Sdownsj * of the target directory.
7415ac2d602Sdownsj */
7425ac2d602Sdownsj if (doingdirectory && !newparent) {
7435ac2d602Sdownsj dp->i_e2fs_nlink--;
7445ac2d602Sdownsj dp->i_flag |= IN_CHANGE;
7455ac2d602Sdownsj }
7465ac2d602Sdownsj vput(tdvp);
7475ac2d602Sdownsj /*
7485ac2d602Sdownsj * Adjust the link count of the target to
7495ac2d602Sdownsj * reflect the dirrewrite above. If this is
7505ac2d602Sdownsj * a directory it is empty and there are
7515ac2d602Sdownsj * no links to it, so we can squash the inode and
7525ac2d602Sdownsj * any space associated with it. We disallowed
7535ac2d602Sdownsj * renaming over top of a directory with links to
7545ac2d602Sdownsj * it above, as the remaining link would point to
7555ac2d602Sdownsj * a directory without "." or ".." entries.
7565ac2d602Sdownsj */
7575ac2d602Sdownsj xp->i_e2fs_nlink--;
7585ac2d602Sdownsj if (doingdirectory) {
7595ac2d602Sdownsj if (--xp->i_e2fs_nlink != 0)
7605ac2d602Sdownsj panic("rename: linked directory");
761b080ad39Scsapuntz error = ext2fs_truncate(xp, (off_t)0, IO_SYNC,
762b080ad39Scsapuntz tcnp->cn_cred);
7635ac2d602Sdownsj }
7645ac2d602Sdownsj xp->i_flag |= IN_CHANGE;
7655ac2d602Sdownsj vput(tvp);
7665ac2d602Sdownsj xp = NULL;
7675ac2d602Sdownsj }
7685ac2d602Sdownsj
7695ac2d602Sdownsj /*
7705ac2d602Sdownsj * 3) Unlink the source.
7715ac2d602Sdownsj */
7725ac2d602Sdownsj fcnp->cn_flags &= ~MODMASK;
7735ac2d602Sdownsj fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
7745ac2d602Sdownsj if ((fcnp->cn_flags & SAVESTART) == 0)
7755ac2d602Sdownsj panic("ext2fs_rename: lost from startdir");
7769baecc3aSthib (void) vfs_relookup(fdvp, &fvp, fcnp);
7775ac2d602Sdownsj if (fvp != NULL) {
7785ac2d602Sdownsj xp = VTOI(fvp);
7795ac2d602Sdownsj dp = VTOI(fdvp);
7805ac2d602Sdownsj } else {
7815ac2d602Sdownsj /*
7825ac2d602Sdownsj * From name has disappeared.
7835ac2d602Sdownsj */
7845ac2d602Sdownsj if (doingdirectory)
7855ac2d602Sdownsj panic("ext2fs_rename: lost dir entry");
7865ac2d602Sdownsj vrele(ap->a_fvp);
7875ac2d602Sdownsj return (0);
7885ac2d602Sdownsj }
7895ac2d602Sdownsj /*
7905ac2d602Sdownsj * Ensure that the directory entry still exists and has not
7915ac2d602Sdownsj * changed while the new name has been entered. If the source is
7925ac2d602Sdownsj * a file then the entry may have been unlinked or renamed. In
7935ac2d602Sdownsj * either case there is no further work to be done. If the source
7945ac2d602Sdownsj * is a directory then it cannot have been rmdir'ed; its link
7955ac2d602Sdownsj * count of three would cause a rmdir to fail with ENOTEMPTY.
7965ac2d602Sdownsj * The IRENAME flag ensures that it cannot be moved by another
7975ac2d602Sdownsj * rename.
7985ac2d602Sdownsj */
7995ac2d602Sdownsj if (xp != ip) {
8005ac2d602Sdownsj if (doingdirectory)
8015ac2d602Sdownsj panic("ext2fs_rename: lost dir entry");
8025ac2d602Sdownsj } else {
8035ac2d602Sdownsj /*
8045ac2d602Sdownsj * If the source is a directory with a
8055ac2d602Sdownsj * new parent, the link count of the old
8065ac2d602Sdownsj * parent directory must be decremented
8075ac2d602Sdownsj * and ".." set to point to the new parent.
8085ac2d602Sdownsj */
8095ac2d602Sdownsj if (doingdirectory && newparent) {
8105ac2d602Sdownsj dp->i_e2fs_nlink--;
8115ac2d602Sdownsj dp->i_flag |= IN_CHANGE;
8125ac2d602Sdownsj error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
8135ac2d602Sdownsj sizeof (struct ext2fs_dirtemplate), (off_t)0,
8145ac2d602Sdownsj UIO_SYSSPACE, IO_NODELOCKED,
815717ea6e5Soga tcnp->cn_cred, NULL, curproc);
8165ac2d602Sdownsj if (error == 0) {
8175ac2d602Sdownsj namlen = dirbuf.dotdot_namlen;
8185ac2d602Sdownsj if (namlen != 2 ||
8195ac2d602Sdownsj dirbuf.dotdot_name[0] != '.' ||
8205ac2d602Sdownsj dirbuf.dotdot_name[1] != '.') {
8215ac2d602Sdownsj ufs_dirbad(xp, (doff_t)12,
8225ac2d602Sdownsj "ext2fs_rename: mangled dir");
8235ac2d602Sdownsj } else {
824f7dbefaaSpelikan dirbuf.dotdot_ino = htole32(newparent);
8255ac2d602Sdownsj (void) vn_rdwr(UIO_WRITE, fvp,
8265ac2d602Sdownsj (caddr_t)&dirbuf,
8275ac2d602Sdownsj sizeof (struct dirtemplate),
8285ac2d602Sdownsj (off_t)0, UIO_SYSSPACE,
8295ac2d602Sdownsj IO_NODELOCKED|IO_SYNC,
830717ea6e5Soga tcnp->cn_cred, NULL, curproc);
8315ac2d602Sdownsj cache_purge(fdvp);
8325ac2d602Sdownsj }
8335ac2d602Sdownsj }
8345ac2d602Sdownsj }
8355ac2d602Sdownsj error = ext2fs_dirremove(fdvp, fcnp);
8365ac2d602Sdownsj if (!error) {
8375ac2d602Sdownsj xp->i_e2fs_nlink--;
8385ac2d602Sdownsj xp->i_flag |= IN_CHANGE;
8395ac2d602Sdownsj }
8405ac2d602Sdownsj xp->i_flag &= ~IN_RENAME;
8415ac2d602Sdownsj }
8425ac2d602Sdownsj if (dp)
8435ac2d602Sdownsj vput(fdvp);
8445ac2d602Sdownsj if (xp)
8455ac2d602Sdownsj vput(fvp);
8465ac2d602Sdownsj vrele(ap->a_fvp);
8475ac2d602Sdownsj return (error);
8485ac2d602Sdownsj
8495ac2d602Sdownsj bad:
8505ac2d602Sdownsj if (xp)
8515ac2d602Sdownsj vput(ITOV(xp));
8525ac2d602Sdownsj vput(ITOV(dp));
8535ac2d602Sdownsj out:
8545ac2d602Sdownsj if (doingdirectory)
8555ac2d602Sdownsj ip->i_flag &= ~IN_RENAME;
8566e880534Svisa if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
8575ac2d602Sdownsj ip->i_e2fs_nlink--;
8585ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
8595ac2d602Sdownsj vput(fvp);
8605ac2d602Sdownsj } else
8615ac2d602Sdownsj vrele(fvp);
8625ac2d602Sdownsj return (error);
8635ac2d602Sdownsj }
8645ac2d602Sdownsj
8655ac2d602Sdownsj /*
8665ac2d602Sdownsj * Mkdir system call
8675ac2d602Sdownsj */
8685ac2d602Sdownsj int
ext2fs_mkdir(void * v)8695f64cd9cSjasper ext2fs_mkdir(void *v)
8705ac2d602Sdownsj {
87199bc9d31Sderaadt struct vop_mkdir_args *ap = v;
8725f64cd9cSjasper struct vnode *dvp = ap->a_dvp;
8735f64cd9cSjasper struct vattr *vap = ap->a_vap;
8745f64cd9cSjasper struct componentname *cnp = ap->a_cnp;
8755f64cd9cSjasper struct inode *ip, *dp;
8765ac2d602Sdownsj struct vnode *tvp;
877f5ee6277Sjasoni struct ext2fs_dirtemplate dirtemplate;
878cc2fc615Smillert mode_t dmode;
879cc2fc615Smillert int error;
8805ac2d602Sdownsj
8815ac2d602Sdownsj #ifdef DIAGNOSTIC
8825ac2d602Sdownsj if ((cnp->cn_flags & HASBUF) == 0)
8835ac2d602Sdownsj panic("ext2fs_mkdir: no name");
8845ac2d602Sdownsj #endif
8855ac2d602Sdownsj dp = VTOI(dvp);
8865ac2d602Sdownsj if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
8875ac2d602Sdownsj error = EMLINK;
8885ac2d602Sdownsj goto out;
8895ac2d602Sdownsj }
8901f3ff51cSdownsj dmode = vap->va_mode & ACCESSPERMS;
8915ac2d602Sdownsj dmode |= IFDIR;
8925ac2d602Sdownsj /*
8935ac2d602Sdownsj * Must simulate part of ext2fs_makeinode here to acquire the inode,
8945ac2d602Sdownsj * but not have it entered in the parent directory. The entry is
8955ac2d602Sdownsj * made later after writing "." and ".." entries.
8965ac2d602Sdownsj */
897b080ad39Scsapuntz if ((error = ext2fs_inode_alloc(dp, dmode, cnp->cn_cred, &tvp)) != 0)
8985ac2d602Sdownsj goto out;
8995ac2d602Sdownsj ip = VTOI(tvp);
9005ac2d602Sdownsj ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
9015ac2d602Sdownsj ip->i_e2fs_gid = dp->i_e2fs_gid;
9025ac2d602Sdownsj ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
9035ac2d602Sdownsj ip->i_e2fs_mode = dmode;
9045ac2d602Sdownsj tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
9055ac2d602Sdownsj ip->i_e2fs_nlink = 2;
906d4fc1c49Sguenther error = ext2fs_update(ip, 1);
9075ac2d602Sdownsj
9085ac2d602Sdownsj /*
9095ac2d602Sdownsj * Bump link count in parent directory
9105ac2d602Sdownsj * to reflect work done below. Should
9115ac2d602Sdownsj * be done before reference is created
9125ac2d602Sdownsj * so reparation is possible if we crash.
9135ac2d602Sdownsj */
9145ac2d602Sdownsj dp->i_e2fs_nlink++;
9155ac2d602Sdownsj dp->i_flag |= IN_CHANGE;
916d4fc1c49Sguenther if ((error = ext2fs_update(dp, 1)) != 0)
9175ac2d602Sdownsj goto bad;
9185ac2d602Sdownsj
9195ac2d602Sdownsj /* Initialize directory with "." and ".." from static template. */
9200f5c6c8bStedu memset(&dirtemplate, 0, sizeof(dirtemplate));
921f7dbefaaSpelikan dirtemplate.dot_ino = htole32(ip->i_number);
922f7dbefaaSpelikan dirtemplate.dot_reclen = htole16(12);
923f5ee6277Sjasoni dirtemplate.dot_namlen = 1;
924f5ee6277Sjasoni if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
925f5ee6277Sjasoni (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
926f5ee6277Sjasoni dirtemplate.dot_type = EXT2_FT_DIR;
927f5ee6277Sjasoni }
928f5ee6277Sjasoni dirtemplate.dot_name[0] = '.';
929f7dbefaaSpelikan dirtemplate.dotdot_ino = htole32(dp->i_number);
930f7dbefaaSpelikan dirtemplate.dotdot_reclen = htole16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
931f5ee6277Sjasoni dirtemplate.dotdot_namlen = 2;
932f5ee6277Sjasoni if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
933f5ee6277Sjasoni (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
934f5ee6277Sjasoni dirtemplate.dotdot_type = EXT2_FT_DIR;
935f5ee6277Sjasoni }
936f5ee6277Sjasoni dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
9375ac2d602Sdownsj error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
9385ac2d602Sdownsj sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
939717ea6e5Soga IO_NODELOCKED|IO_SYNC, cnp->cn_cred, NULL, curproc);
9405ac2d602Sdownsj if (error) {
9415ac2d602Sdownsj dp->i_e2fs_nlink--;
9425ac2d602Sdownsj dp->i_flag |= IN_CHANGE;
9435ac2d602Sdownsj goto bad;
9445ac2d602Sdownsj }
9455ac2d602Sdownsj if (VTOI(dvp)->i_e2fs->e2fs_bsize >
9465ac2d602Sdownsj VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
9475ac2d602Sdownsj panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
9485ac2d602Sdownsj else {
9494dcbbbb0Sniallo error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize);
9504dcbbbb0Sniallo if (error) {
9514dcbbbb0Sniallo dp->i_e2fs_nlink--;
9524dcbbbb0Sniallo dp->i_flag |= IN_CHANGE;
9534dcbbbb0Sniallo goto bad;
9544dcbbbb0Sniallo }
9555ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
9565ac2d602Sdownsj }
9575ac2d602Sdownsj
9580f1683a6Smiod /* Directory set up, now install its entry in the parent directory. */
9595ac2d602Sdownsj error = ext2fs_direnter(ip, dvp, cnp);
9605ac2d602Sdownsj if (error != 0) {
9615ac2d602Sdownsj dp->i_e2fs_nlink--;
9625ac2d602Sdownsj dp->i_flag |= IN_CHANGE;
9635ac2d602Sdownsj }
9645ac2d602Sdownsj bad:
9655ac2d602Sdownsj /*
9665ac2d602Sdownsj * No need to do an explicit VOP_TRUNCATE here, vrele will do this
9675ac2d602Sdownsj * for us because we set the link count to 0.
9685ac2d602Sdownsj */
9695ac2d602Sdownsj if (error) {
9705ac2d602Sdownsj ip->i_e2fs_nlink = 0;
9715ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
9725ac2d602Sdownsj vput(tvp);
9735ac2d602Sdownsj } else
9745ac2d602Sdownsj *ap->a_vpp = tvp;
9755ac2d602Sdownsj out:
976dbe27ba0Stedu pool_put(&namei_pool, cnp->cn_pnbuf);
9775ac2d602Sdownsj vput(dvp);
9785ac2d602Sdownsj return (error);
9795ac2d602Sdownsj }
9805ac2d602Sdownsj
9815ac2d602Sdownsj /*
9825ac2d602Sdownsj * Rmdir system call.
9835ac2d602Sdownsj */
9845ac2d602Sdownsj int
ext2fs_rmdir(void * v)9855f64cd9cSjasper ext2fs_rmdir(void *v)
9865ac2d602Sdownsj {
98799bc9d31Sderaadt struct vop_rmdir_args *ap = v;
9885f64cd9cSjasper struct vnode *vp = ap->a_vp;
9895f64cd9cSjasper struct vnode *dvp = ap->a_dvp;
9905f64cd9cSjasper struct componentname *cnp = ap->a_cnp;
9915f64cd9cSjasper struct inode *ip, *dp;
9925ac2d602Sdownsj int error;
9935ac2d602Sdownsj
9945ac2d602Sdownsj ip = VTOI(vp);
9955ac2d602Sdownsj dp = VTOI(dvp);
9965ac2d602Sdownsj /*
9975ac2d602Sdownsj * Verify the directory is empty (and valid).
9985ac2d602Sdownsj * (Rmdir ".." won't be valid since
9995ac2d602Sdownsj * ".." will contain a reference to
10005ac2d602Sdownsj * the current directory and thus be
10015ac2d602Sdownsj * non-empty.)
10025ac2d602Sdownsj */
10035ac2d602Sdownsj error = 0;
10045ac2d602Sdownsj if (ip->i_e2fs_nlink != 2 ||
10055ac2d602Sdownsj !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
10065ac2d602Sdownsj error = ENOTEMPTY;
10075ac2d602Sdownsj goto out;
10085ac2d602Sdownsj }
10095ac2d602Sdownsj if ((dp->i_e2fs_flags & EXT2_APPEND) ||
10105ac2d602Sdownsj (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
10115ac2d602Sdownsj error = EPERM;
10125ac2d602Sdownsj goto out;
10135ac2d602Sdownsj }
10145ac2d602Sdownsj /*
10155ac2d602Sdownsj * Delete reference to directory before purging
10165ac2d602Sdownsj * inode. If we crash in between, the directory
10175ac2d602Sdownsj * will be reattached to lost+found,
10185ac2d602Sdownsj */
10195ac2d602Sdownsj error = ext2fs_dirremove(dvp, cnp);
10205ac2d602Sdownsj if (error != 0)
10215ac2d602Sdownsj goto out;
10225ac2d602Sdownsj dp->i_e2fs_nlink--;
10235ac2d602Sdownsj dp->i_flag |= IN_CHANGE;
10245ac2d602Sdownsj cache_purge(dvp);
10255ac2d602Sdownsj vput(dvp);
10265ac2d602Sdownsj dvp = NULL;
10275ac2d602Sdownsj /*
10285ac2d602Sdownsj * Truncate inode. The only stuff left
10295ac2d602Sdownsj * in the directory is "." and "..". The
10305ac2d602Sdownsj * "." reference is inconsequential since
10315ac2d602Sdownsj * we're quashing it. The ".." reference
10325ac2d602Sdownsj * has already been adjusted above. We've
10335ac2d602Sdownsj * removed the "." reference and the reference
10345ac2d602Sdownsj * in the parent directory, but there may be
10355ac2d602Sdownsj * other hard links so decrement by 2 and
10365ac2d602Sdownsj * worry about them later.
10375ac2d602Sdownsj */
10385ac2d602Sdownsj ip->i_e2fs_nlink -= 2;
1039b080ad39Scsapuntz error = ext2fs_truncate(ip, (off_t)0, IO_SYNC, cnp->cn_cred);
10405ac2d602Sdownsj cache_purge(ITOV(ip));
10415ac2d602Sdownsj out:
10425ac2d602Sdownsj if (dvp)
10435ac2d602Sdownsj vput(dvp);
10445ac2d602Sdownsj vput(vp);
10455ac2d602Sdownsj return (error);
10465ac2d602Sdownsj }
10475ac2d602Sdownsj
10485ac2d602Sdownsj /*
10495ac2d602Sdownsj * symlink -- make a symbolic link
10505ac2d602Sdownsj */
10515ac2d602Sdownsj int
ext2fs_symlink(void * v)10525f64cd9cSjasper ext2fs_symlink(void *v)
10535ac2d602Sdownsj {
105499bc9d31Sderaadt struct vop_symlink_args *ap = v;
10555f64cd9cSjasper struct vnode *vp, **vpp = ap->a_vpp;
10565f64cd9cSjasper struct inode *ip;
10575ac2d602Sdownsj int len, error;
10585ac2d602Sdownsj
10595ac2d602Sdownsj error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
10605ac2d602Sdownsj vpp, ap->a_cnp);
10619d1c6d70Svisa vput(ap->a_dvp);
10625ac2d602Sdownsj if (error)
10635ac2d602Sdownsj return (error);
10645ac2d602Sdownsj vp = *vpp;
10655ac2d602Sdownsj len = strlen(ap->a_target);
10663484f39eSnatano if (len < EXT2_MAXSYMLINKLEN) {
10675ac2d602Sdownsj ip = VTOI(vp);
10680f5c6c8bStedu memcpy(ip->i_e2din->e2di_shortlink, ap->a_target, len);
10694dcbbbb0Sniallo error = ext2fs_setsize(ip, len);
10704dcbbbb0Sniallo if (error)
10714dcbbbb0Sniallo goto bad;
10725ac2d602Sdownsj ip->i_flag |= IN_CHANGE | IN_UPDATE;
10735ac2d602Sdownsj } else
10745ac2d602Sdownsj error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
10759b355cb2Smillert UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, NULL,
1076717ea6e5Soga curproc);
10774dcbbbb0Sniallo bad:
10785ac2d602Sdownsj vput(vp);
10795ac2d602Sdownsj return (error);
10805ac2d602Sdownsj }
10815ac2d602Sdownsj
10825ac2d602Sdownsj /*
10835ac2d602Sdownsj * Return target name of a symbolic link
10845ac2d602Sdownsj */
10855ac2d602Sdownsj int
ext2fs_readlink(void * v)10865f64cd9cSjasper ext2fs_readlink(void *v)
10875ac2d602Sdownsj {
108899bc9d31Sderaadt struct vop_readlink_args *ap = v;
10895f64cd9cSjasper struct vnode *vp = ap->a_vp;
10905f64cd9cSjasper struct inode *ip = VTOI(vp);
1091dc30eddbSstefan u_int64_t isize;
10925ac2d602Sdownsj
10934dcbbbb0Sniallo isize = ext2fs_size(ip);
10943484f39eSnatano if (isize < EXT2_MAXSYMLINKLEN) {
1095dc30eddbSstefan return (uiomove((char *)ip->i_e2din->e2di_shortlink, isize,
10961330d409Smatthew ap->a_uio));
10975ac2d602Sdownsj }
10985ac2d602Sdownsj return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
10995ac2d602Sdownsj }
11005ac2d602Sdownsj
11015ac2d602Sdownsj /*
1102d4648cd6Sguenther * Return POSIX pathconf information applicable to ext2 filesystems.
1103d4648cd6Sguenther */
1104d4648cd6Sguenther int
ext2fs_pathconf(void * v)1105d4648cd6Sguenther ext2fs_pathconf(void *v)
1106d4648cd6Sguenther {
1107d4648cd6Sguenther struct vop_pathconf_args *ap = v;
1108d4648cd6Sguenther int error = 0;
1109d4648cd6Sguenther
1110d4648cd6Sguenther switch (ap->a_name) {
1111d4648cd6Sguenther case _PC_TIMESTAMP_RESOLUTION:
1112d4648cd6Sguenther *ap->a_retval = 1000000000; /* 1 billion nanoseconds */
1113d4648cd6Sguenther break;
1114d4648cd6Sguenther default:
1115d4648cd6Sguenther return (ufs_pathconf(v));
1116d4648cd6Sguenther }
1117d4648cd6Sguenther
1118d4648cd6Sguenther return (error);
1119d4648cd6Sguenther }
1120d4648cd6Sguenther
1121d4648cd6Sguenther /*
11225ac2d602Sdownsj * Advisory record locking support
11235ac2d602Sdownsj */
11245ac2d602Sdownsj int
ext2fs_advlock(void * v)11255f64cd9cSjasper ext2fs_advlock(void *v)
11265ac2d602Sdownsj {
112799bc9d31Sderaadt struct vop_advlock_args *ap = v;
11285f64cd9cSjasper struct inode *ip = VTOI(ap->a_vp);
11295ac2d602Sdownsj
11304dcbbbb0Sniallo return (lf_advlock(&ip->i_lockf, ext2fs_size(ip), ap->a_id, ap->a_op,
11315ac2d602Sdownsj ap->a_fl, ap->a_flags));
11325ac2d602Sdownsj }
11335ac2d602Sdownsj
11345ac2d602Sdownsj /*
11355ac2d602Sdownsj * Allocate a new inode.
11365ac2d602Sdownsj */
11375ac2d602Sdownsj int
ext2fs_makeinode(int mode,struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp)11385f64cd9cSjasper ext2fs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
11395f64cd9cSjasper struct componentname *cnp)
11405ac2d602Sdownsj {
11415f64cd9cSjasper struct inode *ip, *pdir;
11425ac2d602Sdownsj struct vnode *tvp;
11435ac2d602Sdownsj int error;
11445ac2d602Sdownsj
11455ac2d602Sdownsj pdir = VTOI(dvp);
11465ac2d602Sdownsj #ifdef DIAGNOSTIC
11475ac2d602Sdownsj if ((cnp->cn_flags & HASBUF) == 0)
11485ac2d602Sdownsj panic("ext2fs_makeinode: no name");
11495ac2d602Sdownsj #endif
11505ac2d602Sdownsj *vpp = NULL;
11515ac2d602Sdownsj if ((mode & IFMT) == 0)
11525ac2d602Sdownsj mode |= IFREG;
11535ac2d602Sdownsj
1154b080ad39Scsapuntz if ((error = ext2fs_inode_alloc(pdir, mode, cnp->cn_cred, &tvp))
1155b080ad39Scsapuntz != 0) {
1156dbe27ba0Stedu pool_put(&namei_pool, cnp->cn_pnbuf);
11575ac2d602Sdownsj return (error);
11585ac2d602Sdownsj }
11595ac2d602Sdownsj ip = VTOI(tvp);
11605ac2d602Sdownsj ip->i_e2fs_gid = pdir->i_e2fs_gid;
11615ac2d602Sdownsj ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
11625ac2d602Sdownsj ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
11635ac2d602Sdownsj ip->i_e2fs_mode = mode;
11645ac2d602Sdownsj tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
11655ac2d602Sdownsj ip->i_e2fs_nlink = 1;
11665ac2d602Sdownsj if ((ip->i_e2fs_mode & ISGID) &&
11675ac2d602Sdownsj !groupmember(ip->i_e2fs_gid, cnp->cn_cred) &&
1168ad392958Stedu suser_ucred(cnp->cn_cred))
11695ac2d602Sdownsj ip->i_e2fs_mode &= ~ISGID;
11705ac2d602Sdownsj
11715ac2d602Sdownsj /*
11725ac2d602Sdownsj * Make sure inode goes to disk before directory entry.
11735ac2d602Sdownsj */
1174d4fc1c49Sguenther if ((error = ext2fs_update(ip, 1)) != 0)
11755ac2d602Sdownsj goto bad;
11765ac2d602Sdownsj error = ext2fs_direnter(ip, dvp, cnp);
11775ac2d602Sdownsj if (error != 0)
11785ac2d602Sdownsj goto bad;
11795ac2d602Sdownsj if ((cnp->cn_flags & SAVESTART) == 0)
1180dbe27ba0Stedu pool_put(&namei_pool, cnp->cn_pnbuf);
11815ac2d602Sdownsj *vpp = tvp;
11825ac2d602Sdownsj return (0);
11835ac2d602Sdownsj
11845ac2d602Sdownsj bad:
11855ac2d602Sdownsj /*
11865ac2d602Sdownsj * Write error occurred trying to update the inode
11875ac2d602Sdownsj * or the directory so must deallocate the inode.
11885ac2d602Sdownsj */
1189dbe27ba0Stedu pool_put(&namei_pool, cnp->cn_pnbuf);
11905ac2d602Sdownsj ip->i_e2fs_nlink = 0;
11915ac2d602Sdownsj ip->i_flag |= IN_CHANGE;
1192e318f6c7Stedu tvp->v_type = VNON;
11935ac2d602Sdownsj vput(tvp);
11945ac2d602Sdownsj return (error);
11955ac2d602Sdownsj }
11965ac2d602Sdownsj
11975ac2d602Sdownsj /*
11987dc61945Sdownsj * Synch an open file.
11997dc61945Sdownsj */
12007dc61945Sdownsj int
ext2fs_fsync(void * v)12015f64cd9cSjasper ext2fs_fsync(void *v)
12027dc61945Sdownsj {
120399bc9d31Sderaadt struct vop_fsync_args *ap = v;
12045f64cd9cSjasper struct vnode *vp = ap->a_vp;
12057dc61945Sdownsj
12067dc61945Sdownsj vflushbuf(vp, ap->a_waitfor == MNT_WAIT);
1207d4fc1c49Sguenther return (ext2fs_update(VTOI(ap->a_vp), ap->a_waitfor == MNT_WAIT));
12087dc61945Sdownsj }
12097dc61945Sdownsj
12107dc61945Sdownsj /*
12115ac2d602Sdownsj * Reclaim an inode so that it can be used for other purposes.
12125ac2d602Sdownsj */
12135ac2d602Sdownsj int
ext2fs_reclaim(void * v)12145f64cd9cSjasper ext2fs_reclaim(void *v)
12155ac2d602Sdownsj {
121699bc9d31Sderaadt struct vop_reclaim_args *ap = v;
12175f64cd9cSjasper struct vnode *vp = ap->a_vp;
12185ac2d602Sdownsj struct inode *ip;
12194df17c52Spedro #ifdef DIAGNOSTIC
12205ac2d602Sdownsj extern int prtactive;
12215ac2d602Sdownsj
12225ac2d602Sdownsj if (prtactive && vp->v_usecount != 0)
12235ac2d602Sdownsj vprint("ext2fs_reclaim: pushing active", vp);
12244df17c52Spedro #endif
1225d2e0ba60Spedro
12265ac2d602Sdownsj /*
12275ac2d602Sdownsj * Remove the inode from its hash chain.
12285ac2d602Sdownsj */
12295ac2d602Sdownsj ip = VTOI(vp);
12305ac2d602Sdownsj ufs_ihashrem(ip);
1231d2e0ba60Spedro
12325ac2d602Sdownsj /*
12335ac2d602Sdownsj * Purge old data structures associated with the inode.
12345ac2d602Sdownsj */
12355ac2d602Sdownsj cache_purge(vp);
1236d2e0ba60Spedro if (ip->i_devvp)
12375ac2d602Sdownsj vrele(ip->i_devvp);
12385ac2d602Sdownsj
12392374824dSpedro if (ip->i_e2din != NULL)
12402374824dSpedro pool_put(&ext2fs_dinode_pool, ip->i_e2din);
12412374824dSpedro
1242b7ade7a9Spedro pool_put(&ext2fs_inode_pool, ip);
12432374824dSpedro
12445ac2d602Sdownsj vp->v_data = NULL;
1245d2e0ba60Spedro
12465ac2d602Sdownsj return (0);
12475ac2d602Sdownsj }
12485ac2d602Sdownsj
12495ac2d602Sdownsj /* Global vfs data structures for ext2fs. */
12502d6b9e38Sclaudio const struct vops ext2fs_vops = {
1251dc81e71aSthib .vop_lookup = ext2fs_lookup,
1252dc81e71aSthib .vop_create = ext2fs_create,
1253dc81e71aSthib .vop_mknod = ext2fs_mknod,
1254dc81e71aSthib .vop_open = ext2fs_open,
1255dc81e71aSthib .vop_close = ufs_close,
1256dc81e71aSthib .vop_access = ext2fs_access,
1257dc81e71aSthib .vop_getattr = ext2fs_getattr,
1258dc81e71aSthib .vop_setattr = ext2fs_setattr,
1259dc81e71aSthib .vop_read = ext2fs_read,
1260dc81e71aSthib .vop_write = ext2fs_write,
1261dc81e71aSthib .vop_ioctl = ufs_ioctl,
1262b78c6981Smillert .vop_kqfilter = ufs_kqfilter,
12638761c2c6Ssemarie .vop_revoke = NULL,
1264dc81e71aSthib .vop_fsync = ext2fs_fsync,
1265dc81e71aSthib .vop_remove = ext2fs_remove,
1266dc81e71aSthib .vop_link = ext2fs_link,
1267dc81e71aSthib .vop_rename = ext2fs_rename,
1268dc81e71aSthib .vop_mkdir = ext2fs_mkdir,
1269dc81e71aSthib .vop_rmdir = ext2fs_rmdir,
1270dc81e71aSthib .vop_symlink = ext2fs_symlink,
1271dc81e71aSthib .vop_readdir = ext2fs_readdir,
1272dc81e71aSthib .vop_readlink = ext2fs_readlink,
1273dc81e71aSthib .vop_abortop = vop_generic_abortop,
1274dc81e71aSthib .vop_inactive = ext2fs_inactive,
1275dc81e71aSthib .vop_reclaim = ext2fs_reclaim,
1276dc81e71aSthib .vop_lock = ufs_lock,
1277dc81e71aSthib .vop_unlock = ufs_unlock,
1278dc81e71aSthib .vop_bmap = ext2fs_bmap,
1279dc81e71aSthib .vop_strategy = ufs_strategy,
1280dc81e71aSthib .vop_print = ufs_print,
1281dc81e71aSthib .vop_islocked = ufs_islocked,
1282d4648cd6Sguenther .vop_pathconf = ext2fs_pathconf,
1283dc81e71aSthib .vop_advlock = ext2fs_advlock,
12848761c2c6Ssemarie .vop_bwrite = vop_generic_bwrite,
12855ac2d602Sdownsj };
12865ac2d602Sdownsj
12872d6b9e38Sclaudio const struct vops ext2fs_specvops = {
1288dc81e71aSthib .vop_close = ufsspec_close,
1289dc81e71aSthib .vop_access = ext2fs_access,
1290dc81e71aSthib .vop_getattr = ext2fs_getattr,
1291dc81e71aSthib .vop_setattr = ext2fs_setattr,
1292dc81e71aSthib .vop_read = ufsspec_read,
1293dc81e71aSthib .vop_write = ufsspec_write,
1294dc81e71aSthib .vop_fsync = ext2fs_fsync,
1295dc81e71aSthib .vop_inactive = ext2fs_inactive,
1296dc81e71aSthib .vop_reclaim = ext2fs_reclaim,
1297dc81e71aSthib .vop_lock = ufs_lock,
1298dc81e71aSthib .vop_unlock = ufs_unlock,
1299dc81e71aSthib .vop_print = ufs_print,
1300dc81e71aSthib .vop_islocked = ufs_islocked,
1301dc81e71aSthib
1302dc81e71aSthib /* XXX: Keep in sync with spec_vops. */
1303dc81e71aSthib .vop_lookup = vop_generic_lookup,
13049aafff14Ssemarie .vop_create = vop_generic_badop,
13059aafff14Ssemarie .vop_mknod = vop_generic_badop,
1306dc81e71aSthib .vop_open = spec_open,
1307dc81e71aSthib .vop_ioctl = spec_ioctl,
1308dc81e71aSthib .vop_kqfilter = spec_kqfilter,
1309dc81e71aSthib .vop_revoke = vop_generic_revoke,
13109aafff14Ssemarie .vop_remove = vop_generic_badop,
13119aafff14Ssemarie .vop_link = vop_generic_badop,
13129aafff14Ssemarie .vop_rename = vop_generic_badop,
13139aafff14Ssemarie .vop_mkdir = vop_generic_badop,
13149aafff14Ssemarie .vop_rmdir = vop_generic_badop,
13159aafff14Ssemarie .vop_symlink = vop_generic_badop,
13169aafff14Ssemarie .vop_readdir = vop_generic_badop,
13179aafff14Ssemarie .vop_readlink = vop_generic_badop,
13189aafff14Ssemarie .vop_abortop = vop_generic_badop,
1319dc81e71aSthib .vop_bmap = vop_generic_bmap,
1320dc81e71aSthib .vop_strategy = spec_strategy,
1321dc81e71aSthib .vop_pathconf = spec_pathconf,
1322dc81e71aSthib .vop_advlock = spec_advlock,
1323dc81e71aSthib .vop_bwrite = vop_generic_bwrite,
13245ac2d602Sdownsj };
13255ac2d602Sdownsj
13265ac2d602Sdownsj #ifdef FIFO
13272d6b9e38Sclaudio const struct vops ext2fs_fifovops = {
1328dc81e71aSthib .vop_close = ufsfifo_close,
1329dc81e71aSthib .vop_access = ufsfifo_close,
1330dc81e71aSthib .vop_getattr = ext2fs_getattr,
1331dc81e71aSthib .vop_setattr = ext2fs_setattr,
1332dc81e71aSthib .vop_read = ufsfifo_read,
1333dc81e71aSthib .vop_write = ufsfifo_write,
1334dc81e71aSthib .vop_fsync = ext2fs_fsync,
1335dc81e71aSthib .vop_inactive = ext2fs_inactive,
1336dc81e71aSthib .vop_reclaim = ext2fsfifo_reclaim,
1337dc81e71aSthib .vop_lock = ufs_lock,
1338dc81e71aSthib .vop_unlock = ufs_unlock,
1339dc81e71aSthib .vop_print = ufs_print,
1340dc81e71aSthib .vop_islocked = ufs_islocked,
1341dc81e71aSthib .vop_bwrite = vop_generic_bwrite,
1342dc81e71aSthib
1343dc81e71aSthib /* XXX: Keep in sync with fifo_vops */
1344dc81e71aSthib .vop_lookup = vop_generic_lookup,
13459aafff14Ssemarie .vop_create = vop_generic_badop,
13469aafff14Ssemarie .vop_mknod = vop_generic_badop,
1347dc81e71aSthib .vop_open = fifo_open,
1348dc81e71aSthib .vop_ioctl = fifo_ioctl,
1349dc81e71aSthib .vop_kqfilter = fifo_kqfilter,
1350dc81e71aSthib .vop_revoke = vop_generic_revoke,
13519aafff14Ssemarie .vop_remove = vop_generic_badop,
13529aafff14Ssemarie .vop_link = vop_generic_badop,
13539aafff14Ssemarie .vop_rename = vop_generic_badop,
13549aafff14Ssemarie .vop_mkdir = vop_generic_badop,
13559aafff14Ssemarie .vop_rmdir = vop_generic_badop,
13569aafff14Ssemarie .vop_symlink = vop_generic_badop,
13579aafff14Ssemarie .vop_readdir = vop_generic_badop,
13589aafff14Ssemarie .vop_readlink = vop_generic_badop,
13599aafff14Ssemarie .vop_abortop = vop_generic_badop,
1360dc81e71aSthib .vop_bmap = vop_generic_bmap,
13619aafff14Ssemarie .vop_strategy = vop_generic_badop,
1362dc81e71aSthib .vop_pathconf = fifo_pathconf,
1363dc81e71aSthib .vop_advlock = fifo_advlock,
13645ac2d602Sdownsj };
13651816238dStedu
13661816238dStedu int
ext2fsfifo_reclaim(void * v)13671816238dStedu ext2fsfifo_reclaim(void *v)
13681816238dStedu {
13691816238dStedu fifo_reclaim(v);
13701816238dStedu return (ext2fs_reclaim(v));
13711816238dStedu }
13725ac2d602Sdownsj #endif /* FIFO */
1373