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