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