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