util.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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, struct ufs_buffer_head *ubh)
  107. {
  108. if (!ubh)
  109. return;
  110. ll_rw_block(rw, ubh->count, ubh->bh);
  111. }
  112. void ubh_wait_on_buffer (struct ufs_buffer_head * ubh)
  113. {
  114. unsigned i;
  115. if (!ubh)
  116. return;
  117. for ( i = 0; i < ubh->count; i++ )
  118. wait_on_buffer (ubh->bh[i]);
  119. }
  120. void ubh_bforget (struct ufs_buffer_head * ubh)
  121. {
  122. unsigned i;
  123. if (!ubh)
  124. return;
  125. for ( i = 0; i < ubh->count; i++ ) if ( ubh->bh[i] )
  126. bforget (ubh->bh[i]);
  127. }
  128. int ubh_buffer_dirty (struct ufs_buffer_head * ubh)
  129. {
  130. unsigned i;
  131. unsigned result = 0;
  132. if (!ubh)
  133. return 0;
  134. for ( i = 0; i < ubh->count; i++ )
  135. result |= buffer_dirty(ubh->bh[i]);
  136. return result;
  137. }
  138. void _ubh_ubhcpymem_(struct ufs_sb_private_info * uspi,
  139. unsigned char * mem, struct ufs_buffer_head * ubh, unsigned size)
  140. {
  141. unsigned len, bhno;
  142. if (size > (ubh->count << uspi->s_fshift))
  143. size = ubh->count << uspi->s_fshift;
  144. bhno = 0;
  145. while (size) {
  146. len = min_t(unsigned int, size, uspi->s_fsize);
  147. memcpy (mem, ubh->bh[bhno]->b_data, len);
  148. mem += uspi->s_fsize;
  149. size -= len;
  150. bhno++;
  151. }
  152. }
  153. void _ubh_memcpyubh_(struct ufs_sb_private_info * uspi,
  154. struct ufs_buffer_head * ubh, unsigned char * mem, unsigned size)
  155. {
  156. unsigned len, bhno;
  157. if (size > (ubh->count << uspi->s_fshift))
  158. size = ubh->count << uspi->s_fshift;
  159. bhno = 0;
  160. while (size) {
  161. len = min_t(unsigned int, size, uspi->s_fsize);
  162. memcpy (ubh->bh[bhno]->b_data, mem, len);
  163. mem += uspi->s_fsize;
  164. size -= len;
  165. bhno++;
  166. }
  167. }
  168. dev_t
  169. ufs_get_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi)
  170. {
  171. __fs32 fs32;
  172. dev_t dev;
  173. if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
  174. fs32 = ufsi->i_u1.i_data[1];
  175. else
  176. fs32 = ufsi->i_u1.i_data[0];
  177. fs32 = fs32_to_cpu(sb, fs32);
  178. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  179. case UFS_ST_SUNx86:
  180. case UFS_ST_SUN:
  181. if ((fs32 & 0xffff0000) == 0 ||
  182. (fs32 & 0xffff0000) == 0xffff0000)
  183. dev = old_decode_dev(fs32 & 0x7fff);
  184. else
  185. dev = MKDEV(sysv_major(fs32), sysv_minor(fs32));
  186. break;
  187. default:
  188. dev = old_decode_dev(fs32);
  189. break;
  190. }
  191. return dev;
  192. }
  193. void
  194. ufs_set_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi, dev_t dev)
  195. {
  196. __fs32 fs32;
  197. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  198. case UFS_ST_SUNx86:
  199. case UFS_ST_SUN:
  200. fs32 = sysv_encode_dev(dev);
  201. if ((fs32 & 0xffff8000) == 0) {
  202. fs32 = old_encode_dev(dev);
  203. }
  204. break;
  205. default:
  206. fs32 = old_encode_dev(dev);
  207. break;
  208. }
  209. fs32 = cpu_to_fs32(sb, fs32);
  210. if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
  211. ufsi->i_u1.i_data[1] = fs32;
  212. else
  213. ufsi->i_u1.i_data[0] = fs32;
  214. }
  215. /**
  216. * ufs_get_locked_page() - locate, pin and lock a pagecache page, if not exist
  217. * read it from disk.
  218. * @mapping: the address_space to search
  219. * @index: the page index
  220. *
  221. * Locates the desired pagecache page, if not exist we'll read it,
  222. * locks it, increments its reference
  223. * count and returns its address.
  224. *
  225. */
  226. struct page *ufs_get_locked_page(struct address_space *mapping,
  227. pgoff_t index)
  228. {
  229. struct page *page;
  230. page = find_lock_page(mapping, index);
  231. if (!page) {
  232. page = read_cache_page(mapping, index,
  233. (filler_t*)mapping->a_ops->readpage,
  234. NULL);
  235. if (IS_ERR(page)) {
  236. printk(KERN_ERR "ufs_change_blocknr: "
  237. "read_cache_page error: ino %lu, index: %lu\n",
  238. mapping->host->i_ino, index);
  239. goto out;
  240. }
  241. lock_page(page);
  242. if (unlikely(page->mapping == NULL)) {
  243. /* Truncate got there first */
  244. unlock_page(page);
  245. page_cache_release(page);
  246. page = NULL;
  247. goto out;
  248. }
  249. if (!PageUptodate(page) || PageError(page)) {
  250. unlock_page(page);
  251. page_cache_release(page);
  252. printk(KERN_ERR "ufs_change_blocknr: "
  253. "can not read page: ino %lu, index: %lu\n",
  254. mapping->host->i_ino, index);
  255. page = ERR_PTR(-EIO);
  256. }
  257. }
  258. out:
  259. return page;
  260. }