util.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. struct ufs_buffer_head * _ubh_bread_ (struct ufs_sb_private_info * uspi,
  15. struct super_block *sb, u64 fragment, u64 size)
  16. {
  17. struct ufs_buffer_head * ubh;
  18. unsigned i, j ;
  19. u64 count = 0;
  20. if (size & ~uspi->s_fmask)
  21. return NULL;
  22. count = size >> uspi->s_fshift;
  23. if (count > UFS_MAXFRAG)
  24. return NULL;
  25. ubh = (struct ufs_buffer_head *)
  26. kmalloc (sizeof (struct ufs_buffer_head), GFP_KERNEL);
  27. if (!ubh)
  28. return NULL;
  29. ubh->fragment = fragment;
  30. ubh->count = count;
  31. for (i = 0; i < count; i++)
  32. if (!(ubh->bh[i] = sb_bread(sb, fragment + i)))
  33. goto failed;
  34. for (; i < UFS_MAXFRAG; i++)
  35. ubh->bh[i] = NULL;
  36. return ubh;
  37. failed:
  38. for (j = 0; j < i; j++)
  39. brelse (ubh->bh[j]);
  40. kfree(ubh);
  41. return NULL;
  42. }
  43. struct ufs_buffer_head * ubh_bread_uspi (struct ufs_sb_private_info * uspi,
  44. struct super_block *sb, u64 fragment, u64 size)
  45. {
  46. unsigned i, j;
  47. u64 count = 0;
  48. if (size & ~uspi->s_fmask)
  49. return NULL;
  50. count = size >> uspi->s_fshift;
  51. if (count <= 0 || count > UFS_MAXFRAG)
  52. return NULL;
  53. USPI_UBH(uspi)->fragment = fragment;
  54. USPI_UBH(uspi)->count = count;
  55. for (i = 0; i < count; i++)
  56. if (!(USPI_UBH(uspi)->bh[i] = sb_bread(sb, fragment + i)))
  57. goto failed;
  58. for (; i < UFS_MAXFRAG; i++)
  59. USPI_UBH(uspi)->bh[i] = NULL;
  60. return USPI_UBH(uspi);
  61. failed:
  62. for (j = 0; j < i; j++)
  63. brelse (USPI_UBH(uspi)->bh[j]);
  64. return NULL;
  65. }
  66. void ubh_brelse (struct ufs_buffer_head * ubh)
  67. {
  68. unsigned i;
  69. if (!ubh)
  70. return;
  71. for (i = 0; i < ubh->count; i++)
  72. brelse (ubh->bh[i]);
  73. kfree (ubh);
  74. }
  75. void ubh_brelse_uspi (struct ufs_sb_private_info * uspi)
  76. {
  77. unsigned i;
  78. if (!USPI_UBH(uspi))
  79. return;
  80. for ( i = 0; i < USPI_UBH(uspi)->count; i++ ) {
  81. brelse (USPI_UBH(uspi)->bh[i]);
  82. USPI_UBH(uspi)->bh[i] = NULL;
  83. }
  84. }
  85. void ubh_mark_buffer_dirty (struct ufs_buffer_head * ubh)
  86. {
  87. unsigned i;
  88. if (!ubh)
  89. return;
  90. for ( i = 0; i < ubh->count; i++ )
  91. mark_buffer_dirty (ubh->bh[i]);
  92. }
  93. void ubh_mark_buffer_uptodate (struct ufs_buffer_head * ubh, int flag)
  94. {
  95. unsigned i;
  96. if (!ubh)
  97. return;
  98. if (flag) {
  99. for ( i = 0; i < ubh->count; i++ )
  100. set_buffer_uptodate (ubh->bh[i]);
  101. } else {
  102. for ( i = 0; i < ubh->count; i++ )
  103. clear_buffer_uptodate (ubh->bh[i]);
  104. }
  105. }
  106. void ubh_ll_rw_block (int rw, unsigned nr, struct ufs_buffer_head * ubh[])
  107. {
  108. unsigned i;
  109. if (!ubh)
  110. return;
  111. for ( i = 0; i < nr; i++ )
  112. ll_rw_block (rw, ubh[i]->count, ubh[i]->bh);
  113. }
  114. void ubh_wait_on_buffer (struct ufs_buffer_head * ubh)
  115. {
  116. unsigned i;
  117. if (!ubh)
  118. return;
  119. for ( i = 0; i < ubh->count; i++ )
  120. wait_on_buffer (ubh->bh[i]);
  121. }
  122. void ubh_bforget (struct ufs_buffer_head * ubh)
  123. {
  124. unsigned i;
  125. if (!ubh)
  126. return;
  127. for ( i = 0; i < ubh->count; i++ ) if ( ubh->bh[i] )
  128. bforget (ubh->bh[i]);
  129. }
  130. int ubh_buffer_dirty (struct ufs_buffer_head * ubh)
  131. {
  132. unsigned i;
  133. unsigned result = 0;
  134. if (!ubh)
  135. return 0;
  136. for ( i = 0; i < ubh->count; i++ )
  137. result |= buffer_dirty(ubh->bh[i]);
  138. return result;
  139. }
  140. void _ubh_ubhcpymem_(struct ufs_sb_private_info * uspi,
  141. unsigned char * mem, struct ufs_buffer_head * ubh, unsigned size)
  142. {
  143. unsigned len, bhno;
  144. if (size > (ubh->count << uspi->s_fshift))
  145. size = ubh->count << uspi->s_fshift;
  146. bhno = 0;
  147. while (size) {
  148. len = min_t(unsigned int, size, uspi->s_fsize);
  149. memcpy (mem, ubh->bh[bhno]->b_data, len);
  150. mem += uspi->s_fsize;
  151. size -= len;
  152. bhno++;
  153. }
  154. }
  155. void _ubh_memcpyubh_(struct ufs_sb_private_info * uspi,
  156. struct ufs_buffer_head * ubh, unsigned char * mem, unsigned size)
  157. {
  158. unsigned len, bhno;
  159. if (size > (ubh->count << uspi->s_fshift))
  160. size = ubh->count << uspi->s_fshift;
  161. bhno = 0;
  162. while (size) {
  163. len = min_t(unsigned int, size, uspi->s_fsize);
  164. memcpy (ubh->bh[bhno]->b_data, mem, len);
  165. mem += uspi->s_fsize;
  166. size -= len;
  167. bhno++;
  168. }
  169. }
  170. dev_t
  171. ufs_get_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi)
  172. {
  173. __fs32 fs32;
  174. dev_t dev;
  175. if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
  176. fs32 = ufsi->i_u1.i_data[1];
  177. else
  178. fs32 = ufsi->i_u1.i_data[0];
  179. fs32 = fs32_to_cpu(sb, fs32);
  180. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  181. case UFS_ST_SUNx86:
  182. case UFS_ST_SUN:
  183. if ((fs32 & 0xffff0000) == 0 ||
  184. (fs32 & 0xffff0000) == 0xffff0000)
  185. dev = old_decode_dev(fs32 & 0x7fff);
  186. else
  187. dev = MKDEV(sysv_major(fs32), sysv_minor(fs32));
  188. break;
  189. default:
  190. dev = old_decode_dev(fs32);
  191. break;
  192. }
  193. return dev;
  194. }
  195. void
  196. ufs_set_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi, dev_t dev)
  197. {
  198. __fs32 fs32;
  199. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  200. case UFS_ST_SUNx86:
  201. case UFS_ST_SUN:
  202. fs32 = sysv_encode_dev(dev);
  203. if ((fs32 & 0xffff8000) == 0) {
  204. fs32 = old_encode_dev(dev);
  205. }
  206. break;
  207. default:
  208. fs32 = old_encode_dev(dev);
  209. break;
  210. }
  211. fs32 = cpu_to_fs32(sb, fs32);
  212. if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
  213. ufsi->i_u1.i_data[1] = fs32;
  214. else
  215. ufsi->i_u1.i_data[0] = fs32;
  216. }