]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_attr_leaf.c
xfs: move local to extent inode logging into bmap helper
[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(args->trans, dp, XFS_ATTR_FORK);
826
827 bp = NULL;
828 error = xfs_da_grow_inode(args, &blkno);
829 if (error)
830 goto out;
831
832 ASSERT(blkno == 0);
833 error = xfs_attr3_leaf_create(args, blkno, &bp);
834 if (error)
835 goto out;
836
837 memset((char *)&nargs, 0, sizeof(nargs));
838 nargs.dp = dp;
839 nargs.geo = args->geo;
840 nargs.total = args->total;
841 nargs.whichfork = XFS_ATTR_FORK;
842 nargs.trans = args->trans;
843 nargs.op_flags = XFS_DA_OP_OKNOENT;
844
845 sfe = &sf->list[0];
846 for (i = 0; i < sf->hdr.count; i++) {
847 nargs.name = sfe->nameval;
848 nargs.namelen = sfe->namelen;
849 nargs.value = &sfe->nameval[nargs.namelen];
850 nargs.valuelen = sfe->valuelen;
851 nargs.hashval = xfs_da_hashname(sfe->nameval,
852 sfe->namelen);
853 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
854 error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
855 ASSERT(error == -ENOATTR);
856 error = xfs_attr3_leaf_add(bp, &nargs);
857 ASSERT(error != -ENOSPC);
858 if (error)
859 goto out;
860 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
861 }
862 error = 0;
863 *leaf_bp = bp;
864 out:
865 kmem_free(tmpbuffer);
866 return error;
867 }
868
869 /*
870 * Check a leaf attribute block to see if all the entries would fit into
871 * a shortform attribute list.
872 */
873 int
874 xfs_attr_shortform_allfit(
875 struct xfs_buf *bp,
876 struct xfs_inode *dp)
877 {
878 struct xfs_attr_leafblock *leaf;
879 struct xfs_attr_leaf_entry *entry;
880 xfs_attr_leaf_name_local_t *name_loc;
881 struct xfs_attr3_icleaf_hdr leafhdr;
882 int bytes;
883 int i;
884 struct xfs_mount *mp = bp->b_mount;
885
886 leaf = bp->b_addr;
887 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
888 entry = xfs_attr3_leaf_entryp(leaf);
889
890 bytes = sizeof(struct xfs_attr_sf_hdr);
891 for (i = 0; i < leafhdr.count; entry++, i++) {
892 if (entry->flags & XFS_ATTR_INCOMPLETE)
893 continue; /* don't copy partial entries */
894 if (!(entry->flags & XFS_ATTR_LOCAL))
895 return 0;
896 name_loc = xfs_attr3_leaf_name_local(leaf, i);
897 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
898 return 0;
899 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
900 return 0;
901 bytes += sizeof(struct xfs_attr_sf_entry) - 1
902 + name_loc->namelen
903 + be16_to_cpu(name_loc->valuelen);
904 }
905 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
906 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
907 (bytes == sizeof(struct xfs_attr_sf_hdr)))
908 return -1;
909 return xfs_attr_shortform_bytesfit(dp, bytes);
910 }
911
912 /* Verify the consistency of an inline attribute fork. */
913 xfs_failaddr_t
914 xfs_attr_shortform_verify(
915 struct xfs_inode *ip)
916 {
917 struct xfs_attr_shortform *sfp;
918 struct xfs_attr_sf_entry *sfep;
919 struct xfs_attr_sf_entry *next_sfep;
920 char *endp;
921 struct xfs_ifork *ifp;
922 int i;
923 int size;
924
925 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL);
926 ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK);
927 sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
928 size = ifp->if_bytes;
929
930 /*
931 * Give up if the attribute is way too short.
932 */
933 if (size < sizeof(struct xfs_attr_sf_hdr))
934 return __this_address;
935
936 endp = (char *)sfp + size;
937
938 /* Check all reported entries */
939 sfep = &sfp->list[0];
940 for (i = 0; i < sfp->hdr.count; i++) {
941 /*
942 * struct xfs_attr_sf_entry has a variable length.
943 * Check the fixed-offset parts of the structure are
944 * within the data buffer.
945 */
946 if (((char *)sfep + sizeof(*sfep)) >= endp)
947 return __this_address;
948
949 /* Don't allow names with known bad length. */
950 if (sfep->namelen == 0)
951 return __this_address;
952
953 /*
954 * Check that the variable-length part of the structure is
955 * within the data buffer. The next entry starts after the
956 * name component, so nextentry is an acceptable test.
957 */
958 next_sfep = XFS_ATTR_SF_NEXTENTRY(sfep);
959 if ((char *)next_sfep > endp)
960 return __this_address;
961
962 /*
963 * Check for unknown flags. Short form doesn't support
964 * the incomplete or local bits, so we can use the namespace
965 * mask here.
966 */
967 if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK)
968 return __this_address;
969
970 /*
971 * Check for invalid namespace combinations. We only allow
972 * one namespace flag per xattr, so we can just count the
973 * bits (i.e. hweight) here.
974 */
975 if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
976 return __this_address;
977
978 sfep = next_sfep;
979 }
980 if ((void *)sfep != (void *)endp)
981 return __this_address;
982
983 return NULL;
984 }
985
986 /*
987 * Convert a leaf attribute list to shortform attribute list
988 */
989 int
990 xfs_attr3_leaf_to_shortform(
991 struct xfs_buf *bp,
992 struct xfs_da_args *args,
993 int forkoff)
994 {
995 struct xfs_attr_leafblock *leaf;
996 struct xfs_attr3_icleaf_hdr ichdr;
997 struct xfs_attr_leaf_entry *entry;
998 struct xfs_attr_leaf_name_local *name_loc;
999 struct xfs_da_args nargs;
1000 struct xfs_inode *dp = args->dp;
1001 char *tmpbuffer;
1002 int error;
1003 int i;
1004
1005 trace_xfs_attr_leaf_to_sf(args);
1006
1007 tmpbuffer = kmem_alloc(args->geo->blksize, 0);
1008 if (!tmpbuffer)
1009 return -ENOMEM;
1010
1011 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1012
1013 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1014 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1015 entry = xfs_attr3_leaf_entryp(leaf);
1016
1017 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
1018 memset(bp->b_addr, 0, args->geo->blksize);
1019
1020 /*
1021 * Clean out the prior contents of the attribute list.
1022 */
1023 error = xfs_da_shrink_inode(args, 0, bp);
1024 if (error)
1025 goto out;
1026
1027 if (forkoff == -1) {
1028 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
1029 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
1030 xfs_attr_fork_remove(dp, args->trans);
1031 goto out;
1032 }
1033
1034 xfs_attr_shortform_create(args);
1035
1036 /*
1037 * Copy the attributes
1038 */
1039 memset((char *)&nargs, 0, sizeof(nargs));
1040 nargs.geo = args->geo;
1041 nargs.dp = dp;
1042 nargs.total = args->total;
1043 nargs.whichfork = XFS_ATTR_FORK;
1044 nargs.trans = args->trans;
1045 nargs.op_flags = XFS_DA_OP_OKNOENT;
1046
1047 for (i = 0; i < ichdr.count; entry++, i++) {
1048 if (entry->flags & XFS_ATTR_INCOMPLETE)
1049 continue; /* don't copy partial entries */
1050 if (!entry->nameidx)
1051 continue;
1052 ASSERT(entry->flags & XFS_ATTR_LOCAL);
1053 name_loc = xfs_attr3_leaf_name_local(leaf, i);
1054 nargs.name = name_loc->nameval;
1055 nargs.namelen = name_loc->namelen;
1056 nargs.value = &name_loc->nameval[nargs.namelen];
1057 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
1058 nargs.hashval = be32_to_cpu(entry->hashval);
1059 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
1060 xfs_attr_shortform_add(&nargs, forkoff);
1061 }
1062 error = 0;
1063
1064 out:
1065 kmem_free(tmpbuffer);
1066 return error;
1067 }
1068
1069 /*
1070 * Convert from using a single leaf to a root node and a leaf.
1071 */
1072 int
1073 xfs_attr3_leaf_to_node(
1074 struct xfs_da_args *args)
1075 {
1076 struct xfs_attr_leafblock *leaf;
1077 struct xfs_attr3_icleaf_hdr icleafhdr;
1078 struct xfs_attr_leaf_entry *entries;
1079 struct xfs_da_node_entry *btree;
1080 struct xfs_da3_icnode_hdr icnodehdr;
1081 struct xfs_da_intnode *node;
1082 struct xfs_inode *dp = args->dp;
1083 struct xfs_mount *mp = dp->i_mount;
1084 struct xfs_buf *bp1 = NULL;
1085 struct xfs_buf *bp2 = NULL;
1086 xfs_dablk_t blkno;
1087 int error;
1088
1089 trace_xfs_attr_leaf_to_node(args);
1090
1091 error = xfs_da_grow_inode(args, &blkno);
1092 if (error)
1093 goto out;
1094 error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
1095 if (error)
1096 goto out;
1097
1098 error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
1099 if (error)
1100 goto out;
1101
1102 /* copy leaf to new buffer, update identifiers */
1103 xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
1104 bp2->b_ops = bp1->b_ops;
1105 memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize);
1106 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1107 struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1108 hdr3->blkno = cpu_to_be64(bp2->b_bn);
1109 }
1110 xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
1111
1112 /*
1113 * Set up the new root node.
1114 */
1115 error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
1116 if (error)
1117 goto out;
1118 node = bp1->b_addr;
1119 dp->d_ops->node_hdr_from_disk(&icnodehdr, node);
1120 btree = dp->d_ops->node_tree_p(node);
1121
1122 leaf = bp2->b_addr;
1123 xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
1124 entries = xfs_attr3_leaf_entryp(leaf);
1125
1126 /* both on-disk, don't endian-flip twice */
1127 btree[0].hashval = entries[icleafhdr.count - 1].hashval;
1128 btree[0].before = cpu_to_be32(blkno);
1129 icnodehdr.count = 1;
1130 dp->d_ops->node_hdr_to_disk(node, &icnodehdr);
1131 xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
1132 error = 0;
1133 out:
1134 return error;
1135 }
1136
1137 /*========================================================================
1138 * Routines used for growing the Btree.
1139 *========================================================================*/
1140
1141 /*
1142 * Create the initial contents of a leaf attribute list
1143 * or a leaf in a node attribute list.
1144 */
1145 STATIC int
1146 xfs_attr3_leaf_create(
1147 struct xfs_da_args *args,
1148 xfs_dablk_t blkno,
1149 struct xfs_buf **bpp)
1150 {
1151 struct xfs_attr_leafblock *leaf;
1152 struct xfs_attr3_icleaf_hdr ichdr;
1153 struct xfs_inode *dp = args->dp;
1154 struct xfs_mount *mp = dp->i_mount;
1155 struct xfs_buf *bp;
1156 int error;
1157
1158 trace_xfs_attr_leaf_create(args);
1159
1160 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
1161 XFS_ATTR_FORK);
1162 if (error)
1163 return error;
1164 bp->b_ops = &xfs_attr3_leaf_buf_ops;
1165 xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
1166 leaf = bp->b_addr;
1167 memset(leaf, 0, args->geo->blksize);
1168
1169 memset(&ichdr, 0, sizeof(ichdr));
1170 ichdr.firstused = args->geo->blksize;
1171
1172 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1173 struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1174
1175 ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1176
1177 hdr3->blkno = cpu_to_be64(bp->b_bn);
1178 hdr3->owner = cpu_to_be64(dp->i_ino);
1179 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
1180
1181 ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1182 } else {
1183 ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1184 ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
1185 }
1186 ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
1187
1188 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1189 xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
1190
1191 *bpp = bp;
1192 return 0;
1193 }
1194
1195 /*
1196 * Split the leaf node, rebalance, then add the new entry.
1197 */
1198 int
1199 xfs_attr3_leaf_split(
1200 struct xfs_da_state *state,
1201 struct xfs_da_state_blk *oldblk,
1202 struct xfs_da_state_blk *newblk)
1203 {
1204 xfs_dablk_t blkno;
1205 int error;
1206
1207 trace_xfs_attr_leaf_split(state->args);
1208
1209 /*
1210 * Allocate space for a new leaf node.
1211 */
1212 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1213 error = xfs_da_grow_inode(state->args, &blkno);
1214 if (error)
1215 return error;
1216 error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
1217 if (error)
1218 return error;
1219 newblk->blkno = blkno;
1220 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1221
1222 /*
1223 * Rebalance the entries across the two leaves.
1224 * NOTE: rebalance() currently depends on the 2nd block being empty.
1225 */
1226 xfs_attr3_leaf_rebalance(state, oldblk, newblk);
1227 error = xfs_da3_blk_link(state, oldblk, newblk);
1228 if (error)
1229 return error;
1230
1231 /*
1232 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1233 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1234 * "new" attrs info. Will need the "old" info to remove it later.
1235 *
1236 * Insert the "new" entry in the correct block.
1237 */
1238 if (state->inleaf) {
1239 trace_xfs_attr_leaf_add_old(state->args);
1240 error = xfs_attr3_leaf_add(oldblk->bp, state->args);
1241 } else {
1242 trace_xfs_attr_leaf_add_new(state->args);
1243 error = xfs_attr3_leaf_add(newblk->bp, state->args);
1244 }
1245
1246 /*
1247 * Update last hashval in each block since we added the name.
1248 */
1249 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1250 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1251 return error;
1252 }
1253
1254 /*
1255 * Add a name to the leaf attribute list structure.
1256 */
1257 int
1258 xfs_attr3_leaf_add(
1259 struct xfs_buf *bp,
1260 struct xfs_da_args *args)
1261 {
1262 struct xfs_attr_leafblock *leaf;
1263 struct xfs_attr3_icleaf_hdr ichdr;
1264 int tablesize;
1265 int entsize;
1266 int sum;
1267 int tmp;
1268 int i;
1269
1270 trace_xfs_attr_leaf_add(args);
1271
1272 leaf = bp->b_addr;
1273 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1274 ASSERT(args->index >= 0 && args->index <= ichdr.count);
1275 entsize = xfs_attr_leaf_newentsize(args, NULL);
1276
1277 /*
1278 * Search through freemap for first-fit on new name length.
1279 * (may need to figure in size of entry struct too)
1280 */
1281 tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1282 + xfs_attr3_leaf_hdr_size(leaf);
1283 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1284 if (tablesize > ichdr.firstused) {
1285 sum += ichdr.freemap[i].size;
1286 continue;
1287 }
1288 if (!ichdr.freemap[i].size)
1289 continue; /* no space in this map */
1290 tmp = entsize;
1291 if (ichdr.freemap[i].base < ichdr.firstused)
1292 tmp += sizeof(xfs_attr_leaf_entry_t);
1293 if (ichdr.freemap[i].size >= tmp) {
1294 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1295 goto out_log_hdr;
1296 }
1297 sum += ichdr.freemap[i].size;
1298 }
1299
1300 /*
1301 * If there are no holes in the address space of the block,
1302 * and we don't have enough freespace, then compaction will do us
1303 * no good and we should just give up.
1304 */
1305 if (!ichdr.holes && sum < entsize)
1306 return -ENOSPC;
1307
1308 /*
1309 * Compact the entries to coalesce free space.
1310 * This may change the hdr->count via dropping INCOMPLETE entries.
1311 */
1312 xfs_attr3_leaf_compact(args, &ichdr, bp);
1313
1314 /*
1315 * After compaction, the block is guaranteed to have only one
1316 * free region, in freemap[0]. If it is not big enough, give up.
1317 */
1318 if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
1319 tmp = -ENOSPC;
1320 goto out_log_hdr;
1321 }
1322
1323 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1324
1325 out_log_hdr:
1326 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1327 xfs_trans_log_buf(args->trans, bp,
1328 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1329 xfs_attr3_leaf_hdr_size(leaf)));
1330 return tmp;
1331 }
1332
1333 /*
1334 * Add a name to a leaf attribute list structure.
1335 */
1336 STATIC int
1337 xfs_attr3_leaf_add_work(
1338 struct xfs_buf *bp,
1339 struct xfs_attr3_icleaf_hdr *ichdr,
1340 struct xfs_da_args *args,
1341 int mapindex)
1342 {
1343 struct xfs_attr_leafblock *leaf;
1344 struct xfs_attr_leaf_entry *entry;
1345 struct xfs_attr_leaf_name_local *name_loc;
1346 struct xfs_attr_leaf_name_remote *name_rmt;
1347 struct xfs_mount *mp;
1348 int tmp;
1349 int i;
1350
1351 trace_xfs_attr_leaf_add_work(args);
1352
1353 leaf = bp->b_addr;
1354 ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1355 ASSERT(args->index >= 0 && args->index <= ichdr->count);
1356
1357 /*
1358 * Force open some space in the entry array and fill it in.
1359 */
1360 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1361 if (args->index < ichdr->count) {
1362 tmp = ichdr->count - args->index;
1363 tmp *= sizeof(xfs_attr_leaf_entry_t);
1364 memmove(entry + 1, entry, tmp);
1365 xfs_trans_log_buf(args->trans, bp,
1366 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1367 }
1368 ichdr->count++;
1369
1370 /*
1371 * Allocate space for the new string (at the end of the run).
1372 */
1373 mp = args->trans->t_mountp;
1374 ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
1375 ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1376 ASSERT(ichdr->freemap[mapindex].size >=
1377 xfs_attr_leaf_newentsize(args, NULL));
1378 ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
1379 ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1380
1381 ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
1382
1383 entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1384 ichdr->freemap[mapindex].size);
1385 entry->hashval = cpu_to_be32(args->hashval);
1386 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1387 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
1388 if (args->op_flags & XFS_DA_OP_RENAME) {
1389 entry->flags |= XFS_ATTR_INCOMPLETE;
1390 if ((args->blkno2 == args->blkno) &&
1391 (args->index2 <= args->index)) {
1392 args->index2++;
1393 }
1394 }
1395 xfs_trans_log_buf(args->trans, bp,
1396 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1397 ASSERT((args->index == 0) ||
1398 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1399 ASSERT((args->index == ichdr->count - 1) ||
1400 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1401
1402 /*
1403 * For "remote" attribute values, simply note that we need to
1404 * allocate space for the "remote" value. We can't actually
1405 * allocate the extents in this transaction, and we can't decide
1406 * which blocks they should be as we might allocate more blocks
1407 * as part of this transaction (a split operation for example).
1408 */
1409 if (entry->flags & XFS_ATTR_LOCAL) {
1410 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
1411 name_loc->namelen = args->namelen;
1412 name_loc->valuelen = cpu_to_be16(args->valuelen);
1413 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1414 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1415 be16_to_cpu(name_loc->valuelen));
1416 } else {
1417 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
1418 name_rmt->namelen = args->namelen;
1419 memcpy((char *)name_rmt->name, args->name, args->namelen);
1420 entry->flags |= XFS_ATTR_INCOMPLETE;
1421 /* just in case */
1422 name_rmt->valuelen = 0;
1423 name_rmt->valueblk = 0;
1424 args->rmtblkno = 1;
1425 args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
1426 args->rmtvaluelen = args->valuelen;
1427 }
1428 xfs_trans_log_buf(args->trans, bp,
1429 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
1430 xfs_attr_leaf_entsize(leaf, args->index)));
1431
1432 /*
1433 * Update the control info for this leaf node
1434 */
1435 if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1436 ichdr->firstused = be16_to_cpu(entry->nameidx);
1437
1438 ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1439 + xfs_attr3_leaf_hdr_size(leaf));
1440 tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1441 + xfs_attr3_leaf_hdr_size(leaf);
1442
1443 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1444 if (ichdr->freemap[i].base == tmp) {
1445 ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1446 ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
1447 }
1448 }
1449 ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1450 return 0;
1451 }
1452
1453 /*
1454 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1455 */
1456 STATIC void
1457 xfs_attr3_leaf_compact(
1458 struct xfs_da_args *args,
1459 struct xfs_attr3_icleaf_hdr *ichdr_dst,
1460 struct xfs_buf *bp)
1461 {
1462 struct xfs_attr_leafblock *leaf_src;
1463 struct xfs_attr_leafblock *leaf_dst;
1464 struct xfs_attr3_icleaf_hdr ichdr_src;
1465 struct xfs_trans *trans = args->trans;
1466 char *tmpbuffer;
1467
1468 trace_xfs_attr_leaf_compact(args);
1469
1470 tmpbuffer = kmem_alloc(args->geo->blksize, 0);
1471 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1472 memset(bp->b_addr, 0, args->geo->blksize);
1473 leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1474 leaf_dst = bp->b_addr;
1475
1476 /*
1477 * Copy the on-disk header back into the destination buffer to ensure
1478 * all the information in the header that is not part of the incore
1479 * header structure is preserved.
1480 */
1481 memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1482
1483 /* Initialise the incore headers */
1484 ichdr_src = *ichdr_dst; /* struct copy */
1485 ichdr_dst->firstused = args->geo->blksize;
1486 ichdr_dst->usedbytes = 0;
1487 ichdr_dst->count = 0;
1488 ichdr_dst->holes = 0;
1489 ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1490 ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1491 ichdr_dst->freemap[0].base;
1492
1493 /* write the header back to initialise the underlying buffer */
1494 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
1495
1496 /*
1497 * Copy all entry's in the same (sorted) order,
1498 * but allocate name/value pairs packed and in sequence.
1499 */
1500 xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
1501 leaf_dst, ichdr_dst, 0, ichdr_src.count);
1502 /*
1503 * this logs the entire buffer, but the caller must write the header
1504 * back to the buffer when it is finished modifying it.
1505 */
1506 xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
1507
1508 kmem_free(tmpbuffer);
1509 }
1510
1511 /*
1512 * Compare two leaf blocks "order".
1513 * Return 0 unless leaf2 should go before leaf1.
1514 */
1515 static int
1516 xfs_attr3_leaf_order(
1517 struct xfs_buf *leaf1_bp,
1518 struct xfs_attr3_icleaf_hdr *leaf1hdr,
1519 struct xfs_buf *leaf2_bp,
1520 struct xfs_attr3_icleaf_hdr *leaf2hdr)
1521 {
1522 struct xfs_attr_leaf_entry *entries1;
1523 struct xfs_attr_leaf_entry *entries2;
1524
1525 entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1526 entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1527 if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1528 ((be32_to_cpu(entries2[0].hashval) <
1529 be32_to_cpu(entries1[0].hashval)) ||
1530 (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1531 be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1532 return 1;
1533 }
1534 return 0;
1535 }
1536
1537 int
1538 xfs_attr_leaf_order(
1539 struct xfs_buf *leaf1_bp,
1540 struct xfs_buf *leaf2_bp)
1541 {
1542 struct xfs_attr3_icleaf_hdr ichdr1;
1543 struct xfs_attr3_icleaf_hdr ichdr2;
1544 struct xfs_mount *mp = leaf1_bp->b_mount;
1545
1546 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
1547 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
1548 return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1549 }
1550
1551 /*
1552 * Redistribute the attribute list entries between two leaf nodes,
1553 * taking into account the size of the new entry.
1554 *
1555 * NOTE: if new block is empty, then it will get the upper half of the
1556 * old block. At present, all (one) callers pass in an empty second block.
1557 *
1558 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1559 * to match what it is doing in splitting the attribute leaf block. Those
1560 * values are used in "atomic rename" operations on attributes. Note that
1561 * the "new" and "old" values can end up in different blocks.
1562 */
1563 STATIC void
1564 xfs_attr3_leaf_rebalance(
1565 struct xfs_da_state *state,
1566 struct xfs_da_state_blk *blk1,
1567 struct xfs_da_state_blk *blk2)
1568 {
1569 struct xfs_da_args *args;
1570 struct xfs_attr_leafblock *leaf1;
1571 struct xfs_attr_leafblock *leaf2;
1572 struct xfs_attr3_icleaf_hdr ichdr1;
1573 struct xfs_attr3_icleaf_hdr ichdr2;
1574 struct xfs_attr_leaf_entry *entries1;
1575 struct xfs_attr_leaf_entry *entries2;
1576 int count;
1577 int totallen;
1578 int max;
1579 int space;
1580 int swap;
1581
1582 /*
1583 * Set up environment.
1584 */
1585 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1586 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1587 leaf1 = blk1->bp->b_addr;
1588 leaf2 = blk2->bp->b_addr;
1589 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
1590 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
1591 ASSERT(ichdr2.count == 0);
1592 args = state->args;
1593
1594 trace_xfs_attr_leaf_rebalance(args);
1595
1596 /*
1597 * Check ordering of blocks, reverse if it makes things simpler.
1598 *
1599 * NOTE: Given that all (current) callers pass in an empty
1600 * second block, this code should never set "swap".
1601 */
1602 swap = 0;
1603 if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1604 swap(blk1, blk2);
1605
1606 /* swap structures rather than reconverting them */
1607 swap(ichdr1, ichdr2);
1608
1609 leaf1 = blk1->bp->b_addr;
1610 leaf2 = blk2->bp->b_addr;
1611 swap = 1;
1612 }
1613
1614 /*
1615 * Examine entries until we reduce the absolute difference in
1616 * byte usage between the two blocks to a minimum. Then get
1617 * the direction to copy and the number of elements to move.
1618 *
1619 * "inleaf" is true if the new entry should be inserted into blk1.
1620 * If "swap" is also true, then reverse the sense of "inleaf".
1621 */
1622 state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1623 blk2, &ichdr2,
1624 &count, &totallen);
1625 if (swap)
1626 state->inleaf = !state->inleaf;
1627
1628 /*
1629 * Move any entries required from leaf to leaf:
1630 */
1631 if (count < ichdr1.count) {
1632 /*
1633 * Figure the total bytes to be added to the destination leaf.
1634 */
1635 /* number entries being moved */
1636 count = ichdr1.count - count;
1637 space = ichdr1.usedbytes - totallen;
1638 space += count * sizeof(xfs_attr_leaf_entry_t);
1639
1640 /*
1641 * leaf2 is the destination, compact it if it looks tight.
1642 */
1643 max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1644 max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
1645 if (space > max)
1646 xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
1647
1648 /*
1649 * Move high entries from leaf1 to low end of leaf2.
1650 */
1651 xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
1652 ichdr1.count - count, leaf2, &ichdr2, 0, count);
1653
1654 } else if (count > ichdr1.count) {
1655 /*
1656 * I assert that since all callers pass in an empty
1657 * second buffer, this code should never execute.
1658 */
1659 ASSERT(0);
1660
1661 /*
1662 * Figure the total bytes to be added to the destination leaf.
1663 */
1664 /* number entries being moved */
1665 count -= ichdr1.count;
1666 space = totallen - ichdr1.usedbytes;
1667 space += count * sizeof(xfs_attr_leaf_entry_t);
1668
1669 /*
1670 * leaf1 is the destination, compact it if it looks tight.
1671 */
1672 max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1673 max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
1674 if (space > max)
1675 xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
1676
1677 /*
1678 * Move low entries from leaf2 to high end of leaf1.
1679 */
1680 xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
1681 ichdr1.count, count);
1682 }
1683
1684 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
1685 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
1686 xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
1687 xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
1688
1689 /*
1690 * Copy out last hashval in each block for B-tree code.
1691 */
1692 entries1 = xfs_attr3_leaf_entryp(leaf1);
1693 entries2 = xfs_attr3_leaf_entryp(leaf2);
1694 blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1695 blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
1696
1697 /*
1698 * Adjust the expected index for insertion.
1699 * NOTE: this code depends on the (current) situation that the
1700 * second block was originally empty.
1701 *
1702 * If the insertion point moved to the 2nd block, we must adjust
1703 * the index. We must also track the entry just following the
1704 * new entry for use in an "atomic rename" operation, that entry
1705 * is always the "old" entry and the "new" entry is what we are
1706 * inserting. The index/blkno fields refer to the "old" entry,
1707 * while the index2/blkno2 fields refer to the "new" entry.
1708 */
1709 if (blk1->index > ichdr1.count) {
1710 ASSERT(state->inleaf == 0);
1711 blk2->index = blk1->index - ichdr1.count;
1712 args->index = args->index2 = blk2->index;
1713 args->blkno = args->blkno2 = blk2->blkno;
1714 } else if (blk1->index == ichdr1.count) {
1715 if (state->inleaf) {
1716 args->index = blk1->index;
1717 args->blkno = blk1->blkno;
1718 args->index2 = 0;
1719 args->blkno2 = blk2->blkno;
1720 } else {
1721 /*
1722 * On a double leaf split, the original attr location
1723 * is already stored in blkno2/index2, so don't
1724 * overwrite it overwise we corrupt the tree.
1725 */
1726 blk2->index = blk1->index - ichdr1.count;
1727 args->index = blk2->index;
1728 args->blkno = blk2->blkno;
1729 if (!state->extravalid) {
1730 /*
1731 * set the new attr location to match the old
1732 * one and let the higher level split code
1733 * decide where in the leaf to place it.
1734 */
1735 args->index2 = blk2->index;
1736 args->blkno2 = blk2->blkno;
1737 }
1738 }
1739 } else {
1740 ASSERT(state->inleaf == 1);
1741 args->index = args->index2 = blk1->index;
1742 args->blkno = args->blkno2 = blk1->blkno;
1743 }
1744 }
1745
1746 /*
1747 * Examine entries until we reduce the absolute difference in
1748 * byte usage between the two blocks to a minimum.
1749 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1750 * GROT: there will always be enough room in either block for a new entry.
1751 * GROT: Do a double-split for this case?
1752 */
1753 STATIC int
1754 xfs_attr3_leaf_figure_balance(
1755 struct xfs_da_state *state,
1756 struct xfs_da_state_blk *blk1,
1757 struct xfs_attr3_icleaf_hdr *ichdr1,
1758 struct xfs_da_state_blk *blk2,
1759 struct xfs_attr3_icleaf_hdr *ichdr2,
1760 int *countarg,
1761 int *usedbytesarg)
1762 {
1763 struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
1764 struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
1765 struct xfs_attr_leaf_entry *entry;
1766 int count;
1767 int max;
1768 int index;
1769 int totallen = 0;
1770 int half;
1771 int lastdelta;
1772 int foundit = 0;
1773 int tmp;
1774
1775 /*
1776 * Examine entries until we reduce the absolute difference in
1777 * byte usage between the two blocks to a minimum.
1778 */
1779 max = ichdr1->count + ichdr2->count;
1780 half = (max + 1) * sizeof(*entry);
1781 half += ichdr1->usedbytes + ichdr2->usedbytes +
1782 xfs_attr_leaf_newentsize(state->args, NULL);
1783 half /= 2;
1784 lastdelta = state->args->geo->blksize;
1785 entry = xfs_attr3_leaf_entryp(leaf1);
1786 for (count = index = 0; count < max; entry++, index++, count++) {
1787
1788 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1789 /*
1790 * The new entry is in the first block, account for it.
1791 */
1792 if (count == blk1->index) {
1793 tmp = totallen + sizeof(*entry) +
1794 xfs_attr_leaf_newentsize(state->args, NULL);
1795 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1796 break;
1797 lastdelta = XFS_ATTR_ABS(half - tmp);
1798 totallen = tmp;
1799 foundit = 1;
1800 }
1801
1802 /*
1803 * Wrap around into the second block if necessary.
1804 */
1805 if (count == ichdr1->count) {
1806 leaf1 = leaf2;
1807 entry = xfs_attr3_leaf_entryp(leaf1);
1808 index = 0;
1809 }
1810
1811 /*
1812 * Figure out if next leaf entry would be too much.
1813 */
1814 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1815 index);
1816 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1817 break;
1818 lastdelta = XFS_ATTR_ABS(half - tmp);
1819 totallen = tmp;
1820 #undef XFS_ATTR_ABS
1821 }
1822
1823 /*
1824 * Calculate the number of usedbytes that will end up in lower block.
1825 * If new entry not in lower block, fix up the count.
1826 */
1827 totallen -= count * sizeof(*entry);
1828 if (foundit) {
1829 totallen -= sizeof(*entry) +
1830 xfs_attr_leaf_newentsize(state->args, NULL);
1831 }
1832
1833 *countarg = count;
1834 *usedbytesarg = totallen;
1835 return foundit;
1836 }
1837
1838 /*========================================================================
1839 * Routines used for shrinking the Btree.
1840 *========================================================================*/
1841
1842 /*
1843 * Check a leaf block and its neighbors to see if the block should be
1844 * collapsed into one or the other neighbor. Always keep the block
1845 * with the smaller block number.
1846 * If the current block is over 50% full, don't try to join it, return 0.
1847 * If the block is empty, fill in the state structure and return 2.
1848 * If it can be collapsed, fill in the state structure and return 1.
1849 * If nothing can be done, return 0.
1850 *
1851 * GROT: allow for INCOMPLETE entries in calculation.
1852 */
1853 int
1854 xfs_attr3_leaf_toosmall(
1855 struct xfs_da_state *state,
1856 int *action)
1857 {
1858 struct xfs_attr_leafblock *leaf;
1859 struct xfs_da_state_blk *blk;
1860 struct xfs_attr3_icleaf_hdr ichdr;
1861 struct xfs_buf *bp;
1862 xfs_dablk_t blkno;
1863 int bytes;
1864 int forward;
1865 int error;
1866 int retval;
1867 int i;
1868
1869 trace_xfs_attr_leaf_toosmall(state->args);
1870
1871 /*
1872 * Check for the degenerate case of the block being over 50% full.
1873 * If so, it's not worth even looking to see if we might be able
1874 * to coalesce with a sibling.
1875 */
1876 blk = &state->path.blk[ state->path.active-1 ];
1877 leaf = blk->bp->b_addr;
1878 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
1879 bytes = xfs_attr3_leaf_hdr_size(leaf) +
1880 ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
1881 ichdr.usedbytes;
1882 if (bytes > (state->args->geo->blksize >> 1)) {
1883 *action = 0; /* blk over 50%, don't try to join */
1884 return 0;
1885 }
1886
1887 /*
1888 * Check for the degenerate case of the block being empty.
1889 * If the block is empty, we'll simply delete it, no need to
1890 * coalesce it with a sibling block. We choose (arbitrarily)
1891 * to merge with the forward block unless it is NULL.
1892 */
1893 if (ichdr.count == 0) {
1894 /*
1895 * Make altpath point to the block we want to keep and
1896 * path point to the block we want to drop (this one).
1897 */
1898 forward = (ichdr.forw != 0);
1899 memcpy(&state->altpath, &state->path, sizeof(state->path));
1900 error = xfs_da3_path_shift(state, &state->altpath, forward,
1901 0, &retval);
1902 if (error)
1903 return error;
1904 if (retval) {
1905 *action = 0;
1906 } else {
1907 *action = 2;
1908 }
1909 return 0;
1910 }
1911
1912 /*
1913 * Examine each sibling block to see if we can coalesce with
1914 * at least 25% free space to spare. We need to figure out
1915 * whether to merge with the forward or the backward block.
1916 * We prefer coalescing with the lower numbered sibling so as
1917 * to shrink an attribute list over time.
1918 */
1919 /* start with smaller blk num */
1920 forward = ichdr.forw < ichdr.back;
1921 for (i = 0; i < 2; forward = !forward, i++) {
1922 struct xfs_attr3_icleaf_hdr ichdr2;
1923 if (forward)
1924 blkno = ichdr.forw;
1925 else
1926 blkno = ichdr.back;
1927 if (blkno == 0)
1928 continue;
1929 error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
1930 blkno, -1, &bp);
1931 if (error)
1932 return error;
1933
1934 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
1935
1936 bytes = state->args->geo->blksize -
1937 (state->args->geo->blksize >> 2) -
1938 ichdr.usedbytes - ichdr2.usedbytes -
1939 ((ichdr.count + ichdr2.count) *
1940 sizeof(xfs_attr_leaf_entry_t)) -
1941 xfs_attr3_leaf_hdr_size(leaf);
1942
1943 xfs_trans_brelse(state->args->trans, bp);
1944 if (bytes >= 0)
1945 break; /* fits with at least 25% to spare */
1946 }
1947 if (i >= 2) {
1948 *action = 0;
1949 return 0;
1950 }
1951
1952 /*
1953 * Make altpath point to the block we want to keep (the lower
1954 * numbered block) and path point to the block we want to drop.
1955 */
1956 memcpy(&state->altpath, &state->path, sizeof(state->path));
1957 if (blkno < blk->blkno) {
1958 error = xfs_da3_path_shift(state, &state->altpath, forward,
1959 0, &retval);
1960 } else {
1961 error = xfs_da3_path_shift(state, &state->path, forward,
1962 0, &retval);
1963 }
1964 if (error)
1965 return error;
1966 if (retval) {
1967 *action = 0;
1968 } else {
1969 *action = 1;
1970 }
1971 return 0;
1972 }
1973
1974 /*
1975 * Remove a name from the leaf attribute list structure.
1976 *
1977 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1978 * If two leaves are 37% full, when combined they will leave 25% free.
1979 */
1980 int
1981 xfs_attr3_leaf_remove(
1982 struct xfs_buf *bp,
1983 struct xfs_da_args *args)
1984 {
1985 struct xfs_attr_leafblock *leaf;
1986 struct xfs_attr3_icleaf_hdr ichdr;
1987 struct xfs_attr_leaf_entry *entry;
1988 int before;
1989 int after;
1990 int smallest;
1991 int entsize;
1992 int tablesize;
1993 int tmp;
1994 int i;
1995
1996 trace_xfs_attr_leaf_remove(args);
1997
1998 leaf = bp->b_addr;
1999 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2000
2001 ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
2002 ASSERT(args->index >= 0 && args->index < ichdr.count);
2003 ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
2004 xfs_attr3_leaf_hdr_size(leaf));
2005
2006 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2007
2008 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2009 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2010
2011 /*
2012 * Scan through free region table:
2013 * check for adjacency of free'd entry with an existing one,
2014 * find smallest free region in case we need to replace it,
2015 * adjust any map that borders the entry table,
2016 */
2017 tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
2018 + xfs_attr3_leaf_hdr_size(leaf);
2019 tmp = ichdr.freemap[0].size;
2020 before = after = -1;
2021 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2022 entsize = xfs_attr_leaf_entsize(leaf, args->index);
2023 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
2024 ASSERT(ichdr.freemap[i].base < args->geo->blksize);
2025 ASSERT(ichdr.freemap[i].size < args->geo->blksize);
2026 if (ichdr.freemap[i].base == tablesize) {
2027 ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2028 ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
2029 }
2030
2031 if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2032 be16_to_cpu(entry->nameidx)) {
2033 before = i;
2034 } else if (ichdr.freemap[i].base ==
2035 (be16_to_cpu(entry->nameidx) + entsize)) {
2036 after = i;
2037 } else if (ichdr.freemap[i].size < tmp) {
2038 tmp = ichdr.freemap[i].size;
2039 smallest = i;
2040 }
2041 }
2042
2043 /*
2044 * Coalesce adjacent freemap regions,
2045 * or replace the smallest region.
2046 */
2047 if ((before >= 0) || (after >= 0)) {
2048 if ((before >= 0) && (after >= 0)) {
2049 ichdr.freemap[before].size += entsize;
2050 ichdr.freemap[before].size += ichdr.freemap[after].size;
2051 ichdr.freemap[after].base = 0;
2052 ichdr.freemap[after].size = 0;
2053 } else if (before >= 0) {
2054 ichdr.freemap[before].size += entsize;
2055 } else {
2056 ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2057 ichdr.freemap[after].size += entsize;
2058 }
2059 } else {
2060 /*
2061 * Replace smallest region (if it is smaller than free'd entry)
2062 */
2063 if (ichdr.freemap[smallest].size < entsize) {
2064 ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2065 ichdr.freemap[smallest].size = entsize;
2066 }
2067 }
2068
2069 /*
2070 * Did we remove the first entry?
2071 */
2072 if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
2073 smallest = 1;
2074 else
2075 smallest = 0;
2076
2077 /*
2078 * Compress the remaining entries and zero out the removed stuff.
2079 */
2080 memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2081 ichdr.usedbytes -= entsize;
2082 xfs_trans_log_buf(args->trans, bp,
2083 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
2084 entsize));
2085
2086 tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2087 memmove(entry, entry + 1, tmp);
2088 ichdr.count--;
2089 xfs_trans_log_buf(args->trans, bp,
2090 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2091
2092 entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2093 memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
2094
2095 /*
2096 * If we removed the first entry, re-find the first used byte
2097 * in the name area. Note that if the entry was the "firstused",
2098 * then we don't have a "hole" in our block resulting from
2099 * removing the name.
2100 */
2101 if (smallest) {
2102 tmp = args->geo->blksize;
2103 entry = xfs_attr3_leaf_entryp(leaf);
2104 for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2105 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2106 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2107
2108 if (be16_to_cpu(entry->nameidx) < tmp)
2109 tmp = be16_to_cpu(entry->nameidx);
2110 }
2111 ichdr.firstused = tmp;
2112 ASSERT(ichdr.firstused != 0);
2113 } else {
2114 ichdr.holes = 1; /* mark as needing compaction */
2115 }
2116 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
2117 xfs_trans_log_buf(args->trans, bp,
2118 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2119 xfs_attr3_leaf_hdr_size(leaf)));
2120
2121 /*
2122 * Check if leaf is less than 50% full, caller may want to
2123 * "join" the leaf with a sibling if so.
2124 */
2125 tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2126 ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2127
2128 return tmp < args->geo->magicpct; /* leaf is < 37% full */
2129 }
2130
2131 /*
2132 * Move all the attribute list entries from drop_leaf into save_leaf.
2133 */
2134 void
2135 xfs_attr3_leaf_unbalance(
2136 struct xfs_da_state *state,
2137 struct xfs_da_state_blk *drop_blk,
2138 struct xfs_da_state_blk *save_blk)
2139 {
2140 struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2141 struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2142 struct xfs_attr3_icleaf_hdr drophdr;
2143 struct xfs_attr3_icleaf_hdr savehdr;
2144 struct xfs_attr_leaf_entry *entry;
2145
2146 trace_xfs_attr_leaf_unbalance(state->args);
2147
2148 drop_leaf = drop_blk->bp->b_addr;
2149 save_leaf = save_blk->bp->b_addr;
2150 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
2151 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
2152 entry = xfs_attr3_leaf_entryp(drop_leaf);
2153
2154 /*
2155 * Save last hashval from dying block for later Btree fixup.
2156 */
2157 drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
2158
2159 /*
2160 * Check if we need a temp buffer, or can we do it in place.
2161 * Note that we don't check "leaf" for holes because we will
2162 * always be dropping it, toosmall() decided that for us already.
2163 */
2164 if (savehdr.holes == 0) {
2165 /*
2166 * dest leaf has no holes, so we add there. May need
2167 * to make some room in the entry array.
2168 */
2169 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2170 drop_blk->bp, &drophdr)) {
2171 xfs_attr3_leaf_moveents(state->args,
2172 drop_leaf, &drophdr, 0,
2173 save_leaf, &savehdr, 0,
2174 drophdr.count);
2175 } else {
2176 xfs_attr3_leaf_moveents(state->args,
2177 drop_leaf, &drophdr, 0,
2178 save_leaf, &savehdr,
2179 savehdr.count, drophdr.count);
2180 }
2181 } else {
2182 /*
2183 * Destination has holes, so we make a temporary copy
2184 * of the leaf and add them both to that.
2185 */
2186 struct xfs_attr_leafblock *tmp_leaf;
2187 struct xfs_attr3_icleaf_hdr tmphdr;
2188
2189 tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0);
2190
2191 /*
2192 * Copy the header into the temp leaf so that all the stuff
2193 * not in the incore header is present and gets copied back in
2194 * once we've moved all the entries.
2195 */
2196 memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2197
2198 memset(&tmphdr, 0, sizeof(tmphdr));
2199 tmphdr.magic = savehdr.magic;
2200 tmphdr.forw = savehdr.forw;
2201 tmphdr.back = savehdr.back;
2202 tmphdr.firstused = state->args->geo->blksize;
2203
2204 /* write the header to the temp buffer to initialise it */
2205 xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
2206
2207 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2208 drop_blk->bp, &drophdr)) {
2209 xfs_attr3_leaf_moveents(state->args,
2210 drop_leaf, &drophdr, 0,
2211 tmp_leaf, &tmphdr, 0,
2212 drophdr.count);
2213 xfs_attr3_leaf_moveents(state->args,
2214 save_leaf, &savehdr, 0,
2215 tmp_leaf, &tmphdr, tmphdr.count,
2216 savehdr.count);
2217 } else {
2218 xfs_attr3_leaf_moveents(state->args,
2219 save_leaf, &savehdr, 0,
2220 tmp_leaf, &tmphdr, 0,
2221 savehdr.count);
2222 xfs_attr3_leaf_moveents(state->args,
2223 drop_leaf, &drophdr, 0,
2224 tmp_leaf, &tmphdr, tmphdr.count,
2225 drophdr.count);
2226 }
2227 memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
2228 savehdr = tmphdr; /* struct copy */
2229 kmem_free(tmp_leaf);
2230 }
2231
2232 xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
2233 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
2234 state->args->geo->blksize - 1);
2235
2236 /*
2237 * Copy out last hashval in each block for B-tree code.
2238 */
2239 entry = xfs_attr3_leaf_entryp(save_leaf);
2240 save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
2241 }
2242
2243 /*========================================================================
2244 * Routines used for finding things in the Btree.
2245 *========================================================================*/
2246
2247 /*
2248 * Look up a name in a leaf attribute list structure.
2249 * This is the internal routine, it uses the caller's buffer.
2250 *
2251 * Note that duplicate keys are allowed, but only check within the
2252 * current leaf node. The Btree code must check in adjacent leaf nodes.
2253 *
2254 * Return in args->index the index into the entry[] array of either
2255 * the found entry, or where the entry should have been (insert before
2256 * that entry).
2257 *
2258 * Don't change the args->value unless we find the attribute.
2259 */
2260 int
2261 xfs_attr3_leaf_lookup_int(
2262 struct xfs_buf *bp,
2263 struct xfs_da_args *args)
2264 {
2265 struct xfs_attr_leafblock *leaf;
2266 struct xfs_attr3_icleaf_hdr ichdr;
2267 struct xfs_attr_leaf_entry *entry;
2268 struct xfs_attr_leaf_entry *entries;
2269 struct xfs_attr_leaf_name_local *name_loc;
2270 struct xfs_attr_leaf_name_remote *name_rmt;
2271 xfs_dahash_t hashval;
2272 int probe;
2273 int span;
2274
2275 trace_xfs_attr_leaf_lookup(args);
2276
2277 leaf = bp->b_addr;
2278 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2279 entries = xfs_attr3_leaf_entryp(leaf);
2280 if (ichdr.count >= args->geo->blksize / 8)
2281 return -EFSCORRUPTED;
2282
2283 /*
2284 * Binary search. (note: small blocks will skip this loop)
2285 */
2286 hashval = args->hashval;
2287 probe = span = ichdr.count / 2;
2288 for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
2289 span /= 2;
2290 if (be32_to_cpu(entry->hashval) < hashval)
2291 probe += span;
2292 else if (be32_to_cpu(entry->hashval) > hashval)
2293 probe -= span;
2294 else
2295 break;
2296 }
2297 if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count)))
2298 return -EFSCORRUPTED;
2299 if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval))
2300 return -EFSCORRUPTED;
2301
2302 /*
2303 * Since we may have duplicate hashval's, find the first matching
2304 * hashval in the leaf.
2305 */
2306 while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
2307 entry--;
2308 probe--;
2309 }
2310 while (probe < ichdr.count &&
2311 be32_to_cpu(entry->hashval) < hashval) {
2312 entry++;
2313 probe++;
2314 }
2315 if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
2316 args->index = probe;
2317 return -ENOATTR;
2318 }
2319
2320 /*
2321 * Duplicate keys may be present, so search all of them for a match.
2322 */
2323 for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
2324 entry++, probe++) {
2325 /*
2326 * GROT: Add code to remove incomplete entries.
2327 */
2328 /*
2329 * If we are looking for INCOMPLETE entries, show only those.
2330 * If we are looking for complete entries, show only those.
2331 */
2332 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2333 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2334 continue;
2335 }
2336 if (entry->flags & XFS_ATTR_LOCAL) {
2337 name_loc = xfs_attr3_leaf_name_local(leaf, probe);
2338 if (name_loc->namelen != args->namelen)
2339 continue;
2340 if (memcmp(args->name, name_loc->nameval,
2341 args->namelen) != 0)
2342 continue;
2343 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2344 continue;
2345 args->index = probe;
2346 return -EEXIST;
2347 } else {
2348 name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
2349 if (name_rmt->namelen != args->namelen)
2350 continue;
2351 if (memcmp(args->name, name_rmt->name,
2352 args->namelen) != 0)
2353 continue;
2354 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2355 continue;
2356 args->index = probe;
2357 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2358 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2359 args->rmtblkcnt = xfs_attr3_rmt_blocks(
2360 args->dp->i_mount,
2361 args->rmtvaluelen);
2362 return -EEXIST;
2363 }
2364 }
2365 args->index = probe;
2366 return -ENOATTR;
2367 }
2368
2369 /*
2370 * Get the value associated with an attribute name from a leaf attribute
2371 * list structure.
2372 *
2373 * If ATTR_KERNOVAL is specified, only the length needs to be returned.
2374 * Unlike a lookup, we only return an error if the attribute does not
2375 * exist or we can't retrieve the value.
2376 */
2377 int
2378 xfs_attr3_leaf_getvalue(
2379 struct xfs_buf *bp,
2380 struct xfs_da_args *args)
2381 {
2382 struct xfs_attr_leafblock *leaf;
2383 struct xfs_attr3_icleaf_hdr ichdr;
2384 struct xfs_attr_leaf_entry *entry;
2385 struct xfs_attr_leaf_name_local *name_loc;
2386 struct xfs_attr_leaf_name_remote *name_rmt;
2387
2388 leaf = bp->b_addr;
2389 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2390 ASSERT(ichdr.count < args->geo->blksize / 8);
2391 ASSERT(args->index < ichdr.count);
2392
2393 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2394 if (entry->flags & XFS_ATTR_LOCAL) {
2395 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2396 ASSERT(name_loc->namelen == args->namelen);
2397 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2398 return xfs_attr_copy_value(args,
2399 &name_loc->nameval[args->namelen],
2400 be16_to_cpu(name_loc->valuelen));
2401 }
2402
2403 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2404 ASSERT(name_rmt->namelen == args->namelen);
2405 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2406 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2407 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2408 args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
2409 args->rmtvaluelen);
2410 return xfs_attr_copy_value(args, NULL, args->rmtvaluelen);
2411 }
2412
2413 /*========================================================================
2414 * Utility routines.
2415 *========================================================================*/
2416
2417 /*
2418 * Move the indicated entries from one leaf to another.
2419 * NOTE: this routine modifies both source and destination leaves.
2420 */
2421 /*ARGSUSED*/
2422 STATIC void
2423 xfs_attr3_leaf_moveents(
2424 struct xfs_da_args *args,
2425 struct xfs_attr_leafblock *leaf_s,
2426 struct xfs_attr3_icleaf_hdr *ichdr_s,
2427 int start_s,
2428 struct xfs_attr_leafblock *leaf_d,
2429 struct xfs_attr3_icleaf_hdr *ichdr_d,
2430 int start_d,
2431 int count)
2432 {
2433 struct xfs_attr_leaf_entry *entry_s;
2434 struct xfs_attr_leaf_entry *entry_d;
2435 int desti;
2436 int tmp;
2437 int i;
2438
2439 /*
2440 * Check for nothing to do.
2441 */
2442 if (count == 0)
2443 return;
2444
2445 /*
2446 * Set up environment.
2447 */
2448 ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2449 ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2450 ASSERT(ichdr_s->magic == ichdr_d->magic);
2451 ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
2452 ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2453 + xfs_attr3_leaf_hdr_size(leaf_s));
2454 ASSERT(ichdr_d->count < args->geo->blksize / 8);
2455 ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2456 + xfs_attr3_leaf_hdr_size(leaf_d));
2457
2458 ASSERT(start_s < ichdr_s->count);
2459 ASSERT(start_d <= ichdr_d->count);
2460 ASSERT(count <= ichdr_s->count);
2461
2462
2463 /*
2464 * Move the entries in the destination leaf up to make a hole?
2465 */
2466 if (start_d < ichdr_d->count) {
2467 tmp = ichdr_d->count - start_d;
2468 tmp *= sizeof(xfs_attr_leaf_entry_t);
2469 entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2470 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2471 memmove(entry_d, entry_s, tmp);
2472 }
2473
2474 /*
2475 * Copy all entry's in the same (sorted) order,
2476 * but allocate attribute info packed and in sequence.
2477 */
2478 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2479 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2480 desti = start_d;
2481 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2482 ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
2483 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2484 #ifdef GROT
2485 /*
2486 * Code to drop INCOMPLETE entries. Difficult to use as we
2487 * may also need to change the insertion index. Code turned
2488 * off for 6.2, should be revisited later.
2489 */
2490 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2491 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2492 ichdr_s->usedbytes -= tmp;
2493 ichdr_s->count -= 1;
2494 entry_d--; /* to compensate for ++ in loop hdr */
2495 desti--;
2496 if ((start_s + i) < offset)
2497 result++; /* insertion index adjustment */
2498 } else {
2499 #endif /* GROT */
2500 ichdr_d->firstused -= tmp;
2501 /* both on-disk, don't endian flip twice */
2502 entry_d->hashval = entry_s->hashval;
2503 entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
2504 entry_d->flags = entry_s->flags;
2505 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2506 <= args->geo->blksize);
2507 memmove(xfs_attr3_leaf_name(leaf_d, desti),
2508 xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
2509 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2510 <= args->geo->blksize);
2511 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2512 ichdr_s->usedbytes -= tmp;
2513 ichdr_d->usedbytes += tmp;
2514 ichdr_s->count -= 1;
2515 ichdr_d->count += 1;
2516 tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2517 + xfs_attr3_leaf_hdr_size(leaf_d);
2518 ASSERT(ichdr_d->firstused >= tmp);
2519 #ifdef GROT
2520 }
2521 #endif /* GROT */
2522 }
2523
2524 /*
2525 * Zero out the entries we just copied.
2526 */
2527 if (start_s == ichdr_s->count) {
2528 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2529 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2530 ASSERT(((char *)entry_s + tmp) <=
2531 ((char *)leaf_s + args->geo->blksize));
2532 memset(entry_s, 0, tmp);
2533 } else {
2534 /*
2535 * Move the remaining entries down to fill the hole,
2536 * then zero the entries at the top.
2537 */
2538 tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2539 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2540 entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2541 memmove(entry_d, entry_s, tmp);
2542
2543 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2544 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
2545 ASSERT(((char *)entry_s + tmp) <=
2546 ((char *)leaf_s + args->geo->blksize));
2547 memset(entry_s, 0, tmp);
2548 }
2549
2550 /*
2551 * Fill in the freemap information
2552 */
2553 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2554 ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2555 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2556 ichdr_d->freemap[1].base = 0;
2557 ichdr_d->freemap[2].base = 0;
2558 ichdr_d->freemap[1].size = 0;
2559 ichdr_d->freemap[2].size = 0;
2560 ichdr_s->holes = 1; /* leaf may not be compact */
2561 }
2562
2563 /*
2564 * Pick up the last hashvalue from a leaf block.
2565 */
2566 xfs_dahash_t
2567 xfs_attr_leaf_lasthash(
2568 struct xfs_buf *bp,
2569 int *count)
2570 {
2571 struct xfs_attr3_icleaf_hdr ichdr;
2572 struct xfs_attr_leaf_entry *entries;
2573 struct xfs_mount *mp = bp->b_mount;
2574
2575 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
2576 entries = xfs_attr3_leaf_entryp(bp->b_addr);
2577 if (count)
2578 *count = ichdr.count;
2579 if (!ichdr.count)
2580 return 0;
2581 return be32_to_cpu(entries[ichdr.count - 1].hashval);
2582 }
2583
2584 /*
2585 * Calculate the number of bytes used to store the indicated attribute
2586 * (whether local or remote only calculate bytes in this block).
2587 */
2588 STATIC int
2589 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2590 {
2591 struct xfs_attr_leaf_entry *entries;
2592 xfs_attr_leaf_name_local_t *name_loc;
2593 xfs_attr_leaf_name_remote_t *name_rmt;
2594 int size;
2595
2596 entries = xfs_attr3_leaf_entryp(leaf);
2597 if (entries[index].flags & XFS_ATTR_LOCAL) {
2598 name_loc = xfs_attr3_leaf_name_local(leaf, index);
2599 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2600 be16_to_cpu(name_loc->valuelen));
2601 } else {
2602 name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
2603 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2604 }
2605 return size;
2606 }
2607
2608 /*
2609 * Calculate the number of bytes that would be required to store the new
2610 * attribute (whether local or remote only calculate bytes in this block).
2611 * This routine decides as a side effect whether the attribute will be
2612 * a "local" or a "remote" attribute.
2613 */
2614 int
2615 xfs_attr_leaf_newentsize(
2616 struct xfs_da_args *args,
2617 int *local)
2618 {
2619 int size;
2620
2621 size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
2622 if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
2623 if (local)
2624 *local = 1;
2625 return size;
2626 }
2627 if (local)
2628 *local = 0;
2629 return xfs_attr_leaf_entsize_remote(args->namelen);
2630 }
2631
2632
2633 /*========================================================================
2634 * Manage the INCOMPLETE flag in a leaf entry
2635 *========================================================================*/
2636
2637 /*
2638 * Clear the INCOMPLETE flag on an entry in a leaf block.
2639 */
2640 int
2641 xfs_attr3_leaf_clearflag(
2642 struct xfs_da_args *args)
2643 {
2644 struct xfs_attr_leafblock *leaf;
2645 struct xfs_attr_leaf_entry *entry;
2646 struct xfs_attr_leaf_name_remote *name_rmt;
2647 struct xfs_buf *bp;
2648 int error;
2649 #ifdef DEBUG
2650 struct xfs_attr3_icleaf_hdr ichdr;
2651 xfs_attr_leaf_name_local_t *name_loc;
2652 int namelen;
2653 char *name;
2654 #endif /* DEBUG */
2655
2656 trace_xfs_attr_leaf_clearflag(args);
2657 /*
2658 * Set up the operation.
2659 */
2660 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2661 if (error)
2662 return error;
2663
2664 leaf = bp->b_addr;
2665 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2666 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2667
2668 #ifdef DEBUG
2669 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2670 ASSERT(args->index < ichdr.count);
2671 ASSERT(args->index >= 0);
2672
2673 if (entry->flags & XFS_ATTR_LOCAL) {
2674 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2675 namelen = name_loc->namelen;
2676 name = (char *)name_loc->nameval;
2677 } else {
2678 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2679 namelen = name_rmt->namelen;
2680 name = (char *)name_rmt->name;
2681 }
2682 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2683 ASSERT(namelen == args->namelen);
2684 ASSERT(memcmp(name, args->name, namelen) == 0);
2685 #endif /* DEBUG */
2686
2687 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2688 xfs_trans_log_buf(args->trans, bp,
2689 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2690
2691 if (args->rmtblkno) {
2692 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2693 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2694 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2695 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2696 xfs_trans_log_buf(args->trans, bp,
2697 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2698 }
2699
2700 /*
2701 * Commit the flag value change and start the next trans in series.
2702 */
2703 return xfs_trans_roll_inode(&args->trans, args->dp);
2704 }
2705
2706 /*
2707 * Set the INCOMPLETE flag on an entry in a leaf block.
2708 */
2709 int
2710 xfs_attr3_leaf_setflag(
2711 struct xfs_da_args *args)
2712 {
2713 struct xfs_attr_leafblock *leaf;
2714 struct xfs_attr_leaf_entry *entry;
2715 struct xfs_attr_leaf_name_remote *name_rmt;
2716 struct xfs_buf *bp;
2717 int error;
2718 #ifdef DEBUG
2719 struct xfs_attr3_icleaf_hdr ichdr;
2720 #endif
2721
2722 trace_xfs_attr_leaf_setflag(args);
2723
2724 /*
2725 * Set up the operation.
2726 */
2727 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2728 if (error)
2729 return error;
2730
2731 leaf = bp->b_addr;
2732 #ifdef DEBUG
2733 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2734 ASSERT(args->index < ichdr.count);
2735 ASSERT(args->index >= 0);
2736 #endif
2737 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2738
2739 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2740 entry->flags |= XFS_ATTR_INCOMPLETE;
2741 xfs_trans_log_buf(args->trans, bp,
2742 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2743 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2744 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2745 name_rmt->valueblk = 0;
2746 name_rmt->valuelen = 0;
2747 xfs_trans_log_buf(args->trans, bp,
2748 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2749 }
2750
2751 /*
2752 * Commit the flag value change and start the next trans in series.
2753 */
2754 return xfs_trans_roll_inode(&args->trans, args->dp);
2755 }
2756
2757 /*
2758 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2759 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2760 * entry given by args->blkno2/index2.
2761 *
2762 * Note that they could be in different blocks, or in the same block.
2763 */
2764 int
2765 xfs_attr3_leaf_flipflags(
2766 struct xfs_da_args *args)
2767 {
2768 struct xfs_attr_leafblock *leaf1;
2769 struct xfs_attr_leafblock *leaf2;
2770 struct xfs_attr_leaf_entry *entry1;
2771 struct xfs_attr_leaf_entry *entry2;
2772 struct xfs_attr_leaf_name_remote *name_rmt;
2773 struct xfs_buf *bp1;
2774 struct xfs_buf *bp2;
2775 int error;
2776 #ifdef DEBUG
2777 struct xfs_attr3_icleaf_hdr ichdr1;
2778 struct xfs_attr3_icleaf_hdr ichdr2;
2779 xfs_attr_leaf_name_local_t *name_loc;
2780 int namelen1, namelen2;
2781 char *name1, *name2;
2782 #endif /* DEBUG */
2783
2784 trace_xfs_attr_leaf_flipflags(args);
2785
2786 /*
2787 * Read the block containing the "old" attr
2788 */
2789 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
2790 if (error)
2791 return error;
2792
2793 /*
2794 * Read the block containing the "new" attr, if it is different
2795 */
2796 if (args->blkno2 != args->blkno) {
2797 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
2798 -1, &bp2);
2799 if (error)
2800 return error;
2801 } else {
2802 bp2 = bp1;
2803 }
2804
2805 leaf1 = bp1->b_addr;
2806 entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
2807
2808 leaf2 = bp2->b_addr;
2809 entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
2810
2811 #ifdef DEBUG
2812 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
2813 ASSERT(args->index < ichdr1.count);
2814 ASSERT(args->index >= 0);
2815
2816 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
2817 ASSERT(args->index2 < ichdr2.count);
2818 ASSERT(args->index2 >= 0);
2819
2820 if (entry1->flags & XFS_ATTR_LOCAL) {
2821 name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
2822 namelen1 = name_loc->namelen;
2823 name1 = (char *)name_loc->nameval;
2824 } else {
2825 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
2826 namelen1 = name_rmt->namelen;
2827 name1 = (char *)name_rmt->name;
2828 }
2829 if (entry2->flags & XFS_ATTR_LOCAL) {
2830 name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
2831 namelen2 = name_loc->namelen;
2832 name2 = (char *)name_loc->nameval;
2833 } else {
2834 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
2835 namelen2 = name_rmt->namelen;
2836 name2 = (char *)name_rmt->name;
2837 }
2838 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2839 ASSERT(namelen1 == namelen2);
2840 ASSERT(memcmp(name1, name2, namelen1) == 0);
2841 #endif /* DEBUG */
2842
2843 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2844 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2845
2846 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2847 xfs_trans_log_buf(args->trans, bp1,
2848 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2849 if (args->rmtblkno) {
2850 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2851 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
2852 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2853 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2854 xfs_trans_log_buf(args->trans, bp1,
2855 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2856 }
2857
2858 entry2->flags |= XFS_ATTR_INCOMPLETE;
2859 xfs_trans_log_buf(args->trans, bp2,
2860 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2861 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2862 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
2863 name_rmt->valueblk = 0;
2864 name_rmt->valuelen = 0;
2865 xfs_trans_log_buf(args->trans, bp2,
2866 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2867 }
2868
2869 /*
2870 * Commit the flag value change and start the next trans in series.
2871 */
2872 error = xfs_trans_roll_inode(&args->trans, args->dp);
2873
2874 return error;
2875 }