inode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. return;
  165. }
  166. void ext2fs_set_inode_callback(ext2_inode_scan scan,
  167. errcode_t (*done_group)(ext2_filsys fs,
  168. dgrp_t group,
  169. void * priv_data),
  170. void *done_group_data)
  171. {
  172. if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
  173. return;
  174. scan->done_group = done_group;
  175. scan->done_group_data = done_group_data;
  176. }
  177. int ext2fs_inode_scan_flags(ext2_inode_scan scan, int set_flags,
  178. int clear_flags)
  179. {
  180. int old_flags;
  181. if (!scan || (scan->magic != EXT2_ET_MAGIC_INODE_SCAN))
  182. return 0;
  183. old_flags = scan->scan_flags;
  184. scan->scan_flags &= ~clear_flags;
  185. scan->scan_flags |= set_flags;
  186. return old_flags;
  187. }
  188. /*
  189. * This function is called by ext2fs_get_next_inode when it needs to
  190. * get ready to read in a new blockgroup.
  191. */
  192. static errcode_t get_next_blockgroup(ext2_inode_scan scan)
  193. {
  194. scan->current_group++;
  195. scan->groups_left--;
  196. scan->current_block = scan->fs->
  197. group_desc[scan->current_group].bg_inode_table;
  198. scan->current_inode = scan->current_group *
  199. EXT2_INODES_PER_GROUP(scan->fs->super);
  200. scan->bytes_left = 0;
  201. scan->inodes_left = EXT2_INODES_PER_GROUP(scan->fs->super);
  202. scan->blocks_left = scan->fs->inode_blocks_per_group;
  203. return 0;
  204. }
  205. errcode_t ext2fs_inode_scan_goto_blockgroup(ext2_inode_scan scan,
  206. int group)
  207. {
  208. scan->current_group = group - 1;
  209. scan->groups_left = scan->fs->group_desc_count - group;
  210. return get_next_blockgroup(scan);
  211. }
  212. /*
  213. * This function is called by get_next_blocks() to check for bad
  214. * blocks in the inode table.
  215. *
  216. * This function assumes that badblocks_list->list is sorted in
  217. * increasing order.
  218. */
  219. static errcode_t check_for_inode_bad_blocks(ext2_inode_scan scan,
  220. blk_t *num_blocks)
  221. {
  222. blk_t blk = scan->current_block;
  223. badblocks_list bb = scan->fs->badblocks;
  224. /*
  225. * If the inode table is missing, then obviously there are no
  226. * bad blocks. :-)
  227. */
  228. if (blk == 0)
  229. return 0;
  230. /*
  231. * If the current block is greater than the bad block listed
  232. * in the bad block list, then advance the pointer until this
  233. * is no longer the case. If we run out of bad blocks, then
  234. * we don't need to do any more checking!
  235. */
  236. while (blk > bb->list[scan->bad_block_ptr]) {
  237. if (++scan->bad_block_ptr >= bb->num) {
  238. scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
  239. return 0;
  240. }
  241. }
  242. /*
  243. * If the current block is equal to the bad block listed in
  244. * the bad block list, then handle that one block specially.
  245. * (We could try to handle runs of bad blocks, but that
  246. * only increases CPU efficiency by a small amount, at the
  247. * expense of a huge expense of code complexity, and for an
  248. * uncommon case at that.)
  249. */
  250. if (blk == bb->list[scan->bad_block_ptr]) {
  251. scan->scan_flags |= EXT2_SF_BAD_INODE_BLK;
  252. *num_blocks = 1;
  253. if (++scan->bad_block_ptr >= bb->num)
  254. scan->scan_flags &= ~EXT2_SF_CHK_BADBLOCKS;
  255. return 0;
  256. }
  257. /*
  258. * If there is a bad block in the range that we're about to
  259. * read in, adjust the number of blocks to read so that we we
  260. * don't read in the bad block. (Then the next block to read
  261. * will be the bad block, which is handled in the above case.)
  262. */
  263. if ((blk + *num_blocks) > bb->list[scan->bad_block_ptr])
  264. *num_blocks = (int) (bb->list[scan->bad_block_ptr] - blk);
  265. return 0;
  266. }
  267. /*
  268. * This function is called by ext2fs_get_next_inode when it needs to
  269. * read in more blocks from the current blockgroup's inode table.
  270. */
  271. static errcode_t get_next_blocks(ext2_inode_scan scan)
  272. {
  273. blk_t num_blocks;
  274. errcode_t retval;
  275. /*
  276. * Figure out how many blocks to read; we read at most
  277. * inode_buffer_blocks, and perhaps less if there aren't that
  278. * many blocks left to read.
  279. */
  280. num_blocks = scan->inode_buffer_blocks;
  281. if (num_blocks > scan->blocks_left)
  282. num_blocks = scan->blocks_left;
  283. /*
  284. * If the past block "read" was a bad block, then mark the
  285. * left-over extra bytes as also being bad.
  286. */
  287. if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK) {
  288. if (scan->bytes_left)
  289. scan->scan_flags |= EXT2_SF_BAD_EXTRA_BYTES;
  290. scan->scan_flags &= ~EXT2_SF_BAD_INODE_BLK;
  291. }
  292. /*
  293. * Do inode bad block processing, if necessary.
  294. */
  295. if (scan->scan_flags & EXT2_SF_CHK_BADBLOCKS) {
  296. retval = check_for_inode_bad_blocks(scan, &num_blocks);
  297. if (retval)
  298. return retval;
  299. }
  300. if ((scan->scan_flags & EXT2_SF_BAD_INODE_BLK) ||
  301. (scan->current_block == 0)) {
  302. memset(scan->inode_buffer, 0,
  303. (size_t) num_blocks * scan->fs->blocksize);
  304. } else {
  305. retval = io_channel_read_blk(scan->fs->io,
  306. scan->current_block,
  307. (int) num_blocks,
  308. scan->inode_buffer);
  309. if (retval)
  310. return EXT2_ET_NEXT_INODE_READ;
  311. }
  312. scan->ptr = scan->inode_buffer;
  313. scan->bytes_left = num_blocks * scan->fs->blocksize;
  314. scan->blocks_left -= num_blocks;
  315. if (scan->current_block)
  316. scan->current_block += num_blocks;
  317. return 0;
  318. }
  319. errcode_t ext2fs_get_next_inode_full(ext2_inode_scan scan, ext2_ino_t *ino,
  320. struct ext2_inode *inode, int bufsize)
  321. {
  322. errcode_t retval;
  323. int extra_bytes = 0;
  324. EXT2_CHECK_MAGIC(scan, EXT2_ET_MAGIC_INODE_SCAN);
  325. /*
  326. * Do we need to start reading a new block group?
  327. */
  328. if (scan->inodes_left <= 0) {
  329. force_new_group:
  330. if (scan->done_group) {
  331. retval = (scan->done_group)
  332. (scan->fs, scan->current_group,
  333. scan->done_group_data);
  334. if (retval)
  335. return retval;
  336. }
  337. if (scan->groups_left <= 0) {
  338. *ino = 0;
  339. return 0;
  340. }
  341. retval = get_next_blockgroup(scan);
  342. if (retval)
  343. return retval;
  344. }
  345. /*
  346. * This is done outside the above if statement so that the
  347. * check can be done for block group #0.
  348. */
  349. if (scan->current_block == 0) {
  350. if (scan->scan_flags & EXT2_SF_SKIP_MISSING_ITABLE) {
  351. goto force_new_group;
  352. } else
  353. return EXT2_ET_MISSING_INODE_TABLE;
  354. }
  355. /*
  356. * Have we run out of space in the inode buffer? If so, we
  357. * need to read in more blocks.
  358. */
  359. if (scan->bytes_left < scan->inode_size) {
  360. memcpy(scan->temp_buffer, scan->ptr, scan->bytes_left);
  361. extra_bytes = scan->bytes_left;
  362. retval = get_next_blocks(scan);
  363. if (retval)
  364. return retval;
  365. #if 0
  366. /*
  367. * XXX test Need check for used inode somehow.
  368. * (Note: this is hard.)
  369. */
  370. if (is_empty_scan(scan))
  371. goto force_new_group;
  372. #endif
  373. }
  374. retval = 0;
  375. if (extra_bytes) {
  376. memcpy(scan->temp_buffer+extra_bytes, scan->ptr,
  377. scan->inode_size - extra_bytes);
  378. scan->ptr += scan->inode_size - extra_bytes;
  379. scan->bytes_left -= scan->inode_size - extra_bytes;
  380. #if BB_BIG_ENDIAN
  381. if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  382. (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
  383. ext2fs_swap_inode_full(scan->fs,
  384. (struct ext2_inode_large *) inode,
  385. (struct ext2_inode_large *) scan->temp_buffer,
  386. 0, bufsize);
  387. else
  388. #endif
  389. *inode = *((struct ext2_inode *) scan->temp_buffer);
  390. if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
  391. retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
  392. scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
  393. } else {
  394. #if BB_BIG_ENDIAN
  395. if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  396. (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
  397. ext2fs_swap_inode_full(scan->fs,
  398. (struct ext2_inode_large *) inode,
  399. (struct ext2_inode_large *) scan->ptr,
  400. 0, bufsize);
  401. else
  402. #endif
  403. memcpy(inode, scan->ptr, bufsize);
  404. scan->ptr += scan->inode_size;
  405. scan->bytes_left -= scan->inode_size;
  406. if (scan->scan_flags & EXT2_SF_BAD_INODE_BLK)
  407. retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
  408. }
  409. scan->inodes_left--;
  410. scan->current_inode++;
  411. *ino = scan->current_inode;
  412. return retval;
  413. }
  414. errcode_t ext2fs_get_next_inode(ext2_inode_scan scan, ext2_ino_t *ino,
  415. struct ext2_inode *inode)
  416. {
  417. return ext2fs_get_next_inode_full(scan, ino, inode,
  418. sizeof(struct ext2_inode));
  419. }
  420. /*
  421. * Functions to read and write a single inode.
  422. */
  423. errcode_t ext2fs_read_inode_full(ext2_filsys fs, ext2_ino_t ino,
  424. struct ext2_inode * inode, int bufsize)
  425. {
  426. unsigned long group, block, block_nr, offset;
  427. char *ptr;
  428. errcode_t retval;
  429. int clen, i, inodes_per_block, length;
  430. io_channel io;
  431. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  432. /* Check to see if user has an override function */
  433. if (fs->read_inode) {
  434. retval = (fs->read_inode)(fs, ino, inode);
  435. if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
  436. return retval;
  437. }
  438. /* Create inode cache if not present */
  439. if (!fs->icache) {
  440. retval = create_icache(fs);
  441. if (retval)
  442. return retval;
  443. }
  444. /* Check to see if it's in the inode cache */
  445. if (bufsize == sizeof(struct ext2_inode)) {
  446. /* only old good inode can be retrieve from the cache */
  447. for (i=0; i < fs->icache->cache_size; i++) {
  448. if (fs->icache->cache[i].ino == ino) {
  449. *inode = fs->icache->cache[i].inode;
  450. return 0;
  451. }
  452. }
  453. }
  454. if ((ino == 0) || (ino > fs->super->s_inodes_count))
  455. return EXT2_ET_BAD_INODE_NUM;
  456. if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
  457. inodes_per_block = fs->blocksize / EXT2_INODE_SIZE(fs->super);
  458. block_nr = fs->image_header->offset_inode / fs->blocksize;
  459. block_nr += (ino - 1) / inodes_per_block;
  460. offset = ((ino - 1) % inodes_per_block) *
  461. EXT2_INODE_SIZE(fs->super);
  462. io = fs->image_io;
  463. } else {
  464. group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
  465. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
  466. EXT2_INODE_SIZE(fs->super);
  467. block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
  468. if (!fs->group_desc[(unsigned)group].bg_inode_table)
  469. return EXT2_ET_MISSING_INODE_TABLE;
  470. block_nr = fs->group_desc[(unsigned)group].bg_inode_table +
  471. block;
  472. io = fs->io;
  473. }
  474. offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
  475. length = EXT2_INODE_SIZE(fs->super);
  476. if (bufsize < length)
  477. length = bufsize;
  478. ptr = (char *) inode;
  479. while (length) {
  480. clen = length;
  481. if ((offset + length) > fs->blocksize)
  482. clen = fs->blocksize - offset;
  483. if (block_nr != fs->icache->buffer_blk) {
  484. retval = io_channel_read_blk(io, block_nr, 1,
  485. fs->icache->buffer);
  486. if (retval)
  487. return retval;
  488. fs->icache->buffer_blk = block_nr;
  489. }
  490. memcpy(ptr, ((char *) fs->icache->buffer) + (unsigned) offset,
  491. clen);
  492. offset = 0;
  493. length -= clen;
  494. ptr += clen;
  495. block_nr++;
  496. }
  497. #if BB_BIG_ENDIAN
  498. if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  499. (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
  500. ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) inode,
  501. (struct ext2_inode_large *) inode,
  502. 0, length);
  503. #endif
  504. /* Update the inode cache */
  505. fs->icache->cache_last = (fs->icache->cache_last + 1) %
  506. fs->icache->cache_size;
  507. fs->icache->cache[fs->icache->cache_last].ino = ino;
  508. fs->icache->cache[fs->icache->cache_last].inode = *inode;
  509. return 0;
  510. }
  511. errcode_t ext2fs_read_inode(ext2_filsys fs, ext2_ino_t ino,
  512. struct ext2_inode * inode)
  513. {
  514. return ext2fs_read_inode_full(fs, ino, inode,
  515. sizeof(struct ext2_inode));
  516. }
  517. errcode_t ext2fs_write_inode_full(ext2_filsys fs, ext2_ino_t ino,
  518. struct ext2_inode * inode, int bufsize)
  519. {
  520. unsigned long group, block, block_nr, offset;
  521. errcode_t retval = 0;
  522. struct ext2_inode_large temp_inode, *w_inode;
  523. char *ptr;
  524. int clen, i, length;
  525. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  526. /* Check to see if user provided an override function */
  527. if (fs->write_inode) {
  528. retval = (fs->write_inode)(fs, ino, inode);
  529. if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
  530. return retval;
  531. }
  532. /* Check to see if the inode cache needs to be updated */
  533. if (fs->icache) {
  534. for (i=0; i < fs->icache->cache_size; i++) {
  535. if (fs->icache->cache[i].ino == ino) {
  536. fs->icache->cache[i].inode = *inode;
  537. break;
  538. }
  539. }
  540. } else {
  541. retval = create_icache(fs);
  542. if (retval)
  543. return retval;
  544. }
  545. if (!(fs->flags & EXT2_FLAG_RW))
  546. return EXT2_ET_RO_FILSYS;
  547. if ((ino == 0) || (ino > fs->super->s_inodes_count))
  548. return EXT2_ET_BAD_INODE_NUM;
  549. length = bufsize;
  550. if (length < EXT2_INODE_SIZE(fs->super))
  551. length = EXT2_INODE_SIZE(fs->super);
  552. if (length > (int) sizeof(struct ext2_inode_large)) {
  553. w_inode = xmalloc(length);
  554. } else
  555. w_inode = &temp_inode;
  556. memset(w_inode, 0, length);
  557. #if BB_BIG_ENDIAN
  558. if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  559. (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
  560. ext2fs_swap_inode_full(fs, w_inode,
  561. (struct ext2_inode_large *) inode,
  562. 1, bufsize);
  563. else
  564. #endif
  565. memcpy(w_inode, inode, bufsize);
  566. group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
  567. offset = ((ino - 1) % EXT2_INODES_PER_GROUP(fs->super)) *
  568. EXT2_INODE_SIZE(fs->super);
  569. block = offset >> EXT2_BLOCK_SIZE_BITS(fs->super);
  570. if (!fs->group_desc[(unsigned) group].bg_inode_table)
  571. return EXT2_ET_MISSING_INODE_TABLE;
  572. block_nr = fs->group_desc[(unsigned) group].bg_inode_table + block;
  573. offset &= (EXT2_BLOCK_SIZE(fs->super) - 1);
  574. length = EXT2_INODE_SIZE(fs->super);
  575. if (length > bufsize)
  576. length = bufsize;
  577. ptr = (char *) w_inode;
  578. while (length) {
  579. clen = length;
  580. if ((offset + length) > fs->blocksize)
  581. clen = fs->blocksize - offset;
  582. if (fs->icache->buffer_blk != block_nr) {
  583. retval = io_channel_read_blk(fs->io, block_nr, 1,
  584. fs->icache->buffer);
  585. if (retval)
  586. goto errout;
  587. fs->icache->buffer_blk = block_nr;
  588. }
  589. memcpy((char *) fs->icache->buffer + (unsigned) offset,
  590. ptr, clen);
  591. retval = io_channel_write_blk(fs->io, block_nr, 1,
  592. fs->icache->buffer);
  593. if (retval)
  594. goto errout;
  595. offset = 0;
  596. ptr += clen;
  597. length -= clen;
  598. block_nr++;
  599. }
  600. fs->flags |= EXT2_FLAG_CHANGED;
  601. errout:
  602. if (w_inode && w_inode != &temp_inode)
  603. free(w_inode);
  604. return retval;
  605. }
  606. errcode_t ext2fs_write_inode(ext2_filsys fs, ext2_ino_t ino,
  607. struct ext2_inode *inode)
  608. {
  609. return ext2fs_write_inode_full(fs, ino, inode,
  610. sizeof(struct ext2_inode));
  611. }
  612. /*
  613. * This function should be called when writing a new inode. It makes
  614. * sure that extra part of large inodes is initialized properly.
  615. */
  616. errcode_t ext2fs_write_new_inode(ext2_filsys fs, ext2_ino_t ino,
  617. struct ext2_inode *inode)
  618. {
  619. struct ext2_inode *buf;
  620. int size = EXT2_INODE_SIZE(fs->super);
  621. struct ext2_inode_large *large_inode;
  622. if (size == sizeof(struct ext2_inode))
  623. return ext2fs_write_inode_full(fs, ino, inode,
  624. sizeof(struct ext2_inode));
  625. buf = xmalloc(size);
  626. memset(buf, 0, size);
  627. *buf = *inode;
  628. large_inode = (struct ext2_inode_large *) buf;
  629. large_inode->i_extra_isize = sizeof(struct ext2_inode_large) -
  630. EXT2_GOOD_OLD_INODE_SIZE;
  631. return ext2fs_write_inode_full(fs, ino, buf, size);
  632. }
  633. errcode_t ext2fs_get_blocks(ext2_filsys fs, ext2_ino_t ino, blk_t *blocks)
  634. {
  635. struct ext2_inode inode;
  636. int i;
  637. errcode_t retval;
  638. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  639. if (ino > fs->super->s_inodes_count)
  640. return EXT2_ET_BAD_INODE_NUM;
  641. if (fs->get_blocks) {
  642. if (!(*fs->get_blocks)(fs, ino, blocks))
  643. return 0;
  644. }
  645. retval = ext2fs_read_inode(fs, ino, &inode);
  646. if (retval)
  647. return retval;
  648. for (i=0; i < EXT2_N_BLOCKS; i++)
  649. blocks[i] = inode.i_block[i];
  650. return 0;
  651. }
  652. errcode_t ext2fs_check_directory(ext2_filsys fs, ext2_ino_t ino)
  653. {
  654. struct ext2_inode inode;
  655. errcode_t retval;
  656. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  657. if (ino > fs->super->s_inodes_count)
  658. return EXT2_ET_BAD_INODE_NUM;
  659. if (fs->check_directory) {
  660. retval = (fs->check_directory)(fs, ino);
  661. if (retval != EXT2_ET_CALLBACK_NOTHANDLED)
  662. return retval;
  663. }
  664. retval = ext2fs_read_inode(fs, ino, &inode);
  665. if (retval)
  666. return retval;
  667. if (!LINUX_S_ISDIR(inode.i_mode))
  668. return EXT2_ET_NO_DIRECTORY;
  669. return 0;
  670. }