util.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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_mark_buffer_uptodate (struct ufs_buffer_head *, int);
  244. extern void ubh_sync_block(struct ufs_buffer_head *);
  245. extern void ubh_bforget (struct ufs_buffer_head *);
  246. extern int ubh_buffer_dirty (struct ufs_buffer_head *);
  247. #define ubh_ubhcpymem(mem,ubh,size) _ubh_ubhcpymem_(uspi,mem,ubh,size)
  248. extern void _ubh_ubhcpymem_(struct ufs_sb_private_info *, unsigned char *, struct ufs_buffer_head *, unsigned);
  249. #define ubh_memcpyubh(ubh,mem,size) _ubh_memcpyubh_(uspi,ubh,mem,size)
  250. extern void _ubh_memcpyubh_(struct ufs_sb_private_info *, struct ufs_buffer_head *, unsigned char *, unsigned);
  251. /* This functions works with cache pages*/
  252. struct folio *ufs_get_locked_folio(struct address_space *mapping, pgoff_t index);
  253. static inline void ufs_put_locked_folio(struct folio *folio)
  254. {
  255. folio_unlock(folio);
  256. folio_put(folio);
  257. }
  258. /*
  259. * macros and inline function to get important structures from ufs_sb_private_info
  260. */
  261. static inline void *get_usb_offset(struct ufs_sb_private_info *uspi,
  262. unsigned int offset)
  263. {
  264. unsigned int index;
  265. index = offset >> uspi->s_fshift;
  266. offset &= ~uspi->s_fmask;
  267. return uspi->s_ubh.bh[index]->b_data + offset;
  268. }
  269. #define ubh_get_usb_first(uspi) \
  270. ((struct ufs_super_block_first *)get_usb_offset((uspi), 0))
  271. #define ubh_get_usb_second(uspi) \
  272. ((struct ufs_super_block_second *)get_usb_offset((uspi), UFS_SECTOR_SIZE))
  273. #define ubh_get_usb_third(uspi) \
  274. ((struct ufs_super_block_third *)get_usb_offset((uspi), 2*UFS_SECTOR_SIZE))
  275. #define ubh_get_ucg(ubh) \
  276. ((struct ufs_cylinder_group *)((ubh)->bh[0]->b_data))
  277. /*
  278. * Extract byte from ufs_buffer_head
  279. * Extract the bits for a block from a map inside ufs_buffer_head
  280. */
  281. #define ubh_get_addr8(ubh,begin) \
  282. ((u8*)(ubh)->bh[(begin) >> uspi->s_fshift]->b_data + \
  283. ((begin) & ~uspi->s_fmask))
  284. #define ubh_get_addr16(ubh,begin) \
  285. (((__fs16*)((ubh)->bh[(begin) >> (uspi->s_fshift-1)]->b_data)) + \
  286. ((begin) & ((uspi->fsize>>1) - 1)))
  287. #define ubh_get_addr32(ubh,begin) \
  288. (((__fs32*)((ubh)->bh[(begin) >> (uspi->s_fshift-2)]->b_data)) + \
  289. ((begin) & ((uspi->s_fsize>>2) - 1)))
  290. #define ubh_get_addr64(ubh,begin) \
  291. (((__fs64*)((ubh)->bh[(begin) >> (uspi->s_fshift-3)]->b_data)) + \
  292. ((begin) & ((uspi->s_fsize>>3) - 1)))
  293. #define ubh_get_addr ubh_get_addr8
  294. static inline void *ubh_get_data_ptr(struct ufs_sb_private_info *uspi,
  295. struct ufs_buffer_head *ubh,
  296. u64 blk)
  297. {
  298. if (uspi->fs_magic == UFS2_MAGIC)
  299. return ubh_get_addr64(ubh, blk);
  300. else
  301. return ubh_get_addr32(ubh, blk);
  302. }
  303. #define ubh_blkmap(ubh,begin,bit) \
  304. ((*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) >> ((bit) & 7)) & (0xff >> (UFS_MAXFRAG - uspi->s_fpb)))
  305. static inline u64
  306. ufs_freefrags(struct ufs_sb_private_info *uspi)
  307. {
  308. return ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
  309. uspi->cs_total.cs_nffree;
  310. }
  311. /*
  312. * Macros to access cylinder group array structures
  313. */
  314. #define ubh_cg_blktot(ucpi,cylno) \
  315. (*((__fs32*)ubh_get_addr(UCPI_UBH(ucpi), (ucpi)->c_btotoff + ((cylno) << 2))))
  316. #define ubh_cg_blks(ucpi,cylno,rpos) \
  317. (*((__fs16*)ubh_get_addr(UCPI_UBH(ucpi), \
  318. (ucpi)->c_boff + (((cylno) * uspi->s_nrpos + (rpos)) << 1 ))))
  319. /*
  320. * Bitmap operations
  321. * These functions work like classical bitmap operations.
  322. * The difference is that we don't have the whole bitmap
  323. * in one contiguous chunk of memory, but in several buffers.
  324. * The parameters of each function are super_block, ufs_buffer_head and
  325. * position of the beginning of the bitmap.
  326. */
  327. #define ubh_setbit(ubh,begin,bit) \
  328. (*ubh_get_addr(ubh, (begin) + ((bit) >> 3)) |= (1 << ((bit) & 7)))
  329. #define ubh_clrbit(ubh,begin,bit) \
  330. (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) &= ~(1 << ((bit) & 7)))
  331. #define ubh_isset(ubh,begin,bit) \
  332. (*ubh_get_addr (ubh, (begin) + ((bit) >> 3)) & (1 << ((bit) & 7)))
  333. #define ubh_isclr(ubh,begin,bit) (!ubh_isset(ubh,begin,bit))
  334. #define ubh_find_first_zero_bit(ubh,begin,size) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,0)
  335. #define ubh_find_next_zero_bit(ubh,begin,size,offset) _ubh_find_next_zero_bit_(uspi,ubh,begin,size,offset)
  336. static inline unsigned _ubh_find_next_zero_bit_(
  337. struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
  338. unsigned begin, unsigned size, unsigned offset)
  339. {
  340. unsigned base, count, pos;
  341. size -= offset;
  342. begin <<= 3;
  343. offset += begin;
  344. base = offset >> uspi->s_bpfshift;
  345. offset &= uspi->s_bpfmask;
  346. for (;;) {
  347. count = min_t(unsigned int, size + offset, uspi->s_bpf);
  348. size -= count - offset;
  349. pos = find_next_zero_bit_le(ubh->bh[base]->b_data, count, offset);
  350. if (pos < count || !size)
  351. break;
  352. base++;
  353. offset = 0;
  354. }
  355. return (base << uspi->s_bpfshift) + pos - begin;
  356. }
  357. static inline unsigned find_last_zero_bit (unsigned char * bitmap,
  358. unsigned size, unsigned offset)
  359. {
  360. unsigned bit, i;
  361. unsigned char * mapp;
  362. unsigned char map;
  363. mapp = bitmap + (size >> 3);
  364. map = *mapp--;
  365. bit = 1 << (size & 7);
  366. for (i = size; i > offset; i--) {
  367. if ((map & bit) == 0)
  368. break;
  369. if ((i & 7) != 0) {
  370. bit >>= 1;
  371. } else {
  372. map = *mapp--;
  373. bit = 1 << 7;
  374. }
  375. }
  376. return i;
  377. }
  378. #define ubh_find_last_zero_bit(ubh,begin,size,offset) _ubh_find_last_zero_bit_(uspi,ubh,begin,size,offset)
  379. static inline unsigned _ubh_find_last_zero_bit_(
  380. struct ufs_sb_private_info * uspi, struct ufs_buffer_head * ubh,
  381. unsigned begin, unsigned start, unsigned end)
  382. {
  383. unsigned base, count, pos, size;
  384. size = start - end;
  385. begin <<= 3;
  386. start += begin;
  387. base = start >> uspi->s_bpfshift;
  388. start &= uspi->s_bpfmask;
  389. for (;;) {
  390. count = min_t(unsigned int,
  391. size + (uspi->s_bpf - start), uspi->s_bpf)
  392. - (uspi->s_bpf - start);
  393. size -= count;
  394. pos = find_last_zero_bit (ubh->bh[base]->b_data,
  395. start, start - count);
  396. if (pos > start - count || !size)
  397. break;
  398. base--;
  399. start = uspi->s_bpf;
  400. }
  401. return (base << uspi->s_bpfshift) + pos - begin;
  402. }
  403. #define ubh_isblockclear(ubh,begin,block) (!_ubh_isblockset_(uspi,ubh,begin,block))
  404. #define ubh_isblockset(ubh,begin,block) _ubh_isblockset_(uspi,ubh,begin,block)
  405. static inline int _ubh_isblockset_(struct ufs_sb_private_info * uspi,
  406. struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
  407. {
  408. u8 mask;
  409. switch (uspi->s_fpb) {
  410. case 8:
  411. return (*ubh_get_addr (ubh, begin + block) == 0xff);
  412. case 4:
  413. mask = 0x0f << ((block & 0x01) << 2);
  414. return (*ubh_get_addr (ubh, begin + (block >> 1)) & mask) == mask;
  415. case 2:
  416. mask = 0x03 << ((block & 0x03) << 1);
  417. return (*ubh_get_addr (ubh, begin + (block >> 2)) & mask) == mask;
  418. case 1:
  419. mask = 0x01 << (block & 0x07);
  420. return (*ubh_get_addr (ubh, begin + (block >> 3)) & mask) == mask;
  421. }
  422. return 0;
  423. }
  424. #define ubh_clrblock(ubh,begin,block) _ubh_clrblock_(uspi,ubh,begin,block)
  425. static inline void _ubh_clrblock_(struct ufs_sb_private_info * uspi,
  426. struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
  427. {
  428. switch (uspi->s_fpb) {
  429. case 8:
  430. *ubh_get_addr (ubh, begin + block) = 0x00;
  431. return;
  432. case 4:
  433. *ubh_get_addr (ubh, begin + (block >> 1)) &= ~(0x0f << ((block & 0x01) << 2));
  434. return;
  435. case 2:
  436. *ubh_get_addr (ubh, begin + (block >> 2)) &= ~(0x03 << ((block & 0x03) << 1));
  437. return;
  438. case 1:
  439. *ubh_get_addr (ubh, begin + (block >> 3)) &= ~(0x01 << ((block & 0x07)));
  440. return;
  441. }
  442. }
  443. #define ubh_setblock(ubh,begin,block) _ubh_setblock_(uspi,ubh,begin,block)
  444. static inline void _ubh_setblock_(struct ufs_sb_private_info * uspi,
  445. struct ufs_buffer_head * ubh, unsigned begin, unsigned block)
  446. {
  447. switch (uspi->s_fpb) {
  448. case 8:
  449. *ubh_get_addr(ubh, begin + block) = 0xff;
  450. return;
  451. case 4:
  452. *ubh_get_addr(ubh, begin + (block >> 1)) |= (0x0f << ((block & 0x01) << 2));
  453. return;
  454. case 2:
  455. *ubh_get_addr(ubh, begin + (block >> 2)) |= (0x03 << ((block & 0x03) << 1));
  456. return;
  457. case 1:
  458. *ubh_get_addr(ubh, begin + (block >> 3)) |= (0x01 << ((block & 0x07)));
  459. return;
  460. }
  461. }
  462. static inline void ufs_fragacct (struct super_block * sb, unsigned blockmap,
  463. __fs32 * fraglist, int cnt)
  464. {
  465. struct ufs_sb_private_info * uspi;
  466. unsigned fragsize, pos;
  467. uspi = UFS_SB(sb)->s_uspi;
  468. fragsize = 0;
  469. for (pos = 0; pos < uspi->s_fpb; pos++) {
  470. if (blockmap & (1 << pos)) {
  471. fragsize++;
  472. }
  473. else if (fragsize > 0) {
  474. fs32_add(sb, &fraglist[fragsize], cnt);
  475. fragsize = 0;
  476. }
  477. }
  478. if (fragsize > 0 && fragsize < uspi->s_fpb)
  479. fs32_add(sb, &fraglist[fragsize], cnt);
  480. }
  481. static inline void *ufs_get_direct_data_ptr(struct ufs_sb_private_info *uspi,
  482. struct ufs_inode_info *ufsi,
  483. unsigned blk)
  484. {
  485. BUG_ON(blk > UFS_TIND_BLOCK);
  486. return uspi->fs_magic == UFS2_MAGIC ?
  487. (void *)&ufsi->i_u1.u2_i_data[blk] :
  488. (void *)&ufsi->i_u1.i_data[blk];
  489. }
  490. static inline u64 ufs_data_ptr_to_cpu(struct super_block *sb, void *p)
  491. {
  492. return UFS_SB(sb)->s_uspi->fs_magic == UFS2_MAGIC ?
  493. fs64_to_cpu(sb, *(__fs64 *)p) :
  494. fs32_to_cpu(sb, *(__fs32 *)p);
  495. }
  496. static inline void ufs_cpu_to_data_ptr(struct super_block *sb, void *p, u64 val)
  497. {
  498. if (UFS_SB(sb)->s_uspi->fs_magic == UFS2_MAGIC)
  499. *(__fs64 *)p = cpu_to_fs64(sb, val);
  500. else
  501. *(__fs32 *)p = cpu_to_fs32(sb, val);
  502. }
  503. static inline void ufs_data_ptr_clear(struct ufs_sb_private_info *uspi,
  504. void *p)
  505. {
  506. if (uspi->fs_magic == UFS2_MAGIC)
  507. *(__fs64 *)p = 0;
  508. else
  509. *(__fs32 *)p = 0;
  510. }
  511. static inline int ufs_is_data_ptr_zero(struct ufs_sb_private_info *uspi,
  512. void *p)
  513. {
  514. if (uspi->fs_magic == UFS2_MAGIC)
  515. return *(__fs64 *)p == 0;
  516. else
  517. return *(__fs32 *)p == 0;
  518. }
  519. static inline __fs32 ufs_get_seconds(struct super_block *sbp)
  520. {
  521. time64_t now = ktime_get_real_seconds();
  522. /* Signed 32-bit interpretation wraps around in 2038, which
  523. * happens in ufs1 inode stamps but not ufs2 using 64-bits
  524. * stamps. For superblock and blockgroup, let's assume
  525. * unsigned 32-bit stamps, which are good until y2106.
  526. * Wrap around rather than clamp here to make the dirty
  527. * file system detection work in the superblock stamp.
  528. */
  529. return cpu_to_fs32(sbp, lower_32_bits(now));
  530. }