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