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