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