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