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