Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/adfs/dir.c |
| 3 | * |
| 4 | * Copyright (C) 1999-2000 Russell King |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * Common directory handling for ADFS |
| 11 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include "adfs.h" |
| 13 | |
| 14 | /* |
| 15 | * For future. This should probably be per-directory. |
| 16 | */ |
| 17 | static DEFINE_RWLOCK(adfs_dir_lock); |
| 18 | |
| 19 | static int |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 20 | adfs_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 22 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | struct super_block *sb = inode->i_sb; |
Julia Lawall | 0125f50 | 2015-11-21 16:15:37 +0100 | [diff] [blame] | 24 | const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | struct object_info obj; |
| 26 | struct adfs_dir dir; |
| 27 | int ret = 0; |
| 28 | |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 29 | if (ctx->pos >> 32) |
| 30 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | ret = ops->read(sb, inode->i_ino, inode->i_size, &dir); |
| 33 | if (ret) |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 34 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 36 | if (ctx->pos == 0) { |
| 37 | if (!dir_emit_dot(file, ctx)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | goto free_out; |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 39 | ctx->pos = 1; |
| 40 | } |
| 41 | if (ctx->pos == 1) { |
| 42 | if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | goto free_out; |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 44 | ctx->pos = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | read_lock(&adfs_dir_lock); |
| 48 | |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 49 | ret = ops->setpos(&dir, ctx->pos - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | if (ret) |
| 51 | goto unlock_out; |
| 52 | while (ops->getnext(&dir, &obj) == 0) { |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 53 | if (!dir_emit(ctx, obj.name, obj.name_len, |
| 54 | obj.file_id, DT_UNKNOWN)) |
| 55 | break; |
| 56 | ctx->pos++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | unlock_out: |
| 60 | read_unlock(&adfs_dir_lock); |
| 61 | |
| 62 | free_out: |
| 63 | ops->free(&dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | int |
Al Viro | ffdc906 | 2009-06-08 00:44:42 -0400 | [diff] [blame] | 68 | adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | int ret = -EINVAL; |
| 71 | #ifdef CONFIG_ADFS_FS_RW |
Julia Lawall | 0125f50 | 2015-11-21 16:15:37 +0100 | [diff] [blame] | 72 | const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct adfs_dir dir; |
| 74 | |
| 75 | printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n", |
| 76 | obj->file_id, obj->parent_id); |
| 77 | |
| 78 | if (!ops->update) { |
| 79 | ret = -EINVAL; |
| 80 | goto out; |
| 81 | } |
| 82 | |
| 83 | ret = ops->read(sb, obj->parent_id, 0, &dir); |
| 84 | if (ret) |
| 85 | goto out; |
| 86 | |
| 87 | write_lock(&adfs_dir_lock); |
| 88 | ret = ops->update(&dir, obj); |
| 89 | write_unlock(&adfs_dir_lock); |
| 90 | |
Al Viro | ffdc906 | 2009-06-08 00:44:42 -0400 | [diff] [blame] | 91 | if (wait) { |
| 92 | int err = ops->sync(&dir); |
| 93 | if (!ret) |
| 94 | ret = err; |
| 95 | } |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | ops->free(&dir); |
| 98 | out: |
| 99 | #endif |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static int |
Al Viro | 19a6d89 | 2016-07-20 23:23:32 -0400 | [diff] [blame] | 104 | adfs_match(const struct qstr *name, struct object_info *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | int i; |
| 107 | |
| 108 | if (name->len != obj->name_len) |
| 109 | return 0; |
| 110 | |
| 111 | for (i = 0; i < name->len; i++) { |
| 112 | char c1, c2; |
| 113 | |
| 114 | c1 = name->name[i]; |
| 115 | c2 = obj->name[i]; |
| 116 | |
| 117 | if (c1 >= 'A' && c1 <= 'Z') |
| 118 | c1 += 'a' - 'A'; |
| 119 | if (c2 >= 'A' && c2 <= 'Z') |
| 120 | c2 += 'a' - 'A'; |
| 121 | |
| 122 | if (c1 != c2) |
| 123 | return 0; |
| 124 | } |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | static int |
Al Viro | 19a6d89 | 2016-07-20 23:23:32 -0400 | [diff] [blame] | 129 | adfs_dir_lookup_byname(struct inode *inode, const struct qstr *name, struct object_info *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
| 131 | struct super_block *sb = inode->i_sb; |
Julia Lawall | 0125f50 | 2015-11-21 16:15:37 +0100 | [diff] [blame] | 132 | const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | struct adfs_dir dir; |
| 134 | int ret; |
| 135 | |
| 136 | ret = ops->read(sb, inode->i_ino, inode->i_size, &dir); |
| 137 | if (ret) |
| 138 | goto out; |
| 139 | |
| 140 | if (ADFS_I(inode)->parent_id != dir.parent_id) { |
Joe Perches | 19bdd41a5 | 2014-08-08 14:22:26 -0700 | [diff] [blame] | 141 | adfs_error(sb, "parent directory changed under me! (%lx but got %x)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | ADFS_I(inode)->parent_id, dir.parent_id); |
| 143 | ret = -EIO; |
| 144 | goto free_out; |
| 145 | } |
| 146 | |
| 147 | obj->parent_id = inode->i_ino; |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | read_lock(&adfs_dir_lock); |
| 150 | |
| 151 | ret = ops->setpos(&dir, 0); |
| 152 | if (ret) |
| 153 | goto unlock_out; |
| 154 | |
| 155 | ret = -ENOENT; |
| 156 | while (ops->getnext(&dir, obj) == 0) { |
| 157 | if (adfs_match(name, obj)) { |
| 158 | ret = 0; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | unlock_out: |
| 164 | read_unlock(&adfs_dir_lock); |
| 165 | |
| 166 | free_out: |
| 167 | ops->free(&dir); |
| 168 | out: |
| 169 | return ret; |
| 170 | } |
| 171 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 172 | const struct file_operations adfs_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | .read = generic_read_dir, |
Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 174 | .llseek = generic_file_llseek, |
Al Viro | 2638ffb | 2013-05-17 17:30:10 -0400 | [diff] [blame] | 175 | .iterate = adfs_readdir, |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 176 | .fsync = generic_file_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | static int |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 180 | adfs_hash(const struct dentry *parent, struct qstr *qstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
| 182 | const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen; |
| 183 | const unsigned char *name; |
| 184 | unsigned long hash; |
| 185 | int i; |
| 186 | |
| 187 | if (qstr->len < name_len) |
| 188 | return 0; |
| 189 | |
| 190 | /* |
| 191 | * Truncate the name in place, avoids |
| 192 | * having to define a compare function. |
| 193 | */ |
| 194 | qstr->len = i = name_len; |
| 195 | name = qstr->name; |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 196 | hash = init_name_hash(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | while (i--) { |
| 198 | char c; |
| 199 | |
| 200 | c = *name++; |
| 201 | if (c >= 'A' && c <= 'Z') |
| 202 | c += 'a' - 'A'; |
| 203 | |
| 204 | hash = partial_name_hash(c, hash); |
| 205 | } |
| 206 | qstr->hash = end_name_hash(hash); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Compare two names, taking note of the name length |
| 213 | * requirements of the underlying filesystem. |
| 214 | */ |
| 215 | static int |
Al Viro | 6fa67e7 | 2016-07-31 16:37:25 -0400 | [diff] [blame] | 216 | adfs_compare(const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 217 | unsigned int len, const char *str, const struct qstr *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | int i; |
| 220 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 221 | if (len != name->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return 1; |
| 223 | |
| 224 | for (i = 0; i < name->len; i++) { |
| 225 | char a, b; |
| 226 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 227 | a = str[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | b = name->name[i]; |
| 229 | |
| 230 | if (a >= 'A' && a <= 'Z') |
| 231 | a += 'a' - 'A'; |
| 232 | if (b >= 'A' && b <= 'Z') |
| 233 | b += 'a' - 'A'; |
| 234 | |
| 235 | if (a != b) |
| 236 | return 1; |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
Al Viro | e16404e | 2009-02-20 05:55:13 +0000 | [diff] [blame] | 241 | const struct dentry_operations adfs_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | .d_hash = adfs_hash, |
| 243 | .d_compare = adfs_compare, |
| 244 | }; |
| 245 | |
| 246 | static struct dentry * |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 247 | adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
| 249 | struct inode *inode = NULL; |
| 250 | struct object_info obj; |
| 251 | int error; |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj); |
| 254 | if (error == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | /* |
| 256 | * This only returns NULL if get_empty_inode |
| 257 | * fails. |
| 258 | */ |
| 259 | inode = adfs_iget(dir->i_sb, &obj); |
Al Viro | 9a7dddc | 2018-04-30 22:57:42 -0400 | [diff] [blame] | 260 | if (!inode) |
| 261 | inode = ERR_PTR(-EACCES); |
| 262 | } else if (error != -ENOENT) { |
| 263 | inode = ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
Al Viro | 9a7dddc | 2018-04-30 22:57:42 -0400 | [diff] [blame] | 265 | return d_splice_alias(inode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * directories can handle most operations... |
| 270 | */ |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 271 | const struct inode_operations adfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | .lookup = adfs_lookup, |
| 273 | .setattr = adfs_notify_change, |
| 274 | }; |