util.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * linux/fs/ufs/util.h
  3. *
  4. * Copyright (C) 1998
  5. * Daniel Pirkl <daniel.pirkl@email.cz>
  6. * Charles University, Faculty of Mathematics and Physics
  7. */
  8. #include <linux/buffer_head.h>
  9. #include <linux/fs.h>
  10. #include "swab.h"
  11. /*
  12. * some useful macros
  13. */
  14. #define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))
  15. /*
  16. * functions used for retyping
  17. */
  18. static inline struct ufs_buffer_head *UCPI_UBH(struct ufs_cg_private_info *cpi)
  19. {
  20. return &cpi->c_ubh;
  21. }
  22. static inline struct ufs_buffer_head *USPI_UBH(struct ufs_sb_private_info *spi)
  23. {
  24. return &spi->s_ubh;
  25. }
  26. /*
  27. * macros used for accessing structures
  28. */
  29. static inline s32
  30. ufs_get_fs_state(struct super_block *sb, struct ufs_super_block_first *usb1,
  31. struct ufs_super_block_third *usb3)
  32. {
  33. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  34. case UFS_ST_SUNOS:
  35. if (fs32_to_cpu(sb, usb3->fs_postblformat) == UFS_42POSTBLFMT)
  36. return fs32_to_cpu(sb, usb1->fs_u0.fs_sun.fs_state);
  37. /* Fall Through to UFS_ST_SUN */
  38. case UFS_ST_SUN:
  39. return fs32_to_cpu(sb, usb3->fs_un2.fs_sun.fs_state);
  40. case UFS_ST_SUNx86:
  41. return fs32_to_cpu(sb, usb1->fs_u1.fs_sunx86.fs_state);
  42. case UFS_ST_44BSD:
  43. default:
  44. return fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_state);
  45. }
  46. }
  47. static inline void
  48. ufs_set_fs_state(struct super_block *sb, struct ufs_super_block_first *usb1,
  49. struct ufs_super_block_third *usb3, s32 value)
  50. {
  51. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  52. case UFS_ST_SUNOS:
  53. if (fs32_to_cpu(sb, usb3->fs_postblformat) == UFS_42POSTBLFMT) {
  54. usb1->fs_u0.fs_sun.fs_state = cpu_to_fs32(sb, value);
  55. break;
  56. }
  57. /* Fall Through to UFS_ST_SUN */
  58. case UFS_ST_SUN:
  59. usb3->fs_un2.fs_sun.fs_state = cpu_to_fs32(sb, value);
  60. break;
  61. case UFS_ST_SUNx86:
  62. usb1->fs_u1.fs_sunx86.fs_state = cpu_to_fs32(sb, value);
  63. break;
  64. case UFS_ST_44BSD:
  65. usb3->fs_un2.fs_44.fs_state = cpu_to_fs32(sb, value);
  66. break;
  67. }
  68. }
  69. static inline u32
  70. ufs_get_fs_npsect(struct super_block *sb, struct ufs_super_block_first *usb1,
  71. struct ufs_super_block_third *usb3)
  72. {
  73. if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86)
  74. return fs32_to_cpu(sb, usb3->fs_un2.fs_sunx86.fs_npsect);
  75. else
  76. return fs32_to_cpu(sb, usb1->fs_u1.fs_sun.fs_npsect);
  77. }
  78. static inline u64
  79. ufs_get_fs_qbmask(struct super_block *sb, struct ufs_super_block_third *usb3)
  80. {
  81. __fs64 tmp;
  82. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  83. case UFS_ST_SUNOS:
  84. case UFS_ST_SUN:
  85. ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sun.fs_qbmask[0];
  86. ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sun.fs_qbmask[1];
  87. break;
  88. case UFS_ST_SUNx86:
  89. ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sunx86.fs_qbmask[0];
  90. ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sunx86.fs_qbmask[1];
  91. break;
  92. case UFS_ST_44BSD:
  93. ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_44.fs_qbmask[0];
  94. ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_44.fs_qbmask[1];
  95. break;
  96. }
  97. return fs64_to_cpu(sb, tmp);
  98. }
  99. static inline u64
  100. ufs_get_fs_qfmask(struct super_block *sb, struct ufs_super_block_third *usb3)
  101. {
  102. __fs64 tmp;
  103. switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) {
  104. case UFS_ST_SUNOS:
  105. case UFS_ST_SUN:
  106. ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sun.fs_qfmask[0];
  107. ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sun.fs_qfmask[1];
  108. break;
  109. case UFS_ST_SUNx86:
  110. ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_sunx86.fs_qfmask[0];
  111. ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_sunx86.fs_qfmask[1];
  112. break;
  113. case UFS_ST_44BSD:
  114. ((__fs32 *)&tmp)[0] = usb3->fs_un2.fs_44.fs_qfmask[0];
  115. ((__fs32 *)&tmp)[1] = usb3->fs_un2.fs_44.fs_qfmask[1];
  116. break;
  117. }
  118. return fs64_to_cpu(sb, tmp);
  119. }
  120. static inline u16
  121. ufs_get_de_namlen(struct super_block *sb, struct ufs_dir_entry *de)
  122. {
  123. if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) == UFS_DE_OLD)
  124. return fs16_to_cpu(sb, de->d_u.d_namlen);
  125. else
  126. return de->d_u.d_44.d_namlen; /* XXX this seems wrong */
  127. }
  128. static inline void
  129. ufs_set_de_namlen(struct super_block *sb, struct ufs_dir_entry *de, u16 value)
  130. {
  131. if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) == UFS_DE_OLD)
  132. de->d_u.d_namlen = cpu_to_fs16(sb, value);
  133. else
  134. de->d_u.d_44.d_namlen = value; /* XXX this seems wrong */
  135. }
  136. static inline void
  137. ufs_set_de_type(struct super_block *sb, struct ufs_dir_entry *de, int mode)
  138. {
  139. if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) != UFS_DE_44BSD)
  140. return;
  141. /*
  142. * TODO turn this into a table lookup
  143. */
  144. switch (mode & S_IFMT) {
  145. case S_IFSOCK:
  146. de->d_u.d_44.d_type = DT_SOCK;
  147. break;
  148. case S_IFLNK:
  149. de->d_u.d_44.d_type = DT_LNK;
  150. break;
  151. case S_IFREG:
  152. de->d_u.d_44.d_type = DT_REG;
  153. break;
  154. case S_IFBLK:
  155. de->d_u.d_44.d_type = DT_BLK;
  156. break;
  157. case S_IFDIR:
  158. de->d_u.d_44.d_type = DT_DIR;
  159. break;
  160. case S_IFCHR:
  161. de->d_u.d_44.d_type = DT_CHR;
  162. break;
  163. case S_IFIFO:
  164. de->d_u.d_44.d_type = DT_FIFO;
  165. break;
  166. default:
  167. de->d_u.d_44.d_type = DT_UNKNOWN;
  168. }
  169. }
  170. static inline u32
  171. ufs_get_inode_uid(struct super_block *sb, struct ufs_inode *inode)
  172. {
  173. switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
  174. case UFS_UID_44BSD:
  175. return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_uid);
  176. case UFS_UID_EFT:
  177. if (inode->ui_u1.oldids.ui_suid == 0xFFFF)
  178. return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_uid);
  179. /* Fall through */
  180. default:
  181. return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_suid);
  182. }
  183. }
  184. static inline void
  185. ufs_set_inode_uid(struct super_block *sb, struct ufs_inode *inode, u32 value)
  186. {
  187. switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
  188. case UFS_UID_44BSD:
  189. inode->ui_u3.ui_44.ui_uid = cpu_to_fs32(sb, value);
  190. inode->ui_u1.oldids.ui_suid = cpu_to_fs16(sb, value);
  191. break;
  192. case UFS_UID_EFT:
  193. inode->ui_u3.ui_sun.ui_uid = cpu_to_fs32(sb, value);
  194. if (value > 0xFFFF)
  195. value = 0xFFFF;
  196. /* Fall through */
  197. default:
  198. inode->ui_u1.oldids.ui_suid = cpu_to_fs16(sb, value);
  199. break;
  200. }
  201. }
  202. static inline u32
  203. ufs_get_inode_gid(struct super_block *sb, struct ufs_inode *inode)
  204. {
  205. switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
  206. case UFS_UID_44BSD:
  207. return fs32_to_cpu(sb, inode->ui_u3.ui_44.ui_gid);
  208. case UFS_UID_EFT:
  209. if (inode->ui_u1.oldids.ui_suid == 0xFFFF)
  210. return fs32_to_cpu(sb, inode->ui_u3.ui_sun.ui_gid);
  211. /* Fall through */
  212. default:
  213. return fs16_to_cpu(sb, inode->ui_u1.oldids.ui_sgid);
  214. }
  215. }
  216. static inline void
  217. ufs_set_inode_gid(struct super_block *sb, struct ufs_inode *inode, u32 value)
  218. {
  219. switch (UFS_SB(sb)->s_flags & UFS_UID_MASK) {
  220. case UFS_UID_44BSD:
  221. inode->ui_u3.ui_44.ui_gid = cpu_to_fs32(sb, value);
  222. inode->ui_u1.oldids.ui_sgid = cpu_to_fs16(sb, value);
  223. break;
  224. case UFS_UID_EFT:
  225. inode->ui_u3.ui_sun.ui_gid = cpu_to_fs32(sb, value);
  226. if (value > 0xFFFF)
  227. value = 0xFFFF;
  228. /* Fall through */
  229. default:
  230. inode->ui_u1.oldids.ui_sgid = cpu_to_fs16(sb, value);
  231. break;
  232. }
  233. }
  234. extern dev_t ufs_get_inode_dev(struct super_block *, struct ufs_inode_info *);
  235. extern void ufs_set_inode_dev(struct super_block *, struct ufs_inode_info *, dev_t);
  236. extern int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len);
  237. /*
  238. * These functions manipulate ufs buffers
  239. */
  240. #define ubh_bread(sb,fragment,size) _ubh_bread_(uspi,sb,fragment,size)
  241. extern struct ufs_buffer_head * _ubh_bread_(struct ufs_sb_private_info *, struct super_block *, u64 , u64);
  242. extern struct ufs_buffer_head * ubh_bread_uspi(struct ufs_sb_private_info *, struct super_block *, u64, u64);
  243. extern void ubh_brelse (struct ufs_buffer_head *);
  244. extern void ubh_brelse_uspi (struct ufs_sb_private_info *);
  245. extern void ubh_mark_buffer_dirty (struct ufs_buffer_head *);
  246. extern void ubh_mark_buffer_uptodate (struct ufs_buffer_head *, int);
  247. extern void ubh_sync_block(struct ufs_buffer_head *);
  248. extern void ubh_bforget (struct ufs_buffer_head *);
  249. extern int ubh_buffer_dirty (struct ufs_buffer_head *);
  250. #define ubh_ubhcpymem(mem,ubh,size) _ubh_ubhcpymem_(uspi,mem,ubh,size)
  251. extern void _ubh_ubhcpymem_(struct ufs_sb_private_info *, unsigned char *, struct ufs_buffer_head *, unsigned);
  252. #define ubh_memcpyubh(ubh,mem,size) _ubh_memcpyubh_(uspi,ubh,mem,size)
  253. extern void _ubh_memcpyubh_(struct ufs_sb_private_info *, struct ufs_buffer_head *, unsigned char *, unsigned);
  254. /* This functions works with cache pages*/
  255. extern struct page *ufs_get_locked_page(struct address_space *mapping,
  256. pgoff_t index);
  257. static inline void ufs_put_locked_page(struct page *page)
  258. {
  259. unlock_page(page);
  260. put_page(page);
  261. }
  262. /*
  263. * macros and inline function to get important structures from ufs_sb_private_info
  264. */
  265. static inline void *get_usb_offset(struct ufs_sb_private_info *uspi,
  266. unsigned int offset)
  267. {
  268. unsigned int index;
  269. index = offset >> uspi->s_fshift;
  270. offset &= ~uspi->s_fmask;
  271. return uspi->s_ubh.bh[index]->b_data + offset;
  272. }
  273. #define ubh_get_usb_first(uspi) \
  274. ((struct ufs_super_block_first *)get_usb_offset((uspi), 0))
  275. #define ubh_get_usb_second(uspi) \
  276. ((struct ufs_super_block_second *)get_usb_offset((uspi), UFS_SECTOR_SIZE))
  277. #define ubh_get_usb_third(uspi) \
  278. ((struct ufs_super_block_third *)get_usb_offset((uspi), 2*UFS_SECTOR_SIZE))
  279. #define ubh_get_ucg(ubh) \
  280. ((struct ufs_cylinder_group *)((ubh)->bh[0]->b_data))
  281. /*
  282. * Extract byte from ufs_buffer_head
  283. * Extract the bits for a block from a map inside ufs_buffer_head
  284. */
  285. #define ubh_get_addr8(ubh,begin) \
  286. ((u8*)(ubh)->bh[(begin) >> uspi->s_fshift]->b_data + \
  287. ((begin) & ~uspi->s_fmask))
  288. #define ubh_get_addr16(ubh,begin) \
  289. (((__fs16*)((ubh)->bh[(begin) >> (uspi->s_fshift-1)]->b_data)) + \
  290. ((begin) & ((uspi->fsize>>1) - 1)))
  291. #define ubh_get_addr32(ubh,begin) \
  292. (((__fs32*)((ubh)->bh[(begin) >> (uspi->s_fshift-2)]->b_data)) + \
  293. ((begin) & ((uspi->s_fsize>>2) - 1)))
  294. #define ubh_get_addr64(ubh,begin) \
  295. (((__fs64*)((ubh)->bh[(begin) >> (uspi->s_fshift-3)]->b_data)) + \
  296. ((begin) & ((uspi->s_fsize>>3) - 1)))
  297. #define ubh_get_addr ubh_get_addr8
  298. static inline void *ubh_get_data_ptr(struct ufs_sb_private_info *uspi,
  299. struct ufs_buffer_head *ubh,
  300. u64 blk)
  301. {
  302. if (uspi->fs_magic == UFS2_MAGIC)
  303. return ubh_get_addr64(ubh, blk);
  304. else
  305. return ubh_get_addr32(ubh, blk);
  306. }
  307. #define ubh_blkmap(ubh,begin,bit) \
  308. ((*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) >> ((bit) & 7)) & (0xff >> (UFS_MAXFRAG - uspi->s_fpb)))
  309. /*
  310. * Determine the number of available frags given a
  311. * percentage to hold in reserve.
  312. */
  313. static inline u64
  314. ufs_freespace(struct ufs_sb_private_info *uspi, int percentreserved)
  315. {
  316. return ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
  317. uspi->cs_total.cs_nffree -
  318. (uspi->s_dsize * (percentreserved) / 100);
  319. }
  320. /*
  321. * Macros to access cylinder group array structures
  322. */
  323. #define ubh_cg_blktot(ucpi,cylno) \
  324. (*((__fs32*)ubh_get_addr(UCPI_UBH(ucpi), (ucpi)->c_btotoff + ((cylno) << 2))))
  325. #define ubh_cg_blks(ucpi,cylno,rpos) \
  326. (*((__fs16*)ubh_get_addr(UCPI_UBH(ucpi), \
  327. (ucpi)->c_boff + (((cylno) * uspi->s_nrpos + (rpos)) << 1 ))))
  328. /*
  329. * Bitmap operations
  330. * These functions work like classical bitmap operations.
  331. * The difference is that we don't have the whole bitmap
  332. * in one contiguous chunk of memory, but in several buffers.
  333. * The parameters of each function are super_block, ufs_buffer_head and
  334. * position of the beginning of the bitmap.
  335. */
  336. #define ubh_setbit(ubh,begin,bit) \
  337. (*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) |= (1 << ((bit) & 7)))
  338. #define ubh_clrbit(ubh,begin,bit) \
  339. (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) &= ~(1 << ((bit) & 7)))
  340. #define ubh_isset(ubh,begin,bit) \
  341. (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) & (1 << ((bit) & 7)))
  342. #define ubh_isclr(ubh,begin,bit) (!ubh_isset(ubh,begin,bit))
  343. #define ubh_find_first_zero_bit(ubh,begin,size) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,0)
  344. #define ubh_find_next_zero_bit(ubh,begin,size,offset) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,offset)
  345. static inline unsigned _ubh_find_next_zero_bit_(
  346. struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
  347. unsigned begin, unsigned size, unsigned offset)
  348. {
  349. unsigned base, count, pos;
  350. size -= offset;
  351. begin <<= 3;
  352. offset += begin;
  353. base = offset >> uspi->s_bpfshift;
  354. offset &= uspi->s_bpfmask;
  355. for (;;) {
  356. count = min_t(unsigned int, size + offset, uspi->s_bpf);
  357. size -= count - offset;
  358. pos = find_next_zero_bit_le(ubh->bh[base]->b_data, count, offset);
  359. if (pos < count || !size)
  360. break;
  361. base++;
  362. offset = 0;
  363. }
  364. return (base << uspi->s_bpfshift) + pos - begin;
  365. }
  366. static inline unsigned find_last_zero_bit (unsigned char * bitmap,
  367. unsigned size, unsigned offset)
  368. {
  369. unsigned bit, i;
  370. unsigned char * mapp;
  371. unsigned char map;
  372. mapp = bitmap + (size >> 3);
  373. map = *mapp--;
  374. bit = 1 << (size & 7);
  375. for (i = size; i > offset; i--) {
  376. if ((map & bit) == 0)
  377. break;
  378. if ((i & 7) != 0) {
  379. bit >>= 1;
  380. } else {
  381. map = *mapp--;
  382. bit = 1 << 7;
  383. }
  384. }
  385. return i;
  386. }
  387. #define ubh_find_last_zero_bit(ubh,begin,size,offset) _ubh_find_last_zero_bit_(uspi,ubh,begin,size,offset)
  388. static inline unsigned _ubh_find_last_zero_bit_(
  389. struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
  390. unsigned begin, unsigned start, unsigned end)
  391. {
  392. unsigned base, count, pos, size;
  393. size = start - end;
  394. begin <<= 3;
  395. start += begin;
  396. base = start >> uspi->s_bpfshift;
  397. start &= uspi->s_bpfmask;
  398. for (;;) {
  399. count = min_t(unsigned int,
  400. size + (uspi->s_bpf - start), uspi->s_bpf)
  401. - (uspi->s_bpf - start);
  402. size -= count;
  403. pos = find_last_zero_bit (ubh->bh[base]->b_data,
  404. start, start - count);
  405. if (pos > start - count || !size)
  406. break;
  407. base--;
  408. start = uspi->s_bpf;
  409. }
  410. return (base << uspi->s_bpfshift) + pos - begin;
  411. }
  412. #define ubh_isblockclear(ubh,begin,block) (!_ubh_isblockset_(uspi,ubh,begin,block))
  413. #define ubh_isblockset(ubh,begin,block) _ubh_isblockset_(uspi,ubh,begin,block)
  414. static inline int _ubh_isblockset_(struct ufs_sb_private_info * uspi,
  415. struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
  416. {
  417. u8 mask;
  418. switch (uspi->s_fpb) {
  419. case 8:
  420. return (*ubh_get_addr (ubh, begin + block) == 0xff);
  421. case 4:
  422. mask = 0x0f << ((block & 0x01) << 2);
  423. return (*ubh_get_addr (ubh, begin + (block >> 1)) & mask) == mask;
  424. case 2:
  425. mask = 0x03 << ((block & 0x03) << 1);
  426. return (*ubh_get_addr (ubh, begin + (block >> 2)) & mask) == mask;
  427. case 1:
  428. mask = 0x01 << (block & 0x07);
  429. return (*ubh_get_addr (ubh, begin + (block >> 3)) & mask) == mask;
  430. }
  431. return 0;
  432. }
  433. #define ubh_clrblock(ubh,begin,block) _ubh_clrblock_(uspi,ubh,begin,block)
  434. static inline void _ubh_clrblock_(struct ufs_sb_private_info * uspi,
  435. struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
  436. {
  437. switch (uspi->s_fpb) {
  438. case 8:
  439. *ubh_get_addr (ubh, begin + block) = 0x00;
  440. return;
  441. case 4:
  442. *ubh_get_addr (ubh, begin + (block >> 1)) &= ~(0x0f << ((block & 0x01) << 2));
  443. return;
  444. case 2:
  445. *ubh_get_addr (ubh, begin + (block >> 2)) &= ~(0x03 << ((block & 0x03) << 1));
  446. return;
  447. case 1:
  448. *ubh_get_addr (ubh, begin + (block >> 3)) &= ~(0x01 << ((block & 0x07)));
  449. return;
  450. }
  451. }
  452. #define ubh_setblock(ubh,begin,block) _ubh_setblock_(uspi,ubh,begin,block)
  453. static inline void _ubh_setblock_(struct ufs_sb_private_info * uspi,
  454. struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
  455. {
  456. switch (uspi->s_fpb) {
  457. case 8:
  458. *ubh_get_addr(ubh, begin + block) = 0xff;
  459. return;
  460. case 4:
  461. *ubh_get_addr(ubh, begin + (block >> 1)) |= (0x0f << ((block & 0x01) << 2));
  462. return;
  463. case 2:
  464. *ubh_get_addr(ubh, begin + (block >> 2)) |= (0x03 << ((block & 0x03) << 1));
  465. return;
  466. case 1:
  467. *ubh_get_addr(ubh, begin + (block >> 3)) |= (0x01 << ((block & 0x07)));
  468. return;
  469. }
  470. }
  471. static inline void ufs_fragacct (struct super_block * sb, unsigned blockmap,
  472. __fs32 * fraglist, int cnt)
  473. {
  474. struct ufs_sb_private_info * uspi;
  475. unsigned fragsize, pos;
  476. uspi = UFS_SB(sb)->s_uspi;
  477. fragsize = 0;
  478. for (pos = 0; pos < uspi->s_fpb; pos++) {
  479. if (blockmap & (1 << pos)) {
  480. fragsize++;
  481. }
  482. else if (fragsize > 0) {
  483. fs32_add(sb, &fraglist[fragsize], cnt);
  484. fragsize = 0;
  485. }
  486. }
  487. if (fragsize > 0 && fragsize < uspi->s_fpb)
  488. fs32_add(sb, &fraglist[fragsize], cnt);
  489. }
  490. static inline void *ufs_get_direct_data_ptr(struct ufs_sb_private_info *uspi,
  491. struct ufs_inode_info *ufsi,
  492. unsigned blk)
  493. {
  494. BUG_ON(blk > UFS_TIND_BLOCK);
  495. return uspi->fs_magic == UFS2_MAGIC ?
  496. (void *)&ufsi->i_u1.u2_i_data[blk] :
  497. (void *)&ufsi->i_u1.i_data[blk];
  498. }
  499. static inline u64 ufs_data_ptr_to_cpu(struct super_block *sb, void *p)
  500. {
  501. return UFS_SB(sb)->s_uspi->fs_magic == UFS2_MAGIC ?
  502. fs64_to_cpu(sb, *(__fs64 *)p) :
  503. fs32_to_cpu(sb, *(__fs32 *)p);
  504. }
  505. static inline void ufs_cpu_to_data_ptr(struct super_block *sb, void *p, u64 val)
  506. {
  507. if (UFS_SB(sb)->s_uspi->fs_magic == UFS2_MAGIC)
  508. *(__fs64 *)p = cpu_to_fs64(sb, val);
  509. else
  510. *(__fs32 *)p = cpu_to_fs32(sb, val);
  511. }
  512. static inline void ufs_data_ptr_clear(struct ufs_sb_private_info *uspi,
  513. void *p)
  514. {
  515. if (uspi->fs_magic == UFS2_MAGIC)
  516. *(__fs64 *)p = 0;
  517. else
  518. *(__fs32 *)p = 0;
  519. }
  520. static inline int ufs_is_data_ptr_zero(struct ufs_sb_private_info *uspi,
  521. void *p)
  522. {
  523. if (uspi->fs_magic == UFS2_MAGIC)
  524. return *(__fs64 *)p == 0;
  525. else
  526. return *(__fs32 *)p == 0;
  527. }