util.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/buffer_head.h>
  11. #include "ufs_fs.h"
  12. #include "ufs.h"
  13. #include "swab.h"
  14. #include "util.h"
  15. struct ufs_buffer_head * _ubh_bread_ (struct ufs_sb_private_info * uspi,
  16. struct super_block *sb, u64 fragment, u64 size)
  17. {
  18. struct ufs_buffer_head * ubh;
  19. unsigned i, j ;
  20. u64 count = 0;
  21. if (size & ~uspi->s_fmask)
  22. return NULL;
  23. count = size >> uspi->s_fshift;
  24. if (count > UFS_MAXFRAG)
  25. return NULL;
  26. ubh = (struct ufs_buffer_head *)
  27. kmalloc (sizeof (struct ufs_buffer_head), GFP_KERNEL);
  28. if (!ubh)
  29. return NULL;
  30. ubh->fragment = fragment;
  31. ubh->count = count;
  32. for (i = 0; i < count; i++)
  33. if (!(ubh->bh[i] = sb_bread(sb, fragment + i)))
  34. goto failed;
  35. for (; i < UFS_MAXFRAG; i++)
  36. ubh->bh[i] = NULL;
  37. return ubh;
  38. failed:
  39. for (j = 0; j < i; j++)
  40. brelse (ubh->bh[j]);
  41. kfree(ubh);
  42. return NULL;
  43. }
  44. struct ufs_buffer_head * ubh_bread_uspi (struct ufs_sb_private_info * uspi,
  45. struct super_block *sb, u64 fragment, u64 size)
  46. {
  47. unsigned i, j;
  48. u64 count = 0;
  49. if (size & ~uspi->s_fmask)
  50. return NULL;
  51. count = size >> uspi->s_fshift;
  52. if (count <= 0 || count > UFS_MAXFRAG)
  53. return NULL;
  54. USPI_UBH(uspi)->fragment = fragment;
  55. USPI_UBH(uspi)->count = count;
  56. for (i = 0; i < count; i++)
  57. if (!(USPI_UBH(uspi)->bh[i] = sb_bread(sb, fragment + i)))
  58. goto failed;
  59. for (; i < UFS_MAXFRAG; i++)
  60. USPI_UBH(uspi)->bh[i] = NULL;
  61. return USPI_UBH(uspi);
  62. failed:
  63. for (j = 0; j < i; j++)
  64. brelse (USPI_UBH(uspi)->bh[j]);
  65. return NULL;
  66. }
  67. void ubh_brelse (struct ufs_buffer_head * ubh)
  68. {
  69. unsigned i;
  70. if (!ubh)
  71. return;
  72. for (i = 0; i < ubh->count; i++)
  73. brelse (ubh->bh[i]);
  74. kfree (ubh);
  75. }
  76. void ubh_brelse_uspi (struct ufs_sb_private_info * uspi)
  77. {
  78. unsigned i;
  79. if (!USPI_UBH(uspi))
  80. return;
  81. for ( i = 0; i < USPI_UBH(uspi)->count; i++ ) {
  82. brelse (USPI_UBH(uspi)->bh[i]);
  83. USPI_UBH(uspi)->bh[i] = NULL;
  84. }
  85. }
  86. void ubh_mark_buffer_dirty (struct ufs_buffer_head * ubh)
  87. {
  88. unsigned i;
  89. if (!ubh)
  90. return;
  91. for ( i = 0; i < ubh->count; i++ )
  92. mark_buffer_dirty (ubh->bh[i]);
  93. }
  94. void ubh_mark_buffer_uptodate (struct ufs_buffer_head * ubh, int flag)
  95. {
  96. unsigned i;
  97. if (!ubh)
  98. return;
  99. if (flag) {
  100. for ( i = 0; i < ubh->count; i++ )
  101. set_buffer_uptodate (ubh->bh[i]);
  102. } else {
  103. for ( i = 0; i < ubh->count; i++ )
  104. clear_buffer_uptodate (ubh->bh[i]);
  105. }
  106. }
  107. void ubh_ll_rw_block(int rw, struct ufs_buffer_head *ubh)
  108. {
  109. if (!ubh)
  110. return;
  111. ll_rw_block(rw, ubh->count, ubh->bh);
  112. }
  113. void ubh_wait_on_buffer (struct ufs_buffer_head * ubh)
  114. {
  115. unsigned i;
  116. if (!ubh)
  117. return;
  118. for ( i = 0; i < ubh->count; i++ )
  119. wait_on_buffer (ubh->bh[i]);
  120. }
  121. void ubh_bforget (struct ufs_buffer_head * ubh)
  122. {
  123. unsigned i;
  124. if (!ubh)
  125. return;
  126. for ( i = 0; i < ubh->count; i++ ) if ( ubh->bh[i] )
  127. bforget (ubh->bh[i]);
  128. }
  129. int ubh_buffer_dirty (struct ufs_buffer_head * ubh)
  130. {
  131. unsigned i;
  132. unsigned result = 0;
  133. if (!ubh)
  134. return 0;
  135. for ( i = 0; i < ubh->count; i++ )
  136. result |= buffer_dirty(ubh->bh[i]);
  137. return result;
  138. }
  139. void _ubh_ubhcpymem_(struct ufs_sb_private_info * uspi,
  140. unsigned char * mem, struct ufs_buffer_head * ubh, unsigned size)
  141. {
  142. unsigned len, bhno;
  143. if (size > (ubh->count << uspi->s_fshift))
  144. size = ubh->count << uspi->s_fshift;
  145. bhno = 0;
  146. while (size) {
  147. len = min_t(unsigned int, size, uspi->s_fsize);
  148. memcpy (mem, ubh->bh[bhno]->b_data, len);
  149. mem += uspi->s_fsize;
  150. size -= len;
  151. bhno++;
  152. }
  153. }
  154. void _ubh_memcpyubh_(struct ufs_sb_private_info * uspi,
  155. struct ufs_buffer_head * ubh, unsigned char * mem, unsigned size)
  156. {
  157. unsigned len, bhno;
  158. if (size > (ubh->count << uspi->s_fshift))
  159. size = ubh->count << uspi->s_fshift;
  160. bhno = 0;
  161. while (size) {
  162. len = min_t(unsigned int, size, uspi->s_fsize);
  163. memcpy (ubh->bh[bhno]->b_data, mem, len);
  164. mem += uspi->s_fsize;
  165. size -= len;
  166. bhno++;
  167. }
  168. }
  169. dev_t
  170. ufs_get_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi)
  171. {
  172. __u32 fs32;
  173. dev_t dev;
  174. if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
  175. fs32 = fs32_to_cpu(sb, ufsi->i_u1.i_data[1]);
  176. else
  177. fs32 = fs32_to_cpu(sb, ufsi->i_u1.i_data[0]);
  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. __u32 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. if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
  210. ufsi->i_u1.i_data[1] = cpu_to_fs32(sb, fs32);
  211. else
  212. ufsi->i_u1.i_data[0] = cpu_to_fs32(sb, fs32);
  213. }
  214. /**
  215. * ufs_get_locked_page() - locate, pin and lock a pagecache page, if not exist
  216. * read it from disk.
  217. * @mapping: the address_space to search
  218. * @index: the page index
  219. *
  220. * Locates the desired pagecache page, if not exist we'll read it,
  221. * locks it, increments its reference
  222. * count and returns its address.
  223. *
  224. */
  225. struct page *ufs_get_locked_page(struct address_space *mapping,
  226. pgoff_t index)
  227. {
  228. struct page *page;
  229. page = find_lock_page(mapping, index);
  230. if (!page) {
  231. page = read_mapping_page(mapping, index, NULL);
  232. if (IS_ERR(page)) {
  233. printk(KERN_ERR "ufs_change_blocknr: "
  234. "read_mapping_page error: ino %lu, index: %lu\n",
  235. mapping->host->i_ino, index);
  236. goto out;
  237. }
  238. lock_page(page);
  239. if (unlikely(page->mapping == NULL)) {
  240. /* Truncate got there first */
  241. unlock_page(page);
  242. page_cache_release(page);
  243. page = NULL;
  244. goto out;
  245. }
  246. if (!PageUptodate(page) || PageError(page)) {
  247. unlock_page(page);
  248. page_cache_release(page);
  249. printk(KERN_ERR "ufs_change_blocknr: "
  250. "can not read page: ino %lu, index: %lu\n",
  251. mapping->host->i_ino, index);
  252. page = ERR_PTR(-EIO);
  253. }
  254. }
  255. out:
  256. return page;
  257. }