inode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * inode.c --- utility routines to read and write inodes
  4. *
  5. * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #if HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. #if HAVE_ERRNO_H
  18. #include <errno.h>
  19. #endif
  20. #if HAVE_SYS_STAT_H
  21. #include <sys/stat.h>
  22. #endif
  23. #if HAVE_SYS_TYPES_H
  24. #include <sys/types.h>
  25. #endif
  26. #include "ext2_fs.h"
  27. #include "ext2fsP.h"
  28. #include "e2image.h"
  29. struct ext2_struct_inode_scan {
  30. errcode_t magic;
  31. ext2_filsys fs;
  32. ext2_ino_t current_inode;
  33. blk_t current_block;
  34. dgrp_t current_group;
  35. ext2_ino_t inodes_left;
  36. blk_t blocks_left;
  37. dgrp_t groups_left;
  38. blk_t inode_buffer_blocks;
  39. char * inode_buffer;
  40. int inode_size;
  41. char * ptr;
  42. int bytes_left;
  43. char *temp_buffer;
  44. errcode_t (*done_group)(ext2_filsys fs,
  45. dgrp_t group,
  46. void * priv_data);
  47. void * done_group_data;
  48. int bad_block_ptr;
  49. int scan_flags;
  50. int reserved[6];
  51. };
  52. /*
  53. * This routine flushes the icache, if it exists.
  54. */
  55. errcode_t ext2fs_flush_icache(ext2_filsys fs)
  56. {
  57. int i;
  58. if (!fs->icache)
  59. return 0;
  60. for (i=0; i < fs->icache->cache_size; i++)
  61. fs->icache->cache[i].ino = 0;
  62. fs->icache->buffer_blk = 0;
  63. return 0;
  64. }
  65. static errcode_t create_icache(ext2_filsys fs)
  66. {
  67. errcode_t retval;
  68. if (fs->icache)
  69. return 0;
  70. retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache), &fs->icache);
  71. if (retval)
  72. return retval;
  73. memset(fs->icache, 0, sizeof(struct ext2_inode_cache));
  74. retval = ext2fs_get_mem(fs->blocksize, &fs->icache->buffer);
  75. if (retval) {
  76. ext2fs_free_mem(&fs->icache);
  77. return retval;
  78. }
  79. fs->icache->buffer_blk = 0;
  80. fs->icache->cache_last = -1;
  81. fs->icache->cache_size = 4;
  82. fs->icache->refcount = 1;
  83. retval = ext2fs_get_mem(sizeof(struct ext2_inode_cache_ent)
  84. * fs->icache->cache_size,
  85. &fs->icache->cache);
  86. if (retval) {
  87. ext2fs_free_mem(&fs->icache->buffer);
  88. ext2fs_free_mem(&fs->icache);
  89. return retval;
  90. }
  91. ext2fs_flush_icache(fs);
  92. return 0;
  93. }
  94. errcode_t ext2fs_open_inode_scan(ext2_filsys fs, int buffer_blocks,
  95. ext2_inode_scan *ret_scan)
  96. {
  97. ext2_inode_scan scan;
  98. errcode_t retval;
  99. errcode_t (*save_get_blocks)(ext2_filsys f, ext2_ino_t ino, blk_t *blocks);
  100. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  101. /*
  102. * If fs->badblocks isn't set, then set it --- since the inode
  103. * scanning functions require it.
  104. */
  105. if (fs->badblocks == 0) {
  106. /*
  107. * Temporarly save fs->get_blocks and set it to zero,
  108. * for compatibility with old e2fsck's.
  109. */
  110. save_get_blocks = fs->get_blocks;
  111. fs->get_blocks = 0;
  112. retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
  113. if (retval) {
  114. ext2fs_badblocks_list_free(fs->badblocks);
  115. fs->badblocks = 0;
  116. }
  117. fs->get_blocks = save_get_blocks;
  118. }
  119. retval = ext2fs_get_mem(sizeof(struct ext2_struct_inode_scan), &scan);
  120. if (retval)
  121. return retval;
  122. memset(scan, 0, sizeof(struct ext2_struct_inode_scan));
  123. scan->magic = EXT2_ET_MAGIC_INODE_SCAN;
  124. scan->fs = fs;
  125. scan->inode_size = EXT2_INODE_SIZE(fs->super);
  126. scan->bytes_left = 0;
  127. scan->current_group = 0;
  128. scan->groups_left = fs->group_desc_count - 1;
  129. scan->inode_buffer_blocks = buffer_blocks ? buffer_blocks : 8;
  130. scan->current_block = scan->fs->
  131. group_desc[scan->current_group].bg_inode_table;
  132. scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
  133. scan->blocks_left = scan->fs->inode_blocks_per_group;
  134. retval = ext2fs_get_mem((size_t) (scan->inode_buffer_blocks *
  135. fs->blocksize),
  136. &scan->inode_buffer);
  137. scan->done_group = 0;
  138. scan->done_group_data = 0;
  139. scan->bad_block_ptr = 0;
  140. if (retval) {
  141. ext2fs_free_mem(&scan);
  142. return retval;
  143. }
  144. retval = ext2fs_get_mem(scan->inode_size, &scan->temp_buffer);
  145. if (retval) {
  146. ext2fs_free_mem(&scan->inode_buffer);
  147. ext2fs_free_mem(&scan);
  148. return retval;
  149. }
  150. if (scan->fs->badblocks && scan->fs->badblocks->num)
  151. scan->scan_flags |= EXT2_SF_CHK_BADBLOCKS;
  152. *ret_scan = scan;
  153. return 0;
  154. }
  155. void ext2fs_close_inode_scan(ext2_inode_scan scan)
  156. {
  157. if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
  158. return;
  159. ext2fs_free_mem(&scan->inode_buffer);
  160. scan->inode_buffer = NULL;
  161. ext2fs_free_mem(&scan->temp_buffer);
  162. scan->temp_buffer = NULL;
  163. ext2fs_free_mem(&scan);
  164. }
  165. void ext2fs_set_inode_callback(ext2_inode_scan scan,
  166. errcode_t (*done_group)(ext2_filsys fs,
  167. dgrp_t group,
  168. void * priv_data),
  169. void *done_group_data)
  170. {
  171. if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
  172. return;
  173. scan->done_group = done_group;
  174. scan->done_group_data = done_group_data;
  175. }
  176. int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
  177. int clear_flags)
  178. {
  179. int old_flags;
  180. if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
  181. return 0;
  182. old_flags = scan->scan_flags;
  183. scan->scan_flags &= ~clear_flags;
  184. scan->scan_flags |= set_flags;
  185. return old_flags;
  186. }
  187. /*
  188. * This function is called by ext2fs_get_next_inode when it needs to
  189. * get ready to read in a new blockgroup.
  190. */
  191. static errcode_t get_next_blockgroup(ext2_inode_scan scan)
  192. {
  193. scan->current_group++;
  194. scan->groups_left--;
  195. scan->current_block = scan->fs->
  196. group_desc[scan->current_group].bg_inode_table;
  197. scan->current_inode = scan->current_group *
  198. EXT2_INODES_PER_GROUP(scan->fs->super);
  199. scan->bytes_left = 0;
  200. scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
  201. scan->blocks_left = scan->fs->inode_blocks_per_group;
  202. return 0;
  203. }
  204. errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
  205. int group)
  206. {
  207. scan->current_group = group - 1;
  208. scan->groups_left = scan->fs->group_desc_count - group;
  209. return get_next_blockgroup(scan);
  210. }
  211. /*
  212. * This function is called by get_next_blocks() to check for bad
  213. * blocks in the inode table.
  214. *
  215. * This function assumes that badblocks_list->list is sorted in
  216. * increasing order.
  217. */
  218. static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
  219. blk_t *num_blocks)
  220. {
  221. blk_t blk = scan->current_block;
  222. badblocks_list bb = scan->fs->badblocks;
  223. /*
  224. * If the inode table is missing, then obviously there are no
  225. * bad blocks. :-)
  226. */
  227. if (blk == 0)
  228. return 0;
  229. /*
  230. * If the current block is greater than the bad block listed
  231. * in the bad block list, then advance the pointer until this
  232. * is no longer the case. If we run out of bad blocks, then
  233. * we don't need to do any more checking!
  234. */
  235. while (blk > bb->list[scan->bad_block_ptr]) {
  236. if (++scan->bad_block_ptr >= bb->num) {
  237. scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
  238. return 0;
  239. }
  240. }
  241. /*
  242. * If the current block is equal to the bad block listed in
  243. * the bad block list, then handle that one block specially.
  244. * (We could try to handle runs of bad blocks, but that
  245. * only increases CPU efficiency by a small amount, at the
  246. * expense of a huge expense of code complexity, and for an
  247. * uncommon case at that.)
  248. */
  249. if (blk == bb->list[scan->bad_block_ptr]) {
  250. scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
  251. *num_blocks = 1;
  252. if (++scan->bad_block_ptr >= bb->num)
  253. scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
  254. return 0;
  255. }
  256. /*
  257. * If there is a bad block in the range that we're about to
  258. * read in, adjust the number of blocks to read so that we we
  259. * don't read in the bad block. (Then the next block to read
  260. * will be the bad block, which is handled in the above case.)
  261. */
  262. if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
  263. *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
  264. return 0;
  265. }
  266. /*
  267. * This function is called by ext2fs_get_next_inode when it needs to
  268. * read in more blocks from the current blockgroup's inode table.
  269. */
  270. static errcode_t get_next_blocks(ext2_inode_scan scan)
  271. {
  272. blk_t num_blocks;
  273. errcode_t retval;
  274. /*
  275. * Figure out how many blocks to read; we read at most
  276. * inode_buffer_blocks, and perhaps less if there aren't that
  277. * many blocks left to read.
  278. */
  279. num_blocks = scan->inode_buffer_blocks;
  280. if (num_blocks > scan->blocks_left)
  281. num_blocks = scan->blocks_left;
  282. /*
  283. * If the past block "read" was a bad block, then mark the
  284. * left-over extra bytes as also being bad.
  285. */
  286. if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
  287. if (scan->bytes_left)
  288. scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
  289. scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
  290. }
  291. /*
  292. * Do inode bad block processing, if necessary.
  293. */
  294. if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
  295. retval = check_for_inode_bad_blocks(scan, &num_blocks);
  296. if (retval)
  297. return retval;
  298. }
  299. if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
  300. (scan->current_block == 0)) {
  301. memset(scan->inode_buffer, 0,
  302. (size_t) num_blocks * scan->fs->blocksize);
  303. } else {
  304. retval = io_channel_read_blk(scan->fs->io,
  305. scan->current_block,
  306. (int) num_blocks,
  307. scan->inode_buffer);
  308. if (retval)
  309. return EXT2_ET_NEXT_INODE_READ;
  310. }
  311. scan->ptr = scan->inode_buffer;
  312. scan->bytes_left = num_blocks * scan->fs->blocksize;
  313. scan->blocks_left -= num_blocks;
  314. if (scan->current_block)
  315. scan->current_block += num_blocks;
  316. return 0;
  317. }
  318. errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
  319. struct ext2_inode *inode, int bufsize)
  320. {
  321. errcode_t retval;
  322. int extra_bytes = 0;
  323. EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
  324. /*
  325. * Do we need to start reading a new block group?
  326. */
  327. if (scan->inodes_left <= 0) {
  328. force_new_group:
  329. if (scan->done_group) {
  330. retval = (scan->done_group)
  331. (scan->fs, scan->current_group,
  332. scan->done_group_data);
  333. if (retval)
  334. return retval;
  335. }
  336. if (scan->groups_left <= 0) {
  337. *ino = 0;
  338. return 0;
  339. }
  340. retval = get_next_blockgroup(scan);
  341. if (retval)
  342. return retval;
  343. }
  344. /*
  345. * This is done outside the above if statement so that the
  346. * check can be done for block group #0.
  347. */
  348. if (scan->current_block == 0) {
  349. if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
  350. goto force_new_group;
  351. } else
  352. return EXT2_ET_MISSING_INODE_TABLE;
  353. }
  354. /*
  355. * Have we run out of space in the inode buffer? If so, we
  356. * need to read in more blocks.
  357. */
  358. if (scan->bytes_left < scan->inode_size) {
  359. memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
  360. extra_bytes = scan->bytes_left;
  361. retval = get_next_blocks(scan);
  362. if (retval)
  363. return retval;
  364. #if 0
  365. /*
  366. * XXX test Need check for used inode somehow.
  367. * (Note: this is hard.)
  368. */
  369. if (is_empty_scan(scan))
  370. goto force_new_group;
  371. #endif
  372. }
  373. retval = 0;
  374. if (extra_bytes) {
  375. memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
  376. scan->inode_size - extra_bytes);
  377. scan->ptr += scan->inode_size - extra_bytes;
  378. scan->bytes_left -= scan->inode_size - extra_bytes;
  379. #if BB_BIG_ENDIAN
  380. if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  381. (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
  382. ext2fs_swap_inode_full(scan->fs,
  383. (struct ext2_inode_large *) inode,
  384. (struct ext2_inode_large *) scan->temp_buffer,
  385. 0, bufsize);
  386. else
  387. #endif
  388. *inode = *((struct ext2_inode *) scan->temp_buffer);
  389. if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
  390. retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
  391. scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
  392. } else {
  393. #if BB_BIG_ENDIAN
  394. if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  395. (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
  396. ext2fs_swap_inode_full(scan->fs,
  397. (struct ext2_inode_large *) inode,
  398. (struct ext2_inode_large *) scan->ptr,
  399. 0, bufsize);
  400. else
  401. #endif
  402. memcpy(inode, scan->ptr, bufsize);
  403. scan->ptr += scan->inode_size;
  404. scan->bytes_left -= scan->inode_size;
  405. if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
  406. retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
  407. }
  408. scan->inodes_left--;
  409. scan->current_inode++;
  410. *ino = scan->current_inode;
  411. return retval;
  412. }
  413. errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
  414. struct ext2_inode *inode)
  415. {
  416. return ext2fs_get_next_inode_full(scan, ino, inode,
  417. sizeof(struct ext2_inode));
  418. }
  419. /*
  420. * Functions to read and write a single inode.
  421. */
  422. errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
  423. struct ext2_inode * inode, int bufsize)
  424. {
  425. unsigned long group, block, block_nr, offset;
  426. char *ptr;
  427. errcode_t retval;
  428. int clen, i, inodes_per_block, length;
  429. io_channel io;
  430. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  431. /* Check to see if user has an override function */
  432. if (fs->read_inode) {
  433. retval = (fs->read_inode)(fs, ino, inode);
  434. if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
  435. return retval;
  436. }
  437. /* Create inode cache if not present */
  438. if (!fs->icache) {
  439. retval = create_icache(fs);
  440. if (retval)
  441. return retval;
  442. }
  443. /* Check to see if it's in the inode cache */
  444. if (bufsize == sizeof(struct ext2_inode)) {
  445. /* only old good inode can be retrieve from the cache */
  446. for (i=0; i < fs->icache->cache_size; i++) {
  447. if (fs->icache->cache[i].ino == ino) {
  448. *inode = fs->icache->cache[i].inode;
  449. return 0;
  450. }
  451. }
  452. }
  453. if ((ino == 0) || (ino > fs->super->s_inodes_count))
  454. return EXT2_ET_BAD_INODE_NUM;
  455. if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
  456. inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
  457. block_nr = fs->image_header->offset_inode / fs->blocksize;
  458. block_nr += (ino - 1) / inodes_per_block;
  459. offset = ((ino - 1) % inodes_per_block) *
  460. EXT2_INODE_SIZE(fs->super);
  461. io = fs->image_io;
  462. } else {
  463. group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
  464. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
  465. EXT2_INODE_SIZE(fs->super);
  466. block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
  467. if (!fs->group_desc[(unsigned)group].bg_inode_table)
  468. return EXT2_ET_MISSING_INODE_TABLE;
  469. block_nr = fs->group_desc[(unsigned)group].bg_inode_table +
  470. block;
  471. io = fs->io;
  472. }
  473. offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
  474. length = EXT2_INODE_SIZE(fs->super);
  475. if (bufsize < length)
  476. length = bufsize;
  477. ptr = (char *) inode;
  478. while (length) {
  479. clen = length;
  480. if ((offset + length) > fs->blocksize)
  481. clen = fs->blocksize - offset;
  482. if (block_nr != fs->icache->buffer_blk) {
  483. retval = io_channel_read_blk(io, block_nr, 1,
  484. fs->icache->buffer);
  485. if (retval)
  486. return retval;
  487. fs->icache->buffer_blk = block_nr;
  488. }
  489. memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
  490. clen);
  491. offset = 0;
  492. length -= clen;
  493. ptr += clen;
  494. block_nr++;
  495. }
  496. #if BB_BIG_ENDIAN
  497. if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  498. (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
  499. ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
  500. (struct ext2_inode_large *) inode,
  501. 0, length);
  502. #endif
  503. /* Update the inode cache */
  504. fs->icache->cache_last = (fs->icache->cache_last + 1) %
  505. fs->icache->cache_size;
  506. fs->icache->cache[fs->icache->cache_last].ino = ino;
  507. fs->icache->cache[fs->icache->cache_last].inode = *inode;
  508. return 0;
  509. }
  510. errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
  511. struct ext2_inode * inode)
  512. {
  513. return ext2fs_read_inode_full(fs, ino, inode,
  514. sizeof(struct ext2_inode));
  515. }
  516. errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
  517. struct ext2_inode * inode, int bufsize)
  518. {
  519. unsigned long group, block, block_nr, offset;
  520. errcode_t retval = 0;
  521. struct ext2_inode_large temp_inode, *w_inode;
  522. char *ptr;
  523. int clen, i, length;
  524. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  525. /* Check to see if user provided an override function */
  526. if (fs->write_inode) {
  527. retval = (fs->write_inode)(fs, ino, inode);
  528. if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
  529. return retval;
  530. }
  531. /* Check to see if the inode cache needs to be updated */
  532. if (fs->icache) {
  533. for (i=0; i < fs->icache->cache_size; i++) {
  534. if (fs->icache->cache[i].ino == ino) {
  535. fs->icache->cache[i].inode = *inode;
  536. break;
  537. }
  538. }
  539. } else {
  540. retval = create_icache(fs);
  541. if (retval)
  542. return retval;
  543. }
  544. if (!(fs->flags & EXT2_FLAG_RW))
  545. return EXT2_ET_RO_FILSYS;
  546. if ((ino == 0) || (ino > fs->super->s_inodes_count))
  547. return EXT2_ET_BAD_INODE_NUM;
  548. length = bufsize;
  549. if (length < EXT2_INODE_SIZE(fs->super))
  550. length = EXT2_INODE_SIZE(fs->super);
  551. if (length > (int) sizeof(struct ext2_inode_large)) {
  552. w_inode = xmalloc(length);
  553. } else
  554. w_inode = &temp_inode;
  555. memset(w_inode, 0, length);
  556. #if BB_BIG_ENDIAN
  557. if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  558. (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
  559. ext2fs_swap_inode_full(fs, w_inode,
  560. (struct ext2_inode_large *) inode,
  561. 1, bufsize);
  562. else
  563. #endif
  564. memcpy(w_inode, inode, bufsize);
  565. group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
  566. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
  567. EXT2_INODE_SIZE(fs->super);
  568. block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
  569. if (!fs->group_desc[(unsigned) group].bg_inode_table)
  570. return EXT2_ET_MISSING_INODE_TABLE;
  571. block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block;
  572. offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
  573. length = EXT2_INODE_SIZE(fs->super);
  574. if (length > bufsize)
  575. length = bufsize;
  576. ptr = (char *) w_inode;
  577. while (length) {
  578. clen = length;
  579. if ((offset + length) > fs->blocksize)
  580. clen = fs->blocksize - offset;
  581. if (fs->icache->buffer_blk != block_nr) {
  582. retval = io_channel_read_blk(fs->io, block_nr, 1,
  583. fs->icache->buffer);
  584. if (retval)
  585. goto errout;
  586. fs->icache->buffer_blk = block_nr;
  587. }
  588. memcpy((char *) fs->icache->buffer + (unsigned) offset,
  589. ptr, clen);
  590. retval = io_channel_write_blk(fs->io, block_nr, 1,
  591. fs->icache->buffer);
  592. if (retval)
  593. goto errout;
  594. offset = 0;
  595. ptr += clen;
  596. length -= clen;
  597. block_nr++;
  598. }
  599. fs->flags |= EXT2_FLAG_CHANGED;
  600. errout:
  601. if (w_inode && w_inode != &temp_inode)
  602. free(w_inode);
  603. return retval;
  604. }
  605. errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
  606. struct ext2_inode *inode)
  607. {
  608. return ext2fs_write_inode_full(fs, ino, inode,
  609. sizeof(struct ext2_inode));
  610. }
  611. /*
  612. * This function should be called when writing a new inode. It makes
  613. * sure that extra part of large inodes is initialized properly.
  614. */
  615. errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
  616. struct ext2_inode *inode)
  617. {
  618. struct ext2_inode *buf;
  619. int size = EXT2_INODE_SIZE(fs->super);
  620. struct ext2_inode_large *large_inode;
  621. if (size == sizeof(struct ext2_inode))
  622. return ext2fs_write_inode_full(fs, ino, inode,
  623. sizeof(struct ext2_inode));
  624. buf = xmalloc(size);
  625. memset(buf, 0, size);
  626. *buf = *inode;
  627. large_inode = (struct ext2_inode_large *) buf;
  628. large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
  629. EXT2_GOOD_OLD_INODE_SIZE;
  630. return ext2fs_write_inode_full(fs, ino, buf, size);
  631. }
  632. errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
  633. {
  634. struct ext2_inode inode;
  635. int i;
  636. errcode_t retval;
  637. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  638. if (ino > fs->super->s_inodes_count)
  639. return EXT2_ET_BAD_INODE_NUM;
  640. if (fs->get_blocks) {
  641. if (!(*fs->get_blocks)(fs, ino, blocks))
  642. return 0;
  643. }
  644. retval = ext2fs_read_inode(fs, ino, &inode);
  645. if (retval)
  646. return retval;
  647. for (i=0; i < EXT2_N_BLOCKS; i++)
  648. blocks[i] = inode.i_block[i];
  649. return 0;
  650. }
  651. errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
  652. {
  653. struct ext2_inode inode;
  654. errcode_t retval;
  655. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  656. if (ino > fs->super->s_inodes_count)
  657. return EXT2_ET_BAD_INODE_NUM;
  658. if (fs->check_directory) {
  659. retval = (fs->check_directory)(fs, ino);
  660. if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
  661. return retval;
  662. }
  663. retval = ext2fs_read_inode(fs, ino, &inode);
  664. if (retval)
  665. return retval;
  666. if (!LINUX_S_ISDIR(inode.i_mode))
  667. return EXT2_ET_NO_DIRECTORY;
  668. return 0;
  669. }