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