| 1 | |
| 2 | |
| 3 | * linux/fs/affs/dir.c
|
| 4 | *
|
| 5 | * (c) 1996 Hans-Joachim Widmaier - Rewritten
|
| 6 | *
|
| 7 | * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
|
| 8 | *
|
| 9 | * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
|
| 10 | *
|
| 11 | * (C) 1991 Linus Torvalds - minix filesystem
|
| 12 | *
|
| 13 | * affs directory handling functions
|
| 14 | *
|
| 15 |
|
| 16 |
|
| 17 | #include <linux/iversion.h>
|
| 18 | #include <linux/filelock.h>
|
| 19 | #include "affs.h"
|
| 20 |
|
| 21 | struct affs_dir_data {
|
| 22 | unsigned long ino;
|
| 23 | u64 cookie;
|
| 24 | };
|
| 25 |
|
| 26 | static int affs_readdir(struct file *, struct dir_context *);
|
| 27 |
|
| 28 | static loff_t affs_dir_llseek(struct file *file, loff_t offset, int whence)
|
| 29 | {
|
| 30 | struct affs_dir_data *data = file->private_data;
|
| 31 |
|
| 32 | return generic_llseek_cookie(file, offset, whence, &data->cookie);
|
| 33 | }
|
| 34 |
|
| 35 | static int affs_dir_open(struct inode *inode, struct file *file)
|
| 36 | {
|
| 37 | struct affs_dir_data *data;
|
| 38 |
|
| 39 | data = kzalloc_obj(struct affs_dir_data);
|
| 40 | if (!data)
|
| 41 | return -ENOMEM;
|
| 42 | file->private_data = data;
|
| 43 | return 0;
|
| 44 | }
|
| 45 |
|
| 46 | static int affs_dir_release(struct inode *inode, struct file *file)
|
| 47 | {
|
| 48 | kfree(file->private_data);
|
| 49 | return 0;
|
| 50 | }
|
| 51 |
|
| 52 | const struct file_operations affs_dir_operations = {
|
| 53 | .open = affs_dir_open,
|
| 54 | .read = generic_read_dir,
|
| 55 | .llseek = affs_dir_llseek,
|
| 56 | .iterate_shared = affs_readdir,
|
| 57 | .fsync = affs_file_fsync,
|
| 58 | .release = affs_dir_release,
|
| 59 | .setlease = generic_setlease,
|
| 60 | };
|
| 61 |
|
| 62 | |
| 63 | * directories can handle most operations...
|
| 64 |
|
| 65 | const struct inode_operations affs_dir_inode_operations = {
|
| 66 | .create = affs_create,
|
| 67 | .lookup = affs_lookup,
|
| 68 | .link = affs_link,
|
| 69 | .unlink = affs_unlink,
|
| 70 | .symlink = affs_symlink,
|
| 71 | .mkdir = affs_mkdir,
|
| 72 | .rmdir = affs_rmdir,
|
| 73 | .rename = affs_rename2,
|
| 74 | .setattr = affs_setattr,
|
| 75 | };
|
| 76 |
|
| 77 | static int
|
| 78 | affs_readdir(struct file *file, struct dir_context *ctx)
|
| 79 | {
|
| 80 | struct inode *inode = file_inode(file);
|
| 81 | struct affs_dir_data *data = file->private_data;
|
| 82 | struct super_block *sb = inode->i_sb;
|
| 83 | struct buffer_head *dir_bh = NULL;
|
| 84 | struct buffer_head *fh_bh = NULL;
|
| 85 | unsigned char *name;
|
| 86 | int namelen;
|
| 87 | u32 i;
|
| 88 | int hash_pos;
|
| 89 | int chain_pos;
|
| 90 | u32 ino;
|
| 91 | int error = 0;
|
| 92 |
|
| 93 | pr_debug("%s(ino=%llu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos);
|
| 94 |
|
| 95 | if (ctx->pos < 2) {
|
| 96 | data->ino = 0;
|
| 97 | if (!dir_emit_dots(file, ctx))
|
| 98 | return 0;
|
| 99 | }
|
| 100 |
|
| 101 | affs_lock_dir(inode);
|
| 102 | chain_pos = (ctx->pos - 2) & 0xffff;
|
| 103 | hash_pos = (ctx->pos - 2) >> 16;
|
| 104 | if (chain_pos == 0xffff) {
|
| 105 | affs_warning(sb, "readdir", "More than 65535 entries in chain");
|
| 106 | chain_pos = 0;
|
| 107 | hash_pos++;
|
| 108 | ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
|
| 109 | }
|
| 110 | dir_bh = affs_bread(sb, inode->i_ino);
|
| 111 | if (!dir_bh)
|
| 112 | goto out_unlock_dir;
|
| 113 |
|
| 114 | |
| 115 | * we can jump directly to where we left off.
|
| 116 |
|
| 117 | ino = data->ino;
|
| 118 | if (ino && inode_eq_iversion(inode, data->cookie)) {
|
| 119 | pr_debug("readdir() left off=%d\n", ino);
|
| 120 | goto inside;
|
| 121 | }
|
| 122 | if (hash_pos >= AFFS_SB(sb)->s_hashsize)
|
| 123 | goto done;
|
| 124 |
|
| 125 | ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
|
| 126 | for (i = 0; ino && i < chain_pos; i++) {
|
| 127 | fh_bh = affs_bread(sb, ino);
|
| 128 | if (!fh_bh) {
|
| 129 | affs_error(sb, "readdir","Cannot read block %d", i);
|
| 130 | error = -EIO;
|
| 131 | goto out_brelse_dir;
|
| 132 | }
|
| 133 | ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
|
| 134 | affs_brelse(fh_bh);
|
| 135 | fh_bh = NULL;
|
| 136 | }
|
| 137 | if (ino)
|
| 138 | goto inside;
|
| 139 | hash_pos++;
|
| 140 |
|
| 141 | for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
|
| 142 | ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
|
| 143 | if (!ino)
|
| 144 | continue;
|
| 145 | ctx->pos = (hash_pos << 16) + 2;
|
| 146 | inside:
|
| 147 | do {
|
| 148 | fh_bh = affs_bread(sb, ino);
|
| 149 | if (!fh_bh) {
|
| 150 | affs_error(sb, "readdir",
|
| 151 | "Cannot read block %d", ino);
|
| 152 | break;
|
| 153 | }
|
| 154 |
|
| 155 | namelen = min(AFFS_TAIL(sb, fh_bh)->name[0],
|
| 156 | (u8)AFFSNAMEMAX);
|
| 157 | name = AFFS_TAIL(sb, fh_bh)->name + 1;
|
| 158 | pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n",
|
| 159 | namelen, name, ino, hash_pos, ctx->pos);
|
| 160 |
|
| 161 | if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
|
| 162 | goto done;
|
| 163 | ctx->pos++;
|
| 164 | ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
|
| 165 | affs_brelse(fh_bh);
|
| 166 | fh_bh = NULL;
|
| 167 | } while (ino);
|
| 168 | }
|
| 169 | done:
|
| 170 | data->cookie = inode_query_iversion(inode);
|
| 171 | data->ino = ino;
|
| 172 | affs_brelse(fh_bh);
|
| 173 |
|
| 174 | out_brelse_dir:
|
| 175 | affs_brelse(dir_bh);
|
| 176 |
|
| 177 | out_unlock_dir:
|
| 178 | affs_unlock_dir(inode);
|
| 179 | return error;
|
| 180 | }
|