Commit | Line | Data |
---|---|---|
57926640 CH |
1 | /* |
2 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. | |
f5f3d9b0 | 3 | * Copyright (c) 2013 Red Hat, Inc. |
57926640 CH |
4 | * All Rights Reserved. |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it would be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write the Free Software Foundation, | |
17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | #ifndef __XFS_DIR2_FORMAT_H__ | |
20 | #define __XFS_DIR2_FORMAT_H__ | |
21 | ||
22 | /* | |
23 | * Directory version 2. | |
24 | * | |
25 | * There are 4 possible formats: | |
26 | * - shortform - embedded into the inode | |
27 | * - single block - data with embedded leaf at the end | |
28 | * - multiple data blocks, single leaf+freeindex block | |
29 | * - data blocks, node and leaf blocks (btree), freeindex blocks | |
30 | * | |
31 | * Note: many node blocks structures and constants are shared with the attr | |
32 | * code and defined in xfs_da_btree.h. | |
33 | */ | |
34 | ||
35 | #define XFS_DIR2_BLOCK_MAGIC 0x58443242 /* XD2B: single block dirs */ | |
36 | #define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: multiblock dirs */ | |
37 | #define XFS_DIR2_FREE_MAGIC 0x58443246 /* XD2F: free index blocks */ | |
38 | ||
f5f3d9b0 DC |
39 | /* |
40 | * Directory Version 3 With CRCs. | |
41 | * | |
42 | * The tree formats are the same as for version 2 directories. The difference | |
43 | * is in the block header and dirent formats. In many cases the v3 structures | |
44 | * use v2 definitions as they are no different and this makes code sharing much | |
45 | * easier. | |
46 | * | |
47 | * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the | |
48 | * format is v2 then they switch to the existing v2 code, or the format is v3 | |
49 | * they implement the v3 functionality. This means the existing dir2 is a mix of | |
50 | * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called | |
51 | * where there is a difference in the formats, otherwise the code is unchanged. | |
52 | * | |
53 | * Where it is possible, the code decides what to do based on the magic numbers | |
54 | * in the blocks rather than feature bits in the superblock. This means the code | |
55 | * is as independent of the external XFS code as possible as doesn't require | |
56 | * passing struct xfs_mount pointers into places where it isn't really | |
57 | * necessary. | |
58 | * | |
59 | * Version 3 includes: | |
60 | * | |
61 | * - a larger block header for CRC and identification purposes and so the | |
62 | * offsets of all the structures inside the blocks are different. | |
63 | * | |
64 | * - new magic numbers to be able to detect the v2/v3 types on the fly. | |
65 | */ | |
66 | ||
67 | #define XFS_DIR3_BLOCK_MAGIC 0x58444233 /* XDB3: single block dirs */ | |
68 | #define XFS_DIR3_DATA_MAGIC 0x58444433 /* XDD3: multiblock dirs */ | |
cbc8adf8 | 69 | #define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */ |
f5f3d9b0 | 70 | |
0cb97766 DC |
71 | /* |
72 | * Dirents in version 3 directories have a file type field. Additions to this | |
73 | * list are an on-disk format change, requiring feature bits. Valid values | |
74 | * are as follows: | |
75 | */ | |
76 | #define XFS_DIR3_FT_UNKNOWN 0 | |
77 | #define XFS_DIR3_FT_REG_FILE 1 | |
78 | #define XFS_DIR3_FT_DIR 2 | |
79 | #define XFS_DIR3_FT_CHRDEV 3 | |
80 | #define XFS_DIR3_FT_BLKDEV 4 | |
81 | #define XFS_DIR3_FT_FIFO 5 | |
82 | #define XFS_DIR3_FT_SOCK 6 | |
83 | #define XFS_DIR3_FT_SYMLINK 7 | |
84 | #define XFS_DIR3_FT_WHT 8 | |
85 | ||
86 | #define XFS_DIR3_FT_MAX 9 | |
87 | ||
57926640 CH |
88 | /* |
89 | * Byte offset in data block and shortform entry. | |
90 | */ | |
91 | typedef __uint16_t xfs_dir2_data_off_t; | |
92 | #define NULLDATAOFF 0xffffU | |
93 | typedef uint xfs_dir2_data_aoff_t; /* argument form */ | |
94 | ||
95 | /* | |
96 | * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t. | |
97 | * Only need 16 bits, this is the byte offset into the single block form. | |
98 | */ | |
99 | typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t; | |
100 | ||
101 | /* | |
102 | * Offset in data space of a data entry. | |
103 | */ | |
104 | typedef __uint32_t xfs_dir2_dataptr_t; | |
105 | #define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff) | |
106 | #define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0) | |
107 | ||
108 | /* | |
109 | * Byte offset in a directory. | |
110 | */ | |
111 | typedef xfs_off_t xfs_dir2_off_t; | |
112 | ||
113 | /* | |
114 | * Directory block number (logical dirblk in file) | |
115 | */ | |
116 | typedef __uint32_t xfs_dir2_db_t; | |
117 | ||
118 | /* | |
119 | * Inode number stored as 8 8-bit values. | |
120 | */ | |
121 | typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t; | |
122 | ||
123 | /* | |
124 | * Inode number stored as 4 8-bit values. | |
125 | * Works a lot of the time, when all the inode numbers in a directory | |
126 | * fit in 32 bits. | |
127 | */ | |
128 | typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t; | |
129 | ||
130 | typedef union { | |
131 | xfs_dir2_ino8_t i8; | |
132 | xfs_dir2_ino4_t i4; | |
133 | } xfs_dir2_inou_t; | |
134 | #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL) | |
135 | ||
136 | /* | |
137 | * Directory layout when stored internal to an inode. | |
138 | * | |
139 | * Small directories are packed as tightly as possible so as to fit into the | |
140 | * literal area of the inode. These "shortform" directories consist of a | |
141 | * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry | |
142 | * structures. Due the different inode number storage size and the variable | |
143 | * length name field in the xfs_dir2_sf_entry all these structure are | |
144 | * variable length, and the accessors in this file should be used to iterate | |
145 | * over them. | |
146 | */ | |
147 | typedef struct xfs_dir2_sf_hdr { | |
148 | __uint8_t count; /* count of entries */ | |
149 | __uint8_t i8count; /* count of 8-byte inode #s */ | |
150 | xfs_dir2_inou_t parent; /* parent dir inode number */ | |
151 | } __arch_pack xfs_dir2_sf_hdr_t; | |
152 | ||
153 | typedef struct xfs_dir2_sf_entry { | |
154 | __u8 namelen; /* actual name length */ | |
155 | xfs_dir2_sf_off_t offset; /* saved offset */ | |
156 | __u8 name[]; /* name, variable size */ | |
157 | /* | |
0cb97766 DC |
158 | * A single byte containing the file type field follows the inode |
159 | * number for version 3 directory entries. | |
160 | * | |
57926640 CH |
161 | * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a |
162 | * variable offset after the name. | |
163 | */ | |
164 | } __arch_pack xfs_dir2_sf_entry_t; | |
165 | ||
166 | static inline int xfs_dir2_sf_hdr_size(int i8count) | |
167 | { | |
168 | return sizeof(struct xfs_dir2_sf_hdr) - | |
169 | (i8count == 0) * | |
170 | (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t)); | |
171 | } | |
172 | ||
173 | static inline xfs_dir2_data_aoff_t | |
174 | xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep) | |
175 | { | |
176 | return get_unaligned_be16(&sfep->offset.i); | |
177 | } | |
178 | ||
179 | static inline void | |
180 | xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off) | |
181 | { | |
182 | put_unaligned_be16(off, &sfep->offset.i); | |
183 | } | |
184 | ||
57926640 CH |
185 | static inline struct xfs_dir2_sf_entry * |
186 | xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr) | |
187 | { | |
188 | return (struct xfs_dir2_sf_entry *) | |
189 | ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count)); | |
190 | } | |
191 | ||
0cb97766 DC |
192 | static inline int |
193 | xfs_dir3_sf_entsize( | |
194 | struct xfs_mount *mp, | |
195 | struct xfs_dir2_sf_hdr *hdr, | |
196 | int len) | |
197 | { | |
198 | int count = sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */ | |
199 | ||
200 | count += len; /* name */ | |
201 | count += hdr->i8count ? sizeof(xfs_dir2_ino8_t) : | |
202 | sizeof(xfs_dir2_ino4_t); /* ino # */ | |
203 | if (xfs_sb_version_hasftype(&mp->m_sb)) | |
204 | count += sizeof(__uint8_t); /* file type */ | |
205 | return count; | |
206 | } | |
207 | ||
57926640 | 208 | static inline struct xfs_dir2_sf_entry * |
0cb97766 DC |
209 | xfs_dir3_sf_nextentry( |
210 | struct xfs_mount *mp, | |
211 | struct xfs_dir2_sf_hdr *hdr, | |
212 | struct xfs_dir2_sf_entry *sfep) | |
57926640 CH |
213 | { |
214 | return (struct xfs_dir2_sf_entry *) | |
0cb97766 DC |
215 | ((char *)sfep + xfs_dir3_sf_entsize(mp, hdr, sfep->namelen)); |
216 | } | |
217 | ||
218 | /* | |
219 | * in dir3 shortform directories, the file type field is stored at a variable | |
220 | * offset after the inode number. Because it's only a single byte, endian | |
221 | * conversion is not necessary. | |
222 | */ | |
223 | static inline __uint8_t * | |
224 | xfs_dir3_sfe_ftypep( | |
225 | struct xfs_dir2_sf_hdr *hdr, | |
226 | struct xfs_dir2_sf_entry *sfep) | |
227 | { | |
228 | return (__uint8_t *)&sfep->name[sfep->namelen]; | |
229 | } | |
230 | ||
231 | static inline __uint8_t | |
232 | xfs_dir3_sfe_get_ftype( | |
233 | struct xfs_mount *mp, | |
234 | struct xfs_dir2_sf_hdr *hdr, | |
235 | struct xfs_dir2_sf_entry *sfep) | |
236 | { | |
237 | __uint8_t *ftp; | |
238 | ||
239 | if (!xfs_sb_version_hasftype(&mp->m_sb)) | |
240 | return XFS_DIR3_FT_UNKNOWN; | |
241 | ||
242 | ftp = xfs_dir3_sfe_ftypep(hdr, sfep); | |
243 | if (*ftp >= XFS_DIR3_FT_MAX) | |
244 | return XFS_DIR3_FT_UNKNOWN; | |
245 | return *ftp; | |
57926640 CH |
246 | } |
247 | ||
0cb97766 DC |
248 | static inline void |
249 | xfs_dir3_sfe_put_ftype( | |
250 | struct xfs_mount *mp, | |
251 | struct xfs_dir2_sf_hdr *hdr, | |
252 | struct xfs_dir2_sf_entry *sfep, | |
253 | __uint8_t ftype) | |
254 | { | |
255 | __uint8_t *ftp; | |
256 | ||
257 | ASSERT(ftype < XFS_DIR3_FT_MAX); | |
258 | ||
259 | if (!xfs_sb_version_hasftype(&mp->m_sb)) | |
260 | return; | |
261 | ftp = xfs_dir3_sfe_ftypep(hdr, sfep); | |
262 | *ftp = ftype; | |
263 | } | |
57926640 CH |
264 | |
265 | /* | |
266 | * Data block structures. | |
267 | * | |
268 | * A pure data block looks like the following drawing on disk: | |
269 | * | |
270 | * +-------------------------------------------------+ | |
271 | * | xfs_dir2_data_hdr_t | | |
272 | * +-------------------------------------------------+ | |
273 | * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t | | |
274 | * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t | | |
275 | * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t | | |
276 | * | ... | | |
277 | * +-------------------------------------------------+ | |
278 | * | unused space | | |
279 | * +-------------------------------------------------+ | |
280 | * | |
281 | * As all the entries are variable size structures the accessors below should | |
282 | * be used to iterate over them. | |
283 | * | |
284 | * In addition to the pure data blocks for the data and node formats, | |
285 | * most structures are also used for the combined data/freespace "block" | |
286 | * format below. | |
287 | */ | |
288 | ||
289 | #define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */ | |
290 | #define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG) | |
291 | #define XFS_DIR2_DATA_FREE_TAG 0xffff | |
292 | #define XFS_DIR2_DATA_FD_COUNT 3 | |
293 | ||
294 | /* | |
295 | * Directory address space divided into sections, | |
296 | * spaces separated by 32GB. | |
297 | */ | |
298 | #define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG)) | |
299 | #define XFS_DIR2_DATA_SPACE 0 | |
300 | #define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE) | |
301 | #define XFS_DIR2_DATA_FIRSTDB(mp) \ | |
302 | xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET) | |
303 | ||
57926640 CH |
304 | /* |
305 | * Describe a free area in the data block. | |
306 | * | |
307 | * The freespace will be formatted as a xfs_dir2_data_unused_t. | |
308 | */ | |
309 | typedef struct xfs_dir2_data_free { | |
310 | __be16 offset; /* start of freespace */ | |
311 | __be16 length; /* length of freespace */ | |
312 | } xfs_dir2_data_free_t; | |
313 | ||
314 | /* | |
315 | * Header for the data blocks. | |
316 | * | |
317 | * The code knows that XFS_DIR2_DATA_FD_COUNT is 3. | |
318 | */ | |
319 | typedef struct xfs_dir2_data_hdr { | |
320 | __be32 magic; /* XFS_DIR2_DATA_MAGIC or */ | |
321 | /* XFS_DIR2_BLOCK_MAGIC */ | |
322 | xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT]; | |
323 | } xfs_dir2_data_hdr_t; | |
324 | ||
f5f3d9b0 DC |
325 | /* |
326 | * define a structure for all the verification fields we are adding to the | |
327 | * directory block structures. This will be used in several structures. | |
328 | * The magic number must be the first entry to align with all the dir2 | |
329 | * structures so we determine how to decode them just by the magic number. | |
330 | */ | |
331 | struct xfs_dir3_blk_hdr { | |
332 | __be32 magic; /* magic number */ | |
333 | __be32 crc; /* CRC of block */ | |
334 | __be64 blkno; /* first block of the buffer */ | |
335 | __be64 lsn; /* sequence number of last write */ | |
336 | uuid_t uuid; /* filesystem we belong to */ | |
337 | __be64 owner; /* inode that owns the block */ | |
338 | }; | |
339 | ||
340 | struct xfs_dir3_data_hdr { | |
341 | struct xfs_dir3_blk_hdr hdr; | |
342 | xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT]; | |
5170711d | 343 | __be32 pad; /* 64 bit alignment */ |
f5f3d9b0 DC |
344 | }; |
345 | ||
346 | #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc) | |
347 | ||
348 | static inline struct xfs_dir2_data_free * | |
349 | xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr) | |
350 | { | |
33363fee DC |
351 | if (hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
352 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) { | |
f5f3d9b0 DC |
353 | struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr; |
354 | return hdr3->best_free; | |
355 | } | |
356 | return hdr->bestfree; | |
357 | } | |
358 | ||
57926640 CH |
359 | /* |
360 | * Active entry in a data block. | |
361 | * | |
362 | * Aligned to 8 bytes. After the variable length name field there is a | |
0cb97766 DC |
363 | * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p. |
364 | * | |
365 | * For dir3 structures, there is file type field between the name and the tag. | |
366 | * This can only be manipulated by helper functions. It is packed hard against | |
367 | * the end of the name so any padding for rounding is between the file type and | |
368 | * the tag. | |
57926640 CH |
369 | */ |
370 | typedef struct xfs_dir2_data_entry { | |
371 | __be64 inumber; /* inode number */ | |
372 | __u8 namelen; /* name length */ | |
373 | __u8 name[]; /* name bytes, no null */ | |
0cb97766 | 374 | /* __u8 filetype; */ /* type of inode we point to */ |
57926640 CH |
375 | /* __be16 tag; */ /* starting offset of us */ |
376 | } xfs_dir2_data_entry_t; | |
377 | ||
378 | /* | |
379 | * Unused entry in a data block. | |
380 | * | |
381 | * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed | |
382 | * using xfs_dir2_data_unused_tag_p. | |
383 | */ | |
384 | typedef struct xfs_dir2_data_unused { | |
385 | __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */ | |
386 | __be16 length; /* total free length */ | |
387 | /* variable offset */ | |
388 | __be16 tag; /* starting offset of us */ | |
389 | } xfs_dir2_data_unused_t; | |
390 | ||
391 | /* | |
392 | * Size of a data entry. | |
393 | */ | |
0cb97766 DC |
394 | static inline int |
395 | __xfs_dir3_data_entsize( | |
396 | bool ftype, | |
397 | int n) | |
398 | { | |
399 | int size = offsetof(struct xfs_dir2_data_entry, name[0]); | |
400 | ||
401 | size += n; | |
402 | size += sizeof(xfs_dir2_data_off_t); | |
403 | if (ftype) | |
404 | size += sizeof(__uint8_t); | |
405 | return roundup(size, XFS_DIR2_DATA_ALIGN); | |
406 | } | |
407 | static inline int | |
408 | xfs_dir3_data_entsize( | |
409 | struct xfs_mount *mp, | |
410 | int n) | |
411 | { | |
412 | bool ftype = xfs_sb_version_hasftype(&mp->m_sb) ? true : false; | |
413 | return __xfs_dir3_data_entsize(ftype, n); | |
414 | } | |
415 | ||
416 | static inline __uint8_t | |
417 | xfs_dir3_dirent_get_ftype( | |
418 | struct xfs_mount *mp, | |
419 | struct xfs_dir2_data_entry *dep) | |
420 | { | |
421 | if (xfs_sb_version_hasftype(&mp->m_sb)) { | |
422 | __uint8_t type = dep->name[dep->namelen]; | |
423 | ||
424 | ASSERT(type < XFS_DIR3_FT_MAX); | |
425 | if (type < XFS_DIR3_FT_MAX) | |
426 | return type; | |
427 | ||
428 | } | |
429 | return XFS_DIR3_FT_UNKNOWN; | |
430 | } | |
431 | ||
432 | static inline void | |
433 | xfs_dir3_dirent_put_ftype( | |
434 | struct xfs_mount *mp, | |
435 | struct xfs_dir2_data_entry *dep, | |
436 | __uint8_t type) | |
57926640 | 437 | { |
0cb97766 DC |
438 | ASSERT(type < XFS_DIR3_FT_MAX); |
439 | ASSERT(dep->namelen != 0); | |
440 | ||
441 | if (xfs_sb_version_hasftype(&mp->m_sb)) | |
442 | dep->name[dep->namelen] = type; | |
57926640 CH |
443 | } |
444 | ||
445 | /* | |
446 | * Pointer to an entry's tag word. | |
447 | */ | |
448 | static inline __be16 * | |
0cb97766 DC |
449 | xfs_dir3_data_entry_tag_p( |
450 | struct xfs_mount *mp, | |
451 | struct xfs_dir2_data_entry *dep) | |
57926640 CH |
452 | { |
453 | return (__be16 *)((char *)dep + | |
0cb97766 | 454 | xfs_dir3_data_entsize(mp, dep->namelen) - sizeof(__be16)); |
57926640 CH |
455 | } |
456 | ||
457 | /* | |
458 | * Pointer to a freespace's tag word. | |
459 | */ | |
460 | static inline __be16 * | |
461 | xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup) | |
462 | { | |
463 | return (__be16 *)((char *)dup + | |
464 | be16_to_cpu(dup->length) - sizeof(__be16)); | |
465 | } | |
466 | ||
f5f3d9b0 DC |
467 | static inline size_t |
468 | xfs_dir3_data_hdr_size(bool dir3) | |
469 | { | |
470 | if (dir3) | |
471 | return sizeof(struct xfs_dir3_data_hdr); | |
472 | return sizeof(struct xfs_dir2_data_hdr); | |
473 | } | |
474 | ||
475 | static inline size_t | |
476 | xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr) | |
477 | { | |
478 | bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || | |
479 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC); | |
480 | return xfs_dir3_data_hdr_size(dir3); | |
481 | } | |
482 | ||
483 | static inline struct xfs_dir2_data_entry * | |
484 | xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr) | |
485 | { | |
486 | return (struct xfs_dir2_data_entry *) | |
487 | ((char *)hdr + xfs_dir3_data_entry_offset(hdr)); | |
488 | } | |
489 | ||
33363fee DC |
490 | static inline struct xfs_dir2_data_unused * |
491 | xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr) | |
492 | { | |
493 | return (struct xfs_dir2_data_unused *) | |
494 | ((char *)hdr + xfs_dir3_data_entry_offset(hdr)); | |
495 | } | |
496 | ||
f5f3d9b0 DC |
497 | /* |
498 | * Offsets of . and .. in data space (always block 0) | |
6b2647a1 | 499 | * |
0cb97766 DC |
500 | * XXX: there is scope for significant optimisation of the logic here. Right |
501 | * now we are checking for "dir3 format" over and over again. Ideally we should | |
502 | * only do it once for each operation. | |
f5f3d9b0 DC |
503 | */ |
504 | static inline xfs_dir2_data_aoff_t | |
367993e7 | 505 | xfs_dir3_data_dot_offset(struct xfs_mount *mp) |
f5f3d9b0 | 506 | { |
367993e7 | 507 | return xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb)); |
f5f3d9b0 DC |
508 | } |
509 | ||
510 | static inline xfs_dir2_data_aoff_t | |
367993e7 | 511 | xfs_dir3_data_dotdot_offset(struct xfs_mount *mp) |
f5f3d9b0 | 512 | { |
367993e7 DC |
513 | return xfs_dir3_data_dot_offset(mp) + |
514 | xfs_dir3_data_entsize(mp, 1); | |
f5f3d9b0 DC |
515 | } |
516 | ||
517 | static inline xfs_dir2_data_aoff_t | |
367993e7 | 518 | xfs_dir3_data_first_offset(struct xfs_mount *mp) |
f5f3d9b0 | 519 | { |
367993e7 DC |
520 | return xfs_dir3_data_dotdot_offset(mp) + |
521 | xfs_dir3_data_entsize(mp, 2); | |
f5f3d9b0 DC |
522 | } |
523 | ||
524 | /* | |
525 | * location of . and .. in data space (always block 0) | |
526 | */ | |
527 | static inline struct xfs_dir2_data_entry * | |
367993e7 DC |
528 | xfs_dir3_data_dot_entry_p( |
529 | struct xfs_mount *mp, | |
530 | struct xfs_dir2_data_hdr *hdr) | |
f5f3d9b0 DC |
531 | { |
532 | return (struct xfs_dir2_data_entry *) | |
367993e7 | 533 | ((char *)hdr + xfs_dir3_data_dot_offset(mp)); |
f5f3d9b0 DC |
534 | } |
535 | ||
536 | static inline struct xfs_dir2_data_entry * | |
367993e7 DC |
537 | xfs_dir3_data_dotdot_entry_p( |
538 | struct xfs_mount *mp, | |
539 | struct xfs_dir2_data_hdr *hdr) | |
f5f3d9b0 DC |
540 | { |
541 | return (struct xfs_dir2_data_entry *) | |
367993e7 | 542 | ((char *)hdr + xfs_dir3_data_dotdot_offset(mp)); |
f5f3d9b0 DC |
543 | } |
544 | ||
545 | static inline struct xfs_dir2_data_entry * | |
367993e7 DC |
546 | xfs_dir3_data_first_entry_p( |
547 | struct xfs_mount *mp, | |
548 | struct xfs_dir2_data_hdr *hdr) | |
f5f3d9b0 DC |
549 | { |
550 | return (struct xfs_dir2_data_entry *) | |
367993e7 | 551 | ((char *)hdr + xfs_dir3_data_first_offset(mp)); |
f5f3d9b0 DC |
552 | } |
553 | ||
57926640 CH |
554 | /* |
555 | * Leaf block structures. | |
556 | * | |
557 | * A pure leaf block looks like the following drawing on disk: | |
558 | * | |
559 | * +---------------------------+ | |
560 | * | xfs_dir2_leaf_hdr_t | | |
561 | * +---------------------------+ | |
562 | * | xfs_dir2_leaf_entry_t | | |
563 | * | xfs_dir2_leaf_entry_t | | |
564 | * | xfs_dir2_leaf_entry_t | | |
565 | * | xfs_dir2_leaf_entry_t | | |
566 | * | ... | | |
567 | * +---------------------------+ | |
568 | * | xfs_dir2_data_off_t | | |
569 | * | xfs_dir2_data_off_t | | |
570 | * | xfs_dir2_data_off_t | | |
571 | * | ... | | |
572 | * +---------------------------+ | |
573 | * | xfs_dir2_leaf_tail_t | | |
574 | * +---------------------------+ | |
575 | * | |
576 | * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block | |
577 | * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present | |
578 | * for directories with separate leaf nodes and free space blocks | |
579 | * (magic = XFS_DIR2_LEAFN_MAGIC). | |
580 | * | |
581 | * As all the entries are variable size structures the accessors below should | |
582 | * be used to iterate over them. | |
583 | */ | |
584 | ||
585 | /* | |
586 | * Offset of the leaf/node space. First block in this space | |
587 | * is the btree root. | |
588 | */ | |
589 | #define XFS_DIR2_LEAF_SPACE 1 | |
590 | #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE) | |
591 | #define XFS_DIR2_LEAF_FIRSTDB(mp) \ | |
592 | xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET) | |
593 | ||
594 | /* | |
595 | * Leaf block header. | |
596 | */ | |
597 | typedef struct xfs_dir2_leaf_hdr { | |
598 | xfs_da_blkinfo_t info; /* header for da routines */ | |
599 | __be16 count; /* count of entries */ | |
600 | __be16 stale; /* count of stale entries */ | |
601 | } xfs_dir2_leaf_hdr_t; | |
602 | ||
24df33b4 DC |
603 | struct xfs_dir3_leaf_hdr { |
604 | struct xfs_da3_blkinfo info; /* header for da routines */ | |
605 | __be16 count; /* count of entries */ | |
606 | __be16 stale; /* count of stale entries */ | |
5170711d | 607 | __be32 pad; /* 64 bit alignment */ |
24df33b4 DC |
608 | }; |
609 | ||
610 | struct xfs_dir3_icleaf_hdr { | |
611 | __uint32_t forw; | |
612 | __uint32_t back; | |
613 | __uint16_t magic; | |
614 | __uint16_t count; | |
615 | __uint16_t stale; | |
616 | }; | |
617 | ||
57926640 CH |
618 | /* |
619 | * Leaf block entry. | |
620 | */ | |
621 | typedef struct xfs_dir2_leaf_entry { | |
622 | __be32 hashval; /* hash value of name */ | |
623 | __be32 address; /* address of data entry */ | |
624 | } xfs_dir2_leaf_entry_t; | |
625 | ||
626 | /* | |
627 | * Leaf block tail. | |
628 | */ | |
629 | typedef struct xfs_dir2_leaf_tail { | |
630 | __be32 bestcount; | |
631 | } xfs_dir2_leaf_tail_t; | |
632 | ||
633 | /* | |
634 | * Leaf block. | |
635 | */ | |
636 | typedef struct xfs_dir2_leaf { | |
24df33b4 DC |
637 | xfs_dir2_leaf_hdr_t hdr; /* leaf header */ |
638 | xfs_dir2_leaf_entry_t __ents[]; /* entries */ | |
57926640 CH |
639 | } xfs_dir2_leaf_t; |
640 | ||
24df33b4 DC |
641 | struct xfs_dir3_leaf { |
642 | struct xfs_dir3_leaf_hdr hdr; /* leaf header */ | |
643 | struct xfs_dir2_leaf_entry __ents[]; /* entries */ | |
644 | }; | |
645 | ||
646 | #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc) | |
57926640 | 647 | |
d386b32b DC |
648 | extern void xfs_dir3_leaf_hdr_from_disk(struct xfs_dir3_icleaf_hdr *to, |
649 | struct xfs_dir2_leaf *from); | |
650 | ||
24df33b4 DC |
651 | static inline int |
652 | xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp) | |
57926640 | 653 | { |
24df33b4 DC |
654 | if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || |
655 | lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) | |
656 | return sizeof(struct xfs_dir3_leaf_hdr); | |
657 | return sizeof(struct xfs_dir2_leaf_hdr); | |
658 | } | |
659 | ||
660 | static inline int | |
661 | xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp) | |
662 | { | |
663 | return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) / | |
57926640 CH |
664 | (uint)sizeof(struct xfs_dir2_leaf_entry); |
665 | } | |
666 | ||
24df33b4 DC |
667 | /* |
668 | * Get address of the bestcount field in the single-leaf block. | |
669 | */ | |
670 | static inline struct xfs_dir2_leaf_entry * | |
671 | xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp) | |
672 | { | |
673 | if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || | |
674 | lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) { | |
675 | struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp; | |
676 | return lp3->__ents; | |
677 | } | |
678 | return lp->__ents; | |
679 | } | |
680 | ||
57926640 CH |
681 | /* |
682 | * Get address of the bestcount field in the single-leaf block. | |
683 | */ | |
684 | static inline struct xfs_dir2_leaf_tail * | |
685 | xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp) | |
686 | { | |
687 | return (struct xfs_dir2_leaf_tail *) | |
688 | ((char *)lp + mp->m_dirblksize - | |
689 | sizeof(struct xfs_dir2_leaf_tail)); | |
690 | } | |
691 | ||
692 | /* | |
693 | * Get address of the bests array in the single-leaf block. | |
694 | */ | |
695 | static inline __be16 * | |
696 | xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp) | |
697 | { | |
698 | return (__be16 *)ltp - be32_to_cpu(ltp->bestcount); | |
699 | } | |
700 | ||
24df33b4 DC |
701 | /* |
702 | * DB blocks here are logical directory block numbers, not filesystem blocks. | |
703 | */ | |
704 | ||
57926640 CH |
705 | /* |
706 | * Convert dataptr to byte in file space | |
707 | */ | |
708 | static inline xfs_dir2_off_t | |
709 | xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp) | |
710 | { | |
711 | return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG; | |
712 | } | |
713 | ||
714 | /* | |
715 | * Convert byte in file space to dataptr. It had better be aligned. | |
716 | */ | |
717 | static inline xfs_dir2_dataptr_t | |
718 | xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by) | |
719 | { | |
720 | return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG); | |
721 | } | |
722 | ||
723 | /* | |
724 | * Convert byte in space to (DB) block | |
725 | */ | |
726 | static inline xfs_dir2_db_t | |
727 | xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by) | |
728 | { | |
729 | return (xfs_dir2_db_t) | |
730 | (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)); | |
731 | } | |
732 | ||
733 | /* | |
734 | * Convert dataptr to a block number | |
735 | */ | |
736 | static inline xfs_dir2_db_t | |
737 | xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp) | |
738 | { | |
739 | return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp)); | |
740 | } | |
741 | ||
742 | /* | |
743 | * Convert byte in space to offset in a block | |
744 | */ | |
745 | static inline xfs_dir2_data_aoff_t | |
746 | xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by) | |
747 | { | |
748 | return (xfs_dir2_data_aoff_t)(by & | |
749 | ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1)); | |
750 | } | |
751 | ||
752 | /* | |
753 | * Convert dataptr to a byte offset in a block | |
754 | */ | |
755 | static inline xfs_dir2_data_aoff_t | |
756 | xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp) | |
757 | { | |
758 | return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp)); | |
759 | } | |
760 | ||
761 | /* | |
762 | * Convert block and offset to byte in space | |
763 | */ | |
764 | static inline xfs_dir2_off_t | |
765 | xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db, | |
766 | xfs_dir2_data_aoff_t o) | |
767 | { | |
768 | return ((xfs_dir2_off_t)db << | |
769 | (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o; | |
770 | } | |
771 | ||
772 | /* | |
773 | * Convert block (DB) to block (dablk) | |
774 | */ | |
775 | static inline xfs_dablk_t | |
776 | xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db) | |
777 | { | |
778 | return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog); | |
779 | } | |
780 | ||
781 | /* | |
782 | * Convert byte in space to (DA) block | |
783 | */ | |
784 | static inline xfs_dablk_t | |
785 | xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by) | |
786 | { | |
787 | return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by)); | |
788 | } | |
789 | ||
790 | /* | |
791 | * Convert block and offset to dataptr | |
792 | */ | |
793 | static inline xfs_dir2_dataptr_t | |
794 | xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db, | |
795 | xfs_dir2_data_aoff_t o) | |
796 | { | |
797 | return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o)); | |
798 | } | |
799 | ||
800 | /* | |
801 | * Convert block (dablk) to block (DB) | |
802 | */ | |
803 | static inline xfs_dir2_db_t | |
804 | xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da) | |
805 | { | |
806 | return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog); | |
807 | } | |
808 | ||
809 | /* | |
810 | * Convert block (dablk) to byte offset in space | |
811 | */ | |
812 | static inline xfs_dir2_off_t | |
813 | xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da) | |
814 | { | |
815 | return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0); | |
816 | } | |
817 | ||
818 | /* | |
819 | * Free space block defintions for the node format. | |
820 | */ | |
821 | ||
822 | /* | |
823 | * Offset of the freespace index. | |
824 | */ | |
825 | #define XFS_DIR2_FREE_SPACE 2 | |
826 | #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE) | |
827 | #define XFS_DIR2_FREE_FIRSTDB(mp) \ | |
828 | xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET) | |
829 | ||
830 | typedef struct xfs_dir2_free_hdr { | |
831 | __be32 magic; /* XFS_DIR2_FREE_MAGIC */ | |
832 | __be32 firstdb; /* db of first entry */ | |
833 | __be32 nvalid; /* count of valid entries */ | |
834 | __be32 nused; /* count of used entries */ | |
835 | } xfs_dir2_free_hdr_t; | |
836 | ||
837 | typedef struct xfs_dir2_free { | |
838 | xfs_dir2_free_hdr_t hdr; /* block header */ | |
a00b7745 | 839 | __be16 bests[]; /* best free counts */ |
57926640 CH |
840 | /* unused entries are -1 */ |
841 | } xfs_dir2_free_t; | |
842 | ||
cbc8adf8 DC |
843 | struct xfs_dir3_free_hdr { |
844 | struct xfs_dir3_blk_hdr hdr; | |
845 | __be32 firstdb; /* db of first entry */ | |
846 | __be32 nvalid; /* count of valid entries */ | |
847 | __be32 nused; /* count of used entries */ | |
5170711d | 848 | __be32 pad; /* 64 bit alignment */ |
cbc8adf8 DC |
849 | }; |
850 | ||
851 | struct xfs_dir3_free { | |
852 | struct xfs_dir3_free_hdr hdr; | |
853 | __be16 bests[]; /* best free counts */ | |
854 | /* unused entries are -1 */ | |
855 | }; | |
856 | ||
857 | #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc) | |
858 | ||
859 | /* | |
860 | * In core version of the free block header, abstracted away from on-disk format | |
861 | * differences. Use this in the code, and convert to/from the disk version using | |
862 | * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk. | |
863 | */ | |
864 | struct xfs_dir3_icfree_hdr { | |
865 | __uint32_t magic; | |
866 | __uint32_t firstdb; | |
867 | __uint32_t nvalid; | |
868 | __uint32_t nused; | |
869 | ||
870 | }; | |
871 | ||
872 | void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to, | |
873 | struct xfs_dir2_free *from); | |
874 | ||
875 | static inline int | |
876 | xfs_dir3_free_hdr_size(struct xfs_mount *mp) | |
a00b7745 | 877 | { |
cbc8adf8 DC |
878 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
879 | return sizeof(struct xfs_dir3_free_hdr); | |
880 | return sizeof(struct xfs_dir2_free_hdr); | |
881 | } | |
882 | ||
883 | static inline int | |
884 | xfs_dir3_free_max_bests(struct xfs_mount *mp) | |
885 | { | |
886 | return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) / | |
a00b7745 CH |
887 | sizeof(xfs_dir2_data_off_t); |
888 | } | |
57926640 | 889 | |
cbc8adf8 DC |
890 | static inline __be16 * |
891 | xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free) | |
892 | { | |
893 | return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp)); | |
894 | } | |
895 | ||
57926640 CH |
896 | /* |
897 | * Convert data space db to the corresponding free db. | |
898 | */ | |
899 | static inline xfs_dir2_db_t | |
900 | xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db) | |
901 | { | |
cbc8adf8 | 902 | return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp); |
57926640 CH |
903 | } |
904 | ||
905 | /* | |
906 | * Convert data space db to the corresponding index in a free db. | |
907 | */ | |
908 | static inline int | |
909 | xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db) | |
910 | { | |
cbc8adf8 | 911 | return db % xfs_dir3_free_max_bests(mp); |
57926640 CH |
912 | } |
913 | ||
914 | /* | |
915 | * Single block format. | |
916 | * | |
917 | * The single block format looks like the following drawing on disk: | |
918 | * | |
919 | * +-------------------------------------------------+ | |
920 | * | xfs_dir2_data_hdr_t | | |
921 | * +-------------------------------------------------+ | |
922 | * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t | | |
923 | * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t | | |
924 | * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t : | |
925 | * | ... | | |
926 | * +-------------------------------------------------+ | |
927 | * | unused space | | |
928 | * +-------------------------------------------------+ | |
929 | * | ... | | |
930 | * | xfs_dir2_leaf_entry_t | | |
931 | * | xfs_dir2_leaf_entry_t | | |
932 | * +-------------------------------------------------+ | |
933 | * | xfs_dir2_block_tail_t | | |
934 | * +-------------------------------------------------+ | |
935 | * | |
936 | * As all the entries are variable size structures the accessors below should | |
937 | * be used to iterate over them. | |
938 | */ | |
939 | ||
940 | typedef struct xfs_dir2_block_tail { | |
941 | __be32 count; /* count of leaf entries */ | |
942 | __be32 stale; /* count of stale lf entries */ | |
943 | } xfs_dir2_block_tail_t; | |
944 | ||
945 | /* | |
946 | * Pointer to the leaf header embedded in a data block (1-block format) | |
947 | */ | |
948 | static inline struct xfs_dir2_block_tail * | |
949 | xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr) | |
950 | { | |
951 | return ((struct xfs_dir2_block_tail *) | |
952 | ((char *)hdr + mp->m_dirblksize)) - 1; | |
953 | } | |
954 | ||
955 | /* | |
956 | * Pointer to the leaf entries embedded in a data block (1-block format) | |
957 | */ | |
958 | static inline struct xfs_dir2_leaf_entry * | |
959 | xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp) | |
960 | { | |
961 | return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count); | |
962 | } | |
963 | ||
964 | #endif /* __XFS_DIR2_FORMAT_H__ */ |