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/amigaffs.c |
| 4 | * |
| 5 | * (c) 1996 Hans-Joachim Widmaier - Rewritten |
| 6 | * |
| 7 | * (C) 1993 Ray Burr - Amiga FFS filesystem. |
| 8 | * |
| 9 | * Please send bug reports to: hjw@zvw.de |
| 10 | */ |
| 11 | |
DengChao | db39c16 | 2015-11-12 21:40:41 +0800 | [diff] [blame] | 12 | #include <linux/math64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "affs.h" |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | /* |
| 16 | * Functions for accessing Amiga-FFS structures. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | /* Insert a header block bh into the directory dir |
| 21 | * caller must hold AFFS_DIR->i_hash_lock! |
| 22 | */ |
| 23 | |
| 24 | int |
| 25 | affs_insert_hash(struct inode *dir, struct buffer_head *bh) |
| 26 | { |
| 27 | struct super_block *sb = dir->i_sb; |
| 28 | struct buffer_head *dir_bh; |
| 29 | u32 ino, hash_ino; |
| 30 | int offset; |
| 31 | |
| 32 | ino = bh->b_blocknr; |
| 33 | offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]); |
| 34 | |
Geert Uytterhoeven | 08fe100 | 2015-02-17 13:46:10 -0800 | [diff] [blame] | 35 | pr_debug("%s(dir=%lu, ino=%d)\n", __func__, dir->i_ino, ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | dir_bh = affs_bread(sb, dir->i_ino); |
| 38 | if (!dir_bh) |
| 39 | return -EIO; |
| 40 | |
| 41 | hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]); |
| 42 | while (hash_ino) { |
| 43 | affs_brelse(dir_bh); |
| 44 | dir_bh = affs_bread(sb, hash_ino); |
| 45 | if (!dir_bh) |
| 46 | return -EIO; |
| 47 | hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain); |
| 48 | } |
| 49 | AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino); |
| 50 | AFFS_TAIL(sb, bh)->hash_chain = 0; |
| 51 | affs_fix_checksum(sb, bh); |
| 52 | |
| 53 | if (dir->i_ino == dir_bh->b_blocknr) |
| 54 | AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino); |
| 55 | else |
| 56 | AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino); |
| 57 | |
| 58 | affs_adjust_checksum(dir_bh, ino); |
| 59 | mark_buffer_dirty_inode(dir_bh, dir); |
| 60 | affs_brelse(dir_bh); |
| 61 | |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 62 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | dir->i_version++; |
| 64 | mark_inode_dirty(dir); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* Remove a header block from its directory. |
| 70 | * caller must hold AFFS_DIR->i_hash_lock! |
| 71 | */ |
| 72 | |
| 73 | int |
| 74 | affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh) |
| 75 | { |
| 76 | struct super_block *sb; |
| 77 | struct buffer_head *bh; |
| 78 | u32 rem_ino, hash_ino; |
| 79 | __be32 ino; |
| 80 | int offset, retval; |
| 81 | |
| 82 | sb = dir->i_sb; |
| 83 | rem_ino = rem_bh->b_blocknr; |
| 84 | offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]); |
Geert Uytterhoeven | 08fe100 | 2015-02-17 13:46:10 -0800 | [diff] [blame] | 85 | pr_debug("%s(dir=%lu, ino=%d, hashval=%d)\n", __func__, dir->i_ino, |
| 86 | rem_ino, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | bh = affs_bread(sb, dir->i_ino); |
| 89 | if (!bh) |
| 90 | return -EIO; |
| 91 | |
| 92 | retval = -ENOENT; |
| 93 | hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]); |
| 94 | while (hash_ino) { |
| 95 | if (hash_ino == rem_ino) { |
| 96 | ino = AFFS_TAIL(sb, rem_bh)->hash_chain; |
| 97 | if (dir->i_ino == bh->b_blocknr) |
| 98 | AFFS_HEAD(bh)->table[offset] = ino; |
| 99 | else |
| 100 | AFFS_TAIL(sb, bh)->hash_chain = ino; |
| 101 | affs_adjust_checksum(bh, be32_to_cpu(ino) - hash_ino); |
| 102 | mark_buffer_dirty_inode(bh, dir); |
| 103 | AFFS_TAIL(sb, rem_bh)->parent = 0; |
| 104 | retval = 0; |
| 105 | break; |
| 106 | } |
| 107 | affs_brelse(bh); |
| 108 | bh = affs_bread(sb, hash_ino); |
| 109 | if (!bh) |
| 110 | return -EIO; |
| 111 | hash_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain); |
| 112 | } |
| 113 | |
| 114 | affs_brelse(bh); |
| 115 | |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 116 | dir->i_mtime = dir->i_ctime = current_time(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | dir->i_version++; |
| 118 | mark_inode_dirty(dir); |
| 119 | |
| 120 | return retval; |
| 121 | } |
| 122 | |
| 123 | static void |
Al Viro | 12447c4 | 2012-06-09 13:06:09 -0400 | [diff] [blame] | 124 | affs_fix_dcache(struct inode *inode, u32 entry_ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Al Viro | 12447c4 | 2012-06-09 13:06:09 -0400 | [diff] [blame] | 126 | struct dentry *dentry; |
Nick Piggin | 873feea | 2011-01-07 17:50:06 +1100 | [diff] [blame] | 127 | spin_lock(&inode->i_lock); |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 128 | hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (entry_ino == (u32)(long)dentry->d_fsdata) { |
Al Viro | 12447c4 | 2012-06-09 13:06:09 -0400 | [diff] [blame] | 130 | dentry->d_fsdata = (void *)inode->i_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | break; |
| 132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
Nick Piggin | 873feea | 2011-01-07 17:50:06 +1100 | [diff] [blame] | 134 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
| 138 | /* Remove header from link chain */ |
| 139 | |
| 140 | static int |
| 141 | affs_remove_link(struct dentry *dentry) |
| 142 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 143 | struct inode *dir, *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | struct super_block *sb = inode->i_sb; |
Fabian Frederick | 4709187 | 2015-06-30 14:57:58 -0700 | [diff] [blame] | 145 | struct buffer_head *bh, *link_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | u32 link_ino, ino; |
| 147 | int retval; |
| 148 | |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 149 | pr_debug("%s(key=%ld)\n", __func__, inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | retval = -EIO; |
| 151 | bh = affs_bread(sb, inode->i_ino); |
| 152 | if (!bh) |
| 153 | goto done; |
| 154 | |
| 155 | link_ino = (u32)(long)dentry->d_fsdata; |
| 156 | if (inode->i_ino == link_ino) { |
| 157 | /* we can't remove the head of the link, as its blocknr is still used as ino, |
| 158 | * so we remove the block of the first link instead. |
| 159 | */ |
| 160 | link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain); |
| 161 | link_bh = affs_bread(sb, link_ino); |
| 162 | if (!link_bh) |
| 163 | goto done; |
| 164 | |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 165 | dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent)); |
| 166 | if (IS_ERR(dir)) { |
| 167 | retval = PTR_ERR(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | goto done; |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | affs_lock_dir(dir); |
Al Viro | 12447c4 | 2012-06-09 13:06:09 -0400 | [diff] [blame] | 172 | /* |
| 173 | * if there's a dentry for that block, make it |
| 174 | * refer to inode itself. |
| 175 | */ |
| 176 | affs_fix_dcache(inode, link_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | retval = affs_remove_hash(dir, link_bh); |
Christoph Hellwig | ec1ab0a | 2008-05-09 12:35:29 +0200 | [diff] [blame] | 178 | if (retval) { |
| 179 | affs_unlock_dir(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | goto done; |
Christoph Hellwig | ec1ab0a | 2008-05-09 12:35:29 +0200 | [diff] [blame] | 181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | mark_buffer_dirty_inode(link_bh, inode); |
| 183 | |
| 184 | memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32); |
| 185 | retval = affs_insert_hash(dir, bh); |
Christoph Hellwig | ec1ab0a | 2008-05-09 12:35:29 +0200 | [diff] [blame] | 186 | if (retval) { |
| 187 | affs_unlock_dir(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | goto done; |
Christoph Hellwig | ec1ab0a | 2008-05-09 12:35:29 +0200 | [diff] [blame] | 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | mark_buffer_dirty_inode(bh, inode); |
| 191 | |
| 192 | affs_unlock_dir(dir); |
| 193 | iput(dir); |
| 194 | } else { |
| 195 | link_bh = affs_bread(sb, link_ino); |
| 196 | if (!link_bh) |
| 197 | goto done; |
| 198 | } |
| 199 | |
| 200 | while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) { |
| 201 | if (ino == link_ino) { |
| 202 | __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain; |
| 203 | AFFS_TAIL(sb, bh)->link_chain = ino2; |
| 204 | affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino); |
| 205 | mark_buffer_dirty_inode(bh, inode); |
| 206 | retval = 0; |
| 207 | /* Fix the link count, if bh is a normal header block without links */ |
| 208 | switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) { |
| 209 | case ST_LINKDIR: |
| 210 | case ST_LINKFILE: |
| 211 | break; |
| 212 | default: |
| 213 | if (!AFFS_TAIL(sb, bh)->link_chain) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 214 | set_nlink(inode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | affs_free_block(sb, link_ino); |
| 217 | goto done; |
| 218 | } |
| 219 | affs_brelse(bh); |
| 220 | bh = affs_bread(sb, ino); |
| 221 | if (!bh) |
| 222 | goto done; |
| 223 | } |
| 224 | retval = -ENOENT; |
| 225 | done: |
| 226 | affs_brelse(link_bh); |
| 227 | affs_brelse(bh); |
| 228 | return retval; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | static int |
| 233 | affs_empty_dir(struct inode *inode) |
| 234 | { |
| 235 | struct super_block *sb = inode->i_sb; |
| 236 | struct buffer_head *bh; |
| 237 | int retval, size; |
| 238 | |
| 239 | retval = -EIO; |
| 240 | bh = affs_bread(sb, inode->i_ino); |
| 241 | if (!bh) |
| 242 | goto done; |
| 243 | |
| 244 | retval = -ENOTEMPTY; |
| 245 | for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--) |
| 246 | if (AFFS_HEAD(bh)->table[size]) |
| 247 | goto not_empty; |
| 248 | retval = 0; |
| 249 | not_empty: |
| 250 | affs_brelse(bh); |
| 251 | done: |
| 252 | return retval; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /* Remove a filesystem object. If the object to be removed has |
| 257 | * links to it, one of the links must be changed to inherit |
| 258 | * the file or directory. As above, any inode will do. |
| 259 | * The buffer will not be freed. If the header is a link, the |
| 260 | * block will be marked as free. |
| 261 | * This function returns a negative error number in case of |
| 262 | * an error, else 0 if the inode is to be deleted or 1 if not. |
| 263 | */ |
| 264 | |
| 265 | int |
| 266 | affs_remove_header(struct dentry *dentry) |
| 267 | { |
| 268 | struct super_block *sb; |
| 269 | struct inode *inode, *dir; |
| 270 | struct buffer_head *bh = NULL; |
| 271 | int retval; |
| 272 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 273 | dir = d_inode(dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | sb = dir->i_sb; |
| 275 | |
| 276 | retval = -ENOENT; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 277 | inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (!inode) |
| 279 | goto done; |
| 280 | |
Fabian Frederick | 9606d9a | 2014-06-06 14:37:25 -0700 | [diff] [blame] | 281 | pr_debug("%s(key=%ld)\n", __func__, inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | retval = -EIO; |
| 283 | bh = affs_bread(sb, (u32)(long)dentry->d_fsdata); |
| 284 | if (!bh) |
| 285 | goto done; |
| 286 | |
| 287 | affs_lock_link(inode); |
| 288 | affs_lock_dir(dir); |
| 289 | switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) { |
| 290 | case ST_USERDIR: |
| 291 | /* if we ever want to support links to dirs |
| 292 | * i_hash_lock of the inode must only be |
| 293 | * taken after some checks |
| 294 | */ |
| 295 | affs_lock_dir(inode); |
| 296 | retval = affs_empty_dir(inode); |
| 297 | affs_unlock_dir(inode); |
| 298 | if (retval) |
| 299 | goto done_unlock; |
| 300 | break; |
| 301 | default: |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | retval = affs_remove_hash(dir, bh); |
| 306 | if (retval) |
| 307 | goto done_unlock; |
| 308 | mark_buffer_dirty_inode(bh, inode); |
| 309 | |
| 310 | affs_unlock_dir(dir); |
| 311 | |
| 312 | if (inode->i_nlink > 1) |
| 313 | retval = affs_remove_link(dentry); |
| 314 | else |
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 | affs_unlock_link(inode); |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 317 | inode->i_ctime = current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | mark_inode_dirty(inode); |
| 319 | |
| 320 | done: |
| 321 | affs_brelse(bh); |
| 322 | return retval; |
| 323 | |
| 324 | done_unlock: |
| 325 | affs_unlock_dir(dir); |
| 326 | affs_unlock_link(inode); |
| 327 | goto done; |
| 328 | } |
| 329 | |
| 330 | /* Checksum a block, do various consistency checks and optionally return |
| 331 | the blocks type number. DATA points to the block. If their pointers |
| 332 | are non-null, *PTYPE and *STYPE are set to the primary and secondary |
| 333 | block types respectively, *HASHSIZE is set to the size of the hashtable |
| 334 | (which lets us calculate the block size). |
| 335 | Returns non-zero if the block is not consistent. */ |
| 336 | |
| 337 | u32 |
| 338 | affs_checksum_block(struct super_block *sb, struct buffer_head *bh) |
| 339 | { |
| 340 | __be32 *ptr = (__be32 *)bh->b_data; |
| 341 | u32 sum; |
| 342 | int bsize; |
| 343 | |
| 344 | sum = 0; |
| 345 | for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--) |
| 346 | sum += be32_to_cpu(*ptr++); |
| 347 | return sum; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Calculate the checksum of a disk block and store it |
| 352 | * at the indicated position. |
| 353 | */ |
| 354 | |
| 355 | void |
| 356 | affs_fix_checksum(struct super_block *sb, struct buffer_head *bh) |
| 357 | { |
| 358 | int cnt = sb->s_blocksize / sizeof(__be32); |
| 359 | __be32 *ptr = (__be32 *)bh->b_data; |
| 360 | u32 checksum; |
| 361 | __be32 *checksumptr; |
| 362 | |
| 363 | checksumptr = ptr + 5; |
| 364 | *checksumptr = 0; |
| 365 | for (checksum = 0; cnt > 0; ptr++, cnt--) |
| 366 | checksum += be32_to_cpu(*ptr); |
| 367 | *checksumptr = cpu_to_be32(-checksum); |
| 368 | } |
| 369 | |
| 370 | void |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 371 | affs_secs_to_datestamp(time64_t secs, struct affs_date *ds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
| 373 | u32 days; |
| 374 | u32 minute; |
DengChao | db39c16 | 2015-11-12 21:40:41 +0800 | [diff] [blame] | 375 | s32 rem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
| 377 | secs -= sys_tz.tz_minuteswest * 60 + ((8 * 365 + 2) * 24 * 60 * 60); |
| 378 | if (secs < 0) |
| 379 | secs = 0; |
DengChao | db39c16 | 2015-11-12 21:40:41 +0800 | [diff] [blame] | 380 | days = div_s64_rem(secs, 86400, &rem); |
| 381 | minute = rem / 60; |
| 382 | rem -= minute * 60; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | ds->days = cpu_to_be32(days); |
| 385 | ds->mins = cpu_to_be32(minute); |
DengChao | db39c16 | 2015-11-12 21:40:41 +0800 | [diff] [blame] | 386 | ds->ticks = cpu_to_be32(rem * 50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Al Viro | a760b03 | 2011-07-26 03:04:30 -0400 | [diff] [blame] | 389 | umode_t |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 390 | affs_prot_to_mode(u32 prot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
Al Viro | a760b03 | 2011-07-26 03:04:30 -0400 | [diff] [blame] | 392 | umode_t mode = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | if (!(prot & FIBF_NOWRITE)) |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 395 | mode |= 0200; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | if (!(prot & FIBF_NOREAD)) |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 397 | mode |= 0400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (!(prot & FIBF_NOEXECUTE)) |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 399 | mode |= 0100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (prot & FIBF_GRP_WRITE) |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 401 | mode |= 0020; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | if (prot & FIBF_GRP_READ) |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 403 | mode |= 0040; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | if (prot & FIBF_GRP_EXECUTE) |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 405 | mode |= 0010; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | if (prot & FIBF_OTR_WRITE) |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 407 | mode |= 0002; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | if (prot & FIBF_OTR_READ) |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 409 | mode |= 0004; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (prot & FIBF_OTR_EXECUTE) |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 411 | mode |= 0001; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | return mode; |
| 414 | } |
| 415 | |
| 416 | void |
Fabian Frederick | c161820 | 2017-02-27 14:27:54 -0800 | [diff] [blame] | 417 | affs_mode_to_prot(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
| 419 | u32 prot = AFFS_I(inode)->i_protect; |
Al Viro | a760b03 | 2011-07-26 03:04:30 -0400 | [diff] [blame] | 420 | umode_t mode = inode->i_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 422 | if (!(mode & 0100)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | prot |= FIBF_NOEXECUTE; |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 424 | if (!(mode & 0400)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | prot |= FIBF_NOREAD; |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 426 | if (!(mode & 0200)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | prot |= FIBF_NOWRITE; |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 428 | if (mode & 0010) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | prot |= FIBF_GRP_EXECUTE; |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 430 | if (mode & 0040) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | prot |= FIBF_GRP_READ; |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 432 | if (mode & 0020) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | prot |= FIBF_GRP_WRITE; |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 434 | if (mode & 0001) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | prot |= FIBF_OTR_EXECUTE; |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 436 | if (mode & 0004) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | prot |= FIBF_OTR_READ; |
Fabian Frederick | 1bafd6f | 2017-02-27 14:27:52 -0800 | [diff] [blame] | 438 | if (mode & 0002) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | prot |= FIBF_OTR_WRITE; |
| 440 | |
| 441 | AFFS_I(inode)->i_protect = prot; |
| 442 | } |
| 443 | |
| 444 | void |
| 445 | affs_error(struct super_block *sb, const char *function, const char *fmt, ...) |
| 446 | { |
Fabian Frederick | 1ee54b0 | 2014-12-12 16:57:49 -0800 | [diff] [blame] | 447 | struct va_format vaf; |
| 448 | va_list args; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Fabian Frederick | 1ee54b0 | 2014-12-12 16:57:49 -0800 | [diff] [blame] | 450 | va_start(args, fmt); |
| 451 | vaf.fmt = fmt; |
| 452 | vaf.va = &args; |
| 453 | pr_crit("error (device %s): %s(): %pV\n", sb->s_id, function, &vaf); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 454 | if (!sb_rdonly(sb)) |
Fabian Frederick | 0158de1 | 2014-06-06 14:37:24 -0700 | [diff] [blame] | 455 | pr_warn("Remounting filesystem read-only\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | sb->s_flags |= MS_RDONLY; |
Fabian Frederick | 1ee54b0 | 2014-12-12 16:57:49 -0800 | [diff] [blame] | 457 | va_end(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void |
| 461 | affs_warning(struct super_block *sb, const char *function, const char *fmt, ...) |
| 462 | { |
Fabian Frederick | 1ee54b0 | 2014-12-12 16:57:49 -0800 | [diff] [blame] | 463 | struct va_format vaf; |
| 464 | va_list args; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Fabian Frederick | 1ee54b0 | 2014-12-12 16:57:49 -0800 | [diff] [blame] | 466 | va_start(args, fmt); |
| 467 | vaf.fmt = fmt; |
| 468 | vaf.va = &args; |
| 469 | pr_warn("(device %s): %s(): %pV\n", sb->s_id, function, &vaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | va_end(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 473 | bool |
| 474 | affs_nofilenametruncate(const struct dentry *dentry) |
| 475 | { |
Al Viro | e0b3f59 | 2016-07-29 18:22:49 -0400 | [diff] [blame] | 476 | return affs_test_opt(AFFS_SB(dentry->d_sb)->s_flags, SF_NO_TRUNCATE); |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | /* Check if the name is valid for a affs object. */ |
| 480 | |
| 481 | int |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 482 | affs_check_name(const unsigned char *name, int len, bool notruncate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | { |
| 484 | int i; |
| 485 | |
Fabian Frederick | f157853 | 2015-02-17 13:46:23 -0800 | [diff] [blame] | 486 | if (len > AFFSNAMEMAX) { |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 487 | if (notruncate) |
| 488 | return -ENAMETOOLONG; |
Fabian Frederick | b4478e3 | 2015-02-17 13:46:25 -0800 | [diff] [blame] | 489 | len = AFFSNAMEMAX; |
Fabian Frederick | 8ca5772 | 2014-04-07 15:39:01 -0700 | [diff] [blame] | 490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | for (i = 0; i < len; i++) { |
| 492 | if (name[i] < ' ' || name[i] == ':' |
| 493 | || (name[i] > 0x7e && name[i] < 0xa0)) |
| 494 | return -EINVAL; |
| 495 | } |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | /* This function copies name to bstr, with at most 30 |
| 501 | * characters length. The bstr will be prepended by |
| 502 | * a length byte. |
| 503 | * NOTE: The name will must be already checked by |
| 504 | * affs_check_name()! |
| 505 | */ |
| 506 | |
| 507 | int |
| 508 | affs_copy_name(unsigned char *bstr, struct dentry *dentry) |
| 509 | { |
Fabian Frederick | f157853 | 2015-02-17 13:46:23 -0800 | [diff] [blame] | 510 | u32 len = min(dentry->d_name.len, AFFSNAMEMAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | *bstr++ = len; |
| 513 | memcpy(bstr, dentry->d_name.name, len); |
| 514 | return len; |
| 515 | } |