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