blob: a105e77df2c10b818bb4134b7fa6cf2075db34d4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 "affs.h"
18
Al Viro0edf9772013-05-17 17:44:42 -040019static int affs_readdir(struct file *, struct dir_context *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080021const struct file_operations affs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 .read = generic_read_dir,
Al Viro59af1582008-08-24 07:24:41 -040023 .llseek = generic_file_llseek,
Al Viro3b0a3c12016-04-20 23:42:46 -040024 .iterate_shared = affs_readdir,
Al Viroc4758792009-06-08 01:22:00 -040025 .fsync = affs_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
28/*
29 * directories can handle most operations...
30 */
Arjan van de Ven754661f2007-02-12 00:55:38 -080031const struct inode_operations affs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .create = affs_create,
33 .lookup = affs_lookup,
34 .link = affs_link,
35 .unlink = affs_unlink,
36 .symlink = affs_symlink,
37 .mkdir = affs_mkdir,
38 .rmdir = affs_rmdir,
Fabian Frederickc6184022017-05-05 20:51:42 +020039 .rename = affs_rename2,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 .setattr = affs_notify_change,
41};
42
43static int
Al Viro0edf9772013-05-17 17:44:42 -040044affs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Al Viro0edf9772013-05-17 17:44:42 -040046 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct super_block *sb = inode->i_sb;
Al Viro0edf9772013-05-17 17:44:42 -040048 struct buffer_head *dir_bh = NULL;
49 struct buffer_head *fh_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned char *name;
51 int namelen;
52 u32 i;
53 int hash_pos;
54 int chain_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 u32 ino;
Fabian Frederickd40c4d42014-04-07 15:39:00 -070056 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -080058 pr_debug("%s(ino=%lu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Al Viro0edf9772013-05-17 17:44:42 -040060 if (ctx->pos < 2) {
61 file->private_data = (void *)0;
62 if (!dir_emit_dots(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65
66 affs_lock_dir(inode);
Al Viro0edf9772013-05-17 17:44:42 -040067 chain_pos = (ctx->pos - 2) & 0xffff;
68 hash_pos = (ctx->pos - 2) >> 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (chain_pos == 0xffff) {
70 affs_warning(sb, "readdir", "More than 65535 entries in chain");
71 chain_pos = 0;
72 hash_pos++;
Al Viro0edf9772013-05-17 17:44:42 -040073 ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 dir_bh = affs_bread(sb, inode->i_ino);
76 if (!dir_bh)
Fabian Frederickd40c4d42014-04-07 15:39:00 -070077 goto out_unlock_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 /* If the directory hasn't changed since the last call to readdir(),
80 * we can jump directly to where we left off.
81 */
Al Viro0edf9772013-05-17 17:44:42 -040082 ino = (u32)(long)file->private_data;
83 if (ino && file->f_version == inode->i_version) {
Fabian Frederick9606d9a2014-06-06 14:37:25 -070084 pr_debug("readdir() left off=%d\n", ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 goto inside;
86 }
87
88 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
89 for (i = 0; ino && i < chain_pos; i++) {
90 fh_bh = affs_bread(sb, ino);
91 if (!fh_bh) {
92 affs_error(sb, "readdir","Cannot read block %d", i);
Fabian Frederickd40c4d42014-04-07 15:39:00 -070093 error = -EIO;
94 goto out_brelse_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
97 affs_brelse(fh_bh);
98 fh_bh = NULL;
99 }
100 if (ino)
101 goto inside;
102 hash_pos++;
103
104 for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
105 ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
106 if (!ino)
107 continue;
Al Viro0edf9772013-05-17 17:44:42 -0400108 ctx->pos = (hash_pos << 16) + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109inside:
110 do {
111 fh_bh = affs_bread(sb, ino);
112 if (!fh_bh) {
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700113 affs_error(sb, "readdir",
114 "Cannot read block %d", ino);
Al Viro0edf9772013-05-17 17:44:42 -0400115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
Fabian Frederickf1578532015-02-17 13:46:23 -0800118 namelen = min(AFFS_TAIL(sb, fh_bh)->name[0],
119 (u8)AFFSNAMEMAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 name = AFFS_TAIL(sb, fh_bh)->name + 1;
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -0800121 pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n",
122 namelen, name, ino, hash_pos, ctx->pos);
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700123
Al Viro0edf9772013-05-17 17:44:42 -0400124 if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700125 goto done;
Al Viro0edf9772013-05-17 17:44:42 -0400126 ctx->pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
128 affs_brelse(fh_bh);
129 fh_bh = NULL;
130 } while (ino);
131 }
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700132done:
Al Viro0edf9772013-05-17 17:44:42 -0400133 file->f_version = inode->i_version;
134 file->private_data = (void *)(long)ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 affs_brelse(fh_bh);
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700136
137out_brelse_dir:
138 affs_brelse(dir_bh);
139
140out_unlock_dir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 affs_unlock_dir(inode);
Fabian Frederickd40c4d42014-04-07 15:39:00 -0700142 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}