Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/affs/namei.c |
| 3 | * |
| 4 | * (c) 1996 Hans-Joachim Widmaier - Rewritten |
| 5 | * |
| 6 | * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem. |
| 7 | * |
| 8 | * (C) 1991 Linus Torvalds - minix filesystem |
| 9 | */ |
| 10 | |
| 11 | #include "affs.h" |
Fabian Frederick | ed4433d | 2017-02-27 14:27:49 -0800 | [diff] [blame] | 12 | #include <linux/exportfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | typedef int (*toupper_t)(int); |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | /* Simple toupper() for DOS\1 */ |
| 17 | |
| 18 | static int |
| 19 | affs_toupper(int ch) |
| 20 | { |
| 21 | return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch; |
| 22 | } |
| 23 | |
| 24 | /* International toupper() for DOS\3 ("international") */ |
| 25 | |
| 26 | static int |
| 27 | affs_intl_toupper(int ch) |
| 28 | { |
| 29 | return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0 |
| 30 | && ch <= 0xFE && ch != 0xF7) ? |
| 31 | ch - ('a' - 'A') : ch; |
| 32 | } |
| 33 | |
| 34 | static inline toupper_t |
| 35 | affs_get_toupper(struct super_block *sb) |
| 36 | { |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 37 | return affs_test_opt(AFFS_SB(sb)->s_flags, SF_INTL) ? |
| 38 | affs_intl_toupper : affs_toupper; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Note: the dentry argument is the parent dentry. |
| 43 | */ |
| 44 | static inline int |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 45 | __affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr, toupper_t toupper, bool notruncate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | { |
| 47 | const u8 *name = qstr->name; |
| 48 | unsigned long hash; |
Fabian Frederick | eeb36f8 | 2015-02-17 13:46:20 -0800 | [diff] [blame] | 49 | int retval; |
| 50 | u32 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Fabian Frederick | eeb36f8 | 2015-02-17 13:46:20 -0800 | [diff] [blame] | 52 | retval = affs_check_name(qstr->name, qstr->len, notruncate); |
| 53 | if (retval) |
| 54 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 56 | hash = init_name_hash(dentry); |
Fabian Frederick | f157853 | 2015-02-17 13:46:23 -0800 | [diff] [blame] | 57 | len = min(qstr->len, AFFSNAMEMAX); |
Fabian Frederick | eeb36f8 | 2015-02-17 13:46:20 -0800 | [diff] [blame] | 58 | for (; len > 0; name++, len--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | hash = partial_name_hash(toupper(*name), hash); |
| 60 | qstr->hash = end_name_hash(hash); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 66 | affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 68 | return __affs_hash_dentry(dentry, qstr, affs_toupper, |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 69 | affs_nofilenametruncate(dentry)); |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static int |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 74 | affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 76 | return __affs_hash_dentry(dentry, qstr, affs_intl_toupper, |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 77 | affs_nofilenametruncate(dentry)); |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 81 | static inline int __affs_compare_dentry(unsigned int len, |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 82 | const char *str, const struct qstr *name, toupper_t toupper, |
| 83 | bool notruncate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 85 | const u8 *aname = str; |
| 86 | const u8 *bname = name->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 88 | /* |
| 89 | * 'str' is the name of an already existing dentry, so the name |
| 90 | * must be valid. 'name' must be validated first. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | */ |
| 92 | |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 93 | if (affs_check_name(name->name, name->len, notruncate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return 1; |
| 95 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 96 | /* |
| 97 | * If the names are longer than the allowed 30 chars, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * the excess is ignored, so their length may differ. |
| 99 | */ |
Fabian Frederick | f157853 | 2015-02-17 13:46:23 -0800 | [diff] [blame] | 100 | if (len >= AFFSNAMEMAX) { |
| 101 | if (name->len < AFFSNAMEMAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return 1; |
Fabian Frederick | f157853 | 2015-02-17 13:46:23 -0800 | [diff] [blame] | 103 | len = AFFSNAMEMAX; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 104 | } else if (len != name->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return 1; |
| 106 | |
| 107 | for (; len > 0; len--) |
| 108 | if (toupper(*aname++) != toupper(*bname++)) |
| 109 | return 1; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int |
Al Viro | 6fa67e7 | 2016-07-31 16:37:25 -0400 | [diff] [blame] | 115 | affs_compare_dentry(const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 116 | unsigned int len, const char *str, const struct qstr *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 118 | |
| 119 | return __affs_compare_dentry(len, str, name, affs_toupper, |
Al Viro | e0b3f59 | 2016-07-29 18:22:49 -0400 | [diff] [blame] | 120 | affs_nofilenametruncate(dentry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | static int |
Al Viro | 6fa67e7 | 2016-07-31 16:37:25 -0400 | [diff] [blame] | 124 | affs_intl_compare_dentry(const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 125 | unsigned int len, const char *str, const struct qstr *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 127 | return __affs_compare_dentry(len, str, name, affs_intl_toupper, |
Al Viro | e0b3f59 | 2016-07-29 18:22:49 -0400 | [diff] [blame] | 128 | affs_nofilenametruncate(dentry)); |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* |
| 133 | * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure. |
| 134 | */ |
| 135 | |
| 136 | static inline int |
| 137 | affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper) |
| 138 | { |
| 139 | const u8 *name = dentry->d_name.name; |
| 140 | int len = dentry->d_name.len; |
| 141 | |
Fabian Frederick | f157853 | 2015-02-17 13:46:23 -0800 | [diff] [blame] | 142 | if (len >= AFFSNAMEMAX) { |
| 143 | if (*name2 < AFFSNAMEMAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return 0; |
Fabian Frederick | f157853 | 2015-02-17 13:46:23 -0800 | [diff] [blame] | 145 | len = AFFSNAMEMAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } else if (len != *name2) |
| 147 | return 0; |
| 148 | |
| 149 | for (name2++; len > 0; len--) |
| 150 | if (toupper(*name++) != toupper(*name2++)) |
| 151 | return 0; |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | int |
| 156 | affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len) |
| 157 | { |
| 158 | toupper_t toupper = affs_get_toupper(sb); |
Fabian Frederick | eeb36f8 | 2015-02-17 13:46:20 -0800 | [diff] [blame] | 159 | u32 hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Fabian Frederick | f157853 | 2015-02-17 13:46:23 -0800 | [diff] [blame] | 161 | hash = len = min(len, AFFSNAMEMAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | for (; len > 0; len--) |
| 163 | hash = (hash * 13 + toupper(*name++)) & 0x7ff; |
| 164 | |
| 165 | return hash % AFFS_SB(sb)->s_hashsize; |
| 166 | } |
| 167 | |
| 168 | static struct buffer_head * |
| 169 | affs_find_entry(struct inode *dir, struct dentry *dentry) |
| 170 | { |
| 171 | struct super_block *sb = dir->i_sb; |
| 172 | struct buffer_head *bh; |
| 173 | toupper_t toupper = affs_get_toupper(sb); |
| 174 | u32 key; |
| 175 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 176 | pr_debug("%s(\"%pd\")\n", __func__, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | bh = affs_bread(sb, dir->i_ino); |
| 179 | if (!bh) |
| 180 | return ERR_PTR(-EIO); |
| 181 | |
| 182 | key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]); |
| 183 | |
| 184 | for (;;) { |
| 185 | affs_brelse(bh); |
| 186 | if (key == 0) |
| 187 | return NULL; |
| 188 | bh = affs_bread(sb, key); |
| 189 | if (!bh) |
| 190 | return ERR_PTR(-EIO); |
| 191 | if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper)) |
| 192 | return bh; |
| 193 | key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | struct dentry * |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 198 | affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
| 200 | struct super_block *sb = dir->i_sb; |
| 201 | struct buffer_head *bh; |
| 202 | struct inode *inode = NULL; |
| 203 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 204 | pr_debug("%s(\"%pd\")\n", __func__, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | affs_lock_dir(dir); |
| 207 | bh = affs_find_entry(dir, dentry); |
| 208 | affs_unlock_dir(dir); |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 209 | if (IS_ERR(bh)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 210 | return ERR_CAST(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (bh) { |
| 212 | u32 ino = bh->b_blocknr; |
| 213 | |
| 214 | /* store the real header ino in d_fsdata for faster lookups */ |
| 215 | dentry->d_fsdata = (void *)(long)ino; |
| 216 | switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) { |
| 217 | //link to dirs disabled |
| 218 | //case ST_LINKDIR: |
| 219 | case ST_LINKFILE: |
| 220 | ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original); |
| 221 | } |
| 222 | affs_brelse(bh); |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 223 | inode = affs_iget(sb, ino); |
| 224 | if (IS_ERR(inode)) |
Julia Lawall | cccad8f | 2010-05-26 14:44:23 -0700 | [diff] [blame] | 225 | return ERR_CAST(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | d_add(dentry, inode); |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | int |
| 232 | affs_unlink(struct inode *dir, struct dentry *dentry) |
| 233 | { |
Geert Uytterhoeven | 08fe100 | 2015-02-17 13:46:10 -0800 | [diff] [blame] | 234 | pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 235 | d_inode(dentry)->i_ino, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | return affs_remove_header(dentry); |
| 238 | } |
| 239 | |
| 240 | int |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 241 | affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
| 243 | struct super_block *sb = dir->i_sb; |
| 244 | struct inode *inode; |
| 245 | int error; |
| 246 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 247 | pr_debug("%s(%lu,\"%pd\",0%ho)\n", |
| 248 | __func__, dir->i_ino, dentry, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | inode = affs_new_inode(dir); |
| 251 | if (!inode) |
| 252 | return -ENOSPC; |
| 253 | |
| 254 | inode->i_mode = mode; |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 255 | affs_mode_to_prot(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | mark_inode_dirty(inode); |
| 257 | |
| 258 | inode->i_op = &affs_file_inode_operations; |
| 259 | inode->i_fop = &affs_file_operations; |
Fabian Frederick | 79bda4d | 2015-04-16 12:48:24 -0700 | [diff] [blame] | 260 | inode->i_mapping->a_ops = affs_test_opt(AFFS_SB(sb)->s_flags, SF_OFS) ? |
Fabian Frederick | a0016ff | 2015-04-16 12:48:15 -0700 | [diff] [blame] | 261 | &affs_aops_ofs : &affs_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | error = affs_add_entry(dir, inode, dentry, ST_FILE); |
| 263 | if (error) { |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 264 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | iput(inode); |
| 266 | return error; |
| 267 | } |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | int |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 272 | affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
| 274 | struct inode *inode; |
| 275 | int error; |
| 276 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 277 | pr_debug("%s(%lu,\"%pd\",0%ho)\n", |
| 278 | __func__, dir->i_ino, dentry, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | inode = affs_new_inode(dir); |
| 281 | if (!inode) |
| 282 | return -ENOSPC; |
| 283 | |
| 284 | inode->i_mode = S_IFDIR | mode; |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 285 | affs_mode_to_prot(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | inode->i_op = &affs_dir_inode_operations; |
| 288 | inode->i_fop = &affs_dir_operations; |
| 289 | |
| 290 | error = affs_add_entry(dir, inode, dentry, ST_USERDIR); |
| 291 | if (error) { |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 292 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | mark_inode_dirty(inode); |
| 294 | iput(inode); |
| 295 | return error; |
| 296 | } |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | int |
| 301 | affs_rmdir(struct inode *dir, struct dentry *dentry) |
| 302 | { |
Geert Uytterhoeven | 08fe100 | 2015-02-17 13:46:10 -0800 | [diff] [blame] | 303 | pr_debug("%s(dir=%lu, %lu \"%pd\")\n", __func__, dir->i_ino, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 304 | d_inode(dentry)->i_ino, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | return affs_remove_header(dentry); |
| 307 | } |
| 308 | |
| 309 | int |
| 310 | affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) |
| 311 | { |
| 312 | struct super_block *sb = dir->i_sb; |
| 313 | struct buffer_head *bh; |
| 314 | struct inode *inode; |
| 315 | char *p; |
| 316 | int i, maxlen, error; |
| 317 | char c, lc; |
| 318 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 319 | pr_debug("%s(%lu,\"%pd\" -> \"%s\")\n", |
| 320 | __func__, dir->i_ino, dentry, symname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
| 322 | maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1; |
| 323 | inode = affs_new_inode(dir); |
| 324 | if (!inode) |
| 325 | return -ENOSPC; |
| 326 | |
| 327 | inode->i_op = &affs_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 328 | inode_nohighmem(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | inode->i_data.a_ops = &affs_symlink_aops; |
| 330 | inode->i_mode = S_IFLNK | 0777; |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 331 | affs_mode_to_prot(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | error = -EIO; |
| 334 | bh = affs_bread(sb, inode->i_ino); |
| 335 | if (!bh) |
| 336 | goto err; |
| 337 | i = 0; |
| 338 | p = (char *)AFFS_HEAD(bh)->table; |
| 339 | lc = '/'; |
| 340 | if (*symname == '/') { |
Al Viro | 2933392 | 2010-01-24 00:04:07 -0500 | [diff] [blame] | 341 | struct affs_sb_info *sbi = AFFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | while (*symname == '/') |
| 343 | symname++; |
Al Viro | 2933392 | 2010-01-24 00:04:07 -0500 | [diff] [blame] | 344 | spin_lock(&sbi->symlink_lock); |
| 345 | while (sbi->s_volume[i]) /* Cannot overflow */ |
| 346 | *p++ = sbi->s_volume[i++]; |
| 347 | spin_unlock(&sbi->symlink_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | while (i < maxlen && (c = *symname++)) { |
| 350 | if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') { |
| 351 | *p++ = '/'; |
| 352 | i++; |
| 353 | symname += 2; |
| 354 | lc = '/'; |
| 355 | } else if (c == '.' && lc == '/' && *symname == '/') { |
| 356 | symname++; |
| 357 | lc = '/'; |
| 358 | } else { |
| 359 | *p++ = c; |
| 360 | lc = c; |
| 361 | i++; |
| 362 | } |
| 363 | if (lc == '/') |
| 364 | while (*symname == '/') |
| 365 | symname++; |
| 366 | } |
| 367 | *p = 0; |
Fabian Frederick | f1bf907 | 2017-04-15 08:54:36 +0200 | [diff] [blame] | 368 | inode->i_size = i + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | mark_buffer_dirty_inode(bh, inode); |
| 370 | affs_brelse(bh); |
| 371 | mark_inode_dirty(inode); |
| 372 | |
| 373 | error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK); |
| 374 | if (error) |
| 375 | goto err; |
| 376 | |
| 377 | return 0; |
| 378 | |
| 379 | err: |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 380 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | mark_inode_dirty(inode); |
| 382 | iput(inode); |
| 383 | return error; |
| 384 | } |
| 385 | |
| 386 | int |
| 387 | affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
| 388 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 389 | struct inode *inode = d_inode(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Geert Uytterhoeven | 08fe100 | 2015-02-17 13:46:10 -0800 | [diff] [blame] | 391 | pr_debug("%s(%lu, %lu, \"%pd\")\n", __func__, inode->i_ino, dir->i_ino, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 392 | dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | return affs_add_entry(dir, inode, dentry, ST_LINKFILE); |
| 395 | } |
| 396 | |
Fabian Frederick | c618402 | 2017-05-05 20:51:42 +0200 | [diff] [blame] | 397 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | affs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Fabian Frederick | c618402 | 2017-05-05 20:51:42 +0200 | [diff] [blame] | 399 | struct inode *new_dir, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { |
| 401 | struct super_block *sb = old_dir->i_sb; |
| 402 | struct buffer_head *bh = NULL; |
| 403 | int retval; |
| 404 | |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 405 | retval = affs_check_name(new_dentry->d_name.name, |
| 406 | new_dentry->d_name.len, |
| 407 | affs_nofilenametruncate(old_dentry)); |
| 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (retval) |
| 410 | return retval; |
| 411 | |
| 412 | /* Unlink destination if it already exists */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 413 | if (d_really_is_positive(new_dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | retval = affs_remove_header(new_dentry); |
| 415 | if (retval) |
| 416 | return retval; |
| 417 | } |
| 418 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 419 | bh = affs_bread(sb, d_inode(old_dentry)->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (!bh) |
Florin Malita | 3ac8141 | 2006-05-25 18:44:23 -0700 | [diff] [blame] | 421 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | /* Remove header from its parent directory. */ |
| 424 | affs_lock_dir(old_dir); |
| 425 | retval = affs_remove_hash(old_dir, bh); |
| 426 | affs_unlock_dir(old_dir); |
| 427 | if (retval) |
| 428 | goto done; |
| 429 | |
| 430 | /* And insert it into the new directory with the new name. */ |
| 431 | affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry); |
| 432 | affs_fix_checksum(sb, bh); |
| 433 | affs_lock_dir(new_dir); |
| 434 | retval = affs_insert_hash(new_dir, bh); |
| 435 | affs_unlock_dir(new_dir); |
| 436 | /* TODO: move it back to old_dir, if error? */ |
| 437 | |
| 438 | done: |
| 439 | mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir); |
| 440 | affs_brelse(bh); |
| 441 | return retval; |
| 442 | } |
Fabian Frederick | ed4433d | 2017-02-27 14:27:49 -0800 | [diff] [blame] | 443 | |
Fabian Frederick | 6b46576 | 2017-05-05 20:52:07 +0200 | [diff] [blame] | 444 | static int |
| 445 | affs_xrename(struct inode *old_dir, struct dentry *old_dentry, |
| 446 | struct inode *new_dir, struct dentry *new_dentry) |
| 447 | { |
| 448 | |
| 449 | struct super_block *sb = old_dir->i_sb; |
| 450 | struct buffer_head *bh_old = NULL; |
| 451 | struct buffer_head *bh_new = NULL; |
| 452 | int retval; |
| 453 | |
| 454 | bh_old = affs_bread(sb, d_inode(old_dentry)->i_ino); |
| 455 | if (!bh_old) |
| 456 | return -EIO; |
| 457 | |
| 458 | bh_new = affs_bread(sb, d_inode(new_dentry)->i_ino); |
| 459 | if (!bh_new) |
| 460 | return -EIO; |
| 461 | |
| 462 | /* Remove old header from its parent directory. */ |
| 463 | affs_lock_dir(old_dir); |
| 464 | retval = affs_remove_hash(old_dir, bh_old); |
| 465 | affs_unlock_dir(old_dir); |
| 466 | if (retval) |
| 467 | goto done; |
| 468 | |
| 469 | /* Remove new header from its parent directory. */ |
| 470 | affs_lock_dir(new_dir); |
| 471 | retval = affs_remove_hash(new_dir, bh_new); |
| 472 | affs_unlock_dir(new_dir); |
| 473 | if (retval) |
| 474 | goto done; |
| 475 | |
| 476 | /* Insert old into the new directory with the new name. */ |
| 477 | affs_copy_name(AFFS_TAIL(sb, bh_old)->name, new_dentry); |
| 478 | affs_fix_checksum(sb, bh_old); |
| 479 | affs_lock_dir(new_dir); |
| 480 | retval = affs_insert_hash(new_dir, bh_old); |
| 481 | affs_unlock_dir(new_dir); |
| 482 | |
| 483 | /* Insert new into the old directory with the old name. */ |
| 484 | affs_copy_name(AFFS_TAIL(sb, bh_new)->name, old_dentry); |
| 485 | affs_fix_checksum(sb, bh_new); |
| 486 | affs_lock_dir(old_dir); |
| 487 | retval = affs_insert_hash(old_dir, bh_new); |
| 488 | affs_unlock_dir(old_dir); |
| 489 | done: |
| 490 | mark_buffer_dirty_inode(bh_old, new_dir); |
| 491 | mark_buffer_dirty_inode(bh_new, old_dir); |
| 492 | affs_brelse(bh_old); |
| 493 | affs_brelse(bh_new); |
| 494 | return retval; |
| 495 | } |
| 496 | |
Fabian Frederick | c618402 | 2017-05-05 20:51:42 +0200 | [diff] [blame] | 497 | int affs_rename2(struct inode *old_dir, struct dentry *old_dentry, |
| 498 | struct inode *new_dir, struct dentry *new_dentry, |
| 499 | unsigned int flags) |
| 500 | { |
| 501 | |
Fabian Frederick | 6b46576 | 2017-05-05 20:52:07 +0200 | [diff] [blame] | 502 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) |
Fabian Frederick | c618402 | 2017-05-05 20:51:42 +0200 | [diff] [blame] | 503 | return -EINVAL; |
| 504 | |
| 505 | pr_debug("%s(old=%lu,\"%pd\" to new=%lu,\"%pd\")\n", __func__, |
| 506 | old_dir->i_ino, old_dentry, new_dir->i_ino, new_dentry); |
| 507 | |
Fabian Frederick | 6b46576 | 2017-05-05 20:52:07 +0200 | [diff] [blame] | 508 | if (flags & RENAME_EXCHANGE) |
| 509 | return affs_xrename(old_dir, old_dentry, new_dir, new_dentry); |
| 510 | |
Fabian Frederick | c618402 | 2017-05-05 20:51:42 +0200 | [diff] [blame] | 511 | return affs_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 512 | } |
| 513 | |
Fabian Frederick | b3b42c0 | 2017-02-27 14:28:00 -0800 | [diff] [blame] | 514 | static struct dentry *affs_get_parent(struct dentry *child) |
| 515 | { |
| 516 | struct inode *parent; |
| 517 | struct buffer_head *bh; |
| 518 | |
| 519 | bh = affs_bread(child->d_sb, d_inode(child)->i_ino); |
| 520 | if (!bh) |
| 521 | return ERR_PTR(-EIO); |
| 522 | |
| 523 | parent = affs_iget(child->d_sb, |
| 524 | be32_to_cpu(AFFS_TAIL(child->d_sb, bh)->parent)); |
| 525 | brelse(bh); |
| 526 | if (IS_ERR(parent)) |
| 527 | return ERR_CAST(parent); |
| 528 | |
| 529 | return d_obtain_alias(parent); |
| 530 | } |
| 531 | |
Fabian Frederick | ed4433d | 2017-02-27 14:27:49 -0800 | [diff] [blame] | 532 | static struct inode *affs_nfs_get_inode(struct super_block *sb, u64 ino, |
| 533 | u32 generation) |
| 534 | { |
| 535 | struct inode *inode; |
| 536 | |
| 537 | if (!affs_validblock(sb, ino)) |
| 538 | return ERR_PTR(-ESTALE); |
| 539 | |
| 540 | inode = affs_iget(sb, ino); |
| 541 | if (IS_ERR(inode)) |
| 542 | return ERR_CAST(inode); |
| 543 | |
Fabian Frederick | ed4433d | 2017-02-27 14:27:49 -0800 | [diff] [blame] | 544 | return inode; |
| 545 | } |
| 546 | |
| 547 | static struct dentry *affs_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 548 | int fh_len, int fh_type) |
| 549 | { |
| 550 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, |
| 551 | affs_nfs_get_inode); |
| 552 | } |
| 553 | |
| 554 | static struct dentry *affs_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 555 | int fh_len, int fh_type) |
| 556 | { |
| 557 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, |
| 558 | affs_nfs_get_inode); |
| 559 | } |
| 560 | |
| 561 | const struct export_operations affs_export_ops = { |
| 562 | .fh_to_dentry = affs_fh_to_dentry, |
| 563 | .fh_to_parent = affs_fh_to_parent, |
Fabian Frederick | b3b42c0 | 2017-02-27 14:28:00 -0800 | [diff] [blame] | 564 | .get_parent = affs_get_parent, |
Fabian Frederick | ed4433d | 2017-02-27 14:27:49 -0800 | [diff] [blame] | 565 | }; |
Fabian Frederick | f567acc | 2017-02-27 14:27:57 -0800 | [diff] [blame] | 566 | |
| 567 | const struct dentry_operations affs_dentry_operations = { |
| 568 | .d_hash = affs_hash_dentry, |
| 569 | .d_compare = affs_compare_dentry, |
| 570 | }; |
| 571 | |
| 572 | const struct dentry_operations affs_intl_dentry_operations = { |
| 573 | .d_hash = affs_intl_hash_dentry, |
| 574 | .d_compare = affs_intl_compare_dentry, |
| 575 | }; |