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