Skip to content

Commit

Permalink
Restructure zfs_readdir() to fix regressions
Browse files Browse the repository at this point in the history
This does the following:

1. It creates a uint8_t type value, which is initialized to DT_DIR on
dot directories and ZFS_DIRENT_TYPE(zap.za_first_integer) otherwise.
This resolves a regression where we return unintialized values as the
directory entry type on dot directories. This was accidentally
introduced by commit 8170d28.

2. It restructures zfs_readdir() code to use `uint64_t offset` like
Illumos instead of `loff_t *pos`. This resolves a regression where
negative ZAP cursors were treated as if they were dot directories.

3. It restructures the function to more closely match the structure of
zfs_readdir() on Illumos and removes the unused variable outcount, which
was only used on Illumos.

Signed-off-by: Richard Yao <[email protected]>
Signed-off-by: Brian Behlendorf <[email protected]>
Closes openzfs#1750
  • Loading branch information
ryao authored and behlendorf committed Oct 29, 2013
1 parent d65e738 commit c12e3a5
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions module/zfs/zfs_vnops.c
Original file line number Diff line number Diff line change
Expand Up @@ -2004,12 +2004,12 @@ zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
objset_t *os;
zap_cursor_t zc;
zap_attribute_t zap;
int outcount;
int error;
uint8_t prefetch;
uint8_t type;
int done = 0;
uint64_t parent;
loff_t *pos = &(ctx->pos);
uint64_t offset; /* must be unsigned; checks for < 1 */

ZFS_ENTER(zsb);
ZFS_VERIFY_ZP(zp);
Expand All @@ -2021,17 +2021,18 @@ zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
/*
* Quit if directory has been removed (posix)
*/
error = 0;
if (zp->z_unlinked)
goto out;

error = 0;
os = zsb->z_os;
offset = ctx->pos;
prefetch = zp->z_zn_prefetch;

/*
* Initialize the iterator cursor.
*/
if (*pos <= 3) {
if (offset <= 3) {
/*
* Start iteration from the beginning of the directory.
*/
Expand All @@ -2040,31 +2041,32 @@ zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
/*
* The offset is a serialized cursor.
*/
zap_cursor_init_serialized(&zc, os, zp->z_id, *pos);
zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
}

/*
* Transform to file-system independent format
*/
outcount = 0;

while (!done) {
uint64_t objnum;
/*
* Special case `.', `..', and `.zfs'.
*/
if (*pos == 0) {
if (offset == 0) {
(void) strcpy(zap.za_name, ".");
zap.za_normalization_conflict = 0;
objnum = zp->z_id;
} else if (*pos == 1) {
type = DT_DIR;
} else if (offset == 1) {
(void) strcpy(zap.za_name, "..");
zap.za_normalization_conflict = 0;
objnum = parent;
} else if (*pos == 2 && zfs_show_ctldir(zp)) {
type = DT_DIR;
} else if (offset == 2 && zfs_show_ctldir(zp)) {
(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
zap.za_normalization_conflict = 0;
objnum = ZFSCTL_INO_ROOT;
type = DT_DIR;
} else {
/*
* Grab next entry.
Expand All @@ -2089,18 +2091,19 @@ zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
"entry, obj = %lld, offset = %lld, "
"length = %d, num = %lld\n",
(u_longlong_t)zp->z_id,
(u_longlong_t)*pos,
(u_longlong_t)offset,
zap.za_integer_length,
(u_longlong_t)zap.za_num_integers);
error = ENXIO;
goto update;
}

objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
type = ZFS_DIRENT_TYPE(zap.za_first_integer);
}

done = !dir_emit(ctx, zap.za_name, strlen(zap.za_name),
objnum, ZFS_DIRENT_TYPE(zap.za_first_integer));
objnum, type);
if (done)
break;

Expand All @@ -2109,12 +2112,16 @@ zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
dmu_prefetch(os, objnum, 0, 0);
}

if (*pos > 2 || (*pos == 2 && !zfs_show_ctldir(zp))) {
/*
* Move to the next entry, fill in the previous offset.
*/
if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
zap_cursor_advance(&zc);
*pos = zap_cursor_serialize(&zc);
offset = zap_cursor_serialize(&zc);
} else {
(*pos)++;
offset += 1;
}
ctx->pos = offset;
}
zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */

Expand Down

0 comments on commit c12e3a5

Please sign in to comment.