xref: /linux/fs/xfs/libxfs/xfs_attr_leaf.c (revision f0100363d8c374bd8e9ea7c9ba02744f0b802ca4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * Copyright (c) 2013 Red Hat, Inc.
5  * All Rights Reserved.
6  */
7 #include "xfs_platform.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_sb.h"
14 #include "xfs_mount.h"
15 #include "xfs_da_format.h"
16 #include "xfs_da_btree.h"
17 #include "xfs_inode.h"
18 #include "xfs_trans.h"
19 #include "xfs_bmap_btree.h"
20 #include "xfs_bmap.h"
21 #include "xfs_attr_sf.h"
22 #include "xfs_attr.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_attr_leaf.h"
25 #include "xfs_error.h"
26 #include "xfs_trace.h"
27 #include "xfs_buf_item.h"
28 #include "xfs_dir2.h"
29 #include "xfs_log.h"
30 #include "xfs_ag.h"
31 #include "xfs_errortag.h"
32 #include "xfs_health.h"
33 
34 
35 /*
36  * xfs_attr_leaf.c
37  *
38  * Routines to implement leaf blocks of attributes as Btrees of hashed names.
39  */
40 
41 /*========================================================================
42  * Function prototypes for the kernel.
43  *========================================================================*/
44 
45 /*
46  * Routines used for growing the Btree.
47  */
48 STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args,
49 				 xfs_dablk_t which_block, struct xfs_buf **bpp);
50 STATIC void xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer,
51 				   struct xfs_attr3_icleaf_hdr *ichdr,
52 				   struct xfs_da_args *args, int freemap_index);
53 STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args,
54 				   struct xfs_attr3_icleaf_hdr *ichdr,
55 				   struct xfs_buf *leaf_buffer);
56 STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state,
57 						   xfs_da_state_blk_t *blk1,
58 						   xfs_da_state_blk_t *blk2);
59 STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
60 			xfs_da_state_blk_t *leaf_blk_1,
61 			struct xfs_attr3_icleaf_hdr *ichdr1,
62 			xfs_da_state_blk_t *leaf_blk_2,
63 			struct xfs_attr3_icleaf_hdr *ichdr2,
64 			int *number_entries_in_blk1,
65 			int *number_usedbytes_in_blk1);
66 
67 /*
68  * Utility routines.
69  */
70 STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
71 			struct xfs_attr_leafblock *src_leaf,
72 			struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
73 			struct xfs_attr_leafblock *dst_leaf,
74 			struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
75 			int move_count);
76 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
77 
78 /* Compute the byte offset of the end of the leaf entry array. */
79 static inline int
xfs_attr_leaf_entries_end(unsigned int hdrcount,const struct xfs_attr_leafblock * leaf)80 xfs_attr_leaf_entries_end(
81 	unsigned int			hdrcount,
82 	const struct xfs_attr_leafblock	*leaf)
83 {
84 	return hdrcount * sizeof(struct xfs_attr_leaf_entry) +
85 			xfs_attr3_leaf_hdr_size(leaf);
86 }
87 
88 static inline bool
ichdr_freemaps_overlap(const struct xfs_attr3_icleaf_hdr * ichdr,unsigned int x,unsigned int y)89 ichdr_freemaps_overlap(
90 	const struct xfs_attr3_icleaf_hdr	*ichdr,
91 	unsigned int				x,
92 	unsigned int				y)
93 {
94 	const unsigned int			xend =
95 		ichdr->freemap[x].base + ichdr->freemap[x].size;
96 	const unsigned int			yend =
97 		ichdr->freemap[y].base + ichdr->freemap[y].size;
98 
99 	/* empty slots do not overlap */
100 	if (!ichdr->freemap[x].size || !ichdr->freemap[y].size)
101 		return false;
102 
103 	return ichdr->freemap[x].base < yend && xend > ichdr->freemap[y].base;
104 }
105 
106 static inline xfs_failaddr_t
xfs_attr_leaf_ichdr_freemaps_verify(const struct xfs_attr3_icleaf_hdr * ichdr,const struct xfs_attr_leafblock * leaf)107 xfs_attr_leaf_ichdr_freemaps_verify(
108 	const struct xfs_attr3_icleaf_hdr	*ichdr,
109 	const struct xfs_attr_leafblock		*leaf)
110 {
111 	unsigned int				entries_end =
112 		xfs_attr_leaf_entries_end(ichdr->count, leaf);
113 	int					i;
114 
115 	if (ichdr_freemaps_overlap(ichdr, 0, 1))
116 		return __this_address;
117 	if (ichdr_freemaps_overlap(ichdr, 0, 2))
118 		return __this_address;
119 	if (ichdr_freemaps_overlap(ichdr, 1, 2))
120 		return __this_address;
121 
122 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
123 		if (ichdr->freemap[i].size > 0 &&
124 		    ichdr->freemap[i].base < entries_end)
125 			return __this_address;
126 	}
127 
128 	return NULL;
129 }
130 
131 /*
132  * attr3 block 'firstused' conversion helpers.
133  *
134  * firstused refers to the offset of the first used byte of the nameval region
135  * of an attr leaf block. The region starts at the tail of the block and expands
136  * backwards towards the middle. As such, firstused is initialized to the block
137  * size for an empty leaf block and is reduced from there.
138  *
139  * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
140  * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
141  * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
142  * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
143  * the attr block size. The following helpers manage the conversion between the
144  * in-core and on-disk formats.
145  */
146 
147 static void
xfs_attr3_leaf_firstused_from_disk(struct xfs_da_geometry * geo,struct xfs_attr3_icleaf_hdr * to,struct xfs_attr_leafblock * from)148 xfs_attr3_leaf_firstused_from_disk(
149 	struct xfs_da_geometry		*geo,
150 	struct xfs_attr3_icleaf_hdr	*to,
151 	struct xfs_attr_leafblock	*from)
152 {
153 	struct xfs_attr3_leaf_hdr	*hdr3;
154 
155 	if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
156 		hdr3 = (struct xfs_attr3_leaf_hdr *) from;
157 		to->firstused = be16_to_cpu(hdr3->firstused);
158 	} else {
159 		to->firstused = be16_to_cpu(from->hdr.firstused);
160 	}
161 
162 	/*
163 	 * Convert from the magic fsb size value to actual blocksize. This
164 	 * should only occur for empty blocks when the block size overflows
165 	 * 16-bits.
166 	 */
167 	if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) {
168 		ASSERT(!to->count && !to->usedbytes);
169 		ASSERT(geo->blksize > USHRT_MAX);
170 		to->firstused = geo->blksize;
171 	}
172 }
173 
174 static void
xfs_attr3_leaf_firstused_to_disk(struct xfs_da_geometry * geo,struct xfs_attr_leafblock * to,struct xfs_attr3_icleaf_hdr * from)175 xfs_attr3_leaf_firstused_to_disk(
176 	struct xfs_da_geometry		*geo,
177 	struct xfs_attr_leafblock	*to,
178 	struct xfs_attr3_icleaf_hdr	*from)
179 {
180 	struct xfs_attr3_leaf_hdr	*hdr3;
181 	uint32_t			firstused;
182 
183 	/* magic value should only be seen on disk */
184 	ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF);
185 
186 	/*
187 	 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
188 	 * value. This only overflows at the max supported value of 64k. Use the
189 	 * magic on-disk value to represent block size in this case.
190 	 */
191 	firstused = from->firstused;
192 	if (firstused > USHRT_MAX) {
193 		ASSERT(from->firstused == geo->blksize);
194 		firstused = XFS_ATTR3_LEAF_NULLOFF;
195 	}
196 
197 	if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
198 		hdr3 = (struct xfs_attr3_leaf_hdr *) to;
199 		hdr3->firstused = cpu_to_be16(firstused);
200 	} else {
201 		to->hdr.firstused = cpu_to_be16(firstused);
202 	}
203 }
204 
205 void
xfs_attr3_leaf_hdr_from_disk(struct xfs_da_geometry * geo,struct xfs_attr3_icleaf_hdr * to,struct xfs_attr_leafblock * from)206 xfs_attr3_leaf_hdr_from_disk(
207 	struct xfs_da_geometry		*geo,
208 	struct xfs_attr3_icleaf_hdr	*to,
209 	struct xfs_attr_leafblock	*from)
210 {
211 	int	i;
212 
213 	ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
214 	       from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
215 
216 	if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
217 		struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from;
218 
219 		to->forw = be32_to_cpu(hdr3->info.hdr.forw);
220 		to->back = be32_to_cpu(hdr3->info.hdr.back);
221 		to->magic = be16_to_cpu(hdr3->info.hdr.magic);
222 		to->count = be16_to_cpu(hdr3->count);
223 		to->usedbytes = be16_to_cpu(hdr3->usedbytes);
224 		xfs_attr3_leaf_firstused_from_disk(geo, to, from);
225 		to->holes = hdr3->holes;
226 
227 		for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
228 			to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base);
229 			to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size);
230 		}
231 		return;
232 	}
233 	to->forw = be32_to_cpu(from->hdr.info.forw);
234 	to->back = be32_to_cpu(from->hdr.info.back);
235 	to->magic = be16_to_cpu(from->hdr.info.magic);
236 	to->count = be16_to_cpu(from->hdr.count);
237 	to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
238 	xfs_attr3_leaf_firstused_from_disk(geo, to, from);
239 	to->holes = from->hdr.holes;
240 
241 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
242 		to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base);
243 		to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size);
244 	}
245 }
246 
247 void
xfs_attr3_leaf_hdr_to_disk(struct xfs_da_geometry * geo,struct xfs_attr_leafblock * to,struct xfs_attr3_icleaf_hdr * from)248 xfs_attr3_leaf_hdr_to_disk(
249 	struct xfs_da_geometry		*geo,
250 	struct xfs_attr_leafblock	*to,
251 	struct xfs_attr3_icleaf_hdr	*from)
252 {
253 	int				i;
254 
255 	ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
256 	       from->magic == XFS_ATTR3_LEAF_MAGIC);
257 
258 	if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
259 		struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to;
260 
261 		hdr3->info.hdr.forw = cpu_to_be32(from->forw);
262 		hdr3->info.hdr.back = cpu_to_be32(from->back);
263 		hdr3->info.hdr.magic = cpu_to_be16(from->magic);
264 		hdr3->count = cpu_to_be16(from->count);
265 		hdr3->usedbytes = cpu_to_be16(from->usedbytes);
266 		xfs_attr3_leaf_firstused_to_disk(geo, to, from);
267 		hdr3->holes = from->holes;
268 		hdr3->pad1 = 0;
269 
270 		for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
271 			hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base);
272 			hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size);
273 		}
274 
275 		ASSERT(xfs_attr_leaf_ichdr_freemaps_verify(from, to) == NULL);
276 		return;
277 	}
278 	to->hdr.info.forw = cpu_to_be32(from->forw);
279 	to->hdr.info.back = cpu_to_be32(from->back);
280 	to->hdr.info.magic = cpu_to_be16(from->magic);
281 	to->hdr.count = cpu_to_be16(from->count);
282 	to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
283 	xfs_attr3_leaf_firstused_to_disk(geo, to, from);
284 	to->hdr.holes = from->holes;
285 	to->hdr.pad1 = 0;
286 
287 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
288 		to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base);
289 		to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size);
290 	}
291 
292 	ASSERT(xfs_attr_leaf_ichdr_freemaps_verify(from, to) == NULL);
293 }
294 
295 static xfs_failaddr_t
xfs_attr3_leaf_verify_entry(struct xfs_mount * mp,char * buf_end,struct xfs_attr_leafblock * leaf,struct xfs_attr3_icleaf_hdr * leafhdr,struct xfs_attr_leaf_entry * ent,int idx,__u32 * last_hashval)296 xfs_attr3_leaf_verify_entry(
297 	struct xfs_mount			*mp,
298 	char					*buf_end,
299 	struct xfs_attr_leafblock		*leaf,
300 	struct xfs_attr3_icleaf_hdr		*leafhdr,
301 	struct xfs_attr_leaf_entry		*ent,
302 	int					idx,
303 	__u32					*last_hashval)
304 {
305 	struct xfs_attr_leaf_name_local		*lentry;
306 	struct xfs_attr_leaf_name_remote	*rentry;
307 	char					*name_end;
308 	unsigned int				nameidx;
309 	unsigned int				namesize;
310 	__u32					hashval;
311 
312 	/* hash order check */
313 	hashval = be32_to_cpu(ent->hashval);
314 	if (hashval < *last_hashval)
315 		return __this_address;
316 	*last_hashval = hashval;
317 
318 	nameidx = be16_to_cpu(ent->nameidx);
319 	if (nameidx < leafhdr->firstused || nameidx >= mp->m_attr_geo->blksize)
320 		return __this_address;
321 
322 	/*
323 	 * Check the name information.  The namelen fields are u8 so we can't
324 	 * possibly exceed the maximum name length of 255 bytes.
325 	 */
326 	if (ent->flags & XFS_ATTR_LOCAL) {
327 		lentry = xfs_attr3_leaf_name_local(leaf, idx);
328 
329 		/* Validate lentry pointer is within bounds before field access */
330 		if ((char *)lentry >= buf_end)
331 			return __this_address;
332 		if ((char *)lentry + offsetof(struct xfs_attr_leaf_name_local, nameval) > buf_end)
333 			return __this_address;
334 
335 		namesize = xfs_attr_leaf_entsize_local(lentry->namelen,
336 				be16_to_cpu(lentry->valuelen));
337 		name_end = (char *)lentry + namesize;
338 		if (lentry->namelen == 0)
339 			return __this_address;
340 	} else {
341 		rentry = xfs_attr3_leaf_name_remote(leaf, idx);
342 
343 		/* Validate rentry pointer is within bounds before field access */
344 		if ((char *)rentry >= buf_end)
345 			return __this_address;
346 		if ((char *)rentry + offsetof(struct xfs_attr_leaf_name_remote, name) > buf_end)
347 			return __this_address;
348 
349 		namesize = xfs_attr_leaf_entsize_remote(rentry->namelen);
350 		name_end = (char *)rentry + namesize;
351 		if (rentry->namelen == 0)
352 			return __this_address;
353 		if (!(ent->flags & XFS_ATTR_INCOMPLETE) &&
354 		    rentry->valueblk == 0)
355 			return __this_address;
356 	}
357 
358 	if (name_end > buf_end)
359 		return __this_address;
360 
361 	return NULL;
362 }
363 
364 /*
365  * Validate an attribute leaf block.
366  *
367  * Empty leaf blocks can occur under the following circumstances:
368  *
369  * 1. setxattr adds a new extended attribute to a file;
370  * 2. The file has zero existing attributes;
371  * 3. The attribute is too large to fit in the attribute fork;
372  * 4. The attribute is small enough to fit in a leaf block;
373  * 5. A log flush occurs after committing the transaction that creates
374  *    the (empty) leaf block; and
375  * 6. The filesystem goes down after the log flush but before the new
376  *    attribute can be committed to the leaf block.
377  *
378  * Hence we need to ensure that we don't fail the validation purely
379  * because the leaf is empty.
380  */
381 static xfs_failaddr_t
xfs_attr3_leaf_verify(struct xfs_buf * bp)382 xfs_attr3_leaf_verify(
383 	struct xfs_buf			*bp)
384 {
385 	struct xfs_attr3_icleaf_hdr	ichdr;
386 	struct xfs_mount		*mp = bp->b_mount;
387 	struct xfs_attr_leafblock	*leaf = bp->b_addr;
388 	struct xfs_attr_leaf_entry	*entries;
389 	struct xfs_attr_leaf_entry	*ent;
390 	char				*buf_end;
391 	uint32_t			end;	/* must be 32bit - see below */
392 	__u32				last_hashval = 0;
393 	int				i;
394 	xfs_failaddr_t			fa;
395 
396 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
397 
398 	fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
399 	if (fa)
400 		return fa;
401 
402 	/*
403 	 * firstused is the block offset of the first name info structure.
404 	 * Make sure it doesn't go off the block or crash into the header.
405 	 */
406 	if (ichdr.firstused > mp->m_attr_geo->blksize)
407 		return __this_address;
408 	if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf))
409 		return __this_address;
410 
411 	/* Make sure the entries array doesn't crash into the name info. */
412 	entries = xfs_attr3_leaf_entryp(bp->b_addr);
413 	if ((char *)&entries[ichdr.count] >
414 	    (char *)bp->b_addr + ichdr.firstused)
415 		return __this_address;
416 
417 	/*
418 	 * NOTE: This verifier historically failed empty leaf buffers because
419 	 * we expect the fork to be in another format. Empty attr fork format
420 	 * conversions are possible during xattr set, however, and format
421 	 * conversion is not atomic with the xattr set that triggers it. We
422 	 * cannot assume leaf blocks are non-empty until that is addressed.
423 	*/
424 	buf_end = (char *)bp->b_addr + mp->m_attr_geo->blksize;
425 	for (i = 0, ent = entries; i < ichdr.count; ent++, i++) {
426 		fa = xfs_attr3_leaf_verify_entry(mp, buf_end, leaf, &ichdr,
427 				ent, i, &last_hashval);
428 		if (fa)
429 			return fa;
430 	}
431 
432 	/*
433 	 * Quickly check the freemap information.  Attribute data has to be
434 	 * aligned to 4-byte boundaries, and likewise for the free space.
435 	 *
436 	 * Note that for 64k block size filesystems, the freemap entries cannot
437 	 * overflow as they are only be16 fields. However, when checking end
438 	 * pointer of the freemap, we have to be careful to detect overflows and
439 	 * so use uint32_t for those checks.
440 	 */
441 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
442 		if (ichdr.freemap[i].base > mp->m_attr_geo->blksize)
443 			return __this_address;
444 		if (ichdr.freemap[i].base & 0x3)
445 			return __this_address;
446 		if (ichdr.freemap[i].size > mp->m_attr_geo->blksize)
447 			return __this_address;
448 		if (ichdr.freemap[i].size & 0x3)
449 			return __this_address;
450 
451 		/* be care of 16 bit overflows here */
452 		end = (uint32_t)ichdr.freemap[i].base + ichdr.freemap[i].size;
453 		if (end < ichdr.freemap[i].base)
454 			return __this_address;
455 		if (end > mp->m_attr_geo->blksize)
456 			return __this_address;
457 	}
458 
459 	fa = xfs_attr_leaf_ichdr_freemaps_verify(&ichdr, leaf);
460 	if (fa)
461 		return fa;
462 
463 	return NULL;
464 }
465 
466 xfs_failaddr_t
xfs_attr3_leaf_header_check(struct xfs_buf * bp,xfs_ino_t owner)467 xfs_attr3_leaf_header_check(
468 	struct xfs_buf		*bp,
469 	xfs_ino_t		owner)
470 {
471 	struct xfs_mount	*mp = bp->b_mount;
472 
473 	if (xfs_has_crc(mp)) {
474 		struct xfs_attr3_leafblock *hdr3 = bp->b_addr;
475 
476 		if (hdr3->hdr.info.hdr.magic !=
477 				cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
478 			return __this_address;
479 
480 		if (be64_to_cpu(hdr3->hdr.info.owner) != owner)
481 			return __this_address;
482 	}
483 
484 	return NULL;
485 }
486 
487 static void
xfs_attr3_leaf_write_verify(struct xfs_buf * bp)488 xfs_attr3_leaf_write_verify(
489 	struct xfs_buf	*bp)
490 {
491 	struct xfs_mount	*mp = bp->b_mount;
492 	struct xfs_buf_log_item	*bip = bp->b_log_item;
493 	struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
494 	xfs_failaddr_t		fa;
495 
496 	fa = xfs_attr3_leaf_verify(bp);
497 	if (fa) {
498 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
499 		return;
500 	}
501 
502 	if (!xfs_has_crc(mp))
503 		return;
504 
505 	if (bip)
506 		hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
507 
508 	xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF);
509 }
510 
511 /*
512  * leaf/node format detection on trees is sketchy, so a node read can be done on
513  * leaf level blocks when detection identifies the tree as a node format tree
514  * incorrectly. In this case, we need to swap the verifier to match the correct
515  * format of the block being read.
516  */
517 static void
xfs_attr3_leaf_read_verify(struct xfs_buf * bp)518 xfs_attr3_leaf_read_verify(
519 	struct xfs_buf		*bp)
520 {
521 	struct xfs_mount	*mp = bp->b_mount;
522 	xfs_failaddr_t		fa;
523 
524 	if (xfs_has_crc(mp) &&
525 	     !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF))
526 		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
527 	else {
528 		fa = xfs_attr3_leaf_verify(bp);
529 		if (fa)
530 			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
531 	}
532 }
533 
534 const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
535 	.name = "xfs_attr3_leaf",
536 	.magic16 = { cpu_to_be16(XFS_ATTR_LEAF_MAGIC),
537 		     cpu_to_be16(XFS_ATTR3_LEAF_MAGIC) },
538 	.verify_read = xfs_attr3_leaf_read_verify,
539 	.verify_write = xfs_attr3_leaf_write_verify,
540 	.verify_struct = xfs_attr3_leaf_verify,
541 };
542 
543 int
xfs_attr3_leaf_read(struct xfs_trans * tp,struct xfs_inode * dp,xfs_ino_t owner,xfs_dablk_t bno,struct xfs_buf ** bpp)544 xfs_attr3_leaf_read(
545 	struct xfs_trans	*tp,
546 	struct xfs_inode	*dp,
547 	xfs_ino_t		owner,
548 	xfs_dablk_t		bno,
549 	struct xfs_buf		**bpp)
550 {
551 	xfs_failaddr_t		fa;
552 	int			err;
553 
554 	err = xfs_da_read_buf(tp, dp, bno, 0, bpp, XFS_ATTR_FORK,
555 			&xfs_attr3_leaf_buf_ops);
556 	if (err || !(*bpp))
557 		return err;
558 
559 	fa = xfs_attr3_leaf_header_check(*bpp, owner);
560 	if (fa) {
561 		__xfs_buf_mark_corrupt(*bpp, fa);
562 		xfs_trans_brelse(tp, *bpp);
563 		*bpp = NULL;
564 		xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
565 		return -EFSCORRUPTED;
566 	}
567 
568 	if (tp)
569 		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
570 	return 0;
571 }
572 
573 /*========================================================================
574  * Namespace helper routines
575  *========================================================================*/
576 
577 /*
578  * If we are in log recovery, then we want the lookup to ignore the INCOMPLETE
579  * flag on disk - if there's an incomplete attr then recovery needs to tear it
580  * down. If there's no incomplete attr, then recovery needs to tear that attr
581  * down to replace it with the attr that has been logged. In this case, the
582  * INCOMPLETE flag will not be set in attr->attr_filter, but rather
583  * XFS_DA_OP_RECOVERY will be set in args->op_flags.
584  */
xfs_attr_match_mask(const struct xfs_da_args * args)585 static inline unsigned int xfs_attr_match_mask(const struct xfs_da_args *args)
586 {
587 	if (args->op_flags & XFS_DA_OP_RECOVERY)
588 		return XFS_ATTR_NSP_ONDISK_MASK;
589 	return XFS_ATTR_NSP_ONDISK_MASK | XFS_ATTR_INCOMPLETE;
590 }
591 
592 static inline bool
xfs_attr_parent_match(const struct xfs_da_args * args,const void * value,unsigned int valuelen)593 xfs_attr_parent_match(
594 	const struct xfs_da_args	*args,
595 	const void			*value,
596 	unsigned int			valuelen)
597 {
598 	ASSERT(args->value != NULL);
599 
600 	/* Parent pointers do not use remote values */
601 	if (!value)
602 		return false;
603 
604 	/*
605 	 * The only value we support is a parent rec.  However, we'll accept
606 	 * any valuelen so that offline repair can delete ATTR_PARENT values
607 	 * that are not parent pointers.
608 	 */
609 	if (valuelen != args->valuelen)
610 		return false;
611 
612 	return memcmp(args->value, value, valuelen) == 0;
613 }
614 
615 static bool
xfs_attr_match(struct xfs_da_args * args,unsigned int attr_flags,const unsigned char * name,unsigned int namelen,const void * value,unsigned int valuelen)616 xfs_attr_match(
617 	struct xfs_da_args	*args,
618 	unsigned int		attr_flags,
619 	const unsigned char	*name,
620 	unsigned int		namelen,
621 	const void		*value,
622 	unsigned int		valuelen)
623 {
624 	unsigned int		mask = xfs_attr_match_mask(args);
625 
626 	if (args->namelen != namelen)
627 		return false;
628 	if ((args->attr_filter & mask) != (attr_flags & mask))
629 		return false;
630 	if (memcmp(args->name, name, namelen) != 0)
631 		return false;
632 
633 	if (attr_flags & XFS_ATTR_PARENT)
634 		return xfs_attr_parent_match(args, value, valuelen);
635 
636 	return true;
637 }
638 
639 static int
xfs_attr_copy_value(struct xfs_da_args * args,unsigned char * value,int valuelen)640 xfs_attr_copy_value(
641 	struct xfs_da_args	*args,
642 	unsigned char		*value,
643 	int			valuelen)
644 {
645 	/*
646 	 * Parent pointer lookups require the caller to specify the name and
647 	 * value, so don't copy anything.
648 	 */
649 	if (args->attr_filter & XFS_ATTR_PARENT)
650 		return 0;
651 
652 	/*
653 	 * No copy if all we have to do is get the length
654 	 */
655 	if (!args->valuelen) {
656 		args->valuelen = valuelen;
657 		return 0;
658 	}
659 
660 	/*
661 	 * No copy if the length of the existing buffer is too small
662 	 */
663 	if (args->valuelen < valuelen) {
664 		args->valuelen = valuelen;
665 		return -ERANGE;
666 	}
667 
668 	if (!args->value) {
669 		args->value = kvmalloc(valuelen, GFP_KERNEL | __GFP_NOLOCKDEP);
670 		if (!args->value)
671 			return -ENOMEM;
672 	}
673 	args->valuelen = valuelen;
674 
675 	/* remote block xattr requires IO for copy-in */
676 	if (args->rmtblkno)
677 		return xfs_attr_rmtval_get(args);
678 
679 	/*
680 	 * This is to prevent a GCC warning because the remote xattr case
681 	 * doesn't have a value to pass in. In that case, we never reach here,
682 	 * but GCC can't work that out and so throws a "passing NULL to
683 	 * memcpy" warning.
684 	 */
685 	if (!value)
686 		return -EINVAL;
687 	memcpy(args->value, value, valuelen);
688 	return 0;
689 }
690 
691 /*========================================================================
692  * External routines when attribute fork size < XFS_LITINO(mp).
693  *========================================================================*/
694 
695 /*
696  * Query whether the total requested number of attr fork bytes of extended
697  * attribute space will be able to fit inline.
698  *
699  * Returns zero if not, else the i_forkoff fork offset to be used in the
700  * literal area for attribute data once the new bytes have been added.
701  *
702  * i_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
703  * special case for dev/uuid inodes, they have fixed size data forks.
704  */
705 int
xfs_attr_shortform_bytesfit(struct xfs_inode * dp,int bytes)706 xfs_attr_shortform_bytesfit(
707 	struct xfs_inode	*dp,
708 	int			bytes)
709 {
710 	struct xfs_mount	*mp = dp->i_mount;
711 	int64_t			dsize;
712 	int			minforkoff;
713 	int			maxforkoff;
714 	int			offset;
715 
716 	/*
717 	 * Check if the new size could fit at all first:
718 	 */
719 	if (bytes > XFS_LITINO(mp))
720 		return 0;
721 
722 	/* rounded down */
723 	offset = (XFS_LITINO(mp) - bytes) >> 3;
724 
725 	if (dp->i_df.if_format == XFS_DINODE_FMT_DEV) {
726 		minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
727 		return (offset >= minforkoff) ? minforkoff : 0;
728 	}
729 
730 	/*
731 	 * If the requested numbers of bytes is smaller or equal to the
732 	 * current attribute fork size we can always proceed.
733 	 *
734 	 * Note that if_bytes in the data fork might actually be larger than
735 	 * the current data fork size is due to delalloc extents. In that
736 	 * case either the extent count will go down when they are converted
737 	 * to real extents, or the delalloc conversion will take care of the
738 	 * literal area rebalancing.
739 	 */
740 	if (bytes <= xfs_inode_attr_fork_size(dp))
741 		return dp->i_forkoff;
742 
743 	/*
744 	 * For attr2 we can try to move the forkoff if there is space in the
745 	 * literal area
746 	 */
747 	dsize = dp->i_df.if_bytes;
748 
749 	switch (dp->i_df.if_format) {
750 	case XFS_DINODE_FMT_EXTENTS:
751 		/*
752 		 * If there is no attr fork and the data fork is extents,
753 		 * determine if creating the default attr fork will result
754 		 * in the extents form migrating to btree. If so, the
755 		 * minimum offset only needs to be the space required for
756 		 * the btree root.
757 		 */
758 		if (!dp->i_forkoff && dp->i_df.if_bytes >
759 		    xfs_default_attroffset(dp))
760 			dsize = xfs_bmdr_space_calc(MINDBTPTRS);
761 		break;
762 	case XFS_DINODE_FMT_BTREE:
763 		/*
764 		 * If we have a data btree then keep forkoff if we have one,
765 		 * otherwise we are adding a new attr, so then we set
766 		 * minforkoff to where the btree root can finish so we have
767 		 * plenty of room for attrs
768 		 */
769 		if (dp->i_forkoff) {
770 			if (offset < dp->i_forkoff)
771 				return 0;
772 			return dp->i_forkoff;
773 		}
774 		dsize = xfs_bmap_bmdr_space(dp->i_df.if_broot);
775 		break;
776 	}
777 
778 	/*
779 	 * A data fork btree root must have space for at least
780 	 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
781 	 */
782 	minforkoff = max_t(int64_t, dsize, xfs_bmdr_space_calc(MINDBTPTRS));
783 	minforkoff = roundup(minforkoff, 8) >> 3;
784 
785 	/* attr fork btree root can have at least this many key/ptr pairs */
786 	maxforkoff = XFS_LITINO(mp) - xfs_bmdr_space_calc(MINABTPTRS);
787 	maxforkoff = maxforkoff >> 3;	/* rounded down */
788 
789 	if (offset >= maxforkoff)
790 		return maxforkoff;
791 	if (offset >= minforkoff)
792 		return offset;
793 	return 0;
794 }
795 
796 /*
797  * Switch on the ATTR2 superblock bit (implies also FEATURES2) unless
798  * on-disk version bit says it is already set
799  */
800 STATIC void
xfs_sbversion_add_attr2(struct xfs_mount * mp,struct xfs_trans * tp)801 xfs_sbversion_add_attr2(
802 	struct xfs_mount	*mp,
803 	struct xfs_trans	*tp)
804 {
805 	if (mp->m_sb.sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
806 		return;
807 
808 	spin_lock(&mp->m_sb_lock);
809 	xfs_add_attr2(mp);
810 	spin_unlock(&mp->m_sb_lock);
811 	xfs_log_sb(tp);
812 }
813 
814 /*
815  * Create the initial contents of a shortform attribute list.
816  */
817 void
xfs_attr_shortform_create(struct xfs_da_args * args)818 xfs_attr_shortform_create(
819 	struct xfs_da_args	*args)
820 {
821 	struct xfs_inode	*dp = args->dp;
822 	struct xfs_ifork	*ifp = &dp->i_af;
823 	struct xfs_attr_sf_hdr	*hdr;
824 
825 	trace_xfs_attr_sf_create(args);
826 
827 	ASSERT(ifp->if_bytes == 0);
828 	if (ifp->if_format == XFS_DINODE_FMT_EXTENTS)
829 		ifp->if_format = XFS_DINODE_FMT_LOCAL;
830 
831 	hdr = xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
832 	memset(hdr, 0, sizeof(*hdr));
833 	hdr->totsize = cpu_to_be16(sizeof(*hdr));
834 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
835 }
836 
837 /*
838  * Return the entry if the attr in args is found, or NULL if not.
839  */
840 struct xfs_attr_sf_entry *
xfs_attr_sf_findname(struct xfs_da_args * args)841 xfs_attr_sf_findname(
842 	struct xfs_da_args		*args)
843 {
844 	struct xfs_attr_sf_hdr		*sf = args->dp->i_af.if_data;
845 	struct xfs_attr_sf_entry	*sfe;
846 
847 	for (sfe = xfs_attr_sf_firstentry(sf);
848 	     sfe < xfs_attr_sf_endptr(sf);
849 	     sfe = xfs_attr_sf_nextentry(sfe)) {
850 		if (xfs_attr_match(args, sfe->flags, sfe->nameval,
851 				sfe->namelen, &sfe->nameval[sfe->namelen],
852 				sfe->valuelen))
853 			return sfe;
854 	}
855 
856 	return NULL;
857 }
858 
859 /*
860  * Replace a shortform xattr if it's the right length.  Returns 0 on success,
861  * -ENOSPC if the length is wrong, or -ENOATTR if the attr was not found.
862  */
863 int
xfs_attr_shortform_replace(struct xfs_da_args * args)864 xfs_attr_shortform_replace(
865 	struct xfs_da_args		*args)
866 {
867 	struct xfs_attr_sf_entry	*sfe;
868 
869 	ASSERT(args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL);
870 
871 	trace_xfs_attr_sf_replace(args);
872 
873 	sfe = xfs_attr_sf_findname(args);
874 	if (!sfe)
875 		return -ENOATTR;
876 
877 	if (args->attr_filter & XFS_ATTR_PARENT) {
878 		if (sfe->namelen != args->new_namelen ||
879 		    sfe->valuelen != args->new_valuelen)
880 			return -ENOSPC;
881 
882 		memcpy(sfe->nameval, args->new_name, sfe->namelen);
883 		memcpy(&sfe->nameval[sfe->namelen], args->new_value,
884 				sfe->valuelen);
885 	} else {
886 		if (sfe->valuelen != args->valuelen)
887 			return -ENOSPC;
888 		memcpy(&sfe->nameval[sfe->namelen], args->value,
889 				sfe->valuelen);
890 	}
891 
892 	xfs_trans_log_inode(args->trans, args->dp,
893 			XFS_ILOG_CORE | XFS_ILOG_ADATA);
894 	return 0;
895 }
896 
897 /*
898  * Add a name/value pair to the shortform attribute list.
899  * Overflow from the inode has already been checked for.
900  */
901 void
xfs_attr_shortform_add(struct xfs_da_args * args,int forkoff)902 xfs_attr_shortform_add(
903 	struct xfs_da_args		*args,
904 	int				forkoff)
905 {
906 	struct xfs_inode		*dp = args->dp;
907 	struct xfs_mount		*mp = dp->i_mount;
908 	struct xfs_ifork		*ifp = &dp->i_af;
909 	struct xfs_attr_sf_hdr		*sf = ifp->if_data;
910 	struct xfs_attr_sf_entry	*sfe;
911 	int				size;
912 
913 	trace_xfs_attr_sf_add(args);
914 
915 	dp->i_forkoff = forkoff;
916 
917 	ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
918 	ASSERT(!xfs_attr_sf_findname(args));
919 
920 	size = xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
921 	sf = xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
922 
923 	sfe = xfs_attr_sf_endptr(sf);
924 	sfe->namelen = args->namelen;
925 	sfe->valuelen = args->valuelen;
926 	sfe->flags = args->attr_filter;
927 	memcpy(sfe->nameval, args->name, args->namelen);
928 	memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
929 	sf->count++;
930 	be16_add_cpu(&sf->totsize, size);
931 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
932 
933 	xfs_sbversion_add_attr2(mp, args->trans);
934 }
935 
936 /*
937  * After the last attribute is removed revert to original inode format,
938  * making all literal area available to the data fork once more.
939  */
940 void
xfs_attr_fork_remove(struct xfs_inode * ip,struct xfs_trans * tp)941 xfs_attr_fork_remove(
942 	struct xfs_inode	*ip,
943 	struct xfs_trans	*tp)
944 {
945 	ASSERT(ip->i_af.if_nextents == 0);
946 
947 	xfs_ifork_zap_attr(ip);
948 	ip->i_forkoff = 0;
949 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
950 }
951 
952 /*
953  * Remove an attribute from the shortform attribute list structure.
954  */
955 int
xfs_attr_sf_removename(struct xfs_da_args * args)956 xfs_attr_sf_removename(
957 	struct xfs_da_args		*args)
958 {
959 	struct xfs_inode		*dp = args->dp;
960 	struct xfs_mount		*mp = dp->i_mount;
961 	struct xfs_attr_sf_hdr		*sf = dp->i_af.if_data;
962 	struct xfs_attr_sf_entry	*sfe;
963 	uint16_t			totsize = be16_to_cpu(sf->totsize);
964 	void				*next, *end;
965 	int				size = 0;
966 
967 	trace_xfs_attr_sf_remove(args);
968 
969 	sfe = xfs_attr_sf_findname(args);
970 	if (!sfe) {
971 		/*
972 		 * If we are recovering an operation, finding nothing to remove
973 		 * is not an error, it just means there was nothing to clean up.
974 		 */
975 		if (args->op_flags & XFS_DA_OP_RECOVERY)
976 			return 0;
977 		return -ENOATTR;
978 	}
979 
980 	/*
981 	 * Fix up the attribute fork data, covering the hole
982 	 */
983 	size = xfs_attr_sf_entsize(sfe);
984 	next = xfs_attr_sf_nextentry(sfe);
985 	end = xfs_attr_sf_endptr(sf);
986 	if (next < end)
987 		memmove(sfe, next, end - next);
988 	sf->count--;
989 	totsize -= size;
990 	sf->totsize = cpu_to_be16(totsize);
991 
992 	/*
993 	 * Fix up the start offset of the attribute fork
994 	 */
995 	if (totsize == sizeof(struct xfs_attr_sf_hdr) &&
996 	    (dp->i_df.if_format != XFS_DINODE_FMT_BTREE) &&
997 	    !(args->op_flags & (XFS_DA_OP_ADDNAME | XFS_DA_OP_REPLACE)) &&
998 	    !xfs_has_parent(mp)) {
999 		xfs_attr_fork_remove(dp, args->trans);
1000 	} else {
1001 		xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
1002 		dp->i_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
1003 		ASSERT(dp->i_forkoff);
1004 		ASSERT(totsize > sizeof(struct xfs_attr_sf_hdr) ||
1005 				(args->op_flags & XFS_DA_OP_ADDNAME) ||
1006 				dp->i_df.if_format == XFS_DINODE_FMT_BTREE ||
1007 				xfs_has_parent(mp));
1008 		xfs_trans_log_inode(args->trans, dp,
1009 					XFS_ILOG_CORE | XFS_ILOG_ADATA);
1010 	}
1011 
1012 	xfs_sbversion_add_attr2(mp, args->trans);
1013 
1014 	return 0;
1015 }
1016 
1017 /*
1018  * Retrieve the attribute value and length.
1019  *
1020  * If args->valuelen is zero, only the length needs to be returned.  Unlike a
1021  * lookup, we only return an error if the attribute does not exist or we can't
1022  * retrieve the value.
1023  */
1024 int
xfs_attr_shortform_getvalue(struct xfs_da_args * args)1025 xfs_attr_shortform_getvalue(
1026 	struct xfs_da_args		*args)
1027 {
1028 	struct xfs_attr_sf_entry	*sfe;
1029 
1030 	ASSERT(args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL);
1031 
1032 	trace_xfs_attr_sf_lookup(args);
1033 
1034 	sfe = xfs_attr_sf_findname(args);
1035 	if (!sfe)
1036 		return -ENOATTR;
1037 	return xfs_attr_copy_value(args, &sfe->nameval[args->namelen],
1038 			sfe->valuelen);
1039 }
1040 
1041 /* Convert from using the shortform to the leaf format. */
1042 int
xfs_attr_shortform_to_leaf(struct xfs_da_args * args)1043 xfs_attr_shortform_to_leaf(
1044 	struct xfs_da_args		*args)
1045 {
1046 	struct xfs_inode		*dp = args->dp;
1047 	struct xfs_ifork		*ifp = &dp->i_af;
1048 	struct xfs_attr_sf_hdr		*sf = ifp->if_data;
1049 	struct xfs_attr_sf_entry	*sfe;
1050 	int				size = be16_to_cpu(sf->totsize);
1051 	struct xfs_da_args		nargs;
1052 	char				*tmpbuffer;
1053 	int				error, i;
1054 	xfs_dablk_t			blkno;
1055 	struct xfs_buf			*bp;
1056 
1057 	trace_xfs_attr_sf_to_leaf(args);
1058 
1059 	tmpbuffer = kmalloc(size, GFP_KERNEL | __GFP_NOFAIL);
1060 	memcpy(tmpbuffer, ifp->if_data, size);
1061 	sf = (struct xfs_attr_sf_hdr *)tmpbuffer;
1062 
1063 	xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
1064 	xfs_bmap_local_to_extents_empty(args->trans, dp, XFS_ATTR_FORK);
1065 
1066 	bp = NULL;
1067 	error = xfs_da_grow_inode(args, &blkno);
1068 	if (error)
1069 		goto out;
1070 
1071 	ASSERT(blkno == 0);
1072 	error = xfs_attr3_leaf_create(args, blkno, &bp);
1073 	if (error)
1074 		goto out;
1075 
1076 	memset((char *)&nargs, 0, sizeof(nargs));
1077 	nargs.dp = dp;
1078 	nargs.geo = args->geo;
1079 	nargs.total = args->total;
1080 	nargs.whichfork = XFS_ATTR_FORK;
1081 	nargs.trans = args->trans;
1082 	nargs.op_flags = XFS_DA_OP_OKNOENT;
1083 	nargs.owner = args->owner;
1084 
1085 	sfe = xfs_attr_sf_firstentry(sf);
1086 	for (i = 0; i < sf->count; i++) {
1087 		nargs.name = sfe->nameval;
1088 		nargs.namelen = sfe->namelen;
1089 		nargs.value = &sfe->nameval[nargs.namelen];
1090 		nargs.valuelen = sfe->valuelen;
1091 		nargs.attr_filter = sfe->flags & XFS_ATTR_NSP_ONDISK_MASK;
1092 		if (!xfs_attr_check_namespace(sfe->flags)) {
1093 			xfs_da_mark_sick(args);
1094 			error = -EFSCORRUPTED;
1095 			goto out;
1096 		}
1097 		xfs_attr_sethash(&nargs);
1098 		error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
1099 		ASSERT(error == -ENOATTR);
1100 		if (!xfs_attr3_leaf_add(bp, &nargs))
1101 			ASSERT(0);
1102 		sfe = xfs_attr_sf_nextentry(sfe);
1103 	}
1104 	error = 0;
1105 out:
1106 	kfree(tmpbuffer);
1107 	return error;
1108 }
1109 
1110 /*
1111  * Check a leaf attribute block to see if all the entries would fit into
1112  * a shortform attribute list.
1113  */
1114 int
xfs_attr_shortform_allfit(struct xfs_buf * bp,struct xfs_inode * dp)1115 xfs_attr_shortform_allfit(
1116 	struct xfs_buf		*bp,
1117 	struct xfs_inode	*dp)
1118 {
1119 	struct xfs_attr_leafblock *leaf;
1120 	struct xfs_attr_leaf_entry *entry;
1121 	xfs_attr_leaf_name_local_t *name_loc;
1122 	struct xfs_attr3_icleaf_hdr leafhdr;
1123 	int			bytes;
1124 	int			i;
1125 	struct xfs_mount	*mp = bp->b_mount;
1126 
1127 	leaf = bp->b_addr;
1128 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
1129 	entry = xfs_attr3_leaf_entryp(leaf);
1130 
1131 	bytes = sizeof(struct xfs_attr_sf_hdr);
1132 	for (i = 0; i < leafhdr.count; entry++, i++) {
1133 		if (entry->flags & XFS_ATTR_INCOMPLETE)
1134 			continue;		/* don't copy partial entries */
1135 		if (!(entry->flags & XFS_ATTR_LOCAL))
1136 			return 0;
1137 		name_loc = xfs_attr3_leaf_name_local(leaf, i);
1138 		if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
1139 			return 0;
1140 		if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
1141 			return 0;
1142 		bytes += xfs_attr_sf_entsize_byname(name_loc->namelen,
1143 					be16_to_cpu(name_loc->valuelen));
1144 	}
1145 	if ((dp->i_df.if_format != XFS_DINODE_FMT_BTREE) &&
1146 	    (bytes == sizeof(struct xfs_attr_sf_hdr)))
1147 		return -1;
1148 	return xfs_attr_shortform_bytesfit(dp, bytes);
1149 }
1150 
1151 /* Verify the consistency of a raw inline attribute fork. */
1152 xfs_failaddr_t
xfs_attr_shortform_verify(struct xfs_attr_sf_hdr * sfp,size_t size)1153 xfs_attr_shortform_verify(
1154 	struct xfs_attr_sf_hdr		*sfp,
1155 	size_t				size)
1156 {
1157 	struct xfs_attr_sf_entry	*sfep = xfs_attr_sf_firstentry(sfp);
1158 	struct xfs_attr_sf_entry	*next_sfep;
1159 	char				*endp;
1160 	int				i;
1161 
1162 	/*
1163 	 * Give up if the attribute is way too short.
1164 	 */
1165 	if (size < sizeof(struct xfs_attr_sf_hdr))
1166 		return __this_address;
1167 
1168 	endp = (char *)sfp + size;
1169 
1170 	/* Check all reported entries */
1171 	for (i = 0; i < sfp->count; i++) {
1172 		/*
1173 		 * struct xfs_attr_sf_entry has a variable length.
1174 		 * Check the fixed-offset parts of the structure are
1175 		 * within the data buffer.
1176 		 * xfs_attr_sf_entry is defined with a 1-byte variable
1177 		 * array at the end, so we must subtract that off.
1178 		 */
1179 		if (((char *)sfep + sizeof(*sfep)) >= endp)
1180 			return __this_address;
1181 
1182 		/* Don't allow names with known bad length. */
1183 		if (sfep->namelen == 0)
1184 			return __this_address;
1185 
1186 		/*
1187 		 * Check that the variable-length part of the structure is
1188 		 * within the data buffer.  The next entry starts after the
1189 		 * name component, so nextentry is an acceptable test.
1190 		 */
1191 		next_sfep = xfs_attr_sf_nextentry(sfep);
1192 		if ((char *)next_sfep > endp)
1193 			return __this_address;
1194 
1195 		/*
1196 		 * Check for unknown flags.  Short form doesn't support
1197 		 * the incomplete or local bits, so we can use the namespace
1198 		 * mask here.
1199 		 */
1200 		if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK)
1201 			return __this_address;
1202 
1203 		/*
1204 		 * Check for invalid namespace combinations.  We only allow
1205 		 * one namespace flag per xattr, so we can just count the
1206 		 * bits (i.e. hweight) here.
1207 		 */
1208 		if (!xfs_attr_check_namespace(sfep->flags))
1209 			return __this_address;
1210 
1211 		sfep = next_sfep;
1212 	}
1213 	if ((void *)sfep != (void *)endp)
1214 		return __this_address;
1215 
1216 	return NULL;
1217 }
1218 
1219 /*
1220  * Convert a leaf attribute list to shortform attribute list
1221  */
1222 int
xfs_attr3_leaf_to_shortform(struct xfs_buf * bp,struct xfs_da_args * args,int forkoff)1223 xfs_attr3_leaf_to_shortform(
1224 	struct xfs_buf		*bp,
1225 	struct xfs_da_args	*args,
1226 	int			forkoff)
1227 {
1228 	struct xfs_attr_leafblock *leaf;
1229 	struct xfs_attr3_icleaf_hdr ichdr;
1230 	struct xfs_attr_leaf_entry *entry;
1231 	struct xfs_attr_leaf_name_local *name_loc;
1232 	struct xfs_da_args	nargs;
1233 	struct xfs_inode	*dp = args->dp;
1234 	char			*tmpbuffer;
1235 	int			error;
1236 	int			i;
1237 
1238 	trace_xfs_attr_leaf_to_sf(args);
1239 
1240 	tmpbuffer = kvmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
1241 	memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1242 
1243 	leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1244 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1245 	entry = xfs_attr3_leaf_entryp(leaf);
1246 
1247 	/* XXX (dgc): buffer is about to be marked stale - why zero it? */
1248 	memset(bp->b_addr, 0, args->geo->blksize);
1249 
1250 	/*
1251 	 * Clean out the prior contents of the attribute list.
1252 	 */
1253 	error = xfs_da_shrink_inode(args, 0, bp);
1254 	if (error)
1255 		goto out;
1256 
1257 	if (forkoff == -1) {
1258 		/*
1259 		 * Don't remove the attr fork if this operation is the first
1260 		 * part of a attr replace operations. We're going to add a new
1261 		 * attr immediately, so we need to keep the attr fork around in
1262 		 * this case.
1263 		 */
1264 		if (!(args->op_flags & XFS_DA_OP_REPLACE)) {
1265 			ASSERT(dp->i_df.if_format != XFS_DINODE_FMT_BTREE);
1266 			xfs_attr_fork_remove(dp, args->trans);
1267 		}
1268 		goto out;
1269 	}
1270 
1271 	xfs_attr_shortform_create(args);
1272 
1273 	/*
1274 	 * Copy the attributes
1275 	 */
1276 	memset((char *)&nargs, 0, sizeof(nargs));
1277 	nargs.geo = args->geo;
1278 	nargs.dp = dp;
1279 	nargs.total = args->total;
1280 	nargs.whichfork = XFS_ATTR_FORK;
1281 	nargs.trans = args->trans;
1282 	nargs.op_flags = XFS_DA_OP_OKNOENT;
1283 	nargs.owner = args->owner;
1284 
1285 	for (i = 0; i < ichdr.count; entry++, i++) {
1286 		if (entry->flags & XFS_ATTR_INCOMPLETE)
1287 			continue;	/* don't copy partial entries */
1288 		if (!entry->nameidx)
1289 			continue;
1290 		ASSERT(entry->flags & XFS_ATTR_LOCAL);
1291 		name_loc = xfs_attr3_leaf_name_local(leaf, i);
1292 		nargs.name = name_loc->nameval;
1293 		nargs.namelen = name_loc->namelen;
1294 		nargs.value = &name_loc->nameval[nargs.namelen];
1295 		nargs.valuelen = be16_to_cpu(name_loc->valuelen);
1296 		nargs.hashval = be32_to_cpu(entry->hashval);
1297 		nargs.attr_filter = entry->flags & XFS_ATTR_NSP_ONDISK_MASK;
1298 		xfs_attr_shortform_add(&nargs, forkoff);
1299 	}
1300 	error = 0;
1301 
1302 out:
1303 	kvfree(tmpbuffer);
1304 	return error;
1305 }
1306 
1307 /*
1308  * Convert from using a single leaf to a root node and a leaf.
1309  */
1310 int
xfs_attr3_leaf_to_node(struct xfs_da_args * args)1311 xfs_attr3_leaf_to_node(
1312 	struct xfs_da_args	*args)
1313 {
1314 	struct xfs_attr_leafblock *leaf;
1315 	struct xfs_attr3_icleaf_hdr icleafhdr;
1316 	struct xfs_attr_leaf_entry *entries;
1317 	struct xfs_da3_icnode_hdr icnodehdr;
1318 	struct xfs_da_intnode	*node;
1319 	struct xfs_inode	*dp = args->dp;
1320 	struct xfs_mount	*mp = dp->i_mount;
1321 	struct xfs_buf		*bp1 = NULL;
1322 	struct xfs_buf		*bp2 = NULL;
1323 	xfs_dablk_t		blkno;
1324 	int			error;
1325 
1326 	trace_xfs_attr_leaf_to_node(args);
1327 
1328 	if (XFS_TEST_ERROR(mp, XFS_ERRTAG_ATTR_LEAF_TO_NODE)) {
1329 		error = -EIO;
1330 		goto out;
1331 	}
1332 
1333 	error = xfs_da_grow_inode(args, &blkno);
1334 	if (error)
1335 		goto out;
1336 	error = xfs_attr3_leaf_read(args->trans, dp, args->owner, 0, &bp1);
1337 	if (error)
1338 		goto out;
1339 
1340 	error = xfs_da_get_buf(args->trans, dp, blkno, &bp2, XFS_ATTR_FORK);
1341 	if (error)
1342 		goto out;
1343 
1344 	/*
1345 	 * Copy leaf to new buffer and log it.
1346 	 */
1347 	xfs_da_buf_copy(bp2, bp1, args->geo->blksize);
1348 	xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
1349 
1350 	/*
1351 	 * Set up the new root node.
1352 	 */
1353 	error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
1354 	if (error)
1355 		goto out;
1356 	node = bp1->b_addr;
1357 	xfs_da3_node_hdr_from_disk(mp, &icnodehdr, node);
1358 
1359 	leaf = bp2->b_addr;
1360 	xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
1361 	entries = xfs_attr3_leaf_entryp(leaf);
1362 
1363 	/* both on-disk, don't endian-flip twice */
1364 	icnodehdr.btree[0].hashval = entries[icleafhdr.count - 1].hashval;
1365 	icnodehdr.btree[0].before = cpu_to_be32(blkno);
1366 	icnodehdr.count = 1;
1367 	xfs_da3_node_hdr_to_disk(dp->i_mount, node, &icnodehdr);
1368 	xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
1369 	error = 0;
1370 out:
1371 	return error;
1372 }
1373 
1374 /*========================================================================
1375  * Routines used for growing the Btree.
1376  *========================================================================*/
1377 
1378 /*
1379  * Create the initial contents of a leaf attribute list
1380  * or a leaf in a node attribute list.
1381  */
1382 STATIC int
xfs_attr3_leaf_create(struct xfs_da_args * args,xfs_dablk_t blkno,struct xfs_buf ** bpp)1383 xfs_attr3_leaf_create(
1384 	struct xfs_da_args	*args,
1385 	xfs_dablk_t		blkno,
1386 	struct xfs_buf		**bpp)
1387 {
1388 	struct xfs_attr_leafblock *leaf;
1389 	struct xfs_attr3_icleaf_hdr ichdr;
1390 	struct xfs_inode	*dp = args->dp;
1391 	struct xfs_mount	*mp = dp->i_mount;
1392 	struct xfs_buf		*bp;
1393 	int			error;
1394 
1395 	trace_xfs_attr_leaf_create(args);
1396 
1397 	error = xfs_da_get_buf(args->trans, args->dp, blkno, &bp,
1398 					    XFS_ATTR_FORK);
1399 	if (error)
1400 		return error;
1401 	bp->b_ops = &xfs_attr3_leaf_buf_ops;
1402 	xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
1403 	leaf = bp->b_addr;
1404 	memset(leaf, 0, args->geo->blksize);
1405 
1406 	memset(&ichdr, 0, sizeof(ichdr));
1407 	ichdr.firstused = args->geo->blksize;
1408 
1409 	if (xfs_has_crc(mp)) {
1410 		struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1411 
1412 		ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1413 
1414 		hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
1415 		hdr3->owner = cpu_to_be64(args->owner);
1416 		uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
1417 
1418 		ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1419 	} else {
1420 		ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1421 		ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
1422 	}
1423 	ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
1424 
1425 	xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1426 	xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
1427 
1428 	*bpp = bp;
1429 	return 0;
1430 }
1431 
1432 /*
1433  * Reinitialize an existing attr fork block as an empty leaf, and attach
1434  * the buffer to tp.
1435  */
1436 int
xfs_attr3_leaf_init(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t blkno)1437 xfs_attr3_leaf_init(
1438 	struct xfs_trans	*tp,
1439 	struct xfs_inode	*dp,
1440 	xfs_dablk_t		blkno)
1441 {
1442 	struct xfs_buf		*bp = NULL;
1443 	struct xfs_da_args	args = {
1444 		.trans		= tp,
1445 		.dp		= dp,
1446 		.owner		= I_INO(dp),
1447 		.geo		= dp->i_mount->m_attr_geo,
1448 	};
1449 
1450 	ASSERT(tp != NULL);
1451 
1452 	return xfs_attr3_leaf_create(&args, blkno, &bp);
1453 }
1454 /*
1455  * Split the leaf node, rebalance, then add the new entry.
1456  *
1457  * Returns 0 if the entry was added, 1 if a further split is needed or a
1458  * negative error number otherwise.
1459  */
1460 int
xfs_attr3_leaf_split(struct xfs_da_state * state,struct xfs_da_state_blk * oldblk,struct xfs_da_state_blk * newblk)1461 xfs_attr3_leaf_split(
1462 	struct xfs_da_state	*state,
1463 	struct xfs_da_state_blk	*oldblk,
1464 	struct xfs_da_state_blk	*newblk)
1465 {
1466 	bool			added;
1467 	xfs_dablk_t		blkno;
1468 	int			error;
1469 
1470 	trace_xfs_attr_leaf_split(state->args);
1471 
1472 	/*
1473 	 * Allocate space for a new leaf node.
1474 	 */
1475 	ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1476 	error = xfs_da_grow_inode(state->args, &blkno);
1477 	if (error)
1478 		return error;
1479 	error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
1480 	if (error)
1481 		return error;
1482 	newblk->blkno = blkno;
1483 	newblk->magic = XFS_ATTR_LEAF_MAGIC;
1484 
1485 	/*
1486 	 * Rebalance the entries across the two leaves.
1487 	 * NOTE: rebalance() currently depends on the 2nd block being empty.
1488 	 */
1489 	xfs_attr3_leaf_rebalance(state, oldblk, newblk);
1490 	error = xfs_da3_blk_link(state, oldblk, newblk);
1491 	if (error)
1492 		return error;
1493 
1494 	/*
1495 	 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1496 	 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1497 	 * "new" attrs info.  Will need the "old" info to remove it later.
1498 	 *
1499 	 * Insert the "new" entry in the correct block.
1500 	 */
1501 	if (state->inleaf) {
1502 		trace_xfs_attr_leaf_add_old(state->args);
1503 		added = xfs_attr3_leaf_add(oldblk->bp, state->args);
1504 	} else {
1505 		trace_xfs_attr_leaf_add_new(state->args);
1506 		added = xfs_attr3_leaf_add(newblk->bp, state->args);
1507 	}
1508 
1509 	/*
1510 	 * Update last hashval in each block since we added the name.
1511 	 */
1512 	oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1513 	newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1514 	if (!added)
1515 		return 1;
1516 	return 0;
1517 }
1518 
1519 /*
1520  * Add a name to the leaf attribute list structure.
1521  */
1522 bool
xfs_attr3_leaf_add(struct xfs_buf * bp,struct xfs_da_args * args)1523 xfs_attr3_leaf_add(
1524 	struct xfs_buf		*bp,
1525 	struct xfs_da_args	*args)
1526 {
1527 	struct xfs_attr_leafblock *leaf;
1528 	struct xfs_attr3_icleaf_hdr ichdr;
1529 	int			tablesize;
1530 	int			entsize;
1531 	bool			added = true;
1532 	int			sum;
1533 	int			tmp;
1534 	int			i;
1535 
1536 	trace_xfs_attr_leaf_add(args);
1537 
1538 	leaf = bp->b_addr;
1539 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1540 	ASSERT(args->index >= 0 && args->index <= ichdr.count);
1541 	entsize = xfs_attr_leaf_newentsize(args, NULL);
1542 
1543 	/*
1544 	 * Search through freemap for first-fit on new name length.
1545 	 * (may need to figure in size of entry struct too)
1546 	 */
1547 	tablesize = xfs_attr_leaf_entries_end(ichdr.count + 1, leaf);
1548 	for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1549 		if (tablesize > ichdr.firstused) {
1550 			sum += ichdr.freemap[i].size;
1551 			continue;
1552 		}
1553 		if (!ichdr.freemap[i].size)
1554 			continue;	/* no space in this map */
1555 		tmp = entsize;
1556 		if (ichdr.freemap[i].base < ichdr.firstused)
1557 			tmp += sizeof(xfs_attr_leaf_entry_t);
1558 		if (ichdr.freemap[i].size >= tmp) {
1559 			xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1560 			goto out_log_hdr;
1561 		}
1562 		sum += ichdr.freemap[i].size;
1563 	}
1564 
1565 	/*
1566 	 * If there are no holes in the address space of the block,
1567 	 * and we don't have enough freespace, then compaction will do us
1568 	 * no good and we should just give up.
1569 	 */
1570 	if (!ichdr.holes && sum < entsize)
1571 		return false;
1572 
1573 	/*
1574 	 * Compact the entries to coalesce free space.
1575 	 * This may change the hdr->count via dropping INCOMPLETE entries.
1576 	 */
1577 	xfs_attr3_leaf_compact(args, &ichdr, bp);
1578 
1579 	/*
1580 	 * After compaction, the block is guaranteed to have only one
1581 	 * free region, in freemap[0].  If it is not big enough, give up.
1582 	 */
1583 	if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
1584 		added = false;
1585 		goto out_log_hdr;
1586 	}
1587 
1588 	xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1589 
1590 out_log_hdr:
1591 	xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1592 	xfs_trans_log_buf(args->trans, bp,
1593 		XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1594 				xfs_attr3_leaf_hdr_size(leaf)));
1595 	return added;
1596 }
1597 
1598 /*
1599  * Add a name to a leaf attribute list structure.
1600  */
1601 STATIC void
xfs_attr3_leaf_add_work(struct xfs_buf * bp,struct xfs_attr3_icleaf_hdr * ichdr,struct xfs_da_args * args,int mapindex)1602 xfs_attr3_leaf_add_work(
1603 	struct xfs_buf		*bp,
1604 	struct xfs_attr3_icleaf_hdr *ichdr,
1605 	struct xfs_da_args	*args,
1606 	int			mapindex)
1607 {
1608 	struct xfs_attr_leafblock *leaf;
1609 	struct xfs_attr_leaf_entry *entry;
1610 	struct xfs_attr_leaf_name_local *name_loc;
1611 	struct xfs_attr_leaf_name_remote *name_rmt;
1612 	struct xfs_mount	*mp;
1613 	int			old_end, new_end;
1614 	int			tmp;
1615 	int			i;
1616 
1617 	trace_xfs_attr_leaf_add_work(args);
1618 
1619 	leaf = bp->b_addr;
1620 	ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1621 	ASSERT(args->index >= 0 && args->index <= ichdr->count);
1622 
1623 	/*
1624 	 * Force open some space in the entry array and fill it in.
1625 	 */
1626 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1627 	if (args->index < ichdr->count) {
1628 		tmp  = ichdr->count - args->index;
1629 		tmp *= sizeof(xfs_attr_leaf_entry_t);
1630 		memmove(entry + 1, entry, tmp);
1631 		xfs_trans_log_buf(args->trans, bp,
1632 		    XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1633 	}
1634 	ichdr->count++;
1635 
1636 	/*
1637 	 * Allocate space for the new string (at the end of the run).
1638 	 */
1639 	mp = args->trans->t_mountp;
1640 	ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
1641 	ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1642 	ASSERT(ichdr->freemap[mapindex].size >=
1643 		xfs_attr_leaf_newentsize(args, NULL));
1644 	ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
1645 	ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1646 
1647 	ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
1648 
1649 	entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1650 				     ichdr->freemap[mapindex].size);
1651 	entry->hashval = cpu_to_be32(args->hashval);
1652 	entry->flags = args->attr_filter;
1653 	if (tmp)
1654 		entry->flags |= XFS_ATTR_LOCAL;
1655 	if (args->op_flags & XFS_DA_OP_REPLACE) {
1656 		if (!(args->op_flags & XFS_DA_OP_LOGGED))
1657 			entry->flags |= XFS_ATTR_INCOMPLETE;
1658 		if ((args->blkno2 == args->blkno) &&
1659 		    (args->index2 <= args->index)) {
1660 			args->index2++;
1661 		}
1662 	}
1663 	xfs_trans_log_buf(args->trans, bp,
1664 			  XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1665 	ASSERT((args->index == 0) ||
1666 	       (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1667 	ASSERT((args->index == ichdr->count - 1) ||
1668 	       (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1669 
1670 	/*
1671 	 * For "remote" attribute values, simply note that we need to
1672 	 * allocate space for the "remote" value.  We can't actually
1673 	 * allocate the extents in this transaction, and we can't decide
1674 	 * which blocks they should be as we might allocate more blocks
1675 	 * as part of this transaction (a split operation for example).
1676 	 */
1677 	if (entry->flags & XFS_ATTR_LOCAL) {
1678 		name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
1679 		name_loc->namelen = args->namelen;
1680 		name_loc->valuelen = cpu_to_be16(args->valuelen);
1681 		memcpy((char *)name_loc->nameval, args->name, args->namelen);
1682 		memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1683 				   be16_to_cpu(name_loc->valuelen));
1684 	} else {
1685 		name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
1686 		name_rmt->namelen = args->namelen;
1687 		memcpy((char *)name_rmt->name, args->name, args->namelen);
1688 		entry->flags |= XFS_ATTR_INCOMPLETE;
1689 		/* just in case */
1690 		name_rmt->valuelen = 0;
1691 		name_rmt->valueblk = 0;
1692 		args->rmtblkno = 1;
1693 		args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
1694 		args->rmtvaluelen = args->valuelen;
1695 	}
1696 	xfs_trans_log_buf(args->trans, bp,
1697 	     XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
1698 				   xfs_attr_leaf_entsize(leaf, args->index)));
1699 
1700 	/*
1701 	 * Update the control info for this leaf node
1702 	 */
1703 	if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1704 		ichdr->firstused = be16_to_cpu(entry->nameidx);
1705 
1706 	new_end = xfs_attr_leaf_entries_end(ichdr->count, leaf);
1707 	old_end = new_end - sizeof(struct xfs_attr_leaf_entry);
1708 
1709 	ASSERT(ichdr->firstused >= new_end);
1710 
1711 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1712 		int		diff = 0;
1713 
1714 		if (ichdr->freemap[i].base == old_end) {
1715 			/*
1716 			 * This freemap entry starts at the old end of the
1717 			 * leaf entry array, so we need to adjust its base
1718 			 * upward to accommodate the larger array.
1719 			 */
1720 			diff = sizeof(struct xfs_attr_leaf_entry);
1721 		} else if (ichdr->freemap[i].size > 0 &&
1722 			   ichdr->freemap[i].base < new_end) {
1723 			/*
1724 			 * This freemap entry starts in the space claimed by
1725 			 * the new leaf entry.  Adjust its base upward to
1726 			 * reflect that.
1727 			 */
1728 			diff = new_end - ichdr->freemap[i].base;
1729 		}
1730 
1731 		if (diff) {
1732 			ichdr->freemap[i].base += diff;
1733 			ichdr->freemap[i].size -=
1734 				min_t(uint16_t, ichdr->freemap[i].size, diff);
1735 		}
1736 
1737 		/*
1738 		 * Don't leave zero-length freemaps with nonzero base lying
1739 		 * around, because we don't want the code in _remove that
1740 		 * matches on base address to get confused and create
1741 		 * overlapping freemaps.  If we end up with no freemap entries
1742 		 * then the next _add will compact the leaf block and
1743 		 * regenerate the freemaps.
1744 		 */
1745 		if (ichdr->freemap[i].size == 0 && ichdr->freemap[i].base > 0) {
1746 			ichdr->freemap[i].base = 0;
1747 			ichdr->holes = 1;
1748 		}
1749 	}
1750 	ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1751 }
1752 
1753 /*
1754  * Garbage collect a leaf attribute list block by copying it to a new buffer.
1755  */
1756 STATIC void
xfs_attr3_leaf_compact(struct xfs_da_args * args,struct xfs_attr3_icleaf_hdr * ichdr_dst,struct xfs_buf * bp)1757 xfs_attr3_leaf_compact(
1758 	struct xfs_da_args	*args,
1759 	struct xfs_attr3_icleaf_hdr *ichdr_dst,
1760 	struct xfs_buf		*bp)
1761 {
1762 	struct xfs_attr_leafblock *leaf_src;
1763 	struct xfs_attr_leafblock *leaf_dst;
1764 	struct xfs_attr3_icleaf_hdr ichdr_src;
1765 	struct xfs_trans	*trans = args->trans;
1766 	char			*tmpbuffer;
1767 
1768 	trace_xfs_attr_leaf_compact(args);
1769 
1770 	tmpbuffer = kvmalloc(args->geo->blksize, GFP_KERNEL | __GFP_NOFAIL);
1771 	memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1772 	memset(bp->b_addr, 0, args->geo->blksize);
1773 	leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1774 	leaf_dst = bp->b_addr;
1775 
1776 	/*
1777 	 * Copy the on-disk header back into the destination buffer to ensure
1778 	 * all the information in the header that is not part of the incore
1779 	 * header structure is preserved.
1780 	 */
1781 	memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1782 
1783 	/* Initialise the incore headers */
1784 	ichdr_src = *ichdr_dst;	/* struct copy */
1785 	ichdr_dst->firstused = args->geo->blksize;
1786 	ichdr_dst->usedbytes = 0;
1787 	ichdr_dst->count = 0;
1788 	ichdr_dst->holes = 0;
1789 	ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1790 	ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1791 						ichdr_dst->freemap[0].base;
1792 	ichdr_dst->freemap[1].base = 0;
1793 	ichdr_dst->freemap[2].base = 0;
1794 	ichdr_dst->freemap[1].size = 0;
1795 	ichdr_dst->freemap[2].size = 0;
1796 
1797 	/* write the header back to initialise the underlying buffer */
1798 	xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
1799 
1800 	/*
1801 	 * Copy all entry's in the same (sorted) order,
1802 	 * but allocate name/value pairs packed and in sequence.
1803 	 */
1804 	xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
1805 				leaf_dst, ichdr_dst, 0, ichdr_src.count);
1806 	/*
1807 	 * this logs the entire buffer, but the caller must write the header
1808 	 * back to the buffer when it is finished modifying it.
1809 	 */
1810 	xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
1811 
1812 	kvfree(tmpbuffer);
1813 }
1814 
1815 /*
1816  * Compare two leaf blocks "order".
1817  * Return 0 unless leaf2 should go before leaf1.
1818  */
1819 static int
xfs_attr3_leaf_order(struct xfs_buf * leaf1_bp,struct xfs_attr3_icleaf_hdr * leaf1hdr,struct xfs_buf * leaf2_bp,struct xfs_attr3_icleaf_hdr * leaf2hdr)1820 xfs_attr3_leaf_order(
1821 	struct xfs_buf	*leaf1_bp,
1822 	struct xfs_attr3_icleaf_hdr *leaf1hdr,
1823 	struct xfs_buf	*leaf2_bp,
1824 	struct xfs_attr3_icleaf_hdr *leaf2hdr)
1825 {
1826 	struct xfs_attr_leaf_entry *entries1;
1827 	struct xfs_attr_leaf_entry *entries2;
1828 
1829 	entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1830 	entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1831 	if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1832 	    ((be32_to_cpu(entries2[0].hashval) <
1833 	      be32_to_cpu(entries1[0].hashval)) ||
1834 	     (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1835 	      be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1836 		return 1;
1837 	}
1838 	return 0;
1839 }
1840 
1841 int
xfs_attr_leaf_order(struct xfs_buf * leaf1_bp,struct xfs_buf * leaf2_bp)1842 xfs_attr_leaf_order(
1843 	struct xfs_buf	*leaf1_bp,
1844 	struct xfs_buf	*leaf2_bp)
1845 {
1846 	struct xfs_attr3_icleaf_hdr ichdr1;
1847 	struct xfs_attr3_icleaf_hdr ichdr2;
1848 	struct xfs_mount *mp = leaf1_bp->b_mount;
1849 
1850 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
1851 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
1852 	return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1853 }
1854 
1855 /*
1856  * Redistribute the attribute list entries between two leaf nodes,
1857  * taking into account the size of the new entry.
1858  *
1859  * NOTE: if new block is empty, then it will get the upper half of the
1860  * old block.  At present, all (one) callers pass in an empty second block.
1861  *
1862  * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1863  * to match what it is doing in splitting the attribute leaf block.  Those
1864  * values are used in "atomic rename" operations on attributes.  Note that
1865  * the "new" and "old" values can end up in different blocks.
1866  */
1867 STATIC void
xfs_attr3_leaf_rebalance(struct xfs_da_state * state,struct xfs_da_state_blk * blk1,struct xfs_da_state_blk * blk2)1868 xfs_attr3_leaf_rebalance(
1869 	struct xfs_da_state	*state,
1870 	struct xfs_da_state_blk	*blk1,
1871 	struct xfs_da_state_blk	*blk2)
1872 {
1873 	struct xfs_da_args	*args;
1874 	struct xfs_attr_leafblock *leaf1;
1875 	struct xfs_attr_leafblock *leaf2;
1876 	struct xfs_attr3_icleaf_hdr ichdr1;
1877 	struct xfs_attr3_icleaf_hdr ichdr2;
1878 	struct xfs_attr_leaf_entry *entries1;
1879 	struct xfs_attr_leaf_entry *entries2;
1880 	int			count;
1881 	int			totallen;
1882 	int			max;
1883 	int			space;
1884 	int			swap;
1885 
1886 	/*
1887 	 * Set up environment.
1888 	 */
1889 	ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1890 	ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1891 	leaf1 = blk1->bp->b_addr;
1892 	leaf2 = blk2->bp->b_addr;
1893 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
1894 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
1895 	ASSERT(ichdr2.count == 0);
1896 	args = state->args;
1897 
1898 	trace_xfs_attr_leaf_rebalance(args);
1899 
1900 	/*
1901 	 * Check ordering of blocks, reverse if it makes things simpler.
1902 	 *
1903 	 * NOTE: Given that all (current) callers pass in an empty
1904 	 * second block, this code should never set "swap".
1905 	 */
1906 	swap = 0;
1907 	if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1908 		swap(blk1, blk2);
1909 
1910 		/* swap structures rather than reconverting them */
1911 		swap(ichdr1, ichdr2);
1912 
1913 		leaf1 = blk1->bp->b_addr;
1914 		leaf2 = blk2->bp->b_addr;
1915 		swap = 1;
1916 	}
1917 
1918 	/*
1919 	 * Examine entries until we reduce the absolute difference in
1920 	 * byte usage between the two blocks to a minimum.  Then get
1921 	 * the direction to copy and the number of elements to move.
1922 	 *
1923 	 * "inleaf" is true if the new entry should be inserted into blk1.
1924 	 * If "swap" is also true, then reverse the sense of "inleaf".
1925 	 */
1926 	state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1927 						      blk2, &ichdr2,
1928 						      &count, &totallen);
1929 	if (swap)
1930 		state->inleaf = !state->inleaf;
1931 
1932 	/*
1933 	 * Move any entries required from leaf to leaf:
1934 	 */
1935 	if (count < ichdr1.count) {
1936 		/*
1937 		 * Figure the total bytes to be added to the destination leaf.
1938 		 */
1939 		/* number entries being moved */
1940 		count = ichdr1.count - count;
1941 		space  = ichdr1.usedbytes - totallen;
1942 		space += count * sizeof(xfs_attr_leaf_entry_t);
1943 
1944 		/*
1945 		 * leaf2 is the destination, compact it if it looks tight.
1946 		 */
1947 		max = ichdr2.firstused -
1948 				xfs_attr_leaf_entries_end(ichdr2.count, leaf1);
1949 		if (space > max)
1950 			xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
1951 
1952 		/*
1953 		 * Move high entries from leaf1 to low end of leaf2.
1954 		 */
1955 		xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
1956 				ichdr1.count - count, leaf2, &ichdr2, 0, count);
1957 
1958 	} else if (count > ichdr1.count) {
1959 		/*
1960 		 * I assert that since all callers pass in an empty
1961 		 * second buffer, this code should never execute.
1962 		 */
1963 		ASSERT(0);
1964 
1965 		/*
1966 		 * Figure the total bytes to be added to the destination leaf.
1967 		 */
1968 		/* number entries being moved */
1969 		count -= ichdr1.count;
1970 		space  = totallen - ichdr1.usedbytes;
1971 		space += count * sizeof(xfs_attr_leaf_entry_t);
1972 
1973 		/*
1974 		 * leaf1 is the destination, compact it if it looks tight.
1975 		 */
1976 		max = ichdr1.firstused -
1977 				xfs_attr_leaf_entries_end(ichdr1.count, leaf1);
1978 		if (space > max)
1979 			xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
1980 
1981 		/*
1982 		 * Move low entries from leaf2 to high end of leaf1.
1983 		 */
1984 		xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
1985 					ichdr1.count, count);
1986 	}
1987 
1988 	xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
1989 	xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
1990 	xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
1991 	xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
1992 
1993 	/*
1994 	 * Copy out last hashval in each block for B-tree code.
1995 	 */
1996 	entries1 = xfs_attr3_leaf_entryp(leaf1);
1997 	entries2 = xfs_attr3_leaf_entryp(leaf2);
1998 	blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1999 	blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
2000 
2001 	/*
2002 	 * Adjust the expected index for insertion.
2003 	 * NOTE: this code depends on the (current) situation that the
2004 	 * second block was originally empty.
2005 	 *
2006 	 * If the insertion point moved to the 2nd block, we must adjust
2007 	 * the index.  We must also track the entry just following the
2008 	 * new entry for use in an "atomic rename" operation, that entry
2009 	 * is always the "old" entry and the "new" entry is what we are
2010 	 * inserting.  The index/blkno fields refer to the "old" entry,
2011 	 * while the index2/blkno2 fields refer to the "new" entry.
2012 	 */
2013 	if (blk1->index > ichdr1.count) {
2014 		ASSERT(state->inleaf == 0);
2015 		blk2->index = blk1->index - ichdr1.count;
2016 		args->index = args->index2 = blk2->index;
2017 		args->blkno = args->blkno2 = blk2->blkno;
2018 	} else if (blk1->index == ichdr1.count) {
2019 		if (state->inleaf) {
2020 			args->index = blk1->index;
2021 			args->blkno = blk1->blkno;
2022 			args->index2 = 0;
2023 			args->blkno2 = blk2->blkno;
2024 		} else {
2025 			/*
2026 			 * On a double leaf split, the original attr location
2027 			 * is already stored in blkno2/index2, so don't
2028 			 * overwrite it overwise we corrupt the tree.
2029 			 */
2030 			blk2->index = blk1->index - ichdr1.count;
2031 			args->index = blk2->index;
2032 			args->blkno = blk2->blkno;
2033 			if (!state->extravalid) {
2034 				/*
2035 				 * set the new attr location to match the old
2036 				 * one and let the higher level split code
2037 				 * decide where in the leaf to place it.
2038 				 */
2039 				args->index2 = blk2->index;
2040 				args->blkno2 = blk2->blkno;
2041 			}
2042 		}
2043 	} else {
2044 		ASSERT(state->inleaf == 1);
2045 		args->index = args->index2 = blk1->index;
2046 		args->blkno = args->blkno2 = blk1->blkno;
2047 	}
2048 }
2049 
2050 /*
2051  * Examine entries until we reduce the absolute difference in
2052  * byte usage between the two blocks to a minimum.
2053  * GROT: Is this really necessary?  With other than a 512 byte blocksize,
2054  * GROT: there will always be enough room in either block for a new entry.
2055  * GROT: Do a double-split for this case?
2056  */
2057 STATIC int
xfs_attr3_leaf_figure_balance(struct xfs_da_state * state,struct xfs_da_state_blk * blk1,struct xfs_attr3_icleaf_hdr * ichdr1,struct xfs_da_state_blk * blk2,struct xfs_attr3_icleaf_hdr * ichdr2,int * countarg,int * usedbytesarg)2058 xfs_attr3_leaf_figure_balance(
2059 	struct xfs_da_state		*state,
2060 	struct xfs_da_state_blk		*blk1,
2061 	struct xfs_attr3_icleaf_hdr	*ichdr1,
2062 	struct xfs_da_state_blk		*blk2,
2063 	struct xfs_attr3_icleaf_hdr	*ichdr2,
2064 	int				*countarg,
2065 	int				*usedbytesarg)
2066 {
2067 	struct xfs_attr_leafblock	*leaf1 = blk1->bp->b_addr;
2068 	struct xfs_attr_leafblock	*leaf2 = blk2->bp->b_addr;
2069 	struct xfs_attr_leaf_entry	*entry;
2070 	int				count;
2071 	int				max;
2072 	int				index;
2073 	int				totallen = 0;
2074 	int				half;
2075 	int				lastdelta;
2076 	int				foundit = 0;
2077 	int				tmp;
2078 
2079 	/*
2080 	 * Examine entries until we reduce the absolute difference in
2081 	 * byte usage between the two blocks to a minimum.
2082 	 */
2083 	max = ichdr1->count + ichdr2->count;
2084 	half = (max + 1) * sizeof(*entry);
2085 	half += ichdr1->usedbytes + ichdr2->usedbytes +
2086 			xfs_attr_leaf_newentsize(state->args, NULL);
2087 	half /= 2;
2088 	lastdelta = state->args->geo->blksize;
2089 	entry = xfs_attr3_leaf_entryp(leaf1);
2090 	for (count = index = 0; count < max; entry++, index++, count++) {
2091 
2092 #define XFS_ATTR_ABS(A)	(((A) < 0) ? -(A) : (A))
2093 		/*
2094 		 * The new entry is in the first block, account for it.
2095 		 */
2096 		if (count == blk1->index) {
2097 			tmp = totallen + sizeof(*entry) +
2098 				xfs_attr_leaf_newentsize(state->args, NULL);
2099 			if (XFS_ATTR_ABS(half - tmp) > lastdelta)
2100 				break;
2101 			lastdelta = XFS_ATTR_ABS(half - tmp);
2102 			totallen = tmp;
2103 			foundit = 1;
2104 		}
2105 
2106 		/*
2107 		 * Wrap around into the second block if necessary.
2108 		 */
2109 		if (count == ichdr1->count) {
2110 			leaf1 = leaf2;
2111 			entry = xfs_attr3_leaf_entryp(leaf1);
2112 			index = 0;
2113 		}
2114 
2115 		/*
2116 		 * Figure out if next leaf entry would be too much.
2117 		 */
2118 		tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
2119 									index);
2120 		if (XFS_ATTR_ABS(half - tmp) > lastdelta)
2121 			break;
2122 		lastdelta = XFS_ATTR_ABS(half - tmp);
2123 		totallen = tmp;
2124 #undef XFS_ATTR_ABS
2125 	}
2126 
2127 	/*
2128 	 * Calculate the number of usedbytes that will end up in lower block.
2129 	 * If new entry not in lower block, fix up the count.
2130 	 */
2131 	totallen -= count * sizeof(*entry);
2132 	if (foundit) {
2133 		totallen -= sizeof(*entry) +
2134 				xfs_attr_leaf_newentsize(state->args, NULL);
2135 	}
2136 
2137 	*countarg = count;
2138 	*usedbytesarg = totallen;
2139 	return foundit;
2140 }
2141 
2142 /*========================================================================
2143  * Routines used for shrinking the Btree.
2144  *========================================================================*/
2145 
2146 /*
2147  * Check a leaf block and its neighbors to see if the block should be
2148  * collapsed into one or the other neighbor.  Always keep the block
2149  * with the smaller block number.
2150  * If the current block is over 50% full, don't try to join it, return 0.
2151  * If the block is empty, fill in the state structure and return 2.
2152  * If it can be collapsed, fill in the state structure and return 1.
2153  * If nothing can be done, return 0.
2154  *
2155  * GROT: allow for INCOMPLETE entries in calculation.
2156  */
2157 int
xfs_attr3_leaf_toosmall(struct xfs_da_state * state,int * action)2158 xfs_attr3_leaf_toosmall(
2159 	struct xfs_da_state	*state,
2160 	int			*action)
2161 {
2162 	struct xfs_attr_leafblock *leaf;
2163 	struct xfs_da_state_blk	*blk;
2164 	struct xfs_attr3_icleaf_hdr ichdr;
2165 	struct xfs_buf		*bp;
2166 	xfs_dablk_t		blkno;
2167 	int			bytes;
2168 	int			forward;
2169 	int			error;
2170 	int			retval;
2171 	int			i;
2172 
2173 	trace_xfs_attr_leaf_toosmall(state->args);
2174 
2175 	/*
2176 	 * Check for the degenerate case of the block being over 50% full.
2177 	 * If so, it's not worth even looking to see if we might be able
2178 	 * to coalesce with a sibling.
2179 	 */
2180 	blk = &state->path.blk[ state->path.active-1 ];
2181 	leaf = blk->bp->b_addr;
2182 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
2183 	bytes = xfs_attr_leaf_entries_end(ichdr.count, leaf) + ichdr.usedbytes;
2184 	if (bytes > (state->args->geo->blksize >> 1)) {
2185 		*action = 0;	/* blk over 50%, don't try to join */
2186 		return 0;
2187 	}
2188 
2189 	/*
2190 	 * Check for the degenerate case of the block being empty.
2191 	 * If the block is empty, we'll simply delete it, no need to
2192 	 * coalesce it with a sibling block.  We choose (arbitrarily)
2193 	 * to merge with the forward block unless it is NULL.
2194 	 */
2195 	if (ichdr.count == 0) {
2196 		/*
2197 		 * Make altpath point to the block we want to keep and
2198 		 * path point to the block we want to drop (this one).
2199 		 */
2200 		forward = (ichdr.forw != 0);
2201 		memcpy(&state->altpath, &state->path, sizeof(state->path));
2202 		error = xfs_da3_path_shift(state, &state->altpath, forward,
2203 						 0, &retval);
2204 		if (error)
2205 			return error;
2206 		if (retval) {
2207 			*action = 0;
2208 		} else {
2209 			*action = 2;
2210 		}
2211 		return 0;
2212 	}
2213 
2214 	/*
2215 	 * Examine each sibling block to see if we can coalesce with
2216 	 * at least 25% free space to spare.  We need to figure out
2217 	 * whether to merge with the forward or the backward block.
2218 	 * We prefer coalescing with the lower numbered sibling so as
2219 	 * to shrink an attribute list over time.
2220 	 */
2221 	/* start with smaller blk num */
2222 	forward = ichdr.forw < ichdr.back;
2223 	for (i = 0; i < 2; forward = !forward, i++) {
2224 		struct xfs_attr3_icleaf_hdr ichdr2;
2225 		if (forward)
2226 			blkno = ichdr.forw;
2227 		else
2228 			blkno = ichdr.back;
2229 		if (blkno == 0)
2230 			continue;
2231 		error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
2232 					state->args->owner, blkno, &bp);
2233 		if (error)
2234 			return error;
2235 
2236 		xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
2237 
2238 		bytes = state->args->geo->blksize -
2239 			(state->args->geo->blksize >> 2) -
2240 			ichdr.usedbytes - ichdr2.usedbytes -
2241 			xfs_attr_leaf_entries_end(ichdr.count + ichdr2.count,
2242 					leaf);
2243 
2244 		xfs_trans_brelse(state->args->trans, bp);
2245 		if (bytes >= 0)
2246 			break;	/* fits with at least 25% to spare */
2247 	}
2248 	if (i >= 2) {
2249 		*action = 0;
2250 		return 0;
2251 	}
2252 
2253 	/*
2254 	 * Make altpath point to the block we want to keep (the lower
2255 	 * numbered block) and path point to the block we want to drop.
2256 	 */
2257 	memcpy(&state->altpath, &state->path, sizeof(state->path));
2258 	if (blkno < blk->blkno) {
2259 		error = xfs_da3_path_shift(state, &state->altpath, forward,
2260 						 0, &retval);
2261 	} else {
2262 		error = xfs_da3_path_shift(state, &state->path, forward,
2263 						 0, &retval);
2264 	}
2265 	if (error)
2266 		return error;
2267 	if (retval) {
2268 		*action = 0;
2269 	} else {
2270 		*action = 1;
2271 	}
2272 	return 0;
2273 }
2274 
2275 /*
2276  * Remove a name from the leaf attribute list structure.
2277  *
2278  * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
2279  * If two leaves are 37% full, when combined they will leave 25% free.
2280  */
2281 int
xfs_attr3_leaf_remove(struct xfs_buf * bp,struct xfs_da_args * args)2282 xfs_attr3_leaf_remove(
2283 	struct xfs_buf		*bp,
2284 	struct xfs_da_args	*args)
2285 {
2286 	struct xfs_attr_leafblock *leaf;
2287 	struct xfs_attr3_icleaf_hdr ichdr;
2288 	struct xfs_attr_leaf_entry *entry;
2289 	int			before;
2290 	int			after;
2291 	int			smallest;
2292 	int			entsize;
2293 	int			tablesize;
2294 	int			tmp;
2295 	int			i;
2296 
2297 	trace_xfs_attr_leaf_remove(args);
2298 
2299 	leaf = bp->b_addr;
2300 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2301 
2302 	ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
2303 	ASSERT(args->index >= 0 && args->index < ichdr.count);
2304 	ASSERT(ichdr.firstused >= xfs_attr_leaf_entries_end(ichdr.count, leaf));
2305 
2306 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2307 
2308 	ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2309 	ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2310 
2311 	/*
2312 	 * Scan through free region table:
2313 	 *    check for adjacency of free'd entry with an existing one,
2314 	 *    find smallest free region in case we need to replace it,
2315 	 *    adjust any map that borders the entry table,
2316 	 */
2317 	tablesize = xfs_attr_leaf_entries_end(ichdr.count, leaf);
2318 	tmp = ichdr.freemap[0].size;
2319 	before = after = -1;
2320 	smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2321 	entsize = xfs_attr_leaf_entsize(leaf, args->index);
2322 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
2323 		ASSERT(ichdr.freemap[i].base < args->geo->blksize);
2324 		ASSERT(ichdr.freemap[i].size < args->geo->blksize);
2325 		if (ichdr.freemap[i].base == tablesize) {
2326 			ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2327 			ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
2328 		}
2329 
2330 		if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2331 				be16_to_cpu(entry->nameidx)) {
2332 			before = i;
2333 		} else if (ichdr.freemap[i].base ==
2334 				(be16_to_cpu(entry->nameidx) + entsize)) {
2335 			after = i;
2336 		} else if (ichdr.freemap[i].size < tmp) {
2337 			tmp = ichdr.freemap[i].size;
2338 			smallest = i;
2339 		}
2340 	}
2341 
2342 	/*
2343 	 * Coalesce adjacent freemap regions,
2344 	 * or replace the smallest region.
2345 	 */
2346 	if ((before >= 0) || (after >= 0)) {
2347 		if ((before >= 0) && (after >= 0)) {
2348 			ichdr.freemap[before].size += entsize;
2349 			ichdr.freemap[before].size += ichdr.freemap[after].size;
2350 			ichdr.freemap[after].base = 0;
2351 			ichdr.freemap[after].size = 0;
2352 		} else if (before >= 0) {
2353 			ichdr.freemap[before].size += entsize;
2354 		} else {
2355 			ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2356 			ichdr.freemap[after].size += entsize;
2357 		}
2358 	} else {
2359 		/*
2360 		 * Replace smallest region (if it is smaller than free'd entry)
2361 		 */
2362 		if (ichdr.freemap[smallest].size < entsize) {
2363 			ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2364 			ichdr.freemap[smallest].size = entsize;
2365 		}
2366 	}
2367 
2368 	/*
2369 	 * Did we remove the first entry?
2370 	 */
2371 	if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
2372 		smallest = 1;
2373 	else
2374 		smallest = 0;
2375 
2376 	/*
2377 	 * Compress the remaining entries and zero out the removed stuff.
2378 	 */
2379 	memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2380 	ichdr.usedbytes -= entsize;
2381 	xfs_trans_log_buf(args->trans, bp,
2382 	     XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
2383 				   entsize));
2384 
2385 	tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2386 	memmove(entry, entry + 1, tmp);
2387 	ichdr.count--;
2388 	xfs_trans_log_buf(args->trans, bp,
2389 	    XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2390 
2391 	entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2392 	memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
2393 
2394 	/*
2395 	 * If we removed the first entry, re-find the first used byte
2396 	 * in the name area.  Note that if the entry was the "firstused",
2397 	 * then we don't have a "hole" in our block resulting from
2398 	 * removing the name.
2399 	 */
2400 	if (smallest) {
2401 		tmp = args->geo->blksize;
2402 		entry = xfs_attr3_leaf_entryp(leaf);
2403 		for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2404 			ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2405 			ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2406 
2407 			if (be16_to_cpu(entry->nameidx) < tmp)
2408 				tmp = be16_to_cpu(entry->nameidx);
2409 		}
2410 		ichdr.firstused = tmp;
2411 		ASSERT(ichdr.firstused != 0);
2412 	} else {
2413 		ichdr.holes = 1;	/* mark as needing compaction */
2414 	}
2415 	xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
2416 	xfs_trans_log_buf(args->trans, bp,
2417 			  XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2418 					  xfs_attr3_leaf_hdr_size(leaf)));
2419 
2420 	/*
2421 	 * Check if leaf is less than 50% full, caller may want to
2422 	 * "join" the leaf with a sibling if so.
2423 	 */
2424 	tmp = ichdr.usedbytes + xfs_attr_leaf_entries_end(ichdr.count, leaf);
2425 
2426 	return tmp < args->geo->magicpct; /* leaf is < 37% full */
2427 }
2428 
2429 /*
2430  * Move all the attribute list entries from drop_leaf into save_leaf.
2431  */
2432 void
xfs_attr3_leaf_unbalance(struct xfs_da_state * state,struct xfs_da_state_blk * drop_blk,struct xfs_da_state_blk * save_blk)2433 xfs_attr3_leaf_unbalance(
2434 	struct xfs_da_state	*state,
2435 	struct xfs_da_state_blk	*drop_blk,
2436 	struct xfs_da_state_blk	*save_blk)
2437 {
2438 	struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2439 	struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2440 	struct xfs_attr3_icleaf_hdr drophdr;
2441 	struct xfs_attr3_icleaf_hdr savehdr;
2442 	struct xfs_attr_leaf_entry *entry;
2443 
2444 	trace_xfs_attr_leaf_unbalance(state->args);
2445 
2446 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
2447 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
2448 	entry = xfs_attr3_leaf_entryp(drop_leaf);
2449 
2450 	/*
2451 	 * Save last hashval from dying block for later Btree fixup.
2452 	 */
2453 	drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
2454 
2455 	/*
2456 	 * Check if we need a temp buffer, or can we do it in place.
2457 	 * Note that we don't check "leaf" for holes because we will
2458 	 * always be dropping it, toosmall() decided that for us already.
2459 	 */
2460 	if (savehdr.holes == 0) {
2461 		/*
2462 		 * dest leaf has no holes, so we add there.  May need
2463 		 * to make some room in the entry array.
2464 		 */
2465 		if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2466 					 drop_blk->bp, &drophdr)) {
2467 			xfs_attr3_leaf_moveents(state->args,
2468 						drop_leaf, &drophdr, 0,
2469 						save_leaf, &savehdr, 0,
2470 						drophdr.count);
2471 		} else {
2472 			xfs_attr3_leaf_moveents(state->args,
2473 						drop_leaf, &drophdr, 0,
2474 						save_leaf, &savehdr,
2475 						savehdr.count, drophdr.count);
2476 		}
2477 	} else {
2478 		/*
2479 		 * Destination has holes, so we make a temporary copy
2480 		 * of the leaf and add them both to that.
2481 		 */
2482 		struct xfs_attr_leafblock *tmp_leaf;
2483 		struct xfs_attr3_icleaf_hdr tmphdr;
2484 
2485 		tmp_leaf = kvzalloc(state->args->geo->blksize,
2486 				GFP_KERNEL | __GFP_NOFAIL);
2487 
2488 		/*
2489 		 * Copy the header into the temp leaf so that all the stuff
2490 		 * not in the incore header is present and gets copied back in
2491 		 * once we've moved all the entries.
2492 		 */
2493 		memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2494 
2495 		memset(&tmphdr, 0, sizeof(tmphdr));
2496 		tmphdr.magic = savehdr.magic;
2497 		tmphdr.forw = savehdr.forw;
2498 		tmphdr.back = savehdr.back;
2499 		tmphdr.firstused = state->args->geo->blksize;
2500 
2501 		/* write the header to the temp buffer to initialise it */
2502 		xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
2503 
2504 		if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2505 					 drop_blk->bp, &drophdr)) {
2506 			xfs_attr3_leaf_moveents(state->args,
2507 						drop_leaf, &drophdr, 0,
2508 						tmp_leaf, &tmphdr, 0,
2509 						drophdr.count);
2510 			xfs_attr3_leaf_moveents(state->args,
2511 						save_leaf, &savehdr, 0,
2512 						tmp_leaf, &tmphdr, tmphdr.count,
2513 						savehdr.count);
2514 		} else {
2515 			xfs_attr3_leaf_moveents(state->args,
2516 						save_leaf, &savehdr, 0,
2517 						tmp_leaf, &tmphdr, 0,
2518 						savehdr.count);
2519 			xfs_attr3_leaf_moveents(state->args,
2520 						drop_leaf, &drophdr, 0,
2521 						tmp_leaf, &tmphdr, tmphdr.count,
2522 						drophdr.count);
2523 		}
2524 		memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
2525 		savehdr = tmphdr; /* struct copy */
2526 		kvfree(tmp_leaf);
2527 	}
2528 
2529 	xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
2530 	xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
2531 					   state->args->geo->blksize - 1);
2532 
2533 	/*
2534 	 * Copy out last hashval in each block for B-tree code.
2535 	 */
2536 	entry = xfs_attr3_leaf_entryp(save_leaf);
2537 	save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
2538 }
2539 
2540 /*========================================================================
2541  * Routines used for finding things in the Btree.
2542  *========================================================================*/
2543 
2544 /*
2545  * Look up a name in a leaf attribute list structure.
2546  * This is the internal routine, it uses the caller's buffer.
2547  *
2548  * Note that duplicate keys are allowed, but only check within the
2549  * current leaf node.  The Btree code must check in adjacent leaf nodes.
2550  *
2551  * Return in args->index the index into the entry[] array of either
2552  * the found entry, or where the entry should have been (insert before
2553  * that entry).
2554  *
2555  * Don't change the args->value unless we find the attribute.
2556  */
2557 int
xfs_attr3_leaf_lookup_int(struct xfs_buf * bp,struct xfs_da_args * args)2558 xfs_attr3_leaf_lookup_int(
2559 	struct xfs_buf		*bp,
2560 	struct xfs_da_args	*args)
2561 {
2562 	struct xfs_attr_leafblock *leaf;
2563 	struct xfs_attr3_icleaf_hdr ichdr;
2564 	struct xfs_attr_leaf_entry *entry;
2565 	struct xfs_attr_leaf_entry *entries;
2566 	struct xfs_attr_leaf_name_local *name_loc;
2567 	struct xfs_attr_leaf_name_remote *name_rmt;
2568 	xfs_dahash_t		hashval;
2569 	int			probe;
2570 	int			span;
2571 
2572 	trace_xfs_attr_leaf_lookup(args);
2573 
2574 	leaf = bp->b_addr;
2575 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2576 	entries = xfs_attr3_leaf_entryp(leaf);
2577 	if (ichdr.count >= args->geo->blksize / 8) {
2578 		xfs_buf_mark_corrupt(bp);
2579 		xfs_da_mark_sick(args);
2580 		return -EFSCORRUPTED;
2581 	}
2582 
2583 	/*
2584 	 * Binary search.  (note: small blocks will skip this loop)
2585 	 */
2586 	hashval = args->hashval;
2587 	probe = span = ichdr.count / 2;
2588 	for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
2589 		span /= 2;
2590 		if (be32_to_cpu(entry->hashval) < hashval)
2591 			probe += span;
2592 		else if (be32_to_cpu(entry->hashval) > hashval)
2593 			probe -= span;
2594 		else
2595 			break;
2596 	}
2597 	if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count))) {
2598 		xfs_buf_mark_corrupt(bp);
2599 		xfs_da_mark_sick(args);
2600 		return -EFSCORRUPTED;
2601 	}
2602 	if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval)) {
2603 		xfs_buf_mark_corrupt(bp);
2604 		xfs_da_mark_sick(args);
2605 		return -EFSCORRUPTED;
2606 	}
2607 
2608 	/*
2609 	 * Since we may have duplicate hashval's, find the first matching
2610 	 * hashval in the leaf.
2611 	 */
2612 	while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
2613 		entry--;
2614 		probe--;
2615 	}
2616 	while (probe < ichdr.count &&
2617 	       be32_to_cpu(entry->hashval) < hashval) {
2618 		entry++;
2619 		probe++;
2620 	}
2621 	if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
2622 		args->index = probe;
2623 		return -ENOATTR;
2624 	}
2625 
2626 	/*
2627 	 * Duplicate keys may be present, so search all of them for a match.
2628 	 */
2629 	for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
2630 			entry++, probe++) {
2631 /*
2632  * GROT: Add code to remove incomplete entries.
2633  */
2634 		if (entry->flags & XFS_ATTR_LOCAL) {
2635 			name_loc = xfs_attr3_leaf_name_local(leaf, probe);
2636 			if (!xfs_attr_match(args, entry->flags,
2637 					name_loc->nameval, name_loc->namelen,
2638 					&name_loc->nameval[name_loc->namelen],
2639 					be16_to_cpu(name_loc->valuelen)))
2640 				continue;
2641 			args->index = probe;
2642 			return -EEXIST;
2643 		} else {
2644 			unsigned int	valuelen;
2645 
2646 			name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
2647 			valuelen = be32_to_cpu(name_rmt->valuelen);
2648 			if (!xfs_attr_match(args, entry->flags, name_rmt->name,
2649 					name_rmt->namelen, NULL, valuelen))
2650 				continue;
2651 			args->index = probe;
2652 			args->rmtvaluelen = valuelen;
2653 			args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2654 			args->rmtblkcnt = xfs_attr3_rmt_blocks(
2655 							args->dp->i_mount,
2656 							args->rmtvaluelen);
2657 			return -EEXIST;
2658 		}
2659 	}
2660 	args->index = probe;
2661 	return -ENOATTR;
2662 }
2663 
2664 /*
2665  * Get the value associated with an attribute name from a leaf attribute
2666  * list structure.
2667  *
2668  * If args->valuelen is zero, only the length needs to be returned.  Unlike a
2669  * lookup, we only return an error if the attribute does not exist or we can't
2670  * retrieve the value.
2671  */
2672 int
xfs_attr3_leaf_getvalue(struct xfs_buf * bp,struct xfs_da_args * args)2673 xfs_attr3_leaf_getvalue(
2674 	struct xfs_buf		*bp,
2675 	struct xfs_da_args	*args)
2676 {
2677 	struct xfs_attr_leafblock *leaf;
2678 	struct xfs_attr3_icleaf_hdr ichdr;
2679 	struct xfs_attr_leaf_entry *entry;
2680 	struct xfs_attr_leaf_name_local *name_loc;
2681 	struct xfs_attr_leaf_name_remote *name_rmt;
2682 
2683 	leaf = bp->b_addr;
2684 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2685 	ASSERT(ichdr.count < args->geo->blksize / 8);
2686 	ASSERT(args->index < ichdr.count);
2687 
2688 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2689 	if (entry->flags & XFS_ATTR_LOCAL) {
2690 		name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2691 		ASSERT(name_loc->namelen == args->namelen);
2692 		ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2693 		return xfs_attr_copy_value(args,
2694 					&name_loc->nameval[args->namelen],
2695 					be16_to_cpu(name_loc->valuelen));
2696 	}
2697 
2698 	name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2699 	ASSERT(name_rmt->namelen == args->namelen);
2700 	ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2701 	args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2702 	args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2703 	args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
2704 					       args->rmtvaluelen);
2705 	return xfs_attr_copy_value(args, NULL, args->rmtvaluelen);
2706 }
2707 
2708 /*========================================================================
2709  * Utility routines.
2710  *========================================================================*/
2711 
2712 /*
2713  * Move the indicated entries from one leaf to another.
2714  * NOTE: this routine modifies both source and destination leaves.
2715  */
2716 /*ARGSUSED*/
2717 STATIC void
xfs_attr3_leaf_moveents(struct xfs_da_args * args,struct xfs_attr_leafblock * leaf_s,struct xfs_attr3_icleaf_hdr * ichdr_s,int start_s,struct xfs_attr_leafblock * leaf_d,struct xfs_attr3_icleaf_hdr * ichdr_d,int start_d,int count)2718 xfs_attr3_leaf_moveents(
2719 	struct xfs_da_args		*args,
2720 	struct xfs_attr_leafblock	*leaf_s,
2721 	struct xfs_attr3_icleaf_hdr	*ichdr_s,
2722 	int				start_s,
2723 	struct xfs_attr_leafblock	*leaf_d,
2724 	struct xfs_attr3_icleaf_hdr	*ichdr_d,
2725 	int				start_d,
2726 	int				count)
2727 {
2728 	struct xfs_attr_leaf_entry	*entry_s;
2729 	struct xfs_attr_leaf_entry	*entry_d;
2730 	int				desti;
2731 	int				tmp;
2732 	int				i;
2733 
2734 	/*
2735 	 * Check for nothing to do.
2736 	 */
2737 	if (count == 0)
2738 		return;
2739 
2740 	/*
2741 	 * Set up environment.
2742 	 */
2743 	ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2744 	       ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2745 	ASSERT(ichdr_s->magic == ichdr_d->magic);
2746 	ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
2747 	ASSERT(ichdr_s->firstused >=
2748 			xfs_attr_leaf_entries_end(ichdr_s->count, leaf_s));
2749 	ASSERT(ichdr_d->count < args->geo->blksize / 8);
2750 	ASSERT(ichdr_d->firstused >=
2751 			xfs_attr_leaf_entries_end(ichdr_d->count, leaf_d));
2752 
2753 	ASSERT(start_s < ichdr_s->count);
2754 	ASSERT(start_d <= ichdr_d->count);
2755 	ASSERT(count <= ichdr_s->count);
2756 
2757 
2758 	/*
2759 	 * Move the entries in the destination leaf up to make a hole?
2760 	 */
2761 	if (start_d < ichdr_d->count) {
2762 		tmp  = ichdr_d->count - start_d;
2763 		tmp *= sizeof(xfs_attr_leaf_entry_t);
2764 		entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2765 		entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2766 		memmove(entry_d, entry_s, tmp);
2767 	}
2768 
2769 	/*
2770 	 * Copy all entry's in the same (sorted) order,
2771 	 * but allocate attribute info packed and in sequence.
2772 	 */
2773 	entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2774 	entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2775 	desti = start_d;
2776 	for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2777 		ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
2778 		tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2779 #ifdef GROT
2780 		/*
2781 		 * Code to drop INCOMPLETE entries.  Difficult to use as we
2782 		 * may also need to change the insertion index.  Code turned
2783 		 * off for 6.2, should be revisited later.
2784 		 */
2785 		if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2786 			memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2787 			ichdr_s->usedbytes -= tmp;
2788 			ichdr_s->count -= 1;
2789 			entry_d--;	/* to compensate for ++ in loop hdr */
2790 			desti--;
2791 			if ((start_s + i) < offset)
2792 				result++;	/* insertion index adjustment */
2793 		} else {
2794 #endif /* GROT */
2795 			ichdr_d->firstused -= tmp;
2796 			/* both on-disk, don't endian flip twice */
2797 			entry_d->hashval = entry_s->hashval;
2798 			entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
2799 			entry_d->flags = entry_s->flags;
2800 			ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2801 							<= args->geo->blksize);
2802 			memmove(xfs_attr3_leaf_name(leaf_d, desti),
2803 				xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
2804 			ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2805 							<= args->geo->blksize);
2806 			memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2807 			ichdr_s->usedbytes -= tmp;
2808 			ichdr_d->usedbytes += tmp;
2809 			ichdr_s->count -= 1;
2810 			ichdr_d->count += 1;
2811 			tmp = xfs_attr_leaf_entries_end(ichdr_d->count, leaf_d);
2812 			ASSERT(ichdr_d->firstused >= tmp);
2813 #ifdef GROT
2814 		}
2815 #endif /* GROT */
2816 	}
2817 
2818 	/*
2819 	 * Zero out the entries we just copied.
2820 	 */
2821 	if (start_s == ichdr_s->count) {
2822 		tmp = count * sizeof(xfs_attr_leaf_entry_t);
2823 		entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2824 		ASSERT(((char *)entry_s + tmp) <=
2825 		       ((char *)leaf_s + args->geo->blksize));
2826 		memset(entry_s, 0, tmp);
2827 	} else {
2828 		/*
2829 		 * Move the remaining entries down to fill the hole,
2830 		 * then zero the entries at the top.
2831 		 */
2832 		tmp  = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2833 		entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2834 		entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2835 		memmove(entry_d, entry_s, tmp);
2836 
2837 		tmp = count * sizeof(xfs_attr_leaf_entry_t);
2838 		entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
2839 		ASSERT(((char *)entry_s + tmp) <=
2840 		       ((char *)leaf_s + args->geo->blksize));
2841 		memset(entry_s, 0, tmp);
2842 	}
2843 
2844 	/*
2845 	 * Fill in the freemap information
2846 	 */
2847 	ichdr_d->freemap[0].base =
2848 		xfs_attr_leaf_entries_end(ichdr_d->count, leaf_d);
2849 	ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2850 	ichdr_d->freemap[1].base = 0;
2851 	ichdr_d->freemap[2].base = 0;
2852 	ichdr_d->freemap[1].size = 0;
2853 	ichdr_d->freemap[2].size = 0;
2854 	ichdr_s->holes = 1;	/* leaf may not be compact */
2855 }
2856 
2857 /*
2858  * Pick up the last hashvalue from a leaf block.
2859  */
2860 xfs_dahash_t
xfs_attr_leaf_lasthash(struct xfs_buf * bp,int * count)2861 xfs_attr_leaf_lasthash(
2862 	struct xfs_buf	*bp,
2863 	int		*count)
2864 {
2865 	struct xfs_attr3_icleaf_hdr ichdr;
2866 	struct xfs_attr_leaf_entry *entries;
2867 	struct xfs_mount *mp = bp->b_mount;
2868 
2869 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
2870 	entries = xfs_attr3_leaf_entryp(bp->b_addr);
2871 	if (count)
2872 		*count = ichdr.count;
2873 	if (!ichdr.count)
2874 		return 0;
2875 	return be32_to_cpu(entries[ichdr.count - 1].hashval);
2876 }
2877 
2878 /*
2879  * Calculate the number of bytes used to store the indicated attribute
2880  * (whether local or remote only calculate bytes in this block).
2881  */
2882 STATIC int
xfs_attr_leaf_entsize(xfs_attr_leafblock_t * leaf,int index)2883 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2884 {
2885 	struct xfs_attr_leaf_entry *entries;
2886 	xfs_attr_leaf_name_local_t *name_loc;
2887 	xfs_attr_leaf_name_remote_t *name_rmt;
2888 	int size;
2889 
2890 	entries = xfs_attr3_leaf_entryp(leaf);
2891 	if (entries[index].flags & XFS_ATTR_LOCAL) {
2892 		name_loc = xfs_attr3_leaf_name_local(leaf, index);
2893 		size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2894 						   be16_to_cpu(name_loc->valuelen));
2895 	} else {
2896 		name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
2897 		size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2898 	}
2899 	return size;
2900 }
2901 
2902 /*
2903  * Calculate the number of bytes that would be required to store the new
2904  * attribute (whether local or remote only calculate bytes in this block).
2905  * This routine decides as a side effect whether the attribute will be
2906  * a "local" or a "remote" attribute.
2907  */
2908 int
xfs_attr_leaf_newentsize(struct xfs_da_args * args,int * local)2909 xfs_attr_leaf_newentsize(
2910 	struct xfs_da_args	*args,
2911 	int			*local)
2912 {
2913 	int			size;
2914 
2915 	size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
2916 	if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
2917 		if (local)
2918 			*local = 1;
2919 		return size;
2920 	}
2921 	if (local)
2922 		*local = 0;
2923 	return xfs_attr_leaf_entsize_remote(args->namelen);
2924 }
2925 
2926 
2927 /*========================================================================
2928  * Manage the INCOMPLETE flag in a leaf entry
2929  *========================================================================*/
2930 
2931 /*
2932  * Clear the INCOMPLETE flag on an entry in a leaf block.
2933  */
2934 int
xfs_attr3_leaf_clearflag(struct xfs_da_args * args)2935 xfs_attr3_leaf_clearflag(
2936 	struct xfs_da_args	*args)
2937 {
2938 	struct xfs_attr_leafblock *leaf;
2939 	struct xfs_attr_leaf_entry *entry;
2940 	struct xfs_attr_leaf_name_remote *name_rmt;
2941 	struct xfs_buf		*bp;
2942 	int			error;
2943 #ifdef DEBUG
2944 	struct xfs_attr3_icleaf_hdr ichdr;
2945 	xfs_attr_leaf_name_local_t *name_loc;
2946 	int namelen;
2947 	char *name;
2948 #endif /* DEBUG */
2949 
2950 	trace_xfs_attr_leaf_clearflag(args);
2951 	/*
2952 	 * Set up the operation.
2953 	 */
2954 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
2955 			args->blkno, &bp);
2956 	if (error)
2957 		return error;
2958 
2959 	leaf = bp->b_addr;
2960 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2961 	ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2962 
2963 #ifdef DEBUG
2964 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2965 	ASSERT(args->index < ichdr.count);
2966 	ASSERT(args->index >= 0);
2967 
2968 	if (entry->flags & XFS_ATTR_LOCAL) {
2969 		name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2970 		namelen = name_loc->namelen;
2971 		name = (char *)name_loc->nameval;
2972 	} else {
2973 		name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2974 		namelen = name_rmt->namelen;
2975 		name = (char *)name_rmt->name;
2976 	}
2977 	ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2978 	ASSERT(namelen == args->namelen);
2979 	ASSERT(memcmp(name, args->name, namelen) == 0);
2980 #endif /* DEBUG */
2981 
2982 	entry->flags &= ~XFS_ATTR_INCOMPLETE;
2983 	xfs_trans_log_buf(args->trans, bp,
2984 			 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2985 
2986 	if (args->rmtblkno) {
2987 		ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2988 		name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2989 		name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2990 		name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2991 		xfs_trans_log_buf(args->trans, bp,
2992 			 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2993 	}
2994 
2995 	return 0;
2996 }
2997 
2998 /*
2999  * Set the INCOMPLETE flag on an entry in a leaf block.
3000  */
3001 int
xfs_attr3_leaf_setflag(struct xfs_da_args * args)3002 xfs_attr3_leaf_setflag(
3003 	struct xfs_da_args	*args)
3004 {
3005 	struct xfs_attr_leafblock *leaf;
3006 	struct xfs_attr_leaf_entry *entry;
3007 	struct xfs_attr_leaf_name_remote *name_rmt;
3008 	struct xfs_buf		*bp;
3009 	int error;
3010 #ifdef DEBUG
3011 	struct xfs_attr3_icleaf_hdr ichdr;
3012 #endif
3013 
3014 	trace_xfs_attr_leaf_setflag(args);
3015 
3016 	/*
3017 	 * Set up the operation.
3018 	 */
3019 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
3020 			args->blkno, &bp);
3021 	if (error)
3022 		return error;
3023 
3024 	leaf = bp->b_addr;
3025 #ifdef DEBUG
3026 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
3027 	ASSERT(args->index < ichdr.count);
3028 	ASSERT(args->index >= 0);
3029 #endif
3030 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
3031 
3032 	ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
3033 	entry->flags |= XFS_ATTR_INCOMPLETE;
3034 	xfs_trans_log_buf(args->trans, bp,
3035 			XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
3036 	if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
3037 		name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
3038 		name_rmt->valueblk = 0;
3039 		name_rmt->valuelen = 0;
3040 		xfs_trans_log_buf(args->trans, bp,
3041 			 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
3042 	}
3043 
3044 	return 0;
3045 }
3046 
3047 /*
3048  * In a single transaction, clear the INCOMPLETE flag on the leaf entry
3049  * given by args->blkno/index and set the INCOMPLETE flag on the leaf
3050  * entry given by args->blkno2/index2.
3051  *
3052  * Note that they could be in different blocks, or in the same block.
3053  */
3054 int
xfs_attr3_leaf_flipflags(struct xfs_da_args * args)3055 xfs_attr3_leaf_flipflags(
3056 	struct xfs_da_args	*args)
3057 {
3058 	struct xfs_attr_leafblock *leaf1;
3059 	struct xfs_attr_leafblock *leaf2;
3060 	struct xfs_attr_leaf_entry *entry1;
3061 	struct xfs_attr_leaf_entry *entry2;
3062 	struct xfs_attr_leaf_name_remote *name_rmt;
3063 	struct xfs_buf		*bp1;
3064 	struct xfs_buf		*bp2;
3065 	int error;
3066 #ifdef DEBUG
3067 	struct xfs_attr3_icleaf_hdr ichdr1;
3068 	struct xfs_attr3_icleaf_hdr ichdr2;
3069 	xfs_attr_leaf_name_local_t *name_loc;
3070 	int namelen1, namelen2;
3071 	char *name1, *name2;
3072 #endif /* DEBUG */
3073 
3074 	trace_xfs_attr_leaf_flipflags(args);
3075 
3076 	/*
3077 	 * Read the block containing the "old" attr
3078 	 */
3079 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
3080 			args->blkno, &bp1);
3081 	if (error)
3082 		return error;
3083 
3084 	/*
3085 	 * Read the block containing the "new" attr, if it is different
3086 	 */
3087 	if (args->blkno2 != args->blkno) {
3088 		error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
3089 				args->blkno2, &bp2);
3090 		if (error)
3091 			return error;
3092 	} else {
3093 		bp2 = bp1;
3094 	}
3095 
3096 	leaf1 = bp1->b_addr;
3097 	entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
3098 
3099 	leaf2 = bp2->b_addr;
3100 	entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
3101 
3102 #ifdef DEBUG
3103 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
3104 	ASSERT(args->index < ichdr1.count);
3105 	ASSERT(args->index >= 0);
3106 
3107 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
3108 	ASSERT(args->index2 < ichdr2.count);
3109 	ASSERT(args->index2 >= 0);
3110 
3111 	if (entry1->flags & XFS_ATTR_LOCAL) {
3112 		name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
3113 		namelen1 = name_loc->namelen;
3114 		name1 = (char *)name_loc->nameval;
3115 	} else {
3116 		name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
3117 		namelen1 = name_rmt->namelen;
3118 		name1 = (char *)name_rmt->name;
3119 	}
3120 	if (entry2->flags & XFS_ATTR_LOCAL) {
3121 		name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
3122 		namelen2 = name_loc->namelen;
3123 		name2 = (char *)name_loc->nameval;
3124 	} else {
3125 		name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
3126 		namelen2 = name_rmt->namelen;
3127 		name2 = (char *)name_rmt->name;
3128 	}
3129 	ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
3130 	ASSERT(namelen1 == namelen2);
3131 	ASSERT(memcmp(name1, name2, namelen1) == 0);
3132 #endif /* DEBUG */
3133 
3134 	ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
3135 	ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
3136 
3137 	entry1->flags &= ~XFS_ATTR_INCOMPLETE;
3138 	xfs_trans_log_buf(args->trans, bp1,
3139 			  XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
3140 	if (args->rmtblkno) {
3141 		ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
3142 		name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
3143 		name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
3144 		name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
3145 		xfs_trans_log_buf(args->trans, bp1,
3146 			 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
3147 	}
3148 
3149 	entry2->flags |= XFS_ATTR_INCOMPLETE;
3150 	xfs_trans_log_buf(args->trans, bp2,
3151 			  XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
3152 	if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
3153 		name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
3154 		name_rmt->valueblk = 0;
3155 		name_rmt->valuelen = 0;
3156 		xfs_trans_log_buf(args->trans, bp2,
3157 			 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
3158 	}
3159 
3160 	return 0;
3161 }
3162