amigaffs.c 11 KB

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