f2fs.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. /*
  2. * f2fs.c - Flash-Friendly File System
  3. *
  4. * Written by Jaegeuk Kim <jaegeuk@kernel.org>
  5. *
  6. * Copyright (C) 2015 Free Software Foundation, Inc.
  7. *
  8. * GRUB is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * GRUB is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <grub/err.h>
  22. #include <grub/file.h>
  23. #include <grub/mm.h>
  24. #include <grub/misc.h>
  25. #include <grub/disk.h>
  26. #include <grub/dl.h>
  27. #include <grub/types.h>
  28. #include <grub/charset.h>
  29. #include <grub/fshelp.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. /* F2FS Magic Number. */
  32. #define F2FS_SUPER_MAGIC 0xf2f52010
  33. #define CHECKSUM_OFFSET 4092 /* Must be aligned 4 bytes. */
  34. #define U32_CHECKSUM_OFFSET (CHECKSUM_OFFSET >> 2)
  35. #define CRCPOLY_LE 0xedb88320
  36. /* Byte-size offset. */
  37. #define F2FS_SUPER_OFFSET ((grub_disk_addr_t)1024)
  38. #define F2FS_SUPER_OFFSET0 (F2FS_SUPER_OFFSET >> GRUB_DISK_SECTOR_BITS)
  39. #define F2FS_SUPER_OFFSET1 ((F2FS_SUPER_OFFSET + F2FS_BLKSIZE) >> \
  40. GRUB_DISK_SECTOR_BITS)
  41. /* 9 bits for 512 bytes. */
  42. #define F2FS_MIN_LOG_SECTOR_SIZE 9
  43. /* Support only 4KB block. */
  44. #define F2FS_BLK_BITS 12
  45. #define F2FS_BLKSIZE (1 << F2FS_BLK_BITS)
  46. #define F2FS_BLK_SEC_BITS (F2FS_BLK_BITS - GRUB_DISK_SECTOR_BITS)
  47. #define VERSION_LEN 256
  48. #define F2FS_MAX_EXTENSION 64
  49. #define CP_COMPACT_SUM_FLAG 0x00000004
  50. #define CP_UMOUNT_FLAG 0x00000001
  51. #define MAX_ACTIVE_LOGS 16
  52. #define MAX_ACTIVE_NODE_LOGS 8
  53. #define MAX_ACTIVE_DATA_LOGS 8
  54. #define NR_CURSEG_DATA_TYPE 3
  55. #define NR_CURSEG_NODE_TYPE 3
  56. #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
  57. #define ENTRIES_IN_SUM 512
  58. #define SUMMARY_SIZE 7
  59. #define SUM_FOOTER_SIZE 5
  60. #define JENTRY_SIZE (sizeof(struct grub_f2fs_nat_jent))
  61. #define SUM_ENTRIES_SIZE (SUMMARY_SIZE * ENTRIES_IN_SUM)
  62. #define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE - SUM_ENTRIES_SIZE)
  63. #define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) / JENTRY_SIZE)
  64. #define NAT_JOURNAL_RESERVED ((SUM_JOURNAL_SIZE - 2) % JENTRY_SIZE)
  65. #define NAT_ENTRY_SIZE (sizeof(struct grub_f2fs_nat_entry))
  66. #define NAT_ENTRY_PER_BLOCK (F2FS_BLKSIZE / NAT_ENTRY_SIZE)
  67. #define F2FS_NAME_LEN 255
  68. #define F2FS_SLOT_LEN 8
  69. #define NR_DENTRY_IN_BLOCK 214
  70. #define SIZE_OF_DIR_ENTRY 11 /* By byte. */
  71. #define BITS_PER_BYTE 8
  72. #define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
  73. BITS_PER_BYTE)
  74. #define SIZE_OF_RESERVED (F2FS_BLKSIZE - \
  75. ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
  76. NR_DENTRY_IN_BLOCK + SIZE_OF_DENTRY_BITMAP))
  77. #define F2FS_INLINE_XATTR_ADDRS 50 /* 200 bytes for inline xattrs. */
  78. #define DEF_ADDRS_PER_INODE 923 /* Address Pointers in an Inode. */
  79. #define ADDRS_PER_BLOCK 1018 /* Address Pointers in a Direct Block. */
  80. #define NIDS_PER_BLOCK 1018 /* Node IDs in an Indirect Block. */
  81. #define NODE_DIR1_BLOCK (DEF_ADDRS_PER_INODE + 1)
  82. #define NODE_DIR2_BLOCK (DEF_ADDRS_PER_INODE + 2)
  83. #define NODE_IND1_BLOCK (DEF_ADDRS_PER_INODE + 3)
  84. #define NODE_IND2_BLOCK (DEF_ADDRS_PER_INODE + 4)
  85. #define NODE_DIND_BLOCK (DEF_ADDRS_PER_INODE + 5)
  86. #define MAX_INLINE_DATA (4 * (DEF_ADDRS_PER_INODE - \
  87. F2FS_INLINE_XATTR_ADDRS - 1))
  88. #define NR_INLINE_DENTRY (MAX_INLINE_DATA * BITS_PER_BYTE / \
  89. ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
  90. BITS_PER_BYTE + 1))
  91. #define INLINE_DENTRY_BITMAP_SIZE ((NR_INLINE_DENTRY + BITS_PER_BYTE - 1) / \
  92. BITS_PER_BYTE)
  93. #define INLINE_RESERVED_SIZE (MAX_INLINE_DATA - \
  94. ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
  95. NR_INLINE_DENTRY + \
  96. INLINE_DENTRY_BITMAP_SIZE))
  97. #define CURSEG_HOT_DATA 0
  98. #define CKPT_FLAG_SET(ckpt, f) (ckpt)->ckpt_flags & \
  99. grub_cpu_to_le32_compile_time (f)
  100. #define F2FS_INLINE_XATTR 0x01 /* File inline xattr flag. */
  101. #define F2FS_INLINE_DATA 0x02 /* File inline data flag. */
  102. #define F2FS_INLINE_DENTRY 0x04 /* File inline dentry flag. */
  103. #define F2FS_DATA_EXIST 0x08 /* File inline data exist flag. */
  104. #define F2FS_INLINE_DOTS 0x10 /* File having implicit dot dentries. */
  105. #define MAX_VOLUME_NAME 512
  106. #define MAX_NAT_BITMAP_SIZE 3900
  107. enum FILE_TYPE
  108. {
  109. F2FS_FT_UNKNOWN,
  110. F2FS_FT_REG_FILE = 1,
  111. F2FS_FT_DIR = 2,
  112. F2FS_FT_SYMLINK = 7
  113. };
  114. struct grub_f2fs_superblock
  115. {
  116. grub_uint32_t magic;
  117. grub_uint16_t dummy1[2];
  118. grub_uint32_t log_sectorsize;
  119. grub_uint32_t log_sectors_per_block;
  120. grub_uint32_t log_blocksize;
  121. grub_uint32_t log_blocks_per_seg;
  122. grub_uint32_t segs_per_sec;
  123. grub_uint32_t secs_per_zone;
  124. grub_uint32_t checksum_offset;
  125. grub_uint8_t dummy2[40];
  126. grub_uint32_t cp_blkaddr;
  127. grub_uint32_t sit_blkaddr;
  128. grub_uint32_t nat_blkaddr;
  129. grub_uint32_t ssa_blkaddr;
  130. grub_uint32_t main_blkaddr;
  131. grub_uint32_t root_ino;
  132. grub_uint32_t node_ino;
  133. grub_uint32_t meta_ino;
  134. grub_uint8_t uuid[16];
  135. grub_uint16_t volume_name[MAX_VOLUME_NAME];
  136. grub_uint32_t extension_count;
  137. grub_uint8_t extension_list[F2FS_MAX_EXTENSION][8];
  138. grub_uint32_t cp_payload;
  139. grub_uint8_t version[VERSION_LEN];
  140. grub_uint8_t init_version[VERSION_LEN];
  141. } GRUB_PACKED;
  142. struct grub_f2fs_checkpoint
  143. {
  144. grub_uint64_t checkpoint_ver;
  145. grub_uint64_t user_block_count;
  146. grub_uint64_t valid_block_count;
  147. grub_uint32_t rsvd_segment_count;
  148. grub_uint32_t overprov_segment_count;
  149. grub_uint32_t free_segment_count;
  150. grub_uint32_t cur_node_segno[MAX_ACTIVE_NODE_LOGS];
  151. grub_uint16_t cur_node_blkoff[MAX_ACTIVE_NODE_LOGS];
  152. grub_uint32_t cur_data_segno[MAX_ACTIVE_DATA_LOGS];
  153. grub_uint16_t cur_data_blkoff[MAX_ACTIVE_DATA_LOGS];
  154. grub_uint32_t ckpt_flags;
  155. grub_uint32_t cp_pack_total_block_count;
  156. grub_uint32_t cp_pack_start_sum;
  157. grub_uint32_t valid_node_count;
  158. grub_uint32_t valid_inode_count;
  159. grub_uint32_t next_free_nid;
  160. grub_uint32_t sit_ver_bitmap_bytesize;
  161. grub_uint32_t nat_ver_bitmap_bytesize;
  162. grub_uint32_t checksum_offset;
  163. grub_uint64_t elapsed_time;
  164. grub_uint8_t alloc_type[MAX_ACTIVE_LOGS];
  165. grub_uint8_t sit_nat_version_bitmap[MAX_NAT_BITMAP_SIZE];
  166. grub_uint32_t checksum;
  167. } GRUB_PACKED;
  168. struct grub_f2fs_nat_entry {
  169. grub_uint8_t version;
  170. grub_uint32_t ino;
  171. grub_uint32_t block_addr;
  172. } GRUB_PACKED;
  173. struct grub_f2fs_nat_jent
  174. {
  175. grub_uint32_t nid;
  176. struct grub_f2fs_nat_entry ne;
  177. } GRUB_PACKED;
  178. struct grub_f2fs_nat_journal {
  179. grub_uint16_t n_nats;
  180. struct grub_f2fs_nat_jent entries[NAT_JOURNAL_ENTRIES];
  181. grub_uint8_t reserved[NAT_JOURNAL_RESERVED];
  182. } GRUB_PACKED;
  183. struct grub_f2fs_nat_block {
  184. struct grub_f2fs_nat_entry ne[NAT_ENTRY_PER_BLOCK];
  185. } GRUB_PACKED;
  186. struct grub_f2fs_dir_entry
  187. {
  188. grub_uint32_t hash_code;
  189. grub_uint32_t ino;
  190. grub_uint16_t name_len;
  191. grub_uint8_t file_type;
  192. } GRUB_PACKED;
  193. struct grub_f2fs_inline_dentry
  194. {
  195. grub_uint8_t dentry_bitmap[INLINE_DENTRY_BITMAP_SIZE];
  196. grub_uint8_t reserved[INLINE_RESERVED_SIZE];
  197. struct grub_f2fs_dir_entry dentry[NR_INLINE_DENTRY];
  198. grub_uint8_t filename[NR_INLINE_DENTRY][F2FS_SLOT_LEN];
  199. } GRUB_PACKED;
  200. struct grub_f2fs_dentry_block {
  201. grub_uint8_t dentry_bitmap[SIZE_OF_DENTRY_BITMAP];
  202. grub_uint8_t reserved[SIZE_OF_RESERVED];
  203. struct grub_f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
  204. grub_uint8_t filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
  205. } GRUB_PACKED;
  206. struct grub_f2fs_inode
  207. {
  208. grub_uint16_t i_mode;
  209. grub_uint8_t i_advise;
  210. grub_uint8_t i_inline;
  211. grub_uint32_t i_uid;
  212. grub_uint32_t i_gid;
  213. grub_uint32_t i_links;
  214. grub_uint64_t i_size;
  215. grub_uint64_t i_blocks;
  216. grub_uint64_t i_atime;
  217. grub_uint64_t i_ctime;
  218. grub_uint64_t i_mtime;
  219. grub_uint32_t i_atime_nsec;
  220. grub_uint32_t i_ctime_nsec;
  221. grub_uint32_t i_mtime_nsec;
  222. grub_uint32_t i_generation;
  223. grub_uint32_t i_current_depth;
  224. grub_uint32_t i_xattr_nid;
  225. grub_uint32_t i_flags;
  226. grub_uint32_t i_pino;
  227. grub_uint32_t i_namelen;
  228. grub_uint8_t i_name[F2FS_NAME_LEN];
  229. grub_uint8_t i_dir_level;
  230. grub_uint8_t i_ext[12];
  231. grub_uint32_t i_addr[DEF_ADDRS_PER_INODE];
  232. grub_uint32_t i_nid[5];
  233. } GRUB_PACKED;
  234. struct grub_direct_node {
  235. grub_uint32_t addr[ADDRS_PER_BLOCK];
  236. } GRUB_PACKED;
  237. struct grub_indirect_node {
  238. grub_uint32_t nid[NIDS_PER_BLOCK];
  239. } GRUB_PACKED;
  240. struct grub_f2fs_node
  241. {
  242. union
  243. {
  244. struct grub_f2fs_inode i;
  245. struct grub_direct_node dn;
  246. struct grub_indirect_node in;
  247. /* Should occupy F2FS_BLKSIZE totally. */
  248. char buf[F2FS_BLKSIZE - 40];
  249. };
  250. grub_uint8_t dummy[40];
  251. } GRUB_PACKED;
  252. struct grub_fshelp_node
  253. {
  254. struct grub_f2fs_data *data;
  255. struct grub_f2fs_node inode;
  256. grub_uint32_t ino;
  257. int inode_read;
  258. };
  259. struct grub_f2fs_data
  260. {
  261. struct grub_f2fs_superblock sblock;
  262. struct grub_f2fs_checkpoint ckpt;
  263. grub_uint32_t root_ino;
  264. grub_uint32_t blocks_per_seg;
  265. grub_uint32_t cp_blkaddr;
  266. grub_uint32_t nat_blkaddr;
  267. struct grub_f2fs_nat_journal nat_j;
  268. char *nat_bitmap;
  269. grub_uint32_t nat_bitmap_size;
  270. grub_disk_t disk;
  271. struct grub_f2fs_node *inode;
  272. struct grub_fshelp_node diropen;
  273. };
  274. struct grub_f2fs_dir_iter_ctx
  275. {
  276. struct grub_f2fs_data *data;
  277. grub_fshelp_iterate_dir_hook_t hook;
  278. void *hook_data;
  279. grub_uint8_t *bitmap;
  280. grub_uint8_t (*filename)[F2FS_SLOT_LEN];
  281. struct grub_f2fs_dir_entry *dentry;
  282. int max;
  283. };
  284. struct grub_f2fs_dir_ctx
  285. {
  286. grub_fs_dir_hook_t hook;
  287. void *hook_data;
  288. struct grub_f2fs_data *data;
  289. };
  290. static grub_dl_t my_mod;
  291. static int
  292. grub_f2fs_test_bit_le (int nr, const grub_uint8_t *addr)
  293. {
  294. return addr[nr >> 3] & (1 << (nr & 7));
  295. }
  296. static char *
  297. get_inline_addr (struct grub_f2fs_inode *inode)
  298. {
  299. return (char *) &inode->i_addr[1];
  300. }
  301. static grub_uint64_t
  302. grub_f2fs_file_size (struct grub_f2fs_inode *inode)
  303. {
  304. return grub_le_to_cpu64 (inode->i_size);
  305. }
  306. static grub_uint32_t
  307. start_cp_addr (struct grub_f2fs_data *data)
  308. {
  309. struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
  310. grub_uint32_t start_addr = data->cp_blkaddr;
  311. if (!(ckpt->checkpoint_ver & grub_cpu_to_le64_compile_time(1)))
  312. return start_addr + data->blocks_per_seg;
  313. return start_addr;
  314. }
  315. static grub_uint32_t
  316. start_sum_block (struct grub_f2fs_data *data)
  317. {
  318. struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
  319. return start_cp_addr (data) + grub_le_to_cpu32 (ckpt->cp_pack_start_sum);
  320. }
  321. static grub_uint32_t
  322. sum_blk_addr (struct grub_f2fs_data *data, int base, int type)
  323. {
  324. struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
  325. return start_cp_addr (data) +
  326. grub_le_to_cpu32 (ckpt->cp_pack_total_block_count) -
  327. (base + 1) + type;
  328. }
  329. static void *
  330. nat_bitmap_ptr (struct grub_f2fs_data *data, grub_uint32_t *nat_bitmap_size)
  331. {
  332. struct grub_f2fs_checkpoint *ckpt = &data->ckpt;
  333. grub_uint32_t offset;
  334. *nat_bitmap_size = MAX_NAT_BITMAP_SIZE;
  335. if (grub_le_to_cpu32 (data->sblock.cp_payload) > 0)
  336. return ckpt->sit_nat_version_bitmap;
  337. offset = grub_le_to_cpu32 (ckpt->sit_ver_bitmap_bytesize);
  338. if (offset >= MAX_NAT_BITMAP_SIZE)
  339. return NULL;
  340. *nat_bitmap_size = *nat_bitmap_size - offset;
  341. return ckpt->sit_nat_version_bitmap + offset;
  342. }
  343. static grub_uint32_t
  344. get_node_id (struct grub_f2fs_node *rn, int off, int inode_block)
  345. {
  346. if (inode_block)
  347. return grub_le_to_cpu32 (rn->i.i_nid[off - NODE_DIR1_BLOCK]);
  348. return grub_le_to_cpu32 (rn->in.nid[off]);
  349. }
  350. static grub_err_t
  351. grub_f2fs_block_read (struct grub_f2fs_data *data, grub_uint32_t blkaddr,
  352. void *buf)
  353. {
  354. return grub_disk_read (data->disk,
  355. ((grub_disk_addr_t)blkaddr) << F2FS_BLK_SEC_BITS,
  356. 0, F2FS_BLKSIZE, buf);
  357. }
  358. /* CRC32 */
  359. static grub_uint32_t
  360. grub_f2fs_cal_crc32 (const void *buf, const grub_uint32_t len)
  361. {
  362. grub_uint32_t crc = F2FS_SUPER_MAGIC;
  363. unsigned char *p = (unsigned char *)buf;
  364. grub_uint32_t tmp = len;
  365. int i;
  366. while (tmp--)
  367. {
  368. crc ^= *p++;
  369. for (i = 0; i < 8; i++)
  370. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  371. }
  372. return crc;
  373. }
  374. static int
  375. grub_f2fs_crc_valid (grub_uint32_t blk_crc, void *buf, const grub_uint32_t len)
  376. {
  377. grub_uint32_t cal_crc = 0;
  378. cal_crc = grub_f2fs_cal_crc32 (buf, len);
  379. return (cal_crc == blk_crc) ? 1 : 0;
  380. }
  381. static int
  382. grub_f2fs_test_bit (grub_uint32_t nr, const char *p, grub_uint32_t len)
  383. {
  384. int mask;
  385. grub_uint32_t shifted_nr = (nr >> 3);
  386. if (shifted_nr >= len)
  387. return -1;
  388. p += shifted_nr;
  389. mask = 1 << (7 - (nr & 0x07));
  390. return mask & *p;
  391. }
  392. static int
  393. grub_f2fs_sanity_check_sb (struct grub_f2fs_superblock *sb)
  394. {
  395. grub_uint32_t log_sectorsize, log_sectors_per_block;
  396. if (sb->magic != grub_cpu_to_le32_compile_time (F2FS_SUPER_MAGIC))
  397. return -1;
  398. if (sb->log_blocksize != grub_cpu_to_le32_compile_time (F2FS_BLK_BITS))
  399. return -1;
  400. log_sectorsize = grub_le_to_cpu32 (sb->log_sectorsize);
  401. log_sectors_per_block = grub_le_to_cpu32 (sb->log_sectors_per_block);
  402. if (log_sectorsize > F2FS_BLK_BITS)
  403. return -1;
  404. if (log_sectorsize < F2FS_MIN_LOG_SECTOR_SIZE)
  405. return -1;
  406. if (log_sectors_per_block + log_sectorsize != F2FS_BLK_BITS)
  407. return -1;
  408. return 0;
  409. }
  410. static int
  411. grub_f2fs_read_sb (struct grub_f2fs_data *data, grub_disk_addr_t offset)
  412. {
  413. grub_disk_t disk = data->disk;
  414. grub_err_t err;
  415. /* Read first super block. */
  416. err = grub_disk_read (disk, offset, 0, sizeof (data->sblock), &data->sblock);
  417. if (err)
  418. return -1;
  419. return grub_f2fs_sanity_check_sb (&data->sblock);
  420. }
  421. static void *
  422. validate_checkpoint (struct grub_f2fs_data *data, grub_uint32_t cp_addr,
  423. grub_uint64_t *version)
  424. {
  425. grub_uint32_t *cp_page_1, *cp_page_2;
  426. struct grub_f2fs_checkpoint *cp_block;
  427. grub_uint64_t cur_version = 0, pre_version = 0;
  428. grub_uint32_t crc = 0;
  429. grub_uint32_t crc_offset;
  430. grub_err_t err;
  431. /* Read the 1st cp block in this CP pack. */
  432. cp_page_1 = grub_malloc (F2FS_BLKSIZE);
  433. if (!cp_page_1)
  434. return NULL;
  435. err = grub_f2fs_block_read (data, cp_addr, cp_page_1);
  436. if (err)
  437. goto invalid_cp1;
  438. cp_block = (struct grub_f2fs_checkpoint *)cp_page_1;
  439. crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
  440. if (crc_offset != CHECKSUM_OFFSET)
  441. goto invalid_cp1;
  442. crc = grub_le_to_cpu32 (*(cp_page_1 + U32_CHECKSUM_OFFSET));
  443. if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
  444. goto invalid_cp1;
  445. pre_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
  446. /* Read the 2nd cp block in this CP pack. */
  447. cp_page_2 = grub_malloc (F2FS_BLKSIZE);
  448. if (!cp_page_2)
  449. goto invalid_cp1;
  450. cp_addr += grub_le_to_cpu32 (cp_block->cp_pack_total_block_count) - 1;
  451. err = grub_f2fs_block_read (data, cp_addr, cp_page_2);
  452. if (err)
  453. goto invalid_cp2;
  454. cp_block = (struct grub_f2fs_checkpoint *)cp_page_2;
  455. crc_offset = grub_le_to_cpu32 (cp_block->checksum_offset);
  456. if (crc_offset != CHECKSUM_OFFSET)
  457. goto invalid_cp2;
  458. crc = grub_le_to_cpu32 (*(cp_page_2 + U32_CHECKSUM_OFFSET));
  459. if (!grub_f2fs_crc_valid (crc, cp_block, crc_offset))
  460. goto invalid_cp2;
  461. cur_version = grub_le_to_cpu64 (cp_block->checkpoint_ver);
  462. if (cur_version == pre_version)
  463. {
  464. *version = cur_version;
  465. grub_free (cp_page_2);
  466. return cp_page_1;
  467. }
  468. invalid_cp2:
  469. grub_free (cp_page_2);
  470. invalid_cp1:
  471. grub_free (cp_page_1);
  472. return NULL;
  473. }
  474. static grub_err_t
  475. grub_f2fs_read_cp (struct grub_f2fs_data *data)
  476. {
  477. void *cp1, *cp2, *cur_page;
  478. grub_uint64_t cp1_version = 0, cp2_version = 0;
  479. grub_uint64_t cp_start_blk_no;
  480. /*
  481. * Finding out valid cp block involves read both
  482. * sets (cp pack1 and cp pack 2).
  483. */
  484. cp_start_blk_no = data->cp_blkaddr;
  485. cp1 = validate_checkpoint (data, cp_start_blk_no, &cp1_version);
  486. if (!cp1 && grub_errno)
  487. return grub_errno;
  488. /* The second checkpoint pack should start at the next segment. */
  489. cp_start_blk_no += data->blocks_per_seg;
  490. cp2 = validate_checkpoint (data, cp_start_blk_no, &cp2_version);
  491. if (!cp2 && grub_errno)
  492. {
  493. grub_free (cp1);
  494. return grub_errno;
  495. }
  496. if (cp1 && cp2)
  497. cur_page = (cp2_version > cp1_version) ? cp2 : cp1;
  498. else if (cp1)
  499. cur_page = cp1;
  500. else if (cp2)
  501. cur_page = cp2;
  502. else
  503. return grub_error (GRUB_ERR_BAD_FS, "no checkpoints");
  504. grub_memcpy (&data->ckpt, cur_page, F2FS_BLKSIZE);
  505. grub_free (cp1);
  506. grub_free (cp2);
  507. return 0;
  508. }
  509. static grub_err_t
  510. get_nat_journal (struct grub_f2fs_data *data)
  511. {
  512. grub_uint32_t block;
  513. char *buf;
  514. grub_err_t err;
  515. buf = grub_malloc (F2FS_BLKSIZE);
  516. if (!buf)
  517. return grub_errno;
  518. if (CKPT_FLAG_SET(&data->ckpt, CP_COMPACT_SUM_FLAG))
  519. block = start_sum_block (data);
  520. else if (CKPT_FLAG_SET (&data->ckpt, CP_UMOUNT_FLAG))
  521. block = sum_blk_addr (data, NR_CURSEG_TYPE, CURSEG_HOT_DATA);
  522. else
  523. block = sum_blk_addr (data, NR_CURSEG_DATA_TYPE, CURSEG_HOT_DATA);
  524. err = grub_f2fs_block_read (data, block, buf);
  525. if (err)
  526. goto fail;
  527. if (CKPT_FLAG_SET (&data->ckpt, CP_COMPACT_SUM_FLAG))
  528. grub_memcpy (&data->nat_j, buf, SUM_JOURNAL_SIZE);
  529. else
  530. grub_memcpy (&data->nat_j, buf + SUM_ENTRIES_SIZE, SUM_JOURNAL_SIZE);
  531. fail:
  532. grub_free (buf);
  533. return err;
  534. }
  535. static grub_err_t
  536. get_blkaddr_from_nat_journal (struct grub_f2fs_data *data, grub_uint32_t nid,
  537. grub_uint32_t *blkaddr)
  538. {
  539. grub_uint16_t n = grub_le_to_cpu16 (data->nat_j.n_nats);
  540. grub_uint16_t i;
  541. if (n > NAT_JOURNAL_ENTRIES)
  542. return grub_error (GRUB_ERR_BAD_FS,
  543. "invalid number of nat journal entries");
  544. for (i = 0; i < n; i++)
  545. {
  546. if (grub_le_to_cpu32 (data->nat_j.entries[i].nid) == nid)
  547. {
  548. *blkaddr = grub_le_to_cpu32 (data->nat_j.entries[i].ne.block_addr);
  549. break;
  550. }
  551. }
  552. return GRUB_ERR_NONE;
  553. }
  554. static grub_uint32_t
  555. get_node_blkaddr (struct grub_f2fs_data *data, grub_uint32_t nid)
  556. {
  557. struct grub_f2fs_nat_block *nat_block;
  558. grub_uint32_t seg_off, block_off, entry_off, block_addr;
  559. grub_uint32_t blkaddr = 0;
  560. grub_err_t err;
  561. int result_bit;
  562. err = get_blkaddr_from_nat_journal (data, nid, &blkaddr);
  563. if (err != GRUB_ERR_NONE)
  564. return 0;
  565. if (blkaddr)
  566. return blkaddr;
  567. nat_block = grub_malloc (F2FS_BLKSIZE);
  568. if (!nat_block)
  569. return 0;
  570. block_off = nid / NAT_ENTRY_PER_BLOCK;
  571. entry_off = nid % NAT_ENTRY_PER_BLOCK;
  572. seg_off = block_off / data->blocks_per_seg;
  573. block_addr = data->nat_blkaddr +
  574. ((seg_off * data->blocks_per_seg) << 1) +
  575. (block_off & (data->blocks_per_seg - 1));
  576. result_bit = grub_f2fs_test_bit (block_off, data->nat_bitmap,
  577. data->nat_bitmap_size);
  578. if (result_bit > 0)
  579. block_addr += data->blocks_per_seg;
  580. else if (result_bit == -1)
  581. {
  582. grub_free (nat_block);
  583. return 0;
  584. }
  585. err = grub_f2fs_block_read (data, block_addr, nat_block);
  586. if (err)
  587. {
  588. grub_free (nat_block);
  589. return 0;
  590. }
  591. blkaddr = grub_le_to_cpu32 (nat_block->ne[entry_off].block_addr);
  592. grub_free (nat_block);
  593. return blkaddr;
  594. }
  595. static int
  596. grub_get_node_path (struct grub_f2fs_inode *inode, grub_uint32_t block,
  597. grub_uint32_t offset[4], grub_uint32_t noffset[4])
  598. {
  599. grub_uint32_t direct_blks = ADDRS_PER_BLOCK;
  600. grub_uint32_t dptrs_per_blk = NIDS_PER_BLOCK;
  601. grub_uint32_t indirect_blks = ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
  602. grub_uint32_t dindirect_blks = indirect_blks * NIDS_PER_BLOCK;
  603. grub_uint32_t direct_index = DEF_ADDRS_PER_INODE;
  604. int n = 0;
  605. int level = -1;
  606. if (inode->i_inline & F2FS_INLINE_XATTR)
  607. direct_index -= F2FS_INLINE_XATTR_ADDRS;
  608. noffset[0] = 0;
  609. if (block < direct_index)
  610. {
  611. offset[n] = block;
  612. level = 0;
  613. goto got;
  614. }
  615. block -= direct_index;
  616. if (block < direct_blks)
  617. {
  618. offset[n++] = NODE_DIR1_BLOCK;
  619. noffset[n] = 1;
  620. offset[n] = block;
  621. level = 1;
  622. goto got;
  623. }
  624. block -= direct_blks;
  625. if (block < direct_blks)
  626. {
  627. offset[n++] = NODE_DIR2_BLOCK;
  628. noffset[n] = 2;
  629. offset[n] = block;
  630. level = 1;
  631. goto got;
  632. }
  633. block -= direct_blks;
  634. if (block < indirect_blks)
  635. {
  636. offset[n++] = NODE_IND1_BLOCK;
  637. noffset[n] = 3;
  638. offset[n++] = block / direct_blks;
  639. noffset[n] = 4 + offset[n - 1];
  640. offset[n] = block % direct_blks;
  641. level = 2;
  642. goto got;
  643. }
  644. block -= indirect_blks;
  645. if (block < indirect_blks)
  646. {
  647. offset[n++] = NODE_IND2_BLOCK;
  648. noffset[n] = 4 + dptrs_per_blk;
  649. offset[n++] = block / direct_blks;
  650. noffset[n] = 5 + dptrs_per_blk + offset[n - 1];
  651. offset[n] = block % direct_blks;
  652. level = 2;
  653. goto got;
  654. }
  655. block -= indirect_blks;
  656. if (block < dindirect_blks)
  657. {
  658. offset[n++] = NODE_DIND_BLOCK;
  659. noffset[n] = 5 + (dptrs_per_blk * 2);
  660. offset[n++] = block / indirect_blks;
  661. noffset[n] = 6 + (dptrs_per_blk * 2) +
  662. offset[n - 1] * (dptrs_per_blk + 1);
  663. offset[n++] = (block / direct_blks) % dptrs_per_blk;
  664. noffset[n] = 7 + (dptrs_per_blk * 2) +
  665. offset[n - 2] * (dptrs_per_blk + 1) + offset[n - 1];
  666. offset[n] = block % direct_blks;
  667. level = 3;
  668. goto got;
  669. }
  670. got:
  671. return level;
  672. }
  673. static grub_err_t
  674. grub_f2fs_read_node (struct grub_f2fs_data *data,
  675. grub_uint32_t nid, struct grub_f2fs_node *np)
  676. {
  677. grub_uint32_t blkaddr;
  678. blkaddr = get_node_blkaddr (data, nid);
  679. if (!blkaddr)
  680. return grub_errno;
  681. return grub_f2fs_block_read (data, blkaddr, np);
  682. }
  683. static struct grub_f2fs_data *
  684. grub_f2fs_mount (grub_disk_t disk)
  685. {
  686. struct grub_f2fs_data *data;
  687. grub_err_t err;
  688. data = grub_malloc (sizeof (*data));
  689. if (!data)
  690. return NULL;
  691. data->disk = disk;
  692. if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET0))
  693. {
  694. if (grub_f2fs_read_sb (data, F2FS_SUPER_OFFSET1))
  695. {
  696. if (grub_errno == GRUB_ERR_NONE)
  697. grub_error (GRUB_ERR_BAD_FS,
  698. "not a F2FS filesystem (no superblock)");
  699. goto fail;
  700. }
  701. }
  702. data->root_ino = grub_le_to_cpu32 (data->sblock.root_ino);
  703. data->cp_blkaddr = grub_le_to_cpu32 (data->sblock.cp_blkaddr);
  704. data->nat_blkaddr = grub_le_to_cpu32 (data->sblock.nat_blkaddr);
  705. data->blocks_per_seg = 1 <<
  706. grub_le_to_cpu32 (data->sblock.log_blocks_per_seg);
  707. err = grub_f2fs_read_cp (data);
  708. if (err)
  709. goto fail;
  710. data->nat_bitmap = nat_bitmap_ptr (data, &data->nat_bitmap_size);
  711. if (data->nat_bitmap == NULL)
  712. goto fail;
  713. err = get_nat_journal (data);
  714. if (err)
  715. goto fail;
  716. data->diropen.data = data;
  717. data->diropen.ino = data->root_ino;
  718. data->diropen.inode_read = 1;
  719. data->inode = &data->diropen.inode;
  720. err = grub_f2fs_read_node (data, data->root_ino, data->inode);
  721. if (err)
  722. goto fail;
  723. return data;
  724. fail:
  725. grub_free (data);
  726. return NULL;
  727. }
  728. /* Guarantee inline_data was handled by caller. */
  729. static grub_disk_addr_t
  730. grub_f2fs_get_block (grub_fshelp_node_t node, grub_disk_addr_t block_ofs)
  731. {
  732. struct grub_f2fs_data *data = node->data;
  733. struct grub_f2fs_inode *inode = &node->inode.i;
  734. grub_uint32_t offset[4], noffset[4], nids[4];
  735. struct grub_f2fs_node *node_block;
  736. grub_uint32_t block_addr = -1;
  737. int level, i;
  738. level = grub_get_node_path (inode, block_ofs, offset, noffset);
  739. if (level < 0)
  740. return -1;
  741. if (level == 0)
  742. return grub_le_to_cpu32 (inode->i_addr[offset[0]]);
  743. node_block = grub_malloc (F2FS_BLKSIZE);
  744. if (!node_block)
  745. return -1;
  746. nids[1] = get_node_id (&node->inode, offset[0], 1);
  747. /* Get indirect or direct nodes. */
  748. for (i = 1; i <= level; i++)
  749. {
  750. grub_f2fs_read_node (data, nids[i], node_block);
  751. if (grub_errno)
  752. goto fail;
  753. if (i < level)
  754. nids[i + 1] = get_node_id (node_block, offset[i], 0);
  755. }
  756. block_addr = grub_le_to_cpu32 (node_block->dn.addr[offset[level]]);
  757. fail:
  758. grub_free (node_block);
  759. return block_addr;
  760. }
  761. static grub_ssize_t
  762. grub_f2fs_read_file (grub_fshelp_node_t node,
  763. grub_disk_read_hook_t read_hook, void *read_hook_data,
  764. grub_off_t pos, grub_size_t len, char *buf)
  765. {
  766. struct grub_f2fs_inode *inode = &node->inode.i;
  767. grub_off_t filesize = grub_f2fs_file_size (inode);
  768. char *inline_addr = get_inline_addr (inode);
  769. if (inode->i_inline & F2FS_INLINE_DATA)
  770. {
  771. if (filesize > MAX_INLINE_DATA)
  772. return -1;
  773. if (len > filesize - pos)
  774. len = filesize - pos;
  775. grub_memcpy (buf, inline_addr + pos, len);
  776. return len;
  777. }
  778. return grub_fshelp_read_file (node->data->disk, node,
  779. read_hook, read_hook_data,
  780. pos, len, buf, grub_f2fs_get_block,
  781. filesize,
  782. F2FS_BLK_SEC_BITS, 0);
  783. }
  784. static char *
  785. grub_f2fs_read_symlink (grub_fshelp_node_t node)
  786. {
  787. char *symlink;
  788. struct grub_fshelp_node *diro = node;
  789. grub_uint64_t filesize;
  790. if (!diro->inode_read)
  791. {
  792. grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
  793. if (grub_errno)
  794. return 0;
  795. }
  796. filesize = grub_f2fs_file_size(&diro->inode.i);
  797. symlink = grub_malloc (filesize + 1);
  798. if (!symlink)
  799. return 0;
  800. grub_f2fs_read_file (diro, 0, 0, 0, filesize, symlink);
  801. if (grub_errno)
  802. {
  803. grub_free (symlink);
  804. return 0;
  805. }
  806. symlink[filesize] = '\0';
  807. return symlink;
  808. }
  809. static int
  810. grub_f2fs_check_dentries (struct grub_f2fs_dir_iter_ctx *ctx)
  811. {
  812. struct grub_fshelp_node *fdiro;
  813. int i;
  814. for (i = 0; i < ctx->max;)
  815. {
  816. char *filename;
  817. enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
  818. enum FILE_TYPE ftype;
  819. int name_len;
  820. int ret;
  821. if (grub_f2fs_test_bit_le (i, ctx->bitmap) == 0)
  822. {
  823. i++;
  824. continue;
  825. }
  826. ftype = ctx->dentry[i].file_type;
  827. name_len = grub_le_to_cpu16 (ctx->dentry[i].name_len);
  828. if (name_len >= F2FS_NAME_LEN)
  829. return 0;
  830. filename = grub_malloc (name_len + 1);
  831. if (!filename)
  832. return 0;
  833. grub_memcpy (filename, ctx->filename[i], name_len);
  834. filename[name_len] = 0;
  835. fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
  836. if (!fdiro)
  837. {
  838. grub_free(filename);
  839. return 0;
  840. }
  841. if (ftype == F2FS_FT_DIR)
  842. type = GRUB_FSHELP_DIR;
  843. else if (ftype == F2FS_FT_SYMLINK)
  844. type = GRUB_FSHELP_SYMLINK;
  845. else if (ftype == F2FS_FT_REG_FILE)
  846. type = GRUB_FSHELP_REG;
  847. fdiro->data = ctx->data;
  848. fdiro->ino = grub_le_to_cpu32 (ctx->dentry[i].ino);
  849. fdiro->inode_read = 0;
  850. ret = ctx->hook (filename, type, fdiro, ctx->hook_data);
  851. grub_free(filename);
  852. if (ret)
  853. return 1;
  854. i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
  855. }
  856. return 0;
  857. }
  858. static int
  859. grub_f2fs_iterate_inline_dir (struct grub_f2fs_inode *dir,
  860. struct grub_f2fs_dir_iter_ctx *ctx)
  861. {
  862. struct grub_f2fs_inline_dentry *de_blk;
  863. de_blk = (struct grub_f2fs_inline_dentry *) get_inline_addr (dir);
  864. ctx->bitmap = de_blk->dentry_bitmap;
  865. ctx->dentry = de_blk->dentry;
  866. ctx->filename = de_blk->filename;
  867. ctx->max = NR_INLINE_DENTRY;
  868. return grub_f2fs_check_dentries (ctx);
  869. }
  870. static int
  871. grub_f2fs_iterate_dir (grub_fshelp_node_t dir,
  872. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  873. {
  874. struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
  875. struct grub_f2fs_inode *inode;
  876. struct grub_f2fs_dir_iter_ctx ctx = {
  877. .data = diro->data,
  878. .hook = hook,
  879. .hook_data = hook_data
  880. };
  881. grub_off_t fpos = 0;
  882. if (!diro->inode_read)
  883. {
  884. grub_f2fs_read_node (diro->data, diro->ino, &diro->inode);
  885. if (grub_errno)
  886. return 0;
  887. }
  888. inode = &diro->inode.i;
  889. if (inode->i_inline & F2FS_INLINE_DENTRY)
  890. return grub_f2fs_iterate_inline_dir (inode, &ctx);
  891. while (fpos < grub_f2fs_file_size (inode))
  892. {
  893. struct grub_f2fs_dentry_block *de_blk;
  894. char *buf;
  895. int ret;
  896. buf = grub_zalloc (F2FS_BLKSIZE);
  897. if (!buf)
  898. return 0;
  899. grub_f2fs_read_file (diro, 0, 0, fpos, F2FS_BLKSIZE, buf);
  900. if (grub_errno)
  901. {
  902. grub_free (buf);
  903. return 0;
  904. }
  905. de_blk = (struct grub_f2fs_dentry_block *) buf;
  906. ctx.bitmap = de_blk->dentry_bitmap;
  907. ctx.dentry = de_blk->dentry;
  908. ctx.filename = de_blk->filename;
  909. ctx.max = NR_DENTRY_IN_BLOCK;
  910. ret = grub_f2fs_check_dentries (&ctx);
  911. grub_free (buf);
  912. if (ret)
  913. return 1;
  914. fpos += F2FS_BLKSIZE;
  915. }
  916. return 0;
  917. }
  918. static int
  919. grub_f2fs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
  920. grub_fshelp_node_t node, void *data)
  921. {
  922. struct grub_f2fs_dir_ctx *ctx = data;
  923. struct grub_dirhook_info info;
  924. grub_memset (&info, 0, sizeof (info));
  925. if (!node->inode_read)
  926. {
  927. grub_f2fs_read_node (ctx->data, node->ino, &node->inode);
  928. if (!grub_errno)
  929. node->inode_read = 1;
  930. grub_errno = GRUB_ERR_NONE;
  931. }
  932. if (node->inode_read)
  933. {
  934. info.mtimeset = 1;
  935. info.mtime = grub_le_to_cpu64 (node->inode.i.i_mtime);
  936. }
  937. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  938. grub_free (node);
  939. return ctx->hook (filename, &info, ctx->hook_data);
  940. }
  941. static grub_err_t
  942. grub_f2fs_dir (grub_device_t device, const char *path,
  943. grub_fs_dir_hook_t hook, void *hook_data)
  944. {
  945. struct grub_f2fs_dir_ctx ctx = {
  946. .hook = hook,
  947. .hook_data = hook_data
  948. };
  949. struct grub_fshelp_node *fdiro = 0;
  950. grub_dl_ref (my_mod);
  951. ctx.data = grub_f2fs_mount (device->disk);
  952. if (!ctx.data)
  953. goto fail;
  954. grub_fshelp_find_file (path, &ctx.data->diropen, &fdiro,
  955. grub_f2fs_iterate_dir, grub_f2fs_read_symlink,
  956. GRUB_FSHELP_DIR);
  957. if (grub_errno)
  958. goto fail;
  959. grub_f2fs_iterate_dir (fdiro, grub_f2fs_dir_iter, &ctx);
  960. fail:
  961. if (fdiro != &ctx.data->diropen)
  962. grub_free (fdiro);
  963. grub_free (ctx.data);
  964. grub_dl_unref (my_mod);
  965. return grub_errno;
  966. }
  967. /* Open a file named NAME and initialize FILE. */
  968. static grub_err_t
  969. grub_f2fs_open (struct grub_file *file, const char *name)
  970. {
  971. struct grub_f2fs_data *data = NULL;
  972. struct grub_fshelp_node *fdiro = 0;
  973. struct grub_f2fs_inode *inode;
  974. grub_dl_ref (my_mod);
  975. data = grub_f2fs_mount (file->device->disk);
  976. if (!data)
  977. goto fail;
  978. grub_fshelp_find_file (name, &data->diropen, &fdiro,
  979. grub_f2fs_iterate_dir, grub_f2fs_read_symlink,
  980. GRUB_FSHELP_REG);
  981. if (grub_errno)
  982. goto fail;
  983. if (!fdiro->inode_read)
  984. {
  985. grub_f2fs_read_node (data, fdiro->ino, &fdiro->inode);
  986. if (grub_errno)
  987. goto fail;
  988. }
  989. grub_memcpy (data->inode, &fdiro->inode, sizeof (*data->inode));
  990. grub_free (fdiro);
  991. inode = &(data->inode->i);
  992. file->size = grub_f2fs_file_size (inode);
  993. file->data = data;
  994. file->offset = 0;
  995. if (inode->i_inline & F2FS_INLINE_DATA && file->size > MAX_INLINE_DATA)
  996. grub_error (GRUB_ERR_BAD_FS, "corrupted inline_data: need fsck");
  997. return 0;
  998. fail:
  999. if (fdiro != &data->diropen)
  1000. grub_free (fdiro);
  1001. grub_free (data);
  1002. grub_dl_unref (my_mod);
  1003. return grub_errno;
  1004. }
  1005. static grub_ssize_t
  1006. grub_f2fs_read (grub_file_t file, char *buf, grub_size_t len)
  1007. {
  1008. struct grub_f2fs_data *data = (struct grub_f2fs_data *) file->data;
  1009. return grub_f2fs_read_file (&data->diropen,
  1010. file->read_hook, file->read_hook_data,
  1011. file->offset, len, buf);
  1012. }
  1013. static grub_err_t
  1014. grub_f2fs_close (grub_file_t file)
  1015. {
  1016. struct grub_f2fs_data *data = (struct grub_f2fs_data *) file->data;
  1017. grub_free (data);
  1018. grub_dl_unref (my_mod);
  1019. return GRUB_ERR_NONE;
  1020. }
  1021. static grub_uint8_t *
  1022. grub_f2fs_utf16_to_utf8 (grub_uint16_t *in_buf_le)
  1023. {
  1024. grub_uint16_t in_buf[MAX_VOLUME_NAME];
  1025. grub_uint8_t *out_buf;
  1026. int len = 0;
  1027. out_buf = grub_malloc (MAX_VOLUME_NAME * GRUB_MAX_UTF8_PER_UTF16 + 1);
  1028. if (!out_buf)
  1029. return NULL;
  1030. while (*in_buf_le != 0 && len < MAX_VOLUME_NAME) {
  1031. in_buf[len] = grub_le_to_cpu16 (in_buf_le[len]);
  1032. len++;
  1033. }
  1034. *grub_utf16_to_utf8 (out_buf, in_buf, len) = '\0';
  1035. return out_buf;
  1036. }
  1037. #if __GNUC__ >= 9
  1038. #pragma GCC diagnostic push
  1039. #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
  1040. #endif
  1041. static grub_err_t
  1042. grub_f2fs_label (grub_device_t device, char **label)
  1043. {
  1044. struct grub_f2fs_data *data;
  1045. grub_disk_t disk = device->disk;
  1046. grub_dl_ref (my_mod);
  1047. data = grub_f2fs_mount (disk);
  1048. if (data)
  1049. *label = (char *) grub_f2fs_utf16_to_utf8 (data->sblock.volume_name);
  1050. else
  1051. *label = NULL;
  1052. grub_free (data);
  1053. grub_dl_unref (my_mod);
  1054. return grub_errno;
  1055. }
  1056. #if __GNUC__ >= 9
  1057. #pragma GCC diagnostic pop
  1058. #endif
  1059. static grub_err_t
  1060. grub_f2fs_uuid (grub_device_t device, char **uuid)
  1061. {
  1062. struct grub_f2fs_data *data;
  1063. grub_disk_t disk = device->disk;
  1064. grub_dl_ref (my_mod);
  1065. data = grub_f2fs_mount (disk);
  1066. if (data)
  1067. {
  1068. *uuid =
  1069. grub_xasprintf
  1070. ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  1071. data->sblock.uuid[0], data->sblock.uuid[1],
  1072. data->sblock.uuid[2], data->sblock.uuid[3],
  1073. data->sblock.uuid[4], data->sblock.uuid[5],
  1074. data->sblock.uuid[6], data->sblock.uuid[7],
  1075. data->sblock.uuid[8], data->sblock.uuid[9],
  1076. data->sblock.uuid[10], data->sblock.uuid[11],
  1077. data->sblock.uuid[12], data->sblock.uuid[13],
  1078. data->sblock.uuid[14], data->sblock.uuid[15]);
  1079. }
  1080. else
  1081. *uuid = NULL;
  1082. grub_free (data);
  1083. grub_dl_unref (my_mod);
  1084. return grub_errno;
  1085. }
  1086. static struct grub_fs grub_f2fs_fs = {
  1087. .name = "f2fs",
  1088. .fs_dir = grub_f2fs_dir,
  1089. .fs_open = grub_f2fs_open,
  1090. .fs_read = grub_f2fs_read,
  1091. .fs_close = grub_f2fs_close,
  1092. .fs_label = grub_f2fs_label,
  1093. .fs_uuid = grub_f2fs_uuid,
  1094. #ifdef GRUB_UTIL
  1095. .reserved_first_sector = 1,
  1096. .blocklist_install = 0,
  1097. #endif
  1098. .next = 0
  1099. };
  1100. GRUB_MOD_INIT (f2fs)
  1101. {
  1102. grub_fs_register (&grub_f2fs_fs);
  1103. my_mod = mod;
  1104. }
  1105. GRUB_MOD_FINI (f2fs)
  1106. {
  1107. grub_fs_unregister (&grub_f2fs_fs);
  1108. }