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