expanddir.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * expand.c --- expand an ext2fs directory
  4. *
  5. * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 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. #include "ext2_fs.h"
  18. #include "ext2fs.h"
  19. struct expand_dir_struct {
  20. int done;
  21. int newblocks;
  22. errcode_t err;
  23. };
  24. static int expand_dir_proc(ext2_filsys fs,
  25. blk_t *blocknr,
  26. e2_blkcnt_t blockcnt,
  27. blk_t ref_block EXT2FS_ATTR((unused)),
  28. int ref_offset EXT2FS_ATTR((unused)),
  29. void *priv_data)
  30. {
  31. struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
  32. blk_t new_blk;
  33. static blk_t last_blk = 0;
  34. char *block;
  35. errcode_t retval;
  36. if (*blocknr) {
  37. last_blk = *blocknr;
  38. return 0;
  39. }
  40. retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
  41. if (retval) {
  42. es->err = retval;
  43. return BLOCK_ABORT;
  44. }
  45. if (blockcnt > 0) {
  46. retval = ext2fs_new_dir_block(fs, 0, 0, &block);
  47. if (retval) {
  48. es->err = retval;
  49. return BLOCK_ABORT;
  50. }
  51. es->done = 1;
  52. retval = ext2fs_write_dir_block(fs, new_blk, block);
  53. } else {
  54. retval = ext2fs_get_mem(fs->blocksize, &block);
  55. if (retval) {
  56. es->err = retval;
  57. return BLOCK_ABORT;
  58. }
  59. memset(block, 0, fs->blocksize);
  60. retval = io_channel_write_blk(fs->io, new_blk, 1, block);
  61. }
  62. if (retval) {
  63. es->err = retval;
  64. return BLOCK_ABORT;
  65. }
  66. ext2fs_free_mem(&block);
  67. *blocknr = new_blk;
  68. ext2fs_block_alloc_stats(fs, new_blk, +1);
  69. es->newblocks++;
  70. if (es->done)
  71. return (BLOCK_CHANGED | BLOCK_ABORT);
  72. else
  73. return BLOCK_CHANGED;
  74. }
  75. errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir)
  76. {
  77. errcode_t retval;
  78. struct expand_dir_struct es;
  79. struct ext2_inode inode;
  80. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  81. if (!(fs->flags & EXT2_FLAG_RW))
  82. return EXT2_ET_RO_FILSYS;
  83. if (!fs->block_map)
  84. return EXT2_ET_NO_BLOCK_BITMAP;
  85. retval = ext2fs_check_directory(fs, dir);
  86. if (retval)
  87. return retval;
  88. es.done = 0;
  89. es.err = 0;
  90. es.newblocks = 0;
  91. retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
  92. 0, expand_dir_proc, &es);
  93. if (es.err)
  94. return es.err;
  95. if (!es.done)
  96. return EXT2_ET_EXPAND_DIR_ERR;
  97. /*
  98. * Update the size and block count fields in the inode.
  99. */
  100. retval = ext2fs_read_inode(fs, dir, &inode);
  101. if (retval)
  102. return retval;
  103. inode.i_size += fs->blocksize;
  104. inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
  105. retval = ext2fs_write_inode(fs, dir, &inode);
  106. if (retval)
  107. return retval;
  108. return 0;
  109. }