diff options
Diffstat (limited to 'ext2fs/dir.c')
-rw-r--r-- | ext2fs/dir.c | 469 |
1 files changed, 289 insertions, 180 deletions
diff --git a/ext2fs/dir.c b/ext2fs/dir.c index 36fcfd58..d70dbf32 100644 --- a/ext2fs/dir.c +++ b/ext2fs/dir.c @@ -1,8 +1,9 @@ /* Directory management routines - Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc. + Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2007 + Free Software Foundation, Inc. - Converted for ext2fs by Miles Bader <miles@gnu.ai.mit.edu> + Converted for ext2fs by Miles Bader <miles@gnu.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -72,39 +73,66 @@ struct dirstat /* Index of this directory block. */ int idx; - - /* For stat COMPRESS, this is the address (inside mapbuf) + + /* For stat COMPRESS, this is the address (inside mapbuf) of the first direct in the directory block to be compressed. */ /* For stat HERE_TIS, SHRINK, and TAKE, this is the entry referenced. */ - struct ext2_dir_entry *entry; + struct ext2_dir_entry_2 *entry; /* For stat HERE_TIS, type REMOVE, this is the address of the immediately previous direct in this directory block, or zero if this is the first. */ - struct ext2_dir_entry *preventry; + struct ext2_dir_entry_2 *preventry; /* For stat COMPRESS, this is the number of bytes needed to be copied in order to undertake the compression. */ size_t nbytes; }; -size_t diskfs_dirstat_size = sizeof (struct dirstat); +const size_t diskfs_dirstat_size = sizeof (struct dirstat); /* Initialize DS such that diskfs_drop_dirstat will ignore it. */ -void +void diskfs_null_dirstat (struct dirstat *ds) { ds->type = LOOKUP; } -static error_t -dirscanblock (vm_address_t blockoff, struct node *dp, int idx, char *name, - int namelen, enum lookup_type type, struct dirstat *ds, - ino_t *inum); +static error_t +dirscanblock (vm_address_t blockoff, struct node *dp, int idx, + const char *name, int namelen, enum lookup_type type, + struct dirstat *ds, ino_t *inum); + +#if 0 /* XXX unused for now */ +static const unsigned char ext2_file_type[EXT2_FT_MAX] = +{ + [EXT2_FT_UNKNOWN] = DT_UNKNOWN, + [EXT2_FT_REG_FILE] = DT_REG, + [EXT2_FT_DIR] = DT_DIR, + [EXT2_FT_CHRDEV] = DT_CHR, + [EXT2_FT_BLKDEV] = DT_BLK, + [EXT2_FT_FIFO] = DT_FIFO, + [EXT2_FT_SOCK] = DT_SOCK, + [EXT2_FT_SYMLINK] = DT_LNK, +}; + +static const unsigned char file_type_ext2[] = +{ + [DT_UNKNOWN] = EXT2_FT_UNKNOWN, + [DT_REG] = EXT2_FT_REG_FILE, + [DT_DIR] = EXT2_FT_DIR, + [DT_CHR] = EXT2_FT_CHRDEV, + [DT_BLK] = EXT2_FT_BLKDEV, + [DT_FIFO] = EXT2_FT_FIFO, + [DT_SOCK] = EXT2_FT_SOCK, + [DT_LNK] = EXT2_FT_SYMLINK, +}; +#endif + /* Implement the diskfs_lookup from the diskfs library. See <hurd/diskfs.h> for the interface specification. */ error_t -diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, +diskfs_lookup_hard (struct node *dp, const char *name, enum lookup_type type, struct node **npp, struct dirstat *ds, struct protid *cred) { error_t err; @@ -119,8 +147,9 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, vm_address_t buf = 0; vm_size_t buflen = 0; int blockaddr; - int idx; - + int idx, lastidx; + int looped; + if ((type == REMOVE) || (type == RENAME)) assert (npp); @@ -129,12 +158,16 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, spec_dotdot = type & SPEC_DOTDOT; type &= ~SPEC_DOTDOT; - + namelen = strlen (name); if (namelen > EXT2_NAME_LEN) - return ENAMETOOLONG; - + { + if (ds) + diskfs_null_dirstat (ds); + return ENAMETOOLONG; + } + try_again: if (ds) { @@ -144,7 +177,7 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, } if (buf) { - vm_deallocate (mach_task_self (), buf, buflen); + munmap ((caddr_t) buf, buflen); buf = 0; } if (ds && (type == CREATE || type == RENAME)) @@ -152,6 +185,10 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, /* Map in the directory contents. */ memobj = diskfs_get_filemap (dp, prot); + + if (memobj == MACH_PORT_NULL) + return errno; + buf = 0; /* We allow extra space in case we have to do an EXTEND. */ buflen = round_page (dp->dn_stat.st_size + DIRBLKSIZ); @@ -160,26 +197,45 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, mach_port_deallocate (mach_task_self (), memobj); inum = 0; - - if (!diskfs_check_readonly ()) - dp->dn_set_atime = 1; - - for (blockaddr = buf, idx = 0; - blockaddr - buf < dp->dn_stat.st_size; - blockaddr += DIRBLKSIZ, idx++) + + diskfs_set_node_atime (dp); + + /* Start the lookup at DP->dn->dir_idx. */ + idx = dp->dn->dir_idx; + if (idx * DIRBLKSIZ > dp->dn_stat.st_size) + idx = 0; /* just in case */ + blockaddr = buf + idx * DIRBLKSIZ; + looped = (idx == 0); + lastidx = idx; + if (lastidx == 0) + lastidx = dp->dn_stat.st_size / DIRBLKSIZ; + + while (!looped || idx < lastidx) { err = dirscanblock (blockaddr, dp, idx, name, namelen, type, ds, &inum); if (!err) - break; + { + dp->dn->dir_idx = idx; + break; + } if (err != ENOENT) { - vm_deallocate (mach_task_self (), buf, buflen); + munmap ((caddr_t) buf, buflen); return err; } + + blockaddr += DIRBLKSIZ; + idx++; + if (blockaddr - buf >= dp->dn_stat.st_size && !looped) + { + /* We've gotten to the end; start back at the beginning */ + looped = 1; + blockaddr = buf; + idx = 0; + } } - if (!diskfs_check_readonly ()) - dp->dn_set_atime = 1; + diskfs_set_node_atime (dp); if (diskfs_synchronous) diskfs_node_update (dp, 1); @@ -203,7 +259,7 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, goto out; } } - + /* We are looking up .. */ /* Check to see if this is the root of the filesystem. */ else if (dp->cache_id == 2) @@ -211,7 +267,7 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, err = EAGAIN; goto out; } - + /* We can't just do diskfs_cached_lookup, because we would then deadlock. So we do this. Ick. */ else if (retry_dotdot) @@ -245,11 +301,11 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, retry_dotdot = inum; goto try_again; } - + /* Here below are the spec dotdot cases. */ else if (type == RENAME || type == REMOVE) np = ifind (inum); - + else if (type == LOOKUP) { diskfs_nput (dp); @@ -260,13 +316,13 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, else assert (0); } - + if ((type == CREATE || type == RENAME) && !inum && ds && ds->stat == LOOKING) { /* We didn't find any room, so mark ds to extend the dir */ ds->type = CREATE; ds->stat = EXTEND; - ds->idx = idx; + ds->idx = dp->dn_stat.st_size / DIRBLKSIZ; } /* Return to the user; if we can't, release the reference @@ -277,7 +333,7 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, || !ds || ds->type == LOOKUP) { - vm_deallocate (mach_task_self (), buf, buflen); + munmap ((caddr_t) buf, buflen); if (ds) ds->type = LOOKUP; /* set to be ignored by drop_dirstat */ } @@ -286,7 +342,7 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, ds->mapbuf = buf; ds->mapextent = buflen; } - + if (np) { assert (npp); @@ -320,20 +376,20 @@ diskfs_lookup_hard (struct node *dp, char *name, enum lookup_type type, diskfs_lookup. If found, set *INUM to the inode number, else return ENOENT. */ static error_t -dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, - int namelen, enum lookup_type type, +dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, + const char *name, int namelen, enum lookup_type type, struct dirstat *ds, ino_t *inum) { int nfree = 0; int needed = 0; vm_address_t currentoff, prevoff; - struct ext2_dir_entry *entry = 0; + struct ext2_dir_entry_2 *entry = 0; int nentries = 0; size_t nbytes = 0; int looking = 0; int countcopies = 0; int consider_compress = 0; - + if (ds && (ds->stat == LOOKING || ds->stat == COMPRESS)) { @@ -346,8 +402,8 @@ dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, currentoff < blockaddr + DIRBLKSIZ; prevoff = currentoff, currentoff += entry->rec_len) { - entry = (struct ext2_dir_entry *)currentoff; - + entry = (struct ext2_dir_entry_2 *)currentoff; + if (!entry->rec_len || entry->rec_len % EXT2_DIR_PAD || entry->name_len > EXT2_NAME_LEN @@ -355,16 +411,16 @@ dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, || EXT2_DIR_REC_LEN (entry->name_len) > entry->rec_len || memchr (entry->name, '\0', entry->name_len)) { - ext2_warning ("bad directory entry: inode: %d offset: %ld", + ext2_warning ("bad directory entry: inode: %Ld offset: %zd", dp->cache_id, currentoff - blockaddr + idx * DIRBLKSIZ); return ENOENT; } - + if (looking || countcopies) { int thisfree; - + /* Count how much free space this entry has in it. */ if (entry->inode == 0) thisfree = entry->rec_len; @@ -376,9 +432,9 @@ dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, number of bytes there too. */ if (countcopies && currentoff != blockaddr) nbytes += EXT2_DIR_REC_LEN (entry->name_len); - + if (ds->stat == COMPRESS && nbytes > ds->nbytes) - /* The previously found compress is better than + /* The previously found compress is better than this one, so don't bother counting any more. */ countcopies = 0; @@ -395,9 +451,9 @@ dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, nfree += thisfree; if (nfree >= needed) consider_compress = 1; - } + } } - + if (entry->inode) nentries++; @@ -408,17 +464,17 @@ dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, break; } - if (consider_compress + if (consider_compress && (ds->type == LOOKING || (ds->type == COMPRESS && ds->nbytes > nbytes))) { ds->type = CREATE; ds->stat = COMPRESS; - ds->entry = (struct ext2_dir_entry *) blockaddr; + ds->entry = (struct ext2_dir_entry_2 *) blockaddr; ds->idx = idx; ds->nbytes = nbytes; } - + if (currentoff >= blockaddr + DIRBLKSIZ) { int i; @@ -428,9 +484,9 @@ dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, down how many entries there were. */ if (!dp->dn->dirents) { - dp->dn->dirents = malloc ((dp->dn_stat.st_size / DIRBLKSIZ + 1) - * sizeof (int)); - for (i = 0; i < dp->dn_stat.st_size/DIRBLKSIZ; i++) + dp->dn->dirents = malloc ((dp->dn_stat.st_size / DIRBLKSIZ) + * sizeof (int)); + for (i = 0; i < dp->dn_stat.st_size/DIRBLKSIZ; i++) dp->dn->dirents[i] = -1; } /* Make sure the count is correct if there is one now. */ @@ -440,7 +496,7 @@ dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, return ENOENT; } - + /* We have found the required name. */ if (ds && type == CREATE) @@ -451,7 +507,7 @@ dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, ds->stat = HERE_TIS; ds->entry = entry; ds->idx = idx; - ds->preventry = (struct ext2_dir_entry *) prevoff; + ds->preventry = (struct ext2_dir_entry_2 *) prevoff; } *inum = entry->inode; @@ -465,53 +521,49 @@ dirscanblock (vm_address_t blockaddr, struct node *dp, int idx, char *name, only be made if the directory has been held locked continuously since the preceding lookup call, and only if that call returned ENOENT. */ error_t -diskfs_direnter_hard (struct node *dp, char *name, struct node *np, +diskfs_direnter_hard (struct node *dp, const char *name, struct node *np, struct dirstat *ds, struct protid *cred) { - struct ext2_dir_entry *new; + struct ext2_dir_entry_2 *new; int namelen = strlen (name); int needed = EXT2_DIR_REC_LEN (namelen); int oldneeded; vm_address_t fromoff, tooff; - int totfreed; + int totfreed; error_t err; - off_t oldsize; + size_t oldsize = 0; assert (ds->type == CREATE); - + assert (!diskfs_readonly); dp->dn_set_mtime = 1; + /* Select a location for the new directory entry. Each branch of this + switch is responsible for setting NEW to point to the on-disk + directory entry being written, and setting NEW->rec_len appropriately. */ + switch (ds->stat) { case TAKE: /* We are supposed to consume this slot. */ assert (ds->entry->inode == 0 && ds->entry->rec_len >= needed); - - ds->entry->inode = np->cache_id; - ds->entry->name_len = namelen; - bcopy (name, ds->entry->name, namelen); + new = ds->entry; break; - + case SHRINK: /* We are supposed to take the extra space at the end of this slot. */ oldneeded = EXT2_DIR_REC_LEN (ds->entry->name_len); assert (ds->entry->rec_len - oldneeded >= needed); - - new = (struct ext2_dir_entry *) ((vm_address_t) ds->entry + oldneeded); - new->inode = np->cache_id; + new = (struct ext2_dir_entry_2 *) ((vm_address_t) ds->entry + oldneeded); + new->rec_len = ds->entry->rec_len - oldneeded; - new->name_len = namelen; - bcopy (name, new->name, namelen); - ds->entry->rec_len = oldneeded; - break; - + case COMPRESS: /* We are supposed to move all the entries to the front of the block, giving each the minimum @@ -521,15 +573,15 @@ diskfs_direnter_hard (struct node *dp, char *name, struct node *np, while (fromoff < (vm_address_t) ds->entry + DIRBLKSIZ) { - struct ext2_dir_entry *from = (struct ext2_dir_entry *)fromoff; - struct ext2_dir_entry *to = (struct ext2_dir_entry *) tooff; + struct ext2_dir_entry_2 *from = (struct ext2_dir_entry_2 *)fromoff; + struct ext2_dir_entry_2 *to = (struct ext2_dir_entry_2 *) tooff; int fromreclen = from->rec_len; if (from->inode != 0) { assert (fromoff >= tooff); - bcopy (from, to, fromreclen); + memmove (to, from, fromreclen); to->rec_len = EXT2_DIR_REC_LEN (to->name_len); tooff += to->rec_len; @@ -539,75 +591,105 @@ diskfs_direnter_hard (struct node *dp, char *name, struct node *np, totfreed = (vm_address_t) ds->entry + DIRBLKSIZ - tooff; assert (totfreed >= needed); - - new = (struct ext2_dir_entry *) tooff; - new->inode = np->cache_id; + + new = (struct ext2_dir_entry_2 *) tooff; new->rec_len = totfreed; - new->name_len = namelen; - bcopy (name, new->name, namelen); break; case EXTEND: /* Extend the file. */ assert (needed <= DIRBLKSIZ); - + oldsize = dp->dn_stat.st_size; + if ((off_t)(oldsize + DIRBLKSIZ) != (dp->dn_stat.st_size + DIRBLKSIZ)) + { + /* We can't possibly map the whole directory in. */ + munmap ((caddr_t) ds->mapbuf, ds->mapextent); + return EOVERFLOW; + } while (oldsize + DIRBLKSIZ > dp->allocsize) { err = diskfs_grow (dp, oldsize + DIRBLKSIZ, cred); if (err) { - vm_deallocate (mach_task_self (), ds->mapbuf, ds->mapextent); + munmap ((caddr_t) ds->mapbuf, ds->mapextent); return err; } } - new = (struct ext2_dir_entry *) (ds->mapbuf + oldsize); + new = (struct ext2_dir_entry_2 *) (ds->mapbuf + oldsize); dp->dn_stat.st_size = oldsize + DIRBLKSIZ; dp->dn_set_ctime = 1; - new->inode = np->cache_id; new->rec_len = DIRBLKSIZ; - new->name_len = namelen; - bcopy (name, new->name, namelen); break; - + default: - assert (0); + new = 0; + assert (! "impossible: bogus status field in dirstat"); } + /* NEW points to the directory entry being written, and its + rec_len field is already filled in. Now fill in the rest. */ + + new->inode = np->cache_id; +#if 0 + /* XXX We cannot enable this code because file types can change + (and conceivably quite often) with translator settings. + There is no way for the translator that determines the type of + the virtual node to cause all the directory entries linked to + its underlying inode to reflect the proper type. */ + new->file_type = (EXT2_HAS_INCOMPAT_FEATURE (sblock, + EXT2_FEATURE_INCOMPAT_FILETYPE) + ? file_type_ext2[IFTODT (np->dn_stat.st_mode & S_IFMT)] + : 0); +#else + new->file_type = 0; +#endif + new->name_len = namelen; + memcpy (new->name, name, namelen); + + /* Mark the directory inode has having been written. */ + dp->dn->info.i_flags &= ~EXT2_BTREE_FL; dp->dn_set_mtime = 1; - vm_deallocate (mach_task_self (), ds->mapbuf, ds->mapextent); - + munmap ((caddr_t) ds->mapbuf, ds->mapextent); + if (ds->stat != EXTEND) { - /* If we are keeping count of this block, then keep the count up + /* If we are keeping count of this block, then keep the count up to date. */ if (dp->dn->dirents && dp->dn->dirents[ds->idx] != -1) dp->dn->dirents[ds->idx]++; } else { + int i; /* It's cheap, so start a count here even if we aren't counting anything at all. */ if (dp->dn->dirents) { - dp->dn->dirents = realloc (dp->dn->dirents, - (ds->idx + 1) * sizeof (int)); + dp->dn->dirents = realloc (dp->dn->dirents, + (dp->dn_stat.st_size / DIRBLKSIZ + * sizeof (int))); + for (i = oldsize / DIRBLKSIZ; + i < dp->dn_stat.st_size / DIRBLKSIZ; + i++) + dp->dn->dirents[i] = -1; + dp->dn->dirents[ds->idx] = 1; } else { - int i; - dp->dn->dirents = malloc ((ds->idx + 1) * sizeof (int)); - for (i = 0; i < ds->idx; i++) + dp->dn->dirents = malloc (dp->dn_stat.st_size / DIRBLKSIZ + * sizeof (int)); + for (i = 0; i < dp->dn_stat.st_size / DIRBLKSIZ; i++) dp->dn->dirents[i] = -1; dp->dn->dirents[ds->idx] = 1; } } - + diskfs_file_update (dp, 1); return 0; @@ -616,17 +698,15 @@ diskfs_direnter_hard (struct node *dp, char *name, struct node *np, /* Following a lookup call for REMOVE, this removes the link from the directory. DP is the directory being changed and DS is the cached information returned from lookup. This call is only valid if the - directory has been locked continously since the call to lookup, and + directory has been locked continuously since the call to lookup, and only if that call succeeded. */ error_t diskfs_dirremove_hard (struct node *dp, struct dirstat *ds) { assert (ds->type == REMOVE); assert (ds->stat == HERE_TIS); - - assert (!diskfs_readonly); - dp->dn_set_mtime = 1; + assert (!diskfs_readonly); if (ds->preventry == 0) ds->entry->inode = 0; @@ -638,23 +718,24 @@ diskfs_dirremove_hard (struct node *dp, struct dirstat *ds) } dp->dn_set_mtime = 1; + dp->dn->info.i_flags &= ~EXT2_BTREE_FL; - vm_deallocate (mach_task_self (), ds->mapbuf, ds->mapextent); + munmap ((caddr_t) ds->mapbuf, ds->mapextent); /* If we are keeping count of this block, then keep the count up to date. */ if (dp->dn->dirents && dp->dn->dirents[ds->idx] != -1) dp->dn->dirents[ds->idx]--; - + diskfs_file_update (dp, 1); return 0; } - + /* Following a lookup call for RENAME, this changes the inode number - on a directory entry. DP is the directory being changed; NP is - the new node being linked in; DP is the cached information returned + on a directory entry. DP is the directory being changed; NP is + the new node being linked in; DP is the cached information returned by lookup. This call is only valid if the directory has been locked continuously since the call to lookup, and only if that call succeeded. */ @@ -663,14 +744,15 @@ diskfs_dirrewrite_hard (struct node *dp, struct node *np, struct dirstat *ds) { assert (ds->type == RENAME); assert (ds->stat == HERE_TIS); - + assert (!diskfs_readonly); ds->entry->inode = np->cache_id; dp->dn_set_mtime = 1; + dp->dn->info.i_flags &= ~EXT2_BTREE_FL; + + munmap ((caddr_t) ds->mapbuf, ds->mapextent); - vm_deallocate (mach_task_self (), ds->mapbuf, ds->mapextent); - diskfs_file_update (dp, 1); return 0; @@ -683,23 +765,26 @@ diskfs_dirempty (struct node *dp, struct protid *cred) { error_t err; vm_address_t buf = 0, curoff; - struct ext2_dir_entry *entry; + struct ext2_dir_entry_2 *entry; int hit = 0; /* Found something in the directory. */ memory_object_t memobj = diskfs_get_filemap (dp, VM_PROT_READ); - + + if (memobj == MACH_PORT_NULL) + /* XXX should reflect error properly. */ + return 0; + err = vm_map (mach_task_self (), &buf, dp->dn_stat.st_size, 0, 1, memobj, 0, 0, VM_PROT_READ, VM_PROT_READ, 0); mach_port_deallocate (mach_task_self (), memobj); assert (!err); - if (! diskfs_check_readonly ()) - dp->dn_set_atime = 1; + diskfs_set_node_atime (dp); - for (curoff = buf; + for (curoff = buf; !hit && curoff < buf + dp->dn_stat.st_size; curoff += entry->rec_len) { - entry = (struct ext2_dir_entry *) curoff; + entry = (struct ext2_dir_entry_2 *) curoff; if (entry->inode != 0 && (entry->name_len > 2 @@ -709,12 +794,11 @@ diskfs_dirempty (struct node *dp, struct protid *cred) hit = 1; } - if (! diskfs_check_readonly ()) - dp->dn_set_atime = 1; + diskfs_set_node_atime (dp); if (diskfs_synchronous) diskfs_node_update (dp, 1); - vm_deallocate (mach_task_self (), buf, dp->dn_stat.st_size); + munmap ((caddr_t) buf, dp->dn_stat.st_size); return !hit; } @@ -726,7 +810,7 @@ diskfs_drop_dirstat (struct node *dp, struct dirstat *ds) if (ds->type != LOOKUP) { assert (ds->mapbuf); - vm_deallocate (mach_task_self (), ds->mapbuf, ds->mapextent); + munmap ((caddr_t) ds->mapbuf, ds->mapextent); ds->type = LOOKUP; } return 0; @@ -739,29 +823,29 @@ diskfs_drop_dirstat (struct node *dp, struct dirstat *ds) static error_t count_dirents (struct node *dp, int nb, char *buf) { - int amt; + size_t amt; char *offinblk; - struct ext2_dir_entry *entry; + struct ext2_dir_entry_2 *entry; int count = 0; error_t err; assert (dp->dn->dirents); assert ((nb + 1) * DIRBLKSIZ <= dp->dn_stat.st_size); - + err = diskfs_node_rdwr (dp, buf, nb * DIRBLKSIZ, DIRBLKSIZ, 0, 0, &amt); if (err) return err; assert (amt == DIRBLKSIZ); - + for (offinblk = buf; offinblk < buf + DIRBLKSIZ; offinblk += entry->rec_len) { - entry = (struct ext2_dir_entry *) offinblk; + entry = (struct ext2_dir_entry_2 *) offinblk; if (entry->inode) count++; } - + assert (dp->dn->dirents[nb] == -1 || dp->dn->dirents[nb] == count); dp->dn->dirents[nb] = count; return 0; @@ -771,14 +855,14 @@ count_dirents (struct node *dp, int nb, char *buf) Must be a power of two. */ #define DIRENT_ALIGN 4 -/* Implement the disikfs_get_directs callback as described in +/* Implement the diskfs_get_directs callback as described in <hurd/diskfs.h>. */ error_t -diskfs_get_directs (struct node *dp, - int entry, +diskfs_get_directs (struct node *dp, + int entry, int nentries, char **data, - u_int *datacnt, + size_t *datacnt, vm_size_t bufsiz, int *amt) { @@ -791,11 +875,11 @@ diskfs_get_directs (struct node *dp, error_t err; int i; char *datap; - struct ext2_dir_entry *entryp; + struct ext2_dir_entry_2 *entryp; int allocsize; - int checklen; + size_t checklen; struct dirent *userp; - + nblks = dp->dn_stat.st_size/DIRBLKSIZ; if (!dp->dn->dirents) @@ -805,33 +889,6 @@ diskfs_get_directs (struct node *dp, dp->dn->dirents[i] = -1; } - /* Allocate enough space to hold the maximum we might return */ - if (!bufsiz || bufsiz > dp->dn_stat.st_size) - /* Allocate enough to return the entire directory. Since ext2's - directory format is different than the format used to return the - entries, we allocate enough to hold the on disk directory plus - whatever extra would be necessary in the worst-case. */ - { - /* The minimum size of an ext2fs directory entry. */ - size_t min_entry_size = EXT2_DIR_REC_LEN (0); - /* The minimum size of a returned dirent entry. The +1 is for '\0'. */ - size_t min_dirent_size = offsetof (struct dirent, d_name) + 1; - /* The maximum possible number of ext2fs dir entries in this dir. */ - size_t max_entries = dp->dn_stat.st_size / min_entry_size; - /* The maximum difference in size per directory entry. */ - size_t entry_extra = - DIRENT_ALIGN - + (min_dirent_size > min_entry_size - ? min_dirent_size - min_entry_size : 0); - - allocsize = round_page (dp->dn_stat.st_size + max_entries * entry_extra); - } - else - allocsize = round_page (bufsiz); - - if (allocsize > *datacnt) - vm_allocate (mach_task_self (), (vm_address_t *) data, allocsize, 1); - /* Scan through the entries to find ENTRY. If we encounter a -1 in the process then stop to fill it. When we run off the end, ENTRY is too big. */ @@ -852,17 +909,47 @@ diskfs_get_directs (struct node *dp, break; curentry += dp->dn->dirents[blkno]; - + bufvalid = 0; } - + if (blkno == nblks) { + /* We reached the end of the directory without seeing ENTRY. + This is treated as an EOF condition, meaning we return + success with empty results. */ *datacnt = 0; *amt = 0; return 0; } + /* Allocate enough space to hold the maximum we might return */ + if (!bufsiz || bufsiz > dp->dn_stat.st_size) + /* Allocate enough to return the entire directory. Since ext2's + directory format is different than the format used to return the + entries, we allocate enough to hold the on disk directory plus + whatever extra would be necessary in the worst-case. */ + { + /* The minimum size of an ext2fs directory entry. */ + size_t min_entry_size = EXT2_DIR_REC_LEN (0); + /* The minimum size of a returned dirent entry. The +1 is for '\0'. */ + size_t min_dirent_size = offsetof (struct dirent, d_name) + 1; + /* The maximum possible number of ext2fs dir entries in this dir. */ + size_t max_entries = dp->dn_stat.st_size / min_entry_size; + /* The maximum difference in size per directory entry. */ + size_t entry_extra = + DIRENT_ALIGN + + (min_dirent_size > min_entry_size + ? min_dirent_size - min_entry_size : 0); + + allocsize = round_page (dp->dn_stat.st_size + max_entries * entry_extra); + } + else + allocsize = round_page (bufsiz); + + if (allocsize > *datacnt) + *data = mmap (0, allocsize, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0); + /* Set bufp appropriately */ bufp = buf; if (curentry != entry) @@ -878,13 +965,13 @@ diskfs_get_directs (struct node *dp, assert (checklen == DIRBLKSIZ); bufvalid = 1; } - for (i = 0, bufp = buf; - i < entry - curentry && bufp - buf < DIRBLKSIZ; - bufp += ((struct ext2_dir_entry *)bufp)->rec_len, i++) + for (i = 0, bufp = buf; + i < entry - curentry && bufp - buf < DIRBLKSIZ; + bufp += ((struct ext2_dir_entry_2 *)bufp)->rec_len, i++) ; /* Make sure we didn't run off the end. */ assert (bufp - buf < DIRBLKSIZ); - } + } i = 0; datap = *data; @@ -896,7 +983,7 @@ diskfs_get_directs (struct node *dp, { if (!bufvalid) { - err = diskfs_node_rdwr (dp, buf, blkno * DIRBLKSIZ, DIRBLKSIZ, + err = diskfs_node_rdwr (dp, buf, blkno * DIRBLKSIZ, DIRBLKSIZ, 0, 0, &checklen); if (err) return err; @@ -905,7 +992,7 @@ diskfs_get_directs (struct node *dp, bufp = buf; } - entryp = (struct ext2_dir_entry *)bufp; + entryp = (struct ext2_dir_entry_2 *)bufp; if (entryp->inode) { @@ -934,7 +1021,30 @@ diskfs_get_directs (struct node *dp, userp->d_fileno = entryp->inode; userp->d_reclen = rec_len; userp->d_namlen = name_len; - bcopy (entryp->name, userp->d_name, name_len); + +#if 0 + /* We don't bother to check the EXT2_FEATURE_INCOMPAT_FILETYPE + flag in the superblock, because in old filesystems the + file_type field is the high byte of the length field and is + always zero because names cannot be that long. */ + if (entryp->file_type < EXT2_FT_MAX) + userp->d_type = ext2_file_type[entryp->file_type]; + else + { + ext2_warning ("bad type %d in directory entry: " + "inode: %d offset: %d", + entryp->file_type, + dp->cache_id, + blkno * DIRBLKSIZ + bufp - buf); + userp->d_type = DT_UNKNOWN; + } +#else + /* XXX + For complex reasons it might not be correct to return + the filesystem's d_type value to the user. */ + userp->d_type = DT_UNKNOWN; +#endif + memcpy (userp->d_name, entryp->name, name_len); userp->d_name[name_len] = '\0'; datap += rec_len; @@ -943,7 +1053,7 @@ diskfs_get_directs (struct node *dp, if (entryp->rec_len == 0) { - ext2_warning ("zero length directory entry: inode: %d offset: %d", + ext2_warning ("zero length directory entry: inode: %Ld offset: %zd", dp->cache_id, blkno * DIRBLKSIZ + bufp - buf); return EIO; @@ -957,23 +1067,22 @@ diskfs_get_directs (struct node *dp, } else if (bufp - buf > DIRBLKSIZ) { - ext2_warning ("directory entry too long: inode: %d offset: %d", + ext2_warning ("directory entry too long: inode: %Ld offset: %zd", dp->cache_id, blkno * DIRBLKSIZ + bufp - buf - entryp->rec_len); return EIO; } } - + /* We've copied all we can. If we allocated our own array but didn't fill all of it, then free whatever memory we didn't use. */ if (allocsize > *datacnt) { if (round_page (datap - *data) < allocsize) - vm_deallocate (mach_task_self (), - (vm_address_t) (*data + round_page (datap - *data)), - allocsize - round_page (datap - *data)); + munmap ((caddr_t) (*data + round_page (datap - *data)), + allocsize - round_page (datap - *data)); } - + /* Set variables for return */ *datacnt = datap - *data; *amt = i; |