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