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