amigaffs.c 11 KB

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