amigaffs.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 <stdarg.h>
  11. #include <linux/stat.h>
  12. #include <linux/sched.h>
  13. #include <linux/affs_fs.h>
  14. #include <linux/string.h>
  15. #include <linux/locks.h>
  16. #include <linux/mm.h>
  17. #include <linux/amigaffs.h>
  18. extern struct timezone sys_tz;
  19. static char ErrorBuffer[256];
  20. /*
  21.  * Functions for accessing Amiga-FFS structures.
  22.  */
  23. /* Insert a header block bh into the directory dir
  24.  * caller must hold AFFS_DIR->i_hash_lock!
  25.  */
  26. int
  27. affs_insert_hash(struct inode *dir, struct buffer_head *bh)
  28. {
  29. struct super_block *sb = dir->i_sb;
  30. struct buffer_head *dir_bh;
  31. u32 ino, hash_ino;
  32. int offset;
  33. ino = bh->b_blocknr;
  34. offset = affs_hash_name(sb, AFFS_TAIL(sb, bh)->name + 1, AFFS_TAIL(sb, bh)->name[0]);
  35. pr_debug("AFFS: insert_hash(dir=%u, ino=%d)n", (u32)dir->i_ino, ino);
  36. dir_bh = affs_bread(sb, dir->i_ino);
  37. if (!dir_bh)
  38. return -EIO;
  39. hash_ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[offset]);
  40. while (hash_ino) {
  41. affs_brelse(dir_bh);
  42. dir_bh = affs_bread(sb, hash_ino);
  43. if (!dir_bh)
  44. return -EIO;
  45. hash_ino = be32_to_cpu(AFFS_TAIL(sb, dir_bh)->hash_chain);
  46. }
  47. AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
  48. AFFS_TAIL(sb, bh)->hash_chain = 0;
  49. affs_fix_checksum(sb, bh);
  50. if (dir->i_ino == dir_bh->b_blocknr)
  51. AFFS_HEAD(dir_bh)->table[offset] = cpu_to_be32(ino);
  52. else
  53. AFFS_TAIL(sb, dir_bh)->hash_chain = cpu_to_be32(ino);
  54. affs_adjust_checksum(dir_bh, ino);
  55. mark_buffer_dirty_inode(dir_bh, dir);
  56. affs_brelse(dir_bh);
  57. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  58. dir->i_version = ++event;
  59. mark_inode_dirty(dir);
  60. return 0;
  61. }
  62. /* Remove a header block from its directory.
  63.  * caller must hold AFFS_DIR->i_hash_lock!
  64.  */
  65. int
  66. affs_remove_hash(struct inode *dir, struct buffer_head *rem_bh)
  67. {
  68. struct super_block *sb;
  69. struct buffer_head *bh;
  70. u32 rem_ino, hash_ino, ino;
  71. int offset, retval;
  72. sb = dir->i_sb;
  73. rem_ino = rem_bh->b_blocknr;
  74. offset = affs_hash_name(sb, AFFS_TAIL(sb, rem_bh)->name+1, AFFS_TAIL(sb, rem_bh)->name[0]);
  75. pr_debug("AFFS: remove_hash(dir=%d, ino=%d, hashval=%d)n", (u32)dir->i_ino, rem_ino, offset);
  76. bh = affs_bread(sb, dir->i_ino);
  77. if (!bh)
  78. return -EIO;
  79. retval = -ENOENT;
  80. hash_ino = be32_to_cpu(AFFS_HEAD(bh)->table[offset]);
  81. while (hash_ino) {
  82. if (hash_ino == rem_ino) {
  83. ino = AFFS_TAIL(sb, rem_bh)->hash_chain;
  84. if (dir->i_ino == bh->b_blocknr)
  85. AFFS_HEAD(bh)->table[offset] = ino;
  86. else
  87. AFFS_TAIL(sb, bh)->hash_chain = ino;
  88. affs_adjust_checksum(bh, be32_to_cpu(ino) - hash_ino);
  89. mark_buffer_dirty_inode(bh, dir);
  90. AFFS_TAIL(sb, rem_bh)->parent = 0;
  91. retval = 0;
  92. break;
  93. }
  94. affs_brelse(bh);
  95. bh = affs_bread(sb, hash_ino);
  96. if (!bh)
  97. return -EIO;
  98. hash_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain);
  99. }
  100. affs_brelse(bh);
  101. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  102. dir->i_version = ++event;
  103. mark_inode_dirty(dir);
  104. return retval;
  105. }
  106. static void
  107. affs_fix_dcache(struct dentry *dentry, u32 entry_ino)
  108. {
  109. struct inode *inode = dentry->d_inode;
  110. void *data = dentry->d_fsdata;
  111. struct list_head *head, *next;
  112. spin_lock(&dcache_lock);
  113. head = &inode->i_dentry;
  114. next = head->next;
  115. while (next != head) {
  116. dentry = list_entry(next, struct dentry, d_alias);
  117. if (entry_ino == (u32)(long)dentry->d_fsdata) {
  118. dentry->d_fsdata = data;
  119. break;
  120. }
  121. next = next->next;
  122. }
  123. spin_unlock(&dcache_lock);
  124. }
  125. /* Remove header from link chain */
  126. static int
  127. affs_remove_link(struct dentry *dentry)
  128. {
  129. struct inode *dir, *inode = dentry->d_inode;
  130. struct super_block *sb = inode->i_sb;
  131. struct buffer_head *bh = NULL, *link_bh = NULL;
  132. u32 link_ino, ino;
  133. int retval;
  134. pr_debug("AFFS: remove_link(key=%ld)n", inode->i_ino);
  135. retval = -EIO;
  136. bh = affs_bread(sb, inode->i_ino);
  137. if (!bh)
  138. goto done;
  139. link_ino = (u32)(long)dentry->d_fsdata;
  140. if (inode->i_ino == link_ino) {
  141. /* we can't remove the head of the link, as its blocknr is still used as ino,
  142.  * so we remove the block of the first link instead.
  143.  */ 
  144. link_ino = be32_to_cpu(AFFS_TAIL(sb, bh)->link_chain);
  145. link_bh = affs_bread(sb, link_ino);
  146. if (!link_bh)
  147. goto done;
  148. dir = iget(sb, be32_to_cpu(AFFS_TAIL(sb, link_bh)->parent));
  149. if (!dir)
  150. goto done;
  151. affs_lock_dir(dir);
  152. affs_fix_dcache(dentry, link_ino);
  153. retval = affs_remove_hash(dir, link_bh);
  154. if (retval)
  155. goto done;
  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. goto done;
  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))) {
  170. if (ino == link_ino) {
  171. ino = AFFS_TAIL(sb, link_bh)->link_chain;
  172. AFFS_TAIL(sb, bh)->link_chain = ino;
  173. affs_adjust_checksum(bh, be32_to_cpu(ino) - 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. inode->i_nlink = 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->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 = dentry->d_parent->d_inode;
  235. sb = dir->i_sb;
  236. retval = -ENOENT;
  237. inode = dentry->d_inode;
  238. if (!inode)
  239. goto done;
  240. pr_debug("AFFS: remove_header(key=%ld)n", 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. inode->i_nlink = 0;
  271. affs_unlock_link(inode);
  272. inode->i_ctime = CURRENT_TIME;
  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. u32 *ptr = (u32 *)bh->b_data;
  292. u32 sum;
  293. int bsize;
  294. sum = 0;
  295. for (bsize = sb->s_blocksize / sizeof(u32); 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(u32);
  307. u32 *ptr = (u32 *)bh->b_data;
  308. u32 checksum, *checksumptr;
  309. checksumptr = ptr + 5;
  310. *checksumptr = 0;
  311. for (checksum = 0; cnt > 0; ptr++, cnt--)
  312. checksum += be32_to_cpu(*ptr);
  313. *checksumptr = cpu_to_be32(-checksum);
  314. }
  315. void
  316. secs_to_datestamp(time_t secs, struct affs_date *ds)
  317. {
  318. u32  days;
  319. u32  minute;
  320. secs -= sys_tz.tz_minuteswest * 60 + ((8 * 365 + 2) * 24 * 60 * 60);
  321. if (secs < 0)
  322. secs = 0;
  323. days    = secs / 86400;
  324. secs   -= days * 86400;
  325. minute  = secs / 60;
  326. secs   -= minute * 60;
  327. ds->days = be32_to_cpu(days);
  328. ds->mins = be32_to_cpu(minute);
  329. ds->ticks = be32_to_cpu(secs * 50);
  330. }
  331. mode_t
  332. prot_to_mode(u32 prot)
  333. {
  334. int mode = 0;
  335. if (!(prot & FIBF_NOWRITE))
  336. mode |= S_IWUSR;
  337. if (!(prot & FIBF_NOREAD))
  338. mode |= S_IRUSR;
  339. if (!(prot & FIBF_NOEXECUTE))
  340. mode |= S_IXUSR;
  341. if (prot & FIBF_GRP_WRITE)
  342. mode |= S_IWGRP;
  343. if (prot & FIBF_GRP_READ)
  344. mode |= S_IRGRP;
  345. if (prot & FIBF_GRP_EXECUTE)
  346. mode |= S_IXGRP;
  347. if (prot & FIBF_OTR_WRITE)
  348. mode |= S_IWOTH;
  349. if (prot & FIBF_OTR_READ)
  350. mode |= S_IROTH;
  351. if (prot & FIBF_OTR_EXECUTE)
  352. mode |= S_IXOTH;
  353. return mode;
  354. }
  355. void
  356. mode_to_prot(struct inode *inode)
  357. {
  358. u32 prot = AFFS_INODE->i_protect;
  359. mode_t mode = inode->i_mode;
  360. if (!(mode & S_IXUSR))
  361. prot |= FIBF_NOEXECUTE;
  362. if (!(mode & S_IRUSR))
  363. prot |= FIBF_NOREAD;
  364. if (!(mode & S_IWUSR))
  365. prot |= FIBF_NOWRITE;
  366. if (mode & S_IXGRP)
  367. prot |= FIBF_GRP_EXECUTE;
  368. if (mode & S_IRGRP)
  369. prot |= FIBF_GRP_READ;
  370. if (mode & S_IWGRP)
  371. prot |= FIBF_GRP_WRITE;
  372. if (mode & S_IXOTH)
  373. prot |= FIBF_OTR_EXECUTE;
  374. if (mode & S_IROTH)
  375. prot |= FIBF_OTR_READ;
  376. if (mode & S_IWOTH)
  377. prot |= FIBF_OTR_WRITE;
  378. AFFS_INODE->i_protect = prot;
  379. }
  380. void
  381. affs_error(struct super_block *sb, const char *function, const char *fmt, ...)
  382. {
  383. va_list  args;
  384. va_start(args,fmt);
  385. vsprintf(ErrorBuffer,fmt,args);
  386. va_end(args);
  387. printk(KERN_CRIT "AFFS error (device %s): %s(): %sn", bdevname(sb->s_dev),
  388. function,ErrorBuffer);
  389. if (!(sb->s_flags & MS_RDONLY))
  390. printk(KERN_WARNING "AFFS: Remounting filesystem read-onlyn");
  391. sb->s_flags |= MS_RDONLY;
  392. AFFS_SB->s_flags |= SF_READONLY; /* Don't allow to remount rw */
  393. }
  394. void
  395. affs_warning(struct super_block *sb, const char *function, const char *fmt, ...)
  396. {
  397. va_list  args;
  398. va_start(args,fmt);
  399. vsprintf(ErrorBuffer,fmt,args);
  400. va_end(args);
  401. printk(KERN_WARNING "AFFS warning (device %s): %s(): %sn", bdevname(sb->s_dev),
  402. function,ErrorBuffer);
  403. }
  404. /* Check if the name is valid for a affs object. */
  405. int
  406. affs_check_name(const unsigned char *name, int len)
  407. {
  408. int  i;
  409. if (len > 30)
  410. #ifdef AFFS_NO_TRUNCATE
  411. return -ENAMETOOLONG;
  412. #else
  413. len = 30;
  414. #endif
  415. for (i = 0; i < len; i++) {
  416. if (name[i] < ' ' || name[i] == ':'
  417.     || (name[i] > 0x7e && name[i] < 0xa0))
  418. return -EINVAL;
  419. }
  420. return 0;
  421. }
  422. /* This function copies name to bstr, with at most 30
  423.  * characters length. The bstr will be prepended by
  424.  * a length byte.
  425.  * NOTE: The name will must be already checked by
  426.  *       affs_check_name()!
  427.  */
  428. int
  429. affs_copy_name(unsigned char *bstr, struct dentry *dentry)
  430. {
  431. int len = min(dentry->d_name.len, 30u);
  432. *bstr++ = len;
  433. memcpy(bstr, dentry->d_name.name, len);
  434. return len;
  435. }