Commit 4f42c1b5 authored by Al Viro's avatar Al Viro

libfs.c: new helper - next_positive()

Return nth positive child after given or NULL if there's
less than n left.  dcache_readdir() and dcache_dir_lseek()
switched to it.
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 274f5b04
...@@ -84,6 +84,39 @@ int dcache_dir_close(struct inode *inode, struct file *file) ...@@ -84,6 +84,39 @@ int dcache_dir_close(struct inode *inode, struct file *file)
} }
EXPORT_SYMBOL(dcache_dir_close); EXPORT_SYMBOL(dcache_dir_close);
/* parent is locked at least shared */
static struct dentry *next_positive(struct dentry *parent,
struct list_head *from,
int count)
{
struct dentry *res = NULL;
struct list_head *p;
spin_lock(&parent->d_lock);
for (p = from->next; p != &parent->d_subdirs; p = p->next) {
struct dentry *d = list_entry(p, struct dentry, d_child);
if (simple_positive(d) && !--count) {
res = d;
break;
}
}
spin_unlock(&parent->d_lock);
return res;
}
static void move_cursor(struct dentry *cursor, struct list_head *after)
{
struct dentry *parent = cursor->d_parent;
spin_lock(&parent->d_lock);
__list_del(cursor->d_child.prev, cursor->d_child.next);
if (after)
list_add(&cursor->d_child, after);
else
list_add_tail(&cursor->d_child, &parent->d_subdirs);
spin_unlock(&parent->d_lock);
}
loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence) loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
{ {
struct dentry *dentry = file->f_path.dentry; struct dentry *dentry = file->f_path.dentry;
...@@ -99,24 +132,13 @@ loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence) ...@@ -99,24 +132,13 @@ loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
if (offset != file->f_pos) { if (offset != file->f_pos) {
file->f_pos = offset; file->f_pos = offset;
if (file->f_pos >= 2) { if (file->f_pos >= 2) {
struct list_head *p;
struct dentry *cursor = file->private_data; struct dentry *cursor = file->private_data;
struct dentry *to;
loff_t n = file->f_pos - 2; loff_t n = file->f_pos - 2;
inode_lock_shared(dentry->d_inode); inode_lock_shared(dentry->d_inode);
spin_lock(&dentry->d_lock); to = next_positive(dentry, &dentry->d_subdirs, n);
/* d_lock not required for cursor */ move_cursor(cursor, to ? &to->d_child : NULL);
list_del(&cursor->d_child);
p = dentry->d_subdirs.next;
while (n && p != &dentry->d_subdirs) {
struct dentry *next;
next = list_entry(p, struct dentry, d_child);
if (simple_positive(next))
n--;
p = p->next;
}
list_add_tail(&cursor->d_child, p);
spin_unlock(&dentry->d_lock);
inode_unlock_shared(dentry->d_inode); inode_unlock_shared(dentry->d_inode);
} }
} }
...@@ -140,30 +162,25 @@ int dcache_readdir(struct file *file, struct dir_context *ctx) ...@@ -140,30 +162,25 @@ int dcache_readdir(struct file *file, struct dir_context *ctx)
{ {
struct dentry *dentry = file->f_path.dentry; struct dentry *dentry = file->f_path.dentry;
struct dentry *cursor = file->private_data; struct dentry *cursor = file->private_data;
struct list_head *p, *q = &cursor->d_child; struct list_head *p = &cursor->d_child;
struct dentry *next;
bool moved = false;
if (!dir_emit_dots(file, ctx)) if (!dir_emit_dots(file, ctx))
return 0; return 0;
spin_lock(&dentry->d_lock);
if (ctx->pos == 2)
list_move(q, &dentry->d_subdirs);
for (p = q->next; p != &dentry->d_subdirs; p = p->next) { if (ctx->pos == 2)
struct dentry *next = list_entry(p, struct dentry, d_child); p = &dentry->d_subdirs;
if (!simple_positive(next)) while ((next = next_positive(dentry, p, 1)) != NULL) {
continue;
spin_unlock(&dentry->d_lock);
if (!dir_emit(ctx, next->d_name.name, next->d_name.len, if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
d_inode(next)->i_ino, dt_type(d_inode(next)))) d_inode(next)->i_ino, dt_type(d_inode(next))))
return 0; break;
spin_lock(&dentry->d_lock); moved = true;
/* next is still alive */ p = &next->d_child;
list_move(q, p);
p = q;
ctx->pos++; ctx->pos++;
} }
spin_unlock(&dentry->d_lock); if (moved)
move_cursor(cursor, p);
return 0; return 0;
} }
EXPORT_SYMBOL(dcache_readdir); EXPORT_SYMBOL(dcache_readdir);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment