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