]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_da_format.c
libxfs: update to 3.16 kernel code
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_da_format.c
1 /*
2 * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
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 #include <xfs.h>
20
21 /*
22 * Shortform directory ops
23 */
24 static int
25 xfs_dir2_sf_entsize(
26 struct xfs_dir2_sf_hdr *hdr,
27 int len)
28 {
29 int count = sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */
30
31 count += len; /* name */
32 count += hdr->i8count ? sizeof(xfs_dir2_ino8_t) :
33 sizeof(xfs_dir2_ino4_t); /* ino # */
34 return count;
35 }
36
37 static int
38 xfs_dir3_sf_entsize(
39 struct xfs_dir2_sf_hdr *hdr,
40 int len)
41 {
42 return xfs_dir2_sf_entsize(hdr, len) + sizeof(__uint8_t);
43 }
44
45 static struct xfs_dir2_sf_entry *
46 xfs_dir2_sf_nextentry(
47 struct xfs_dir2_sf_hdr *hdr,
48 struct xfs_dir2_sf_entry *sfep)
49 {
50 return (struct xfs_dir2_sf_entry *)
51 ((char *)sfep + xfs_dir2_sf_entsize(hdr, sfep->namelen));
52 }
53
54 static struct xfs_dir2_sf_entry *
55 xfs_dir3_sf_nextentry(
56 struct xfs_dir2_sf_hdr *hdr,
57 struct xfs_dir2_sf_entry *sfep)
58 {
59 return (struct xfs_dir2_sf_entry *)
60 ((char *)sfep + xfs_dir3_sf_entsize(hdr, sfep->namelen));
61 }
62
63
64 /*
65 * For filetype enabled shortform directories, the file type field is stored at
66 * the end of the name. Because it's only a single byte, endian conversion is
67 * not necessary. For non-filetype enable directories, the type is always
68 * unknown and we never store the value.
69 */
70 static __uint8_t
71 xfs_dir2_sfe_get_ftype(
72 struct xfs_dir2_sf_entry *sfep)
73 {
74 return XFS_DIR3_FT_UNKNOWN;
75 }
76
77 static void
78 xfs_dir2_sfe_put_ftype(
79 struct xfs_dir2_sf_entry *sfep,
80 __uint8_t ftype)
81 {
82 ASSERT(ftype < XFS_DIR3_FT_MAX);
83 }
84
85 static __uint8_t
86 xfs_dir3_sfe_get_ftype(
87 struct xfs_dir2_sf_entry *sfep)
88 {
89 __uint8_t ftype;
90
91 ftype = sfep->name[sfep->namelen];
92 if (ftype >= XFS_DIR3_FT_MAX)
93 return XFS_DIR3_FT_UNKNOWN;
94 return ftype;
95 }
96
97 static void
98 xfs_dir3_sfe_put_ftype(
99 struct xfs_dir2_sf_entry *sfep,
100 __uint8_t ftype)
101 {
102 ASSERT(ftype < XFS_DIR3_FT_MAX);
103
104 sfep->name[sfep->namelen] = ftype;
105 }
106
107 /*
108 * Inode numbers in short-form directories can come in two versions,
109 * either 4 bytes or 8 bytes wide. These helpers deal with the
110 * two forms transparently by looking at the headers i8count field.
111 *
112 * For 64-bit inode number the most significant byte must be zero.
113 */
114 static xfs_ino_t
115 xfs_dir2_sf_get_ino(
116 struct xfs_dir2_sf_hdr *hdr,
117 xfs_dir2_inou_t *from)
118 {
119 if (hdr->i8count)
120 return get_unaligned_be64(&from->i8.i) & 0x00ffffffffffffffULL;
121 else
122 return get_unaligned_be32(&from->i4.i);
123 }
124
125 static void
126 xfs_dir2_sf_put_ino(
127 struct xfs_dir2_sf_hdr *hdr,
128 xfs_dir2_inou_t *to,
129 xfs_ino_t ino)
130 {
131 ASSERT((ino & 0xff00000000000000ULL) == 0);
132
133 if (hdr->i8count)
134 put_unaligned_be64(ino, &to->i8.i);
135 else
136 put_unaligned_be32(ino, &to->i4.i);
137 }
138
139 static xfs_ino_t
140 xfs_dir2_sf_get_parent_ino(
141 struct xfs_dir2_sf_hdr *hdr)
142 {
143 return xfs_dir2_sf_get_ino(hdr, &hdr->parent);
144 }
145
146 static void
147 xfs_dir2_sf_put_parent_ino(
148 struct xfs_dir2_sf_hdr *hdr,
149 xfs_ino_t ino)
150 {
151 xfs_dir2_sf_put_ino(hdr, &hdr->parent, ino);
152 }
153
154 /*
155 * In short-form directory entries the inode numbers are stored at variable
156 * offset behind the entry name. If the entry stores a filetype value, then it
157 * sits between the name and the inode number. Hence the inode numbers may only
158 * be accessed through the helpers below.
159 */
160 static xfs_ino_t
161 xfs_dir2_sfe_get_ino(
162 struct xfs_dir2_sf_hdr *hdr,
163 struct xfs_dir2_sf_entry *sfep)
164 {
165 return xfs_dir2_sf_get_ino(hdr,
166 (xfs_dir2_inou_t *)&sfep->name[sfep->namelen]);
167 }
168
169 static void
170 xfs_dir2_sfe_put_ino(
171 struct xfs_dir2_sf_hdr *hdr,
172 struct xfs_dir2_sf_entry *sfep,
173 xfs_ino_t ino)
174 {
175 xfs_dir2_sf_put_ino(hdr,
176 (xfs_dir2_inou_t *)&sfep->name[sfep->namelen], ino);
177 }
178
179 static xfs_ino_t
180 xfs_dir3_sfe_get_ino(
181 struct xfs_dir2_sf_hdr *hdr,
182 struct xfs_dir2_sf_entry *sfep)
183 {
184 return xfs_dir2_sf_get_ino(hdr,
185 (xfs_dir2_inou_t *)&sfep->name[sfep->namelen + 1]);
186 }
187
188 static void
189 xfs_dir3_sfe_put_ino(
190 struct xfs_dir2_sf_hdr *hdr,
191 struct xfs_dir2_sf_entry *sfep,
192 xfs_ino_t ino)
193 {
194 xfs_dir2_sf_put_ino(hdr,
195 (xfs_dir2_inou_t *)&sfep->name[sfep->namelen + 1], ino);
196 }
197
198
199 /*
200 * Directory data block operations
201 */
202
203 /*
204 * For special situations, the dirent size ends up fixed because we always know
205 * what the size of the entry is. That's true for the "." and "..", and
206 * therefore we know that they are a fixed size and hence their offsets are
207 * constant, as is the first entry.
208 *
209 * Hence, this calculation is written as a macro to be able to be calculated at
210 * compile time and so certain offsets can be calculated directly in the
211 * structure initaliser via the macro. There are two macros - one for dirents
212 * with ftype and without so there are no unresolvable conditionals in the
213 * calculations. We also use round_up() as XFS_DIR2_DATA_ALIGN is always a power
214 * of 2 and the compiler doesn't reject it (unlike roundup()).
215 */
216 #define XFS_DIR2_DATA_ENTSIZE(n) \
217 round_up((offsetof(struct xfs_dir2_data_entry, name[0]) + (n) + \
218 sizeof(xfs_dir2_data_off_t)), XFS_DIR2_DATA_ALIGN)
219
220 #define XFS_DIR3_DATA_ENTSIZE(n) \
221 round_up((offsetof(struct xfs_dir2_data_entry, name[0]) + (n) + \
222 sizeof(xfs_dir2_data_off_t) + sizeof(__uint8_t)), \
223 XFS_DIR2_DATA_ALIGN)
224
225 static int
226 xfs_dir2_data_entsize(
227 int n)
228 {
229 return XFS_DIR2_DATA_ENTSIZE(n);
230 }
231
232 static int
233 xfs_dir3_data_entsize(
234 int n)
235 {
236 return XFS_DIR3_DATA_ENTSIZE(n);
237 }
238
239 static __uint8_t
240 xfs_dir2_data_get_ftype(
241 struct xfs_dir2_data_entry *dep)
242 {
243 return XFS_DIR3_FT_UNKNOWN;
244 }
245
246 static void
247 xfs_dir2_data_put_ftype(
248 struct xfs_dir2_data_entry *dep,
249 __uint8_t ftype)
250 {
251 ASSERT(ftype < XFS_DIR3_FT_MAX);
252 }
253
254 static __uint8_t
255 xfs_dir3_data_get_ftype(
256 struct xfs_dir2_data_entry *dep)
257 {
258 __uint8_t ftype = dep->name[dep->namelen];
259
260 ASSERT(ftype < XFS_DIR3_FT_MAX);
261 if (ftype >= XFS_DIR3_FT_MAX)
262 return XFS_DIR3_FT_UNKNOWN;
263 return ftype;
264 }
265
266 static void
267 xfs_dir3_data_put_ftype(
268 struct xfs_dir2_data_entry *dep,
269 __uint8_t type)
270 {
271 ASSERT(type < XFS_DIR3_FT_MAX);
272 ASSERT(dep->namelen != 0);
273
274 dep->name[dep->namelen] = type;
275 }
276
277 /*
278 * Pointer to an entry's tag word.
279 */
280 static __be16 *
281 xfs_dir2_data_entry_tag_p(
282 struct xfs_dir2_data_entry *dep)
283 {
284 return (__be16 *)((char *)dep +
285 xfs_dir2_data_entsize(dep->namelen) - sizeof(__be16));
286 }
287
288 static __be16 *
289 xfs_dir3_data_entry_tag_p(
290 struct xfs_dir2_data_entry *dep)
291 {
292 return (__be16 *)((char *)dep +
293 xfs_dir3_data_entsize(dep->namelen) - sizeof(__be16));
294 }
295
296 /*
297 * location of . and .. in data space (always block 0)
298 */
299 static struct xfs_dir2_data_entry *
300 xfs_dir2_data_dot_entry_p(
301 struct xfs_dir2_data_hdr *hdr)
302 {
303 return (struct xfs_dir2_data_entry *)
304 ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
305 }
306
307 static struct xfs_dir2_data_entry *
308 xfs_dir2_data_dotdot_entry_p(
309 struct xfs_dir2_data_hdr *hdr)
310 {
311 return (struct xfs_dir2_data_entry *)
312 ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
313 XFS_DIR2_DATA_ENTSIZE(1));
314 }
315
316 static struct xfs_dir2_data_entry *
317 xfs_dir2_data_first_entry_p(
318 struct xfs_dir2_data_hdr *hdr)
319 {
320 return (struct xfs_dir2_data_entry *)
321 ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
322 XFS_DIR2_DATA_ENTSIZE(1) +
323 XFS_DIR2_DATA_ENTSIZE(2));
324 }
325
326 static struct xfs_dir2_data_entry *
327 xfs_dir2_ftype_data_dotdot_entry_p(
328 struct xfs_dir2_data_hdr *hdr)
329 {
330 return (struct xfs_dir2_data_entry *)
331 ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
332 XFS_DIR3_DATA_ENTSIZE(1));
333 }
334
335 static struct xfs_dir2_data_entry *
336 xfs_dir2_ftype_data_first_entry_p(
337 struct xfs_dir2_data_hdr *hdr)
338 {
339 return (struct xfs_dir2_data_entry *)
340 ((char *)hdr + sizeof(struct xfs_dir2_data_hdr) +
341 XFS_DIR3_DATA_ENTSIZE(1) +
342 XFS_DIR3_DATA_ENTSIZE(2));
343 }
344
345 static struct xfs_dir2_data_entry *
346 xfs_dir3_data_dot_entry_p(
347 struct xfs_dir2_data_hdr *hdr)
348 {
349 return (struct xfs_dir2_data_entry *)
350 ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
351 }
352
353 static struct xfs_dir2_data_entry *
354 xfs_dir3_data_dotdot_entry_p(
355 struct xfs_dir2_data_hdr *hdr)
356 {
357 return (struct xfs_dir2_data_entry *)
358 ((char *)hdr + sizeof(struct xfs_dir3_data_hdr) +
359 XFS_DIR3_DATA_ENTSIZE(1));
360 }
361
362 static struct xfs_dir2_data_entry *
363 xfs_dir3_data_first_entry_p(
364 struct xfs_dir2_data_hdr *hdr)
365 {
366 return (struct xfs_dir2_data_entry *)
367 ((char *)hdr + sizeof(struct xfs_dir3_data_hdr) +
368 XFS_DIR3_DATA_ENTSIZE(1) +
369 XFS_DIR3_DATA_ENTSIZE(2));
370 }
371
372 static struct xfs_dir2_data_free *
373 xfs_dir2_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
374 {
375 return hdr->bestfree;
376 }
377
378 static struct xfs_dir2_data_free *
379 xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
380 {
381 return ((struct xfs_dir3_data_hdr *)hdr)->best_free;
382 }
383
384 static struct xfs_dir2_data_entry *
385 xfs_dir2_data_entry_p(struct xfs_dir2_data_hdr *hdr)
386 {
387 return (struct xfs_dir2_data_entry *)
388 ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
389 }
390
391 static struct xfs_dir2_data_unused *
392 xfs_dir2_data_unused_p(struct xfs_dir2_data_hdr *hdr)
393 {
394 return (struct xfs_dir2_data_unused *)
395 ((char *)hdr + sizeof(struct xfs_dir2_data_hdr));
396 }
397
398 static struct xfs_dir2_data_entry *
399 xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
400 {
401 return (struct xfs_dir2_data_entry *)
402 ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
403 }
404
405 static struct xfs_dir2_data_unused *
406 xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
407 {
408 return (struct xfs_dir2_data_unused *)
409 ((char *)hdr + sizeof(struct xfs_dir3_data_hdr));
410 }
411
412
413 /*
414 * Directory Leaf block operations
415 */
416 static int
417 xfs_dir2_max_leaf_ents(struct xfs_da_geometry *geo)
418 {
419 return (geo->blksize - sizeof(struct xfs_dir2_leaf_hdr)) /
420 (uint)sizeof(struct xfs_dir2_leaf_entry);
421 }
422
423 static struct xfs_dir2_leaf_entry *
424 xfs_dir2_leaf_ents_p(struct xfs_dir2_leaf *lp)
425 {
426 return lp->__ents;
427 }
428
429 static int
430 xfs_dir3_max_leaf_ents(struct xfs_da_geometry *geo)
431 {
432 return (geo->blksize - sizeof(struct xfs_dir3_leaf_hdr)) /
433 (uint)sizeof(struct xfs_dir2_leaf_entry);
434 }
435
436 static struct xfs_dir2_leaf_entry *
437 xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
438 {
439 return ((struct xfs_dir3_leaf *)lp)->__ents;
440 }
441
442 static void
443 xfs_dir2_leaf_hdr_from_disk(
444 struct xfs_dir3_icleaf_hdr *to,
445 struct xfs_dir2_leaf *from)
446 {
447 to->forw = be32_to_cpu(from->hdr.info.forw);
448 to->back = be32_to_cpu(from->hdr.info.back);
449 to->magic = be16_to_cpu(from->hdr.info.magic);
450 to->count = be16_to_cpu(from->hdr.count);
451 to->stale = be16_to_cpu(from->hdr.stale);
452
453 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
454 to->magic == XFS_DIR2_LEAFN_MAGIC);
455 }
456
457 static void
458 xfs_dir2_leaf_hdr_to_disk(
459 struct xfs_dir2_leaf *to,
460 struct xfs_dir3_icleaf_hdr *from)
461 {
462 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
463 from->magic == XFS_DIR2_LEAFN_MAGIC);
464
465 to->hdr.info.forw = cpu_to_be32(from->forw);
466 to->hdr.info.back = cpu_to_be32(from->back);
467 to->hdr.info.magic = cpu_to_be16(from->magic);
468 to->hdr.count = cpu_to_be16(from->count);
469 to->hdr.stale = cpu_to_be16(from->stale);
470 }
471
472 static void
473 xfs_dir3_leaf_hdr_from_disk(
474 struct xfs_dir3_icleaf_hdr *to,
475 struct xfs_dir2_leaf *from)
476 {
477 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
478
479 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
480 to->back = be32_to_cpu(hdr3->info.hdr.back);
481 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
482 to->count = be16_to_cpu(hdr3->count);
483 to->stale = be16_to_cpu(hdr3->stale);
484
485 ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
486 to->magic == XFS_DIR3_LEAFN_MAGIC);
487 }
488
489 static void
490 xfs_dir3_leaf_hdr_to_disk(
491 struct xfs_dir2_leaf *to,
492 struct xfs_dir3_icleaf_hdr *from)
493 {
494 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
495
496 ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
497 from->magic == XFS_DIR3_LEAFN_MAGIC);
498
499 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
500 hdr3->info.hdr.back = cpu_to_be32(from->back);
501 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
502 hdr3->count = cpu_to_be16(from->count);
503 hdr3->stale = cpu_to_be16(from->stale);
504 }
505
506
507 /*
508 * Directory/Attribute Node block operations
509 */
510 static struct xfs_da_node_entry *
511 xfs_da2_node_tree_p(struct xfs_da_intnode *dap)
512 {
513 return dap->__btree;
514 }
515
516 static struct xfs_da_node_entry *
517 xfs_da3_node_tree_p(struct xfs_da_intnode *dap)
518 {
519 return ((struct xfs_da3_intnode *)dap)->__btree;
520 }
521
522 static void
523 xfs_da2_node_hdr_from_disk(
524 struct xfs_da3_icnode_hdr *to,
525 struct xfs_da_intnode *from)
526 {
527 ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
528 to->forw = be32_to_cpu(from->hdr.info.forw);
529 to->back = be32_to_cpu(from->hdr.info.back);
530 to->magic = be16_to_cpu(from->hdr.info.magic);
531 to->count = be16_to_cpu(from->hdr.__count);
532 to->level = be16_to_cpu(from->hdr.__level);
533 }
534
535 static void
536 xfs_da2_node_hdr_to_disk(
537 struct xfs_da_intnode *to,
538 struct xfs_da3_icnode_hdr *from)
539 {
540 ASSERT(from->magic == XFS_DA_NODE_MAGIC);
541 to->hdr.info.forw = cpu_to_be32(from->forw);
542 to->hdr.info.back = cpu_to_be32(from->back);
543 to->hdr.info.magic = cpu_to_be16(from->magic);
544 to->hdr.__count = cpu_to_be16(from->count);
545 to->hdr.__level = cpu_to_be16(from->level);
546 }
547
548 static void
549 xfs_da3_node_hdr_from_disk(
550 struct xfs_da3_icnode_hdr *to,
551 struct xfs_da_intnode *from)
552 {
553 struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)from;
554
555 ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
556 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
557 to->back = be32_to_cpu(hdr3->info.hdr.back);
558 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
559 to->count = be16_to_cpu(hdr3->__count);
560 to->level = be16_to_cpu(hdr3->__level);
561 }
562
563 static void
564 xfs_da3_node_hdr_to_disk(
565 struct xfs_da_intnode *to,
566 struct xfs_da3_icnode_hdr *from)
567 {
568 struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)to;
569
570 ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
571 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
572 hdr3->info.hdr.back = cpu_to_be32(from->back);
573 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
574 hdr3->__count = cpu_to_be16(from->count);
575 hdr3->__level = cpu_to_be16(from->level);
576 }
577
578
579 /*
580 * Directory free space block operations
581 */
582 static int
583 xfs_dir2_free_max_bests(struct xfs_da_geometry *geo)
584 {
585 return (geo->blksize - sizeof(struct xfs_dir2_free_hdr)) /
586 sizeof(xfs_dir2_data_off_t);
587 }
588
589 static __be16 *
590 xfs_dir2_free_bests_p(struct xfs_dir2_free *free)
591 {
592 return (__be16 *)((char *)free + sizeof(struct xfs_dir2_free_hdr));
593 }
594
595 /*
596 * Convert data space db to the corresponding free db.
597 */
598 static xfs_dir2_db_t
599 xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
600 {
601 return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
602 (db / xfs_dir2_free_max_bests(geo));
603 }
604
605 /*
606 * Convert data space db to the corresponding index in a free db.
607 */
608 static int
609 xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
610 {
611 return db % xfs_dir2_free_max_bests(geo);
612 }
613
614 static int
615 xfs_dir3_free_max_bests(struct xfs_da_geometry *geo)
616 {
617 return (geo->blksize - sizeof(struct xfs_dir3_free_hdr)) /
618 sizeof(xfs_dir2_data_off_t);
619 }
620
621 static __be16 *
622 xfs_dir3_free_bests_p(struct xfs_dir2_free *free)
623 {
624 return (__be16 *)((char *)free + sizeof(struct xfs_dir3_free_hdr));
625 }
626
627 /*
628 * Convert data space db to the corresponding free db.
629 */
630 static xfs_dir2_db_t
631 xfs_dir3_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
632 {
633 return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
634 (db / xfs_dir3_free_max_bests(geo));
635 }
636
637 /*
638 * Convert data space db to the corresponding index in a free db.
639 */
640 static int
641 xfs_dir3_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
642 {
643 return db % xfs_dir3_free_max_bests(geo);
644 }
645
646 static void
647 xfs_dir2_free_hdr_from_disk(
648 struct xfs_dir3_icfree_hdr *to,
649 struct xfs_dir2_free *from)
650 {
651 to->magic = be32_to_cpu(from->hdr.magic);
652 to->firstdb = be32_to_cpu(from->hdr.firstdb);
653 to->nvalid = be32_to_cpu(from->hdr.nvalid);
654 to->nused = be32_to_cpu(from->hdr.nused);
655 ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
656 }
657
658 static void
659 xfs_dir2_free_hdr_to_disk(
660 struct xfs_dir2_free *to,
661 struct xfs_dir3_icfree_hdr *from)
662 {
663 ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
664
665 to->hdr.magic = cpu_to_be32(from->magic);
666 to->hdr.firstdb = cpu_to_be32(from->firstdb);
667 to->hdr.nvalid = cpu_to_be32(from->nvalid);
668 to->hdr.nused = cpu_to_be32(from->nused);
669 }
670
671 static void
672 xfs_dir3_free_hdr_from_disk(
673 struct xfs_dir3_icfree_hdr *to,
674 struct xfs_dir2_free *from)
675 {
676 struct xfs_dir3_free_hdr *hdr3 = (struct xfs_dir3_free_hdr *)from;
677
678 to->magic = be32_to_cpu(hdr3->hdr.magic);
679 to->firstdb = be32_to_cpu(hdr3->firstdb);
680 to->nvalid = be32_to_cpu(hdr3->nvalid);
681 to->nused = be32_to_cpu(hdr3->nused);
682
683 ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
684 }
685
686 static void
687 xfs_dir3_free_hdr_to_disk(
688 struct xfs_dir2_free *to,
689 struct xfs_dir3_icfree_hdr *from)
690 {
691 struct xfs_dir3_free_hdr *hdr3 = (struct xfs_dir3_free_hdr *)to;
692
693 ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
694
695 hdr3->hdr.magic = cpu_to_be32(from->magic);
696 hdr3->firstdb = cpu_to_be32(from->firstdb);
697 hdr3->nvalid = cpu_to_be32(from->nvalid);
698 hdr3->nused = cpu_to_be32(from->nused);
699 }
700
701 static const struct xfs_dir_ops xfs_dir2_ops = {
702 .sf_entsize = xfs_dir2_sf_entsize,
703 .sf_nextentry = xfs_dir2_sf_nextentry,
704 .sf_get_ftype = xfs_dir2_sfe_get_ftype,
705 .sf_put_ftype = xfs_dir2_sfe_put_ftype,
706 .sf_get_ino = xfs_dir2_sfe_get_ino,
707 .sf_put_ino = xfs_dir2_sfe_put_ino,
708 .sf_get_parent_ino = xfs_dir2_sf_get_parent_ino,
709 .sf_put_parent_ino = xfs_dir2_sf_put_parent_ino,
710
711 .data_entsize = xfs_dir2_data_entsize,
712 .data_get_ftype = xfs_dir2_data_get_ftype,
713 .data_put_ftype = xfs_dir2_data_put_ftype,
714 .data_entry_tag_p = xfs_dir2_data_entry_tag_p,
715 .data_bestfree_p = xfs_dir2_data_bestfree_p,
716
717 .data_dot_offset = sizeof(struct xfs_dir2_data_hdr),
718 .data_dotdot_offset = sizeof(struct xfs_dir2_data_hdr) +
719 XFS_DIR2_DATA_ENTSIZE(1),
720 .data_first_offset = sizeof(struct xfs_dir2_data_hdr) +
721 XFS_DIR2_DATA_ENTSIZE(1) +
722 XFS_DIR2_DATA_ENTSIZE(2),
723 .data_entry_offset = sizeof(struct xfs_dir2_data_hdr),
724
725 .data_dot_entry_p = xfs_dir2_data_dot_entry_p,
726 .data_dotdot_entry_p = xfs_dir2_data_dotdot_entry_p,
727 .data_first_entry_p = xfs_dir2_data_first_entry_p,
728 .data_entry_p = xfs_dir2_data_entry_p,
729 .data_unused_p = xfs_dir2_data_unused_p,
730
731 .leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr),
732 .leaf_hdr_to_disk = xfs_dir2_leaf_hdr_to_disk,
733 .leaf_hdr_from_disk = xfs_dir2_leaf_hdr_from_disk,
734 .leaf_max_ents = xfs_dir2_max_leaf_ents,
735 .leaf_ents_p = xfs_dir2_leaf_ents_p,
736
737 .node_hdr_size = sizeof(struct xfs_da_node_hdr),
738 .node_hdr_to_disk = xfs_da2_node_hdr_to_disk,
739 .node_hdr_from_disk = xfs_da2_node_hdr_from_disk,
740 .node_tree_p = xfs_da2_node_tree_p,
741
742 .free_hdr_size = sizeof(struct xfs_dir2_free_hdr),
743 .free_hdr_to_disk = xfs_dir2_free_hdr_to_disk,
744 .free_hdr_from_disk = xfs_dir2_free_hdr_from_disk,
745 .free_max_bests = xfs_dir2_free_max_bests,
746 .free_bests_p = xfs_dir2_free_bests_p,
747 .db_to_fdb = xfs_dir2_db_to_fdb,
748 .db_to_fdindex = xfs_dir2_db_to_fdindex,
749 };
750
751 static const struct xfs_dir_ops xfs_dir2_ftype_ops = {
752 .sf_entsize = xfs_dir3_sf_entsize,
753 .sf_nextentry = xfs_dir3_sf_nextentry,
754 .sf_get_ftype = xfs_dir3_sfe_get_ftype,
755 .sf_put_ftype = xfs_dir3_sfe_put_ftype,
756 .sf_get_ino = xfs_dir3_sfe_get_ino,
757 .sf_put_ino = xfs_dir3_sfe_put_ino,
758 .sf_get_parent_ino = xfs_dir2_sf_get_parent_ino,
759 .sf_put_parent_ino = xfs_dir2_sf_put_parent_ino,
760
761 .data_entsize = xfs_dir3_data_entsize,
762 .data_get_ftype = xfs_dir3_data_get_ftype,
763 .data_put_ftype = xfs_dir3_data_put_ftype,
764 .data_entry_tag_p = xfs_dir3_data_entry_tag_p,
765 .data_bestfree_p = xfs_dir2_data_bestfree_p,
766
767 .data_dot_offset = sizeof(struct xfs_dir2_data_hdr),
768 .data_dotdot_offset = sizeof(struct xfs_dir2_data_hdr) +
769 XFS_DIR3_DATA_ENTSIZE(1),
770 .data_first_offset = sizeof(struct xfs_dir2_data_hdr) +
771 XFS_DIR3_DATA_ENTSIZE(1) +
772 XFS_DIR3_DATA_ENTSIZE(2),
773 .data_entry_offset = sizeof(struct xfs_dir2_data_hdr),
774
775 .data_dot_entry_p = xfs_dir2_data_dot_entry_p,
776 .data_dotdot_entry_p = xfs_dir2_ftype_data_dotdot_entry_p,
777 .data_first_entry_p = xfs_dir2_ftype_data_first_entry_p,
778 .data_entry_p = xfs_dir2_data_entry_p,
779 .data_unused_p = xfs_dir2_data_unused_p,
780
781 .leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr),
782 .leaf_hdr_to_disk = xfs_dir2_leaf_hdr_to_disk,
783 .leaf_hdr_from_disk = xfs_dir2_leaf_hdr_from_disk,
784 .leaf_max_ents = xfs_dir2_max_leaf_ents,
785 .leaf_ents_p = xfs_dir2_leaf_ents_p,
786
787 .node_hdr_size = sizeof(struct xfs_da_node_hdr),
788 .node_hdr_to_disk = xfs_da2_node_hdr_to_disk,
789 .node_hdr_from_disk = xfs_da2_node_hdr_from_disk,
790 .node_tree_p = xfs_da2_node_tree_p,
791
792 .free_hdr_size = sizeof(struct xfs_dir2_free_hdr),
793 .free_hdr_to_disk = xfs_dir2_free_hdr_to_disk,
794 .free_hdr_from_disk = xfs_dir2_free_hdr_from_disk,
795 .free_max_bests = xfs_dir2_free_max_bests,
796 .free_bests_p = xfs_dir2_free_bests_p,
797 .db_to_fdb = xfs_dir2_db_to_fdb,
798 .db_to_fdindex = xfs_dir2_db_to_fdindex,
799 };
800
801 static const struct xfs_dir_ops xfs_dir3_ops = {
802 .sf_entsize = xfs_dir3_sf_entsize,
803 .sf_nextentry = xfs_dir3_sf_nextentry,
804 .sf_get_ftype = xfs_dir3_sfe_get_ftype,
805 .sf_put_ftype = xfs_dir3_sfe_put_ftype,
806 .sf_get_ino = xfs_dir3_sfe_get_ino,
807 .sf_put_ino = xfs_dir3_sfe_put_ino,
808 .sf_get_parent_ino = xfs_dir2_sf_get_parent_ino,
809 .sf_put_parent_ino = xfs_dir2_sf_put_parent_ino,
810
811 .data_entsize = xfs_dir3_data_entsize,
812 .data_get_ftype = xfs_dir3_data_get_ftype,
813 .data_put_ftype = xfs_dir3_data_put_ftype,
814 .data_entry_tag_p = xfs_dir3_data_entry_tag_p,
815 .data_bestfree_p = xfs_dir3_data_bestfree_p,
816
817 .data_dot_offset = sizeof(struct xfs_dir3_data_hdr),
818 .data_dotdot_offset = sizeof(struct xfs_dir3_data_hdr) +
819 XFS_DIR3_DATA_ENTSIZE(1),
820 .data_first_offset = sizeof(struct xfs_dir3_data_hdr) +
821 XFS_DIR3_DATA_ENTSIZE(1) +
822 XFS_DIR3_DATA_ENTSIZE(2),
823 .data_entry_offset = sizeof(struct xfs_dir3_data_hdr),
824
825 .data_dot_entry_p = xfs_dir3_data_dot_entry_p,
826 .data_dotdot_entry_p = xfs_dir3_data_dotdot_entry_p,
827 .data_first_entry_p = xfs_dir3_data_first_entry_p,
828 .data_entry_p = xfs_dir3_data_entry_p,
829 .data_unused_p = xfs_dir3_data_unused_p,
830
831 .leaf_hdr_size = sizeof(struct xfs_dir3_leaf_hdr),
832 .leaf_hdr_to_disk = xfs_dir3_leaf_hdr_to_disk,
833 .leaf_hdr_from_disk = xfs_dir3_leaf_hdr_from_disk,
834 .leaf_max_ents = xfs_dir3_max_leaf_ents,
835 .leaf_ents_p = xfs_dir3_leaf_ents_p,
836
837 .node_hdr_size = sizeof(struct xfs_da3_node_hdr),
838 .node_hdr_to_disk = xfs_da3_node_hdr_to_disk,
839 .node_hdr_from_disk = xfs_da3_node_hdr_from_disk,
840 .node_tree_p = xfs_da3_node_tree_p,
841
842 .free_hdr_size = sizeof(struct xfs_dir3_free_hdr),
843 .free_hdr_to_disk = xfs_dir3_free_hdr_to_disk,
844 .free_hdr_from_disk = xfs_dir3_free_hdr_from_disk,
845 .free_max_bests = xfs_dir3_free_max_bests,
846 .free_bests_p = xfs_dir3_free_bests_p,
847 .db_to_fdb = xfs_dir3_db_to_fdb,
848 .db_to_fdindex = xfs_dir3_db_to_fdindex,
849 };
850
851 static const struct xfs_dir_ops xfs_dir2_nondir_ops = {
852 .node_hdr_size = sizeof(struct xfs_da_node_hdr),
853 .node_hdr_to_disk = xfs_da2_node_hdr_to_disk,
854 .node_hdr_from_disk = xfs_da2_node_hdr_from_disk,
855 .node_tree_p = xfs_da2_node_tree_p,
856 };
857
858 static const struct xfs_dir_ops xfs_dir3_nondir_ops = {
859 .node_hdr_size = sizeof(struct xfs_da3_node_hdr),
860 .node_hdr_to_disk = xfs_da3_node_hdr_to_disk,
861 .node_hdr_from_disk = xfs_da3_node_hdr_from_disk,
862 .node_tree_p = xfs_da3_node_tree_p,
863 };
864
865 /*
866 * Return the ops structure according to the current config. If we are passed
867 * an inode, then that overrides the default config we use which is based on
868 * feature bits.
869 */
870 const struct xfs_dir_ops *
871 xfs_dir_get_ops(
872 struct xfs_mount *mp,
873 struct xfs_inode *dp)
874 {
875 if (dp)
876 return dp->d_ops;
877 if (mp->m_dir_inode_ops)
878 return mp->m_dir_inode_ops;
879 if (xfs_sb_version_hascrc(&mp->m_sb))
880 return &xfs_dir3_ops;
881 if (xfs_sb_version_hasftype(&mp->m_sb))
882 return &xfs_dir2_ftype_ops;
883 return &xfs_dir2_ops;
884 }
885
886 const struct xfs_dir_ops *
887 xfs_nondir_get_ops(
888 struct xfs_mount *mp,
889 struct xfs_inode *dp)
890 {
891 if (dp)
892 return dp->d_ops;
893 if (mp->m_nondir_inode_ops)
894 return mp->m_nondir_inode_ops;
895 if (xfs_sb_version_hascrc(&mp->m_sb))
896 return &xfs_dir3_nondir_ops;
897 return &xfs_dir2_nondir_ops;
898 }