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