1cfe60390STomohiro Kusumi /*-
2cfe60390STomohiro Kusumi * modified for Lites 1.1
3cfe60390STomohiro Kusumi *
4cfe60390STomohiro Kusumi * Aug 1995, Godmar Back (gback@cs.utah.edu)
5cfe60390STomohiro Kusumi * University of Utah, Department of Computer Science
6cfe60390STomohiro Kusumi */
7cfe60390STomohiro Kusumi /*-
8cfe60390STomohiro Kusumi * SPDX-License-Identifier: BSD-3-Clause
9cfe60390STomohiro Kusumi *
10cfe60390STomohiro Kusumi * Copyright (c) 1989, 1993
11cfe60390STomohiro Kusumi * The Regents of the University of California. All rights reserved.
12cfe60390STomohiro Kusumi * (c) UNIX System Laboratories, Inc.
13cfe60390STomohiro Kusumi * All or some portions of this file are derived from material licensed
14cfe60390STomohiro Kusumi * to the University of California by American Telephone and Telegraph
15cfe60390STomohiro Kusumi * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16cfe60390STomohiro Kusumi * the permission of UNIX System Laboratories, Inc.
17cfe60390STomohiro Kusumi *
18cfe60390STomohiro Kusumi * Redistribution and use in source and binary forms, with or without
19cfe60390STomohiro Kusumi * modification, are permitted provided that the following conditions
20cfe60390STomohiro Kusumi * are met:
21cfe60390STomohiro Kusumi * 1. Redistributions of source code must retain the above copyright
22cfe60390STomohiro Kusumi * notice, this list of conditions and the following disclaimer.
23cfe60390STomohiro Kusumi * 2. Redistributions in binary form must reproduce the above copyright
24cfe60390STomohiro Kusumi * notice, this list of conditions and the following disclaimer in the
25cfe60390STomohiro Kusumi * documentation and/or other materials provided with the distribution.
26cfe60390STomohiro Kusumi * 3. Neither the name of the University nor the names of its contributors
27cfe60390STomohiro Kusumi * may be used to endorse or promote products derived from this software
28cfe60390STomohiro Kusumi * without specific prior written permission.
29cfe60390STomohiro Kusumi *
30cfe60390STomohiro Kusumi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31cfe60390STomohiro Kusumi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32cfe60390STomohiro Kusumi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33cfe60390STomohiro Kusumi * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34cfe60390STomohiro Kusumi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35cfe60390STomohiro Kusumi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36cfe60390STomohiro Kusumi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37cfe60390STomohiro Kusumi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38cfe60390STomohiro Kusumi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39cfe60390STomohiro Kusumi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40cfe60390STomohiro Kusumi * SUCH DAMAGE.
41cfe60390STomohiro Kusumi *
42cfe60390STomohiro Kusumi * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94
43cfe60390STomohiro Kusumi * $FreeBSD$
44cfe60390STomohiro Kusumi */
45cfe60390STomohiro Kusumi
46cfe60390STomohiro Kusumi #include <sys/param.h>
47cfe60390STomohiro Kusumi #include <sys/systm.h>
48cfe60390STomohiro Kusumi #include <sys/namei.h>
49cfe60390STomohiro Kusumi #include <sys/bio.h>
50cfe60390STomohiro Kusumi #include <sys/buf.h>
51cfe60390STomohiro Kusumi #include <sys/endian.h>
52cfe60390STomohiro Kusumi #include <sys/mount.h>
53cfe60390STomohiro Kusumi #include <sys/vnode.h>
54cfe60390STomohiro Kusumi #include <sys/malloc.h>
55cfe60390STomohiro Kusumi #include <sys/dirent.h>
56cfe60390STomohiro Kusumi #include <sys/sysctl.h>
57cfe60390STomohiro Kusumi #include <sys/uio.h>
58cfe60390STomohiro Kusumi
59cfe60390STomohiro Kusumi #include <vfs/ufs/dir.h>
60cfe60390STomohiro Kusumi
61cfe60390STomohiro Kusumi #include <vfs/ext2fs/fs.h>
62cfe60390STomohiro Kusumi #include <vfs/ext2fs/inode.h>
63cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_mount.h>
64cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2fs.h>
65cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_dinode.h>
66cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_dir.h>
67cfe60390STomohiro Kusumi #include <vfs/ext2fs/ext2_extern.h>
68cfe60390STomohiro Kusumi #include <vfs/ext2fs/fs.h>
69cfe60390STomohiro Kusumi
70cfe60390STomohiro Kusumi SDT_PROVIDER_DECLARE(ext2fs);
71cfe60390STomohiro Kusumi /*
72cfe60390STomohiro Kusumi * ext2fs trace probe:
73cfe60390STomohiro Kusumi * arg0: verbosity. Higher numbers give more verbose messages
74cfe60390STomohiro Kusumi * arg1: Textual message
75cfe60390STomohiro Kusumi */
76cfe60390STomohiro Kusumi SDT_PROBE_DEFINE2(ext2fs, , lookup, trace, "int", "char*");
77cfe60390STomohiro Kusumi SDT_PROBE_DEFINE4(ext2fs, , trace, ext2_dirbad_error,
78cfe60390STomohiro Kusumi "char*", "ino_t", "doff_t", "char*");
79cfe60390STomohiro Kusumi SDT_PROBE_DEFINE5(ext2fs, , trace, ext2_dirbadentry_error,
80cfe60390STomohiro Kusumi "char*", "int", "uint32_t", "uint16_t", "uint8_t");
81cfe60390STomohiro Kusumi
82cfe60390STomohiro Kusumi static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD, 0, "EXT2FS filesystem");
83cfe60390STomohiro Kusumi
84cfe60390STomohiro Kusumi /*
85cfe60390STomohiro Kusumi DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
86cfe60390STomohiro Kusumi while it is the native blocksize in ext2fs - thus, a #define
87cfe60390STomohiro Kusumi is no longer appropriate
88cfe60390STomohiro Kusumi */
89cfe60390STomohiro Kusumi #undef DIRBLKSIZ
90cfe60390STomohiro Kusumi
91cfe60390STomohiro Kusumi static u_char ext2_ft_to_dt[] = {
92cfe60390STomohiro Kusumi DT_UNKNOWN, /* EXT2_FT_UNKNOWN */
93cfe60390STomohiro Kusumi DT_REG, /* EXT2_FT_REG_FILE */
94cfe60390STomohiro Kusumi DT_DIR, /* EXT2_FT_DIR */
95cfe60390STomohiro Kusumi DT_CHR, /* EXT2_FT_CHRDEV */
96cfe60390STomohiro Kusumi DT_BLK, /* EXT2_FT_BLKDEV */
97cfe60390STomohiro Kusumi DT_FIFO, /* EXT2_FT_FIFO */
98cfe60390STomohiro Kusumi DT_SOCK, /* EXT2_FT_SOCK */
99cfe60390STomohiro Kusumi DT_LNK, /* EXT2_FT_SYMLINK */
100cfe60390STomohiro Kusumi };
101cfe60390STomohiro Kusumi #define FTTODT(ft) \
102cfe60390STomohiro Kusumi ((ft) < nitems(ext2_ft_to_dt) ? ext2_ft_to_dt[(ft)] : DT_UNKNOWN)
103cfe60390STomohiro Kusumi
104cfe60390STomohiro Kusumi static u_char dt_to_ext2_ft[] = {
105cfe60390STomohiro Kusumi EXT2_FT_UNKNOWN, /* DT_UNKNOWN */
106cfe60390STomohiro Kusumi EXT2_FT_FIFO, /* DT_FIFO */
107cfe60390STomohiro Kusumi EXT2_FT_CHRDEV, /* DT_CHR */
108cfe60390STomohiro Kusumi EXT2_FT_UNKNOWN, /* unused */
109cfe60390STomohiro Kusumi EXT2_FT_DIR, /* DT_DIR */
110cfe60390STomohiro Kusumi EXT2_FT_UNKNOWN, /* unused */
111cfe60390STomohiro Kusumi EXT2_FT_BLKDEV, /* DT_BLK */
112cfe60390STomohiro Kusumi EXT2_FT_UNKNOWN, /* unused */
113cfe60390STomohiro Kusumi EXT2_FT_REG_FILE, /* DT_REG */
114cfe60390STomohiro Kusumi EXT2_FT_UNKNOWN, /* unused */
115cfe60390STomohiro Kusumi EXT2_FT_SYMLINK, /* DT_LNK */
116cfe60390STomohiro Kusumi EXT2_FT_UNKNOWN, /* unused */
117cfe60390STomohiro Kusumi EXT2_FT_SOCK, /* DT_SOCK */
118cfe60390STomohiro Kusumi EXT2_FT_UNKNOWN, /* unused */
119cfe60390STomohiro Kusumi EXT2_FT_UNKNOWN, /* DT_WHT */
120cfe60390STomohiro Kusumi };
121cfe60390STomohiro Kusumi #define DTTOFT(dt) \
122cfe60390STomohiro Kusumi ((dt) < nitems(dt_to_ext2_ft) ? dt_to_ext2_ft[(dt)] : EXT2_FT_UNKNOWN)
123cfe60390STomohiro Kusumi
124*2532d84aSTomohiro Kusumi static int ext2_check_direntry(struct vnode *dp,
125*2532d84aSTomohiro Kusumi struct ext2fs_direct_2 *de, int entryoffsetinblock);
126cfe60390STomohiro Kusumi static int ext2_is_dot_entry(struct componentname *cnp);
127cfe60390STomohiro Kusumi static int ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp,
128cfe60390STomohiro Kusumi struct componentname *cnp, ino_t *dd_ino);
129cfe60390STomohiro Kusumi
130cfe60390STomohiro Kusumi static int
ext2_is_dot_entry(struct componentname * cnp)131cfe60390STomohiro Kusumi ext2_is_dot_entry(struct componentname *cnp)
132cfe60390STomohiro Kusumi {
133cfe60390STomohiro Kusumi if (cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
134cfe60390STomohiro Kusumi (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0'))
135cfe60390STomohiro Kusumi return (1);
136cfe60390STomohiro Kusumi return (0);
137cfe60390STomohiro Kusumi }
138cfe60390STomohiro Kusumi
139cfe60390STomohiro Kusumi /*
140cfe60390STomohiro Kusumi * Vnode op for reading directories.
141cfe60390STomohiro Kusumi */
142cfe60390STomohiro Kusumi int
ext2_readdir(struct vop_readdir_args * ap)143cfe60390STomohiro Kusumi ext2_readdir(struct vop_readdir_args *ap)
144cfe60390STomohiro Kusumi {
145cfe60390STomohiro Kusumi struct vnode *vp = ap->a_vp;
146cfe60390STomohiro Kusumi struct uio *uio = ap->a_uio;
147cfe60390STomohiro Kusumi struct buf *bp;
148cfe60390STomohiro Kusumi struct inode *ip;
149cfe60390STomohiro Kusumi struct ext2fs_direct_2 *dp, *edp;
150cfe60390STomohiro Kusumi u_long *cookies;
151cfe60390STomohiro Kusumi struct dirent dstdp;
152cfe60390STomohiro Kusumi off_t offset, startoffset;
153cfe60390STomohiro Kusumi size_t readcnt, skipcnt;
154cfe60390STomohiro Kusumi ssize_t startresid;
155cfe60390STomohiro Kusumi u_int ncookies;
156cfe60390STomohiro Kusumi int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize;
157cfe60390STomohiro Kusumi int error;
158cfe60390STomohiro Kusumi
159cfe60390STomohiro Kusumi if (uio->uio_offset < 0)
160cfe60390STomohiro Kusumi return (EINVAL);
161cfe60390STomohiro Kusumi ip = VTOI(vp);
162cfe60390STomohiro Kusumi if (ap->a_ncookies != NULL) {
163cfe60390STomohiro Kusumi if (uio->uio_resid < 0)
164cfe60390STomohiro Kusumi ncookies = 0;
165cfe60390STomohiro Kusumi else
166cfe60390STomohiro Kusumi ncookies = uio->uio_resid;
167cfe60390STomohiro Kusumi if (uio->uio_offset >= ip->i_size)
168cfe60390STomohiro Kusumi ncookies = 0;
169cfe60390STomohiro Kusumi else if (ip->i_size - uio->uio_offset < ncookies)
170cfe60390STomohiro Kusumi ncookies = ip->i_size - uio->uio_offset;
171cfe60390STomohiro Kusumi ncookies = ncookies / (offsetof(struct ext2fs_direct_2,
172cfe60390STomohiro Kusumi e2d_namlen) + 4) + 1;
173cfe60390STomohiro Kusumi cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
174cfe60390STomohiro Kusumi *ap->a_ncookies = ncookies;
175cfe60390STomohiro Kusumi *ap->a_cookies = cookies;
176cfe60390STomohiro Kusumi } else {
177cfe60390STomohiro Kusumi ncookies = 0;
178cfe60390STomohiro Kusumi cookies = NULL;
179cfe60390STomohiro Kusumi }
180cfe60390STomohiro Kusumi offset = startoffset = uio->uio_offset;
181cfe60390STomohiro Kusumi startresid = uio->uio_resid;
182cfe60390STomohiro Kusumi error = 0;
183cfe60390STomohiro Kusumi while (error == 0 && uio->uio_resid > 0 &&
184cfe60390STomohiro Kusumi uio->uio_offset < ip->i_size) {
185cfe60390STomohiro Kusumi error = ext2_blkatoff(vp, uio->uio_offset, NULL, &bp);
186cfe60390STomohiro Kusumi if (error)
187cfe60390STomohiro Kusumi break;
188f5a1d9f6STomohiro Kusumi if (bp->b_loffset + bp->b_bcount > ip->i_size)
189f5a1d9f6STomohiro Kusumi readcnt = ip->i_size - bp->b_loffset;
190cfe60390STomohiro Kusumi else
191cfe60390STomohiro Kusumi readcnt = bp->b_bcount;
192f5a1d9f6STomohiro Kusumi skipcnt = (size_t)(uio->uio_offset - bp->b_loffset) &
193cfe60390STomohiro Kusumi ~(size_t)(DIRBLKSIZ - 1);
194f5a1d9f6STomohiro Kusumi offset = bp->b_loffset + skipcnt;
195cfe60390STomohiro Kusumi dp = (struct ext2fs_direct_2 *)&bp->b_data[skipcnt];
196cfe60390STomohiro Kusumi edp = (struct ext2fs_direct_2 *)&bp->b_data[readcnt];
197cfe60390STomohiro Kusumi while (error == 0 && uio->uio_resid > 0 && dp < edp) {
198cfe60390STomohiro Kusumi if (le16toh(dp->e2d_reclen) <= offsetof(struct ext2fs_direct_2,
199cfe60390STomohiro Kusumi e2d_namlen) || (caddr_t)dp + le16toh(dp->e2d_reclen) >
200cfe60390STomohiro Kusumi (caddr_t)edp) {
201cfe60390STomohiro Kusumi error = EIO;
202cfe60390STomohiro Kusumi break;
203cfe60390STomohiro Kusumi }
204cfe60390STomohiro Kusumi /*-
205cfe60390STomohiro Kusumi * "New" ext2fs directory entries differ in 3 ways
206cfe60390STomohiro Kusumi * from ufs on-disk ones:
207cfe60390STomohiro Kusumi * - the name is not necessarily NUL-terminated.
208cfe60390STomohiro Kusumi * - the file type field always exists and always
209cfe60390STomohiro Kusumi * follows the name length field.
210cfe60390STomohiro Kusumi * - the file type is encoded in a different way.
211cfe60390STomohiro Kusumi *
212cfe60390STomohiro Kusumi * "Old" ext2fs directory entries need no special
213cfe60390STomohiro Kusumi * conversions, since they are binary compatible
214cfe60390STomohiro Kusumi * with "new" entries having a file type of 0 (i.e.,
215cfe60390STomohiro Kusumi * EXT2_FT_UNKNOWN). Splitting the old name length
216cfe60390STomohiro Kusumi * field didn't make a mess like it did in ufs,
217cfe60390STomohiro Kusumi * because ext2fs uses a machine-independent disk
218cfe60390STomohiro Kusumi * layout.
219cfe60390STomohiro Kusumi */
220cfe60390STomohiro Kusumi dstdp.d_namlen = dp->e2d_namlen;
221cfe60390STomohiro Kusumi dstdp.d_type = FTTODT(dp->e2d_type);
222cfe60390STomohiro Kusumi if (offsetof(struct ext2fs_direct_2, e2d_namlen) +
223cfe60390STomohiro Kusumi dstdp.d_namlen > le16toh(dp->e2d_reclen)) {
224cfe60390STomohiro Kusumi error = EIO;
225cfe60390STomohiro Kusumi break;
226cfe60390STomohiro Kusumi }
227cfe60390STomohiro Kusumi if (offset < startoffset || le32toh(dp->e2d_ino) == 0)
228cfe60390STomohiro Kusumi goto nextentry;
229cfe60390STomohiro Kusumi dstdp.d_ino = le32toh(dp->e2d_ino);
230cfe60390STomohiro Kusumi bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen);
231cfe60390STomohiro Kusumi if (vop_write_dirent(&error, uio, dstdp.d_ino,
232cfe60390STomohiro Kusumi dstdp.d_type, dstdp.d_namlen, dstdp.d_name)) {
233cfe60390STomohiro Kusumi if (uio->uio_resid == startresid)
234cfe60390STomohiro Kusumi error = EINVAL;
235cfe60390STomohiro Kusumi else
236cfe60390STomohiro Kusumi error = EJUSTRETURN;
237cfe60390STomohiro Kusumi break;
238cfe60390STomohiro Kusumi }
239cfe60390STomohiro Kusumi if (error)
240cfe60390STomohiro Kusumi break;
241cfe60390STomohiro Kusumi if (cookies != NULL) {
242cfe60390STomohiro Kusumi KASSERT(ncookies > 0,
243cfe60390STomohiro Kusumi ("ext2_readdir: cookies buffer too small"));
244cfe60390STomohiro Kusumi *cookies = offset + le16toh(dp->e2d_reclen);
245cfe60390STomohiro Kusumi cookies++;
246cfe60390STomohiro Kusumi ncookies--;
247cfe60390STomohiro Kusumi }
248cfe60390STomohiro Kusumi nextentry:
249cfe60390STomohiro Kusumi offset += le16toh(dp->e2d_reclen);
250cfe60390STomohiro Kusumi dp = (struct ext2fs_direct_2 *)((caddr_t)dp +
251cfe60390STomohiro Kusumi le16toh(dp->e2d_reclen));
252cfe60390STomohiro Kusumi }
253e5b38eb5STomohiro Kusumi bqrelse(bp);
254cfe60390STomohiro Kusumi uio->uio_offset = offset;
255cfe60390STomohiro Kusumi }
256cfe60390STomohiro Kusumi /* We need to correct uio_offset. */
257cfe60390STomohiro Kusumi uio->uio_offset = offset;
258cfe60390STomohiro Kusumi if (error == EJUSTRETURN)
259cfe60390STomohiro Kusumi error = 0;
260cfe60390STomohiro Kusumi if (ap->a_ncookies != NULL) {
261cfe60390STomohiro Kusumi if (error == 0) {
262cfe60390STomohiro Kusumi ap->a_ncookies -= ncookies;
263cfe60390STomohiro Kusumi } else {
264cfe60390STomohiro Kusumi free(*ap->a_cookies, M_TEMP);
265cfe60390STomohiro Kusumi *ap->a_ncookies = 0;
266cfe60390STomohiro Kusumi *ap->a_cookies = NULL;
267cfe60390STomohiro Kusumi }
268cfe60390STomohiro Kusumi }
269cfe60390STomohiro Kusumi if (error == 0 && ap->a_eofflag)
270cfe60390STomohiro Kusumi *ap->a_eofflag = ip->i_size <= uio->uio_offset;
271cfe60390STomohiro Kusumi return (error);
272cfe60390STomohiro Kusumi }
273cfe60390STomohiro Kusumi
274cfe60390STomohiro Kusumi /*
275cfe60390STomohiro Kusumi * Convert a component of a pathname into a pointer to a locked inode.
276cfe60390STomohiro Kusumi * This is a very central and rather complicated routine.
277cfe60390STomohiro Kusumi * If the file system is not maintained in a strict tree hierarchy,
278cfe60390STomohiro Kusumi * this can result in a deadlock situation (see comments in code below).
279cfe60390STomohiro Kusumi *
280cfe60390STomohiro Kusumi * The cnp->cn_nameiop argument is NAMEI_LOOKUP, NAMEI_CREATE, NAMEI_RENAME, or NAMEI_DELETE depending
281cfe60390STomohiro Kusumi * on whether the name is to be looked up, created, renamed, or deleted.
282cfe60390STomohiro Kusumi * When NAMEI_CREATE, NAMEI_RENAME, or NAMEI_DELETE is specified, information usable in
283cfe60390STomohiro Kusumi * creating, renaming, or deleting a directory entry may be calculated.
284cfe60390STomohiro Kusumi * If flag has LOCKPARENT or'ed into it and the target of the pathname
285cfe60390STomohiro Kusumi * exists, lookup returns both the target and its parent directory locked.
286cfe60390STomohiro Kusumi * When creating or renaming and LOCKPARENT is specified, the target may
287cfe60390STomohiro Kusumi * not be ".". When deleting and LOCKPARENT is specified, the target may
288cfe60390STomohiro Kusumi * be "."., but the caller must check to ensure it does an vrele and vput
289cfe60390STomohiro Kusumi * instead of two vputs.
290cfe60390STomohiro Kusumi *
291cfe60390STomohiro Kusumi * Overall outline of ext2_lookup:
292cfe60390STomohiro Kusumi *
293cfe60390STomohiro Kusumi * search for name in directory, to found or notfound
294cfe60390STomohiro Kusumi * notfound:
295cfe60390STomohiro Kusumi * if creating, return locked directory, leaving info on available slots
296cfe60390STomohiro Kusumi * else return error
297cfe60390STomohiro Kusumi * found:
298cfe60390STomohiro Kusumi * if at end of path and deleting, return information to allow delete
299cfe60390STomohiro Kusumi * if at end of path and rewriting (NAMEI_RENAME and LOCKPARENT), lock target
300cfe60390STomohiro Kusumi * inode and return info to allow rewrite
301cfe60390STomohiro Kusumi * if not at end, add name to cache; if at end and neither creating
302cfe60390STomohiro Kusumi * nor deleting, add name to cache
303cfe60390STomohiro Kusumi */
304cfe60390STomohiro Kusumi int
ext2_lookup(struct vop_old_lookup_args * ap)305cfe60390STomohiro Kusumi ext2_lookup(struct vop_old_lookup_args *ap)
306cfe60390STomohiro Kusumi {
307cfe60390STomohiro Kusumi
308cfe60390STomohiro Kusumi return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
309cfe60390STomohiro Kusumi }
310cfe60390STomohiro Kusumi
311cfe60390STomohiro Kusumi static int
ext2_lookup_ino(struct vnode * vdp,struct vnode ** vpp,struct componentname * cnp,ino_t * dd_ino)312cfe60390STomohiro Kusumi ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
313cfe60390STomohiro Kusumi ino_t *dd_ino)
314cfe60390STomohiro Kusumi {
315cfe60390STomohiro Kusumi struct inode *dp; /* inode for directory being searched */
316cfe60390STomohiro Kusumi struct buf *bp; /* a buffer of directory entries */
317cfe60390STomohiro Kusumi struct ext2fs_direct_2 *ep; /* the current directory entry */
318cfe60390STomohiro Kusumi int entryoffsetinblock; /* offset of ep in bp's buffer */
319cfe60390STomohiro Kusumi struct ext2fs_searchslot ss;
320cfe60390STomohiro Kusumi doff_t i_diroff; /* cached i_diroff value */
321cfe60390STomohiro Kusumi doff_t i_offset; /* cached i_offset value */
322cfe60390STomohiro Kusumi int numdirpasses; /* strategy for directory search */
323cfe60390STomohiro Kusumi doff_t endsearch; /* offset to end directory search */
324cfe60390STomohiro Kusumi doff_t prevoff; /* prev entry dp->i_offset */
325cfe60390STomohiro Kusumi struct vnode *pdp; /* saved dp during symlink work */
326cfe60390STomohiro Kusumi struct vnode *tdp; /* returned by VFS_VGET */
327cfe60390STomohiro Kusumi doff_t enduseful; /* pointer past last used dir slot */
328cfe60390STomohiro Kusumi u_long bmask; /* block offset mask */
329cfe60390STomohiro Kusumi int error;
330cfe60390STomohiro Kusumi struct ucred *cred = cnp->cn_cred;
331cfe60390STomohiro Kusumi int flags = cnp->cn_flags;
332cfe60390STomohiro Kusumi int nameiop = cnp->cn_nameiop;
333cfe60390STomohiro Kusumi ino_t ino;
334cfe60390STomohiro Kusumi int entry_found = 0;
335cfe60390STomohiro Kusumi int lockparent; /* 1 => lockparent flag is set */
336cfe60390STomohiro Kusumi int wantparent; /* 1 => wantparent or lockparent flag */
337cfe60390STomohiro Kusumi
338cfe60390STomohiro Kusumi int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize;
339cfe60390STomohiro Kusumi tdp = NULL;
340cfe60390STomohiro Kusumi
341cfe60390STomohiro Kusumi if (vpp != NULL)
342cfe60390STomohiro Kusumi *vpp = NULL;
343cfe60390STomohiro Kusumi
344cfe60390STomohiro Kusumi dp = VTOI(vdp);
345cfe60390STomohiro Kusumi bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
346cfe60390STomohiro Kusumi lockparent = flags & CNP_LOCKPARENT;
347cfe60390STomohiro Kusumi wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
348cfe60390STomohiro Kusumi
349cfe60390STomohiro Kusumi bp = NULL;
350cfe60390STomohiro Kusumi ss.slotoffset = -1;
351cfe60390STomohiro Kusumi
352cfe60390STomohiro Kusumi /*
353cfe60390STomohiro Kusumi * We now have a segment name to search for, and a directory to search.
354cfe60390STomohiro Kusumi *
355cfe60390STomohiro Kusumi * Suppress search for slots unless creating
356cfe60390STomohiro Kusumi * file and at end of pathname, in which case
357cfe60390STomohiro Kusumi * we watch for a place to put the new file in
358cfe60390STomohiro Kusumi * case it doesn't already exist.
359cfe60390STomohiro Kusumi */
360cfe60390STomohiro Kusumi i_diroff = dp->i_diroff;
361cfe60390STomohiro Kusumi ss.slotstatus = FOUND;
362cfe60390STomohiro Kusumi ss.slotfreespace = ss.slotsize = ss.slotneeded = 0;
363cfe60390STomohiro Kusumi if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME)) {
364cfe60390STomohiro Kusumi ss.slotstatus = NONE;
365cfe60390STomohiro Kusumi ss.slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
366cfe60390STomohiro Kusumi /*
367cfe60390STomohiro Kusumi * was ss.slotneeded = (sizeof(struct direct) - MAXNAMLEN +
368cfe60390STomohiro Kusumi * cnp->cn_namelen + 3) &~ 3;
369cfe60390STomohiro Kusumi */
370cfe60390STomohiro Kusumi }
371cfe60390STomohiro Kusumi /*
372cfe60390STomohiro Kusumi * Try to lookup dir entry using htree directory index.
373cfe60390STomohiro Kusumi *
374cfe60390STomohiro Kusumi * If we got an error or we want to find '.' or '..' entry,
375cfe60390STomohiro Kusumi * we will fall back to linear search.
376cfe60390STomohiro Kusumi */
377cfe60390STomohiro Kusumi if (!ext2_is_dot_entry(cnp) && ext2_htree_has_idx(dp)) {
378cfe60390STomohiro Kusumi numdirpasses = 1;
379cfe60390STomohiro Kusumi entryoffsetinblock = 0;
380cfe60390STomohiro Kusumi switch (ext2_htree_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
381cfe60390STomohiro Kusumi &bp, &entryoffsetinblock, &i_offset, &prevoff,
382cfe60390STomohiro Kusumi &enduseful, &ss)) {
383cfe60390STomohiro Kusumi case 0:
384cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
385cfe60390STomohiro Kusumi (i_offset & bmask));
386cfe60390STomohiro Kusumi goto foundentry;
387cfe60390STomohiro Kusumi case ENOENT:
388cfe60390STomohiro Kusumi i_offset = roundup2(dp->i_size, DIRBLKSIZ);
389cfe60390STomohiro Kusumi goto notfound;
390cfe60390STomohiro Kusumi default:
391cfe60390STomohiro Kusumi /*
392cfe60390STomohiro Kusumi * Something failed; just fallback to do a linear
393cfe60390STomohiro Kusumi * search.
394cfe60390STomohiro Kusumi */
395cfe60390STomohiro Kusumi break;
396cfe60390STomohiro Kusumi }
397cfe60390STomohiro Kusumi }
398cfe60390STomohiro Kusumi
399cfe60390STomohiro Kusumi /*
400cfe60390STomohiro Kusumi * If there is cached information on a previous search of
401cfe60390STomohiro Kusumi * this directory, pick up where we last left off.
402cfe60390STomohiro Kusumi * We cache only lookups as these are the most common
403cfe60390STomohiro Kusumi * and have the greatest payoff. Caching NAMEI_CREATE has little
404cfe60390STomohiro Kusumi * benefit as it usually must search the entire directory
405cfe60390STomohiro Kusumi * to determine that the entry does not exist. Caching the
406cfe60390STomohiro Kusumi * location of the last NAMEI_DELETE or NAMEI_RENAME has not reduced
407cfe60390STomohiro Kusumi * profiling time and hence has been removed in the interest
408cfe60390STomohiro Kusumi * of simplicity.
409cfe60390STomohiro Kusumi */
410cfe60390STomohiro Kusumi if (nameiop != NAMEI_LOOKUP || i_diroff == 0 ||
411cfe60390STomohiro Kusumi i_diroff > dp->i_size) {
412cfe60390STomohiro Kusumi entryoffsetinblock = 0;
413cfe60390STomohiro Kusumi i_offset = 0;
414cfe60390STomohiro Kusumi numdirpasses = 1;
415cfe60390STomohiro Kusumi } else {
416cfe60390STomohiro Kusumi i_offset = i_diroff;
417cfe60390STomohiro Kusumi if ((entryoffsetinblock = i_offset & bmask) &&
418cfe60390STomohiro Kusumi (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL,
419cfe60390STomohiro Kusumi &bp)))
420cfe60390STomohiro Kusumi return (error);
421cfe60390STomohiro Kusumi numdirpasses = 2;
422cfe60390STomohiro Kusumi }
423cfe60390STomohiro Kusumi prevoff = i_offset;
424cfe60390STomohiro Kusumi endsearch = roundup2(dp->i_size, DIRBLKSIZ);
425cfe60390STomohiro Kusumi enduseful = 0;
426cfe60390STomohiro Kusumi
427cfe60390STomohiro Kusumi searchloop:
428cfe60390STomohiro Kusumi while (i_offset < endsearch) {
429cfe60390STomohiro Kusumi /*
430cfe60390STomohiro Kusumi * If necessary, get the next directory block.
431cfe60390STomohiro Kusumi */
432cfe60390STomohiro Kusumi if (bp != NULL)
433e5b38eb5STomohiro Kusumi brelse(bp);
434cfe60390STomohiro Kusumi error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, &bp);
435cfe60390STomohiro Kusumi if (error != 0)
436cfe60390STomohiro Kusumi return (error);
437cfe60390STomohiro Kusumi
438cfe60390STomohiro Kusumi entryoffsetinblock = 0;
439cfe60390STomohiro Kusumi if (ss.slotstatus == NONE) {
440cfe60390STomohiro Kusumi ss.slotoffset = -1;
441cfe60390STomohiro Kusumi ss.slotfreespace = 0;
442cfe60390STomohiro Kusumi }
443cfe60390STomohiro Kusumi
444cfe60390STomohiro Kusumi error = ext2_search_dirblock(dp, bp->b_data, &entry_found,
445cfe60390STomohiro Kusumi cnp->cn_nameptr, cnp->cn_namelen,
446cfe60390STomohiro Kusumi &entryoffsetinblock, &i_offset, &prevoff,
447cfe60390STomohiro Kusumi &enduseful, &ss);
448cfe60390STomohiro Kusumi if (error != 0) {
449e5b38eb5STomohiro Kusumi brelse(bp);
450cfe60390STomohiro Kusumi return (error);
451cfe60390STomohiro Kusumi }
452cfe60390STomohiro Kusumi if (entry_found) {
453cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
454cfe60390STomohiro Kusumi (entryoffsetinblock & bmask));
455cfe60390STomohiro Kusumi foundentry:
456cfe60390STomohiro Kusumi ino = le32toh(ep->e2d_ino);
457cfe60390STomohiro Kusumi goto found;
458cfe60390STomohiro Kusumi }
459cfe60390STomohiro Kusumi }
460cfe60390STomohiro Kusumi notfound:
461cfe60390STomohiro Kusumi /*
462cfe60390STomohiro Kusumi * If we started in the middle of the directory and failed
463cfe60390STomohiro Kusumi * to find our target, we must check the beginning as well.
464cfe60390STomohiro Kusumi */
465cfe60390STomohiro Kusumi if (numdirpasses == 2) {
466cfe60390STomohiro Kusumi numdirpasses--;
467cfe60390STomohiro Kusumi i_offset = 0;
468cfe60390STomohiro Kusumi endsearch = i_diroff;
469cfe60390STomohiro Kusumi goto searchloop;
470cfe60390STomohiro Kusumi }
471cfe60390STomohiro Kusumi if (bp != NULL)
472e5b38eb5STomohiro Kusumi brelse(bp);
473cfe60390STomohiro Kusumi /*
474cfe60390STomohiro Kusumi * If creating, and at end of pathname and current
475cfe60390STomohiro Kusumi * directory has not been removed, then can consider
476cfe60390STomohiro Kusumi * allowing file to be created.
477cfe60390STomohiro Kusumi */
478cfe60390STomohiro Kusumi if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) &&
479cfe60390STomohiro Kusumi dp->i_nlink != 0) {
480cfe60390STomohiro Kusumi /*
481cfe60390STomohiro Kusumi * Access for write is interpreted as allowing
482cfe60390STomohiro Kusumi * creation of files in the directory.
483cfe60390STomohiro Kusumi */
484cfe60390STomohiro Kusumi if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0)
485cfe60390STomohiro Kusumi return (error);
486cfe60390STomohiro Kusumi /*
487cfe60390STomohiro Kusumi * Return an indication of where the new directory
488cfe60390STomohiro Kusumi * entry should be put. If we didn't find a slot,
489cfe60390STomohiro Kusumi * then set dp->i_count to 0 indicating
490cfe60390STomohiro Kusumi * that the new slot belongs at the end of the
491cfe60390STomohiro Kusumi * directory. If we found a slot, then the new entry
492cfe60390STomohiro Kusumi * can be put in the range from dp->i_offset to
493cfe60390STomohiro Kusumi * dp->i_offset + dp->i_count.
494cfe60390STomohiro Kusumi */
495cfe60390STomohiro Kusumi if (ss.slotstatus == NONE) {
496cfe60390STomohiro Kusumi dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
497cfe60390STomohiro Kusumi dp->i_count = 0;
498cfe60390STomohiro Kusumi enduseful = dp->i_offset;
499cfe60390STomohiro Kusumi } else {
500cfe60390STomohiro Kusumi dp->i_offset = ss.slotoffset;
501cfe60390STomohiro Kusumi dp->i_count = ss.slotsize;
502cfe60390STomohiro Kusumi if (enduseful < ss.slotoffset + ss.slotsize)
503cfe60390STomohiro Kusumi enduseful = ss.slotoffset + ss.slotsize;
504cfe60390STomohiro Kusumi }
505cfe60390STomohiro Kusumi dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
506cfe60390STomohiro Kusumi /*
507cfe60390STomohiro Kusumi * We return with the directory locked, so that
508cfe60390STomohiro Kusumi * the parameters we set up above will still be
509cfe60390STomohiro Kusumi * valid if we actually decide to do a direnter().
510cfe60390STomohiro Kusumi * We return ni_vp == NULL to indicate that the entry
511cfe60390STomohiro Kusumi * does not currently exist; we leave a pointer to
512cfe60390STomohiro Kusumi * the (locked) directory inode in ndp->ni_dvp.
513cfe60390STomohiro Kusumi * The pathname buffer is saved so that the name
514cfe60390STomohiro Kusumi * can be obtained later.
515cfe60390STomohiro Kusumi *
516cfe60390STomohiro Kusumi * NB - if the directory is unlocked, then this
517cfe60390STomohiro Kusumi * information cannot be used.
518cfe60390STomohiro Kusumi */
519cfe60390STomohiro Kusumi if (!lockparent)
520cfe60390STomohiro Kusumi vn_unlock(vdp);
521cfe60390STomohiro Kusumi return (EJUSTRETURN);
522cfe60390STomohiro Kusumi }
523cfe60390STomohiro Kusumi return (ENOENT);
524cfe60390STomohiro Kusumi
525cfe60390STomohiro Kusumi found:
526cfe60390STomohiro Kusumi if (dd_ino != NULL)
527cfe60390STomohiro Kusumi *dd_ino = ino;
528cfe60390STomohiro Kusumi /*
529cfe60390STomohiro Kusumi * Check that directory length properly reflects presence
530cfe60390STomohiro Kusumi * of this entry.
531cfe60390STomohiro Kusumi */
532cfe60390STomohiro Kusumi if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen) >
533cfe60390STomohiro Kusumi dp->i_size) {
534cfe60390STomohiro Kusumi ext2_dirbad(dp, i_offset, "i_size too small");
535cfe60390STomohiro Kusumi dp->i_size = entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen);
536cfe60390STomohiro Kusumi dp->i_flag |= IN_CHANGE | IN_UPDATE;
537cfe60390STomohiro Kusumi }
538e5b38eb5STomohiro Kusumi brelse(bp);
539cfe60390STomohiro Kusumi
540cfe60390STomohiro Kusumi /*
541cfe60390STomohiro Kusumi * Found component in pathname.
542cfe60390STomohiro Kusumi * If the final component of path name, save information
543cfe60390STomohiro Kusumi * in the cache as to where the entry was found.
544cfe60390STomohiro Kusumi */
545cfe60390STomohiro Kusumi if (nameiop == NAMEI_LOOKUP)
546cfe60390STomohiro Kusumi dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ);
547cfe60390STomohiro Kusumi /*
548cfe60390STomohiro Kusumi * If deleting, and at end of pathname, return
549cfe60390STomohiro Kusumi * parameters which can be used to remove file.
550cfe60390STomohiro Kusumi */
551cfe60390STomohiro Kusumi if (nameiop == NAMEI_DELETE) {
552cfe60390STomohiro Kusumi if (lockparent)
553cfe60390STomohiro Kusumi ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
554cfe60390STomohiro Kusumi /*
555cfe60390STomohiro Kusumi * Write access to directory required to delete files.
556cfe60390STomohiro Kusumi */
557cfe60390STomohiro Kusumi if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0)
558cfe60390STomohiro Kusumi return (error);
559cfe60390STomohiro Kusumi /*
560cfe60390STomohiro Kusumi * Return pointer to current entry in dp->i_offset,
561cfe60390STomohiro Kusumi * and distance past previous entry (if there
562cfe60390STomohiro Kusumi * is a previous entry in this block) in dp->i_count.
563cfe60390STomohiro Kusumi * Save directory inode pointer in ndp->ni_dvp for dirremove().
564cfe60390STomohiro Kusumi *
565cfe60390STomohiro Kusumi * Technically we shouldn't be setting these in the
566cfe60390STomohiro Kusumi * WANTPARENT case (first lookup in rename()), but any
567cfe60390STomohiro Kusumi * lookups that will result in directory changes will
568cfe60390STomohiro Kusumi * overwrite these.
569cfe60390STomohiro Kusumi */
570cfe60390STomohiro Kusumi dp->i_offset = i_offset;
571cfe60390STomohiro Kusumi if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
572cfe60390STomohiro Kusumi dp->i_count = 0;
573cfe60390STomohiro Kusumi else
574cfe60390STomohiro Kusumi dp->i_count = dp->i_offset - prevoff;
575cfe60390STomohiro Kusumi if (dd_ino != NULL)
576cfe60390STomohiro Kusumi return (0);
577cfe60390STomohiro Kusumi if (dp->i_number == ino) {
578cfe60390STomohiro Kusumi vref(vdp);
579cfe60390STomohiro Kusumi *vpp = vdp;
580cfe60390STomohiro Kusumi return (0);
581cfe60390STomohiro Kusumi }
582cfe60390STomohiro Kusumi if ((error = VFS_VGET(vdp->v_mount, NULL, ino, &tdp)) != 0)
583cfe60390STomohiro Kusumi return (error);
584cfe60390STomohiro Kusumi /*
585cfe60390STomohiro Kusumi * If directory is "sticky", then user must own
586cfe60390STomohiro Kusumi * the directory, or the file in it, else she
587cfe60390STomohiro Kusumi * may not delete it (unless she's root). This
588cfe60390STomohiro Kusumi * implements append-only directories.
589cfe60390STomohiro Kusumi */
590cfe60390STomohiro Kusumi if ((dp->i_mode & ISVTX) &&
591cfe60390STomohiro Kusumi cred->cr_uid != 0 &&
592cfe60390STomohiro Kusumi cred->cr_uid != dp->i_uid &&
593cfe60390STomohiro Kusumi VTOI(tdp)->i_uid != cred->cr_uid) {
594cfe60390STomohiro Kusumi vput(tdp);
595cfe60390STomohiro Kusumi return (EPERM);
596cfe60390STomohiro Kusumi }
597cfe60390STomohiro Kusumi *vpp = tdp;
598cfe60390STomohiro Kusumi if (!lockparent)
599cfe60390STomohiro Kusumi vn_unlock(vdp);
600cfe60390STomohiro Kusumi return (0);
601cfe60390STomohiro Kusumi }
602cfe60390STomohiro Kusumi
603cfe60390STomohiro Kusumi /*
604cfe60390STomohiro Kusumi * If rewriting (NAMEI_RENAME), return the inode and the
605cfe60390STomohiro Kusumi * information required to rewrite the present directory
606cfe60390STomohiro Kusumi * Must get inode of directory entry to verify it's a
607cfe60390STomohiro Kusumi * regular file, or empty directory.
608cfe60390STomohiro Kusumi */
609cfe60390STomohiro Kusumi if (nameiop == NAMEI_RENAME && wantparent) {
610cfe60390STomohiro Kusumi if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0)
611cfe60390STomohiro Kusumi return (error);
612cfe60390STomohiro Kusumi /*
613cfe60390STomohiro Kusumi * Careful about locking second inode.
614cfe60390STomohiro Kusumi * This can only occur if the target is ".".
615cfe60390STomohiro Kusumi */
616cfe60390STomohiro Kusumi dp->i_offset = i_offset;
617cfe60390STomohiro Kusumi if (dp->i_number == ino)
618cfe60390STomohiro Kusumi return (EISDIR);
619cfe60390STomohiro Kusumi if (dd_ino != NULL)
620cfe60390STomohiro Kusumi return (0);
621cfe60390STomohiro Kusumi if ((error = VFS_VGET(vdp->v_mount, NULL, ino, &tdp)) != 0)
622cfe60390STomohiro Kusumi return (error);
623cfe60390STomohiro Kusumi *vpp = tdp;
624cfe60390STomohiro Kusumi if (!lockparent)
625cfe60390STomohiro Kusumi vn_unlock(vdp);
626cfe60390STomohiro Kusumi return (0);
627cfe60390STomohiro Kusumi }
628cfe60390STomohiro Kusumi if (dd_ino != NULL)
629cfe60390STomohiro Kusumi return (0);
630cfe60390STomohiro Kusumi
631cfe60390STomohiro Kusumi /*
632cfe60390STomohiro Kusumi * Step through the translation in the name. We do not `vput' the
633cfe60390STomohiro Kusumi * directory because we may need it again if a symbolic link
634cfe60390STomohiro Kusumi * is relative to the current directory. Instead we save it
635cfe60390STomohiro Kusumi * unlocked as "pdp". We must get the target inode before unlocking
636cfe60390STomohiro Kusumi * the directory to insure that the inode will not be removed
637cfe60390STomohiro Kusumi * before we get it. We prevent deadlock by always fetching
638cfe60390STomohiro Kusumi * inodes from the root, moving down the directory tree. Thus
639cfe60390STomohiro Kusumi * when following backward pointers ".." we must unlock the
640cfe60390STomohiro Kusumi * parent directory before getting the requested directory.
641cfe60390STomohiro Kusumi * There is a potential race condition here if both the current
642cfe60390STomohiro Kusumi * and parent directories are removed before the VFS_VGET for the
643cfe60390STomohiro Kusumi * inode associated with ".." returns. We hope that this occurs
644cfe60390STomohiro Kusumi * infrequently since we cannot avoid this race condition without
645cfe60390STomohiro Kusumi * implementing a sophisticated deadlock detection algorithm.
646cfe60390STomohiro Kusumi * Note also that this simple deadlock detection scheme will not
647cfe60390STomohiro Kusumi * work if the file system has any hard links other than ".."
648cfe60390STomohiro Kusumi * that point backwards in the directory structure.
649cfe60390STomohiro Kusumi */
650cfe60390STomohiro Kusumi pdp = vdp;
651cfe60390STomohiro Kusumi if (flags & CNP_ISDOTDOT) {
652cfe60390STomohiro Kusumi vn_unlock(pdp); /* race to get the inode */
653cfe60390STomohiro Kusumi error = VFS_VGET(vdp->v_mount, NULL, ino, &tdp);
654cfe60390STomohiro Kusumi if (error) {
655cfe60390STomohiro Kusumi vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
656cfe60390STomohiro Kusumi return (error);
657cfe60390STomohiro Kusumi }
658cfe60390STomohiro Kusumi if (lockparent) {
659cfe60390STomohiro Kusumi error = vn_lock(pdp, LK_EXCLUSIVE | LK_FAILRECLAIM);
660cfe60390STomohiro Kusumi if (error) {
661cfe60390STomohiro Kusumi vput(tdp);
662cfe60390STomohiro Kusumi return (error);
663cfe60390STomohiro Kusumi }
664cfe60390STomohiro Kusumi }
665cfe60390STomohiro Kusumi *vpp = tdp;
666cfe60390STomohiro Kusumi } else if (dp->i_number == ino) {
667cfe60390STomohiro Kusumi vref(vdp); /* we want ourself, ie "." */
668cfe60390STomohiro Kusumi *vpp = vdp;
669cfe60390STomohiro Kusumi } else {
670cfe60390STomohiro Kusumi if ((error = VFS_VGET(vdp->v_mount, NULL, ino, &tdp)) != 0)
671cfe60390STomohiro Kusumi return (error);
672cfe60390STomohiro Kusumi if (!lockparent) {
673cfe60390STomohiro Kusumi vn_unlock(pdp);
674cfe60390STomohiro Kusumi cnp->cn_flags |= CNP_PDIRUNLOCK;
675cfe60390STomohiro Kusumi }
676cfe60390STomohiro Kusumi *vpp = tdp;
677cfe60390STomohiro Kusumi }
678cfe60390STomohiro Kusumi return (0);
679cfe60390STomohiro Kusumi }
680cfe60390STomohiro Kusumi
681cfe60390STomohiro Kusumi int
ext2_search_dirblock(struct inode * ip,void * data,int * foundp,const char * name,int namelen,int * entryoffsetinblockp,doff_t * offp,doff_t * prevoffp,doff_t * endusefulp,struct ext2fs_searchslot * ssp)682cfe60390STomohiro Kusumi ext2_search_dirblock(struct inode *ip, void *data, int *foundp,
683cfe60390STomohiro Kusumi const char *name, int namelen, int *entryoffsetinblockp,
684cfe60390STomohiro Kusumi doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
685cfe60390STomohiro Kusumi struct ext2fs_searchslot *ssp)
686cfe60390STomohiro Kusumi {
687cfe60390STomohiro Kusumi struct vnode *vdp;
688cfe60390STomohiro Kusumi struct ext2fs_direct_2 *ep, *top;
689cfe60390STomohiro Kusumi uint32_t bsize = ip->i_e2fs->e2fs_bsize;
690cfe60390STomohiro Kusumi int offset = *entryoffsetinblockp;
691cfe60390STomohiro Kusumi int namlen;
692cfe60390STomohiro Kusumi
693cfe60390STomohiro Kusumi vdp = ITOV(ip);
694cfe60390STomohiro Kusumi
695cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)((char *)data + offset);
696cfe60390STomohiro Kusumi top = (struct ext2fs_direct_2 *)((char *)data + bsize);
697cfe60390STomohiro Kusumi while (ep < top) {
698*2532d84aSTomohiro Kusumi if (ext2_check_direntry(vdp, ep, offset)) {
699cfe60390STomohiro Kusumi int i;
700cfe60390STomohiro Kusumi
701cfe60390STomohiro Kusumi ext2_dirbad(ip, *offp, "mangled entry");
702cfe60390STomohiro Kusumi i = bsize - (offset & (bsize - 1));
703cfe60390STomohiro Kusumi *offp += i;
704cfe60390STomohiro Kusumi offset += i;
705*2532d84aSTomohiro Kusumi ep = (struct ext2fs_direct_2 *)((char *)data + offset);
706cfe60390STomohiro Kusumi continue;
707cfe60390STomohiro Kusumi }
708cfe60390STomohiro Kusumi
709cfe60390STomohiro Kusumi /*
710cfe60390STomohiro Kusumi * If an appropriate sized slot has not yet been found,
711cfe60390STomohiro Kusumi * check to see if one is available. Also accumulate space
712cfe60390STomohiro Kusumi * in the current block so that we can determine if
713cfe60390STomohiro Kusumi * compaction is viable.
714cfe60390STomohiro Kusumi */
715cfe60390STomohiro Kusumi if (ssp->slotstatus != FOUND) {
716cfe60390STomohiro Kusumi int size = le16toh(ep->e2d_reclen);
717cfe60390STomohiro Kusumi
718cfe60390STomohiro Kusumi if (ep->e2d_ino != 0)
719cfe60390STomohiro Kusumi size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
720cfe60390STomohiro Kusumi else if (ext2_is_dirent_tail(ip, ep))
721cfe60390STomohiro Kusumi size -= sizeof(struct ext2fs_direct_tail);
722cfe60390STomohiro Kusumi if (size > 0) {
723cfe60390STomohiro Kusumi if (size >= ssp->slotneeded) {
724cfe60390STomohiro Kusumi ssp->slotstatus = FOUND;
725cfe60390STomohiro Kusumi ssp->slotoffset = *offp;
726cfe60390STomohiro Kusumi ssp->slotsize = le16toh(ep->e2d_reclen);
727cfe60390STomohiro Kusumi } else if (ssp->slotstatus == NONE) {
728cfe60390STomohiro Kusumi ssp->slotfreespace += size;
729cfe60390STomohiro Kusumi if (ssp->slotoffset == -1)
730cfe60390STomohiro Kusumi ssp->slotoffset = *offp;
731cfe60390STomohiro Kusumi if (ssp->slotfreespace >= ssp->slotneeded) {
732cfe60390STomohiro Kusumi ssp->slotstatus = COMPACT;
733cfe60390STomohiro Kusumi ssp->slotsize = *offp +
734cfe60390STomohiro Kusumi le16toh(ep->e2d_reclen) -
735cfe60390STomohiro Kusumi ssp->slotoffset;
736cfe60390STomohiro Kusumi }
737cfe60390STomohiro Kusumi }
738cfe60390STomohiro Kusumi }
739cfe60390STomohiro Kusumi }
740cfe60390STomohiro Kusumi /*
741cfe60390STomohiro Kusumi * Check for a name match.
742cfe60390STomohiro Kusumi */
743cfe60390STomohiro Kusumi if (ep->e2d_ino != 0) {
744cfe60390STomohiro Kusumi namlen = ep->e2d_namlen;
745cfe60390STomohiro Kusumi if (namlen == namelen &&
746cfe60390STomohiro Kusumi !bcmp(name, ep->e2d_name, (unsigned)namlen)) {
747cfe60390STomohiro Kusumi /*
748cfe60390STomohiro Kusumi * Save directory entry's inode number and
749cfe60390STomohiro Kusumi * reclen in ndp->ni_ufs area, and release
750cfe60390STomohiro Kusumi * directory buffer.
751cfe60390STomohiro Kusumi */
752cfe60390STomohiro Kusumi *foundp = 1;
753cfe60390STomohiro Kusumi return (0);
754cfe60390STomohiro Kusumi }
755cfe60390STomohiro Kusumi }
756cfe60390STomohiro Kusumi *prevoffp = *offp;
757cfe60390STomohiro Kusumi *offp += le16toh(ep->e2d_reclen);
758cfe60390STomohiro Kusumi offset += le16toh(ep->e2d_reclen);
759cfe60390STomohiro Kusumi *entryoffsetinblockp = offset;
760cfe60390STomohiro Kusumi if (ep->e2d_ino != 0)
761cfe60390STomohiro Kusumi *endusefulp = *offp;
762cfe60390STomohiro Kusumi /*
763cfe60390STomohiro Kusumi * Get pointer to the next entry.
764cfe60390STomohiro Kusumi */
765cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)((char *)data + offset);
766cfe60390STomohiro Kusumi }
767cfe60390STomohiro Kusumi
768cfe60390STomohiro Kusumi return (0);
769cfe60390STomohiro Kusumi }
770cfe60390STomohiro Kusumi
771cfe60390STomohiro Kusumi void
ext2_dirbad(struct inode * ip,doff_t offset,char * how)772cfe60390STomohiro Kusumi ext2_dirbad(struct inode *ip, doff_t offset, char *how)
773cfe60390STomohiro Kusumi {
774cfe60390STomohiro Kusumi struct mount *mp;
775cfe60390STomohiro Kusumi
776cfe60390STomohiro Kusumi mp = ITOV(ip)->v_mount;
777cfe60390STomohiro Kusumi if ((mp->mnt_flag & MNT_RDONLY) == 0)
778cfe60390STomohiro Kusumi panic("ext2_dirbad: %s: bad dir ino %ju at offset %ld: %s\n",
779cfe60390STomohiro Kusumi mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
780cfe60390STomohiro Kusumi (long)offset, how);
781cfe60390STomohiro Kusumi else
782cfe60390STomohiro Kusumi SDT_PROBE4(ext2fs, , trace, ext2_dirbad_error,
783cfe60390STomohiro Kusumi mp->mnt_stat.f_mntonname, ip->i_number, offset, how);
784cfe60390STomohiro Kusumi }
785cfe60390STomohiro Kusumi
786cfe60390STomohiro Kusumi /*
787cfe60390STomohiro Kusumi * Do consistency checking on a directory entry:
788cfe60390STomohiro Kusumi * record length must be multiple of 4
789cfe60390STomohiro Kusumi * entry must fit in rest of its DIRBLKSIZ block
790cfe60390STomohiro Kusumi * record must be large enough to contain entry
791cfe60390STomohiro Kusumi * name is not longer than MAXNAMLEN
792cfe60390STomohiro Kusumi * name must be as long as advertised, and null terminated
793cfe60390STomohiro Kusumi */
794cfe60390STomohiro Kusumi static int
ext2_check_direntry(struct vnode * dp,struct ext2fs_direct_2 * de,int entryoffsetinblock)795*2532d84aSTomohiro Kusumi ext2_check_direntry(struct vnode *dp, struct ext2fs_direct_2 *de,
796cfe60390STomohiro Kusumi int entryoffsetinblock)
797cfe60390STomohiro Kusumi {
798*2532d84aSTomohiro Kusumi struct m_ext2fs *fs = VTOI(dp)->i_e2fs;
799cfe60390STomohiro Kusumi char *error_msg = NULL;
800cfe60390STomohiro Kusumi
801cfe60390STomohiro Kusumi if (le16toh(de->e2d_reclen) < EXT2_DIR_REC_LEN(1))
802cfe60390STomohiro Kusumi error_msg = "rec_len is smaller than minimal";
803cfe60390STomohiro Kusumi else if (le16toh(de->e2d_reclen) % 4 != 0)
804cfe60390STomohiro Kusumi error_msg = "rec_len % 4 != 0";
805cfe60390STomohiro Kusumi else if (le16toh(de->e2d_reclen) < EXT2_DIR_REC_LEN(de->e2d_namlen))
806cfe60390STomohiro Kusumi error_msg = "reclen is too small for name_len";
807*2532d84aSTomohiro Kusumi else if (entryoffsetinblock + le16toh(de->e2d_reclen)> fs->e2fs_bsize)
808cfe60390STomohiro Kusumi error_msg = "directory entry across blocks";
809*2532d84aSTomohiro Kusumi else if (le32toh(de->e2d_ino) > fs->e2fs->e2fs_icount)
810*2532d84aSTomohiro Kusumi error_msg = "directory entry inode out of bounds";
811cfe60390STomohiro Kusumi
812cfe60390STomohiro Kusumi if (error_msg != NULL) {
813cfe60390STomohiro Kusumi SDT_PROBE5(ext2fs, , trace, ext2_dirbadentry_error,
814cfe60390STomohiro Kusumi error_msg, entryoffsetinblock,
815cfe60390STomohiro Kusumi le32toh(de->e2d_ino), le16toh(de->e2d_reclen),
816cfe60390STomohiro Kusumi de->e2d_namlen);
817cfe60390STomohiro Kusumi }
818*2532d84aSTomohiro Kusumi return (error_msg == NULL ? 0 : EINVAL);
819cfe60390STomohiro Kusumi }
820cfe60390STomohiro Kusumi
821cfe60390STomohiro Kusumi /*
822cfe60390STomohiro Kusumi * Insert an entry into the fresh directory block.
823cfe60390STomohiro Kusumi * Initialize entry tail if the metadata_csum feature is turned on.
824cfe60390STomohiro Kusumi */
825cfe60390STomohiro Kusumi static int
ext2_add_first_entry(struct vnode * dvp,struct ext2fs_direct_2 * entry,struct componentname * cnp)826cfe60390STomohiro Kusumi ext2_add_first_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry,
827cfe60390STomohiro Kusumi struct componentname *cnp)
828cfe60390STomohiro Kusumi {
829cfe60390STomohiro Kusumi struct inode *dp;
830cfe60390STomohiro Kusumi struct iovec aiov;
831cfe60390STomohiro Kusumi struct uio auio;
832cfe60390STomohiro Kusumi char* buf = NULL;
833cfe60390STomohiro Kusumi int dirblksize, error;
834cfe60390STomohiro Kusumi
835cfe60390STomohiro Kusumi dp = VTOI(dvp);
836cfe60390STomohiro Kusumi dirblksize = dp->i_e2fs->e2fs_bsize;
837cfe60390STomohiro Kusumi
838cfe60390STomohiro Kusumi if (dp->i_offset & (dirblksize - 1))
839cfe60390STomohiro Kusumi panic("ext2_add_first_entry: bad directory offset");
840cfe60390STomohiro Kusumi
841cfe60390STomohiro Kusumi if (EXT2_HAS_RO_COMPAT_FEATURE(dp->i_e2fs,
842cfe60390STomohiro Kusumi EXT2F_ROCOMPAT_METADATA_CKSUM)) {
843cfe60390STomohiro Kusumi entry->e2d_reclen = htole16(dirblksize -
844cfe60390STomohiro Kusumi sizeof(struct ext2fs_direct_tail));
845cfe60390STomohiro Kusumi buf = malloc(dirblksize, M_TEMP, M_WAITOK);
846cfe60390STomohiro Kusumi memcpy(buf, entry, EXT2_DIR_REC_LEN(entry->e2d_namlen));
847cfe60390STomohiro Kusumi ext2_init_dirent_tail(EXT2_DIRENT_TAIL(buf, dirblksize));
848cfe60390STomohiro Kusumi ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)buf);
849cfe60390STomohiro Kusumi
850cfe60390STomohiro Kusumi auio.uio_offset = dp->i_offset;
851cfe60390STomohiro Kusumi auio.uio_resid = dirblksize;
852cfe60390STomohiro Kusumi aiov.iov_len = auio.uio_resid;
853cfe60390STomohiro Kusumi aiov.iov_base = (caddr_t)buf;
854cfe60390STomohiro Kusumi } else {
855cfe60390STomohiro Kusumi entry->e2d_reclen = htole16(dirblksize);
856cfe60390STomohiro Kusumi auio.uio_offset = dp->i_offset;
857cfe60390STomohiro Kusumi auio.uio_resid = EXT2_DIR_REC_LEN(entry->e2d_namlen);
858cfe60390STomohiro Kusumi aiov.iov_len = auio.uio_resid;
859cfe60390STomohiro Kusumi aiov.iov_base = (caddr_t)entry;
860cfe60390STomohiro Kusumi }
861cfe60390STomohiro Kusumi
862cfe60390STomohiro Kusumi auio.uio_iov = &aiov;
863cfe60390STomohiro Kusumi auio.uio_iovcnt = 1;
864cfe60390STomohiro Kusumi auio.uio_rw = UIO_WRITE;
865cfe60390STomohiro Kusumi auio.uio_segflg = UIO_SYSSPACE;
866cfe60390STomohiro Kusumi auio.uio_td = (struct thread *)0;
867cfe60390STomohiro Kusumi error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
868cfe60390STomohiro Kusumi if (error)
869cfe60390STomohiro Kusumi goto out;
870cfe60390STomohiro Kusumi
871cfe60390STomohiro Kusumi dp->i_size = roundup2(dp->i_size, dirblksize);
872cfe60390STomohiro Kusumi dp->i_flag |= IN_CHANGE;
873cfe60390STomohiro Kusumi
874cfe60390STomohiro Kusumi out:
875cfe60390STomohiro Kusumi if (buf)
876cfe60390STomohiro Kusumi free(buf, M_TEMP);
877cfe60390STomohiro Kusumi return (error);
878cfe60390STomohiro Kusumi }
879cfe60390STomohiro Kusumi
880cfe60390STomohiro Kusumi /*
881cfe60390STomohiro Kusumi * Write a directory entry after a call to namei, using the parameters
882cfe60390STomohiro Kusumi * that it left in nameidata. The argument ip is the inode which the new
883cfe60390STomohiro Kusumi * directory entry will refer to. Dvp is a pointer to the directory to
884cfe60390STomohiro Kusumi * be written, which was left locked by namei. Remaining parameters
885cfe60390STomohiro Kusumi * (dp->i_offset, dp->i_count) indicate how the space for the new
886cfe60390STomohiro Kusumi * entry is to be obtained.
887cfe60390STomohiro Kusumi */
888cfe60390STomohiro Kusumi int
ext2_direnter(struct inode * ip,struct vnode * dvp,struct componentname * cnp)889cfe60390STomohiro Kusumi ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
890cfe60390STomohiro Kusumi {
891cfe60390STomohiro Kusumi struct inode *dp;
892cfe60390STomohiro Kusumi struct ext2fs_direct_2 newdir;
8934c3802f8STomohiro Kusumi //int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize;
894cfe60390STomohiro Kusumi int error;
895cfe60390STomohiro Kusumi
896cfe60390STomohiro Kusumi dp = VTOI(dvp);
897cfe60390STomohiro Kusumi newdir.e2d_ino = htole32(ip->i_number);
898cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
899cfe60390STomohiro Kusumi EXT2F_INCOMPAT_FTYPE)) {
900cfe60390STomohiro Kusumi newdir.e2d_namlen = cnp->cn_namelen;
901cfe60390STomohiro Kusumi newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode));
902cfe60390STomohiro Kusumi } else
903cfe60390STomohiro Kusumi newdir.e2d_namlen = htole16(cnp->cn_namelen);
904cfe60390STomohiro Kusumi
905cfe60390STomohiro Kusumi bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
906cfe60390STomohiro Kusumi
90790d6e3f0STomohiro Kusumi /*
908f875a8eeSTomohiro Kusumi * XXX HTree dirents sometimes get lost (or not visible via readdir).
909f875a8eeSTomohiro Kusumi * This is reproducible by copying a directory with enough regular files
910f875a8eeSTomohiro Kusumi * (e.g. contrib/libarchive/libarchive) from somewhere to ext2 for
911f875a8eeSTomohiro Kusumi * several times until it reproduces. dumpe2fs shows same allocation as
912f875a8eeSTomohiro Kusumi * successful case. e2fsck says ext2 is clean with correct number of
913f875a8eeSTomohiro Kusumi * files.
914f875a8eeSTomohiro Kusumi *
91558706357STomohiro Kusumi * If that same image is mounted on Linux, some of the lost files are
91658706357STomohiro Kusumi * visible. E2fsck initially doesn't complain on Linux either, but
91758706357STomohiro Kusumi * after unmount, e2fsck starts to complain and files get recovered with
91858706357STomohiro Kusumi * a few sent to lost+found.
91990d6e3f0STomohiro Kusumi */
9204c3802f8STomohiro Kusumi #if 0
92137483ab3STomohiro Kusumi if (ext2_htree_has_idx(dp)) {
92237483ab3STomohiro Kusumi error = ext2_htree_add_entry(dvp, &newdir, cnp);
92337483ab3STomohiro Kusumi if (error) {
92437483ab3STomohiro Kusumi dp->i_flag &= ~IN_E3INDEX;
92537483ab3STomohiro Kusumi dp->i_flag |= IN_CHANGE | IN_UPDATE;
92637483ab3STomohiro Kusumi }
92737483ab3STomohiro Kusumi return (error);
92837483ab3STomohiro Kusumi }
92937483ab3STomohiro Kusumi
930cfe60390STomohiro Kusumi if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
931cfe60390STomohiro Kusumi !ext2_htree_has_idx(dp)) {
932cfe60390STomohiro Kusumi if ((dp->i_size / DIRBLKSIZ) == 1 &&
933cfe60390STomohiro Kusumi dp->i_offset == DIRBLKSIZ) {
934cfe60390STomohiro Kusumi /*
935cfe60390STomohiro Kusumi * Making indexed directory when one block is not
936cfe60390STomohiro Kusumi * enough to save all entries.
937cfe60390STomohiro Kusumi */
938cfe60390STomohiro Kusumi return ext2_htree_create_index(dvp, cnp, &newdir);
939cfe60390STomohiro Kusumi }
940cfe60390STomohiro Kusumi }
9414c3802f8STomohiro Kusumi #endif
942cfe60390STomohiro Kusumi /*
943cfe60390STomohiro Kusumi * If dp->i_count is 0, then namei could find no
944cfe60390STomohiro Kusumi * space in the directory. Here, dp->i_offset will
945cfe60390STomohiro Kusumi * be on a directory block boundary and we will write the
946cfe60390STomohiro Kusumi * new entry into a fresh block.
947cfe60390STomohiro Kusumi */
948cfe60390STomohiro Kusumi if (dp->i_count == 0)
949cfe60390STomohiro Kusumi return ext2_add_first_entry(dvp, &newdir, cnp);
950cfe60390STomohiro Kusumi
951cfe60390STomohiro Kusumi error = ext2_add_entry(dvp, &newdir);
952cfe60390STomohiro Kusumi if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
953cfe60390STomohiro Kusumi error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
954cfe60390STomohiro Kusumi cnp->cn_cred);
955cfe60390STomohiro Kusumi return (error);
956cfe60390STomohiro Kusumi }
957cfe60390STomohiro Kusumi
958cfe60390STomohiro Kusumi /*
959cfe60390STomohiro Kusumi * Insert an entry into the directory block.
960cfe60390STomohiro Kusumi * Compact the contents.
961cfe60390STomohiro Kusumi */
962cfe60390STomohiro Kusumi int
ext2_add_entry(struct vnode * dvp,struct ext2fs_direct_2 * entry)963cfe60390STomohiro Kusumi ext2_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry)
964cfe60390STomohiro Kusumi {
965cfe60390STomohiro Kusumi struct ext2fs_direct_2 *ep, *nep;
966cfe60390STomohiro Kusumi struct inode *dp;
967cfe60390STomohiro Kusumi struct buf *bp;
968cfe60390STomohiro Kusumi u_int dsize;
969cfe60390STomohiro Kusumi int error, loc, newentrysize, spacefree;
970cfe60390STomohiro Kusumi char *dirbuf;
971cfe60390STomohiro Kusumi
972cfe60390STomohiro Kusumi dp = VTOI(dvp);
973cfe60390STomohiro Kusumi
974cfe60390STomohiro Kusumi /*
975cfe60390STomohiro Kusumi * If dp->i_count is non-zero, then namei found space
976cfe60390STomohiro Kusumi * for the new entry in the range dp->i_offset to
977cfe60390STomohiro Kusumi * dp->i_offset + dp->i_count in the directory.
978cfe60390STomohiro Kusumi * To use this space, we may have to compact the entries located
979cfe60390STomohiro Kusumi * there, by copying them together towards the beginning of the
980cfe60390STomohiro Kusumi * block, leaving the free space in one usable chunk at the end.
981cfe60390STomohiro Kusumi */
982cfe60390STomohiro Kusumi
983cfe60390STomohiro Kusumi /*
984cfe60390STomohiro Kusumi * Increase size of directory if entry eats into new space.
985cfe60390STomohiro Kusumi * This should never push the size past a new multiple of
986cfe60390STomohiro Kusumi * DIRBLKSIZE.
987cfe60390STomohiro Kusumi *
988cfe60390STomohiro Kusumi * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
989cfe60390STomohiro Kusumi */
990cfe60390STomohiro Kusumi if (dp->i_offset + dp->i_count > dp->i_size)
991cfe60390STomohiro Kusumi dp->i_size = dp->i_offset + dp->i_count;
992cfe60390STomohiro Kusumi /*
993cfe60390STomohiro Kusumi * Get the block containing the space for the new directory entry.
994cfe60390STomohiro Kusumi */
995cfe60390STomohiro Kusumi if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
996cfe60390STomohiro Kusumi &bp)) != 0)
997cfe60390STomohiro Kusumi return (error);
998cfe60390STomohiro Kusumi /*
999cfe60390STomohiro Kusumi * Find space for the new entry. In the simple case, the entry at
1000cfe60390STomohiro Kusumi * offset base will have the space. If it does not, then namei
1001cfe60390STomohiro Kusumi * arranged that compacting the region dp->i_offset to
1002cfe60390STomohiro Kusumi * dp->i_offset + dp->i_count would yield the
1003cfe60390STomohiro Kusumi * space.
1004cfe60390STomohiro Kusumi */
1005cfe60390STomohiro Kusumi newentrysize = EXT2_DIR_REC_LEN(entry->e2d_namlen);
1006cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)dirbuf;
1007cfe60390STomohiro Kusumi dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen);
1008cfe60390STomohiro Kusumi spacefree = le16toh(ep->e2d_reclen) - dsize;
1009cfe60390STomohiro Kusumi for (loc = le16toh(ep->e2d_reclen); loc < dp->i_count; ) {
1010cfe60390STomohiro Kusumi nep = (struct ext2fs_direct_2 *)(dirbuf + loc);
1011cfe60390STomohiro Kusumi if (le32toh(ep->e2d_ino)) {
1012cfe60390STomohiro Kusumi /* trim the existing slot */
1013cfe60390STomohiro Kusumi ep->e2d_reclen = htole16(dsize);
1014cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1015cfe60390STomohiro Kusumi } else {
1016cfe60390STomohiro Kusumi /* overwrite; nothing there; header is ours */
1017cfe60390STomohiro Kusumi spacefree += dsize;
1018cfe60390STomohiro Kusumi }
1019cfe60390STomohiro Kusumi dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen);
1020cfe60390STomohiro Kusumi spacefree += le16toh(nep->e2d_reclen) - dsize;
1021cfe60390STomohiro Kusumi loc += le16toh(nep->e2d_reclen);
1022cfe60390STomohiro Kusumi bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1023cfe60390STomohiro Kusumi }
1024cfe60390STomohiro Kusumi /*
1025cfe60390STomohiro Kusumi * Update the pointer fields in the previous entry (if any),
1026cfe60390STomohiro Kusumi * copy in the new entry, and write out the block.
1027cfe60390STomohiro Kusumi */
1028cfe60390STomohiro Kusumi if (ep->e2d_ino == 0) {
1029cfe60390STomohiro Kusumi if (spacefree + dsize < newentrysize)
1030cfe60390STomohiro Kusumi panic("ext2_direnter: compact1");
1031cfe60390STomohiro Kusumi entry->e2d_reclen = htole16(spacefree + dsize);
1032cfe60390STomohiro Kusumi } else {
1033cfe60390STomohiro Kusumi if (spacefree < newentrysize)
1034cfe60390STomohiro Kusumi panic("ext2_direnter: compact2");
1035cfe60390STomohiro Kusumi entry->e2d_reclen = htole16(spacefree);
1036cfe60390STomohiro Kusumi ep->e2d_reclen = htole16(dsize);
1037cfe60390STomohiro Kusumi ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1038cfe60390STomohiro Kusumi }
1039cfe60390STomohiro Kusumi bcopy((caddr_t)entry, (caddr_t)ep, (u_int)newentrysize);
1040cfe60390STomohiro Kusumi ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1041cfe60390STomohiro Kusumi if (DOINGASYNC(dvp)) {
1042cfe60390STomohiro Kusumi bdwrite(bp);
1043cfe60390STomohiro Kusumi error = 0;
1044cfe60390STomohiro Kusumi } else {
1045cfe60390STomohiro Kusumi error = bwrite(bp);
1046cfe60390STomohiro Kusumi }
1047cfe60390STomohiro Kusumi dp->i_flag |= IN_CHANGE | IN_UPDATE;
1048cfe60390STomohiro Kusumi return (error);
1049cfe60390STomohiro Kusumi }
1050cfe60390STomohiro Kusumi
1051cfe60390STomohiro Kusumi /*
1052cfe60390STomohiro Kusumi * Remove a directory entry after a call to namei, using
1053cfe60390STomohiro Kusumi * the parameters which it left in nameidata. The entry
1054cfe60390STomohiro Kusumi * dp->i_offset contains the offset into the directory of the
1055cfe60390STomohiro Kusumi * entry to be eliminated. The dp->i_count field contains the
1056cfe60390STomohiro Kusumi * size of the previous record in the directory. If this
1057cfe60390STomohiro Kusumi * is 0, the first entry is being deleted, so we need only
1058cfe60390STomohiro Kusumi * zero the inode number to mark the entry as free. If the
1059cfe60390STomohiro Kusumi * entry is not the first in the directory, we must reclaim
1060cfe60390STomohiro Kusumi * the space of the now empty record by adding the record size
1061cfe60390STomohiro Kusumi * to the size of the previous entry.
1062cfe60390STomohiro Kusumi */
1063cfe60390STomohiro Kusumi int
ext2_dirremove(struct vnode * dvp,struct componentname * cnp)1064cfe60390STomohiro Kusumi ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
1065cfe60390STomohiro Kusumi {
1066cfe60390STomohiro Kusumi struct inode *dp;
1067cfe60390STomohiro Kusumi struct ext2fs_direct_2 *ep, *rep;
1068cfe60390STomohiro Kusumi struct buf *bp;
1069cfe60390STomohiro Kusumi int error;
1070cfe60390STomohiro Kusumi
1071cfe60390STomohiro Kusumi dp = VTOI(dvp);
1072cfe60390STomohiro Kusumi if (dp->i_count == 0) {
1073cfe60390STomohiro Kusumi /*
1074cfe60390STomohiro Kusumi * First entry in block: set d_ino to zero.
1075cfe60390STomohiro Kusumi */
1076cfe60390STomohiro Kusumi if ((error =
1077cfe60390STomohiro Kusumi ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
1078cfe60390STomohiro Kusumi &bp)) != 0)
1079cfe60390STomohiro Kusumi return (error);
1080cfe60390STomohiro Kusumi ep->e2d_ino = 0;
1081cfe60390STomohiro Kusumi ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1082cfe60390STomohiro Kusumi error = bwrite(bp);
1083cfe60390STomohiro Kusumi dp->i_flag |= IN_CHANGE | IN_UPDATE;
1084cfe60390STomohiro Kusumi return (error);
1085cfe60390STomohiro Kusumi }
1086cfe60390STomohiro Kusumi /*
1087cfe60390STomohiro Kusumi * Collapse new free space into previous entry.
1088cfe60390STomohiro Kusumi */
1089cfe60390STomohiro Kusumi if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
1090cfe60390STomohiro Kusumi (char **)&ep, &bp)) != 0)
1091cfe60390STomohiro Kusumi return (error);
1092cfe60390STomohiro Kusumi
1093cfe60390STomohiro Kusumi /* Set 'rep' to the entry being removed. */
1094cfe60390STomohiro Kusumi if (dp->i_count == 0)
1095cfe60390STomohiro Kusumi rep = ep;
1096cfe60390STomohiro Kusumi else
1097cfe60390STomohiro Kusumi rep = (struct ext2fs_direct_2 *)((char *)ep +
1098cfe60390STomohiro Kusumi le16toh(ep->e2d_reclen));
1099cfe60390STomohiro Kusumi ep->e2d_reclen += rep->e2d_reclen;
1100cfe60390STomohiro Kusumi ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1101cfe60390STomohiro Kusumi if (DOINGASYNC(dvp) && dp->i_count != 0)
1102cfe60390STomohiro Kusumi bdwrite(bp);
1103cfe60390STomohiro Kusumi else
1104cfe60390STomohiro Kusumi error = bwrite(bp);
1105cfe60390STomohiro Kusumi dp->i_flag |= IN_CHANGE | IN_UPDATE;
1106cfe60390STomohiro Kusumi return (error);
1107cfe60390STomohiro Kusumi }
1108cfe60390STomohiro Kusumi
1109cfe60390STomohiro Kusumi /*
1110cfe60390STomohiro Kusumi * Rewrite an existing directory entry to point at the inode
1111cfe60390STomohiro Kusumi * supplied. The parameters describing the directory entry are
1112cfe60390STomohiro Kusumi * set up by a call to namei.
1113cfe60390STomohiro Kusumi */
1114cfe60390STomohiro Kusumi int
ext2_dirrewrite(struct inode * dp,struct inode * ip,struct componentname * cnp)1115cfe60390STomohiro Kusumi ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
1116cfe60390STomohiro Kusumi {
1117cfe60390STomohiro Kusumi struct buf *bp;
1118cfe60390STomohiro Kusumi struct ext2fs_direct_2 *ep;
1119cfe60390STomohiro Kusumi struct vnode *vdp = ITOV(dp);
1120cfe60390STomohiro Kusumi int error;
1121cfe60390STomohiro Kusumi
1122cfe60390STomohiro Kusumi if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
1123cfe60390STomohiro Kusumi &bp)) != 0)
1124cfe60390STomohiro Kusumi return (error);
1125cfe60390STomohiro Kusumi ep->e2d_ino = htole32(ip->i_number);
1126cfe60390STomohiro Kusumi if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1127cfe60390STomohiro Kusumi EXT2F_INCOMPAT_FTYPE))
1128cfe60390STomohiro Kusumi ep->e2d_type = DTTOFT(IFTODT(ip->i_mode));
1129cfe60390STomohiro Kusumi else
1130cfe60390STomohiro Kusumi ep->e2d_type = EXT2_FT_UNKNOWN;
1131cfe60390STomohiro Kusumi ext2_dirent_csum_set(dp, (struct ext2fs_direct_2 *)bp->b_data);
1132cfe60390STomohiro Kusumi error = bwrite(bp);
1133cfe60390STomohiro Kusumi dp->i_flag |= IN_CHANGE | IN_UPDATE;
1134cfe60390STomohiro Kusumi return (error);
1135cfe60390STomohiro Kusumi }
1136cfe60390STomohiro Kusumi
1137cfe60390STomohiro Kusumi /*
1138cfe60390STomohiro Kusumi * Check if a directory is empty or not.
1139cfe60390STomohiro Kusumi * Inode supplied must be locked.
1140cfe60390STomohiro Kusumi *
1141cfe60390STomohiro Kusumi * Using a struct dirtemplate here is not precisely
1142cfe60390STomohiro Kusumi * what we want, but better than using a struct direct.
1143cfe60390STomohiro Kusumi *
1144cfe60390STomohiro Kusumi * NB: does not handle corrupted directories.
1145cfe60390STomohiro Kusumi */
1146cfe60390STomohiro Kusumi int
ext2_dirempty(struct inode * ip,ino_t parentino,struct ucred * cred)1147cfe60390STomohiro Kusumi ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1148cfe60390STomohiro Kusumi {
1149cfe60390STomohiro Kusumi off_t off;
1150cfe60390STomohiro Kusumi struct dirtemplate dbuf;
1151cfe60390STomohiro Kusumi struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf;
1152cfe60390STomohiro Kusumi int error, count, namlen;
1153cfe60390STomohiro Kusumi #define MINDIRSIZ (sizeof(struct dirtemplate) / 2)
1154cfe60390STomohiro Kusumi
1155cfe60390STomohiro Kusumi for (off = 0; off < ip->i_size; off += le16toh(dp->e2d_reclen)) {
1156cfe60390STomohiro Kusumi error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1157cfe60390STomohiro Kusumi off, UIO_SYSSPACE, IO_NODELOCKED, cred, &count);
1158cfe60390STomohiro Kusumi /*
1159cfe60390STomohiro Kusumi * Since we read MINDIRSIZ, residual must
1160cfe60390STomohiro Kusumi * be 0 unless we're at end of file.
1161cfe60390STomohiro Kusumi */
1162cfe60390STomohiro Kusumi if (error || count != 0)
1163cfe60390STomohiro Kusumi return (0);
1164cfe60390STomohiro Kusumi /* avoid infinite loops */
1165cfe60390STomohiro Kusumi if (dp->e2d_reclen == 0)
1166cfe60390STomohiro Kusumi return (0);
1167cfe60390STomohiro Kusumi /* skip empty entries */
1168cfe60390STomohiro Kusumi if (dp->e2d_ino == 0)
1169cfe60390STomohiro Kusumi continue;
1170cfe60390STomohiro Kusumi /* accept only "." and ".." */
1171cfe60390STomohiro Kusumi namlen = dp->e2d_namlen;
1172cfe60390STomohiro Kusumi if (namlen > 2)
1173cfe60390STomohiro Kusumi return (0);
1174cfe60390STomohiro Kusumi if (dp->e2d_name[0] != '.')
1175cfe60390STomohiro Kusumi return (0);
1176cfe60390STomohiro Kusumi /*
1177cfe60390STomohiro Kusumi * At this point namlen must be 1 or 2.
1178cfe60390STomohiro Kusumi * 1 implies ".", 2 implies ".." if second
1179cfe60390STomohiro Kusumi * char is also "."
1180cfe60390STomohiro Kusumi */
1181cfe60390STomohiro Kusumi if (namlen == 1)
1182cfe60390STomohiro Kusumi continue;
1183cfe60390STomohiro Kusumi if (dp->e2d_name[1] == '.' && le32toh(dp->e2d_ino) == parentino)
1184cfe60390STomohiro Kusumi continue;
1185cfe60390STomohiro Kusumi return (0);
1186cfe60390STomohiro Kusumi }
1187cfe60390STomohiro Kusumi return (1);
1188cfe60390STomohiro Kusumi }
1189cfe60390STomohiro Kusumi
1190cfe60390STomohiro Kusumi /*
1191cfe60390STomohiro Kusumi * Check if source directory is in the path of the target directory.
1192cfe60390STomohiro Kusumi * Target is supplied locked, source is unlocked.
1193cfe60390STomohiro Kusumi * The target is always vput before returning.
1194cfe60390STomohiro Kusumi */
1195cfe60390STomohiro Kusumi int
ext2_checkpath(struct inode * source,struct inode * target,struct ucred * cred)1196cfe60390STomohiro Kusumi ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
1197cfe60390STomohiro Kusumi {
1198cfe60390STomohiro Kusumi struct vnode *vp;
1199cfe60390STomohiro Kusumi int error, namlen;
1200cfe60390STomohiro Kusumi struct dirtemplate dirbuf;
1201cfe60390STomohiro Kusumi
1202cfe60390STomohiro Kusumi vp = ITOV(target);
1203cfe60390STomohiro Kusumi if (target->i_number == source->i_number) {
1204cfe60390STomohiro Kusumi error = EEXIST;
1205cfe60390STomohiro Kusumi goto out;
1206cfe60390STomohiro Kusumi }
1207cfe60390STomohiro Kusumi if (target->i_number == EXT2_ROOTINO) {
1208cfe60390STomohiro Kusumi error = 0;
1209cfe60390STomohiro Kusumi goto out;
1210cfe60390STomohiro Kusumi }
1211cfe60390STomohiro Kusumi
1212cfe60390STomohiro Kusumi for (;;) {
1213cfe60390STomohiro Kusumi if (vp->v_type != VDIR) {
1214cfe60390STomohiro Kusumi error = ENOTDIR;
1215cfe60390STomohiro Kusumi break;
1216cfe60390STomohiro Kusumi }
1217cfe60390STomohiro Kusumi error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1218cfe60390STomohiro Kusumi sizeof(struct dirtemplate), (off_t)0,
1219cfe60390STomohiro Kusumi UIO_SYSSPACE, IO_NODELOCKED, cred, NULL);
1220cfe60390STomohiro Kusumi if (error != 0)
1221cfe60390STomohiro Kusumi break;
1222cfe60390STomohiro Kusumi namlen = dirbuf.dotdot_type; /* like ufs little-endian */
1223cfe60390STomohiro Kusumi if (namlen != 2 ||
1224cfe60390STomohiro Kusumi dirbuf.dotdot_name[0] != '.' ||
1225cfe60390STomohiro Kusumi dirbuf.dotdot_name[1] != '.') {
1226cfe60390STomohiro Kusumi error = ENOTDIR;
1227cfe60390STomohiro Kusumi break;
1228cfe60390STomohiro Kusumi }
1229cfe60390STomohiro Kusumi if (le32toh(dirbuf.dotdot_ino) == source->i_number) {
1230cfe60390STomohiro Kusumi error = EINVAL;
1231cfe60390STomohiro Kusumi break;
1232cfe60390STomohiro Kusumi }
1233cfe60390STomohiro Kusumi if (le32toh(dirbuf.dotdot_ino) == EXT2_ROOTINO)
1234cfe60390STomohiro Kusumi break;
1235cfe60390STomohiro Kusumi vput(vp);
1236cfe60390STomohiro Kusumi if ((error = VFS_VGET(vp->v_mount, NULL,
1237cfe60390STomohiro Kusumi le32toh(dirbuf.dotdot_ino), &vp)) != 0) {
1238cfe60390STomohiro Kusumi vp = NULL;
1239cfe60390STomohiro Kusumi break;
1240cfe60390STomohiro Kusumi }
1241cfe60390STomohiro Kusumi }
1242cfe60390STomohiro Kusumi
1243cfe60390STomohiro Kusumi out:
1244cfe60390STomohiro Kusumi if (error == ENOTDIR)
1245cfe60390STomohiro Kusumi SDT_PROBE2(ext2fs, , lookup, trace, 1,
1246cfe60390STomohiro Kusumi "checkpath: .. not a directory");
1247cfe60390STomohiro Kusumi if (vp != NULL)
1248cfe60390STomohiro Kusumi vput(vp);
1249cfe60390STomohiro Kusumi return (error);
1250cfe60390STomohiro Kusumi }
1251