Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jeff Layton | 9dffe56 | 2017-12-11 06:35:10 -0500 | [diff] [blame] | 17 | #include <linux/iversion.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "affs.h" |
| 19 | |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 20 | static int affs_readdir(struct file *, struct dir_context *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 22 | const struct file_operations affs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | .read = generic_read_dir, |
Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 24 | .llseek = generic_file_llseek, |
Al Viro | 3b0a3c1 | 2016-04-20 23:42:46 -0400 | [diff] [blame] | 25 | .iterate_shared = affs_readdir, |
Al Viro | c475879 | 2009-06-08 01:22:00 -0400 | [diff] [blame] | 26 | .fsync = affs_file_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | /* |
| 30 | * directories can handle most operations... |
| 31 | */ |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 32 | const struct inode_operations affs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Fabian Frederick | c618402 | 2017-05-05 20:51:42 +0200 | [diff] [blame] | 40 | .rename = affs_rename2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | .setattr = affs_notify_change, |
| 42 | }; |
| 43 | |
| 44 | static int |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 45 | affs_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 47 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct super_block *sb = inode->i_sb; |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 49 | struct buffer_head *dir_bh = NULL; |
| 50 | struct buffer_head *fh_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | unsigned char *name; |
| 52 | int namelen; |
| 53 | u32 i; |
| 54 | int hash_pos; |
| 55 | int chain_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | u32 ino; |
Fabian Frederick | d40c4d4 | 2014-04-07 15:39:00 -0700 | [diff] [blame] | 57 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Geert Uytterhoeven | 08fe100 | 2015-02-17 13:46:10 -0800 | [diff] [blame] | 59 | pr_debug("%s(ino=%lu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 61 | if (ctx->pos < 2) { |
| 62 | file->private_data = (void *)0; |
| 63 | if (!dir_emit_dots(file, ctx)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | affs_lock_dir(inode); |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 68 | chain_pos = (ctx->pos - 2) & 0xffff; |
| 69 | hash_pos = (ctx->pos - 2) >> 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if (chain_pos == 0xffff) { |
| 71 | affs_warning(sb, "readdir", "More than 65535 entries in chain"); |
| 72 | chain_pos = 0; |
| 73 | hash_pos++; |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 74 | ctx->pos = ((hash_pos << 16) | chain_pos) + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | dir_bh = affs_bread(sb, inode->i_ino); |
| 77 | if (!dir_bh) |
Fabian Frederick | d40c4d4 | 2014-04-07 15:39:00 -0700 | [diff] [blame] | 78 | goto out_unlock_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 83 | ino = (u32)(long)file->private_data; |
Goffredo Baroncelli | c472c07 | 2018-02-01 08:15:25 -0500 | [diff] [blame] | 84 | if (ino && inode_eq_iversion(inode, file->f_version)) { |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 85 | pr_debug("readdir() left off=%d\n", ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Fabian Frederick | d40c4d4 | 2014-04-07 15:39:00 -0700 | [diff] [blame] | 94 | error = -EIO; |
| 95 | goto out_brelse_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 109 | ctx->pos = (hash_pos << 16) + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | inside: |
| 111 | do { |
| 112 | fh_bh = affs_bread(sb, ino); |
| 113 | if (!fh_bh) { |
Fabian Frederick | d40c4d4 | 2014-04-07 15:39:00 -0700 | [diff] [blame] | 114 | affs_error(sb, "readdir", |
| 115 | "Cannot read block %d", ino); |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 116 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Fabian Frederick | f157853 | 2015-02-17 13:46:23 -0800 | [diff] [blame] | 119 | namelen = min(AFFS_TAIL(sb, fh_bh)->name[0], |
| 120 | (u8)AFFSNAMEMAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | name = AFFS_TAIL(sb, fh_bh)->name + 1; |
Geert Uytterhoeven | 08fe100 | 2015-02-17 13:46:10 -0800 | [diff] [blame] | 122 | pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n", |
| 123 | namelen, name, ino, hash_pos, ctx->pos); |
Fabian Frederick | d40c4d4 | 2014-04-07 15:39:00 -0700 | [diff] [blame] | 124 | |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 125 | if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN)) |
Fabian Frederick | d40c4d4 | 2014-04-07 15:39:00 -0700 | [diff] [blame] | 126 | goto done; |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 127 | ctx->pos++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Fabian Frederick | d40c4d4 | 2014-04-07 15:39:00 -0700 | [diff] [blame] | 133 | done: |
Jeff Layton | 9dffe56 | 2017-12-11 06:35:10 -0500 | [diff] [blame] | 134 | file->f_version = inode_query_iversion(inode); |
Al Viro | 0edf977 | 2013-05-17 17:44:42 -0400 | [diff] [blame] | 135 | file->private_data = (void *)(long)ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | affs_brelse(fh_bh); |
Fabian Frederick | d40c4d4 | 2014-04-07 15:39:00 -0700 | [diff] [blame] | 137 | |
| 138 | out_brelse_dir: |
| 139 | affs_brelse(dir_bh); |
| 140 | |
| 141 | out_unlock_dir: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | affs_unlock_dir(inode); |
Fabian Frederick | d40c4d4 | 2014-04-07 15:39:00 -0700 | [diff] [blame] | 143 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |