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