0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/math64.h>
0013 #include <linux/iversion.h>
0014 #include "affs.h"
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 int
0026 affs_insert_hash(struct inode *dir, struct buffer_head *bh)
0027 {
0028 struct super_block *sb = dir->i_sb;
0029 struct buffer_head *dir_bh;
0030 u32 ino, hash_ino;
0031 int offset;
0032
0033 ino = bh->b_blocknr;
0034 offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]);
0035
0036 pr_debug("%s(dir=%lu, ino=%d)\n", __func__, dir->i_ino, ino);
0037
0038 dir_bh = affs_bread(sb, dir->i_ino);
0039 if (!dir_bh)
0040 return -EIO;
0041
0042 hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
0043 while (hash_ino) {
0044 affs_brelse(dir_bh);
0045 dir_bh = affs_bread(sb, hash_ino);
0046 if (!dir_bh)
0047 return -EIO;
0048 hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain);
0049 }
0050 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
0051 AFFS_TAIL(sb, bh)->hash_chain = 0;
0052 affs_fix_checksum(sb, bh);
0053
0054 if (dir->i_ino == dir_bh->b_blocknr)
0055 AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino);
0056 else
0057 AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino);
0058
0059 affs_adjust_checksum(dir_bh, ino);
0060 mark_buffer_dirty_inode(dir_bh, dir);
0061 affs_brelse(dir_bh);
0062
0063 dir->i_mtime = dir->i_ctime = current_time(dir);
0064 inode_inc_iversion(dir);
0065 mark_inode_dirty(dir);
0066
0067 return 0;
0068 }
0069
0070
0071
0072
0073
0074 int
0075 affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh)
0076 {
0077 struct super_block *sb;
0078 struct buffer_head *bh;
0079 u32 rem_ino, hash_ino;
0080 __be32 ino;
0081 int offset, retval;
0082
0083 sb = dir->i_sb;
0084 rem_ino = rem_bh->b_blocknr;
0085 offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]);
0086 pr_debug("%s(dir=%lu, ino=%d, hashval=%d)\n", __func__, dir->i_ino,
0087 rem_ino, offset);
0088
0089 bh = affs_bread(sb, dir->i_ino);
0090 if (!bh)
0091 return -EIO;
0092
0093 retval = -ENOENT;
0094 hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]);
0095 while (hash_ino) {
0096 if (hash_ino == rem_ino) {
0097 ino = AFFS_TAIL(sb, rem_bh)->hash_chain;
0098 if (dir->i_ino == bh->b_blocknr)
0099 AFFS_HEAD(bh)->table[offset] = ino;
0100 else
0101 AFFS_TAIL(sb, bh)->hash_chain = ino;
0102 affs_adjust_checksum(bh, be32_to_cpu(ino) - hash_ino);
0103 mark_buffer_dirty_inode(bh, dir);
0104 AFFS_TAIL(sb, rem_bh)->parent = 0;
0105 retval = 0;
0106 break;
0107 }
0108 affs_brelse(bh);
0109 bh = affs_bread(sb, hash_ino);
0110 if (!bh)
0111 return -EIO;
0112 hash_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
0113 }
0114
0115 affs_brelse(bh);
0116
0117 dir->i_mtime = dir->i_ctime = current_time(dir);
0118 inode_inc_iversion(dir);
0119 mark_inode_dirty(dir);
0120
0121 return retval;
0122 }
0123
0124 static void
0125 affs_fix_dcache(struct inode *inode, u32 entry_ino)
0126 {
0127 struct dentry *dentry;
0128 spin_lock(&inode->i_lock);
0129 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
0130 if (entry_ino == (u32)(long)dentry->d_fsdata) {
0131 dentry->d_fsdata = (void *)inode->i_ino;
0132 break;
0133 }
0134 }
0135 spin_unlock(&inode->i_lock);
0136 }
0137
0138
0139
0140
0141 static int
0142 affs_remove_link(struct dentry *dentry)
0143 {
0144 struct inode *dir, *inode = d_inode(dentry);
0145 struct super_block *sb = inode->i_sb;
0146 struct buffer_head *bh, *link_bh = NULL;
0147 u32 link_ino, ino;
0148 int retval;
0149
0150 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
0151 retval = -EIO;
0152 bh = affs_bread(sb, inode->i_ino);
0153 if (!bh)
0154 goto done;
0155
0156 link_ino = (u32)(long)dentry->d_fsdata;
0157 if (inode->i_ino == link_ino) {
0158
0159
0160
0161 link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
0162 link_bh = affs_bread(sb, link_ino);
0163 if (!link_bh)
0164 goto done;
0165
0166 dir = affs_iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
0167 if (IS_ERR(dir)) {
0168 retval = PTR_ERR(dir);
0169 goto done;
0170 }
0171
0172 affs_lock_dir(dir);
0173
0174
0175
0176
0177 affs_fix_dcache(inode, link_ino);
0178 retval = affs_remove_hash(dir, link_bh);
0179 if (retval) {
0180 affs_unlock_dir(dir);
0181 goto done;
0182 }
0183 mark_buffer_dirty_inode(link_bh, inode);
0184
0185 memcpy(AFFS_TAIL(sb, bh)->name, AFFS_TAIL(sb, link_bh)->name, 32);
0186 retval = affs_insert_hash(dir, bh);
0187 if (retval) {
0188 affs_unlock_dir(dir);
0189 goto done;
0190 }
0191 mark_buffer_dirty_inode(bh, inode);
0192
0193 affs_unlock_dir(dir);
0194 iput(dir);
0195 } else {
0196 link_bh = affs_bread(sb, link_ino);
0197 if (!link_bh)
0198 goto done;
0199 }
0200
0201 while ((ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain)) != 0) {
0202 if (ino == link_ino) {
0203 __be32 ino2 = AFFS_TAIL(sb, link_bh)->link_chain;
0204 AFFS_TAIL(sb, bh)->link_chain = ino2;
0205 affs_adjust_checksum(bh, be32_to_cpu(ino2) - link_ino);
0206 mark_buffer_dirty_inode(bh, inode);
0207 retval = 0;
0208
0209 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
0210 case ST_LINKDIR:
0211 case ST_LINKFILE:
0212 break;
0213 default:
0214 if (!AFFS_TAIL(sb, bh)->link_chain)
0215 set_nlink(inode, 1);
0216 }
0217 affs_free_block(sb, link_ino);
0218 goto done;
0219 }
0220 affs_brelse(bh);
0221 bh = affs_bread(sb, ino);
0222 if (!bh)
0223 goto done;
0224 }
0225 retval = -ENOENT;
0226 done:
0227 affs_brelse(link_bh);
0228 affs_brelse(bh);
0229 return retval;
0230 }
0231
0232
0233 static int
0234 affs_empty_dir(struct inode *inode)
0235 {
0236 struct super_block *sb = inode->i_sb;
0237 struct buffer_head *bh;
0238 int retval, size;
0239
0240 retval = -EIO;
0241 bh = affs_bread(sb, inode->i_ino);
0242 if (!bh)
0243 goto done;
0244
0245 retval = -ENOTEMPTY;
0246 for (size = AFFS_SB(sb)->s_hashsize - 1; size >= 0; size--)
0247 if (AFFS_HEAD(bh)->table[size])
0248 goto not_empty;
0249 retval = 0;
0250 not_empty:
0251 affs_brelse(bh);
0252 done:
0253 return retval;
0254 }
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266 int
0267 affs_remove_header(struct dentry *dentry)
0268 {
0269 struct super_block *sb;
0270 struct inode *inode, *dir;
0271 struct buffer_head *bh = NULL;
0272 int retval;
0273
0274 dir = d_inode(dentry->d_parent);
0275 sb = dir->i_sb;
0276
0277 retval = -ENOENT;
0278 inode = d_inode(dentry);
0279 if (!inode)
0280 goto done;
0281
0282 pr_debug("%s(key=%ld)\n", __func__, inode->i_ino);
0283 retval = -EIO;
0284 bh = affs_bread(sb, (u32)(long)dentry->d_fsdata);
0285 if (!bh)
0286 goto done;
0287
0288 affs_lock_link(inode);
0289 affs_lock_dir(dir);
0290 switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) {
0291 case ST_USERDIR:
0292
0293
0294
0295
0296 affs_lock_dir(inode);
0297 retval = affs_empty_dir(inode);
0298 affs_unlock_dir(inode);
0299 if (retval)
0300 goto done_unlock;
0301 break;
0302 default:
0303 break;
0304 }
0305
0306 retval = affs_remove_hash(dir, bh);
0307 if (retval)
0308 goto done_unlock;
0309 mark_buffer_dirty_inode(bh, inode);
0310
0311 affs_unlock_dir(dir);
0312
0313 if (inode->i_nlink > 1)
0314 retval = affs_remove_link(dentry);
0315 else
0316 clear_nlink(inode);
0317 affs_unlock_link(inode);
0318 inode->i_ctime = current_time(inode);
0319 mark_inode_dirty(inode);
0320
0321 done:
0322 affs_brelse(bh);
0323 return retval;
0324
0325 done_unlock:
0326 affs_unlock_dir(dir);
0327 affs_unlock_link(inode);
0328 goto done;
0329 }
0330
0331
0332
0333
0334
0335
0336
0337
0338 u32
0339 affs_checksum_block(struct super_block *sb, struct buffer_head *bh)
0340 {
0341 __be32 *ptr = (__be32 *)bh->b_data;
0342 u32 sum;
0343 int bsize;
0344
0345 sum = 0;
0346 for (bsize = sb->s_blocksize / sizeof(__be32); bsize > 0; bsize--)
0347 sum += be32_to_cpu(*ptr++);
0348 return sum;
0349 }
0350
0351
0352
0353
0354
0355
0356 void
0357 affs_fix_checksum(struct super_block *sb, struct buffer_head *bh)
0358 {
0359 int cnt = sb->s_blocksize / sizeof(__be32);
0360 __be32 *ptr = (__be32 *)bh->b_data;
0361 u32 checksum;
0362 __be32 *checksumptr;
0363
0364 checksumptr = ptr + 5;
0365 *checksumptr = 0;
0366 for (checksum = 0; cnt > 0; ptr++, cnt--)
0367 checksum += be32_to_cpu(*ptr);
0368 *checksumptr = cpu_to_be32(-checksum);
0369 }
0370
0371 void
0372 affs_secs_to_datestamp(time64_t secs, struct affs_date *ds)
0373 {
0374 u32 days;
0375 u32 minute;
0376 s32 rem;
0377
0378 secs -= sys_tz.tz_minuteswest * 60 + AFFS_EPOCH_DELTA;
0379 if (secs < 0)
0380 secs = 0;
0381 days = div_s64_rem(secs, 86400, &rem);
0382 minute = rem / 60;
0383 rem -= minute * 60;
0384
0385 ds->days = cpu_to_be32(days);
0386 ds->mins = cpu_to_be32(minute);
0387 ds->ticks = cpu_to_be32(rem * 50);
0388 }
0389
0390 umode_t
0391 affs_prot_to_mode(u32 prot)
0392 {
0393 umode_t mode = 0;
0394
0395 if (!(prot & FIBF_NOWRITE))
0396 mode |= 0200;
0397 if (!(prot & FIBF_NOREAD))
0398 mode |= 0400;
0399 if (!(prot & FIBF_NOEXECUTE))
0400 mode |= 0100;
0401 if (prot & FIBF_GRP_WRITE)
0402 mode |= 0020;
0403 if (prot & FIBF_GRP_READ)
0404 mode |= 0040;
0405 if (prot & FIBF_GRP_EXECUTE)
0406 mode |= 0010;
0407 if (prot & FIBF_OTR_WRITE)
0408 mode |= 0002;
0409 if (prot & FIBF_OTR_READ)
0410 mode |= 0004;
0411 if (prot & FIBF_OTR_EXECUTE)
0412 mode |= 0001;
0413
0414 return mode;
0415 }
0416
0417 void
0418 affs_mode_to_prot(struct inode *inode)
0419 {
0420 u32 prot = AFFS_I(inode)->i_protect;
0421 umode_t mode = inode->i_mode;
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435 prot &= ~(FIBF_NOEXECUTE | FIBF_NOREAD
0436 | FIBF_NOWRITE | FIBF_NODELETE
0437 | FIBF_GRP_EXECUTE | FIBF_GRP_READ
0438 | FIBF_GRP_WRITE | FIBF_GRP_DELETE
0439 | FIBF_OTR_EXECUTE | FIBF_OTR_READ
0440 | FIBF_OTR_WRITE | FIBF_OTR_DELETE);
0441
0442
0443 if (!(mode & 0100))
0444 prot |= FIBF_NOEXECUTE;
0445 if (!(mode & 0400))
0446 prot |= FIBF_NOREAD;
0447 if (!(mode & 0200))
0448 prot |= FIBF_NOWRITE;
0449
0450
0451 if (mode & 0010)
0452 prot |= FIBF_GRP_EXECUTE;
0453 if (mode & 0040)
0454 prot |= FIBF_GRP_READ;
0455 if (mode & 0020)
0456 prot |= FIBF_GRP_WRITE;
0457 if (mode & 0070)
0458 prot |= FIBF_GRP_DELETE;
0459
0460 if (mode & 0001)
0461 prot |= FIBF_OTR_EXECUTE;
0462 if (mode & 0004)
0463 prot |= FIBF_OTR_READ;
0464 if (mode & 0002)
0465 prot |= FIBF_OTR_WRITE;
0466 if (mode & 0007)
0467 prot |= FIBF_OTR_DELETE;
0468
0469 AFFS_I(inode)->i_protect = prot;
0470 }
0471
0472 void
0473 affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
0474 {
0475 struct va_format vaf;
0476 va_list args;
0477
0478 va_start(args, fmt);
0479 vaf.fmt = fmt;
0480 vaf.va = &args;
0481 pr_crit("error (device %s): %s(): %pV\n", sb->s_id, function, &vaf);
0482 if (!sb_rdonly(sb))
0483 pr_warn("Remounting filesystem read-only\n");
0484 sb->s_flags |= SB_RDONLY;
0485 va_end(args);
0486 }
0487
0488 void
0489 affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
0490 {
0491 struct va_format vaf;
0492 va_list args;
0493
0494 va_start(args, fmt);
0495 vaf.fmt = fmt;
0496 vaf.va = &args;
0497 pr_warn("(device %s): %s(): %pV\n", sb->s_id, function, &vaf);
0498 va_end(args);
0499 }
0500
0501 bool
0502 affs_nofilenametruncate(const struct dentry *dentry)
0503 {
0504 return affs_test_opt(AFFS_SB(dentry->d_sb)->s_flags, SF_NO_TRUNCATE);
0505 }
0506
0507
0508
0509 int
0510 affs_check_name(const unsigned char *name, int len, bool notruncate)
0511 {
0512 int i;
0513
0514 if (len > AFFSNAMEMAX) {
0515 if (notruncate)
0516 return -ENAMETOOLONG;
0517 len = AFFSNAMEMAX;
0518 }
0519 for (i = 0; i < len; i++) {
0520 if (name[i] < ' ' || name[i] == ':'
0521 || (name[i] > 0x7e && name[i] < 0xa0))
0522 return -EINVAL;
0523 }
0524
0525 return 0;
0526 }
0527
0528
0529
0530
0531
0532
0533
0534
0535 int
0536 affs_copy_name(unsigned char *bstr, struct dentry *dentry)
0537 {
0538 u32 len = min(dentry->d_name.len, AFFSNAMEMAX);
0539
0540 *bstr++ = len;
0541 memcpy(bstr, dentry->d_name.name, len);
0542 return len;
0543 }