blob: f1e7294381c5aa48a386a3a7c0db7f4832c568ec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/affs/dir.c
3 *
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
5 *
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
7 *
8 * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
9 *
10 * (C) 1991 Linus Torvalds - minix filesystem
11 *
12 * affs directory handling functions
13 *
14 */
15
16#include "affs.h"
17
Al Viro0edf9772013-05-17 17:44:42 -040018static int affs_readdir(struct file *, struct dir_context *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080020const struct file_operations affs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 .read = generic_read_dir,
Al Viro59af1582008-08-24 07:24:41 -040022 .llseek = generic_file_llseek,
Al Viro3b0a3c12016-04-20 23:42:46 -040023 .iterate_shared = affs_readdir,
Al Viroc4758792009-06-08 01:22:00 -040024 .fsync = affs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
27/*
28 * directories can handle most operations...
29 */
Arjan van de Ven754661f2007-02-12 00:55:38 -080030const struct inode_operations affs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .create = affs_create,
32 .lookup = affs_lookup,
33 .link = affs_link,
34 .unlink = affs_unlink,
35 .symlink = affs_symlink,
36 .mkdir = affs_mkdir,
37 .rmdir = affs_rmdir,
38 .rename = affs_rename,
39 .setattr = affs_notify_change,
40};
41
42static int
Al Viro0edf9772013-05-17 17:44:42 -040043affs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Al Viro0edf9772013-05-17 17:44:42 -040045 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct super_block *sb = inode->i_sb;
Al Viro0edf9772013-05-17 17:44:42 -040047 struct buffer_head *dir_bh = NULL;
48 struct buffer_head *fh_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned char *name;
50 int namelen;
51 u32 i;
52 int hash_pos;
53 int chain_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 u32 ino;
Fabian Frederickd40c4d42014-04-07 15:39:00 -070055 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -080057 pr_debug("%s(ino=%lu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Al Viro0edf9772013-05-17 17:44:42 -040059 if (ctx->pos < 2) {
60 file->private_data = (void *)0;
61 if (!dir_emit_dots(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64
65 affs_lock_dir(inode);
Al Viro0edf9772013-05-17 17:44:42 -040066 chain_pos = (ctx->pos - 2) & 0xffff;
67 hash_pos = (ctx->pos - 2) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (chain_pos == 0xffff) {
69 affs_warning(sb, "readdir", "More than 65535 entries in chain");
70 chain_pos = 0;
71 hash_pos++;
Al Viro0edf9772013-05-17 17:44:42 -040072 ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74 dir_bh = affs_bread(sb, inode->i_ino);
75 if (!dir_bh)
Fabian Frederickd40c4d42014-04-07 15:39:00 -070076 goto out_unlock_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 /* If the directory hasn't changed since the last call to readdir(),
79 * we can jump directly to where we left off.
80 */
Al Viro0edf9772013-05-17 17:44:42 -040081 ino = (u32)(long)file->private_data;
82 if (ino && file->f_version == inode->i_version) {
Fabian Frederick9606d9a2014-06-06 14:37:25 -070083 pr_debug("readdir() left off=%d\n", ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 goto inside;
85 }
86
87 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
88 for (i = 0; ino && i < chain_pos; i++) {
89 fh_bh = affs_bread(sb, ino);
90 if (!fh_bh) {
91 affs_error(sb, "readdir","Cannot read block %d", i);
Fabian Frederickd40c4d42014-04-07 15:39:00 -070092 error = -EIO;
93 goto out_brelse_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
96 affs_brelse(fh_bh);
97 fh_bh = NULL;
98 }
99 if (ino)
100 goto inside;
101 hash_pos++;
102
103 for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
104 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
105 if (!ino)
106 continue;
Al Viro0edf9772013-05-17 17:44:42 -0400107 ctx->pos = (hash_pos << 16) + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108inside:
109 do {
110 fh_bh = affs_bread(sb, ino);
111 if (!fh_bh) {
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700112 affs_error(sb, "readdir",
113 "Cannot read block %d", ino);
Al Viro0edf9772013-05-17 17:44:42 -0400114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
Fabian Frederickf1578532015-02-17 13:46:23 -0800117 namelen = min(AFFS_TAIL(sb, fh_bh)->name[0],
118 (u8)AFFSNAMEMAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 name = AFFS_TAIL(sb, fh_bh)->name + 1;
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -0800120 pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n",
121 namelen, name, ino, hash_pos, ctx->pos);
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700122
Al Viro0edf9772013-05-17 17:44:42 -0400123 if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700124 goto done;
Al Viro0edf9772013-05-17 17:44:42 -0400125 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
127 affs_brelse(fh_bh);
128 fh_bh = NULL;
129 } while (ino);
130 }
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700131done:
Al Viro0edf9772013-05-17 17:44:42 -0400132 file->f_version = inode->i_version;
133 file->private_data = (void *)(long)ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 affs_brelse(fh_bh);
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700135
136out_brelse_dir:
137 affs_brelse(dir_bh);
138
139out_unlock_dir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 affs_unlock_dir(inode);
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700141 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}