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