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