xfs_dir2_sf.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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
  24. * fit into the literal area of the inode.
  25. */
  26. struct uio;
  27. struct xfs_dabuf;
  28. struct xfs_da_args;
  29. struct xfs_dir2_block;
  30. struct xfs_inode;
  31. struct xfs_mount;
  32. struct xfs_trans;
  33. /*
  34. * Maximum size of a shortform directory.
  35. */
  36. #define XFS_DIR2_SF_MAX_SIZE \
  37. (XFS_DINODE_MAX_SIZE - (uint)sizeof(xfs_dinode_core_t) - \
  38. (uint)sizeof(xfs_agino_t))
  39. /*
  40. * Inode number stored as 8 8-bit values.
  41. */
  42. typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
  43. /*
  44. * Inode number stored as 4 8-bit values.
  45. * Works a lot of the time, when all the inode numbers in a directory
  46. * fit in 32 bits.
  47. */
  48. typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
  49. typedef union {
  50. xfs_dir2_ino8_t i8;
  51. xfs_dir2_ino4_t i4;
  52. } xfs_dir2_inou_t;
  53. #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
  54. /*
  55. * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
  56. * Only need 16 bits, this is the byte offset into the single block form.
  57. */
  58. typedef struct { __uint8_t i[2]; } xfs_dir2_sf_off_t;
  59. /*
  60. * The parent directory has a dedicated field, and the self-pointer must
  61. * be calculated on the fly.
  62. *
  63. * Entries are packed toward the top as tightly as possible. The header
  64. * and the elements must be memcpy'd out into a work area to get correct
  65. * alignment for the inode number fields.
  66. */
  67. typedef struct xfs_dir2_sf_hdr {
  68. __uint8_t count; /* count of entries */
  69. __uint8_t i8count; /* count of 8-byte inode #s */
  70. xfs_dir2_inou_t parent; /* parent dir inode number */
  71. } xfs_dir2_sf_hdr_t;
  72. typedef struct xfs_dir2_sf_entry {
  73. __uint8_t namelen; /* actual name length */
  74. xfs_dir2_sf_off_t offset; /* saved offset */
  75. __uint8_t name[1]; /* name, variable size */
  76. xfs_dir2_inou_t inumber; /* inode number, var. offset */
  77. } xfs_dir2_sf_entry_t;
  78. typedef struct xfs_dir2_sf {
  79. xfs_dir2_sf_hdr_t hdr; /* shortform header */
  80. xfs_dir2_sf_entry_t list[1]; /* shortform entries */
  81. } xfs_dir2_sf_t;
  82. #define XFS_DIR2_SF_HDR_SIZE(i8count) xfs_dir2_sf_hdr_size(i8count)
  83. static inline int xfs_dir2_sf_hdr_size(int i8count)
  84. {
  85. return ((uint)sizeof(xfs_dir2_sf_hdr_t) - \
  86. ((i8count) == 0) * \
  87. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
  88. }
  89. #define XFS_DIR2_SF_INUMBERP(sfep) xfs_dir2_sf_inumberp(sfep)
  90. static inline xfs_dir2_inou_t *xfs_dir2_sf_inumberp(xfs_dir2_sf_entry_t *sfep)
  91. {
  92. return (xfs_dir2_inou_t *)&(sfep)->name[(sfep)->namelen];
  93. }
  94. #define XFS_DIR2_SF_GET_INUMBER(sfp, from) \
  95. xfs_dir2_sf_get_inumber(sfp, from)
  96. static inline xfs_intino_t
  97. xfs_dir2_sf_get_inumber(xfs_dir2_sf_t *sfp, xfs_dir2_inou_t *from)
  98. {
  99. return ((sfp)->hdr.i8count == 0 ? \
  100. (xfs_intino_t)XFS_GET_DIR_INO4((from)->i4) : \
  101. (xfs_intino_t)XFS_GET_DIR_INO8((from)->i8));
  102. }
  103. #define XFS_DIR2_SF_PUT_INUMBER(sfp,from,to) \
  104. xfs_dir2_sf_put_inumber(sfp,from,to)
  105. static inline void xfs_dir2_sf_put_inumber(xfs_dir2_sf_t *sfp, xfs_ino_t *from,
  106. xfs_dir2_inou_t *to)
  107. {
  108. if ((sfp)->hdr.i8count == 0)
  109. XFS_PUT_DIR_INO4(*(from), (to)->i4);
  110. else
  111. XFS_PUT_DIR_INO8(*(from), (to)->i8);
  112. }
  113. #define XFS_DIR2_SF_GET_OFFSET(sfep) \
  114. xfs_dir2_sf_get_offset(sfep)
  115. static inline xfs_dir2_data_aoff_t
  116. xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
  117. {
  118. return INT_GET_UNALIGNED_16_BE(&(sfep)->offset.i);
  119. }
  120. #define XFS_DIR2_SF_PUT_OFFSET(sfep,off) \
  121. xfs_dir2_sf_put_offset(sfep,off)
  122. static inline void
  123. xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
  124. {
  125. INT_SET_UNALIGNED_16_BE(&(sfep)->offset.i, off);
  126. }
  127. #define XFS_DIR2_SF_ENTSIZE_BYNAME(sfp,len) \
  128. xfs_dir2_sf_entsize_byname(sfp,len)
  129. static inline int xfs_dir2_sf_entsize_byname(xfs_dir2_sf_t *sfp, int len)
  130. {
  131. return ((uint)sizeof(xfs_dir2_sf_entry_t) - 1 + (len) - \
  132. ((sfp)->hdr.i8count == 0) * \
  133. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
  134. }
  135. #define XFS_DIR2_SF_ENTSIZE_BYENTRY(sfp,sfep) \
  136. xfs_dir2_sf_entsize_byentry(sfp,sfep)
  137. static inline int
  138. xfs_dir2_sf_entsize_byentry(xfs_dir2_sf_t *sfp, xfs_dir2_sf_entry_t *sfep)
  139. {
  140. return ((uint)sizeof(xfs_dir2_sf_entry_t) - 1 + (sfep)->namelen - \
  141. ((sfp)->hdr.i8count == 0) * \
  142. ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t)));
  143. }
  144. #define XFS_DIR2_SF_FIRSTENTRY(sfp) xfs_dir2_sf_firstentry(sfp)
  145. static inline xfs_dir2_sf_entry_t *xfs_dir2_sf_firstentry(xfs_dir2_sf_t *sfp)
  146. {
  147. return ((xfs_dir2_sf_entry_t *) \
  148. ((char *)(sfp) + XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count)));
  149. }
  150. #define XFS_DIR2_SF_NEXTENTRY(sfp,sfep) xfs_dir2_sf_nextentry(sfp,sfep)
  151. static inline xfs_dir2_sf_entry_t *
  152. xfs_dir2_sf_nextentry(xfs_dir2_sf_t *sfp, xfs_dir2_sf_entry_t *sfep)
  153. {
  154. return ((xfs_dir2_sf_entry_t *) \
  155. ((char *)(sfep) + XFS_DIR2_SF_ENTSIZE_BYENTRY(sfp,sfep)));
  156. }
  157. /*
  158. * Functions.
  159. */
  160. extern int xfs_dir2_block_sfsize(struct xfs_inode *dp,
  161. struct xfs_dir2_block *block,
  162. xfs_dir2_sf_hdr_t *sfhp);
  163. extern int xfs_dir2_block_to_sf(struct xfs_da_args *args, struct xfs_dabuf *bp,
  164. int size, xfs_dir2_sf_hdr_t *sfhp);
  165. extern int xfs_dir2_sf_addname(struct xfs_da_args *args);
  166. extern int xfs_dir2_sf_create(struct xfs_da_args *args, xfs_ino_t pino);
  167. extern int xfs_dir2_sf_getdents(struct xfs_inode *dp, struct uio *uio,
  168. int *eofp, struct xfs_dirent *dbp,
  169. xfs_dir2_put_t put);
  170. extern int xfs_dir2_sf_lookup(struct xfs_da_args *args);
  171. extern int xfs_dir2_sf_removename(struct xfs_da_args *args);
  172. extern int xfs_dir2_sf_replace(struct xfs_da_args *args);
  173. #endif /* __XFS_DIR2_SF_H__ */