util.h 17 KB

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