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