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