ext4_write.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*
  2. * (C) Copyright 2011 - 2012 Samsung Electronics
  3. * EXT4 filesystem implementation in Uboot by
  4. * Uma Shankar <uma.shankar@samsung.com>
  5. * Manjunatha C Achar <a.manjunatha@samsung.com>
  6. *
  7. * ext4ls and ext4load : Based on ext2 ls and load support in Uboot.
  8. * Ext4 read optimization taken from Open-Moko
  9. * Qi bootloader
  10. *
  11. * (C) Copyright 2004
  12. * esd gmbh <www.esd-electronics.com>
  13. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  14. *
  15. * based on code from grub2 fs/ext2.c and fs/fshelp.c by
  16. * GRUB -- GRand Unified Bootloader
  17. * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  18. *
  19. * ext4write : Based on generic ext4 protocol.
  20. *
  21. * SPDX-License-Identifier: GPL-2.0+
  22. */
  23. #include <common.h>
  24. #include <memalign.h>
  25. #include <linux/stat.h>
  26. #include <div64.h>
  27. #include "ext4_common.h"
  28. static inline void ext4fs_sb_free_inodes_inc(struct ext2_sblock *sb)
  29. {
  30. sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) + 1);
  31. }
  32. static inline void ext4fs_sb_free_blocks_inc(struct ext2_sblock *sb)
  33. {
  34. sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) + 1);
  35. }
  36. static inline void ext4fs_bg_free_inodes_inc(struct ext2_block_group *bg)
  37. {
  38. bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) + 1);
  39. }
  40. static inline void ext4fs_bg_free_blocks_inc(struct ext2_block_group *bg)
  41. {
  42. bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) + 1);
  43. }
  44. static void ext4fs_update(void)
  45. {
  46. short i;
  47. ext4fs_update_journal();
  48. struct ext_filesystem *fs = get_fs();
  49. /* update super block */
  50. put_ext4((uint64_t)(SUPERBLOCK_SIZE),
  51. (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
  52. /* update block groups */
  53. for (i = 0; i < fs->no_blkgrp; i++) {
  54. fs->bgd[i].bg_checksum = cpu_to_le16(ext4fs_checksum_update(i));
  55. put_ext4((uint64_t)le32_to_cpu(fs->bgd[i].block_id) * fs->blksz,
  56. fs->blk_bmaps[i], fs->blksz);
  57. }
  58. /* update inode table groups */
  59. for (i = 0; i < fs->no_blkgrp; i++) {
  60. put_ext4((uint64_t)le32_to_cpu(fs->bgd[i].inode_id) * fs->blksz,
  61. fs->inode_bmaps[i], fs->blksz);
  62. }
  63. /* update the block group descriptor table */
  64. put_ext4((uint64_t)((uint64_t)fs->gdtable_blkno * (uint64_t)fs->blksz),
  65. (struct ext2_block_group *)fs->gdtable,
  66. (fs->blksz * fs->no_blk_pergdt));
  67. ext4fs_dump_metadata();
  68. gindex = 0;
  69. gd_index = 0;
  70. }
  71. int ext4fs_get_bgdtable(void)
  72. {
  73. int status;
  74. int grp_desc_size;
  75. struct ext_filesystem *fs = get_fs();
  76. grp_desc_size = sizeof(struct ext2_block_group);
  77. fs->no_blk_pergdt = (fs->no_blkgrp * grp_desc_size) / fs->blksz;
  78. if ((fs->no_blkgrp * grp_desc_size) % fs->blksz)
  79. fs->no_blk_pergdt++;
  80. /* allocate memory for gdtable */
  81. fs->gdtable = zalloc(fs->blksz * fs->no_blk_pergdt);
  82. if (!fs->gdtable)
  83. return -ENOMEM;
  84. /* read the group descriptor table */
  85. status = ext4fs_devread((lbaint_t)fs->gdtable_blkno * fs->sect_perblk,
  86. 0, fs->blksz * fs->no_blk_pergdt, fs->gdtable);
  87. if (status == 0)
  88. goto fail;
  89. if (ext4fs_log_gdt(fs->gdtable)) {
  90. printf("Error in ext4fs_log_gdt\n");
  91. return -1;
  92. }
  93. return 0;
  94. fail:
  95. free(fs->gdtable);
  96. fs->gdtable = NULL;
  97. return -1;
  98. }
  99. static void delete_single_indirect_block(struct ext2_inode *inode)
  100. {
  101. struct ext2_block_group *bgd = NULL;
  102. static int prev_bg_bmap_idx = -1;
  103. uint32_t blknr;
  104. int remainder;
  105. int bg_idx;
  106. int status;
  107. uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
  108. struct ext_filesystem *fs = get_fs();
  109. char *journal_buffer = zalloc(fs->blksz);
  110. if (!journal_buffer) {
  111. printf("No memory\n");
  112. return;
  113. }
  114. /* get block group descriptor table */
  115. bgd = (struct ext2_block_group *)fs->gdtable;
  116. /* deleting the single indirect block associated with inode */
  117. if (inode->b.blocks.indir_block != 0) {
  118. blknr = le32_to_cpu(inode->b.blocks.indir_block);
  119. debug("SIPB releasing %u\n", blknr);
  120. bg_idx = blknr / blk_per_grp;
  121. if (fs->blksz == 1024) {
  122. remainder = blknr % blk_per_grp;
  123. if (!remainder)
  124. bg_idx--;
  125. }
  126. ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
  127. ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
  128. ext4fs_sb_free_blocks_inc(fs->sb);
  129. /* journal backup */
  130. if (prev_bg_bmap_idx != bg_idx) {
  131. status = ext4fs_devread(
  132. (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
  133. fs->sect_perblk, 0, fs->blksz,
  134. journal_buffer);
  135. if (status == 0)
  136. goto fail;
  137. if (ext4fs_log_journal
  138. (journal_buffer, le32_to_cpu(bgd[bg_idx].block_id)))
  139. goto fail;
  140. prev_bg_bmap_idx = bg_idx;
  141. }
  142. }
  143. fail:
  144. free(journal_buffer);
  145. }
  146. static void delete_double_indirect_block(struct ext2_inode *inode)
  147. {
  148. int i;
  149. short status;
  150. static int prev_bg_bmap_idx = -1;
  151. uint32_t blknr;
  152. int remainder;
  153. int bg_idx;
  154. uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
  155. __le32 *di_buffer = NULL;
  156. void *dib_start_addr = NULL;
  157. struct ext2_block_group *bgd = NULL;
  158. struct ext_filesystem *fs = get_fs();
  159. char *journal_buffer = zalloc(fs->blksz);
  160. if (!journal_buffer) {
  161. printf("No memory\n");
  162. return;
  163. }
  164. /* get the block group descriptor table */
  165. bgd = (struct ext2_block_group *)fs->gdtable;
  166. if (inode->b.blocks.double_indir_block != 0) {
  167. di_buffer = zalloc(fs->blksz);
  168. if (!di_buffer) {
  169. printf("No memory\n");
  170. return;
  171. }
  172. dib_start_addr = di_buffer;
  173. blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
  174. status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
  175. fs->blksz, (char *)di_buffer);
  176. for (i = 0; i < fs->blksz / sizeof(int); i++) {
  177. if (*di_buffer == 0)
  178. break;
  179. debug("DICB releasing %u\n", *di_buffer);
  180. bg_idx = le32_to_cpu(*di_buffer) / blk_per_grp;
  181. if (fs->blksz == 1024) {
  182. remainder = le32_to_cpu(*di_buffer) % blk_per_grp;
  183. if (!remainder)
  184. bg_idx--;
  185. }
  186. ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer),
  187. fs->blk_bmaps[bg_idx], bg_idx);
  188. di_buffer++;
  189. ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
  190. ext4fs_sb_free_blocks_inc(fs->sb);
  191. /* journal backup */
  192. if (prev_bg_bmap_idx != bg_idx) {
  193. status = ext4fs_devread(
  194. (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
  195. * fs->sect_perblk, 0,
  196. fs->blksz,
  197. journal_buffer);
  198. if (status == 0)
  199. goto fail;
  200. if (ext4fs_log_journal(journal_buffer,
  201. le32_to_cpu(bgd[bg_idx].block_id)))
  202. goto fail;
  203. prev_bg_bmap_idx = bg_idx;
  204. }
  205. }
  206. /* removing the parent double indirect block */
  207. blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
  208. bg_idx = blknr / blk_per_grp;
  209. if (fs->blksz == 1024) {
  210. remainder = blknr % blk_per_grp;
  211. if (!remainder)
  212. bg_idx--;
  213. }
  214. ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
  215. ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
  216. ext4fs_sb_free_blocks_inc(fs->sb);
  217. /* journal backup */
  218. if (prev_bg_bmap_idx != bg_idx) {
  219. memset(journal_buffer, '\0', fs->blksz);
  220. status = ext4fs_devread((lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
  221. fs->sect_perblk, 0, fs->blksz,
  222. journal_buffer);
  223. if (status == 0)
  224. goto fail;
  225. if (ext4fs_log_journal(journal_buffer,
  226. le32_to_cpu(bgd[bg_idx].block_id)))
  227. goto fail;
  228. prev_bg_bmap_idx = bg_idx;
  229. }
  230. debug("DIPB releasing %d\n", blknr);
  231. }
  232. fail:
  233. free(dib_start_addr);
  234. free(journal_buffer);
  235. }
  236. static void delete_triple_indirect_block(struct ext2_inode *inode)
  237. {
  238. int i, j;
  239. short status;
  240. static int prev_bg_bmap_idx = -1;
  241. uint32_t blknr;
  242. int remainder;
  243. int bg_idx;
  244. uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
  245. __le32 *tigp_buffer = NULL;
  246. void *tib_start_addr = NULL;
  247. __le32 *tip_buffer = NULL;
  248. void *tipb_start_addr = NULL;
  249. struct ext2_block_group *bgd = NULL;
  250. struct ext_filesystem *fs = get_fs();
  251. char *journal_buffer = zalloc(fs->blksz);
  252. if (!journal_buffer) {
  253. printf("No memory\n");
  254. return;
  255. }
  256. /* get block group descriptor table */
  257. bgd = (struct ext2_block_group *)fs->gdtable;
  258. if (inode->b.blocks.triple_indir_block != 0) {
  259. tigp_buffer = zalloc(fs->blksz);
  260. if (!tigp_buffer) {
  261. printf("No memory\n");
  262. return;
  263. }
  264. tib_start_addr = tigp_buffer;
  265. blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
  266. status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
  267. fs->blksz, (char *)tigp_buffer);
  268. for (i = 0; i < fs->blksz / sizeof(int); i++) {
  269. if (*tigp_buffer == 0)
  270. break;
  271. debug("tigp buffer releasing %u\n", *tigp_buffer);
  272. tip_buffer = zalloc(fs->blksz);
  273. if (!tip_buffer)
  274. goto fail;
  275. tipb_start_addr = tip_buffer;
  276. status = ext4fs_devread((lbaint_t)le32_to_cpu(*tigp_buffer) *
  277. fs->sect_perblk, 0, fs->blksz,
  278. (char *)tip_buffer);
  279. for (j = 0; j < fs->blksz / sizeof(int); j++) {
  280. if (le32_to_cpu(*tip_buffer) == 0)
  281. break;
  282. bg_idx = le32_to_cpu(*tip_buffer) / blk_per_grp;
  283. if (fs->blksz == 1024) {
  284. remainder = le32_to_cpu(*tip_buffer) % blk_per_grp;
  285. if (!remainder)
  286. bg_idx--;
  287. }
  288. ext4fs_reset_block_bmap(le32_to_cpu(*tip_buffer),
  289. fs->blk_bmaps[bg_idx],
  290. bg_idx);
  291. tip_buffer++;
  292. ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
  293. ext4fs_sb_free_blocks_inc(fs->sb);
  294. /* journal backup */
  295. if (prev_bg_bmap_idx != bg_idx) {
  296. status =
  297. ext4fs_devread(
  298. (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
  299. fs->sect_perblk, 0,
  300. fs->blksz,
  301. journal_buffer);
  302. if (status == 0)
  303. goto fail;
  304. if (ext4fs_log_journal(journal_buffer,
  305. le32_to_cpu(bgd[bg_idx].block_id)))
  306. goto fail;
  307. prev_bg_bmap_idx = bg_idx;
  308. }
  309. }
  310. free(tipb_start_addr);
  311. tipb_start_addr = NULL;
  312. /*
  313. * removing the grand parent blocks
  314. * which is connected to inode
  315. */
  316. bg_idx = le32_to_cpu(*tigp_buffer) / blk_per_grp;
  317. if (fs->blksz == 1024) {
  318. remainder = le32_to_cpu(*tigp_buffer) % blk_per_grp;
  319. if (!remainder)
  320. bg_idx--;
  321. }
  322. ext4fs_reset_block_bmap(le32_to_cpu(*tigp_buffer),
  323. fs->blk_bmaps[bg_idx], bg_idx);
  324. tigp_buffer++;
  325. ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
  326. ext4fs_sb_free_blocks_inc(fs->sb);
  327. /* journal backup */
  328. if (prev_bg_bmap_idx != bg_idx) {
  329. memset(journal_buffer, '\0', fs->blksz);
  330. status =
  331. ext4fs_devread(
  332. (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
  333. fs->sect_perblk, 0,
  334. fs->blksz, journal_buffer);
  335. if (status == 0)
  336. goto fail;
  337. if (ext4fs_log_journal(journal_buffer,
  338. le32_to_cpu(bgd[bg_idx].block_id)))
  339. goto fail;
  340. prev_bg_bmap_idx = bg_idx;
  341. }
  342. }
  343. /* removing the grand parent triple indirect block */
  344. blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
  345. bg_idx = blknr / blk_per_grp;
  346. if (fs->blksz == 1024) {
  347. remainder = blknr % blk_per_grp;
  348. if (!remainder)
  349. bg_idx--;
  350. }
  351. ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
  352. ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
  353. ext4fs_sb_free_blocks_inc(fs->sb);
  354. /* journal backup */
  355. if (prev_bg_bmap_idx != bg_idx) {
  356. memset(journal_buffer, '\0', fs->blksz);
  357. status = ext4fs_devread(
  358. (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
  359. fs->sect_perblk, 0, fs->blksz,
  360. journal_buffer);
  361. if (status == 0)
  362. goto fail;
  363. if (ext4fs_log_journal(journal_buffer,
  364. le32_to_cpu(bgd[bg_idx].block_id)))
  365. goto fail;
  366. prev_bg_bmap_idx = bg_idx;
  367. }
  368. debug("tigp buffer itself releasing %d\n", blknr);
  369. }
  370. fail:
  371. free(tib_start_addr);
  372. free(tipb_start_addr);
  373. free(journal_buffer);
  374. }
  375. static int ext4fs_delete_file(int inodeno)
  376. {
  377. struct ext2_inode inode;
  378. short status;
  379. int i;
  380. int remainder;
  381. long int blknr;
  382. int bg_idx;
  383. int ibmap_idx;
  384. char *read_buffer = NULL;
  385. char *start_block_address = NULL;
  386. uint32_t no_blocks;
  387. static int prev_bg_bmap_idx = -1;
  388. unsigned int inodes_per_block;
  389. uint32_t blkno;
  390. unsigned int blkoff;
  391. uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
  392. uint32_t inode_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
  393. struct ext2_inode *inode_buffer = NULL;
  394. struct ext2_block_group *bgd = NULL;
  395. struct ext_filesystem *fs = get_fs();
  396. char *journal_buffer = zalloc(fs->blksz);
  397. if (!journal_buffer)
  398. return -ENOMEM;
  399. /* get the block group descriptor table */
  400. bgd = (struct ext2_block_group *)fs->gdtable;
  401. status = ext4fs_read_inode(ext4fs_root, inodeno, &inode);
  402. if (status == 0)
  403. goto fail;
  404. /* read the block no allocated to a file */
  405. no_blocks = le32_to_cpu(inode.size) / fs->blksz;
  406. if (le32_to_cpu(inode.size) % fs->blksz)
  407. no_blocks++;
  408. if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) {
  409. struct ext2fs_node *node_inode =
  410. zalloc(sizeof(struct ext2fs_node));
  411. if (!node_inode)
  412. goto fail;
  413. node_inode->data = ext4fs_root;
  414. node_inode->ino = inodeno;
  415. node_inode->inode_read = 0;
  416. memcpy(&(node_inode->inode), &inode, sizeof(struct ext2_inode));
  417. for (i = 0; i < no_blocks; i++) {
  418. blknr = read_allocated_block(&(node_inode->inode), i);
  419. bg_idx = blknr / blk_per_grp;
  420. if (fs->blksz == 1024) {
  421. remainder = blknr % blk_per_grp;
  422. if (!remainder)
  423. bg_idx--;
  424. }
  425. ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx],
  426. bg_idx);
  427. debug("EXT4_EXTENTS Block releasing %ld: %d\n",
  428. blknr, bg_idx);
  429. ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
  430. ext4fs_sb_free_blocks_inc(fs->sb);
  431. /* journal backup */
  432. if (prev_bg_bmap_idx != bg_idx) {
  433. status =
  434. ext4fs_devread(
  435. (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
  436. fs->sect_perblk, 0,
  437. fs->blksz, journal_buffer);
  438. if (status == 0)
  439. goto fail;
  440. if (ext4fs_log_journal(journal_buffer,
  441. le32_to_cpu(bgd[bg_idx].block_id)))
  442. goto fail;
  443. prev_bg_bmap_idx = bg_idx;
  444. }
  445. }
  446. if (node_inode) {
  447. free(node_inode);
  448. node_inode = NULL;
  449. }
  450. } else {
  451. delete_single_indirect_block(&inode);
  452. delete_double_indirect_block(&inode);
  453. delete_triple_indirect_block(&inode);
  454. /* read the block no allocated to a file */
  455. no_blocks = le32_to_cpu(inode.size) / fs->blksz;
  456. if (le32_to_cpu(inode.size) % fs->blksz)
  457. no_blocks++;
  458. for (i = 0; i < no_blocks; i++) {
  459. blknr = read_allocated_block(&inode, i);
  460. bg_idx = blknr / blk_per_grp;
  461. if (fs->blksz == 1024) {
  462. remainder = blknr % blk_per_grp;
  463. if (!remainder)
  464. bg_idx--;
  465. }
  466. ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx],
  467. bg_idx);
  468. debug("ActualB releasing %ld: %d\n", blknr, bg_idx);
  469. ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
  470. ext4fs_sb_free_blocks_inc(fs->sb);
  471. /* journal backup */
  472. if (prev_bg_bmap_idx != bg_idx) {
  473. memset(journal_buffer, '\0', fs->blksz);
  474. status = ext4fs_devread(
  475. (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
  476. * fs->sect_perblk,
  477. 0, fs->blksz,
  478. journal_buffer);
  479. if (status == 0)
  480. goto fail;
  481. if (ext4fs_log_journal(journal_buffer,
  482. le32_to_cpu(bgd[bg_idx].block_id)))
  483. goto fail;
  484. prev_bg_bmap_idx = bg_idx;
  485. }
  486. }
  487. }
  488. /* from the inode no to blockno */
  489. inodes_per_block = fs->blksz / fs->inodesz;
  490. ibmap_idx = inodeno / inode_per_grp;
  491. /* get the block no */
  492. inodeno--;
  493. blkno = le32_to_cpu(bgd[ibmap_idx].inode_table_id) +
  494. (inodeno % inode_per_grp) / inodes_per_block;
  495. /* get the offset of the inode */
  496. blkoff = ((inodeno) % inodes_per_block) * fs->inodesz;
  497. /* read the block no containing the inode */
  498. read_buffer = zalloc(fs->blksz);
  499. if (!read_buffer)
  500. goto fail;
  501. start_block_address = read_buffer;
  502. status = ext4fs_devread((lbaint_t)blkno * fs->sect_perblk,
  503. 0, fs->blksz, read_buffer);
  504. if (status == 0)
  505. goto fail;
  506. if (ext4fs_log_journal(read_buffer, blkno))
  507. goto fail;
  508. read_buffer = read_buffer + blkoff;
  509. inode_buffer = (struct ext2_inode *)read_buffer;
  510. memset(inode_buffer, '\0', sizeof(struct ext2_inode));
  511. /* write the inode to original position in inode table */
  512. if (ext4fs_put_metadata(start_block_address, blkno))
  513. goto fail;
  514. /* update the respective inode bitmaps */
  515. inodeno++;
  516. ext4fs_reset_inode_bmap(inodeno, fs->inode_bmaps[ibmap_idx], ibmap_idx);
  517. ext4fs_bg_free_inodes_inc(&bgd[ibmap_idx]);
  518. ext4fs_sb_free_inodes_inc(fs->sb);
  519. /* journal backup */
  520. memset(journal_buffer, '\0', fs->blksz);
  521. status = ext4fs_devread((lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id) *
  522. fs->sect_perblk, 0, fs->blksz, journal_buffer);
  523. if (status == 0)
  524. goto fail;
  525. if (ext4fs_log_journal(journal_buffer, le32_to_cpu(bgd[ibmap_idx].inode_id)))
  526. goto fail;
  527. ext4fs_update();
  528. ext4fs_deinit();
  529. ext4fs_reinit_global();
  530. if (ext4fs_init() != 0) {
  531. printf("error in File System init\n");
  532. goto fail;
  533. }
  534. free(start_block_address);
  535. free(journal_buffer);
  536. return 0;
  537. fail:
  538. free(start_block_address);
  539. free(journal_buffer);
  540. return -1;
  541. }
  542. int ext4fs_init(void)
  543. {
  544. short status;
  545. int i;
  546. uint32_t real_free_blocks = 0;
  547. struct ext_filesystem *fs = get_fs();
  548. /* populate fs */
  549. fs->blksz = EXT2_BLOCK_SIZE(ext4fs_root);
  550. fs->inodesz = INODE_SIZE_FILESYSTEM(ext4fs_root);
  551. fs->sect_perblk = fs->blksz >> fs->dev_desc->log2blksz;
  552. /* get the superblock */
  553. fs->sb = zalloc(SUPERBLOCK_SIZE);
  554. if (!fs->sb)
  555. return -ENOMEM;
  556. if (!ext4_read_superblock((char *)fs->sb))
  557. goto fail;
  558. /* init journal */
  559. if (ext4fs_init_journal())
  560. goto fail;
  561. /* get total no of blockgroups */
  562. fs->no_blkgrp = (uint32_t)ext4fs_div_roundup(
  563. le32_to_cpu(ext4fs_root->sblock.total_blocks)
  564. - le32_to_cpu(ext4fs_root->sblock.first_data_block),
  565. le32_to_cpu(ext4fs_root->sblock.blocks_per_group));
  566. /* get the block group descriptor table */
  567. fs->gdtable_blkno = ((EXT2_MIN_BLOCK_SIZE == fs->blksz) + 1);
  568. if (ext4fs_get_bgdtable() == -1) {
  569. printf("Error in getting the block group descriptor table\n");
  570. goto fail;
  571. }
  572. fs->bgd = (struct ext2_block_group *)fs->gdtable;
  573. /* load all the available bitmap block of the partition */
  574. fs->blk_bmaps = zalloc(fs->no_blkgrp * sizeof(char *));
  575. if (!fs->blk_bmaps)
  576. goto fail;
  577. for (i = 0; i < fs->no_blkgrp; i++) {
  578. fs->blk_bmaps[i] = zalloc(fs->blksz);
  579. if (!fs->blk_bmaps[i])
  580. goto fail;
  581. }
  582. for (i = 0; i < fs->no_blkgrp; i++) {
  583. status =
  584. ext4fs_devread(
  585. (lbaint_t)le32_to_cpu(fs->bgd[i].block_id) *
  586. fs->sect_perblk, 0,
  587. fs->blksz, (char *)fs->blk_bmaps[i]);
  588. if (status == 0)
  589. goto fail;
  590. }
  591. /* load all the available inode bitmap of the partition */
  592. fs->inode_bmaps = zalloc(fs->no_blkgrp * sizeof(unsigned char *));
  593. if (!fs->inode_bmaps)
  594. goto fail;
  595. for (i = 0; i < fs->no_blkgrp; i++) {
  596. fs->inode_bmaps[i] = zalloc(fs->blksz);
  597. if (!fs->inode_bmaps[i])
  598. goto fail;
  599. }
  600. for (i = 0; i < fs->no_blkgrp; i++) {
  601. status = ext4fs_devread(
  602. (lbaint_t)le32_to_cpu(fs->bgd[i].inode_id) *
  603. fs->sect_perblk,
  604. 0, fs->blksz,
  605. (char *)fs->inode_bmaps[i]);
  606. if (status == 0)
  607. goto fail;
  608. }
  609. /*
  610. * check filesystem consistency with free blocks of file system
  611. * some time we observed that superblock freeblocks does not match
  612. * with the blockgroups freeblocks when improper
  613. * reboot of a linux kernel
  614. */
  615. for (i = 0; i < fs->no_blkgrp; i++)
  616. real_free_blocks = real_free_blocks + le16_to_cpu(fs->bgd[i].free_blocks);
  617. if (real_free_blocks != le32_to_cpu(fs->sb->free_blocks))
  618. fs->sb->free_blocks = cpu_to_le32(real_free_blocks);
  619. return 0;
  620. fail:
  621. ext4fs_deinit();
  622. return -1;
  623. }
  624. void ext4fs_deinit(void)
  625. {
  626. int i;
  627. struct ext2_inode inode_journal;
  628. struct journal_superblock_t *jsb;
  629. uint32_t blknr;
  630. struct ext_filesystem *fs = get_fs();
  631. uint32_t new_feature_incompat;
  632. /* free journal */
  633. char *temp_buff = zalloc(fs->blksz);
  634. if (temp_buff) {
  635. ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
  636. &inode_journal);
  637. blknr = read_allocated_block(&inode_journal,
  638. EXT2_JOURNAL_SUPERBLOCK);
  639. ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
  640. temp_buff);
  641. jsb = (struct journal_superblock_t *)temp_buff;
  642. jsb->s_start = 0;
  643. put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
  644. (struct journal_superblock_t *)temp_buff, fs->blksz);
  645. free(temp_buff);
  646. }
  647. ext4fs_free_journal();
  648. /* get the superblock */
  649. ext4_read_superblock((char *)fs->sb);
  650. new_feature_incompat = le32_to_cpu(fs->sb->feature_incompat);
  651. new_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
  652. fs->sb->feature_incompat = cpu_to_le32(new_feature_incompat);
  653. put_ext4((uint64_t)(SUPERBLOCK_SIZE),
  654. (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
  655. free(fs->sb);
  656. fs->sb = NULL;
  657. if (fs->blk_bmaps) {
  658. for (i = 0; i < fs->no_blkgrp; i++) {
  659. free(fs->blk_bmaps[i]);
  660. fs->blk_bmaps[i] = NULL;
  661. }
  662. free(fs->blk_bmaps);
  663. fs->blk_bmaps = NULL;
  664. }
  665. if (fs->inode_bmaps) {
  666. for (i = 0; i < fs->no_blkgrp; i++) {
  667. free(fs->inode_bmaps[i]);
  668. fs->inode_bmaps[i] = NULL;
  669. }
  670. free(fs->inode_bmaps);
  671. fs->inode_bmaps = NULL;
  672. }
  673. free(fs->gdtable);
  674. fs->gdtable = NULL;
  675. fs->bgd = NULL;
  676. /*
  677. * reinitiliazed the global inode and
  678. * block bitmap first execution check variables
  679. */
  680. fs->first_pass_ibmap = 0;
  681. fs->first_pass_bbmap = 0;
  682. fs->curr_inode_no = 0;
  683. fs->curr_blkno = 0;
  684. }
  685. static int ext4fs_write_file(struct ext2_inode *file_inode,
  686. int pos, unsigned int len, char *buf)
  687. {
  688. int i;
  689. int blockcnt;
  690. uint32_t filesize = le32_to_cpu(file_inode->size);
  691. struct ext_filesystem *fs = get_fs();
  692. int log2blksz = fs->dev_desc->log2blksz;
  693. int log2_fs_blocksize = LOG2_BLOCK_SIZE(ext4fs_root) - log2blksz;
  694. int previous_block_number = -1;
  695. int delayed_start = 0;
  696. int delayed_extent = 0;
  697. int delayed_next = 0;
  698. char *delayed_buf = NULL;
  699. /* Adjust len so it we can't read past the end of the file. */
  700. if (len > filesize)
  701. len = filesize;
  702. blockcnt = ((len + pos) + fs->blksz - 1) / fs->blksz;
  703. for (i = pos / fs->blksz; i < blockcnt; i++) {
  704. long int blknr;
  705. int blockend = fs->blksz;
  706. int skipfirst = 0;
  707. blknr = read_allocated_block(file_inode, i);
  708. if (blknr < 0)
  709. return -1;
  710. blknr = blknr << log2_fs_blocksize;
  711. if (blknr) {
  712. if (previous_block_number != -1) {
  713. if (delayed_next == blknr) {
  714. delayed_extent += blockend;
  715. delayed_next += blockend >> log2blksz;
  716. } else { /* spill */
  717. put_ext4((uint64_t)
  718. ((uint64_t)delayed_start << log2blksz),
  719. delayed_buf,
  720. (uint32_t) delayed_extent);
  721. previous_block_number = blknr;
  722. delayed_start = blknr;
  723. delayed_extent = blockend;
  724. delayed_buf = buf;
  725. delayed_next = blknr +
  726. (blockend >> log2blksz);
  727. }
  728. } else {
  729. previous_block_number = blknr;
  730. delayed_start = blknr;
  731. delayed_extent = blockend;
  732. delayed_buf = buf;
  733. delayed_next = blknr +
  734. (blockend >> log2blksz);
  735. }
  736. } else {
  737. if (previous_block_number != -1) {
  738. /* spill */
  739. put_ext4((uint64_t) ((uint64_t)delayed_start <<
  740. log2blksz),
  741. delayed_buf,
  742. (uint32_t) delayed_extent);
  743. previous_block_number = -1;
  744. }
  745. memset(buf, 0, fs->blksz - skipfirst);
  746. }
  747. buf += fs->blksz - skipfirst;
  748. }
  749. if (previous_block_number != -1) {
  750. /* spill */
  751. put_ext4((uint64_t) ((uint64_t)delayed_start << log2blksz),
  752. delayed_buf, (uint32_t) delayed_extent);
  753. previous_block_number = -1;
  754. }
  755. return len;
  756. }
  757. int ext4fs_write(const char *fname, unsigned char *buffer,
  758. unsigned long sizebytes)
  759. {
  760. int ret = 0;
  761. struct ext2_inode *file_inode = NULL;
  762. unsigned char *inode_buffer = NULL;
  763. int parent_inodeno;
  764. int inodeno;
  765. time_t timestamp = 0;
  766. uint64_t bytes_reqd_for_file;
  767. unsigned int blks_reqd_for_file;
  768. unsigned int blocks_remaining;
  769. int existing_file_inodeno;
  770. char *temp_ptr = NULL;
  771. long int itable_blkno;
  772. long int parent_itable_blkno;
  773. long int blkoff;
  774. struct ext2_sblock *sblock = &(ext4fs_root->sblock);
  775. unsigned int inodes_per_block;
  776. unsigned int ibmap_idx;
  777. struct ext_filesystem *fs = get_fs();
  778. ALLOC_CACHE_ALIGN_BUFFER(char, filename, 256);
  779. memset(filename, 0x00, 256);
  780. g_parent_inode = zalloc(sizeof(struct ext2_inode));
  781. if (!g_parent_inode)
  782. goto fail;
  783. if (ext4fs_init() != 0) {
  784. printf("error in File System init\n");
  785. return -1;
  786. }
  787. inodes_per_block = fs->blksz / fs->inodesz;
  788. parent_inodeno = ext4fs_get_parent_inode_num(fname, filename, F_FILE);
  789. if (parent_inodeno == -1)
  790. goto fail;
  791. if (ext4fs_iget(parent_inodeno, g_parent_inode))
  792. goto fail;
  793. /* check if the filename is already present in root */
  794. existing_file_inodeno = ext4fs_filename_unlink(filename);
  795. if (existing_file_inodeno != -1) {
  796. ret = ext4fs_delete_file(existing_file_inodeno);
  797. fs->first_pass_bbmap = 0;
  798. fs->curr_blkno = 0;
  799. fs->first_pass_ibmap = 0;
  800. fs->curr_inode_no = 0;
  801. if (ret)
  802. goto fail;
  803. }
  804. /* calucalate how many blocks required */
  805. bytes_reqd_for_file = sizebytes;
  806. blks_reqd_for_file = lldiv(bytes_reqd_for_file, fs->blksz);
  807. if (do_div(bytes_reqd_for_file, fs->blksz) != 0) {
  808. blks_reqd_for_file++;
  809. debug("total bytes for a file %u\n", blks_reqd_for_file);
  810. }
  811. blocks_remaining = blks_reqd_for_file;
  812. /* test for available space in partition */
  813. if (le32_to_cpu(fs->sb->free_blocks) < blks_reqd_for_file) {
  814. printf("Not enough space on partition !!!\n");
  815. goto fail;
  816. }
  817. inodeno = ext4fs_update_parent_dentry(filename, FILETYPE_REG);
  818. if (inodeno == -1)
  819. goto fail;
  820. /* prepare file inode */
  821. inode_buffer = zalloc(fs->inodesz);
  822. if (!inode_buffer)
  823. goto fail;
  824. file_inode = (struct ext2_inode *)inode_buffer;
  825. file_inode->mode = cpu_to_le16(S_IFREG | S_IRWXU |
  826. S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
  827. /* ToDo: Update correct time */
  828. file_inode->mtime = cpu_to_le32(timestamp);
  829. file_inode->atime = cpu_to_le32(timestamp);
  830. file_inode->ctime = cpu_to_le32(timestamp);
  831. file_inode->nlinks = cpu_to_le16(1);
  832. file_inode->size = cpu_to_le32(sizebytes);
  833. /* Allocate data blocks */
  834. ext4fs_allocate_blocks(file_inode, blocks_remaining,
  835. &blks_reqd_for_file);
  836. file_inode->blockcnt = cpu_to_le32((blks_reqd_for_file * fs->blksz) >>
  837. fs->dev_desc->log2blksz);
  838. temp_ptr = zalloc(fs->blksz);
  839. if (!temp_ptr)
  840. goto fail;
  841. ibmap_idx = inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
  842. inodeno--;
  843. itable_blkno = le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) +
  844. (inodeno % le32_to_cpu(sblock->inodes_per_group)) /
  845. inodes_per_block;
  846. blkoff = (inodeno % inodes_per_block) * fs->inodesz;
  847. ext4fs_devread((lbaint_t)itable_blkno * fs->sect_perblk, 0, fs->blksz,
  848. temp_ptr);
  849. if (ext4fs_log_journal(temp_ptr, itable_blkno))
  850. goto fail;
  851. memcpy(temp_ptr + blkoff, inode_buffer, fs->inodesz);
  852. if (ext4fs_put_metadata(temp_ptr, itable_blkno))
  853. goto fail;
  854. /* copy the file content into data blocks */
  855. if (ext4fs_write_file(file_inode, 0, sizebytes, (char *)buffer) == -1) {
  856. printf("Error in copying content\n");
  857. goto fail;
  858. }
  859. ibmap_idx = parent_inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
  860. parent_inodeno--;
  861. parent_itable_blkno = le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) +
  862. (parent_inodeno %
  863. le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
  864. blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz;
  865. if (parent_itable_blkno != itable_blkno) {
  866. memset(temp_ptr, '\0', fs->blksz);
  867. ext4fs_devread((lbaint_t)parent_itable_blkno * fs->sect_perblk,
  868. 0, fs->blksz, temp_ptr);
  869. if (ext4fs_log_journal(temp_ptr, parent_itable_blkno))
  870. goto fail;
  871. memcpy(temp_ptr + blkoff, g_parent_inode,
  872. sizeof(struct ext2_inode));
  873. if (ext4fs_put_metadata(temp_ptr, parent_itable_blkno))
  874. goto fail;
  875. free(temp_ptr);
  876. } else {
  877. /*
  878. * If parent and child fall in same inode table block
  879. * both should be kept in 1 buffer
  880. */
  881. memcpy(temp_ptr + blkoff, g_parent_inode,
  882. sizeof(struct ext2_inode));
  883. gd_index--;
  884. if (ext4fs_put_metadata(temp_ptr, itable_blkno))
  885. goto fail;
  886. free(temp_ptr);
  887. }
  888. ext4fs_update();
  889. ext4fs_deinit();
  890. fs->first_pass_bbmap = 0;
  891. fs->curr_blkno = 0;
  892. fs->first_pass_ibmap = 0;
  893. fs->curr_inode_no = 0;
  894. free(inode_buffer);
  895. free(g_parent_inode);
  896. g_parent_inode = NULL;
  897. return 0;
  898. fail:
  899. ext4fs_deinit();
  900. free(inode_buffer);
  901. free(g_parent_inode);
  902. g_parent_inode = NULL;
  903. return -1;
  904. }
  905. int ext4_write_file(const char *filename, void *buf, loff_t offset,
  906. loff_t len, loff_t *actwrite)
  907. {
  908. int ret;
  909. if (offset != 0) {
  910. printf("** Cannot support non-zero offset **\n");
  911. return -1;
  912. }
  913. ret = ext4fs_write(filename, buf, len);
  914. if (ret) {
  915. printf("** Error ext4fs_write() **\n");
  916. goto fail;
  917. }
  918. *actwrite = len;
  919. return 0;
  920. fail:
  921. *actwrite = 0;
  922. return -1;
  923. }