util.c 6.0 KB

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