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