xfs_dir2_sf.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef __XFS_DIR2_SF_H__
  19. #define __XFS_DIR2_SF_H__
  20. /*
  21. * Directory layout when stored internal to an inode.
  22. *
  23. * Small directories are packed as tightly as possible so as to fit into the
  24. * literal area of the inode. They consist of a single xfs_dir2_sf_hdr header
  25. * followed by zero or more xfs_dir2_sf_entry structures. Due the different
  26. * inode number storage size and the variable length name field in
  27. * the xfs_dir2_sf_entry all these structure are variable length, and the
  28. * accessors in this file should be used to iterate over them.
  29. */
  30. struct uio;
  31. struct xfs_dabuf;
  32. struct xfs_da_args;
  33. struct xfs_dir2_data_hdr;
  34. struct xfs_inode;
  35. struct xfs_mount;
  36. struct xfs_trans;
  37. /*
  38. * Inode number stored as 8 8-bit values.
  39. */
  40. typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
  41. /*
  42. * Inode number stored as 4 8-bit values.
  43. * Works a lot of the time, when all the inode numbers in a directory
  44. * fit in 32 bits.
  45. */
  46. typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
  47. typedef union {
  48. xfs_dir2_ino8_t i8;
  49. xfs_dir2_ino4_t i4;
  50. } xfs_dir2_inou_t;
  51. #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
  52. /*
  53. * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
  54. * Only need 16 bits, this is the byte offset into the single block form.
  55. */
  56. typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
  57. /*
  58. * The parent directory has a dedicated field, and the self-pointer must
  59. * be calculated on the fly.
  60. *
  61. * Entries are packed toward the top as tightly as possible, and thus may
  62. * be misaligned. Care needs to be taken to access them through special
  63. * helpers or copy them into aligned variables first.
  64. */
  65. typedef struct xfs_dir2_sf_hdr {
  66. __uint8_t count; /* count of entries */
  67. __uint8_t i8count; /* count of 8-byte inode #s */
  68. xfs_dir2_inou_t parent; /* parent dir inode number */
  69. } __arch_pack xfs_dir2_sf_hdr_t;
  70. typedef struct xfs_dir2_sf_entry {
  71. __u8 namelen; /* actual name length */
  72. xfs_dir2_sf_off_t offset; /* saved offset */
  73. __u8 name[]; /* name, variable size */
  74. /*
  75. * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
  76. * variable offset after the name.
  77. */
  78. } __arch_pack xfs_dir2_sf_entry_t;
  79. static inline int xfs_dir2_sf_hdr_size(int i8count)
  80. {
  81. return ((uint)sizeof(xfs_dir2_sf_hdr_t) - \
  82. ((i8count) == 0) * \
  83. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
  84. }
  85. static inline xfs_dir2_data_aoff_t
  86. xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
  87. {
  88. return INT_GET_UNALIGNED_16_BE(&(sfep)->offset.i);
  89. }
  90. static inline void
  91. xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
  92. {
  93. INT_SET_UNALIGNED_16_BE(&(sfep)->offset.i, off);
  94. }
  95. static inline int
  96. xfs_dir2_sf_entsize(struct xfs_dir2_sf_hdr *hdr, int len)
  97. {
  98. return sizeof(struct xfs_dir2_sf_entry) + /* namelen + offset */
  99. len + /* name */
  100. (hdr->i8count ? /* ino */
  101. sizeof(xfs_dir2_ino8_t) :
  102. sizeof(xfs_dir2_ino4_t));
  103. }
  104. static inline struct xfs_dir2_sf_entry *
  105. xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
  106. {
  107. return (struct xfs_dir2_sf_entry *)
  108. ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
  109. }
  110. static inline struct xfs_dir2_sf_entry *
  111. xfs_dir2_sf_nextentry(struct xfs_dir2_sf_hdr *hdr,
  112. struct xfs_dir2_sf_entry *sfep)
  113. {
  114. return (struct xfs_dir2_sf_entry *)
  115. ((char *)sfep + xfs_dir2_sf_entsize(hdr, sfep->namelen));
  116. }
  117. /*
  118. * Functions.
  119. */
  120. extern xfs_ino_t xfs_dir2_sf_get_parent_ino(struct xfs_dir2_sf_hdr *sfp);
  121. extern xfs_ino_t xfs_dir2_sfe_get_ino(struct xfs_dir2_sf_hdr *sfp,
  122. struct xfs_dir2_sf_entry *sfep);
  123. extern int xfs_dir2_block_sfsize(struct xfs_inode *dp,
  124. struct xfs_dir2_data_hdr *block,
  125. xfs_dir2_sf_hdr_t *sfhp);
  126. extern int xfs_dir2_block_to_sf(struct xfs_da_args *args, struct xfs_dabuf *bp,
  127. int size, xfs_dir2_sf_hdr_t *sfhp);
  128. extern int xfs_dir2_sf_addname(struct xfs_da_args *args);
  129. extern int xfs_dir2_sf_create(struct xfs_da_args *args, xfs_ino_t pino);
  130. extern int xfs_dir2_sf_getdents(struct xfs_inode *dp, void *dirent,
  131. xfs_off_t *offset, filldir_t filldir);
  132. extern int xfs_dir2_sf_lookup(struct xfs_da_args *args);
  133. extern int xfs_dir2_sf_removename(struct xfs_da_args *args);
  134. extern int xfs_dir2_sf_replace(struct xfs_da_args *args);
  135. #endif /* __XFS_DIR2_SF_H__ */