amigaffs.c 11 KB

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