ext2fs_lookup.c revision 1.78 1 1.78 riastrad /* $NetBSD: ext2fs_lookup.c,v 1.78 2015/03/27 17:27:56 riastradh Exp $ */
2 1.1 bouyer
3 1.32 perry /*
4 1.1 bouyer * Modified for NetBSD 1.2E
5 1.1 bouyer * May 1997, Manuel Bouyer
6 1.1 bouyer * Laboratoire d'informatique de Paris VI
7 1.1 bouyer */
8 1.1 bouyer /*
9 1.1 bouyer * modified for Lites 1.1
10 1.1 bouyer *
11 1.1 bouyer * Aug 1995, Godmar Back (gback (at) cs.utah.edu)
12 1.1 bouyer * University of Utah, Department of Computer Science
13 1.1 bouyer */
14 1.1 bouyer /*
15 1.1 bouyer * Copyright (c) 1989, 1993
16 1.1 bouyer * The Regents of the University of California. All rights reserved.
17 1.1 bouyer * (c) UNIX System Laboratories, Inc.
18 1.1 bouyer * All or some portions of this file are derived from material licensed
19 1.1 bouyer * to the University of California by American Telephone and Telegraph
20 1.1 bouyer * Co. or Unix System Laboratories, Inc. and are reproduced herein with
21 1.1 bouyer * the permission of UNIX System Laboratories, Inc.
22 1.1 bouyer *
23 1.1 bouyer * Redistribution and use in source and binary forms, with or without
24 1.1 bouyer * modification, are permitted provided that the following conditions
25 1.1 bouyer * are met:
26 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
27 1.8 christos * notice, this list of conditions and the following disclaimer.
28 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
29 1.8 christos * notice, this list of conditions and the following disclaimer in the
30 1.8 christos * documentation and/or other materials provided with the distribution.
31 1.28 agc * 3. Neither the name of the University nor the names of its contributors
32 1.8 christos * may be used to endorse or promote products derived from this software
33 1.8 christos * without specific prior written permission.
34 1.1 bouyer *
35 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 1.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 1.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 1.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 1.1 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 1.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 1.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 1.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 1.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 1.1 bouyer * SUCH DAMAGE.
46 1.1 bouyer *
47 1.1 bouyer * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94
48 1.1 bouyer */
49 1.18 lukem
50 1.18 lukem #include <sys/cdefs.h>
51 1.78 riastrad __KERNEL_RCSID(0, "$NetBSD: ext2fs_lookup.c,v 1.78 2015/03/27 17:27:56 riastradh Exp $");
52 1.1 bouyer
53 1.1 bouyer #include <sys/param.h>
54 1.1 bouyer #include <sys/systm.h>
55 1.1 bouyer #include <sys/namei.h>
56 1.1 bouyer #include <sys/buf.h>
57 1.1 bouyer #include <sys/file.h>
58 1.1 bouyer #include <sys/mount.h>
59 1.1 bouyer #include <sys/vnode.h>
60 1.67 para #include <sys/kmem.h>
61 1.1 bouyer #include <sys/malloc.h>
62 1.1 bouyer #include <sys/dirent.h>
63 1.44 elad #include <sys/kauth.h>
64 1.53 ad #include <sys/proc.h>
65 1.1 bouyer
66 1.1 bouyer #include <ufs/ufs/inode.h>
67 1.1 bouyer #include <ufs/ufs/ufsmount.h>
68 1.1 bouyer #include <ufs/ufs/ufs_extern.h>
69 1.1 bouyer
70 1.1 bouyer #include <ufs/ext2fs/ext2fs_extern.h>
71 1.1 bouyer #include <ufs/ext2fs/ext2fs_dir.h>
72 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
73 1.1 bouyer
74 1.68 elad #include <miscfs/genfs/genfs.h>
75 1.68 elad
76 1.1 bouyer extern int dirchk;
77 1.1 bouyer
78 1.37 xtraeme static void ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir,
79 1.37 xtraeme struct dirent *ffsdir);
80 1.37 xtraeme static int ext2fs_dirbadentry(struct vnode *dp,
81 1.1 bouyer struct ext2fs_direct *de,
82 1.37 xtraeme int entryoffsetinblock);
83 1.1 bouyer
84 1.1 bouyer /*
85 1.1 bouyer * the problem that is tackled below is the fact that FFS
86 1.1 bouyer * includes the terminating zero on disk while EXT2FS doesn't
87 1.1 bouyer * this implies that we need to introduce some padding.
88 1.32 perry * For instance, a filename "sbin" has normally a reclen 12
89 1.32 perry * in EXT2, but 16 in FFS.
90 1.1 bouyer * This reminds me of that Pepsi commercial: 'Kid saved a lousy nine cents...'
91 1.1 bouyer * If it wasn't for that, the complete ufs code for directories would
92 1.1 bouyer * have worked w/o changes (except for the difference in DIRBLKSIZ)
93 1.1 bouyer */
94 1.1 bouyer static void
95 1.37 xtraeme ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir, struct dirent *ffsdir)
96 1.1 bouyer {
97 1.7 perry memset(ffsdir, 0, sizeof(struct dirent));
98 1.3 bouyer ffsdir->d_fileno = fs2h32(e2dir->e2d_ino);
99 1.13 bouyer ffsdir->d_namlen = e2dir->e2d_namlen;
100 1.1 bouyer
101 1.1 bouyer ffsdir->d_type = DT_UNKNOWN; /* don't know more here */
102 1.1 bouyer #ifdef DIAGNOSTIC
103 1.36 christos #if MAXNAMLEN < E2FS_MAXNAMLEN
104 1.1 bouyer /*
105 1.36 christos * we should handle this more gracefully !
106 1.1 bouyer */
107 1.13 bouyer if (e2dir->e2d_namlen > MAXNAMLEN)
108 1.21 provos panic("ext2fs: e2dir->e2d_namlen");
109 1.19 thorpej #endif
110 1.1 bouyer #endif
111 1.1 bouyer strncpy(ffsdir->d_name, e2dir->e2d_name, ffsdir->d_namlen);
112 1.1 bouyer
113 1.32 perry /* Godmar thinks: since e2dir->e2d_reclen can be big and means
114 1.1 bouyer nothing anyway, we compute our own reclen according to what
115 1.1 bouyer we think is right
116 1.1 bouyer */
117 1.35 christos ffsdir->d_reclen = _DIRENT_SIZE(ffsdir);
118 1.1 bouyer }
119 1.1 bouyer
120 1.1 bouyer /*
121 1.1 bouyer * Vnode op for reading directories.
122 1.1 bouyer *
123 1.1 bouyer * Convert the on-disk entries to <sys/dirent.h> entries.
124 1.1 bouyer * the problem is that the conversion will blow up some entries by four bytes,
125 1.1 bouyer * so it can't be done in place. This is too bad. Right now the conversion is
126 1.32 perry * done entry by entry, the converted entry is sent via uiomove.
127 1.1 bouyer *
128 1.1 bouyer * XXX allocate a buffer, convert as many entries as possible, then send
129 1.1 bouyer * the whole buffer to uiomove
130 1.1 bouyer */
131 1.1 bouyer int
132 1.37 xtraeme ext2fs_readdir(void *v)
133 1.1 bouyer {
134 1.1 bouyer struct vop_readdir_args /* {
135 1.1 bouyer struct vnode *a_vp;
136 1.1 bouyer struct uio *a_uio;
137 1.44 elad kauth_cred_t a_cred;
138 1.5 fvdl int **a_eofflag;
139 1.5 fvdl off_t **a_cookies;
140 1.1 bouyer int ncookies;
141 1.1 bouyer } */ *ap = v;
142 1.5 fvdl struct uio *uio = ap->a_uio;
143 1.1 bouyer int error;
144 1.2 bouyer size_t e2fs_count, readcnt;
145 1.5 fvdl struct vnode *vp = ap->a_vp;
146 1.5 fvdl struct m_ext2fs *fs = VTOI(vp)->i_e2fs;
147 1.1 bouyer
148 1.1 bouyer struct ext2fs_direct *dp;
149 1.52 rumble struct dirent *dstd;
150 1.1 bouyer struct uio auio;
151 1.1 bouyer struct iovec aiov;
152 1.50 christos void *dirbuf;
153 1.1 bouyer off_t off = uio->uio_offset;
154 1.5 fvdl off_t *cookies = NULL;
155 1.5 fvdl int nc = 0, ncookies = 0;
156 1.3 bouyer int e2d_reclen;
157 1.1 bouyer
158 1.5 fvdl if (vp->v_type != VDIR)
159 1.5 fvdl return (ENOTDIR);
160 1.5 fvdl
161 1.2 bouyer e2fs_count = uio->uio_resid;
162 1.1 bouyer /* Make sure we don't return partial entries. */
163 1.2 bouyer e2fs_count -= (uio->uio_offset + e2fs_count) & (fs->e2fs_bsize -1);
164 1.2 bouyer if (e2fs_count <= 0)
165 1.1 bouyer return (EINVAL);
166 1.1 bouyer
167 1.1 bouyer auio = *uio;
168 1.1 bouyer auio.uio_iov = &aiov;
169 1.1 bouyer auio.uio_iovcnt = 1;
170 1.2 bouyer aiov.iov_len = e2fs_count;
171 1.2 bouyer auio.uio_resid = e2fs_count;
172 1.40 yamt UIO_SETUP_SYSSPACE(&auio);
173 1.67 para dirbuf = kmem_alloc(e2fs_count, KM_SLEEP);
174 1.67 para dstd = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
175 1.5 fvdl if (ap->a_ncookies) {
176 1.52 rumble nc = e2fs_count / _DIRENT_MINSIZE((struct dirent *)0);
177 1.52 rumble ncookies = nc;
178 1.16 thorpej cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
179 1.5 fvdl *ap->a_cookies = cookies;
180 1.5 fvdl }
181 1.1 bouyer aiov.iov_base = dirbuf;
182 1.1 bouyer
183 1.78 riastrad error = UFS_BUFRD(ap->a_vp, &auio, 0, ap->a_cred);
184 1.1 bouyer if (error == 0) {
185 1.2 bouyer readcnt = e2fs_count - auio.uio_resid;
186 1.32 perry for (dp = (struct ext2fs_direct *)dirbuf;
187 1.1 bouyer (char *)dp < (char *)dirbuf + readcnt; ) {
188 1.3 bouyer e2d_reclen = fs2h16(dp->e2d_reclen);
189 1.3 bouyer if (e2d_reclen == 0) {
190 1.1 bouyer error = EIO;
191 1.1 bouyer break;
192 1.1 bouyer }
193 1.52 rumble ext2fs_dirconv2ffs(dp, dstd);
194 1.52 rumble if(dstd->d_reclen > uio->uio_resid) {
195 1.1 bouyer break;
196 1.1 bouyer }
197 1.52 rumble error = uiomove(dstd, dstd->d_reclen, uio);
198 1.52 rumble if (error != 0) {
199 1.1 bouyer break;
200 1.1 bouyer }
201 1.3 bouyer off = off + e2d_reclen;
202 1.1 bouyer if (cookies != NULL) {
203 1.1 bouyer *cookies++ = off;
204 1.1 bouyer if (--ncookies <= 0){
205 1.1 bouyer break; /* out of cookies */
206 1.1 bouyer }
207 1.1 bouyer }
208 1.1 bouyer /* advance dp */
209 1.3 bouyer dp = (struct ext2fs_direct *) ((char *)dp + e2d_reclen);
210 1.1 bouyer }
211 1.1 bouyer /* we need to correct uio_offset */
212 1.1 bouyer uio->uio_offset = off;
213 1.1 bouyer }
214 1.67 para kmem_free(dirbuf, e2fs_count);
215 1.67 para kmem_free(dstd, sizeof(*dstd));
216 1.31 ws *ap->a_eofflag = ext2fs_size(VTOI(ap->a_vp)) <= uio->uio_offset;
217 1.5 fvdl if (ap->a_ncookies) {
218 1.5 fvdl if (error) {
219 1.16 thorpej free(*ap->a_cookies, M_TEMP);
220 1.5 fvdl *ap->a_ncookies = 0;
221 1.5 fvdl *ap->a_cookies = NULL;
222 1.5 fvdl } else
223 1.5 fvdl *ap->a_ncookies = nc - ncookies;
224 1.5 fvdl }
225 1.1 bouyer return (error);
226 1.1 bouyer }
227 1.1 bouyer
228 1.1 bouyer /*
229 1.1 bouyer * Convert a component of a pathname into a pointer to a locked inode.
230 1.1 bouyer * This is a very central and rather complicated routine.
231 1.1 bouyer * If the file system is not maintained in a strict tree hierarchy,
232 1.1 bouyer * this can result in a deadlock situation (see comments in code below).
233 1.1 bouyer *
234 1.1 bouyer * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
235 1.1 bouyer * on whether the name is to be looked up, created, renamed, or deleted.
236 1.1 bouyer * When CREATE, RENAME, or DELETE is specified, information usable in
237 1.1 bouyer * creating, renaming, or deleting a directory entry may be calculated.
238 1.1 bouyer * If flag has LOCKPARENT or'ed into it and the target of the pathname
239 1.1 bouyer * exists, lookup returns both the target and its parent directory locked.
240 1.1 bouyer * When creating or renaming and LOCKPARENT is specified, the target may
241 1.1 bouyer * not be ".". When deleting and LOCKPARENT is specified, the target may
242 1.1 bouyer * be "."., but the caller must check to ensure it does an vrele and vput
243 1.1 bouyer * instead of two vputs.
244 1.1 bouyer *
245 1.1 bouyer * Overall outline of ext2fs_lookup:
246 1.1 bouyer *
247 1.1 bouyer * check accessibility of directory
248 1.1 bouyer * look for name in cache, if found, then if at end of path
249 1.1 bouyer * and deleting or creating, drop it, else return name
250 1.1 bouyer * search for name in directory, to found or notfound
251 1.1 bouyer * notfound:
252 1.1 bouyer * if creating, return locked directory, leaving info on available slots
253 1.1 bouyer * else return error
254 1.1 bouyer * found:
255 1.1 bouyer * if at end of path and deleting, return information to allow delete
256 1.1 bouyer * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
257 1.1 bouyer * inode and return info to allow rewrite
258 1.1 bouyer * if not at end, add name to cache; if at end and neither creating
259 1.1 bouyer * nor deleting, add name to cache
260 1.1 bouyer */
261 1.1 bouyer int
262 1.37 xtraeme ext2fs_lookup(void *v)
263 1.1 bouyer {
264 1.74 hannken struct vop_lookup_v2_args /* {
265 1.1 bouyer struct vnode *a_dvp;
266 1.1 bouyer struct vnode **a_vpp;
267 1.1 bouyer struct componentname *a_cnp;
268 1.1 bouyer } */ *ap = v;
269 1.29 mycroft struct vnode *vdp = ap->a_dvp; /* vnode for directory being searched */
270 1.29 mycroft struct inode *dp = VTOI(vdp); /* inode for directory being searched */
271 1.1 bouyer struct buf *bp; /* a buffer of directory entries */
272 1.60 tsutsui struct ext2fs_direct *ep; /* the current directory entry */
273 1.1 bouyer int entryoffsetinblock; /* offset of ep in bp's buffer */
274 1.1 bouyer enum {NONE, COMPACT, FOUND} slotstatus;
275 1.1 bouyer doff_t slotoffset; /* offset of area with free space */
276 1.1 bouyer int slotsize; /* size of area at slotoffset */
277 1.1 bouyer int slotfreespace; /* amount of space free in slot */
278 1.1 bouyer int slotneeded; /* size of the entry we're seeking */
279 1.1 bouyer int numdirpasses; /* strategy for directory search */
280 1.1 bouyer doff_t endsearch; /* offset to end directory search */
281 1.1 bouyer doff_t prevoff; /* prev entry dp->i_offset */
282 1.75 hannken struct vnode *tdp; /* returned by vcache_get */
283 1.1 bouyer doff_t enduseful; /* pointer past last used dir slot */
284 1.1 bouyer u_long bmask; /* block offset mask */
285 1.1 bouyer int namlen, error;
286 1.1 bouyer struct vnode **vpp = ap->a_vpp;
287 1.1 bouyer struct componentname *cnp = ap->a_cnp;
288 1.44 elad kauth_cred_t cred = cnp->cn_cred;
289 1.29 mycroft int flags;
290 1.1 bouyer int nameiop = cnp->cn_nameiop;
291 1.29 mycroft struct ufsmount *ump = dp->i_ump;
292 1.29 mycroft int dirblksiz = ump->um_dirblksiz;
293 1.23 yamt ino_t foundino;
294 1.65 dholland struct ufs_lookup_results *results;
295 1.1 bouyer
296 1.29 mycroft flags = cnp->cn_flags;
297 1.1 bouyer
298 1.1 bouyer bp = NULL;
299 1.1 bouyer slotoffset = -1;
300 1.1 bouyer *vpp = NULL;
301 1.47 chs
302 1.1 bouyer /*
303 1.65 dholland * Produce the auxiliary lookup results into i_crap. Increment
304 1.65 dholland * its serial number so elsewhere we can tell if we're using
305 1.65 dholland * stale results. This should not be done this way. XXX.
306 1.65 dholland */
307 1.65 dholland results = &dp->i_crap;
308 1.65 dholland dp->i_crapcounter++;
309 1.65 dholland
310 1.65 dholland /*
311 1.1 bouyer * Check accessiblity of directory.
312 1.1 bouyer */
313 1.54 pooka if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
314 1.9 bouyer return (error);
315 1.9 bouyer
316 1.5 fvdl if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
317 1.5 fvdl (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
318 1.5 fvdl return (EROFS);
319 1.1 bouyer
320 1.1 bouyer /*
321 1.1 bouyer * We now have a segment name to search for, and a directory to search.
322 1.1 bouyer *
323 1.1 bouyer * Before tediously performing a linear scan of the directory,
324 1.1 bouyer * check the name cache to see if the directory/name pair
325 1.1 bouyer * we are looking for is known already.
326 1.1 bouyer */
327 1.72 dholland if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
328 1.72 dholland cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
329 1.71 dholland return *vpp == NULLVP ? ENOENT : 0;
330 1.71 dholland }
331 1.1 bouyer
332 1.1 bouyer /*
333 1.1 bouyer * Suppress search for slots unless creating
334 1.1 bouyer * file and at end of pathname, in which case
335 1.1 bouyer * we watch for a place to put the new file in
336 1.1 bouyer * case it doesn't already exist.
337 1.1 bouyer */
338 1.1 bouyer slotstatus = FOUND;
339 1.1 bouyer slotfreespace = slotsize = slotneeded = 0;
340 1.1 bouyer if ((nameiop == CREATE || nameiop == RENAME) &&
341 1.29 mycroft (flags & ISLASTCN)) {
342 1.1 bouyer slotstatus = NONE;
343 1.1 bouyer slotneeded = EXT2FS_DIRSIZ(cnp->cn_namelen);
344 1.1 bouyer }
345 1.1 bouyer
346 1.1 bouyer /*
347 1.1 bouyer * If there is cached information on a previous search of
348 1.1 bouyer * this directory, pick up where we last left off.
349 1.1 bouyer * We cache only lookups as these are the most common
350 1.1 bouyer * and have the greatest payoff. Caching CREATE has little
351 1.1 bouyer * benefit as it usually must search the entire directory
352 1.1 bouyer * to determine that the entry does not exist. Caching the
353 1.1 bouyer * location of the last DELETE or RENAME has not reduced
354 1.1 bouyer * profiling time and hence has been removed in the interest
355 1.1 bouyer * of simplicity.
356 1.1 bouyer */
357 1.29 mycroft bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
358 1.65 dholland if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
359 1.65 dholland results->ulr_diroff >= ext2fs_size(dp)) {
360 1.1 bouyer entryoffsetinblock = 0;
361 1.65 dholland results->ulr_offset = 0;
362 1.1 bouyer numdirpasses = 1;
363 1.1 bouyer } else {
364 1.65 dholland results->ulr_offset = results->ulr_diroff;
365 1.65 dholland if ((entryoffsetinblock = results->ulr_offset & bmask) &&
366 1.65 dholland (error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL, &bp)))
367 1.1 bouyer return (error);
368 1.1 bouyer numdirpasses = 2;
369 1.77 joerg namecache_count_2passes();
370 1.1 bouyer }
371 1.65 dholland prevoff = results->ulr_offset;
372 1.31 ws endsearch = roundup(ext2fs_size(dp), dirblksiz);
373 1.1 bouyer enduseful = 0;
374 1.1 bouyer
375 1.1 bouyer searchloop:
376 1.65 dholland while (results->ulr_offset < endsearch) {
377 1.29 mycroft if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
378 1.49 ad preempt();
379 1.1 bouyer /*
380 1.1 bouyer * If necessary, get the next directory block.
381 1.1 bouyer */
382 1.65 dholland if ((results->ulr_offset & bmask) == 0) {
383 1.1 bouyer if (bp != NULL)
384 1.53 ad brelse(bp, 0);
385 1.65 dholland error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL,
386 1.29 mycroft &bp);
387 1.1 bouyer if (error != 0)
388 1.1 bouyer return (error);
389 1.1 bouyer entryoffsetinblock = 0;
390 1.1 bouyer }
391 1.1 bouyer /*
392 1.1 bouyer * If still looking for a slot, and at a dirblksize
393 1.1 bouyer * boundary, have to start looking for free space again.
394 1.1 bouyer */
395 1.1 bouyer if (slotstatus == NONE &&
396 1.29 mycroft (entryoffsetinblock & (dirblksiz - 1)) == 0) {
397 1.1 bouyer slotoffset = -1;
398 1.1 bouyer slotfreespace = 0;
399 1.1 bouyer }
400 1.1 bouyer /*
401 1.1 bouyer * Get pointer to next entry.
402 1.1 bouyer * Full validation checks are slow, so we only check
403 1.1 bouyer * enough to insure forward progress through the
404 1.1 bouyer * directory. Complete checks can be run by patching
405 1.1 bouyer * "dirchk" to be true.
406 1.1 bouyer */
407 1.43 christos KASSERT(bp != NULL);
408 1.1 bouyer ep = (struct ext2fs_direct *)
409 1.1 bouyer ((char *)bp->b_data + entryoffsetinblock);
410 1.1 bouyer if (ep->e2d_reclen == 0 ||
411 1.9 bouyer (dirchk &&
412 1.29 mycroft ext2fs_dirbadentry(vdp, ep, entryoffsetinblock))) {
413 1.1 bouyer int i;
414 1.29 mycroft
415 1.65 dholland ufs_dirbad(dp, results->ulr_offset, "mangled entry");
416 1.29 mycroft i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
417 1.65 dholland results->ulr_offset += i;
418 1.1 bouyer entryoffsetinblock += i;
419 1.1 bouyer continue;
420 1.1 bouyer }
421 1.1 bouyer
422 1.1 bouyer /*
423 1.1 bouyer * If an appropriate sized slot has not yet been found,
424 1.1 bouyer * check to see if one is available. Also accumulate space
425 1.1 bouyer * in the current block so that we can determine if
426 1.1 bouyer * compaction is viable.
427 1.1 bouyer */
428 1.1 bouyer if (slotstatus != FOUND) {
429 1.3 bouyer int size = fs2h16(ep->e2d_reclen);
430 1.1 bouyer
431 1.1 bouyer if (ep->e2d_ino != 0)
432 1.13 bouyer size -= EXT2FS_DIRSIZ(ep->e2d_namlen);
433 1.1 bouyer if (size > 0) {
434 1.1 bouyer if (size >= slotneeded) {
435 1.1 bouyer slotstatus = FOUND;
436 1.65 dholland slotoffset = results->ulr_offset;
437 1.3 bouyer slotsize = fs2h16(ep->e2d_reclen);
438 1.1 bouyer } else if (slotstatus == NONE) {
439 1.1 bouyer slotfreespace += size;
440 1.1 bouyer if (slotoffset == -1)
441 1.65 dholland slotoffset = results->ulr_offset;
442 1.1 bouyer if (slotfreespace >= slotneeded) {
443 1.1 bouyer slotstatus = COMPACT;
444 1.65 dholland slotsize = results->ulr_offset +
445 1.29 mycroft fs2h16(ep->e2d_reclen) -
446 1.29 mycroft slotoffset;
447 1.1 bouyer }
448 1.1 bouyer }
449 1.1 bouyer }
450 1.1 bouyer }
451 1.1 bouyer
452 1.1 bouyer /*
453 1.1 bouyer * Check for a name match.
454 1.1 bouyer */
455 1.1 bouyer if (ep->e2d_ino) {
456 1.13 bouyer namlen = ep->e2d_namlen;
457 1.1 bouyer if (namlen == cnp->cn_namelen &&
458 1.29 mycroft !memcmp(cnp->cn_nameptr, ep->e2d_name,
459 1.29 mycroft (unsigned)namlen)) {
460 1.1 bouyer /*
461 1.1 bouyer * Save directory entry's inode number and
462 1.1 bouyer * reclen in ndp->ni_ufs area, and release
463 1.1 bouyer * directory buffer.
464 1.1 bouyer */
465 1.23 yamt foundino = fs2h32(ep->e2d_ino);
466 1.65 dholland results->ulr_reclen = fs2h16(ep->e2d_reclen);
467 1.1 bouyer goto found;
468 1.1 bouyer }
469 1.1 bouyer }
470 1.65 dholland prevoff = results->ulr_offset;
471 1.65 dholland results->ulr_offset += fs2h16(ep->e2d_reclen);
472 1.3 bouyer entryoffsetinblock += fs2h16(ep->e2d_reclen);
473 1.1 bouyer if (ep->e2d_ino)
474 1.65 dholland enduseful = results->ulr_offset;
475 1.1 bouyer }
476 1.1 bouyer /* notfound: */
477 1.1 bouyer /*
478 1.1 bouyer * If we started in the middle of the directory and failed
479 1.1 bouyer * to find our target, we must check the beginning as well.
480 1.1 bouyer */
481 1.1 bouyer if (numdirpasses == 2) {
482 1.1 bouyer numdirpasses--;
483 1.65 dholland results->ulr_offset = 0;
484 1.65 dholland endsearch = results->ulr_diroff;
485 1.1 bouyer goto searchloop;
486 1.1 bouyer }
487 1.1 bouyer if (bp != NULL)
488 1.53 ad brelse(bp, 0);
489 1.1 bouyer /*
490 1.1 bouyer * If creating, and at end of pathname and current
491 1.1 bouyer * directory has not been removed, then can consider
492 1.1 bouyer * allowing file to be created.
493 1.1 bouyer */
494 1.1 bouyer if ((nameiop == CREATE || nameiop == RENAME) &&
495 1.29 mycroft (flags & ISLASTCN) && dp->i_e2fs_nlink != 0) {
496 1.1 bouyer /*
497 1.1 bouyer * Access for write is interpreted as allowing
498 1.1 bouyer * creation of files in the directory.
499 1.1 bouyer */
500 1.54 pooka error = VOP_ACCESS(vdp, VWRITE, cred);
501 1.29 mycroft if (error)
502 1.1 bouyer return (error);
503 1.1 bouyer /*
504 1.1 bouyer * Return an indication of where the new directory
505 1.1 bouyer * entry should be put. If we didn't find a slot,
506 1.65 dholland * then set results->ulr_count to 0 indicating
507 1.1 bouyer * that the new slot belongs at the end of the
508 1.1 bouyer * directory. If we found a slot, then the new entry
509 1.65 dholland * can be put in the range from results->ulr_offset to
510 1.65 dholland * results->ulr_offset + results->ulr_count.
511 1.1 bouyer */
512 1.1 bouyer if (slotstatus == NONE) {
513 1.65 dholland results->ulr_offset = roundup(ext2fs_size(dp), dirblksiz);
514 1.65 dholland results->ulr_count = 0;
515 1.65 dholland enduseful = results->ulr_offset;
516 1.1 bouyer } else {
517 1.65 dholland results->ulr_offset = slotoffset;
518 1.65 dholland results->ulr_count = slotsize;
519 1.1 bouyer if (enduseful < slotoffset + slotsize)
520 1.1 bouyer enduseful = slotoffset + slotsize;
521 1.1 bouyer }
522 1.65 dholland results->ulr_endoff = roundup(enduseful, dirblksiz);
523 1.29 mycroft #if 0
524 1.1 bouyer dp->i_flag |= IN_CHANGE | IN_UPDATE;
525 1.29 mycroft #endif
526 1.1 bouyer /*
527 1.1 bouyer * We return with the directory locked, so that
528 1.1 bouyer * the parameters we set up above will still be
529 1.1 bouyer * valid if we actually decide to do a direnter().
530 1.1 bouyer * We return ni_vp == NULL to indicate that the entry
531 1.1 bouyer * does not currently exist; we leave a pointer to
532 1.1 bouyer * the (locked) directory inode in ndp->ni_dvp.
533 1.1 bouyer *
534 1.1 bouyer * NB - if the directory is unlocked, then this
535 1.1 bouyer * information cannot be used.
536 1.1 bouyer */
537 1.1 bouyer return (EJUSTRETURN);
538 1.1 bouyer }
539 1.1 bouyer /*
540 1.1 bouyer * Insert name into cache (as non-existent) if appropriate.
541 1.1 bouyer */
542 1.70 rmind if (nameiop != CREATE) {
543 1.72 dholland cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
544 1.72 dholland cnp->cn_flags);
545 1.70 rmind }
546 1.70 rmind return ENOENT;
547 1.1 bouyer
548 1.1 bouyer found:
549 1.29 mycroft if (numdirpasses == 2)
550 1.77 joerg namecache_count_pass2();
551 1.1 bouyer /*
552 1.1 bouyer * Check that directory length properly reflects presence
553 1.1 bouyer * of this entry.
554 1.1 bouyer */
555 1.65 dholland if (results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen) > ext2fs_size(dp)) {
556 1.65 dholland ufs_dirbad(dp, results->ulr_offset, "i_size too small");
557 1.31 ws error = ext2fs_setsize(dp,
558 1.65 dholland results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen));
559 1.31 ws if (error) {
560 1.53 ad brelse(bp, 0);
561 1.31 ws return (error);
562 1.31 ws }
563 1.1 bouyer dp->i_flag |= IN_CHANGE | IN_UPDATE;
564 1.34 kml uvm_vnp_setsize(vdp, ext2fs_size(dp));
565 1.1 bouyer }
566 1.53 ad brelse(bp, 0);
567 1.1 bouyer
568 1.1 bouyer /*
569 1.1 bouyer * Found component in pathname.
570 1.1 bouyer * If the final component of path name, save information
571 1.1 bouyer * in the cache as to where the entry was found.
572 1.1 bouyer */
573 1.1 bouyer if ((flags & ISLASTCN) && nameiop == LOOKUP)
574 1.65 dholland results->ulr_diroff = results->ulr_offset &~ (dirblksiz - 1);
575 1.1 bouyer
576 1.1 bouyer /*
577 1.1 bouyer * If deleting, and at end of pathname, return
578 1.1 bouyer * parameters which can be used to remove file.
579 1.47 chs * Lock the inode, being careful with ".".
580 1.1 bouyer */
581 1.1 bouyer if (nameiop == DELETE && (flags & ISLASTCN)) {
582 1.1 bouyer /*
583 1.65 dholland * Return pointer to current entry in results->ulr_offset,
584 1.1 bouyer * and distance past previous entry (if there
585 1.65 dholland * is a previous entry in this block) in results->ulr_count.
586 1.1 bouyer * Save directory inode pointer in ndp->ni_dvp for dirremove().
587 1.1 bouyer */
588 1.65 dholland if ((results->ulr_offset & (dirblksiz - 1)) == 0)
589 1.65 dholland results->ulr_count = 0;
590 1.1 bouyer else
591 1.65 dholland results->ulr_count = results->ulr_offset - prevoff;
592 1.69 hannken if (dp->i_number == foundino) {
593 1.69 hannken vref(vdp);
594 1.69 hannken tdp = vdp;
595 1.69 hannken } else {
596 1.75 hannken error = vcache_get(vdp->v_mount,
597 1.75 hannken &foundino, sizeof(foundino), &tdp);
598 1.69 hannken if (error)
599 1.69 hannken return (error);
600 1.69 hannken }
601 1.1 bouyer /*
602 1.68 elad * Write access to directory required to delete files.
603 1.68 elad */
604 1.68 elad if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0) {
605 1.75 hannken vrele(tdp);
606 1.68 elad return (error);
607 1.68 elad }
608 1.68 elad /*
609 1.1 bouyer * If directory is "sticky", then user must own
610 1.1 bouyer * the directory, or the file in it, else she
611 1.1 bouyer * may not delete it (unless she's root). This
612 1.1 bouyer * implements append-only directories.
613 1.1 bouyer */
614 1.68 elad if (dp->i_e2fs_mode & ISVTX) {
615 1.68 elad error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
616 1.68 elad tdp, vdp, genfs_can_sticky(cred, dp->i_uid,
617 1.68 elad VTOI(tdp)->i_uid));
618 1.68 elad if (error) {
619 1.75 hannken vrele(tdp);
620 1.68 elad return (EPERM);
621 1.68 elad }
622 1.68 elad }
623 1.1 bouyer *vpp = tdp;
624 1.1 bouyer return (0);
625 1.1 bouyer }
626 1.1 bouyer
627 1.1 bouyer /*
628 1.1 bouyer * If rewriting (RENAME), return the inode and the
629 1.1 bouyer * information required to rewrite the present directory
630 1.1 bouyer * Must get inode of directory entry to verify it's a
631 1.1 bouyer * regular file, or empty directory.
632 1.1 bouyer */
633 1.47 chs if (nameiop == RENAME && (flags & ISLASTCN)) {
634 1.54 pooka error = VOP_ACCESS(vdp, VWRITE, cred);
635 1.9 bouyer if (error)
636 1.1 bouyer return (error);
637 1.1 bouyer /*
638 1.1 bouyer * Careful about locking second inode.
639 1.1 bouyer * This can only occur if the target is ".".
640 1.1 bouyer */
641 1.23 yamt if (dp->i_number == foundino)
642 1.1 bouyer return (EISDIR);
643 1.75 hannken error = vcache_get(vdp->v_mount,
644 1.75 hannken &foundino, sizeof(foundino), &tdp);
645 1.9 bouyer if (error)
646 1.1 bouyer return (error);
647 1.1 bouyer *vpp = tdp;
648 1.1 bouyer return (0);
649 1.1 bouyer }
650 1.1 bouyer
651 1.75 hannken if (dp->i_number == foundino) {
652 1.61 pooka vref(vdp); /* we want ourself, ie "." */
653 1.1 bouyer *vpp = vdp;
654 1.1 bouyer } else {
655 1.75 hannken error = vcache_get(vdp->v_mount,
656 1.75 hannken &foundino, sizeof(foundino), &tdp);
657 1.29 mycroft if (error)
658 1.1 bouyer return (error);
659 1.1 bouyer *vpp = tdp;
660 1.1 bouyer }
661 1.1 bouyer
662 1.1 bouyer /*
663 1.1 bouyer * Insert name into cache if appropriate.
664 1.1 bouyer */
665 1.72 dholland cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
666 1.70 rmind return 0;
667 1.1 bouyer }
668 1.1 bouyer
669 1.1 bouyer /*
670 1.1 bouyer * Do consistency checking on a directory entry:
671 1.1 bouyer * record length must be multiple of 4
672 1.1 bouyer * entry must fit in rest of its dirblksize block
673 1.1 bouyer * record must be large enough to contain entry
674 1.36 christos * name is not longer than EXT2FS_MAXNAMLEN
675 1.1 bouyer * name must be as long as advertised, and null terminated
676 1.1 bouyer */
677 1.1 bouyer /*
678 1.1 bouyer * changed so that it confirms to ext2fs_check_dir_entry
679 1.1 bouyer */
680 1.1 bouyer static int
681 1.37 xtraeme ext2fs_dirbadentry(struct vnode *dp, struct ext2fs_direct *de,
682 1.37 xtraeme int entryoffsetinblock)
683 1.1 bouyer {
684 1.29 mycroft struct ufsmount *ump = VFSTOUFS(dp->v_mount);
685 1.29 mycroft int dirblksiz = ump->um_dirblksiz;
686 1.1 bouyer
687 1.33 christos const char *error_msg = NULL;
688 1.3 bouyer int reclen = fs2h16(de->e2d_reclen);
689 1.13 bouyer int namlen = de->e2d_namlen;
690 1.1 bouyer
691 1.3 bouyer if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
692 1.36 christos error_msg = "rec_len is smaller than minimal";
693 1.3 bouyer else if (reclen % 4 != 0)
694 1.36 christos error_msg = "rec_len % 4 != 0";
695 1.36 christos else if (namlen > EXT2FS_MAXNAMLEN)
696 1.36 christos error_msg = "namlen > EXT2FS_MAXNAMLEN";
697 1.3 bouyer else if (reclen < EXT2FS_DIRSIZ(namlen))
698 1.36 christos error_msg = "reclen is too small for name_len";
699 1.29 mycroft else if (entryoffsetinblock + reclen > dirblksiz)
700 1.36 christos error_msg = "directory entry across blocks";
701 1.9 bouyer else if (fs2h32(de->e2d_ino) >
702 1.9 bouyer VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
703 1.36 christos error_msg = "inode out of bounds";
704 1.1 bouyer
705 1.1 bouyer if (error_msg != NULL) {
706 1.1 bouyer printf( "bad directory entry: %s\n"
707 1.9 bouyer "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
708 1.9 bouyer error_msg, entryoffsetinblock,
709 1.9 bouyer (unsigned long) fs2h32(de->e2d_ino),
710 1.9 bouyer reclen, namlen);
711 1.1 bouyer panic("ext2fs_dirbadentry");
712 1.1 bouyer }
713 1.1 bouyer return error_msg == NULL ? 0 : 1;
714 1.1 bouyer }
715 1.1 bouyer
716 1.1 bouyer /*
717 1.1 bouyer * Write a directory entry after a call to namei, using the parameters
718 1.1 bouyer * that it left in nameidata. The argument ip is the inode which the new
719 1.1 bouyer * directory entry will refer to. Dvp is a pointer to the directory to
720 1.1 bouyer * be written, which was left locked by namei. Remaining parameters
721 1.65 dholland * (ulr_offset, ulr_count) indicate how the space for the new
722 1.1 bouyer * entry is to be obtained.
723 1.1 bouyer */
724 1.1 bouyer int
725 1.66 dholland ext2fs_direnter(struct inode *ip, struct vnode *dvp,
726 1.66 dholland const struct ufs_lookup_results *ulr,
727 1.66 dholland struct componentname *cnp)
728 1.1 bouyer {
729 1.15 augustss struct ext2fs_direct *ep, *nep;
730 1.15 augustss struct inode *dp;
731 1.1 bouyer struct buf *bp;
732 1.1 bouyer struct ext2fs_direct newdir;
733 1.1 bouyer struct iovec aiov;
734 1.1 bouyer struct uio auio;
735 1.1 bouyer u_int dsize;
736 1.1 bouyer int error, loc, newentrysize, spacefree;
737 1.1 bouyer char *dirbuf;
738 1.29 mycroft struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
739 1.29 mycroft int dirblksiz = ump->um_dirblksiz;
740 1.1 bouyer
741 1.1 bouyer dp = VTOI(dvp);
742 1.65 dholland
743 1.3 bouyer newdir.e2d_ino = h2fs32(ip->i_number);
744 1.13 bouyer newdir.e2d_namlen = cnp->cn_namelen;
745 1.13 bouyer if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
746 1.13 bouyer (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
747 1.24 fvdl newdir.e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
748 1.13 bouyer } else {
749 1.13 bouyer newdir.e2d_type = 0;
750 1.57 tsutsui }
751 1.7 perry memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
752 1.3 bouyer newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
753 1.65 dholland if (ulr->ulr_count == 0) {
754 1.1 bouyer /*
755 1.65 dholland * If ulr_count is 0, then namei could find no
756 1.65 dholland * space in the directory. Here, ulr_offset will
757 1.1 bouyer * be on a directory block boundary and we will write the
758 1.1 bouyer * new entry into a fresh block.
759 1.1 bouyer */
760 1.65 dholland if (ulr->ulr_offset & (dirblksiz - 1))
761 1.1 bouyer panic("ext2fs_direnter: newblk");
762 1.65 dholland auio.uio_offset = ulr->ulr_offset;
763 1.29 mycroft newdir.e2d_reclen = h2fs16(dirblksiz);
764 1.1 bouyer auio.uio_resid = newentrysize;
765 1.1 bouyer aiov.iov_len = newentrysize;
766 1.50 christos aiov.iov_base = (void *)&newdir;
767 1.1 bouyer auio.uio_iov = &aiov;
768 1.1 bouyer auio.uio_iovcnt = 1;
769 1.1 bouyer auio.uio_rw = UIO_WRITE;
770 1.40 yamt UIO_SETUP_SYSSPACE(&auio);
771 1.1 bouyer error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
772 1.29 mycroft if (dirblksiz > dvp->v_mount->mnt_stat.f_bsize)
773 1.1 bouyer /* XXX should grow with balloc() */
774 1.1 bouyer panic("ext2fs_direnter: frag size");
775 1.1 bouyer else if (!error) {
776 1.31 ws error = ext2fs_setsize(dp,
777 1.31 ws roundup(ext2fs_size(dp), dirblksiz));
778 1.31 ws if (error)
779 1.31 ws return (error);
780 1.1 bouyer dp->i_flag |= IN_CHANGE;
781 1.34 kml uvm_vnp_setsize(dvp, ext2fs_size(dp));
782 1.1 bouyer }
783 1.1 bouyer return (error);
784 1.1 bouyer }
785 1.1 bouyer
786 1.1 bouyer /*
787 1.65 dholland * If ulr_count is non-zero, then namei found space
788 1.65 dholland * for the new entry in the range ulr_offset to
789 1.65 dholland * ulr_offset + ulr_count in the directory.
790 1.1 bouyer * To use this space, we may have to compact the entries located
791 1.1 bouyer * there, by copying them together towards the beginning of the
792 1.1 bouyer * block, leaving the free space in one usable chunk at the end.
793 1.1 bouyer */
794 1.1 bouyer
795 1.1 bouyer /*
796 1.1 bouyer * Get the block containing the space for the new directory entry.
797 1.1 bouyer */
798 1.65 dholland if ((error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp)) != 0)
799 1.1 bouyer return (error);
800 1.1 bouyer /*
801 1.1 bouyer * Find space for the new entry. In the simple case, the entry at
802 1.1 bouyer * offset base will have the space. If it does not, then namei
803 1.65 dholland * arranged that compacting the region ulr_offset to
804 1.65 dholland * ulr_offset + ulr_count would yield the
805 1.1 bouyer * space.
806 1.1 bouyer */
807 1.1 bouyer ep = (struct ext2fs_direct *)dirbuf;
808 1.13 bouyer dsize = EXT2FS_DIRSIZ(ep->e2d_namlen);
809 1.3 bouyer spacefree = fs2h16(ep->e2d_reclen) - dsize;
810 1.65 dholland for (loc = fs2h16(ep->e2d_reclen); loc < ulr->ulr_count; ) {
811 1.1 bouyer nep = (struct ext2fs_direct *)(dirbuf + loc);
812 1.1 bouyer if (ep->e2d_ino) {
813 1.1 bouyer /* trim the existing slot */
814 1.3 bouyer ep->e2d_reclen = h2fs16(dsize);
815 1.1 bouyer ep = (struct ext2fs_direct *)((char *)ep + dsize);
816 1.1 bouyer } else {
817 1.1 bouyer /* overwrite; nothing there; header is ours */
818 1.1 bouyer spacefree += dsize;
819 1.1 bouyer }
820 1.13 bouyer dsize = EXT2FS_DIRSIZ(nep->e2d_namlen);
821 1.3 bouyer spacefree += fs2h16(nep->e2d_reclen) - dsize;
822 1.3 bouyer loc += fs2h16(nep->e2d_reclen);
823 1.50 christos memcpy((void *)ep, (void *)nep, dsize);
824 1.1 bouyer }
825 1.1 bouyer /*
826 1.1 bouyer * Update the pointer fields in the previous entry (if any),
827 1.1 bouyer * copy in the new entry, and write out the block.
828 1.1 bouyer */
829 1.1 bouyer if (ep->e2d_ino == 0) {
830 1.1 bouyer #ifdef DIAGNOSTIC
831 1.1 bouyer if (spacefree + dsize < newentrysize)
832 1.1 bouyer panic("ext2fs_direnter: compact1");
833 1.1 bouyer #endif
834 1.3 bouyer newdir.e2d_reclen = h2fs16(spacefree + dsize);
835 1.1 bouyer } else {
836 1.1 bouyer #ifdef DIAGNOSTIC
837 1.1 bouyer if (spacefree < newentrysize) {
838 1.1 bouyer printf("ext2fs_direnter: compact2 %u %u",
839 1.9 bouyer (u_int)spacefree, (u_int)newentrysize);
840 1.1 bouyer panic("ext2fs_direnter: compact2");
841 1.1 bouyer }
842 1.1 bouyer #endif
843 1.3 bouyer newdir.e2d_reclen = h2fs16(spacefree);
844 1.3 bouyer ep->e2d_reclen = h2fs16(dsize);
845 1.1 bouyer ep = (struct ext2fs_direct *)((char *)ep + dsize);
846 1.1 bouyer }
847 1.50 christos memcpy((void *)ep, (void *)&newdir, (u_int)newentrysize);
848 1.64 hannken error = VOP_BWRITE(bp->b_vp, bp);
849 1.1 bouyer dp->i_flag |= IN_CHANGE | IN_UPDATE;
850 1.65 dholland if (!error && ulr->ulr_endoff && ulr->ulr_endoff < ext2fs_size(dp))
851 1.65 dholland error = ext2fs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC,
852 1.55 pooka cnp->cn_cred);
853 1.1 bouyer return (error);
854 1.1 bouyer }
855 1.1 bouyer
856 1.1 bouyer /*
857 1.1 bouyer * Remove a directory entry after a call to namei, using
858 1.65 dholland * the auxiliary results it provided. The entry
859 1.65 dholland * ulr_offset contains the offset into the directory of the
860 1.65 dholland * entry to be eliminated. The ulr_count field contains the
861 1.1 bouyer * size of the previous record in the directory. If this
862 1.1 bouyer * is 0, the first entry is being deleted, so we need only
863 1.1 bouyer * zero the inode number to mark the entry as free. If the
864 1.1 bouyer * entry is not the first in the directory, we must reclaim
865 1.1 bouyer * the space of the now empty record by adding the record size
866 1.1 bouyer * to the size of the previous entry.
867 1.1 bouyer */
868 1.1 bouyer int
869 1.66 dholland ext2fs_dirremove(struct vnode *dvp, const struct ufs_lookup_results *ulr,
870 1.66 dholland struct componentname *cnp)
871 1.1 bouyer {
872 1.15 augustss struct inode *dp;
873 1.1 bouyer struct ext2fs_direct *ep;
874 1.1 bouyer struct buf *bp;
875 1.1 bouyer int error;
876 1.32 perry
877 1.1 bouyer dp = VTOI(dvp);
878 1.65 dholland
879 1.65 dholland if (ulr->ulr_count == 0) {
880 1.1 bouyer /*
881 1.1 bouyer * First entry in block: set d_ino to zero.
882 1.1 bouyer */
883 1.65 dholland error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset,
884 1.22 thorpej (void *)&ep, &bp);
885 1.1 bouyer if (error != 0)
886 1.1 bouyer return (error);
887 1.1 bouyer ep->e2d_ino = 0;
888 1.64 hannken error = VOP_BWRITE(bp->b_vp, bp);
889 1.1 bouyer dp->i_flag |= IN_CHANGE | IN_UPDATE;
890 1.1 bouyer return (error);
891 1.1 bouyer }
892 1.1 bouyer /*
893 1.1 bouyer * Collapse new free space into previous entry.
894 1.1 bouyer */
895 1.65 dholland error = ext2fs_blkatoff(dvp, (off_t)(ulr->ulr_offset - ulr->ulr_count),
896 1.22 thorpej (void *)&ep, &bp);
897 1.1 bouyer if (error != 0)
898 1.1 bouyer return (error);
899 1.65 dholland ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + ulr->ulr_reclen);
900 1.64 hannken error = VOP_BWRITE(bp->b_vp, bp);
901 1.1 bouyer dp->i_flag |= IN_CHANGE | IN_UPDATE;
902 1.1 bouyer return (error);
903 1.1 bouyer }
904 1.1 bouyer
905 1.1 bouyer /*
906 1.1 bouyer * Rewrite an existing directory entry to point at the inode
907 1.1 bouyer * supplied. The parameters describing the directory entry are
908 1.1 bouyer * set up by a call to namei.
909 1.1 bouyer */
910 1.1 bouyer int
911 1.66 dholland ext2fs_dirrewrite(struct inode *dp, const struct ufs_lookup_results *ulr,
912 1.66 dholland struct inode *ip, struct componentname *cnp)
913 1.1 bouyer {
914 1.1 bouyer struct buf *bp;
915 1.1 bouyer struct ext2fs_direct *ep;
916 1.1 bouyer struct vnode *vdp = ITOV(dp);
917 1.1 bouyer int error;
918 1.1 bouyer
919 1.65 dholland error = ext2fs_blkatoff(vdp, (off_t)ulr->ulr_offset, (void *)&ep, &bp);
920 1.1 bouyer if (error != 0)
921 1.1 bouyer return (error);
922 1.3 bouyer ep->e2d_ino = h2fs32(ip->i_number);
923 1.13 bouyer if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
924 1.13 bouyer (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
925 1.24 fvdl ep->e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
926 1.13 bouyer } else {
927 1.13 bouyer ep->e2d_type = 0;
928 1.13 bouyer }
929 1.64 hannken error = VOP_BWRITE(bp->b_vp, bp);
930 1.1 bouyer dp->i_flag |= IN_CHANGE | IN_UPDATE;
931 1.1 bouyer return (error);
932 1.1 bouyer }
933 1.1 bouyer
934 1.1 bouyer /*
935 1.1 bouyer * Check if a directory is empty or not.
936 1.1 bouyer * Inode supplied must be locked.
937 1.1 bouyer *
938 1.1 bouyer * Using a struct dirtemplate here is not precisely
939 1.1 bouyer * what we want, but better than using a struct ext2fs_direct.
940 1.1 bouyer *
941 1.1 bouyer * NB: does not handle corrupted directories.
942 1.1 bouyer */
943 1.1 bouyer int
944 1.44 elad ext2fs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
945 1.1 bouyer {
946 1.15 augustss off_t off;
947 1.1 bouyer struct ext2fs_dirtemplate dbuf;
948 1.15 augustss struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
949 1.6 mjacob int error, namlen;
950 1.6 mjacob size_t count;
951 1.32 perry
952 1.1 bouyer #define MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
953 1.1 bouyer
954 1.31 ws for (off = 0; off < ext2fs_size(ip); off += fs2h16(dp->e2d_reclen)) {
955 1.78 riastrad error = ufs_bufio(UIO_READ, ITOV(ip), (void *)dp, MINDIRSIZ,
956 1.78 riastrad off, IO_NODELOCKED, cred, &count, NULL);
957 1.1 bouyer /*
958 1.1 bouyer * Since we read MINDIRSIZ, residual must
959 1.1 bouyer * be 0 unless we're at end of file.
960 1.1 bouyer */
961 1.1 bouyer if (error || count != 0)
962 1.1 bouyer return (0);
963 1.1 bouyer /* avoid infinite loops */
964 1.1 bouyer if (dp->e2d_reclen == 0)
965 1.1 bouyer return (0);
966 1.1 bouyer /* skip empty entries */
967 1.1 bouyer if (dp->e2d_ino == 0)
968 1.1 bouyer continue;
969 1.1 bouyer /* accept only "." and ".." */
970 1.13 bouyer namlen = dp->e2d_namlen;
971 1.1 bouyer if (namlen > 2)
972 1.1 bouyer return (0);
973 1.1 bouyer if (dp->e2d_name[0] != '.')
974 1.1 bouyer return (0);
975 1.1 bouyer /*
976 1.1 bouyer * At this point namlen must be 1 or 2.
977 1.1 bouyer * 1 implies ".", 2 implies ".." if second
978 1.1 bouyer * char is also "."
979 1.1 bouyer */
980 1.1 bouyer if (namlen == 1)
981 1.1 bouyer continue;
982 1.3 bouyer if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
983 1.1 bouyer continue;
984 1.1 bouyer return (0);
985 1.1 bouyer }
986 1.1 bouyer return (1);
987 1.1 bouyer }
988