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