ext_attr.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ext_attr.c --- extended attribute blocks
  4. *
  5. * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
  6. *
  7. * Copyright (C) 2002 Theodore Ts'o.
  8. *
  9. * %Begin-Header%
  10. * This file may be redistributed under the terms of the GNU Public
  11. * License.
  12. * %End-Header%
  13. */
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include "ext2_fs.h"
  19. #include "ext2_ext_attr.h"
  20. #include "ext2fs.h"
  21. errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
  22. {
  23. errcode_t retval;
  24. retval = io_channel_read_blk(fs->io, block, 1, buf);
  25. if (retval)
  26. return retval;
  27. #if BB_BIG_ENDIAN
  28. if ((fs->flags & (EXT2_FLAG_SWAP_BYTES|
  29. EXT2_FLAG_SWAP_BYTES_READ)) != 0)
  30. ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
  31. #endif
  32. return 0;
  33. }
  34. errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
  35. {
  36. errcode_t retval;
  37. char *write_buf;
  38. char *buf = NULL;
  39. if (BB_BIG_ENDIAN && ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  40. (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))) {
  41. retval = ext2fs_get_mem(fs->blocksize, &buf);
  42. if (retval)
  43. return retval;
  44. write_buf = buf;
  45. ext2fs_swap_ext_attr(buf, inbuf, fs->blocksize, 1);
  46. } else
  47. write_buf = (char *) inbuf;
  48. retval = io_channel_write_blk(fs->io, block, 1, write_buf);
  49. if (buf)
  50. ext2fs_free_mem(&buf);
  51. if (!retval)
  52. ext2fs_mark_changed(fs);
  53. return retval;
  54. }
  55. /*
  56. * This function adjusts the reference count of the EA block.
  57. */
  58. errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
  59. char *block_buf, int adjust,
  60. __u32 *newcount)
  61. {
  62. errcode_t retval;
  63. struct ext2_ext_attr_header *header;
  64. char *buf = 0;
  65. if ((blk >= fs->super->s_blocks_count) ||
  66. (blk < fs->super->s_first_data_block))
  67. return EXT2_ET_BAD_EA_BLOCK_NUM;
  68. if (!block_buf) {
  69. retval = ext2fs_get_mem(fs->blocksize, &buf);
  70. if (retval)
  71. return retval;
  72. block_buf = buf;
  73. }
  74. retval = ext2fs_read_ext_attr(fs, blk, block_buf);
  75. if (retval)
  76. goto errout;
  77. header = (struct ext2_ext_attr_header *) block_buf;
  78. header->h_refcount += adjust;
  79. if (newcount)
  80. *newcount = header->h_refcount;
  81. retval = ext2fs_write_ext_attr(fs, blk, block_buf);
  82. if (retval)
  83. goto errout;
  84. errout:
  85. if (buf)
  86. ext2fs_free_mem(&buf);
  87. return retval;
  88. }