amigaffs.c 11 KB

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