]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/affs/dir.c
Merge tag 'for-linus-5.2-ofs1' of git://git.kernel.org/pub/scm/linux/kernel/git/hubca...
[mirror_ubuntu-jammy-kernel.git] / fs / affs / dir.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
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
9dffe569 17#include <linux/iversion.h>
1da177e4
LT
18#include "affs.h"
19
0edf977d 20static int affs_readdir(struct file *, struct dir_context *);
1da177e4 21
4b6f5d20 22const struct file_operations affs_dir_operations = {
1da177e4 23 .read = generic_read_dir,
59af1584 24 .llseek = generic_file_llseek,
3b0a3c1a 25 .iterate_shared = affs_readdir,
c4758795 26 .fsync = affs_file_fsync,
1da177e4
LT
27};
28
29/*
30 * directories can handle most operations...
31 */
754661f1 32const struct inode_operations affs_dir_inode_operations = {
1da177e4
LT
33 .create = affs_create,
34 .lookup = affs_lookup,
35 .link = affs_link,
36 .unlink = affs_unlink,
37 .symlink = affs_symlink,
38 .mkdir = affs_mkdir,
39 .rmdir = affs_rmdir,
c6184028 40 .rename = affs_rename2,
1da177e4
LT
41 .setattr = affs_notify_change,
42};
43
44static int
0edf977d 45affs_readdir(struct file *file, struct dir_context *ctx)
1da177e4 46{
0edf977d 47 struct inode *inode = file_inode(file);
1da177e4 48 struct super_block *sb = inode->i_sb;
0edf977d
AV
49 struct buffer_head *dir_bh = NULL;
50 struct buffer_head *fh_bh = NULL;
1da177e4
LT
51 unsigned char *name;
52 int namelen;
53 u32 i;
54 int hash_pos;
55 int chain_pos;
1da177e4 56 u32 ino;
d40c4d46 57 int error = 0;
1da177e4 58
08fe100d 59 pr_debug("%s(ino=%lu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos);
1da177e4 60
0edf977d
AV
61 if (ctx->pos < 2) {
62 file->private_data = (void *)0;
63 if (!dir_emit_dots(file, ctx))
1da177e4 64 return 0;
1da177e4
LT
65 }
66
67 affs_lock_dir(inode);
0edf977d
AV
68 chain_pos = (ctx->pos - 2) & 0xffff;
69 hash_pos = (ctx->pos - 2) >> 16;
1da177e4
LT
70 if (chain_pos == 0xffff) {
71 affs_warning(sb, "readdir", "More than 65535 entries in chain");
72 chain_pos = 0;
73 hash_pos++;
0edf977d 74 ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
1da177e4
LT
75 }
76 dir_bh = affs_bread(sb, inode->i_ino);
77 if (!dir_bh)
d40c4d46 78 goto out_unlock_dir;
1da177e4
LT
79
80 /* If the directory hasn't changed since the last call to readdir(),
81 * we can jump directly to where we left off.
82 */
0edf977d 83 ino = (u32)(long)file->private_data;
c472c07b 84 if (ino && inode_eq_iversion(inode, file->f_version)) {
9606d9aa 85 pr_debug("readdir() left off=%d\n", ino);
1da177e4
LT
86 goto inside;
87 }
88
89 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
90 for (i = 0; ino && i < chain_pos; i++) {
91 fh_bh = affs_bread(sb, ino);
92 if (!fh_bh) {
93 affs_error(sb, "readdir","Cannot read block %d", i);
d40c4d46
FF
94 error = -EIO;
95 goto out_brelse_dir;
1da177e4
LT
96 }
97 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
98 affs_brelse(fh_bh);
99 fh_bh = NULL;
100 }
101 if (ino)
102 goto inside;
103 hash_pos++;
104
105 for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
106 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
107 if (!ino)
108 continue;
0edf977d 109 ctx->pos = (hash_pos << 16) + 2;
1da177e4
LT
110inside:
111 do {
112 fh_bh = affs_bread(sb, ino);
113 if (!fh_bh) {
d40c4d46
FF
114 affs_error(sb, "readdir",
115 "Cannot read block %d", ino);
0edf977d 116 break;
1da177e4
LT
117 }
118
f157853e
FF
119 namelen = min(AFFS_TAIL(sb, fh_bh)->name[0],
120 (u8)AFFSNAMEMAX);
1da177e4 121 name = AFFS_TAIL(sb, fh_bh)->name + 1;
08fe100d
GU
122 pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n",
123 namelen, name, ino, hash_pos, ctx->pos);
d40c4d46 124
0edf977d 125 if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
d40c4d46 126 goto done;
0edf977d 127 ctx->pos++;
1da177e4
LT
128 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
129 affs_brelse(fh_bh);
130 fh_bh = NULL;
131 } while (ino);
132 }
d40c4d46 133done:
9dffe569 134 file->f_version = inode_query_iversion(inode);
0edf977d 135 file->private_data = (void *)(long)ino;
d40c4d46 136 affs_brelse(fh_bh);
1da177e4 137
d40c4d46 138out_brelse_dir:
1da177e4 139 affs_brelse(dir_bh);
d40c4d46
FF
140
141out_unlock_dir:
1da177e4 142 affs_unlock_dir(inode);
d40c4d46 143 return error;
1da177e4 144}