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