@@ -241,9 +241,16 @@ const struct inode_operations simple_dir_inode_operations = {
};
EXPORT_SYMBOL(simple_dir_inode_operations);
-/* 0 is '.', 1 is '..', so always start with offset 2 or more */
+/* simple_offset_add() allocation range */
enum {
- DIR_OFFSET_MIN = 2,
+ DIR_OFFSET_MIN = 2,
+ DIR_OFFSET_MAX = LONG_MAX - 1,
+};
+
+/* simple_offset_add() never assigns these to a dentry */
+enum {
+ DIR_OFFSET_EOD = LONG_MAX, /* Marks EOD */
+
};
static void offset_set(struct dentry *dentry, long offset)
@@ -287,7 +294,8 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry)
return -EBUSY;
ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN,
- LONG_MAX, &octx->next_offset, GFP_KERNEL);
+ DIR_OFFSET_MAX, &octx->next_offset,
+ GFP_KERNEL);
if (unlikely(ret == -EBUSY))
return -ENOSPC;
if (unlikely(ret < 0))
@@ -445,8 +453,6 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)
return -EINVAL;
}
- /* In this case, ->private_data is protected by f_pos_lock */
- file->private_data = NULL;
return vfs_setpos(file, offset, LONG_MAX);
}
@@ -456,7 +462,7 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset)
struct dentry *child, *found = NULL;
rcu_read_lock();
- child = mas_find(&mas, LONG_MAX);
+ child = mas_find(&mas, DIR_OFFSET_MAX);
if (!child)
goto out;
spin_lock(&child->d_lock);
@@ -477,7 +483,7 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
inode->i_ino, fs_umode_to_dtype(inode->i_mode));
}
-static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
+static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
{
struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode);
struct dentry *dentry;
@@ -485,7 +491,7 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
while (true) {
dentry = offset_find_next(octx, ctx->pos);
if (!dentry)
- return ERR_PTR(-ENOENT);
+ goto out_eod;
if (!offset_dir_emit(ctx, dentry)) {
dput(dentry);
@@ -495,7 +501,10 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
ctx->pos = dentry2offset(dentry) + 1;
dput(dentry);
}
- return NULL;
+ return;
+
+out_eod:
+ ctx->pos = DIR_OFFSET_EOD;
}
/**
@@ -515,6 +524,8 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
*
* On return, @ctx->pos contains an offset that will read the next entry
* in this directory when offset_readdir() is called again with @ctx.
+ * Caller places this value in the d_off field of the last entry in the
+ * user's buffer.
*
* Return values:
* %0 - Complete
@@ -527,13 +538,8 @@ static int offset_readdir(struct file *file, struct dir_context *ctx)
if (!dir_emit_dots(file, ctx))
return 0;
-
- /* In this case, ->private_data is protected by f_pos_lock */
- if (ctx->pos == DIR_OFFSET_MIN)
- file->private_data = NULL;
- else if (file->private_data == ERR_PTR(-ENOENT))
- return 0;
- file->private_data = offset_iterate_dir(d_inode(dir), ctx);
+ if (ctx->pos != DIR_OFFSET_EOD)
+ offset_iterate_dir(d_inode(dir), ctx);
return 0;
}