forked from sysprog21/simplefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.c
65 lines (54 loc) · 1.67 KB
/
dir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#define pr_fmt(fmt) "simplefs: " fmt
#include <linux/buffer_head.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include "simplefs.h"
/*
* Iterate over the files contained in dir and commit them in ctx.
* This function is called by the VFS while ctx->pos changes.
* Return 0 on success.
*/
static int simplefs_iterate(struct file *dir, struct dir_context *ctx)
{
struct inode *inode = file_inode(dir);
struct simplefs_inode_info *ci = SIMPLEFS_INODE(inode);
struct super_block *sb = inode->i_sb;
struct buffer_head *bh = NULL;
struct simplefs_dir_block *dblock = NULL;
struct simplefs_file *f = NULL;
int i;
/* Check that dir is a directory */
if (!S_ISDIR(inode->i_mode))
return -ENOTDIR;
/*
* Check that ctx->pos is not bigger than what we can handle (including
* . and ..)
*/
if (ctx->pos > SIMPLEFS_MAX_SUBFILES + 2)
return 0;
/* Commit . and .. to ctx */
if (!dir_emit_dots(dir, ctx))
return 0;
/* Read the directory index block on disk */
bh = sb_bread(sb, ci->dir_block);
if (!bh)
return -EIO;
dblock = (struct simplefs_dir_block *) bh->b_data;
/* Iterate over the index block and commit subfiles */
for (i = ctx->pos - 2; i < SIMPLEFS_MAX_SUBFILES; i++) {
f = &dblock->files[i];
if (!f->inode)
break;
if (!dir_emit(ctx, f->filename, SIMPLEFS_FILENAME_LEN, f->inode,
DT_UNKNOWN))
break;
ctx->pos++;
}
brelse(bh);
return 0;
}
const struct file_operations simplefs_dir_ops = {
.owner = THIS_MODULE,
.iterate_shared = simplefs_iterate,
};