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