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