nilfs2.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /*
  2. * nilfs2.c - New Implementation of Log filesystem
  3. *
  4. * Written by Jiro SEKIBA <jir@unicus.jp>
  5. *
  6. * Copyright (C) 2003,2004,2005,2007,2008,2010 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. /* Filetype information as used in inodes. */
  22. #define FILETYPE_INO_MASK 0170000
  23. #define FILETYPE_INO_REG 0100000
  24. #define FILETYPE_INO_DIRECTORY 0040000
  25. #define FILETYPE_INO_SYMLINK 0120000
  26. #include <grub/err.h>
  27. #include <grub/file.h>
  28. #include <grub/mm.h>
  29. #include <grub/misc.h>
  30. #include <grub/disk.h>
  31. #include <grub/dl.h>
  32. #include <grub/types.h>
  33. #include <grub/fshelp.h>
  34. GRUB_MOD_LICENSE ("GPLv3+");
  35. #define NILFS_INODE_BMAP_SIZE 7
  36. #define NILFS_SUPORT_REV 2
  37. /* Magic value used to identify an nilfs2 filesystem. */
  38. #define NILFS2_SUPER_MAGIC 0x3434
  39. /* nilfs btree node flag. */
  40. #define NILFS_BTREE_NODE_ROOT 0x01
  41. /* nilfs btree node level. */
  42. #define NILFS_BTREE_LEVEL_DATA 0
  43. #define NILFS_BTREE_LEVEL_NODE_MIN (NILFS_BTREE_LEVEL_DATA + 1)
  44. /* nilfs 1st super block posission from beginning of the partition
  45. in 512 block size */
  46. #define NILFS_1ST_SUPER_BLOCK 2
  47. /* nilfs 2nd super block posission from beginning of the partition
  48. in 512 block size */
  49. #define NILFS_2ND_SUPER_BLOCK(devsize) (((devsize >> 3) - 1) << 3)
  50. #define LOG_INODE_SIZE 7
  51. struct grub_nilfs2_inode
  52. {
  53. grub_uint64_t i_blocks;
  54. grub_uint64_t i_size;
  55. grub_uint64_t i_ctime;
  56. grub_uint64_t i_mtime;
  57. grub_uint32_t i_ctime_nsec;
  58. grub_uint32_t i_mtime_nsec;
  59. grub_uint32_t i_uid;
  60. grub_uint32_t i_gid;
  61. grub_uint16_t i_mode;
  62. grub_uint16_t i_links_count;
  63. grub_uint32_t i_flags;
  64. grub_uint64_t i_bmap[NILFS_INODE_BMAP_SIZE];
  65. #define i_device_code i_bmap[0]
  66. grub_uint64_t i_xattr;
  67. grub_uint32_t i_generation;
  68. grub_uint32_t i_pad;
  69. };
  70. struct grub_nilfs2_super_root
  71. {
  72. grub_uint32_t sr_sum;
  73. grub_uint16_t sr_bytes;
  74. grub_uint16_t sr_flags;
  75. grub_uint64_t sr_nongc_ctime;
  76. struct grub_nilfs2_inode sr_dat;
  77. struct grub_nilfs2_inode sr_cpfile;
  78. struct grub_nilfs2_inode sr_sufile;
  79. };
  80. struct grub_nilfs2_super_block
  81. {
  82. grub_uint32_t s_rev_level;
  83. grub_uint16_t s_minor_rev_level;
  84. grub_uint16_t s_magic;
  85. grub_uint16_t s_bytes;
  86. grub_uint16_t s_flags;
  87. grub_uint32_t s_crc_seed;
  88. grub_uint32_t s_sum;
  89. grub_uint32_t s_log_block_size;
  90. grub_uint64_t s_nsegments;
  91. grub_uint64_t s_dev_size;
  92. grub_uint64_t s_first_data_block;
  93. grub_uint32_t s_blocks_per_segment;
  94. grub_uint32_t s_r_segments_percentage;
  95. grub_uint64_t s_last_cno;
  96. grub_uint64_t s_last_pseg;
  97. grub_uint64_t s_last_seq;
  98. grub_uint64_t s_free_blocks_count;
  99. grub_uint64_t s_ctime;
  100. grub_uint64_t s_mtime;
  101. grub_uint64_t s_wtime;
  102. grub_uint16_t s_mnt_count;
  103. grub_uint16_t s_max_mnt_count;
  104. grub_uint16_t s_state;
  105. grub_uint16_t s_errors;
  106. grub_uint64_t s_lastcheck;
  107. grub_uint32_t s_checkinterval;
  108. grub_uint32_t s_creator_os;
  109. grub_uint16_t s_def_resuid;
  110. grub_uint16_t s_def_resgid;
  111. grub_uint32_t s_first_ino;
  112. grub_uint16_t s_inode_size;
  113. grub_uint16_t s_dat_entry_size;
  114. grub_uint16_t s_checkpoint_size;
  115. grub_uint16_t s_segment_usage_size;
  116. grub_uint8_t s_uuid[16];
  117. char s_volume_name[80];
  118. grub_uint32_t s_c_interval;
  119. grub_uint32_t s_c_block_max;
  120. grub_uint32_t s_reserved[192];
  121. };
  122. struct grub_nilfs2_dir_entry
  123. {
  124. grub_uint64_t inode;
  125. grub_uint16_t rec_len;
  126. #define MAX_NAMELEN 255
  127. grub_uint8_t name_len;
  128. grub_uint8_t file_type;
  129. #if 0 /* followed by file name. */
  130. char name[NILFS_NAME_LEN];
  131. char pad;
  132. #endif
  133. } GRUB_PACKED;
  134. enum
  135. {
  136. NILFS_FT_UNKNOWN,
  137. NILFS_FT_REG_FILE,
  138. NILFS_FT_DIR,
  139. NILFS_FT_CHRDEV,
  140. NILFS_FT_BLKDEV,
  141. NILFS_FT_FIFO,
  142. NILFS_FT_SOCK,
  143. NILFS_FT_SYMLINK,
  144. NILFS_FT_MAX
  145. };
  146. struct grub_nilfs2_finfo
  147. {
  148. grub_uint64_t fi_ino;
  149. grub_uint64_t fi_cno;
  150. grub_uint32_t fi_nblocks;
  151. grub_uint32_t fi_ndatablk;
  152. };
  153. struct grub_nilfs2_binfo_v
  154. {
  155. grub_uint64_t bi_vblocknr;
  156. grub_uint64_t bi_blkoff;
  157. };
  158. struct grub_nilfs2_binfo_dat
  159. {
  160. grub_uint64_t bi_blkoff;
  161. grub_uint8_t bi_level;
  162. grub_uint8_t bi_pad[7];
  163. };
  164. union grub_nilfs2_binfo
  165. {
  166. struct grub_nilfs2_binfo_v bi_v;
  167. struct grub_nilfs2_binfo_dat bi_dat;
  168. };
  169. struct grub_nilfs2_segment_summary
  170. {
  171. grub_uint32_t ss_datasum;
  172. grub_uint32_t ss_sumsum;
  173. grub_uint32_t ss_magic;
  174. grub_uint16_t ss_bytes;
  175. grub_uint16_t ss_flags;
  176. grub_uint64_t ss_seq;
  177. grub_uint64_t ss_create;
  178. grub_uint64_t ss_next;
  179. grub_uint32_t ss_nblocks;
  180. grub_uint32_t ss_nfinfo;
  181. grub_uint32_t ss_sumbytes;
  182. grub_uint32_t ss_pad;
  183. };
  184. struct grub_nilfs2_btree_node
  185. {
  186. grub_uint8_t bn_flags;
  187. grub_uint8_t bn_level;
  188. grub_uint16_t bn_nchildren;
  189. grub_uint32_t bn_pad;
  190. grub_uint64_t keys[0];
  191. };
  192. struct grub_nilfs2_palloc_group_desc
  193. {
  194. grub_uint32_t pg_nfrees;
  195. };
  196. #define LOG_SIZE_GROUP_DESC 2
  197. #define LOG_NILFS_DAT_ENTRY_SIZE 5
  198. struct grub_nilfs2_dat_entry
  199. {
  200. grub_uint64_t de_blocknr;
  201. grub_uint64_t de_start;
  202. grub_uint64_t de_end;
  203. grub_uint64_t de_rsv;
  204. };
  205. struct grub_nilfs2_snapshot_list
  206. {
  207. grub_uint64_t ssl_next;
  208. grub_uint64_t ssl_prev;
  209. };
  210. struct grub_nilfs2_cpfile_header
  211. {
  212. grub_uint64_t ch_ncheckpoints;
  213. grub_uint64_t ch_nsnapshots;
  214. struct grub_nilfs2_snapshot_list ch_snapshot_list;
  215. };
  216. struct grub_nilfs2_checkpoint
  217. {
  218. grub_uint32_t cp_flags;
  219. grub_uint32_t cp_checkpoints_count;
  220. struct grub_nilfs2_snapshot_list cp_snapshot_list;
  221. grub_uint64_t cp_cno;
  222. grub_uint64_t cp_create;
  223. grub_uint64_t cp_nblk_inc;
  224. grub_uint64_t cp_inodes_count;
  225. grub_uint64_t cp_blocks_count;
  226. struct grub_nilfs2_inode cp_ifile_inode;
  227. };
  228. #define NILFS_BMAP_LARGE 0x1
  229. #define NILFS_BMAP_SIZE (NILFS_INODE_BMAP_SIZE * sizeof(grub_uint64_t))
  230. /* nilfs extra padding for nonroot btree node. */
  231. #define NILFS_BTREE_NODE_EXTRA_PAD_SIZE (sizeof(grub_uint64_t))
  232. #define NILFS_BTREE_ROOT_SIZE NILFS_BMAP_SIZE
  233. #define NILFS_BTREE_ROOT_NCHILDREN_MAX \
  234. ((NILFS_BTREE_ROOT_SIZE - sizeof(struct nilfs_btree_node)) / \
  235. (sizeof(grub_uint64_t) + sizeof(grub_uint64_t)) )
  236. struct grub_fshelp_node
  237. {
  238. struct grub_nilfs2_data *data;
  239. struct grub_nilfs2_inode inode;
  240. grub_uint64_t ino;
  241. int inode_read;
  242. };
  243. struct grub_nilfs2_data
  244. {
  245. struct grub_nilfs2_super_block sblock;
  246. struct grub_nilfs2_super_root sroot;
  247. struct grub_nilfs2_inode ifile;
  248. grub_disk_t disk;
  249. struct grub_nilfs2_inode *inode;
  250. struct grub_fshelp_node diropen;
  251. };
  252. /* Log2 size of nilfs2 block in 512 blocks. */
  253. #define LOG2_NILFS2_BLOCK_SIZE(data) \
  254. (grub_le_to_cpu32 (data->sblock.s_log_block_size) + 1)
  255. /* Log2 size of nilfs2 block in bytes. */
  256. #define LOG2_BLOCK_SIZE(data) \
  257. (grub_le_to_cpu32 (data->sblock.s_log_block_size) + 10)
  258. /* The size of an nilfs2 block in bytes. */
  259. #define NILFS2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE (data))
  260. static grub_uint64_t
  261. grub_nilfs2_dat_translate (struct grub_nilfs2_data *data, grub_uint64_t key);
  262. static grub_dl_t my_mod;
  263. static inline unsigned long
  264. grub_nilfs2_log_palloc_entries_per_group (struct grub_nilfs2_data *data)
  265. {
  266. return LOG2_BLOCK_SIZE (data) + 3;
  267. }
  268. static inline grub_uint64_t
  269. grub_nilfs2_palloc_group (struct grub_nilfs2_data *data,
  270. grub_uint64_t nr, grub_uint64_t * offset)
  271. {
  272. *offset = nr & ((1 << grub_nilfs2_log_palloc_entries_per_group (data)) - 1);
  273. return nr >> grub_nilfs2_log_palloc_entries_per_group (data);
  274. }
  275. static inline grub_uint32_t
  276. grub_nilfs2_palloc_log_groups_per_desc_block (struct grub_nilfs2_data *data)
  277. {
  278. return LOG2_BLOCK_SIZE (data) - LOG_SIZE_GROUP_DESC;
  279. COMPILE_TIME_ASSERT (sizeof (struct grub_nilfs2_palloc_group_desc)
  280. == (1 << LOG_SIZE_GROUP_DESC));
  281. }
  282. static inline grub_uint32_t
  283. grub_nilfs2_log_entries_per_block_log (struct grub_nilfs2_data *data,
  284. unsigned long log_entry_size)
  285. {
  286. return LOG2_BLOCK_SIZE (data) - log_entry_size;
  287. }
  288. static inline grub_uint32_t
  289. grub_nilfs2_blocks_per_group_log (struct grub_nilfs2_data *data,
  290. unsigned long log_entry_size)
  291. {
  292. return (1 << (grub_nilfs2_log_palloc_entries_per_group (data)
  293. - grub_nilfs2_log_entries_per_block_log (data,
  294. log_entry_size))) + 1;
  295. }
  296. static inline grub_uint32_t
  297. grub_nilfs2_blocks_per_desc_block_log (struct grub_nilfs2_data *data,
  298. unsigned long log_entry_size)
  299. {
  300. return(grub_nilfs2_blocks_per_group_log (data, log_entry_size)
  301. << grub_nilfs2_palloc_log_groups_per_desc_block (data)) + 1;
  302. }
  303. static inline grub_uint32_t
  304. grub_nilfs2_palloc_desc_block_offset_log (struct grub_nilfs2_data *data,
  305. unsigned long group,
  306. unsigned long log_entry_size)
  307. {
  308. grub_uint32_t desc_block =
  309. group >> grub_nilfs2_palloc_log_groups_per_desc_block (data);
  310. return desc_block * grub_nilfs2_blocks_per_desc_block_log (data,
  311. log_entry_size);
  312. }
  313. static inline grub_uint32_t
  314. grub_nilfs2_palloc_bitmap_block_offset (struct grub_nilfs2_data *data,
  315. unsigned long group,
  316. unsigned long log_entry_size)
  317. {
  318. unsigned long desc_offset = group
  319. & ((1 << grub_nilfs2_palloc_log_groups_per_desc_block (data)) - 1);
  320. return grub_nilfs2_palloc_desc_block_offset_log (data, group, log_entry_size)
  321. + 1
  322. + desc_offset * grub_nilfs2_blocks_per_group_log (data, log_entry_size);
  323. }
  324. static inline grub_uint32_t
  325. grub_nilfs2_palloc_entry_offset_log (struct grub_nilfs2_data *data,
  326. grub_uint64_t nr,
  327. unsigned long log_entry_size)
  328. {
  329. unsigned long group;
  330. grub_uint64_t group_offset;
  331. group = grub_nilfs2_palloc_group (data, nr, &group_offset);
  332. return grub_nilfs2_palloc_bitmap_block_offset (data, group,
  333. log_entry_size) + 1 +
  334. (group_offset >> grub_nilfs2_log_entries_per_block_log (data,
  335. log_entry_size));
  336. }
  337. static inline struct grub_nilfs2_btree_node *
  338. grub_nilfs2_btree_get_root (struct grub_nilfs2_inode *inode)
  339. {
  340. return (struct grub_nilfs2_btree_node *) &inode->i_bmap[0];
  341. }
  342. static inline int
  343. grub_nilfs2_btree_get_level (struct grub_nilfs2_btree_node *node)
  344. {
  345. return node->bn_level;
  346. }
  347. static inline grub_uint64_t *
  348. grub_nilfs2_btree_node_dkeys (struct grub_nilfs2_btree_node *node)
  349. {
  350. return (node->keys +
  351. ((node->bn_flags & NILFS_BTREE_NODE_ROOT) ?
  352. 0 : (NILFS_BTREE_NODE_EXTRA_PAD_SIZE / sizeof (grub_uint64_t))));
  353. }
  354. static inline grub_uint64_t
  355. grub_nilfs2_btree_node_get_key (struct grub_nilfs2_btree_node *node,
  356. int index)
  357. {
  358. return grub_le_to_cpu64 (*(grub_nilfs2_btree_node_dkeys (node) + index));
  359. }
  360. static inline int
  361. grub_nilfs2_btree_node_lookup (struct grub_nilfs2_btree_node *node,
  362. grub_uint64_t key, int *indexp)
  363. {
  364. grub_uint64_t nkey;
  365. int index, low, high, s;
  366. low = 0;
  367. high = grub_le_to_cpu16 (node->bn_nchildren) - 1;
  368. index = 0;
  369. s = 0;
  370. while (low <= high)
  371. {
  372. index = (low + high) / 2;
  373. nkey = grub_nilfs2_btree_node_get_key (node, index);
  374. if (nkey == key)
  375. {
  376. *indexp = index;
  377. return 1;
  378. }
  379. else if (nkey < key)
  380. {
  381. low = index + 1;
  382. s = -1;
  383. }
  384. else
  385. {
  386. high = index - 1;
  387. s = 1;
  388. }
  389. }
  390. if (node->bn_level > NILFS_BTREE_LEVEL_NODE_MIN)
  391. {
  392. if (s > 0 && index > 0)
  393. index--;
  394. }
  395. else if (s < 0)
  396. index++;
  397. *indexp = index;
  398. return s == 0;
  399. }
  400. static inline int
  401. grub_nilfs2_btree_node_nchildren_max (struct grub_nilfs2_data *data,
  402. struct grub_nilfs2_btree_node *node)
  403. {
  404. int node_children_max = ((NILFS2_BLOCK_SIZE (data) -
  405. sizeof (struct grub_nilfs2_btree_node) -
  406. NILFS_BTREE_NODE_EXTRA_PAD_SIZE) /
  407. (sizeof (grub_uint64_t) + sizeof (grub_uint64_t)));
  408. return (node->bn_flags & NILFS_BTREE_NODE_ROOT) ? 3 : node_children_max;
  409. }
  410. static inline grub_uint64_t *
  411. grub_nilfs2_btree_node_dptrs (struct grub_nilfs2_data *data,
  412. struct grub_nilfs2_btree_node *node)
  413. {
  414. return (grub_uint64_t *) (grub_nilfs2_btree_node_dkeys (node) +
  415. grub_nilfs2_btree_node_nchildren_max (data,
  416. node));
  417. }
  418. static inline grub_uint64_t
  419. grub_nilfs2_btree_node_get_ptr (struct grub_nilfs2_data *data,
  420. struct grub_nilfs2_btree_node *node,
  421. int index)
  422. {
  423. return
  424. grub_le_to_cpu64 (*(grub_nilfs2_btree_node_dptrs (data, node) + index));
  425. }
  426. static inline int
  427. grub_nilfs2_btree_get_nonroot_node (struct grub_nilfs2_data *data,
  428. grub_uint64_t ptr, void *block)
  429. {
  430. grub_disk_t disk = data->disk;
  431. unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
  432. return grub_disk_read (disk, ptr * nilfs2_block_count, 0,
  433. NILFS2_BLOCK_SIZE (data), block);
  434. }
  435. static grub_uint64_t
  436. grub_nilfs2_btree_lookup (struct grub_nilfs2_data *data,
  437. struct grub_nilfs2_inode *inode,
  438. grub_uint64_t key, int need_translate)
  439. {
  440. struct grub_nilfs2_btree_node *node;
  441. void *block;
  442. grub_uint64_t ptr;
  443. int level, found = 0, index;
  444. block = grub_malloc (NILFS2_BLOCK_SIZE (data));
  445. if (!block)
  446. return -1;
  447. node = grub_nilfs2_btree_get_root (inode);
  448. level = grub_nilfs2_btree_get_level (node);
  449. found = grub_nilfs2_btree_node_lookup (node, key, &index);
  450. ptr = grub_nilfs2_btree_node_get_ptr (data, node, index);
  451. if (need_translate)
  452. ptr = grub_nilfs2_dat_translate (data, ptr);
  453. for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--)
  454. {
  455. grub_nilfs2_btree_get_nonroot_node (data, ptr, block);
  456. if (grub_errno)
  457. {
  458. goto fail;
  459. }
  460. node = (struct grub_nilfs2_btree_node *) block;
  461. if (node->bn_level != level)
  462. {
  463. grub_error (GRUB_ERR_BAD_FS, "btree level mismatch\n");
  464. goto fail;
  465. }
  466. if (!found)
  467. found = grub_nilfs2_btree_node_lookup (node, key, &index);
  468. else
  469. index = 0;
  470. if (index < grub_nilfs2_btree_node_nchildren_max (data, node))
  471. {
  472. ptr = grub_nilfs2_btree_node_get_ptr (data, node, index);
  473. if (need_translate)
  474. ptr = grub_nilfs2_dat_translate (data, ptr);
  475. }
  476. else
  477. {
  478. grub_error (GRUB_ERR_BAD_FS, "btree corruption\n");
  479. goto fail;
  480. }
  481. }
  482. grub_free (block);
  483. if (!found)
  484. return -1;
  485. return ptr;
  486. fail:
  487. grub_free (block);
  488. return -1;
  489. }
  490. static inline grub_uint64_t
  491. grub_nilfs2_direct_lookup (struct grub_nilfs2_inode *inode, grub_uint64_t key)
  492. {
  493. return grub_le_to_cpu64 (inode->i_bmap[1 + key]);
  494. }
  495. static inline grub_uint64_t
  496. grub_nilfs2_bmap_lookup (struct grub_nilfs2_data *data,
  497. struct grub_nilfs2_inode *inode,
  498. grub_uint64_t key, int need_translate)
  499. {
  500. struct grub_nilfs2_btree_node *root = grub_nilfs2_btree_get_root (inode);
  501. if (root->bn_flags & NILFS_BMAP_LARGE)
  502. return grub_nilfs2_btree_lookup (data, inode, key, need_translate);
  503. else
  504. {
  505. grub_uint64_t ptr;
  506. ptr = grub_nilfs2_direct_lookup (inode, key);
  507. if (need_translate)
  508. ptr = grub_nilfs2_dat_translate (data, ptr);
  509. return ptr;
  510. }
  511. }
  512. static grub_uint64_t
  513. grub_nilfs2_dat_translate (struct grub_nilfs2_data *data, grub_uint64_t key)
  514. {
  515. struct grub_nilfs2_dat_entry entry;
  516. grub_disk_t disk = data->disk;
  517. grub_uint64_t pptr;
  518. grub_uint64_t blockno, offset;
  519. unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
  520. blockno = grub_nilfs2_palloc_entry_offset_log (data, key,
  521. LOG_NILFS_DAT_ENTRY_SIZE);
  522. offset = ((key * sizeof (struct grub_nilfs2_dat_entry))
  523. & ((1 << LOG2_BLOCK_SIZE (data)) - 1));
  524. pptr = grub_nilfs2_bmap_lookup (data, &data->sroot.sr_dat, blockno, 0);
  525. if (pptr == (grub_uint64_t) - 1)
  526. {
  527. grub_error (GRUB_ERR_BAD_FS, "btree lookup failure");
  528. return -1;
  529. }
  530. grub_disk_read (disk, pptr * nilfs2_block_count, offset,
  531. sizeof (struct grub_nilfs2_dat_entry), &entry);
  532. return grub_le_to_cpu64 (entry.de_blocknr);
  533. }
  534. static grub_disk_addr_t
  535. grub_nilfs2_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  536. {
  537. struct grub_nilfs2_data *data = node->data;
  538. struct grub_nilfs2_inode *inode = &node->inode;
  539. grub_uint64_t pptr = -1;
  540. pptr = grub_nilfs2_bmap_lookup (data, inode, fileblock, 1);
  541. if (pptr == (grub_uint64_t) - 1)
  542. {
  543. grub_error (GRUB_ERR_BAD_FS, "btree lookup failure");
  544. return -1;
  545. }
  546. return pptr;
  547. }
  548. /* Read LEN bytes from the file described by DATA starting with byte
  549. POS. Return the amount of read bytes in READ. */
  550. static grub_ssize_t
  551. grub_nilfs2_read_file (grub_fshelp_node_t node,
  552. grub_disk_read_hook_t read_hook, void *read_hook_data,
  553. grub_off_t pos, grub_size_t len, char *buf)
  554. {
  555. return grub_fshelp_read_file (node->data->disk, node,
  556. read_hook, read_hook_data,
  557. pos, len, buf, grub_nilfs2_read_block,
  558. grub_le_to_cpu64 (node->inode.i_size),
  559. LOG2_NILFS2_BLOCK_SIZE (node->data), 0);
  560. }
  561. static grub_err_t
  562. grub_nilfs2_read_checkpoint (struct grub_nilfs2_data *data,
  563. grub_uint64_t cpno,
  564. struct grub_nilfs2_checkpoint *cpp)
  565. {
  566. grub_uint64_t blockno;
  567. grub_uint64_t offset;
  568. grub_uint64_t pptr;
  569. grub_disk_t disk = data->disk;
  570. unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
  571. /* Assume sizeof(struct grub_nilfs2_cpfile_header) <
  572. sizeof(struct grub_nilfs2_checkpoint).
  573. */
  574. blockno = grub_divmod64 (cpno, NILFS2_BLOCK_SIZE (data) /
  575. sizeof (struct grub_nilfs2_checkpoint), &offset);
  576. pptr = grub_nilfs2_bmap_lookup (data, &data->sroot.sr_cpfile, blockno, 1);
  577. if (pptr == (grub_uint64_t) - 1)
  578. {
  579. return grub_error (GRUB_ERR_BAD_FS, "btree lookup failure");
  580. }
  581. return grub_disk_read (disk, pptr * nilfs2_block_count,
  582. offset * sizeof (struct grub_nilfs2_checkpoint),
  583. sizeof (struct grub_nilfs2_checkpoint), cpp);
  584. }
  585. static inline grub_err_t
  586. grub_nilfs2_read_last_checkpoint (struct grub_nilfs2_data *data,
  587. struct grub_nilfs2_checkpoint *cpp)
  588. {
  589. return grub_nilfs2_read_checkpoint (data,
  590. grub_le_to_cpu64 (data->
  591. sblock.s_last_cno),
  592. cpp);
  593. }
  594. /* Read the inode INO for the file described by DATA into INODE. */
  595. static grub_err_t
  596. grub_nilfs2_read_inode (struct grub_nilfs2_data *data,
  597. grub_uint64_t ino, struct grub_nilfs2_inode *inodep)
  598. {
  599. grub_uint64_t blockno;
  600. grub_uint64_t offset;
  601. grub_uint64_t pptr;
  602. grub_disk_t disk = data->disk;
  603. unsigned int nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
  604. blockno = grub_nilfs2_palloc_entry_offset_log (data, ino,
  605. LOG_INODE_SIZE);
  606. offset = ((sizeof (struct grub_nilfs2_inode) * ino)
  607. & ((1 << LOG2_BLOCK_SIZE (data)) - 1));
  608. pptr = grub_nilfs2_bmap_lookup (data, &data->ifile, blockno, 1);
  609. if (pptr == (grub_uint64_t) - 1)
  610. {
  611. return grub_error (GRUB_ERR_BAD_FS, "btree lookup failure");
  612. }
  613. return grub_disk_read (disk, pptr * nilfs2_block_count, offset,
  614. sizeof (struct grub_nilfs2_inode), inodep);
  615. }
  616. static int
  617. grub_nilfs2_valid_sb (struct grub_nilfs2_super_block *sbp)
  618. {
  619. if (grub_le_to_cpu16 (sbp->s_magic) != NILFS2_SUPER_MAGIC)
  620. return 0;
  621. if (grub_le_to_cpu32 (sbp->s_rev_level) != NILFS_SUPORT_REV)
  622. return 0;
  623. /* 20 already means 1GiB blocks. We don't want to deal with blocks overflowing int32. */
  624. if (grub_le_to_cpu32 (sbp->s_log_block_size) > 20)
  625. return 0;
  626. return 1;
  627. }
  628. static grub_err_t
  629. grub_nilfs2_load_sb (struct grub_nilfs2_data *data)
  630. {
  631. grub_disk_t disk = data->disk;
  632. struct grub_nilfs2_super_block sb2;
  633. grub_uint64_t partition_size;
  634. int valid[2];
  635. int swp = 0;
  636. grub_err_t err;
  637. /* Read first super block. */
  638. err = grub_disk_read (disk, NILFS_1ST_SUPER_BLOCK, 0,
  639. sizeof (struct grub_nilfs2_super_block), &data->sblock);
  640. if (err)
  641. return err;
  642. /* Make sure if 1st super block is valid. */
  643. valid[0] = grub_nilfs2_valid_sb (&data->sblock);
  644. if (valid[0])
  645. partition_size = (grub_le_to_cpu64 (data->sblock.s_dev_size)
  646. >> GRUB_DISK_SECTOR_BITS);
  647. else
  648. partition_size = grub_disk_get_size (disk);
  649. if (partition_size != GRUB_DISK_SIZE_UNKNOWN)
  650. {
  651. /* Read second super block. */
  652. err = grub_disk_read (disk, NILFS_2ND_SUPER_BLOCK (partition_size), 0,
  653. sizeof (struct grub_nilfs2_super_block), &sb2);
  654. if (err)
  655. {
  656. valid[1] = 0;
  657. grub_errno = GRUB_ERR_NONE;
  658. }
  659. else
  660. /* Make sure if 2nd super block is valid. */
  661. valid[1] = grub_nilfs2_valid_sb (&sb2);
  662. }
  663. else
  664. /* 2nd super block may not exist, so it's invalid. */
  665. valid[1] = 0;
  666. if (!valid[0] && !valid[1])
  667. return grub_error (GRUB_ERR_BAD_FS, "not a nilfs2 filesystem");
  668. swp = valid[1] && (!valid[0] ||
  669. grub_le_to_cpu64 (data->sblock.s_last_cno) <
  670. grub_le_to_cpu64 (sb2.s_last_cno));
  671. /* swap if first super block is invalid or older than second one. */
  672. if (swp)
  673. grub_memcpy (&data->sblock, &sb2,
  674. sizeof (struct grub_nilfs2_super_block));
  675. return GRUB_ERR_NONE;
  676. }
  677. static struct grub_nilfs2_data *
  678. grub_nilfs2_mount (grub_disk_t disk)
  679. {
  680. struct grub_nilfs2_data *data;
  681. struct grub_nilfs2_segment_summary ss;
  682. struct grub_nilfs2_checkpoint last_checkpoint;
  683. grub_uint64_t last_pseg;
  684. grub_uint32_t nblocks;
  685. unsigned int nilfs2_block_count;
  686. data = grub_malloc (sizeof (struct grub_nilfs2_data));
  687. if (!data)
  688. return 0;
  689. data->disk = disk;
  690. /* Read the superblock. */
  691. grub_nilfs2_load_sb (data);
  692. if (grub_errno)
  693. goto fail;
  694. nilfs2_block_count = (1 << LOG2_NILFS2_BLOCK_SIZE (data));
  695. /* Read the last segment summary. */
  696. last_pseg = grub_le_to_cpu64 (data->sblock.s_last_pseg);
  697. grub_disk_read (disk, last_pseg * nilfs2_block_count, 0,
  698. sizeof (struct grub_nilfs2_segment_summary), &ss);
  699. if (grub_errno)
  700. goto fail;
  701. /* Read the super root block. */
  702. nblocks = grub_le_to_cpu32 (ss.ss_nblocks);
  703. grub_disk_read (disk, (last_pseg + (nblocks - 1)) * nilfs2_block_count, 0,
  704. sizeof (struct grub_nilfs2_super_root), &data->sroot);
  705. if (grub_errno)
  706. goto fail;
  707. grub_nilfs2_read_last_checkpoint (data, &last_checkpoint);
  708. if (grub_errno)
  709. goto fail;
  710. grub_memcpy (&data->ifile, &last_checkpoint.cp_ifile_inode,
  711. sizeof (struct grub_nilfs2_inode));
  712. data->diropen.data = data;
  713. data->diropen.ino = 2;
  714. data->diropen.inode_read = 1;
  715. data->inode = &data->diropen.inode;
  716. grub_nilfs2_read_inode (data, 2, data->inode);
  717. return data;
  718. fail:
  719. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  720. grub_error (GRUB_ERR_BAD_FS, "not a nilfs2 filesystem");
  721. grub_free (data);
  722. return 0;
  723. }
  724. static char *
  725. grub_nilfs2_read_symlink (grub_fshelp_node_t node)
  726. {
  727. char *symlink;
  728. struct grub_fshelp_node *diro = node;
  729. if (!diro->inode_read)
  730. {
  731. grub_nilfs2_read_inode (diro->data, diro->ino, &diro->inode);
  732. if (grub_errno)
  733. return 0;
  734. }
  735. symlink = grub_malloc (grub_le_to_cpu64 (diro->inode.i_size) + 1);
  736. if (!symlink)
  737. return 0;
  738. grub_nilfs2_read_file (diro, 0, 0, 0,
  739. grub_le_to_cpu64 (diro->inode.i_size), symlink);
  740. if (grub_errno)
  741. {
  742. grub_free (symlink);
  743. return 0;
  744. }
  745. symlink[grub_le_to_cpu64 (diro->inode.i_size)] = '\0';
  746. return symlink;
  747. }
  748. static int
  749. grub_nilfs2_iterate_dir (grub_fshelp_node_t dir,
  750. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  751. {
  752. grub_off_t fpos = 0;
  753. struct grub_fshelp_node *diro = (struct grub_fshelp_node *) dir;
  754. if (!diro->inode_read)
  755. {
  756. grub_nilfs2_read_inode (diro->data, diro->ino, &diro->inode);
  757. if (grub_errno)
  758. return 0;
  759. }
  760. /* Iterate files. */
  761. while (fpos < grub_le_to_cpu64 (diro->inode.i_size))
  762. {
  763. struct grub_nilfs2_dir_entry dirent;
  764. grub_nilfs2_read_file (diro, 0, 0, fpos,
  765. sizeof (struct grub_nilfs2_dir_entry),
  766. (char *) &dirent);
  767. if (grub_errno)
  768. return 0;
  769. if (dirent.rec_len == 0)
  770. return 0;
  771. if (dirent.name_len != 0)
  772. {
  773. char filename[MAX_NAMELEN + 1];
  774. struct grub_fshelp_node *fdiro;
  775. enum grub_fshelp_filetype type = GRUB_FSHELP_UNKNOWN;
  776. grub_nilfs2_read_file (diro, 0, 0,
  777. fpos + sizeof (struct grub_nilfs2_dir_entry),
  778. dirent.name_len, filename);
  779. if (grub_errno)
  780. return 0;
  781. fdiro = grub_malloc (sizeof (struct grub_fshelp_node));
  782. if (!fdiro)
  783. return 0;
  784. fdiro->data = diro->data;
  785. fdiro->ino = grub_le_to_cpu64 (dirent.inode);
  786. filename[dirent.name_len] = '\0';
  787. if (dirent.file_type != NILFS_FT_UNKNOWN)
  788. {
  789. fdiro->inode_read = 0;
  790. if (dirent.file_type == NILFS_FT_DIR)
  791. type = GRUB_FSHELP_DIR;
  792. else if (dirent.file_type == NILFS_FT_SYMLINK)
  793. type = GRUB_FSHELP_SYMLINK;
  794. else if (dirent.file_type == NILFS_FT_REG_FILE)
  795. type = GRUB_FSHELP_REG;
  796. }
  797. else
  798. {
  799. /* The filetype can not be read from the dirent, read
  800. the inode to get more information. */
  801. grub_nilfs2_read_inode (diro->data,
  802. grub_le_to_cpu64 (dirent.inode),
  803. &fdiro->inode);
  804. if (grub_errno)
  805. {
  806. grub_free (fdiro);
  807. return 0;
  808. }
  809. fdiro->inode_read = 1;
  810. if ((grub_le_to_cpu16 (fdiro->inode.i_mode)
  811. & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
  812. type = GRUB_FSHELP_DIR;
  813. else if ((grub_le_to_cpu16 (fdiro->inode.i_mode)
  814. & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
  815. type = GRUB_FSHELP_SYMLINK;
  816. else if ((grub_le_to_cpu16 (fdiro->inode.i_mode)
  817. & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
  818. type = GRUB_FSHELP_REG;
  819. }
  820. if (hook (filename, type, fdiro, hook_data))
  821. return 1;
  822. }
  823. fpos += grub_le_to_cpu16 (dirent.rec_len);
  824. }
  825. return 0;
  826. }
  827. /* Open a file named NAME and initialize FILE. */
  828. static grub_err_t
  829. grub_nilfs2_open (struct grub_file *file, const char *name)
  830. {
  831. struct grub_nilfs2_data *data = NULL;
  832. struct grub_fshelp_node *fdiro = 0;
  833. grub_dl_ref (my_mod);
  834. data = grub_nilfs2_mount (file->device->disk);
  835. if (!data)
  836. goto fail;
  837. grub_fshelp_find_file (name, &data->diropen, &fdiro,
  838. grub_nilfs2_iterate_dir, grub_nilfs2_read_symlink,
  839. GRUB_FSHELP_REG);
  840. if (grub_errno)
  841. goto fail;
  842. if (!fdiro->inode_read)
  843. {
  844. grub_nilfs2_read_inode (data, fdiro->ino, &fdiro->inode);
  845. if (grub_errno)
  846. goto fail;
  847. }
  848. grub_memcpy (data->inode, &fdiro->inode, sizeof (struct grub_nilfs2_inode));
  849. grub_free (fdiro);
  850. file->size = grub_le_to_cpu64 (data->inode->i_size);
  851. file->data = data;
  852. file->offset = 0;
  853. return 0;
  854. fail:
  855. if (fdiro != &data->diropen)
  856. grub_free (fdiro);
  857. grub_free (data);
  858. grub_dl_unref (my_mod);
  859. return grub_errno;
  860. }
  861. static grub_err_t
  862. grub_nilfs2_close (grub_file_t file)
  863. {
  864. grub_free (file->data);
  865. grub_dl_unref (my_mod);
  866. return GRUB_ERR_NONE;
  867. }
  868. /* Read LEN bytes data from FILE into BUF. */
  869. static grub_ssize_t
  870. grub_nilfs2_read (grub_file_t file, char *buf, grub_size_t len)
  871. {
  872. struct grub_nilfs2_data *data = (struct grub_nilfs2_data *) file->data;
  873. return grub_nilfs2_read_file (&data->diropen,
  874. file->read_hook, file->read_hook_data,
  875. file->offset, len, buf);
  876. }
  877. /* Context for grub_nilfs2_dir. */
  878. struct grub_nilfs2_dir_ctx
  879. {
  880. grub_fs_dir_hook_t hook;
  881. void *hook_data;
  882. struct grub_nilfs2_data *data;
  883. };
  884. /* Helper for grub_nilfs2_dir. */
  885. static int
  886. grub_nilfs2_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
  887. grub_fshelp_node_t node, void *data)
  888. {
  889. struct grub_nilfs2_dir_ctx *ctx = data;
  890. struct grub_dirhook_info info;
  891. grub_memset (&info, 0, sizeof (info));
  892. if (!node->inode_read)
  893. {
  894. grub_nilfs2_read_inode (ctx->data, node->ino, &node->inode);
  895. if (!grub_errno)
  896. node->inode_read = 1;
  897. grub_errno = GRUB_ERR_NONE;
  898. }
  899. if (node->inode_read)
  900. {
  901. info.mtimeset = 1;
  902. info.mtime = grub_le_to_cpu64 (node->inode.i_mtime);
  903. }
  904. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  905. grub_free (node);
  906. return ctx->hook (filename, &info, ctx->hook_data);
  907. }
  908. static grub_err_t
  909. grub_nilfs2_dir (grub_device_t device, const char *path,
  910. grub_fs_dir_hook_t hook, void *hook_data)
  911. {
  912. struct grub_nilfs2_dir_ctx ctx = {
  913. .hook = hook,
  914. .hook_data = hook_data
  915. };
  916. struct grub_fshelp_node *fdiro = 0;
  917. grub_dl_ref (my_mod);
  918. ctx.data = grub_nilfs2_mount (device->disk);
  919. if (!ctx.data)
  920. goto fail;
  921. grub_fshelp_find_file (path, &ctx.data->diropen, &fdiro,
  922. grub_nilfs2_iterate_dir, grub_nilfs2_read_symlink,
  923. GRUB_FSHELP_DIR);
  924. if (grub_errno)
  925. goto fail;
  926. grub_nilfs2_iterate_dir (fdiro, grub_nilfs2_dir_iter, &ctx);
  927. fail:
  928. if (fdiro != &ctx.data->diropen)
  929. grub_free (fdiro);
  930. grub_free (ctx.data);
  931. grub_dl_unref (my_mod);
  932. return grub_errno;
  933. }
  934. static grub_err_t
  935. grub_nilfs2_label (grub_device_t device, char **label)
  936. {
  937. struct grub_nilfs2_data *data;
  938. grub_disk_t disk = device->disk;
  939. grub_dl_ref (my_mod);
  940. data = grub_nilfs2_mount (disk);
  941. if (data)
  942. *label = grub_strndup (data->sblock.s_volume_name,
  943. sizeof (data->sblock.s_volume_name));
  944. else
  945. *label = NULL;
  946. grub_dl_unref (my_mod);
  947. grub_free (data);
  948. return grub_errno;
  949. }
  950. static grub_err_t
  951. grub_nilfs2_uuid (grub_device_t device, char **uuid)
  952. {
  953. struct grub_nilfs2_data *data;
  954. grub_disk_t disk = device->disk;
  955. grub_dl_ref (my_mod);
  956. data = grub_nilfs2_mount (disk);
  957. if (data)
  958. {
  959. *uuid =
  960. grub_xasprintf
  961. ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  962. data->sblock.s_uuid[0], data->sblock.s_uuid[1],
  963. data->sblock.s_uuid[2], data->sblock.s_uuid[3],
  964. data->sblock.s_uuid[4], data->sblock.s_uuid[5],
  965. data->sblock.s_uuid[6], data->sblock.s_uuid[7],
  966. data->sblock.s_uuid[8], data->sblock.s_uuid[9],
  967. data->sblock.s_uuid[10], data->sblock.s_uuid[11],
  968. data->sblock.s_uuid[12], data->sblock.s_uuid[13],
  969. data->sblock.s_uuid[14], data->sblock.s_uuid[15]);
  970. }
  971. else
  972. *uuid = NULL;
  973. grub_dl_unref (my_mod);
  974. grub_free (data);
  975. return grub_errno;
  976. }
  977. /* Get mtime. */
  978. static grub_err_t
  979. grub_nilfs2_mtime (grub_device_t device, grub_int32_t * tm)
  980. {
  981. struct grub_nilfs2_data *data;
  982. grub_disk_t disk = device->disk;
  983. grub_dl_ref (my_mod);
  984. data = grub_nilfs2_mount (disk);
  985. if (!data)
  986. *tm = 0;
  987. else
  988. *tm = (grub_int32_t) grub_le_to_cpu64 (data->sblock.s_wtime);
  989. grub_dl_unref (my_mod);
  990. grub_free (data);
  991. return grub_errno;
  992. }
  993. static struct grub_fs grub_nilfs2_fs = {
  994. .name = "nilfs2",
  995. .dir = grub_nilfs2_dir,
  996. .open = grub_nilfs2_open,
  997. .read = grub_nilfs2_read,
  998. .close = grub_nilfs2_close,
  999. .label = grub_nilfs2_label,
  1000. .uuid = grub_nilfs2_uuid,
  1001. .mtime = grub_nilfs2_mtime,
  1002. #ifdef GRUB_UTIL
  1003. .reserved_first_sector = 1,
  1004. .blocklist_install = 0,
  1005. #endif
  1006. .next = 0
  1007. };
  1008. GRUB_MOD_INIT (nilfs2)
  1009. {
  1010. COMPILE_TIME_ASSERT ((1 << LOG_NILFS_DAT_ENTRY_SIZE)
  1011. == sizeof (struct
  1012. grub_nilfs2_dat_entry));
  1013. COMPILE_TIME_ASSERT (1 << LOG_INODE_SIZE
  1014. == sizeof (struct grub_nilfs2_inode));
  1015. grub_fs_register (&grub_nilfs2_fs);
  1016. my_mod = mod;
  1017. }
  1018. GRUB_MOD_FINI (nilfs2)
  1019. {
  1020. grub_fs_unregister (&grub_nilfs2_fs);
  1021. }