util.h 16 KB

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