blob: 6c323f8a4cd19cb8b169bcc17446dd62ffb4ea75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
34#include "xfs_alloc.h"
35#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110040#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_attr.h"
43#include "xfs_attr_leaf.h"
44#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * xfs_attr_leaf.c
48 *
49 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
50 */
51
52/*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
55
56/*
57 * Routines used for growing the Btree.
58 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100059STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
60 xfs_dabuf_t **bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
62 int freemap_index);
63STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
64STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
65 xfs_da_state_blk_t *blk1,
66 xfs_da_state_blk_t *blk2);
67STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
68 xfs_da_state_blk_t *leaf_blk_1,
69 xfs_da_state_blk_t *leaf_blk_2,
70 int *number_entries_in_blk1,
71 int *number_usedbytes_in_blk1);
72
73/*
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100074 * Routines used for shrinking the Btree.
75 */
76STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
77 xfs_dabuf_t *bp, int level);
78STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
79 xfs_dabuf_t *bp);
80STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
81 xfs_dablk_t blkno, int blkcnt);
82
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Utility routines.
85 */
86STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
87 int src_start,
88 xfs_attr_leafblock_t *dst_leaf,
89 int dst_start, int move_count,
90 xfs_mount_t *mp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100091STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
Tim Shimmin726801b2006-09-28 11:01:37 +100092
93/*========================================================================
94 * Namespace helper routines
95 *========================================================================*/
96
Tim Shimmin726801b2006-09-28 11:01:37 +100097/*
98 * If namespace bits don't match return 0.
99 * If all match then return 1.
100 */
David Chinner7989cb82007-02-10 18:34:56 +1100101STATIC_INLINE int
Tim Shimmin726801b2006-09-28 11:01:37 +1000102xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
103{
104 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100109 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *========================================================================*/
111
112/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100113 * Query whether the requested number of additional bytes of extended
114 * attribute space will be able to fit inline.
115 * Returns zero if not, else the di_forkoff fork offset to be used in the
116 * literal area for attribute data once the new bytes have been added.
117 *
118 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
119 * special case for dev/uuid inodes, they have fixed size data forks.
120 */
121int
122xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
123{
124 int offset;
125 int minforkoff; /* lower limit on valid forkoff locations */
126 int maxforkoff; /* upper limit on valid forkoff locations */
Barry Naujoke5889e92007-02-10 18:35:58 +1100127 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100128 xfs_mount_t *mp = dp->i_mount;
129
Nathan Scottd8cc8902005-11-02 10:34:53 +1100130 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
131
132 switch (dp->i_d.di_format) {
133 case XFS_DINODE_FMT_DEV:
134 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
135 return (offset >= minforkoff) ? minforkoff : 0;
136 case XFS_DINODE_FMT_UUID:
137 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
138 return (offset >= minforkoff) ? minforkoff : 0;
139 }
140
Nathan Scott13059ff2006-01-11 15:32:01 +1100141 if (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +1100142 if (bytes <= XFS_IFORK_ASIZE(dp))
Eric Sandeencb49dbb2008-04-17 16:50:09 +1000143 return dp->i_d.di_forkoff;
Nathan Scottda087ba2005-11-02 15:00:20 +1100144 return 0;
145 }
146
Barry Naujoke5889e92007-02-10 18:35:58 +1100147 dsize = dp->i_df.if_bytes;
148
149 switch (dp->i_d.di_format) {
150 case XFS_DINODE_FMT_EXTENTS:
151 /*
152 * If there is no attr fork and the data fork is extents,
153 * determine if creating the default attr fork will result
154 * in the extents form migrating to btree. If so, the
155 * minimum offset only needs to be the space required for
156 * the btree root.
157 */
158 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes > mp->m_attroffset)
159 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
160 break;
161
162 case XFS_DINODE_FMT_BTREE:
163 /*
164 * If have data btree then keep forkoff if we have one,
165 * otherwise we are adding a new attr, so then we set
166 * minforkoff to where the btree root can finish so we have
167 * plenty of room for attrs
168 */
169 if (dp->i_d.di_forkoff) {
170 if (offset < dp->i_d.di_forkoff)
171 return 0;
172 else
173 return dp->i_d.di_forkoff;
174 } else
175 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
176 break;
177 }
178
179 /*
180 * A data fork btree root must have space for at least
181 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
182 */
183 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100184 minforkoff = roundup(minforkoff, 8) >> 3;
185
186 /* attr fork btree root can have at least this many key/ptr pairs */
187 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
188 maxforkoff = maxforkoff >> 3; /* rounded down */
189
190 if (offset >= minforkoff && offset < maxforkoff)
191 return offset;
192 if (offset >= maxforkoff)
193 return maxforkoff;
194 return 0;
195}
196
197/*
198 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
199 */
200STATIC void
201xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
202{
Nathan Scott13059ff2006-01-11 15:32:01 +1100203 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100204 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000205 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100206 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
207 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000208 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100209 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
210 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000211 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100212 }
213}
214
215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Create the initial contents of a shortform attribute list.
217 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100218void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219xfs_attr_shortform_create(xfs_da_args_t *args)
220{
221 xfs_attr_sf_hdr_t *hdr;
222 xfs_inode_t *dp;
223 xfs_ifork_t *ifp;
224
225 dp = args->dp;
226 ASSERT(dp != NULL);
227 ifp = dp->i_afp;
228 ASSERT(ifp != NULL);
229 ASSERT(ifp->if_bytes == 0);
230 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
231 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
232 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
233 ifp->if_flags |= XFS_IFINLINE;
234 } else {
235 ASSERT(ifp->if_flags & XFS_IFINLINE);
236 }
237 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
238 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
239 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100240 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244/*
245 * Add a name/value pair to the shortform attribute list.
246 * Overflow from the inode has already been checked for.
247 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100248void
249xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 xfs_attr_shortform_t *sf;
252 xfs_attr_sf_entry_t *sfe;
253 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100254 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 xfs_inode_t *dp;
256 xfs_ifork_t *ifp;
257
258 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100259 mp = dp->i_mount;
260 dp->i_d.di_forkoff = forkoff;
261 dp->i_df.if_ext_max =
262 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
263 dp->i_afp->if_ext_max =
264 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 ifp = dp->i_afp;
267 ASSERT(ifp->if_flags & XFS_IFINLINE);
268 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
269 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100270 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100271#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (sfe->namelen != args->namelen)
273 continue;
274 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
275 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000276 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100278 ASSERT(0);
279#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
282 offset = (char *)sfe - (char *)sf;
283 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
284 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
285 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
286 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
287
288 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100289 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000290 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 memcpy(sfe->nameval, args->name, args->namelen);
292 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100293 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800294 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
296
Nathan Scottd8cc8902005-11-02 10:34:53 +1100297 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100301 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303int
304xfs_attr_shortform_remove(xfs_da_args_t *args)
305{
306 xfs_attr_shortform_t *sf;
307 xfs_attr_sf_entry_t *sfe;
308 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100309 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 xfs_inode_t *dp;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100313 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 base = sizeof(xfs_attr_sf_hdr_t);
315 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
316 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100317 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100318 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 base += size, i++) {
320 size = XFS_ATTR_SF_ENTSIZE(sfe);
321 if (sfe->namelen != args->namelen)
322 continue;
323 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
324 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000325 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 continue;
327 break;
328 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100329 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return(XFS_ERROR(ENOATTR));
331
Nathan Scottd8cc8902005-11-02 10:34:53 +1100332 /*
333 * Fix up the attribute fork data, covering the hole
334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100336 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100337 if (end != totsize)
338 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100339 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800340 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100341
342 /*
343 * Fix up the start offset of the attribute fork
344 */
345 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000346 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
347 !(args->op_flags & XFS_DA_OP_ADDNAME) &&
348 (mp->m_flags & XFS_MOUNT_ATTR2) &&
349 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE)) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100350 /*
351 * Last attribute now removed, revert to original
352 * inode format making all literal area available
353 * to the data fork once more.
354 */
355 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
356 dp->i_d.di_forkoff = 0;
357 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
358 ASSERT(dp->i_d.di_anextents == 0);
359 ASSERT(dp->i_afp == NULL);
360 dp->i_df.if_ext_max =
361 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
362 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
363 } else {
364 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
365 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
366 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000367 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
368 (args->op_flags & XFS_DA_OP_ADDNAME) ||
369 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
370 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100371 dp->i_afp->if_ext_max =
372 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
373 dp->i_df.if_ext_max =
374 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
375 xfs_trans_log_inode(args->trans, dp,
376 XFS_ILOG_CORE | XFS_ILOG_ADATA);
377 }
378
379 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 return(0);
382}
383
384/*
385 * Look up a name in a shortform attribute list structure.
386 */
387/*ARGSUSED*/
388int
389xfs_attr_shortform_lookup(xfs_da_args_t *args)
390{
391 xfs_attr_shortform_t *sf;
392 xfs_attr_sf_entry_t *sfe;
393 int i;
394 xfs_ifork_t *ifp;
395
396 ifp = args->dp->i_afp;
397 ASSERT(ifp->if_flags & XFS_IFINLINE);
398 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
399 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100400 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
402 if (sfe->namelen != args->namelen)
403 continue;
404 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
405 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000406 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 continue;
408 return(XFS_ERROR(EEXIST));
409 }
410 return(XFS_ERROR(ENOATTR));
411}
412
413/*
414 * Look up a name in a shortform attribute list structure.
415 */
416/*ARGSUSED*/
417int
418xfs_attr_shortform_getvalue(xfs_da_args_t *args)
419{
420 xfs_attr_shortform_t *sf;
421 xfs_attr_sf_entry_t *sfe;
422 int i;
423
424 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
425 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
426 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100427 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
429 if (sfe->namelen != args->namelen)
430 continue;
431 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
432 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000433 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 continue;
435 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100436 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return(XFS_ERROR(EEXIST));
438 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100439 if (args->valuelen < sfe->valuelen) {
440 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return(XFS_ERROR(ERANGE));
442 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100443 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 memcpy(args->value, &sfe->nameval[args->namelen],
445 args->valuelen);
446 return(XFS_ERROR(EEXIST));
447 }
448 return(XFS_ERROR(ENOATTR));
449}
450
451/*
452 * Convert from using the shortform to the leaf.
453 */
454int
455xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
456{
457 xfs_inode_t *dp;
458 xfs_attr_shortform_t *sf;
459 xfs_attr_sf_entry_t *sfe;
460 xfs_da_args_t nargs;
461 char *tmpbuffer;
462 int error, i, size;
463 xfs_dablk_t blkno;
464 xfs_dabuf_t *bp;
465 xfs_ifork_t *ifp;
466
467 dp = args->dp;
468 ifp = dp->i_afp;
469 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100470 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 tmpbuffer = kmem_alloc(size, KM_SLEEP);
472 ASSERT(tmpbuffer != NULL);
473 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
474 sf = (xfs_attr_shortform_t *)tmpbuffer;
475
476 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
477 bp = NULL;
478 error = xfs_da_grow_inode(args, &blkno);
479 if (error) {
480 /*
481 * If we hit an IO error middle of the transaction inside
482 * grow_inode(), we may have inconsistent data. Bail out.
483 */
484 if (error == EIO)
485 goto out;
486 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
487 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
488 goto out;
489 }
490
491 ASSERT(blkno == 0);
492 error = xfs_attr_leaf_create(args, blkno, &bp);
493 if (error) {
494 error = xfs_da_shrink_inode(args, 0, bp);
495 bp = NULL;
496 if (error)
497 goto out;
498 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
499 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
500 goto out;
501 }
502
503 memset((char *)&nargs, 0, sizeof(nargs));
504 nargs.dp = dp;
505 nargs.firstblock = args->firstblock;
506 nargs.flist = args->flist;
507 nargs.total = args->total;
508 nargs.whichfork = XFS_ATTR_FORK;
509 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000510 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100513 for (i = 0; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 nargs.name = (char *)sfe->nameval;
515 nargs.namelen = sfe->namelen;
516 nargs.value = (char *)&sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100517 nargs.valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
519 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000520 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
522 ASSERT(error == ENOATTR);
523 error = xfs_attr_leaf_add(bp, &nargs);
524 ASSERT(error != ENOSPC);
525 if (error)
526 goto out;
527 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
528 }
529 error = 0;
530
531out:
532 if(bp)
533 xfs_da_buf_done(bp);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000534 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return(error);
536}
537
538STATIC int
539xfs_attr_shortform_compare(const void *a, const void *b)
540{
541 xfs_attr_sf_sort_t *sa, *sb;
542
543 sa = (xfs_attr_sf_sort_t *)a;
544 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100545 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100547 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return(1);
549 } else {
550 return(sa->entno - sb->entno);
551 }
552}
553
Tim Shimmin726801b2006-09-28 11:01:37 +1000554
555#define XFS_ISRESET_CURSOR(cursor) \
556 (!((cursor)->initted) && !((cursor)->hashval) && \
557 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558/*
559 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000560 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * If the output buffer is not large enough to hold them all, then we
562 * we have to calculate each entries' hashvalue and sort them before
563 * we can begin returning them to the user.
564 */
565/*ARGSUSED*/
566int
567xfs_attr_shortform_list(xfs_attr_list_context_t *context)
568{
569 attrlist_cursor_kern_t *cursor;
570 xfs_attr_sf_sort_t *sbuf, *sbp;
571 xfs_attr_shortform_t *sf;
572 xfs_attr_sf_entry_t *sfe;
573 xfs_inode_t *dp;
574 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000575 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 ASSERT(context != NULL);
578 dp = context->dp;
579 ASSERT(dp != NULL);
580 ASSERT(dp->i_afp != NULL);
581 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
582 ASSERT(sf != NULL);
583 if (!sf->hdr.count)
584 return(0);
585 cursor = context->cursor;
586 ASSERT(cursor != NULL);
587
588 xfs_attr_trace_l_c("sf start", context);
589
590 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000591 * If the buffer is large enough and the cursor is at the start,
592 * do not bother with sorting since we will return everything in
593 * one buffer and another call using the cursor won't need to be
594 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000596 * If bufsize is zero then put_listent must be a search function
597 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000599 if (context->bufsize == 0 ||
600 (XFS_ISRESET_CURSOR(cursor) &&
601 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100602 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000603 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000604 sfe->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000605 (char *)sfe->nameval,
606 (int)sfe->namelen,
607 (int)sfe->valuelen,
608 (char*)&sfe->nameval[sfe->namelen]);
609
610 /*
611 * Either search callback finished early or
612 * didn't fit it all in the buffer after all.
613 */
614 if (context->seen_enough)
615 break;
616
617 if (error)
618 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
620 }
621 xfs_attr_trace_l_c("sf big-gulp", context);
622 return(0);
623 }
624
Tim Shimmin726801b2006-09-28 11:01:37 +1000625 /* do no more for a search callback */
626 if (context->bufsize == 0)
627 return 0;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * It didn't all fit, so we have to sort everything on hashval.
631 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100632 sbsize = sf->hdr.count * sizeof(*sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
634
635 /*
636 * Scan the attribute list for the rest of the entries, storing
637 * the relevant info from only those that match into a buffer.
638 */
639 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100640 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (unlikely(
642 ((char *)sfe < (char *)sf) ||
643 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
644 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
645 XFS_ERRLEVEL_LOW,
646 context->dp->i_mount, sfe);
647 xfs_attr_trace_l_c("sf corrupted", context);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000648 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return XFS_ERROR(EFSCORRUPTED);
650 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 sbp->entno = i;
Nathan Scott984a0812006-03-17 17:29:31 +1100653 sbp->hash = xfs_da_hashname((char *)sfe->nameval, sfe->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 sbp->name = (char *)sfe->nameval;
655 sbp->namelen = sfe->namelen;
656 /* These are bytes, and both on-disk, don't endian-flip */
657 sbp->valuelen = sfe->valuelen;
658 sbp->flags = sfe->flags;
659 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
660 sbp++;
661 nsbuf++;
662 }
663
664 /*
665 * Sort the entries on hash then entno.
666 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100667 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /*
670 * Re-find our place IN THE SORTED LIST.
671 */
672 count = 0;
673 cursor->initted = 1;
674 cursor->blkno = 0;
675 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100676 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (cursor->offset == count) {
678 break;
679 }
680 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100681 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 break;
683 }
684 }
685 if (i == nsbuf) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000686 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 xfs_attr_trace_l_c("blk end", context);
688 return(0);
689 }
690
691 /*
692 * Loop putting entries into the user buffer.
693 */
694 for ( ; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100695 if (cursor->hashval != sbp->hash) {
696 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 cursor->offset = 0;
698 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000699 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000700 sbp->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000701 sbp->name,
702 sbp->namelen,
703 sbp->valuelen,
704 &sbp->name[sbp->namelen]);
705 if (error)
706 return error;
707 if (context->seen_enough)
708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 cursor->offset++;
710 }
711
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000712 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 xfs_attr_trace_l_c("sf E-O-F", context);
714 return(0);
715}
716
717/*
718 * Check a leaf attribute block to see if all the entries would fit into
719 * a shortform attribute list.
720 */
721int
722xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
723{
724 xfs_attr_leafblock_t *leaf;
725 xfs_attr_leaf_entry_t *entry;
726 xfs_attr_leaf_name_local_t *name_loc;
727 int bytes, i;
728
729 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100730 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 entry = &leaf->entries[0];
733 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100734 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (entry->flags & XFS_ATTR_INCOMPLETE)
736 continue; /* don't copy partial entries */
737 if (!(entry->flags & XFS_ATTR_LOCAL))
738 return(0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600739 name_loc = xfs_attr_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
741 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100742 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return(0);
744 bytes += sizeof(struct xfs_attr_sf_entry)-1
745 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100746 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100748 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100749 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100750 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100751 return(-1);
752 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/*
756 * Convert a leaf attribute list to shortform attribute list
757 */
758int
Nathan Scottd8cc8902005-11-02 10:34:53 +1100759xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 xfs_attr_leafblock_t *leaf;
762 xfs_attr_leaf_entry_t *entry;
763 xfs_attr_leaf_name_local_t *name_loc;
764 xfs_da_args_t nargs;
765 xfs_inode_t *dp;
766 char *tmpbuffer;
767 int error, i;
768
769 dp = args->dp;
770 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
771 ASSERT(tmpbuffer != NULL);
772
773 ASSERT(bp != NULL);
774 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
775 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Nathan Scott89da0542006-03-17 17:28:40 +1100776 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
778
779 /*
780 * Clean out the prior contents of the attribute list.
781 */
782 error = xfs_da_shrink_inode(args, 0, bp);
783 if (error)
784 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100785
786 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +1100787 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +1100788 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Nathan Scotte0144ca2005-11-25 16:42:22 +1100789
Nathan Scottd8cc8902005-11-02 10:34:53 +1100790 /*
791 * Last attribute was removed, revert to original
792 * inode format making all literal area available
793 * to the data fork once more.
794 */
795 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
796 dp->i_d.di_forkoff = 0;
797 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
798 ASSERT(dp->i_d.di_anextents == 0);
799 ASSERT(dp->i_afp == NULL);
800 dp->i_df.if_ext_max =
801 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
802 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100804 }
805
806 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /*
809 * Copy the attributes
810 */
811 memset((char *)&nargs, 0, sizeof(nargs));
812 nargs.dp = dp;
813 nargs.firstblock = args->firstblock;
814 nargs.flist = args->flist;
815 nargs.total = args->total;
816 nargs.whichfork = XFS_ATTR_FORK;
817 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000818 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +1100820 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (entry->flags & XFS_ATTR_INCOMPLETE)
822 continue; /* don't copy partial entries */
823 if (!entry->nameidx)
824 continue;
825 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600826 name_loc = xfs_attr_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 nargs.name = (char *)name_loc->nameval;
828 nargs.namelen = name_loc->namelen;
829 nargs.value = (char *)&name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +1100830 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100831 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +1000832 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100833 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 error = 0;
836
837out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000838 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return(error);
840}
841
842/*
843 * Convert from using a single leaf to a root node and a leaf.
844 */
845int
846xfs_attr_leaf_to_node(xfs_da_args_t *args)
847{
848 xfs_attr_leafblock_t *leaf;
849 xfs_da_intnode_t *node;
850 xfs_inode_t *dp;
851 xfs_dabuf_t *bp1, *bp2;
852 xfs_dablk_t blkno;
853 int error;
854
855 dp = args->dp;
856 bp1 = bp2 = NULL;
857 error = xfs_da_grow_inode(args, &blkno);
858 if (error)
859 goto out;
860 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
861 XFS_ATTR_FORK);
862 if (error)
863 goto out;
864 ASSERT(bp1 != NULL);
865 bp2 = NULL;
866 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
867 XFS_ATTR_FORK);
868 if (error)
869 goto out;
870 ASSERT(bp2 != NULL);
871 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
872 xfs_da_buf_done(bp1);
873 bp1 = NULL;
874 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
875
876 /*
877 * Set up the new root node.
878 */
879 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
880 if (error)
881 goto out;
882 node = bp1->data;
883 leaf = bp2->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100884 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /* both on-disk, don't endian-flip twice */
886 node->btree[0].hashval =
Nathan Scott918ae422006-03-17 17:28:54 +1100887 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
Nathan Scott403432d2006-03-17 17:29:46 +1100888 node->btree[0].before = cpu_to_be32(blkno);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100889 node->hdr.count = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
891 error = 0;
892out:
893 if (bp1)
894 xfs_da_buf_done(bp1);
895 if (bp2)
896 xfs_da_buf_done(bp2);
897 return(error);
898}
899
900
901/*========================================================================
902 * Routines used for growing the Btree.
903 *========================================================================*/
904
905/*
906 * Create the initial contents of a leaf attribute list
907 * or a leaf in a node attribute list.
908 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000909STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
911{
912 xfs_attr_leafblock_t *leaf;
913 xfs_attr_leaf_hdr_t *hdr;
914 xfs_inode_t *dp;
915 xfs_dabuf_t *bp;
916 int error;
917
918 dp = args->dp;
919 ASSERT(dp != NULL);
920 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
921 XFS_ATTR_FORK);
922 if (error)
923 return(error);
924 ASSERT(bp != NULL);
925 leaf = bp->data;
926 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
927 hdr = &leaf->hdr;
Nathan Scott89da0542006-03-17 17:28:40 +1100928 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +1100929 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +1100931 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
933 }
934
Nathan Scott918ae422006-03-17 17:28:54 +1100935 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
936 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
937 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
940
941 *bpp = bp;
942 return(0);
943}
944
945/*
946 * Split the leaf node, rebalance, then add the new entry.
947 */
948int
949xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
950 xfs_da_state_blk_t *newblk)
951{
952 xfs_dablk_t blkno;
953 int error;
954
955 /*
956 * Allocate space for a new leaf node.
957 */
958 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
959 error = xfs_da_grow_inode(state->args, &blkno);
960 if (error)
961 return(error);
962 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
963 if (error)
964 return(error);
965 newblk->blkno = blkno;
966 newblk->magic = XFS_ATTR_LEAF_MAGIC;
967
968 /*
969 * Rebalance the entries across the two leaves.
970 * NOTE: rebalance() currently depends on the 2nd block being empty.
971 */
972 xfs_attr_leaf_rebalance(state, oldblk, newblk);
973 error = xfs_da_blk_link(state, oldblk, newblk);
974 if (error)
975 return(error);
976
977 /*
978 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
979 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
980 * "new" attrs info. Will need the "old" info to remove it later.
981 *
982 * Insert the "new" entry in the correct block.
983 */
984 if (state->inleaf)
985 error = xfs_attr_leaf_add(oldblk->bp, state->args);
986 else
987 error = xfs_attr_leaf_add(newblk->bp, state->args);
988
989 /*
990 * Update last hashval in each block since we added the name.
991 */
992 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
993 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
994 return(error);
995}
996
997/*
998 * Add a name to the leaf attribute list structure.
999 */
1000int
1001xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
1002{
1003 xfs_attr_leafblock_t *leaf;
1004 xfs_attr_leaf_hdr_t *hdr;
1005 xfs_attr_leaf_map_t *map;
1006 int tablesize, entsize, sum, tmp, i;
1007
1008 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001009 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001011 && (args->index <= be16_to_cpu(leaf->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 hdr = &leaf->hdr;
Nathan Scottaa82daa2005-11-02 10:33:33 +11001013 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1015
1016 /*
1017 * Search through freemap for first-fit on new name length.
1018 * (may need to figure in size of entry struct too)
1019 */
Nathan Scott918ae422006-03-17 17:28:54 +11001020 tablesize = (be16_to_cpu(hdr->count) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 * sizeof(xfs_attr_leaf_entry_t)
1022 + sizeof(xfs_attr_leaf_hdr_t);
1023 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1024 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
Nathan Scott918ae422006-03-17 17:28:54 +11001025 if (tablesize > be16_to_cpu(hdr->firstused)) {
1026 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 continue;
1028 }
1029 if (!map->size)
1030 continue; /* no space in this map */
1031 tmp = entsize;
Nathan Scott918ae422006-03-17 17:28:54 +11001032 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 tmp += sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001034 if (be16_to_cpu(map->size) >= tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 tmp = xfs_attr_leaf_add_work(bp, args, i);
1036 return(tmp);
1037 }
Nathan Scott918ae422006-03-17 17:28:54 +11001038 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040
1041 /*
1042 * If there are no holes in the address space of the block,
1043 * and we don't have enough freespace, then compaction will do us
1044 * no good and we should just give up.
1045 */
1046 if (!hdr->holes && (sum < entsize))
1047 return(XFS_ERROR(ENOSPC));
1048
1049 /*
1050 * Compact the entries to coalesce free space.
1051 * This may change the hdr->count via dropping INCOMPLETE entries.
1052 */
1053 xfs_attr_leaf_compact(args->trans, bp);
1054
1055 /*
1056 * After compaction, the block is guaranteed to have only one
1057 * free region, in freemap[0]. If it is not big enough, give up.
1058 */
Nathan Scott918ae422006-03-17 17:28:54 +11001059 if (be16_to_cpu(hdr->freemap[0].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1061 return(XFS_ERROR(ENOSPC));
1062
1063 return(xfs_attr_leaf_add_work(bp, args, 0));
1064}
1065
1066/*
1067 * Add a name to a leaf attribute list structure.
1068 */
1069STATIC int
1070xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1071{
1072 xfs_attr_leafblock_t *leaf;
1073 xfs_attr_leaf_hdr_t *hdr;
1074 xfs_attr_leaf_entry_t *entry;
1075 xfs_attr_leaf_name_local_t *name_loc;
1076 xfs_attr_leaf_name_remote_t *name_rmt;
1077 xfs_attr_leaf_map_t *map;
1078 xfs_mount_t *mp;
1079 int tmp, i;
1080
1081 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001082 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 hdr = &leaf->hdr;
1084 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
Nathan Scott918ae422006-03-17 17:28:54 +11001085 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /*
1088 * Force open some space in the entry array and fill it in.
1089 */
1090 entry = &leaf->entries[args->index];
Nathan Scott918ae422006-03-17 17:28:54 +11001091 if (args->index < be16_to_cpu(hdr->count)) {
1092 tmp = be16_to_cpu(hdr->count) - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 tmp *= sizeof(xfs_attr_leaf_entry_t);
1094 memmove((char *)(entry+1), (char *)entry, tmp);
1095 xfs_da_log_buf(args->trans, bp,
1096 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1097 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001098 be16_add_cpu(&hdr->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /*
1101 * Allocate space for the new string (at the end of the run).
1102 */
1103 map = &hdr->freemap[mapindex];
1104 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001105 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1106 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1107 ASSERT(be16_to_cpu(map->size) >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001108 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1109 mp->m_sb.sb_blocksize, NULL));
Nathan Scott918ae422006-03-17 17:28:54 +11001110 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1111 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001112 be16_add_cpu(&map->size,
Nathan Scottaa82daa2005-11-02 10:33:33 +11001113 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1114 mp->m_sb.sb_blocksize, &tmp));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001115 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1116 be16_to_cpu(map->size));
1117 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001119 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001120 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 entry->flags |= XFS_ATTR_INCOMPLETE;
1122 if ((args->blkno2 == args->blkno) &&
1123 (args->index2 <= args->index)) {
1124 args->index2++;
1125 }
1126 }
1127 xfs_da_log_buf(args->trans, bp,
1128 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001129 ASSERT((args->index == 0) ||
1130 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Nathan Scott918ae422006-03-17 17:28:54 +11001131 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001132 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /*
1135 * Copy the attribute name and value into the new space.
1136 *
1137 * For "remote" attribute values, simply note that we need to
1138 * allocate space for the "remote" value. We can't actually
1139 * allocate the extents in this transaction, and we can't decide
1140 * which blocks they should be as we might allocate more blocks
1141 * as part of this transaction (a split operation for example).
1142 */
1143 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001144 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001146 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1148 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001149 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001151 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 name_rmt->namelen = args->namelen;
1153 memcpy((char *)name_rmt->name, args->name, args->namelen);
1154 entry->flags |= XFS_ATTR_INCOMPLETE;
1155 /* just in case */
1156 name_rmt->valuelen = 0;
1157 name_rmt->valueblk = 0;
1158 args->rmtblkno = 1;
1159 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1160 }
1161 xfs_da_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001162 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 xfs_attr_leaf_entsize(leaf, args->index)));
1164
1165 /*
1166 * Update the control info for this leaf node
1167 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001168 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* both on-disk, don't endian-flip twice */
1170 hdr->firstused = entry->nameidx;
1171 }
Nathan Scott918ae422006-03-17 17:28:54 +11001172 ASSERT(be16_to_cpu(hdr->firstused) >=
1173 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1174 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 + sizeof(xfs_attr_leaf_hdr_t);
1176 map = &hdr->freemap[0];
1177 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001178 if (be16_to_cpu(map->base) == tmp) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001179 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1180 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001181 -((int)sizeof(xfs_attr_leaf_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001184 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 xfs_da_log_buf(args->trans, bp,
1186 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1187 return(0);
1188}
1189
1190/*
1191 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1192 */
1193STATIC void
1194xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1195{
1196 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1197 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1198 xfs_mount_t *mp;
1199 char *tmpbuffer;
1200
1201 mp = trans->t_mountp;
1202 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1203 ASSERT(tmpbuffer != NULL);
1204 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1205 memset(bp->data, 0, XFS_LBSIZE(mp));
1206
1207 /*
1208 * Copy basic information
1209 */
1210 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1211 leaf_d = bp->data;
1212 hdr_s = &leaf_s->hdr;
1213 hdr_d = &leaf_d->hdr;
1214 hdr_d->info = hdr_s->info; /* struct copy */
Nathan Scott918ae422006-03-17 17:28:54 +11001215 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /* handle truncation gracefully */
1217 if (!hdr_d->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001218 hdr_d->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1220 }
1221 hdr_d->usedbytes = 0;
1222 hdr_d->count = 0;
1223 hdr_d->holes = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001224 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1225 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1226 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /*
1229 * Copy all entry's in the same (sorted) order,
1230 * but allocate name/value pairs packed and in sequence.
1231 */
1232 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001233 be16_to_cpu(hdr_s->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1235
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001236 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239/*
1240 * Redistribute the attribute list entries between two leaf nodes,
1241 * taking into account the size of the new entry.
1242 *
1243 * NOTE: if new block is empty, then it will get the upper half of the
1244 * old block. At present, all (one) callers pass in an empty second block.
1245 *
1246 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1247 * to match what it is doing in splitting the attribute leaf block. Those
1248 * values are used in "atomic rename" operations on attributes. Note that
1249 * the "new" and "old" values can end up in different blocks.
1250 */
1251STATIC void
1252xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1253 xfs_da_state_blk_t *blk2)
1254{
1255 xfs_da_args_t *args;
1256 xfs_da_state_blk_t *tmp_blk;
1257 xfs_attr_leafblock_t *leaf1, *leaf2;
1258 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1259 int count, totallen, max, space, swap;
1260
1261 /*
1262 * Set up environment.
1263 */
1264 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1265 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1266 leaf1 = blk1->bp->data;
1267 leaf2 = blk2->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001268 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1269 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 args = state->args;
1271
1272 /*
1273 * Check ordering of blocks, reverse if it makes things simpler.
1274 *
1275 * NOTE: Given that all (current) callers pass in an empty
1276 * second block, this code should never set "swap".
1277 */
1278 swap = 0;
1279 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1280 tmp_blk = blk1;
1281 blk1 = blk2;
1282 blk2 = tmp_blk;
1283 leaf1 = blk1->bp->data;
1284 leaf2 = blk2->bp->data;
1285 swap = 1;
1286 }
1287 hdr1 = &leaf1->hdr;
1288 hdr2 = &leaf2->hdr;
1289
1290 /*
1291 * Examine entries until we reduce the absolute difference in
1292 * byte usage between the two blocks to a minimum. Then get
1293 * the direction to copy and the number of elements to move.
1294 *
1295 * "inleaf" is true if the new entry should be inserted into blk1.
1296 * If "swap" is also true, then reverse the sense of "inleaf".
1297 */
1298 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1299 &count, &totallen);
1300 if (swap)
1301 state->inleaf = !state->inleaf;
1302
1303 /*
1304 * Move any entries required from leaf to leaf:
1305 */
Nathan Scott918ae422006-03-17 17:28:54 +11001306 if (count < be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /*
1308 * Figure the total bytes to be added to the destination leaf.
1309 */
1310 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001311 count = be16_to_cpu(hdr1->count) - count;
1312 space = be16_to_cpu(hdr1->usedbytes) - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 space += count * sizeof(xfs_attr_leaf_entry_t);
1314
1315 /*
1316 * leaf2 is the destination, compact it if it looks tight.
1317 */
Nathan Scott918ae422006-03-17 17:28:54 +11001318 max = be16_to_cpu(hdr2->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001320 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (space > max) {
1322 xfs_attr_leaf_compact(args->trans, blk2->bp);
1323 }
1324
1325 /*
1326 * Move high entries from leaf1 to low end of leaf2.
1327 */
Nathan Scott918ae422006-03-17 17:28:54 +11001328 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 leaf2, 0, count, state->mp);
1330
1331 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1332 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Nathan Scott918ae422006-03-17 17:28:54 +11001333 } else if (count > be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
1335 * I assert that since all callers pass in an empty
1336 * second buffer, this code should never execute.
1337 */
1338
1339 /*
1340 * Figure the total bytes to be added to the destination leaf.
1341 */
1342 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001343 count -= be16_to_cpu(hdr1->count);
1344 space = totallen - be16_to_cpu(hdr1->usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 space += count * sizeof(xfs_attr_leaf_entry_t);
1346
1347 /*
1348 * leaf1 is the destination, compact it if it looks tight.
1349 */
Nathan Scott918ae422006-03-17 17:28:54 +11001350 max = be16_to_cpu(hdr1->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001352 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (space > max) {
1354 xfs_attr_leaf_compact(args->trans, blk1->bp);
1355 }
1356
1357 /*
1358 * Move low entries from leaf2 to high end of leaf1.
1359 */
1360 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
Nathan Scott918ae422006-03-17 17:28:54 +11001361 be16_to_cpu(hdr1->count), count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1364 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1365 }
1366
1367 /*
1368 * Copy out last hashval in each block for B-tree code.
1369 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001370 blk1->hashval = be32_to_cpu(
1371 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1372 blk2->hashval = be32_to_cpu(
1373 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /*
1376 * Adjust the expected index for insertion.
1377 * NOTE: this code depends on the (current) situation that the
1378 * second block was originally empty.
1379 *
1380 * If the insertion point moved to the 2nd block, we must adjust
1381 * the index. We must also track the entry just following the
1382 * new entry for use in an "atomic rename" operation, that entry
1383 * is always the "old" entry and the "new" entry is what we are
1384 * inserting. The index/blkno fields refer to the "old" entry,
1385 * while the index2/blkno2 fields refer to the "new" entry.
1386 */
Nathan Scott918ae422006-03-17 17:28:54 +11001387 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ASSERT(state->inleaf == 0);
Nathan Scott918ae422006-03-17 17:28:54 +11001389 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 args->index = args->index2 = blk2->index;
1391 args->blkno = args->blkno2 = blk2->blkno;
Nathan Scott918ae422006-03-17 17:28:54 +11001392 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (state->inleaf) {
1394 args->index = blk1->index;
1395 args->blkno = blk1->blkno;
1396 args->index2 = 0;
1397 args->blkno2 = blk2->blkno;
1398 } else {
1399 blk2->index = blk1->index
Nathan Scott918ae422006-03-17 17:28:54 +11001400 - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 args->index = args->index2 = blk2->index;
1402 args->blkno = args->blkno2 = blk2->blkno;
1403 }
1404 } else {
1405 ASSERT(state->inleaf == 1);
1406 args->index = args->index2 = blk1->index;
1407 args->blkno = args->blkno2 = blk1->blkno;
1408 }
1409}
1410
1411/*
1412 * Examine entries until we reduce the absolute difference in
1413 * byte usage between the two blocks to a minimum.
1414 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1415 * GROT: there will always be enough room in either block for a new entry.
1416 * GROT: Do a double-split for this case?
1417 */
1418STATIC int
1419xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1420 xfs_da_state_blk_t *blk1,
1421 xfs_da_state_blk_t *blk2,
1422 int *countarg, int *usedbytesarg)
1423{
1424 xfs_attr_leafblock_t *leaf1, *leaf2;
1425 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1426 xfs_attr_leaf_entry_t *entry;
1427 int count, max, index, totallen, half;
1428 int lastdelta, foundit, tmp;
1429
1430 /*
1431 * Set up environment.
1432 */
1433 leaf1 = blk1->bp->data;
1434 leaf2 = blk2->bp->data;
1435 hdr1 = &leaf1->hdr;
1436 hdr2 = &leaf2->hdr;
1437 foundit = 0;
1438 totallen = 0;
1439
1440 /*
1441 * Examine entries until we reduce the absolute difference in
1442 * byte usage between the two blocks to a minimum.
1443 */
Nathan Scott918ae422006-03-17 17:28:54 +11001444 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 half = (max+1) * sizeof(*entry);
Nathan Scott918ae422006-03-17 17:28:54 +11001446 half += be16_to_cpu(hdr1->usedbytes) +
1447 be16_to_cpu(hdr2->usedbytes) +
1448 xfs_attr_leaf_newentsize(
1449 state->args->namelen,
1450 state->args->valuelen,
1451 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 half /= 2;
1453 lastdelta = state->blocksize;
1454 entry = &leaf1->entries[0];
1455 for (count = index = 0; count < max; entry++, index++, count++) {
1456
1457#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1458 /*
1459 * The new entry is in the first block, account for it.
1460 */
1461 if (count == blk1->index) {
1462 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001463 xfs_attr_leaf_newentsize(
1464 state->args->namelen,
1465 state->args->valuelen,
1466 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1468 break;
1469 lastdelta = XFS_ATTR_ABS(half - tmp);
1470 totallen = tmp;
1471 foundit = 1;
1472 }
1473
1474 /*
1475 * Wrap around into the second block if necessary.
1476 */
Nathan Scott918ae422006-03-17 17:28:54 +11001477 if (count == be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 leaf1 = leaf2;
1479 entry = &leaf1->entries[0];
1480 index = 0;
1481 }
1482
1483 /*
1484 * Figure out if next leaf entry would be too much.
1485 */
1486 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1487 index);
1488 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1489 break;
1490 lastdelta = XFS_ATTR_ABS(half - tmp);
1491 totallen = tmp;
1492#undef XFS_ATTR_ABS
1493 }
1494
1495 /*
1496 * Calculate the number of usedbytes that will end up in lower block.
1497 * If new entry not in lower block, fix up the count.
1498 */
1499 totallen -= count * sizeof(*entry);
1500 if (foundit) {
1501 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001502 xfs_attr_leaf_newentsize(
1503 state->args->namelen,
1504 state->args->valuelen,
1505 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507
1508 *countarg = count;
1509 *usedbytesarg = totallen;
1510 return(foundit);
1511}
1512
1513/*========================================================================
1514 * Routines used for shrinking the Btree.
1515 *========================================================================*/
1516
1517/*
1518 * Check a leaf block and its neighbors to see if the block should be
1519 * collapsed into one or the other neighbor. Always keep the block
1520 * with the smaller block number.
1521 * If the current block is over 50% full, don't try to join it, return 0.
1522 * If the block is empty, fill in the state structure and return 2.
1523 * If it can be collapsed, fill in the state structure and return 1.
1524 * If nothing can be done, return 0.
1525 *
1526 * GROT: allow for INCOMPLETE entries in calculation.
1527 */
1528int
1529xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1530{
1531 xfs_attr_leafblock_t *leaf;
1532 xfs_da_state_blk_t *blk;
1533 xfs_da_blkinfo_t *info;
1534 int count, bytes, forward, error, retval, i;
1535 xfs_dablk_t blkno;
1536 xfs_dabuf_t *bp;
1537
1538 /*
1539 * Check for the degenerate case of the block being over 50% full.
1540 * If so, it's not worth even looking to see if we might be able
1541 * to coalesce with a sibling.
1542 */
1543 blk = &state->path.blk[ state->path.active-1 ];
1544 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001545 ASSERT(be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001547 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1549 count * sizeof(xfs_attr_leaf_entry_t) +
Nathan Scott918ae422006-03-17 17:28:54 +11001550 be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (bytes > (state->blocksize >> 1)) {
1552 *action = 0; /* blk over 50%, don't try to join */
1553 return(0);
1554 }
1555
1556 /*
1557 * Check for the degenerate case of the block being empty.
1558 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001559 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 * to merge with the forward block unless it is NULL.
1561 */
1562 if (count == 0) {
1563 /*
1564 * Make altpath point to the block we want to keep and
1565 * path point to the block we want to drop (this one).
1566 */
Nathan Scott89da0542006-03-17 17:28:40 +11001567 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 memcpy(&state->altpath, &state->path, sizeof(state->path));
1569 error = xfs_da_path_shift(state, &state->altpath, forward,
1570 0, &retval);
1571 if (error)
1572 return(error);
1573 if (retval) {
1574 *action = 0;
1575 } else {
1576 *action = 2;
1577 }
1578 return(0);
1579 }
1580
1581 /*
1582 * Examine each sibling block to see if we can coalesce with
1583 * at least 25% free space to spare. We need to figure out
1584 * whether to merge with the forward or the backward block.
1585 * We prefer coalescing with the lower numbered sibling so as
1586 * to shrink an attribute list over time.
1587 */
1588 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +11001589 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 for (i = 0; i < 2; forward = !forward, i++) {
1591 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +11001592 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 else
Nathan Scott89da0542006-03-17 17:28:40 +11001594 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (blkno == 0)
1596 continue;
1597 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1598 blkno, -1, &bp, XFS_ATTR_FORK);
1599 if (error)
1600 return(error);
1601 ASSERT(bp != NULL);
1602
1603 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001604 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 bytes = state->blocksize - (state->blocksize>>2);
Nathan Scott918ae422006-03-17 17:28:54 +11001606 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001608 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11001609 count += be16_to_cpu(leaf->hdr.count);
1610 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1612 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1613 xfs_da_brelse(state->args->trans, bp);
1614 if (bytes >= 0)
1615 break; /* fits with at least 25% to spare */
1616 }
1617 if (i >= 2) {
1618 *action = 0;
1619 return(0);
1620 }
1621
1622 /*
1623 * Make altpath point to the block we want to keep (the lower
1624 * numbered block) and path point to the block we want to drop.
1625 */
1626 memcpy(&state->altpath, &state->path, sizeof(state->path));
1627 if (blkno < blk->blkno) {
1628 error = xfs_da_path_shift(state, &state->altpath, forward,
1629 0, &retval);
1630 } else {
1631 error = xfs_da_path_shift(state, &state->path, forward,
1632 0, &retval);
1633 }
1634 if (error)
1635 return(error);
1636 if (retval) {
1637 *action = 0;
1638 } else {
1639 *action = 1;
1640 }
1641 return(0);
1642}
1643
1644/*
1645 * Remove a name from the leaf attribute list structure.
1646 *
1647 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1648 * If two leaves are 37% full, when combined they will leave 25% free.
1649 */
1650int
1651xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1652{
1653 xfs_attr_leafblock_t *leaf;
1654 xfs_attr_leaf_hdr_t *hdr;
1655 xfs_attr_leaf_map_t *map;
1656 xfs_attr_leaf_entry_t *entry;
1657 int before, after, smallest, entsize;
1658 int tablesize, tmp, i;
1659 xfs_mount_t *mp;
1660
1661 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001662 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 hdr = &leaf->hdr;
1664 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001665 ASSERT((be16_to_cpu(hdr->count) > 0)
1666 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001668 && (args->index < be16_to_cpu(hdr->count)));
1669 ASSERT(be16_to_cpu(hdr->firstused) >=
1670 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 entry = &leaf->entries[args->index];
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001672 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1673 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /*
1676 * Scan through free region table:
1677 * check for adjacency of free'd entry with an existing one,
1678 * find smallest free region in case we need to replace it,
1679 * adjust any map that borders the entry table,
1680 */
Nathan Scott918ae422006-03-17 17:28:54 +11001681 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 + sizeof(xfs_attr_leaf_hdr_t);
1683 map = &hdr->freemap[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001684 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 before = after = -1;
1686 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1687 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1688 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001689 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1690 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1691 if (be16_to_cpu(map->base) == tablesize) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001692 be16_add_cpu(&map->base,
Nathan Scott918ae422006-03-17 17:28:54 +11001693 -((int)sizeof(xfs_attr_leaf_entry_t)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001694 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
Nathan Scott918ae422006-03-17 17:28:54 +11001697 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001698 == be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 before = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001700 } else if (be16_to_cpu(map->base)
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001701 == (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 after = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001703 } else if (be16_to_cpu(map->size) < tmp) {
1704 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 smallest = i;
1706 }
1707 }
1708
1709 /*
1710 * Coalesce adjacent freemap regions,
1711 * or replace the smallest region.
1712 */
1713 if ((before >= 0) || (after >= 0)) {
1714 if ((before >= 0) && (after >= 0)) {
1715 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001716 be16_add_cpu(&map->size, entsize);
1717 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001718 be16_to_cpu(hdr->freemap[after].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 hdr->freemap[after].base = 0;
1720 hdr->freemap[after].size = 0;
1721 } else if (before >= 0) {
1722 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001723 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 } else {
1725 map = &hdr->freemap[after];
1726 /* both on-disk, don't endian flip twice */
1727 map->base = entry->nameidx;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001728 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730 } else {
1731 /*
1732 * Replace smallest region (if it is smaller than free'd entry)
1733 */
1734 map = &hdr->freemap[smallest];
Nathan Scott918ae422006-03-17 17:28:54 +11001735 if (be16_to_cpu(map->size) < entsize) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001736 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
Nathan Scott918ae422006-03-17 17:28:54 +11001737 map->size = cpu_to_be16(entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739 }
1740
1741 /*
1742 * Did we remove the first entry?
1743 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001744 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 smallest = 1;
1746 else
1747 smallest = 0;
1748
1749 /*
1750 * Compress the remaining entries and zero out the removed stuff.
1751 */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001752 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001753 be16_add_cpu(&hdr->usedbytes, -entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 xfs_da_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001755 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 entsize));
1757
Nathan Scott918ae422006-03-17 17:28:54 +11001758 tmp = (be16_to_cpu(hdr->count) - args->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 * sizeof(xfs_attr_leaf_entry_t);
1760 memmove((char *)entry, (char *)(entry+1), tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001761 be16_add_cpu(&hdr->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 xfs_da_log_buf(args->trans, bp,
1763 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
Nathan Scott918ae422006-03-17 17:28:54 +11001764 entry = &leaf->entries[be16_to_cpu(hdr->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1766
1767 /*
1768 * If we removed the first entry, re-find the first used byte
1769 * in the name area. Note that if the entry was the "firstused",
1770 * then we don't have a "hole" in our block resulting from
1771 * removing the name.
1772 */
1773 if (smallest) {
1774 tmp = XFS_LBSIZE(mp);
1775 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001776 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001777 ASSERT(be16_to_cpu(entry->nameidx) >=
1778 be16_to_cpu(hdr->firstused));
1779 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1780
1781 if (be16_to_cpu(entry->nameidx) < tmp)
1782 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
Nathan Scott918ae422006-03-17 17:28:54 +11001784 hdr->firstused = cpu_to_be16(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001786 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1788 }
1789 } else {
1790 hdr->holes = 1; /* mark as needing compaction */
1791 }
1792 xfs_da_log_buf(args->trans, bp,
1793 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1794
1795 /*
1796 * Check if leaf is less than 50% full, caller may want to
1797 * "join" the leaf with a sibling if so.
1798 */
1799 tmp = sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001800 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1801 tmp += be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1803}
1804
1805/*
1806 * Move all the attribute list entries from drop_leaf into save_leaf.
1807 */
1808void
1809xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1810 xfs_da_state_blk_t *save_blk)
1811{
1812 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1813 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1814 xfs_mount_t *mp;
1815 char *tmpbuffer;
1816
1817 /*
1818 * Set up environment.
1819 */
1820 mp = state->mp;
1821 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1822 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1823 drop_leaf = drop_blk->bp->data;
1824 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001825 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1826 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 drop_hdr = &drop_leaf->hdr;
1828 save_hdr = &save_leaf->hdr;
1829
1830 /*
1831 * Save last hashval from dying block for later Btree fixup.
1832 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001833 drop_blk->hashval = be32_to_cpu(
1834 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 /*
1837 * Check if we need a temp buffer, or can we do it in place.
1838 * Note that we don't check "leaf" for holes because we will
1839 * always be dropping it, toosmall() decided that for us already.
1840 */
1841 if (save_hdr->holes == 0) {
1842 /*
1843 * dest leaf has no holes, so we add there. May need
1844 * to make some room in the entry array.
1845 */
1846 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1847 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001848 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 } else {
1850 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001851 be16_to_cpu(save_hdr->count),
1852 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
1854 } else {
1855 /*
1856 * Destination has holes, so we make a temporary copy
1857 * of the leaf and add them both to that.
1858 */
1859 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1860 ASSERT(tmpbuffer != NULL);
1861 memset(tmpbuffer, 0, state->blocksize);
1862 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1863 tmp_hdr = &tmp_leaf->hdr;
1864 tmp_hdr->info = save_hdr->info; /* struct copy */
1865 tmp_hdr->count = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001866 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (!tmp_hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001868 tmp_hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1870 }
1871 tmp_hdr->usedbytes = 0;
1872 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1873 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001874 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001876 be16_to_cpu(tmp_leaf->hdr.count),
1877 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 } else {
1879 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001880 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001882 be16_to_cpu(tmp_leaf->hdr.count),
1883 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001886 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888
1889 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1890 state->blocksize - 1);
1891
1892 /*
1893 * Copy out last hashval in each block for B-tree code.
1894 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001895 save_blk->hashval = be32_to_cpu(
1896 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
1899/*========================================================================
1900 * Routines used for finding things in the Btree.
1901 *========================================================================*/
1902
1903/*
1904 * Look up a name in a leaf attribute list structure.
1905 * This is the internal routine, it uses the caller's buffer.
1906 *
1907 * Note that duplicate keys are allowed, but only check within the
1908 * current leaf node. The Btree code must check in adjacent leaf nodes.
1909 *
1910 * Return in args->index the index into the entry[] array of either
1911 * the found entry, or where the entry should have been (insert before
1912 * that entry).
1913 *
1914 * Don't change the args->value unless we find the attribute.
1915 */
1916int
1917xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1918{
1919 xfs_attr_leafblock_t *leaf;
1920 xfs_attr_leaf_entry_t *entry;
1921 xfs_attr_leaf_name_local_t *name_loc;
1922 xfs_attr_leaf_name_remote_t *name_rmt;
1923 int probe, span;
1924 xfs_dahash_t hashval;
1925
1926 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001927 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11001928 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 < (XFS_LBSIZE(args->dp->i_mount)/8));
1930
1931 /*
1932 * Binary search. (note: small blocks will skip this loop)
1933 */
1934 hashval = args->hashval;
Nathan Scott918ae422006-03-17 17:28:54 +11001935 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 for (entry = &leaf->entries[probe]; span > 4;
1937 entry = &leaf->entries[probe]) {
1938 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001939 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001941 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 probe -= span;
1943 else
1944 break;
1945 }
Tim Shimmin726801b2006-09-28 11:01:37 +10001946 ASSERT((probe >= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 (!leaf->hdr.count
Nathan Scott918ae422006-03-17 17:28:54 +11001948 || (probe < be16_to_cpu(leaf->hdr.count))));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001949 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /*
1952 * Since we may have duplicate hashval's, find the first matching
1953 * hashval in the leaf.
1954 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001955 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 entry--;
1957 probe--;
1958 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001959 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
1960 (be32_to_cpu(entry->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 entry++;
1962 probe++;
1963 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001964 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
1965 (be32_to_cpu(entry->hashval) != hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 args->index = probe;
1967 return(XFS_ERROR(ENOATTR));
1968 }
1969
1970 /*
1971 * Duplicate keys may be present, so search all of them for a match.
1972 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001973 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
1974 (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 entry++, probe++) {
1976/*
1977 * GROT: Add code to remove incomplete entries.
1978 */
1979 /*
1980 * If we are looking for INCOMPLETE entries, show only those.
1981 * If we are looking for complete entries, show only those.
1982 */
1983 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
1984 (entry->flags & XFS_ATTR_INCOMPLETE)) {
1985 continue;
1986 }
1987 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001988 name_loc = xfs_attr_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (name_loc->namelen != args->namelen)
1990 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10001991 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10001993 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 continue;
1995 args->index = probe;
1996 return(XFS_ERROR(EEXIST));
1997 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001998 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (name_rmt->namelen != args->namelen)
2000 continue;
2001 if (memcmp(args->name, (char *)name_rmt->name,
2002 args->namelen) != 0)
2003 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002004 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 continue;
2006 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002007 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002009 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return(XFS_ERROR(EEXIST));
2011 }
2012 }
2013 args->index = probe;
2014 return(XFS_ERROR(ENOATTR));
2015}
2016
2017/*
2018 * Get the value associated with an attribute name from a leaf attribute
2019 * list structure.
2020 */
2021int
2022xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2023{
2024 int valuelen;
2025 xfs_attr_leafblock_t *leaf;
2026 xfs_attr_leaf_entry_t *entry;
2027 xfs_attr_leaf_name_local_t *name_loc;
2028 xfs_attr_leaf_name_remote_t *name_rmt;
2029
2030 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002031 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002032 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 < (XFS_LBSIZE(args->dp->i_mount)/8));
Nathan Scott918ae422006-03-17 17:28:54 +11002034 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 entry = &leaf->entries[args->index];
2037 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002038 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 ASSERT(name_loc->namelen == args->namelen);
2040 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002041 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (args->flags & ATTR_KERNOVAL) {
2043 args->valuelen = valuelen;
2044 return(0);
2045 }
2046 if (args->valuelen < valuelen) {
2047 args->valuelen = valuelen;
2048 return(XFS_ERROR(ERANGE));
2049 }
2050 args->valuelen = valuelen;
2051 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2052 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002053 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 ASSERT(name_rmt->namelen == args->namelen);
2055 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002056 valuelen = be32_to_cpu(name_rmt->valuelen);
2057 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2059 if (args->flags & ATTR_KERNOVAL) {
2060 args->valuelen = valuelen;
2061 return(0);
2062 }
2063 if (args->valuelen < valuelen) {
2064 args->valuelen = valuelen;
2065 return(XFS_ERROR(ERANGE));
2066 }
2067 args->valuelen = valuelen;
2068 }
2069 return(0);
2070}
2071
2072/*========================================================================
2073 * Utility routines.
2074 *========================================================================*/
2075
2076/*
2077 * Move the indicated entries from one leaf to another.
2078 * NOTE: this routine modifies both source and destination leaves.
2079 */
2080/*ARGSUSED*/
2081STATIC void
2082xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2083 xfs_attr_leafblock_t *leaf_d, int start_d,
2084 int count, xfs_mount_t *mp)
2085{
2086 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2087 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2088 int desti, tmp, i;
2089
2090 /*
2091 * Check for nothing to do.
2092 */
2093 if (count == 0)
2094 return;
2095
2096 /*
2097 * Set up environment.
2098 */
Nathan Scott89da0542006-03-17 17:28:40 +11002099 ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2100 ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 hdr_s = &leaf_s->hdr;
2102 hdr_d = &leaf_d->hdr;
Nathan Scott918ae422006-03-17 17:28:54 +11002103 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2104 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2105 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2106 ((be16_to_cpu(hdr_s->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 * sizeof(*entry_s))+sizeof(*hdr_s)));
Nathan Scott918ae422006-03-17 17:28:54 +11002108 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2109 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2110 ((be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 * sizeof(*entry_d))+sizeof(*hdr_d)));
2112
Nathan Scott918ae422006-03-17 17:28:54 +11002113 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2114 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2115 ASSERT(count <= be16_to_cpu(hdr_s->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 /*
2118 * Move the entries in the destination leaf up to make a hole?
2119 */
Nathan Scott918ae422006-03-17 17:28:54 +11002120 if (start_d < be16_to_cpu(hdr_d->count)) {
2121 tmp = be16_to_cpu(hdr_d->count) - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 tmp *= sizeof(xfs_attr_leaf_entry_t);
2123 entry_s = &leaf_d->entries[start_d];
2124 entry_d = &leaf_d->entries[start_d + count];
2125 memmove((char *)entry_d, (char *)entry_s, tmp);
2126 }
2127
2128 /*
2129 * Copy all entry's in the same (sorted) order,
2130 * but allocate attribute info packed and in sequence.
2131 */
2132 entry_s = &leaf_s->entries[start_s];
2133 entry_d = &leaf_d->entries[start_d];
2134 desti = start_d;
2135 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002136 ASSERT(be16_to_cpu(entry_s->nameidx)
Nathan Scott918ae422006-03-17 17:28:54 +11002137 >= be16_to_cpu(hdr_s->firstused));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2139#ifdef GROT
2140 /*
2141 * Code to drop INCOMPLETE entries. Difficult to use as we
2142 * may also need to change the insertion index. Code turned
2143 * off for 6.2, should be revisited later.
2144 */
2145 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002146 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002147 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2148 be16_add_cpu(&hdr_s->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 entry_d--; /* to compensate for ++ in loop hdr */
2150 desti--;
2151 if ((start_s + i) < offset)
2152 result++; /* insertion index adjustment */
2153 } else {
2154#endif /* GROT */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002155 be16_add_cpu(&hdr_d->firstused, -tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 /* both on-disk, don't endian flip twice */
2157 entry_d->hashval = entry_s->hashval;
2158 /* both on-disk, don't endian flip twice */
2159 entry_d->nameidx = hdr_d->firstused;
2160 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002161 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002163 memmove(xfs_attr_leaf_name(leaf_d, desti),
2164 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002165 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002167 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002168 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2169 be16_add_cpu(&hdr_d->usedbytes, tmp);
2170 be16_add_cpu(&hdr_s->count, -1);
2171 be16_add_cpu(&hdr_d->count, 1);
Nathan Scott918ae422006-03-17 17:28:54 +11002172 tmp = be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 * sizeof(xfs_attr_leaf_entry_t)
2174 + sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002175 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#ifdef GROT
2177 }
2178#endif /* GROT */
2179 }
2180
2181 /*
2182 * Zero out the entries we just copied.
2183 */
Nathan Scott918ae422006-03-17 17:28:54 +11002184 if (start_s == be16_to_cpu(hdr_s->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2186 entry_s = &leaf_s->entries[start_s];
2187 ASSERT(((char *)entry_s + tmp) <=
2188 ((char *)leaf_s + XFS_LBSIZE(mp)));
2189 memset((char *)entry_s, 0, tmp);
2190 } else {
2191 /*
2192 * Move the remaining entries down to fill the hole,
2193 * then zero the entries at the top.
2194 */
Nathan Scott918ae422006-03-17 17:28:54 +11002195 tmp = be16_to_cpu(hdr_s->count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 tmp *= sizeof(xfs_attr_leaf_entry_t);
2197 entry_s = &leaf_s->entries[start_s + count];
2198 entry_d = &leaf_s->entries[start_s];
2199 memmove((char *)entry_d, (char *)entry_s, tmp);
2200
2201 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002202 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 ASSERT(((char *)entry_s + tmp) <=
2204 ((char *)leaf_s + XFS_LBSIZE(mp)));
2205 memset((char *)entry_s, 0, tmp);
2206 }
2207
2208 /*
2209 * Fill in the freemap information
2210 */
Nathan Scott918ae422006-03-17 17:28:54 +11002211 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002212 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
Nathan Scott918ae422006-03-17 17:28:54 +11002213 sizeof(xfs_attr_leaf_entry_t));
2214 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2215 - be16_to_cpu(hdr_d->freemap[0].base));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 hdr_d->freemap[1].base = 0;
2217 hdr_d->freemap[2].base = 0;
2218 hdr_d->freemap[1].size = 0;
2219 hdr_d->freemap[2].size = 0;
2220 hdr_s->holes = 1; /* leaf may not be compact */
2221}
2222
2223/*
2224 * Compare two leaf blocks "order".
2225 * Return 0 unless leaf2 should go before leaf1.
2226 */
2227int
2228xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2229{
2230 xfs_attr_leafblock_t *leaf1, *leaf2;
2231
2232 leaf1 = leaf1_bp->data;
2233 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002234 ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC) &&
2235 (be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002236 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2237 (be16_to_cpu(leaf2->hdr.count) > 0) &&
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002238 ((be32_to_cpu(leaf2->entries[0].hashval) <
2239 be32_to_cpu(leaf1->entries[0].hashval)) ||
2240 (be32_to_cpu(leaf2->entries[
2241 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2242 be32_to_cpu(leaf1->entries[
2243 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return(1);
2245 }
2246 return(0);
2247}
2248
2249/*
2250 * Pick up the last hashvalue from a leaf block.
2251 */
2252xfs_dahash_t
2253xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2254{
2255 xfs_attr_leafblock_t *leaf;
2256
2257 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002258 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (count)
Nathan Scott918ae422006-03-17 17:28:54 +11002260 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (!leaf->hdr.count)
2262 return(0);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002263 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
2266/*
2267 * Calculate the number of bytes used to store the indicated attribute
2268 * (whether local or remote only calculate bytes in this block).
2269 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002270STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2272{
2273 xfs_attr_leaf_name_local_t *name_loc;
2274 xfs_attr_leaf_name_remote_t *name_rmt;
2275 int size;
2276
Nathan Scott89da0542006-03-17 17:28:40 +11002277 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002279 name_loc = xfs_attr_leaf_name_local(leaf, index);
2280 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002281 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002283 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2284 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
2286 return(size);
2287}
2288
2289/*
2290 * Calculate the number of bytes that would be required to store the new
2291 * attribute (whether local or remote only calculate bytes in this block).
2292 * This routine decides as a side effect whether the attribute will be
2293 * a "local" or a "remote" attribute.
2294 */
2295int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002296xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 int size;
2299
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002300 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2301 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (local) {
2303 *local = 1;
2304 }
2305 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002306 size = xfs_attr_leaf_entsize_remote(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (local) {
2308 *local = 0;
2309 }
2310 }
2311 return(size);
2312}
2313
2314/*
2315 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2316 */
2317int
2318xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2319{
2320 attrlist_cursor_kern_t *cursor;
2321 xfs_attr_leafblock_t *leaf;
2322 xfs_attr_leaf_entry_t *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 int retval, i;
2324
2325 ASSERT(bp != NULL);
2326 leaf = bp->data;
2327 cursor = context->cursor;
2328 cursor->initted = 1;
2329
2330 xfs_attr_trace_l_cl("blk start", context, leaf);
2331
2332 /*
2333 * Re-find our place in the leaf block if this is a new syscall.
2334 */
2335 if (context->resynch) {
2336 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002337 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002338 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (cursor->offset == context->dupcnt) {
2340 context->dupcnt = 0;
2341 break;
2342 }
2343 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002344 } else if (be32_to_cpu(entry->hashval) >
2345 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 context->dupcnt = 0;
2347 break;
2348 }
2349 }
Nathan Scott918ae422006-03-17 17:28:54 +11002350 if (i == be16_to_cpu(leaf->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 xfs_attr_trace_l_c("not found", context);
2352 return(0);
2353 }
2354 } else {
2355 entry = &leaf->entries[0];
2356 i = 0;
2357 }
2358 context->resynch = 0;
2359
2360 /*
2361 * We have found our place, start copying out the new attributes.
2362 */
2363 retval = 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10002364 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002365 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2366 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 cursor->offset = 0;
2368 }
2369
2370 if (entry->flags & XFS_ATTR_INCOMPLETE)
2371 continue; /* skip incomplete entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002374 xfs_attr_leaf_name_local_t *name_loc =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002375 xfs_attr_leaf_name_local(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002376
2377 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002378 entry->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +10002379 (char *)name_loc->nameval,
2380 (int)name_loc->namelen,
2381 be16_to_cpu(name_loc->valuelen),
2382 (char *)&name_loc->nameval[name_loc->namelen]);
2383 if (retval)
2384 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002386 xfs_attr_leaf_name_remote_t *name_rmt =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002387 xfs_attr_leaf_name_remote(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002388
2389 int valuelen = be32_to_cpu(name_rmt->valuelen);
2390
2391 if (context->put_value) {
2392 xfs_da_args_t args;
2393
2394 memset((char *)&args, 0, sizeof(args));
2395 args.dp = context->dp;
2396 args.whichfork = XFS_ATTR_FORK;
2397 args.valuelen = valuelen;
2398 args.value = kmem_alloc(valuelen, KM_SLEEP);
2399 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2400 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2401 retval = xfs_attr_rmtval_get(&args);
2402 if (retval)
2403 return retval;
2404 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002405 entry->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +10002406 (char *)name_rmt->name,
2407 (int)name_rmt->namelen,
2408 valuelen,
2409 (char*)args.value);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002410 kmem_free(args.value);
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002411 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002412 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002413 entry->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +10002414 (char *)name_rmt->name,
2415 (int)name_rmt->namelen,
2416 valuelen,
2417 NULL);
2418 }
2419 if (retval)
2420 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002422 if (context->seen_enough)
2423 break;
2424 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
2426 xfs_attr_trace_l_cl("blk end", context, leaf);
2427 return(retval);
2428}
2429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431/*========================================================================
2432 * Manage the INCOMPLETE flag in a leaf entry
2433 *========================================================================*/
2434
2435/*
2436 * Clear the INCOMPLETE flag on an entry in a leaf block.
2437 */
2438int
2439xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2440{
2441 xfs_attr_leafblock_t *leaf;
2442 xfs_attr_leaf_entry_t *entry;
2443 xfs_attr_leaf_name_remote_t *name_rmt;
2444 xfs_dabuf_t *bp;
2445 int error;
2446#ifdef DEBUG
2447 xfs_attr_leaf_name_local_t *name_loc;
2448 int namelen;
2449 char *name;
2450#endif /* DEBUG */
2451
2452 /*
2453 * Set up the operation.
2454 */
2455 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2456 XFS_ATTR_FORK);
2457 if (error) {
2458 return(error);
2459 }
2460 ASSERT(bp != NULL);
2461
2462 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002463 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002464 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 ASSERT(args->index >= 0);
2466 entry = &leaf->entries[ args->index ];
2467 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2468
2469#ifdef DEBUG
2470 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002471 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 namelen = name_loc->namelen;
2473 name = (char *)name_loc->nameval;
2474 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002475 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 namelen = name_rmt->namelen;
2477 name = (char *)name_rmt->name;
2478 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002479 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 ASSERT(namelen == args->namelen);
2481 ASSERT(memcmp(name, args->name, namelen) == 0);
2482#endif /* DEBUG */
2483
2484 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2485 xfs_da_log_buf(args->trans, bp,
2486 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2487
2488 if (args->rmtblkno) {
2489 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002490 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002491 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2492 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 xfs_da_log_buf(args->trans, bp,
2494 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2495 }
2496 xfs_da_buf_done(bp);
2497
2498 /*
2499 * Commit the flag value change and start the next trans in series.
2500 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002501 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
2504/*
2505 * Set the INCOMPLETE flag on an entry in a leaf block.
2506 */
2507int
2508xfs_attr_leaf_setflag(xfs_da_args_t *args)
2509{
2510 xfs_attr_leafblock_t *leaf;
2511 xfs_attr_leaf_entry_t *entry;
2512 xfs_attr_leaf_name_remote_t *name_rmt;
2513 xfs_dabuf_t *bp;
2514 int error;
2515
2516 /*
2517 * Set up the operation.
2518 */
2519 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2520 XFS_ATTR_FORK);
2521 if (error) {
2522 return(error);
2523 }
2524 ASSERT(bp != NULL);
2525
2526 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002527 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002528 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 ASSERT(args->index >= 0);
2530 entry = &leaf->entries[ args->index ];
2531
2532 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2533 entry->flags |= XFS_ATTR_INCOMPLETE;
2534 xfs_da_log_buf(args->trans, bp,
2535 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2536 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002537 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 name_rmt->valueblk = 0;
2539 name_rmt->valuelen = 0;
2540 xfs_da_log_buf(args->trans, bp,
2541 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2542 }
2543 xfs_da_buf_done(bp);
2544
2545 /*
2546 * Commit the flag value change and start the next trans in series.
2547 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002548 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
2551/*
2552 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2553 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2554 * entry given by args->blkno2/index2.
2555 *
2556 * Note that they could be in different blocks, or in the same block.
2557 */
2558int
2559xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2560{
2561 xfs_attr_leafblock_t *leaf1, *leaf2;
2562 xfs_attr_leaf_entry_t *entry1, *entry2;
2563 xfs_attr_leaf_name_remote_t *name_rmt;
2564 xfs_dabuf_t *bp1, *bp2;
2565 int error;
2566#ifdef DEBUG
2567 xfs_attr_leaf_name_local_t *name_loc;
2568 int namelen1, namelen2;
2569 char *name1, *name2;
2570#endif /* DEBUG */
2571
2572 /*
2573 * Read the block containing the "old" attr
2574 */
2575 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2576 XFS_ATTR_FORK);
2577 if (error) {
2578 return(error);
2579 }
2580 ASSERT(bp1 != NULL);
2581
2582 /*
2583 * Read the block containing the "new" attr, if it is different
2584 */
2585 if (args->blkno2 != args->blkno) {
2586 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2587 -1, &bp2, XFS_ATTR_FORK);
2588 if (error) {
2589 return(error);
2590 }
2591 ASSERT(bp2 != NULL);
2592 } else {
2593 bp2 = bp1;
2594 }
2595
2596 leaf1 = bp1->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002597 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002598 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 ASSERT(args->index >= 0);
2600 entry1 = &leaf1->entries[ args->index ];
2601
2602 leaf2 = bp2->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002603 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002604 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 ASSERT(args->index2 >= 0);
2606 entry2 = &leaf2->entries[ args->index2 ];
2607
2608#ifdef DEBUG
2609 if (entry1->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002610 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 namelen1 = name_loc->namelen;
2612 name1 = (char *)name_loc->nameval;
2613 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002614 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 namelen1 = name_rmt->namelen;
2616 name1 = (char *)name_rmt->name;
2617 }
2618 if (entry2->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002619 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 namelen2 = name_loc->namelen;
2621 name2 = (char *)name_loc->nameval;
2622 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002623 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 namelen2 = name_rmt->namelen;
2625 name2 = (char *)name_rmt->name;
2626 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002627 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 ASSERT(namelen1 == namelen2);
2629 ASSERT(memcmp(name1, name2, namelen1) == 0);
2630#endif /* DEBUG */
2631
2632 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2633 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2634
2635 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2636 xfs_da_log_buf(args->trans, bp1,
2637 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2638 if (args->rmtblkno) {
2639 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002640 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002641 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2642 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 xfs_da_log_buf(args->trans, bp1,
2644 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2645 }
2646
2647 entry2->flags |= XFS_ATTR_INCOMPLETE;
2648 xfs_da_log_buf(args->trans, bp2,
2649 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2650 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002651 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 name_rmt->valueblk = 0;
2653 name_rmt->valuelen = 0;
2654 xfs_da_log_buf(args->trans, bp2,
2655 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2656 }
2657 xfs_da_buf_done(bp1);
2658 if (bp1 != bp2)
2659 xfs_da_buf_done(bp2);
2660
2661 /*
2662 * Commit the flag value change and start the next trans in series.
2663 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002664 error = xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 return(error);
2667}
2668
2669/*========================================================================
2670 * Indiscriminately delete the entire attribute fork
2671 *========================================================================*/
2672
2673/*
2674 * Recurse (gasp!) through the attribute nodes until we find leaves.
2675 * We're doing a depth-first traversal in order to invalidate everything.
2676 */
2677int
2678xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2679{
2680 xfs_da_blkinfo_t *info;
2681 xfs_daddr_t blkno;
2682 xfs_dabuf_t *bp;
2683 int error;
2684
2685 /*
2686 * Read block 0 to see what we have to work with.
2687 * We only get here if we have extents, since we remove
2688 * the extents in reverse order the extent containing
2689 * block 0 must still be there.
2690 */
2691 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2692 if (error)
2693 return(error);
2694 blkno = xfs_da_blkno(bp);
2695
2696 /*
2697 * Invalidate the tree, even if the "tree" is only a single leaf block.
2698 * This is a depth-first traversal!
2699 */
2700 info = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002701 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 error = xfs_attr_node_inactive(trans, dp, bp, 1);
Nathan Scott89da0542006-03-17 17:28:40 +11002703 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 error = xfs_attr_leaf_inactive(trans, dp, bp);
2705 } else {
2706 error = XFS_ERROR(EIO);
2707 xfs_da_brelse(*trans, bp);
2708 }
2709 if (error)
2710 return(error);
2711
2712 /*
2713 * Invalidate the incore copy of the root block.
2714 */
2715 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2716 if (error)
2717 return(error);
2718 xfs_da_binval(*trans, bp); /* remove from cache */
2719 /*
2720 * Commit the invalidate and start the next transaction.
2721 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002722 error = xfs_trans_roll(trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 return (error);
2725}
2726
2727/*
2728 * Recurse (gasp!) through the attribute nodes until we find leaves.
2729 * We're doing a depth-first traversal in order to invalidate everything.
2730 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002731STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2733 int level)
2734{
2735 xfs_da_blkinfo_t *info;
2736 xfs_da_intnode_t *node;
2737 xfs_dablk_t child_fsb;
2738 xfs_daddr_t parent_blkno, child_blkno;
2739 int error, count, i;
2740 xfs_dabuf_t *child_bp;
2741
2742 /*
2743 * Since this code is recursive (gasp!) we must protect ourselves.
2744 */
2745 if (level > XFS_DA_NODE_MAXDEPTH) {
2746 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2747 return(XFS_ERROR(EIO));
2748 }
2749
2750 node = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002751 ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
Nathan Scottfac80cc2006-03-17 17:29:56 +11002753 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (!count) {
2755 xfs_da_brelse(*trans, bp);
2756 return(0);
2757 }
Nathan Scott403432d2006-03-17 17:29:46 +11002758 child_fsb = be32_to_cpu(node->btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2760
2761 /*
2762 * If this is the node level just above the leaves, simply loop
2763 * over the leaves removing all of them. If this is higher up
2764 * in the tree, recurse downward.
2765 */
2766 for (i = 0; i < count; i++) {
2767 /*
2768 * Read the subsidiary block to see what we have to work with.
2769 * Don't do this in a transaction. This is a depth-first
2770 * traversal of the tree so we may deal with many blocks
2771 * before we come back to this one.
2772 */
2773 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2774 XFS_ATTR_FORK);
2775 if (error)
2776 return(error);
2777 if (child_bp) {
2778 /* save for re-read later */
2779 child_blkno = xfs_da_blkno(child_bp);
2780
2781 /*
2782 * Invalidate the subtree, however we have to.
2783 */
2784 info = child_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002785 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 error = xfs_attr_node_inactive(trans, dp,
2787 child_bp, level+1);
Nathan Scott89da0542006-03-17 17:28:40 +11002788 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 error = xfs_attr_leaf_inactive(trans, dp,
2790 child_bp);
2791 } else {
2792 error = XFS_ERROR(EIO);
2793 xfs_da_brelse(*trans, child_bp);
2794 }
2795 if (error)
2796 return(error);
2797
2798 /*
2799 * Remove the subsidiary block from the cache
2800 * and from the log.
2801 */
2802 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2803 &child_bp, XFS_ATTR_FORK);
2804 if (error)
2805 return(error);
2806 xfs_da_binval(*trans, child_bp);
2807 }
2808
2809 /*
2810 * If we're not done, re-read the parent to get the next
2811 * child block number.
2812 */
2813 if ((i+1) < count) {
2814 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2815 &bp, XFS_ATTR_FORK);
2816 if (error)
2817 return(error);
Nathan Scott403432d2006-03-17 17:29:46 +11002818 child_fsb = be32_to_cpu(node->btree[i+1].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 xfs_da_brelse(*trans, bp);
2820 }
2821 /*
2822 * Atomically commit the whole invalidate stuff.
2823 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002824 error = xfs_trans_roll(trans, dp);
2825 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return (error);
2827 }
2828
2829 return(0);
2830}
2831
2832/*
2833 * Invalidate all of the "remote" value regions pointed to by a particular
2834 * leaf block.
2835 * Note that we must release the lock on the buffer so that we are not
2836 * caught holding something that the logging code wants to flush to disk.
2837 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002838STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2840{
2841 xfs_attr_leafblock_t *leaf;
2842 xfs_attr_leaf_entry_t *entry;
2843 xfs_attr_leaf_name_remote_t *name_rmt;
2844 xfs_attr_inactive_list_t *list, *lp;
2845 int error, count, size, tmp, i;
2846
2847 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002848 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /*
2851 * Count the number of "remote" value extents.
2852 */
2853 count = 0;
2854 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002855 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002856 if (be16_to_cpu(entry->nameidx) &&
2857 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002858 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (name_rmt->valueblk)
2860 count++;
2861 }
2862 }
2863
2864 /*
2865 * If there are no "remote" values, we're done.
2866 */
2867 if (count == 0) {
2868 xfs_da_brelse(*trans, bp);
2869 return(0);
2870 }
2871
2872 /*
2873 * Allocate storage for a list of all the "remote" value extents.
2874 */
2875 size = count * sizeof(xfs_attr_inactive_list_t);
2876 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2877
2878 /*
2879 * Identify each of the "remote" value extents.
2880 */
2881 lp = list;
2882 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002883 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002884 if (be16_to_cpu(entry->nameidx) &&
2885 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002886 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11002888 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2889 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2890 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 lp++;
2892 }
2893 }
2894 }
2895 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
2896
2897 /*
2898 * Invalidate each of the "remote" value extents.
2899 */
2900 error = 0;
2901 for (lp = list, i = 0; i < count; i++, lp++) {
2902 tmp = xfs_attr_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11002903 lp->valueblk, lp->valuelen);
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (error == 0)
2906 error = tmp; /* save only the 1st errno */
2907 }
2908
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002909 kmem_free((xfs_caddr_t)list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return(error);
2911}
2912
2913/*
2914 * Look at all the extents for this logical region,
2915 * invalidate any buffers that are incore/in transactions.
2916 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002917STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2919 xfs_dablk_t blkno, int blkcnt)
2920{
2921 xfs_bmbt_irec_t map;
2922 xfs_dablk_t tblkno;
2923 int tblkcnt, dblkcnt, nmap, error;
2924 xfs_daddr_t dblkno;
2925 xfs_buf_t *bp;
2926
2927 /*
2928 * Roll through the "value", invalidating the attribute value's
2929 * blocks.
2930 */
2931 tblkno = blkno;
2932 tblkcnt = blkcnt;
2933 while (tblkcnt > 0) {
2934 /*
2935 * Try to remember where we decided to put the value.
2936 */
2937 nmap = 1;
2938 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
2939 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002940 NULL, 0, &map, &nmap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if (error) {
2942 return(error);
2943 }
2944 ASSERT(nmap == 1);
2945 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
2946
2947 /*
2948 * If it's a hole, these are already unmapped
2949 * so there's nothing to invalidate.
2950 */
2951 if (map.br_startblock != HOLESTARTBLOCK) {
2952
2953 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
2954 map.br_startblock);
2955 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
2956 map.br_blockcount);
2957 bp = xfs_trans_get_buf(*trans,
2958 dp->i_mount->m_ddev_targp,
2959 dblkno, dblkcnt, XFS_BUF_LOCK);
2960 xfs_trans_binval(*trans, bp);
2961 /*
2962 * Roll to next transaction.
2963 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002964 error = xfs_trans_roll(trans, dp);
2965 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return (error);
2967 }
2968
2969 tblkno += map.br_blockcount;
2970 tblkcnt -= map.br_blockcount;
2971 }
2972
2973 return(0);
2974}