util.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * linux/fs/ufs/util.c
  3. *
  4. * Copyright (C) 1998
  5. * Daniel Pirkl <daniel.pirkl@email.cz>
  6. * Charles University, Faculty of Mathematics and Physics
  7. */
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. #include <linux/ufs_fs.h>
  11. #include <linux/buffer_head.h>
  12. #include "swab.h"
  13. #include "util.h"
  14. #undef UFS_UTILS_DEBUG
  15. #ifdef UFS_UTILS_DEBUG
  16. #define UFSD(x) printk("(%s, %d), %s: ", __FILE__, __LINE__, __FUNCTION__); printk x;
  17. #else
  18. #define UFSD(x)
  19. #endif
  20. struct ufs_buffer_head * _ubh_bread_ (struct ufs_sb_private_info * uspi,
  21. struct super_block *sb, u64 fragment, u64 size)
  22. {
  23. struct ufs_buffer_head * ubh;
  24. unsigned i, j ;
  25. u64 count = 0;
  26. if (size & ~uspi->s_fmask)
  27. return NULL;
  28. count = size >> uspi->s_fshift;
  29. if (count > UFS_MAXFRAG)
  30. return NULL;
  31. ubh = (struct ufs_buffer_head *)
  32. kmalloc (sizeof (struct ufs_buffer_head), GFP_KERNEL);
  33. if (!ubh)
  34. return NULL;
  35. ubh->fragment = fragment;
  36. ubh->count = count;
  37. for (i = 0; i < count; i++)
  38. if (!(ubh->bh[i] = sb_bread(sb, fragment + i)))
  39. goto failed;
  40. for (; i < UFS_MAXFRAG; i++)
  41. ubh->bh[i] = NULL;
  42. return ubh;
  43. failed:
  44. for (j = 0; j < i; j++)
  45. brelse (ubh->bh[j]);
  46. kfree(ubh);
  47. return NULL;
  48. }
  49. struct ufs_buffer_head * ubh_bread_uspi (struct ufs_sb_private_info * uspi,
  50. struct super_block *sb, u64 fragment, u64 size)
  51. {
  52. unsigned i, j;
  53. u64 count = 0;
  54. if (size & ~uspi->s_fmask)
  55. return NULL;
  56. count = size >> uspi->s_fshift;
  57. if (count <= 0 || count > UFS_MAXFRAG)
  58. return NULL;
  59. USPI_UBH->fragment = fragment;
  60. USPI_UBH->count = count;
  61. for (i = 0; i < count; i++)
  62. if (!(USPI_UBH->bh[i] = sb_bread(sb, fragment + i)))
  63. goto failed;
  64. for (; i < UFS_MAXFRAG; i++)
  65. USPI_UBH->bh[i] = NULL;
  66. return USPI_UBH;
  67. failed:
  68. for (j = 0; j < i; j++)
  69. brelse (USPI_UBH->bh[j]);
  70. return NULL;
  71. }
  72. void ubh_brelse (struct ufs_buffer_head * ubh)
  73. {
  74. unsigned i;
  75. if (!ubh)
  76. return;
  77. for (i = 0; i < ubh->count; i++)
  78. brelse (ubh->bh[i]);
  79. kfree (ubh);
  80. }
  81. void ubh_brelse_uspi (struct ufs_sb_private_info * uspi)
  82. {
  83. unsigned i;
  84. if (!USPI_UBH)
  85. return;
  86. for ( i = 0; i < USPI_UBH->count; i++ ) {
  87. brelse (USPI_UBH->bh[i]);
  88. USPI_UBH->bh[i] = NULL;
  89. }
  90. }
  91. void ubh_mark_buffer_dirty (struct ufs_buffer_head * ubh)
  92. {
  93. unsigned i;
  94. if (!ubh)
  95. return;
  96. for ( i = 0; i < ubh->count; i++ )
  97. mark_buffer_dirty (ubh->bh[i]);
  98. }
  99. void ubh_mark_buffer_uptodate (struct ufs_buffer_head * ubh, int flag)
  100. {
  101. unsigned i;
  102. if (!ubh)
  103. return;
  104. if (flag) {
  105. for ( i = 0; i < ubh->count; i++ )
  106. set_buffer_uptodate (ubh->bh[i]);
  107. } else {
  108. for ( i = 0; i < ubh->count; i++ )
  109. clear_buffer_uptodate (ubh->bh[i]);
  110. }
  111. }
  112. void ubh_ll_rw_block (int rw, unsigned nr, struct ufs_buffer_head * ubh[])
  113. {
  114. unsigned i;
  115. if (!ubh)
  116. return;
  117. for ( i = 0; i < nr; i++ )
  118. ll_rw_block (rw, ubh[i]->count, ubh[i]->bh);
  119. }
  120. void ubh_wait_on_buffer (struct ufs_buffer_head * ubh)
  121. {
  122. unsigned i;
  123. if (!ubh)
  124. return;
  125. for ( i = 0; i < ubh->count; i++ )
  126. wait_on_buffer (ubh->bh[i]);
  127. }
  128. unsigned ubh_max_bcount (struct ufs_buffer_head * ubh)
  129. {
  130. unsigned i;
  131. unsigned max = 0;
  132. if (!ubh)
  133. return 0;
  134. for ( i = 0; i < ubh->count; i++ )
  135. if ( atomic_read(&ubh->bh[i]->b_count) > max )
  136. max = atomic_read(&ubh->bh[i]->b_count);
  137. return max;
  138. }
  139. void ubh_bforget (struct ufs_buffer_head * ubh)
  140. {
  141. unsigned i;
  142. if (!ubh)
  143. return;
  144. for ( i = 0; i < ubh->count; i++ ) if ( ubh->bh[i] )
  145. bforget (ubh->bh[i]);
  146. }
  147. int ubh_buffer_dirty (struct ufs_buffer_head * ubh)
  148. {
  149. unsigned i;
  150. unsigned result = 0;
  151. if (!ubh)
  152. return 0;
  153. for ( i = 0; i < ubh->count; i++ )
  154. result |= buffer_dirty(ubh->bh[i]);
  155. return result;
  156. }
  157. void _ubh_ubhcpymem_(struct ufs_sb_private_info * uspi,
  158. unsigned char * mem, struct ufs_buffer_head * ubh, unsigned size)
  159. {
  160. unsigned len, bhno;
  161. if (size > (ubh->count << uspi->s_fshift))
  162. size = ubh->count << uspi->s_fshift;
  163. bhno = 0;
  164. while (size) {
  165. len = min_t(unsigned int, size, uspi->s_fsize);
  166. memcpy (mem, ubh->bh[bhno]->b_data, len);
  167. mem += uspi->s_fsize;
  168. size -= len;
  169. bhno++;
  170. }
  171. }
  172. void _ubh_memcpyubh_(struct ufs_sb_private_info * uspi,
  173. struct ufs_buffer_head * ubh, unsigned char * mem, unsigned size)
  174. {
  175. unsigned len, bhno;
  176. if (size > (ubh->count << uspi->s_fshift))
  177. size = ubh->count << uspi->s_fshift;
  178. bhno = 0;
  179. while (size) {
  180. len = min_t(unsigned int, size, uspi->s_fsize);
  181. memcpy (ubh->bh[bhno]->b_data, mem, len);
  182. mem += uspi->s_fsize;
  183. size -= len;
  184. bhno++;
  185. }
  186. }
  187. dev_t
  188. ufs_get_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi)
  189. {
  190. __fs32 fs32;
  191. dev_t dev;
  192. if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
  193. fs32 = ufsi->i_u1.i_data[1];
  194. else
  195. fs32 = ufsi->i_u1.i_data[0];
  196. fs32 = fs32_to_cpu(sb, fs32);
  197. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  198. case UFS_ST_SUNx86:
  199. case UFS_ST_SUN:
  200. if ((fs32 & 0xffff0000) == 0 ||
  201. (fs32 & 0xffff0000) == 0xffff0000)
  202. dev = old_decode_dev(fs32 & 0x7fff);
  203. else
  204. dev = MKDEV(sysv_major(fs32), sysv_minor(fs32));
  205. break;
  206. default:
  207. dev = old_decode_dev(fs32);
  208. break;
  209. }
  210. return dev;
  211. }
  212. void
  213. ufs_set_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi, dev_t dev)
  214. {
  215. __fs32 fs32;
  216. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  217. case UFS_ST_SUNx86:
  218. case UFS_ST_SUN:
  219. fs32 = sysv_encode_dev(dev);
  220. if ((fs32 & 0xffff8000) == 0) {
  221. fs32 = old_encode_dev(dev);
  222. }
  223. break;
  224. default:
  225. fs32 = old_encode_dev(dev);
  226. break;
  227. }
  228. fs32 = cpu_to_fs32(sb, fs32);
  229. if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
  230. ufsi->i_u1.i_data[1] = fs32;
  231. else
  232. ufsi->i_u1.i_data[0] = fs32;
  233. }