This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / sys / ufs / ufs_lookup.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
78ed81a3 33 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
34 * $Id$
15637ed4
RG
35 */
36
37#include "param.h"
78ed81a3 38#include "systm.h"
15637ed4
RG
39#include "namei.h"
40#include "buf.h"
41#include "file.h"
42#include "vnode.h"
43
44#include "quota.h"
45#include "inode.h"
46#include "dir.h"
47#include "fs.h"
48
49struct nchstats nchstats;
50#ifdef DIAGNOSTIC
51int dirchk = 1;
52#else
53int dirchk = 0;
54#endif
55
56/*
57 * Convert a component of a pathname into a pointer to a locked inode.
58 * This is a very central and rather complicated routine.
59 * If the file system is not maintained in a strict tree hierarchy,
60 * this can result in a deadlock situation (see comments in code below).
61 *
62 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
63 * whether the name is to be looked up, created, renamed, or deleted.
64 * When CREATE, RENAME, or DELETE is specified, information usable in
65 * creating, renaming, or deleting a directory entry may be calculated.
66 * If flag has LOCKPARENT or'ed into it and the target of the pathname
67 * exists, lookup returns both the target and its parent directory locked.
68 * When creating or renaming and LOCKPARENT is specified, the target may
69 * not be ".". When deleting and LOCKPARENT is specified, the target may
70 * be "."., but the caller must check to ensure it does an vrele and iput
71 * instead of two iputs.
72 *
73 * Overall outline of ufs_lookup:
74 *
75 * check accessibility of directory
76 * look for name in cache, if found, then if at end of path
77 * and deleting or creating, drop it, else return name
78 * search for name in directory, to found or notfound
79 * notfound:
80 * if creating, return locked directory, leaving info on available slots
81 * else return error
82 * found:
83 * if at end of path and deleting, return information to allow delete
84 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
85 * inode and return info to allow rewrite
86 * if not at end, add name to cache; if at end and neither creating
87 * nor deleting, add name to cache
88 *
89 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
90 */
91ufs_lookup(vdp, ndp, p)
92 register struct vnode *vdp;
93 register struct nameidata *ndp;
94 struct proc *p;
95{
96 register struct inode *dp; /* the directory we are searching */
97 register struct fs *fs; /* file system that directory is in */
98 struct buf *bp = 0; /* a buffer of directory entries */
99 register struct direct *ep; /* the current directory entry */
100 int entryoffsetinblock; /* offset of ep in bp's buffer */
101 enum {NONE, COMPACT, FOUND} slotstatus;
102 int slotoffset = -1; /* offset of area with free space */
103 int slotsize; /* size of area at slotoffset */
104 int slotfreespace; /* amount of space free in slot */
105 int slotneeded; /* size of the entry we're seeking */
106 int numdirpasses; /* strategy for directory search */
107 int endsearch; /* offset to end directory search */
108 int prevoff; /* ndp->ni_ufs.ufs_offset of previous entry */
109 struct inode *pdp; /* saved dp during symlink work */
110 struct inode *tdp; /* returned by iget */
111 off_t enduseful; /* pointer past last used dir slot */
112 int flag; /* LOOKUP, CREATE, RENAME, or DELETE */
113 int lockparent; /* 1 => lockparent flag is set */
114 int wantparent; /* 1 => wantparent or lockparent flag */
115 int error;
116
117 ndp->ni_dvp = vdp;
118 ndp->ni_vp = NULL;
119 dp = VTOI(vdp);
120 fs = dp->i_fs;
121 lockparent = ndp->ni_nameiop & LOCKPARENT;
122 flag = ndp->ni_nameiop & OPMASK;
123 wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
124
125 /*
126 * Check accessiblity of directory.
127 */
128 if ((dp->i_mode&IFMT) != IFDIR)
129 return (ENOTDIR);
130 if (error = ufs_access(vdp, VEXEC, ndp->ni_cred, p))
131 return (error);
132
133 /*
134 * We now have a segment name to search for, and a directory to search.
135 *
136 * Before tediously performing a linear scan of the directory,
137 * check the name cache to see if the directory/name pair
138 * we are looking for is known already.
139 */
140 if (error = cache_lookup(ndp)) {
141 int vpid; /* capability number of vnode */
142
143 if (error == ENOENT)
144 return (error);
145#ifdef PARANOID
78ed81a3 146 if (vdp == ndp->ni_rootdir && ndp->ni_isdotdot)
15637ed4
RG
147 panic("ufs_lookup: .. through root");
148#endif
149 /*
150 * Get the next vnode in the path.
151 * See comment below starting `Step through' for
152 * an explaination of the locking protocol.
153 */
154 pdp = dp;
155 dp = VTOI(ndp->ni_vp);
156 vdp = ndp->ni_vp;
157 vpid = vdp->v_id;
158 if (pdp == dp) {
159 VREF(vdp);
160 error = 0;
161 } else if (ndp->ni_isdotdot) {
162 IUNLOCK(pdp);
163 error = vget(vdp);
164 if (!error && lockparent && *ndp->ni_next == '\0')
165 ILOCK(pdp);
166 } else {
167 error = vget(vdp);
168 if (!lockparent || error || *ndp->ni_next != '\0')
169 IUNLOCK(pdp);
170 }
171 /*
172 * Check that the capability number did not change
173 * while we were waiting for the lock.
174 */
175 if (!error) {
176 if (vpid == vdp->v_id)
177 return (0);
178 iput(dp);
179 if (lockparent && pdp != dp && *ndp->ni_next == '\0')
180 IUNLOCK(pdp);
181 }
182 ILOCK(pdp);
183 dp = pdp;
184 vdp = ITOV(dp);
185 ndp->ni_vp = NULL;
186 }
187
188 /*
189 * Suppress search for slots unless creating
190 * file and at end of pathname, in which case
191 * we watch for a place to put the new file in
192 * case it doesn't already exist.
193 */
194 slotstatus = FOUND;
195 if ((flag == CREATE || flag == RENAME) && *ndp->ni_next == 0) {
196 slotstatus = NONE;
197 slotfreespace = 0;
198 slotneeded = ((sizeof (struct direct) - (MAXNAMLEN + 1)) +
199 ((ndp->ni_namelen + 1 + 3) &~ 3));
200 }
201
202 /*
203 * If there is cached information on a previous search of
204 * this directory, pick up where we last left off.
205 * We cache only lookups as these are the most common
206 * and have the greatest payoff. Caching CREATE has little
207 * benefit as it usually must search the entire directory
208 * to determine that the entry does not exist. Caching the
209 * location of the last DELETE or RENAME has not reduced
210 * profiling time and hence has been removed in the interest
211 * of simplicity.
212 */
213 if (flag != LOOKUP || dp->i_diroff == 0 || dp->i_diroff > dp->i_size) {
214 ndp->ni_ufs.ufs_offset = 0;
215 numdirpasses = 1;
216 } else {
217 ndp->ni_ufs.ufs_offset = dp->i_diroff;
218 entryoffsetinblock = blkoff(fs, ndp->ni_ufs.ufs_offset);
219 if (entryoffsetinblock != 0) {
220 if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset,
221 (char **)0, &bp))
222 return (error);
223 }
224 numdirpasses = 2;
225 nchstats.ncs_2passes++;
226 }
227 endsearch = roundup(dp->i_size, DIRBLKSIZ);
228 enduseful = 0;
229
230searchloop:
231 while (ndp->ni_ufs.ufs_offset < endsearch) {
232 /*
233 * If offset is on a block boundary,
234 * read the next directory block.
235 * Release previous if it exists.
236 */
237 if (blkoff(fs, ndp->ni_ufs.ufs_offset) == 0) {
238 if (bp != NULL)
239 brelse(bp);
240 if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset,
241 (char **)0, &bp))
242 return (error);
243 entryoffsetinblock = 0;
244 }
245 /*
246 * If still looking for a slot, and at a DIRBLKSIZE
247 * boundary, have to start looking for free space again.
248 */
249 if (slotstatus == NONE &&
250 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
251 slotoffset = -1;
252 slotfreespace = 0;
253 }
254 /*
255 * Get pointer to next entry.
256 * Full validation checks are slow, so we only check
257 * enough to insure forward progress through the
258 * directory. Complete checks can be run by patching
259 * "dirchk" to be true.
260 */
261 ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock);
262 if (ep->d_reclen == 0 ||
263 dirchk && dirbadentry(ep, entryoffsetinblock)) {
264 int i;
265
266 dirbad(dp, ndp->ni_ufs.ufs_offset, "mangled entry");
267 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
268 ndp->ni_ufs.ufs_offset += i;
269 entryoffsetinblock += i;
270 continue;
271 }
272
273 /*
274 * If an appropriate sized slot has not yet been found,
275 * check to see if one is available. Also accumulate space
276 * in the current block so that we can determine if
277 * compaction is viable.
278 */
279 if (slotstatus != FOUND) {
280 int size = ep->d_reclen;
281
282 if (ep->d_ino != 0)
283 size -= DIRSIZ(ep);
284 if (size > 0) {
285 if (size >= slotneeded) {
286 slotstatus = FOUND;
287 slotoffset = ndp->ni_ufs.ufs_offset;
288 slotsize = ep->d_reclen;
289 } else if (slotstatus == NONE) {
290 slotfreespace += size;
291 if (slotoffset == -1)
292 slotoffset =
293 ndp->ni_ufs.ufs_offset;
294 if (slotfreespace >= slotneeded) {
295 slotstatus = COMPACT;
296 slotsize =
297 ndp->ni_ufs.ufs_offset +
298 ep->d_reclen - slotoffset;
299 }
300 }
301 }
302 }
303
304 /*
305 * Check for a name match.
306 */
307 if (ep->d_ino) {
308 if (ep->d_namlen == ndp->ni_namelen &&
309 !bcmp(ndp->ni_ptr, ep->d_name,
310 (unsigned)ep->d_namlen)) {
311 /*
312 * Save directory entry's inode number and
313 * reclen in ndp->ni_ufs area, and release
314 * directory buffer.
315 */
316 ndp->ni_ufs.ufs_ino = ep->d_ino;
317 ndp->ni_ufs.ufs_reclen = ep->d_reclen;
318 goto found;
319 }
320 }
321 prevoff = ndp->ni_ufs.ufs_offset;
322 ndp->ni_ufs.ufs_offset += ep->d_reclen;
323 entryoffsetinblock += ep->d_reclen;
324 if (ep->d_ino)
325 enduseful = ndp->ni_ufs.ufs_offset;
326 }
327/* notfound: */
328 /*
329 * If we started in the middle of the directory and failed
330 * to find our target, we must check the beginning as well.
331 */
332 if (numdirpasses == 2) {
333 numdirpasses--;
334 ndp->ni_ufs.ufs_offset = 0;
335 endsearch = dp->i_diroff;
336 goto searchloop;
337 }
338 if (bp != NULL)
339 brelse(bp);
340 /*
341 * If creating, and at end of pathname and current
342 * directory has not been removed, then can consider
343 * allowing file to be created.
344 */
345 if ((flag == CREATE || flag == RENAME) &&
346 *ndp->ni_next == 0 && dp->i_nlink != 0) {
347 /*
348 * Access for write is interpreted as allowing
349 * creation of files in the directory.
350 */
351 if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
352 return (error);
353 /*
354 * Return an indication of where the new directory
355 * entry should be put. If we didn't find a slot,
356 * then set ndp->ni_ufs.ufs_count to 0 indicating
357 * that the new slot belongs at the end of the
358 * directory. If we found a slot, then the new entry
359 * can be put in the range from ndp->ni_ufs.ufs_offset
360 * to ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count.
361 */
362 if (slotstatus == NONE) {
363 ndp->ni_ufs.ufs_offset = roundup(dp->i_size, DIRBLKSIZ);
364 ndp->ni_ufs.ufs_count = 0;
365 enduseful = ndp->ni_ufs.ufs_offset;
366 } else {
367 ndp->ni_ufs.ufs_offset = slotoffset;
368 ndp->ni_ufs.ufs_count = slotsize;
369 if (enduseful < slotoffset + slotsize)
370 enduseful = slotoffset + slotsize;
371 }
372 ndp->ni_ufs.ufs_endoff = roundup(enduseful, DIRBLKSIZ);
373 dp->i_flag |= IUPD|ICHG;
374 /*
375 * We return with the directory locked, so that
376 * the parameters we set up above will still be
377 * valid if we actually decide to do a direnter().
378 * We return ni_vp == NULL to indicate that the entry
379 * does not currently exist; we leave a pointer to
380 * the (locked) directory inode in ndp->ni_dvp.
381 * The pathname buffer is saved so that the name
382 * can be obtained later.
383 *
384 * NB - if the directory is unlocked, then this
385 * information cannot be used.
386 */
387 ndp->ni_nameiop |= SAVENAME;
388 if (!lockparent)
389 IUNLOCK(dp);
390 }
391 /*
392 * Insert name into cache (as non-existent) if appropriate.
393 */
394 if (ndp->ni_makeentry && flag != CREATE)
395 cache_enter(ndp);
396 return (ENOENT);
397
398found:
399 if (numdirpasses == 2)
400 nchstats.ncs_pass2++;
401 /*
402 * Check that directory length properly reflects presence
403 * of this entry.
404 */
405 if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) {
406 dirbad(dp, ndp->ni_ufs.ufs_offset, "i_size too small");
407 dp->i_size = entryoffsetinblock + DIRSIZ(ep);
408 dp->i_flag |= IUPD|ICHG;
409 }
410
411 brelse(bp);
412
413 /*
414 * Found component in pathname.
415 * If the final component of path name, save information
416 * in the cache as to where the entry was found.
417 */
418 if (*ndp->ni_next == '\0' && flag == LOOKUP)
419 dp->i_diroff = ndp->ni_ufs.ufs_offset &~ (DIRBLKSIZ - 1);
420
421 /*
422 * If deleting, and at end of pathname, return
423 * parameters which can be used to remove file.
424 * If the wantparent flag isn't set, we return only
425 * the directory (in ndp->ni_dvp), otherwise we go
426 * on and lock the inode, being careful with ".".
427 */
428 if (flag == DELETE && *ndp->ni_next == 0) {
429 /*
430 * Write access to directory required to delete files.
431 */
432 if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
433 return (error);
434 /*
435 * Return pointer to current entry in ndp->ni_ufs.ufs_offset,
436 * and distance past previous entry (if there
437 * is a previous entry in this block) in ndp->ni_ufs.ufs_count.
438 * Save directory inode pointer in ndp->ni_dvp for dirremove().
439 */
440 if ((ndp->ni_ufs.ufs_offset&(DIRBLKSIZ-1)) == 0)
441 ndp->ni_ufs.ufs_count = 0;
442 else
443 ndp->ni_ufs.ufs_count = ndp->ni_ufs.ufs_offset - prevoff;
444 if (dp->i_number == ndp->ni_ufs.ufs_ino) {
445 VREF(vdp);
446 ndp->ni_vp = vdp;
447 return (0);
448 }
449 if (error = iget(dp, ndp->ni_ufs.ufs_ino, &tdp))
450 return (error);
451 /*
452 * If directory is "sticky", then user must own
453 * the directory, or the file in it, else she
454 * may not delete it (unless she's root). This
455 * implements append-only directories.
456 */
457 if ((dp->i_mode & ISVTX) &&
458 ndp->ni_cred->cr_uid != 0 &&
459 ndp->ni_cred->cr_uid != dp->i_uid &&
460 tdp->i_uid != ndp->ni_cred->cr_uid) {
461 iput(tdp);
462 return (EPERM);
463 }
464 ndp->ni_vp = ITOV(tdp);
465 if (!lockparent)
466 IUNLOCK(dp);
467 return (0);
468 }
469
470 /*
471 * If rewriting (RENAME), return the inode and the
472 * information required to rewrite the present directory
473 * Must get inode of directory entry to verify it's a
474 * regular file, or empty directory.
475 */
476 if (flag == RENAME && wantparent && *ndp->ni_next == 0) {
477 if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
478 return (error);
479 /*
480 * Careful about locking second inode.
481 * This can only occur if the target is ".".
482 */
483 if (dp->i_number == ndp->ni_ufs.ufs_ino)
484 return (EISDIR);
485 if (error = iget(dp, ndp->ni_ufs.ufs_ino, &tdp))
486 return (error);
487 ndp->ni_vp = ITOV(tdp);
488 ndp->ni_nameiop |= SAVENAME;
489 if (!lockparent)
490 IUNLOCK(dp);
491 return (0);
492 }
493
494 /*
495 * Step through the translation in the name. We do not `iput' the
496 * directory because we may need it again if a symbolic link
497 * is relative to the current directory. Instead we save it
498 * unlocked as "pdp". We must get the target inode before unlocking
499 * the directory to insure that the inode will not be removed
500 * before we get it. We prevent deadlock by always fetching
501 * inodes from the root, moving down the directory tree. Thus
502 * when following backward pointers ".." we must unlock the
503 * parent directory before getting the requested directory.
504 * There is a potential race condition here if both the current
505 * and parent directories are removed before the `iget' for the
506 * inode associated with ".." returns. We hope that this occurs
507 * infrequently since we cannot avoid this race condition without
508 * implementing a sophisticated deadlock detection algorithm.
509 * Note also that this simple deadlock detection scheme will not
510 * work if the file system has any hard links other than ".."
511 * that point backwards in the directory structure.
512 */
513 pdp = dp;
514 if (ndp->ni_isdotdot) {
515 IUNLOCK(pdp); /* race to get the inode */
516 if (error = iget(dp, ndp->ni_ufs.ufs_ino, &tdp)) {
517 ILOCK(pdp);
518 return (error);
519 }
520 if (lockparent && *ndp->ni_next == '\0')
521 ILOCK(pdp);
522 ndp->ni_vp = ITOV(tdp);
523 } else if (dp->i_number == ndp->ni_ufs.ufs_ino) {
524 VREF(vdp); /* we want ourself, ie "." */
525 ndp->ni_vp = vdp;
526 } else {
527 if (error = iget(dp, ndp->ni_ufs.ufs_ino, &tdp))
528 return (error);
529 if (!lockparent || *ndp->ni_next != '\0')
530 IUNLOCK(pdp);
531 ndp->ni_vp = ITOV(tdp);
532 }
533
534 /*
535 * Insert name into cache if appropriate.
536 */
537 if (ndp->ni_makeentry)
538 cache_enter(ndp);
539 return (0);
540}
541
542
543dirbad(ip, offset, how)
544 struct inode *ip;
545 off_t offset;
546 char *how;
547{
548
549 printf("%s: bad dir ino %d at offset %d: %s\n",
550 ip->i_fs->fs_fsmnt, ip->i_number, offset, how);
551 if (ip->i_fs->fs_ronly == 0)
552 panic("bad dir");
553}
554
555/*
556 * Do consistency checking on a directory entry:
557 * record length must be multiple of 4
558 * entry must fit in rest of its DIRBLKSIZ block
559 * record must be large enough to contain entry
560 * name is not longer than MAXNAMLEN
561 * name must be as long as advertised, and null terminated
562 */
563dirbadentry(ep, entryoffsetinblock)
564 register struct direct *ep;
565 int entryoffsetinblock;
566{
567 register int i;
568
569 if ((ep->d_reclen & 0x3) != 0 ||
570 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
571 ep->d_reclen < DIRSIZ(ep) || ep->d_namlen > MAXNAMLEN)
572 return (1);
573 for (i = 0; i < ep->d_namlen; i++)
574 if (ep->d_name[i] == '\0')
575 return (1);
576 return (ep->d_name[i]);
577}
578
579/*
580 * Write a directory entry after a call to namei, using the parameters
581 * that it left in nameidata. The argument ip is the inode which the new
582 * directory entry will refer to. The nameidata field ndp->ni_dvp is a
583 * pointer to the directory to be written, which was left locked by namei.
584 * Remaining parameters (ndp->ni_ufs.ufs_offset, ndp->ni_ufs.ufs_count)
585 * indicate how the space for the new entry is to be obtained.
586 */
587direnter(ip, ndp)
588 struct inode *ip;
589 register struct nameidata *ndp;
590{
591 register struct direct *ep, *nep;
592 register struct inode *dp = VTOI(ndp->ni_dvp);
593 struct buf *bp;
594 int loc, spacefree, error = 0;
595 u_int dsize;
596 int newentrysize;
597 char *dirbuf;
598 struct uio auio;
599 struct iovec aiov;
600 struct direct newdir;
601
602#ifdef DIAGNOSTIC
603 if ((ndp->ni_nameiop & SAVENAME) == 0)
604 panic("direnter: missing name");
605#endif
606 newdir.d_ino = ip->i_number;
607 newdir.d_namlen = ndp->ni_namelen;
608 bcopy(ndp->ni_ptr, newdir.d_name, (unsigned)ndp->ni_namelen + 1);
609 newentrysize = DIRSIZ(&newdir);
610 if (ndp->ni_ufs.ufs_count == 0) {
611 /*
612 * If ndp->ni_ufs.ufs_count is 0, then namei could find no
613 * space in the directory. Here, ndp->ni_ufs.ufs_offset will
614 * be on a directory block boundary and we will write the
615 * new entry into a fresh block.
616 */
617 if (ndp->ni_ufs.ufs_offset & (DIRBLKSIZ - 1))
618 panic("wdir: newblk");
619 auio.uio_offset = ndp->ni_ufs.ufs_offset;
620 newdir.d_reclen = DIRBLKSIZ;
621 auio.uio_resid = newentrysize;
622 aiov.iov_len = newentrysize;
623 aiov.iov_base = (caddr_t)&newdir;
624 auio.uio_iov = &aiov;
625 auio.uio_iovcnt = 1;
626 auio.uio_rw = UIO_WRITE;
627 auio.uio_segflg = UIO_SYSSPACE;
628 auio.uio_procp = (struct proc *)0;
629 error = ufs_write(ndp->ni_dvp, &auio, IO_SYNC, ndp->ni_cred);
630 if (DIRBLKSIZ > dp->i_fs->fs_fsize) {
631 panic("wdir: blksize"); /* XXX - should grow w/balloc */
632 } else if (!error) {
633 dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
634 dp->i_flag |= ICHG;
635 }
636 return (error);
637 }
638
639 /*
640 * If ndp->ni_ufs.ufs_count is non-zero, then namei found space
641 * for the new entry in the range ndp->ni_ufs.ufs_offset to
642 * ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count in the directory.
643 * To use this space, we may have to compact the entries located
644 * there, by copying them together towards the beginning of the
645 * block, leaving the free space in one usable chunk at the end.
646 */
647
648 /*
649 * Increase size of directory if entry eats into new space.
650 * This should never push the size past a new multiple of
651 * DIRBLKSIZE.
652 *
653 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
654 */
655 if (ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count > dp->i_size)
656 dp->i_size = ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count;
657 /*
658 * Get the block containing the space for the new directory entry.
659 */
660 if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset, (char **)&dirbuf, &bp))
661 return (error);
662 /*
663 * Find space for the new entry. In the simple case, the entry at
664 * offset base will have the space. If it does not, then namei
665 * arranged that compacting the region ndp->ni_ufs.ufs_offset to
666 * ndp->ni_ufs.ufs_offset + ndp->ni_ufs.ufs_count would yield the
667 * space.
668 */
669 ep = (struct direct *)dirbuf;
670 dsize = DIRSIZ(ep);
671 spacefree = ep->d_reclen - dsize;
672 for (loc = ep->d_reclen; loc < ndp->ni_ufs.ufs_count; ) {
673 nep = (struct direct *)(dirbuf + loc);
674 if (ep->d_ino) {
675 /* trim the existing slot */
676 ep->d_reclen = dsize;
677 ep = (struct direct *)((char *)ep + dsize);
678 } else {
679 /* overwrite; nothing there; header is ours */
680 spacefree += dsize;
681 }
682 dsize = DIRSIZ(nep);
683 spacefree += nep->d_reclen - dsize;
684 loc += nep->d_reclen;
685 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
686 }
687 /*
688 * Update the pointer fields in the previous entry (if any),
689 * copy in the new entry, and write out the block.
690 */
691 if (ep->d_ino == 0) {
692 if (spacefree + dsize < newentrysize)
693 panic("wdir: compact1");
694 newdir.d_reclen = spacefree + dsize;
695 } else {
696 if (spacefree < newentrysize)
697 panic("wdir: compact2");
698 newdir.d_reclen = spacefree;
699 ep->d_reclen = dsize;
700 ep = (struct direct *)((char *)ep + dsize);
701 }
702 bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize);
703 error = bwrite(bp);
704 dp->i_flag |= IUPD|ICHG;
705 if (!error && ndp->ni_ufs.ufs_endoff &&
706 ndp->ni_ufs.ufs_endoff < dp->i_size)
707 error = itrunc(dp, (u_long)ndp->ni_ufs.ufs_endoff, IO_SYNC);
708 return (error);
709}
710
711/*
712 * Remove a directory entry after a call to namei, using
713 * the parameters which it left in nameidata. The entry
714 * ni_ufs.ufs_offset contains the offset into the directory of the
715 * entry to be eliminated. The ni_ufs.ufs_count field contains the
716 * size of the previous record in the directory. If this
717 * is 0, the first entry is being deleted, so we need only
718 * zero the inode number to mark the entry as free. If the
719 * entry is not the first in the directory, we must reclaim
720 * the space of the now empty record by adding the record size
721 * to the size of the previous entry.
722 */
723dirremove(ndp)
724 register struct nameidata *ndp;
725{
726 register struct inode *dp = VTOI(ndp->ni_dvp);
727 struct direct *ep;
728 struct buf *bp;
729 int error;
730
731 if (ndp->ni_ufs.ufs_count == 0) {
732 /*
733 * First entry in block: set d_ino to zero.
734 */
735 error = blkatoff(dp, ndp->ni_ufs.ufs_offset, (char **)&ep, &bp);
736 if (error)
737 return (error);
738 ep->d_ino = 0;
739 error = bwrite(bp);
740 dp->i_flag |= IUPD|ICHG;
741 return (error);
742 }
743 /*
744 * Collapse new free space into previous entry.
745 */
746 if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset - ndp->ni_ufs.ufs_count,
747 (char **)&ep, &bp)) {
748 return (error);
749 }
750 ep->d_reclen += ndp->ni_ufs.ufs_reclen;
751 error = bwrite(bp);
752 dp->i_flag |= IUPD|ICHG;
753 return (error);
754}
755
756/*
757 * Rewrite an existing directory entry to point at the inode
758 * supplied. The parameters describing the directory entry are
759 * set up by a call to namei.
760 */
761dirrewrite(dp, ip, ndp)
762 struct inode *dp, *ip;
763 struct nameidata *ndp;
764{
765 struct direct *ep;
766 struct buf *bp;
767 int error;
768
769 if (error = blkatoff(dp, ndp->ni_ufs.ufs_offset, (char **)&ep, &bp))
770 return (error);
771 ep->d_ino = ip->i_number;
772 error = bwrite(bp);
773 dp->i_flag |= IUPD|ICHG;
774 return (error);
775}
776
777/*
778 * Return buffer with contents of block "offset"
779 * from the beginning of directory "ip". If "res"
780 * is non-zero, fill it in with a pointer to the
781 * remaining space in the directory.
782 */
783blkatoff(ip, offset, res, bpp)
784 struct inode *ip;
785 off_t offset;
786 char **res;
787 struct buf **bpp;
788{
789 register struct fs *fs = ip->i_fs;
790 daddr_t lbn = lblkno(fs, offset);
791 int bsize = blksize(fs, ip, lbn);
792 struct buf *bp;
793 daddr_t bn;
794 int error;
795
796 *bpp = 0;
797 if (error = bread(ITOV(ip), lbn, bsize, NOCRED, &bp)) {
798 brelse(bp);
799 return (error);
800 }
801 if (res)
802 *res = bp->b_un.b_addr + blkoff(fs, offset);
803 *bpp = bp;
804 return (0);
805}
806
807/*
808 * Check if a directory is empty or not.
809 * Inode supplied must be locked.
810 *
811 * Using a struct dirtemplate here is not precisely
812 * what we want, but better than using a struct direct.
813 *
814 * NB: does not handle corrupted directories.
815 */
816dirempty(ip, parentino, cred)
817 register struct inode *ip;
818 ino_t parentino;
819 struct ucred *cred;
820{
821 register off_t off;
822 struct dirtemplate dbuf;
823 register struct direct *dp = (struct direct *)&dbuf;
824 int error, count;
825#define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
826
827 for (off = 0; off < ip->i_size; off += dp->d_reclen) {
828 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
829 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
830 /*
831 * Since we read MINDIRSIZ, residual must
832 * be 0 unless we're at end of file.
833 */
834 if (error || count != 0)
835 return (0);
836 /* avoid infinite loops */
837 if (dp->d_reclen == 0)
838 return (0);
839 /* skip empty entries */
840 if (dp->d_ino == 0)
841 continue;
842 /* accept only "." and ".." */
843 if (dp->d_namlen > 2)
844 return (0);
845 if (dp->d_name[0] != '.')
846 return (0);
847 /*
848 * At this point d_namlen must be 1 or 2.
849 * 1 implies ".", 2 implies ".." if second
850 * char is also "."
851 */
852 if (dp->d_namlen == 1)
853 continue;
854 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
855 continue;
856 return (0);
857 }
858 return (1);
859}
860
861/*
862 * Check if source directory is in the path of the target directory.
863 * Target is supplied locked, source is unlocked.
864 * The target is always iput() before returning.
865 */
866checkpath(source, target, cred)
867 struct inode *source, *target;
868 struct ucred *cred;
869{
870 struct dirtemplate dirbuf;
871 struct inode *ip;
872 int error = 0;
873
874 ip = target;
875 if (ip->i_number == source->i_number) {
876 error = EEXIST;
877 goto out;
878 }
879 if (ip->i_number == ROOTINO)
880 goto out;
881
882 for (;;) {
883 if ((ip->i_mode&IFMT) != IFDIR) {
884 error = ENOTDIR;
885 break;
886 }
887 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)&dirbuf,
888 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
889 IO_NODELOCKED, cred, (int *)0, (struct proc *)0);
890 if (error != 0)
891 break;
892 if (dirbuf.dotdot_namlen != 2 ||
893 dirbuf.dotdot_name[0] != '.' ||
894 dirbuf.dotdot_name[1] != '.') {
895 error = ENOTDIR;
896 break;
897 }
898 if (dirbuf.dotdot_ino == source->i_number) {
899 error = EINVAL;
900 break;
901 }
902 if (dirbuf.dotdot_ino == ROOTINO)
903 break;
904 iput(ip);
905 if (error = iget(ip, dirbuf.dotdot_ino, &ip))
906 break;
907 }
908
909out:
910 if (error == ENOTDIR)
911 printf("checkpath: .. not a directory\n");
912 if (ip != NULL)
913 iput(ip);
914 return (error);
915}