blob: a90ce74fc256904837381f61e49c0e569818e89f [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"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * xfs_attr_leaf.c
49 *
50 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
51 */
52
53/*========================================================================
54 * Function prototypes for the kernel.
55 *========================================================================*/
56
57/*
58 * Routines used for growing the Btree.
59 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100060STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
61 xfs_dabuf_t **bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
63 int freemap_index);
64STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
65STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
66 xfs_da_state_blk_t *blk1,
67 xfs_da_state_blk_t *blk2);
68STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
69 xfs_da_state_blk_t *leaf_blk_1,
70 xfs_da_state_blk_t *leaf_blk_2,
71 int *number_entries_in_blk1,
72 int *number_usedbytes_in_blk1);
73
74/*
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100075 * Routines used for shrinking the Btree.
76 */
77STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
78 xfs_dabuf_t *bp, int level);
79STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
80 xfs_dabuf_t *bp);
81STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
82 xfs_dablk_t blkno, int blkcnt);
83
84/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Utility routines.
86 */
87STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
88 int src_start,
89 xfs_attr_leafblock_t *dst_leaf,
90 int dst_start, int move_count,
91 xfs_mount_t *mp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100092STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
Tim Shimmin726801b2006-09-28 11:01:37 +100093
94/*========================================================================
95 * Namespace helper routines
96 *========================================================================*/
97
Tim Shimmin726801b2006-09-28 11:01:37 +100098/*
99 * If namespace bits don't match return 0.
100 * If all match then return 1.
101 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000102STATIC int
Tim Shimmin726801b2006-09-28 11:01:37 +1000103xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
104{
105 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100110 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *========================================================================*/
112
113/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100114 * Query whether the requested number of additional bytes of extended
115 * attribute space will be able to fit inline.
116 * Returns zero if not, else the di_forkoff fork offset to be used in the
117 * literal area for attribute data once the new bytes have been added.
118 *
119 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
120 * special case for dev/uuid inodes, they have fixed size data forks.
121 */
122int
123xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
124{
125 int offset;
126 int minforkoff; /* lower limit on valid forkoff locations */
127 int maxforkoff; /* upper limit on valid forkoff locations */
Barry Naujoke5889e92007-02-10 18:35:58 +1100128 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100129 xfs_mount_t *mp = dp->i_mount;
130
Nathan Scottd8cc8902005-11-02 10:34:53 +1100131 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
132
133 switch (dp->i_d.di_format) {
134 case XFS_DINODE_FMT_DEV:
135 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
136 return (offset >= minforkoff) ? minforkoff : 0;
137 case XFS_DINODE_FMT_UUID:
138 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
139 return (offset >= minforkoff) ? minforkoff : 0;
140 }
141
Nathan Scott13059ff2006-01-11 15:32:01 +1100142 if (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +1100143 if (bytes <= XFS_IFORK_ASIZE(dp))
Eric Sandeencb49dbb2008-04-17 16:50:09 +1000144 return dp->i_d.di_forkoff;
Nathan Scottda087ba2005-11-02 15:00:20 +1100145 return 0;
146 }
147
Barry Naujoke5889e92007-02-10 18:35:58 +1100148 dsize = dp->i_df.if_bytes;
149
150 switch (dp->i_d.di_format) {
151 case XFS_DINODE_FMT_EXTENTS:
152 /*
153 * If there is no attr fork and the data fork is extents,
154 * determine if creating the default attr fork will result
155 * in the extents form migrating to btree. If so, the
156 * minimum offset only needs to be the space required for
157 * the btree root.
158 */
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200159 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
160 xfs_default_attroffset(dp))
Barry Naujoke5889e92007-02-10 18:35:58 +1100161 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
162 break;
163
164 case XFS_DINODE_FMT_BTREE:
165 /*
166 * If have data btree then keep forkoff if we have one,
167 * otherwise we are adding a new attr, so then we set
168 * minforkoff to where the btree root can finish so we have
169 * plenty of room for attrs
170 */
171 if (dp->i_d.di_forkoff) {
172 if (offset < dp->i_d.di_forkoff)
173 return 0;
174 else
175 return dp->i_d.di_forkoff;
176 } else
177 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
178 break;
179 }
180
181 /*
182 * A data fork btree root must have space for at least
183 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
184 */
185 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100186 minforkoff = roundup(minforkoff, 8) >> 3;
187
188 /* attr fork btree root can have at least this many key/ptr pairs */
189 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
190 maxforkoff = maxforkoff >> 3; /* rounded down */
191
192 if (offset >= minforkoff && offset < maxforkoff)
193 return offset;
194 if (offset >= maxforkoff)
195 return maxforkoff;
196 return 0;
197}
198
199/*
200 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
201 */
202STATIC void
203xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
204{
Nathan Scott13059ff2006-01-11 15:32:01 +1100205 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100206 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000207 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100208 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
209 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000210 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100211 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
212 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000213 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100214 }
215}
216
217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Create the initial contents of a shortform attribute list.
219 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100220void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221xfs_attr_shortform_create(xfs_da_args_t *args)
222{
223 xfs_attr_sf_hdr_t *hdr;
224 xfs_inode_t *dp;
225 xfs_ifork_t *ifp;
226
227 dp = args->dp;
228 ASSERT(dp != NULL);
229 ifp = dp->i_afp;
230 ASSERT(ifp != NULL);
231 ASSERT(ifp->if_bytes == 0);
232 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
233 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
234 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
235 ifp->if_flags |= XFS_IFINLINE;
236 } else {
237 ASSERT(ifp->if_flags & XFS_IFINLINE);
238 }
239 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
240 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
241 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100242 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/*
247 * Add a name/value pair to the shortform attribute list.
248 * Overflow from the inode has already been checked for.
249 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100250void
251xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 xfs_attr_shortform_t *sf;
254 xfs_attr_sf_entry_t *sfe;
255 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100256 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xfs_inode_t *dp;
258 xfs_ifork_t *ifp;
259
260 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100261 mp = dp->i_mount;
262 dp->i_d.di_forkoff = forkoff;
263 dp->i_df.if_ext_max =
264 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
265 dp->i_afp->if_ext_max =
266 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 ifp = dp->i_afp;
269 ASSERT(ifp->if_flags & XFS_IFINLINE);
270 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
271 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100272 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100273#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (sfe->namelen != args->namelen)
275 continue;
276 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
277 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000278 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100280 ASSERT(0);
281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
284 offset = (char *)sfe - (char *)sf;
285 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
286 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
287 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
288 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
289
290 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100291 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000292 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 memcpy(sfe->nameval, args->name, args->namelen);
294 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100295 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800296 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
298
Nathan Scottd8cc8902005-11-02 10:34:53 +1100299 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/*
Christoph Hellwige1486de2009-02-04 09:36:00 +0100303 * After the last attribute is removed revert to original inode format,
304 * making all literal area available to the data fork once more.
305 */
306STATIC void
307xfs_attr_fork_reset(
308 struct xfs_inode *ip,
309 struct xfs_trans *tp)
310{
311 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
312 ip->i_d.di_forkoff = 0;
313 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
314
315 ASSERT(ip->i_d.di_anextents == 0);
316 ASSERT(ip->i_afp == NULL);
317
318 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
319 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
320}
321
322/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100323 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 */
325int
326xfs_attr_shortform_remove(xfs_da_args_t *args)
327{
328 xfs_attr_shortform_t *sf;
329 xfs_attr_sf_entry_t *sfe;
330 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100331 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_inode_t *dp;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100335 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 base = sizeof(xfs_attr_sf_hdr_t);
337 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
338 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100339 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100340 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 base += size, i++) {
342 size = XFS_ATTR_SF_ENTSIZE(sfe);
343 if (sfe->namelen != args->namelen)
344 continue;
345 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
346 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000347 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 continue;
349 break;
350 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100351 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return(XFS_ERROR(ENOATTR));
353
Nathan Scottd8cc8902005-11-02 10:34:53 +1100354 /*
355 * Fix up the attribute fork data, covering the hole
356 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100358 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100359 if (end != totsize)
360 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100361 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800362 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100363
364 /*
365 * Fix up the start offset of the attribute fork
366 */
367 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000368 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
Christoph Hellwige1486de2009-02-04 09:36:00 +0100369 (mp->m_flags & XFS_MOUNT_ATTR2) &&
370 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
371 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
372 xfs_attr_fork_reset(dp, args->trans);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100373 } else {
374 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
375 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
376 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000377 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
378 (args->op_flags & XFS_DA_OP_ADDNAME) ||
379 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
380 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100381 dp->i_afp->if_ext_max =
382 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
383 dp->i_df.if_ext_max =
384 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
385 xfs_trans_log_inode(args->trans, dp,
386 XFS_ILOG_CORE | XFS_ILOG_ADATA);
387 }
388
389 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 return(0);
392}
393
394/*
395 * Look up a name in a shortform attribute list structure.
396 */
397/*ARGSUSED*/
398int
399xfs_attr_shortform_lookup(xfs_da_args_t *args)
400{
401 xfs_attr_shortform_t *sf;
402 xfs_attr_sf_entry_t *sfe;
403 int i;
404 xfs_ifork_t *ifp;
405
406 ifp = args->dp->i_afp;
407 ASSERT(ifp->if_flags & XFS_IFINLINE);
408 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
409 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100410 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
412 if (sfe->namelen != args->namelen)
413 continue;
414 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
415 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000416 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 continue;
418 return(XFS_ERROR(EEXIST));
419 }
420 return(XFS_ERROR(ENOATTR));
421}
422
423/*
424 * Look up a name in a shortform attribute list structure.
425 */
426/*ARGSUSED*/
427int
428xfs_attr_shortform_getvalue(xfs_da_args_t *args)
429{
430 xfs_attr_shortform_t *sf;
431 xfs_attr_sf_entry_t *sfe;
432 int i;
433
434 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
435 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
436 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100437 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
439 if (sfe->namelen != args->namelen)
440 continue;
441 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
442 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000443 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 continue;
445 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100446 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return(XFS_ERROR(EEXIST));
448 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100449 if (args->valuelen < sfe->valuelen) {
450 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return(XFS_ERROR(ERANGE));
452 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100453 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 memcpy(args->value, &sfe->nameval[args->namelen],
455 args->valuelen);
456 return(XFS_ERROR(EEXIST));
457 }
458 return(XFS_ERROR(ENOATTR));
459}
460
461/*
462 * Convert from using the shortform to the leaf.
463 */
464int
465xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
466{
467 xfs_inode_t *dp;
468 xfs_attr_shortform_t *sf;
469 xfs_attr_sf_entry_t *sfe;
470 xfs_da_args_t nargs;
471 char *tmpbuffer;
472 int error, i, size;
473 xfs_dablk_t blkno;
474 xfs_dabuf_t *bp;
475 xfs_ifork_t *ifp;
476
477 dp = args->dp;
478 ifp = dp->i_afp;
479 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100480 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 tmpbuffer = kmem_alloc(size, KM_SLEEP);
482 ASSERT(tmpbuffer != NULL);
483 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
484 sf = (xfs_attr_shortform_t *)tmpbuffer;
485
486 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
487 bp = NULL;
488 error = xfs_da_grow_inode(args, &blkno);
489 if (error) {
490 /*
491 * If we hit an IO error middle of the transaction inside
492 * grow_inode(), we may have inconsistent data. Bail out.
493 */
494 if (error == EIO)
495 goto out;
496 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
497 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
498 goto out;
499 }
500
501 ASSERT(blkno == 0);
502 error = xfs_attr_leaf_create(args, blkno, &bp);
503 if (error) {
504 error = xfs_da_shrink_inode(args, 0, bp);
505 bp = NULL;
506 if (error)
507 goto out;
508 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
509 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
510 goto out;
511 }
512
513 memset((char *)&nargs, 0, sizeof(nargs));
514 nargs.dp = dp;
515 nargs.firstblock = args->firstblock;
516 nargs.flist = args->flist;
517 nargs.total = args->total;
518 nargs.whichfork = XFS_ATTR_FORK;
519 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000520 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100523 for (i = 0; i < sf->hdr.count; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100524 nargs.name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 nargs.namelen = sfe->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100526 nargs.value = &sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100527 nargs.valuelen = sfe->valuelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100528 nargs.hashval = xfs_da_hashname(sfe->nameval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000530 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
532 ASSERT(error == ENOATTR);
533 error = xfs_attr_leaf_add(bp, &nargs);
534 ASSERT(error != ENOSPC);
535 if (error)
536 goto out;
537 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
538 }
539 error = 0;
540
541out:
542 if(bp)
543 xfs_da_buf_done(bp);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000544 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return(error);
546}
547
548STATIC int
549xfs_attr_shortform_compare(const void *a, const void *b)
550{
551 xfs_attr_sf_sort_t *sa, *sb;
552
553 sa = (xfs_attr_sf_sort_t *)a;
554 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100555 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100557 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return(1);
559 } else {
560 return(sa->entno - sb->entno);
561 }
562}
563
Tim Shimmin726801b2006-09-28 11:01:37 +1000564
565#define XFS_ISRESET_CURSOR(cursor) \
566 (!((cursor)->initted) && !((cursor)->hashval) && \
567 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/*
569 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000570 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * If the output buffer is not large enough to hold them all, then we
572 * we have to calculate each entries' hashvalue and sort them before
573 * we can begin returning them to the user.
574 */
575/*ARGSUSED*/
576int
577xfs_attr_shortform_list(xfs_attr_list_context_t *context)
578{
579 attrlist_cursor_kern_t *cursor;
580 xfs_attr_sf_sort_t *sbuf, *sbp;
581 xfs_attr_shortform_t *sf;
582 xfs_attr_sf_entry_t *sfe;
583 xfs_inode_t *dp;
584 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000585 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 ASSERT(context != NULL);
588 dp = context->dp;
589 ASSERT(dp != NULL);
590 ASSERT(dp->i_afp != NULL);
591 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
592 ASSERT(sf != NULL);
593 if (!sf->hdr.count)
594 return(0);
595 cursor = context->cursor;
596 ASSERT(cursor != NULL);
597
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000598 trace_xfs_attr_list_sf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000601 * If the buffer is large enough and the cursor is at the start,
602 * do not bother with sorting since we will return everything in
603 * one buffer and another call using the cursor won't need to be
604 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000606 * If bufsize is zero then put_listent must be a search function
607 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000609 if (context->bufsize == 0 ||
610 (XFS_ISRESET_CURSOR(cursor) &&
611 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100612 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000613 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000614 sfe->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100615 sfe->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +1000616 (int)sfe->namelen,
617 (int)sfe->valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100618 &sfe->nameval[sfe->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000619
620 /*
621 * Either search callback finished early or
622 * didn't fit it all in the buffer after all.
623 */
624 if (context->seen_enough)
625 break;
626
627 if (error)
628 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
630 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000631 trace_xfs_attr_list_sf_all(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return(0);
633 }
634
Tim Shimmin726801b2006-09-28 11:01:37 +1000635 /* do no more for a search callback */
636 if (context->bufsize == 0)
637 return 0;
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /*
640 * It didn't all fit, so we have to sort everything on hashval.
641 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100642 sbsize = sf->hdr.count * sizeof(*sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
644
645 /*
646 * Scan the attribute list for the rest of the entries, storing
647 * the relevant info from only those that match into a buffer.
648 */
649 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100650 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (unlikely(
652 ((char *)sfe < (char *)sf) ||
653 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
654 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
655 XFS_ERRLEVEL_LOW,
656 context->dp->i_mount, sfe);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000657 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return XFS_ERROR(EFSCORRUPTED);
659 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 sbp->entno = i;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100662 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
663 sbp->name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 sbp->namelen = sfe->namelen;
665 /* These are bytes, and both on-disk, don't endian-flip */
666 sbp->valuelen = sfe->valuelen;
667 sbp->flags = sfe->flags;
668 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
669 sbp++;
670 nsbuf++;
671 }
672
673 /*
674 * Sort the entries on hash then entno.
675 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100676 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /*
679 * Re-find our place IN THE SORTED LIST.
680 */
681 count = 0;
682 cursor->initted = 1;
683 cursor->blkno = 0;
684 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100685 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (cursor->offset == count) {
687 break;
688 }
689 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100690 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 break;
692 }
693 }
694 if (i == nsbuf) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000695 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return(0);
697 }
698
699 /*
700 * Loop putting entries into the user buffer.
701 */
702 for ( ; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100703 if (cursor->hashval != sbp->hash) {
704 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 cursor->offset = 0;
706 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000707 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000708 sbp->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000709 sbp->name,
710 sbp->namelen,
711 sbp->valuelen,
712 &sbp->name[sbp->namelen]);
713 if (error)
714 return error;
715 if (context->seen_enough)
716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 cursor->offset++;
718 }
719
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000720 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return(0);
722}
723
724/*
725 * Check a leaf attribute block to see if all the entries would fit into
726 * a shortform attribute list.
727 */
728int
729xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
730{
731 xfs_attr_leafblock_t *leaf;
732 xfs_attr_leaf_entry_t *entry;
733 xfs_attr_leaf_name_local_t *name_loc;
734 int bytes, i;
735
736 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100737 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 entry = &leaf->entries[0];
740 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100741 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (entry->flags & XFS_ATTR_INCOMPLETE)
743 continue; /* don't copy partial entries */
744 if (!(entry->flags & XFS_ATTR_LOCAL))
745 return(0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600746 name_loc = xfs_attr_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
748 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100749 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return(0);
751 bytes += sizeof(struct xfs_attr_sf_entry)-1
752 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100753 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100755 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100756 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100757 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100758 return(-1);
759 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762/*
763 * Convert a leaf attribute list to shortform attribute list
764 */
765int
Nathan Scottd8cc8902005-11-02 10:34:53 +1100766xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 xfs_attr_leafblock_t *leaf;
769 xfs_attr_leaf_entry_t *entry;
770 xfs_attr_leaf_name_local_t *name_loc;
771 xfs_da_args_t nargs;
772 xfs_inode_t *dp;
773 char *tmpbuffer;
774 int error, i;
775
776 dp = args->dp;
777 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
778 ASSERT(tmpbuffer != NULL);
779
780 ASSERT(bp != NULL);
781 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
782 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Nathan Scott89da0542006-03-17 17:28:40 +1100783 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
785
786 /*
787 * Clean out the prior contents of the attribute list.
788 */
789 error = xfs_da_shrink_inode(args, 0, bp);
790 if (error)
791 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100792
793 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +1100794 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +1100795 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Christoph Hellwige1486de2009-02-04 09:36:00 +0100796 xfs_attr_fork_reset(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100798 }
799
800 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /*
803 * Copy the attributes
804 */
805 memset((char *)&nargs, 0, sizeof(nargs));
806 nargs.dp = dp;
807 nargs.firstblock = args->firstblock;
808 nargs.flist = args->flist;
809 nargs.total = args->total;
810 nargs.whichfork = XFS_ATTR_FORK;
811 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000812 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +1100814 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (entry->flags & XFS_ATTR_INCOMPLETE)
816 continue; /* don't copy partial entries */
817 if (!entry->nameidx)
818 continue;
819 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600820 name_loc = xfs_attr_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +1100821 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100823 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +1100824 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100825 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +1000826 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100827 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 error = 0;
830
831out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000832 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return(error);
834}
835
836/*
837 * Convert from using a single leaf to a root node and a leaf.
838 */
839int
840xfs_attr_leaf_to_node(xfs_da_args_t *args)
841{
842 xfs_attr_leafblock_t *leaf;
843 xfs_da_intnode_t *node;
844 xfs_inode_t *dp;
845 xfs_dabuf_t *bp1, *bp2;
846 xfs_dablk_t blkno;
847 int error;
848
849 dp = args->dp;
850 bp1 = bp2 = NULL;
851 error = xfs_da_grow_inode(args, &blkno);
852 if (error)
853 goto out;
854 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
855 XFS_ATTR_FORK);
856 if (error)
857 goto out;
858 ASSERT(bp1 != NULL);
859 bp2 = NULL;
860 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
861 XFS_ATTR_FORK);
862 if (error)
863 goto out;
864 ASSERT(bp2 != NULL);
865 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
866 xfs_da_buf_done(bp1);
867 bp1 = NULL;
868 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
869
870 /*
871 * Set up the new root node.
872 */
873 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
874 if (error)
875 goto out;
876 node = bp1->data;
877 leaf = bp2->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100878 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* both on-disk, don't endian-flip twice */
880 node->btree[0].hashval =
Nathan Scott918ae422006-03-17 17:28:54 +1100881 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
Nathan Scott403432d2006-03-17 17:29:46 +1100882 node->btree[0].before = cpu_to_be32(blkno);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100883 node->hdr.count = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
885 error = 0;
886out:
887 if (bp1)
888 xfs_da_buf_done(bp1);
889 if (bp2)
890 xfs_da_buf_done(bp2);
891 return(error);
892}
893
894
895/*========================================================================
896 * Routines used for growing the Btree.
897 *========================================================================*/
898
899/*
900 * Create the initial contents of a leaf attribute list
901 * or a leaf in a node attribute list.
902 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000903STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
905{
906 xfs_attr_leafblock_t *leaf;
907 xfs_attr_leaf_hdr_t *hdr;
908 xfs_inode_t *dp;
909 xfs_dabuf_t *bp;
910 int error;
911
912 dp = args->dp;
913 ASSERT(dp != NULL);
914 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
915 XFS_ATTR_FORK);
916 if (error)
917 return(error);
918 ASSERT(bp != NULL);
919 leaf = bp->data;
920 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
921 hdr = &leaf->hdr;
Nathan Scott89da0542006-03-17 17:28:40 +1100922 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +1100923 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +1100925 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
927 }
928
Nathan Scott918ae422006-03-17 17:28:54 +1100929 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
930 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
931 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
934
935 *bpp = bp;
936 return(0);
937}
938
939/*
940 * Split the leaf node, rebalance, then add the new entry.
941 */
942int
943xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
944 xfs_da_state_blk_t *newblk)
945{
946 xfs_dablk_t blkno;
947 int error;
948
949 /*
950 * Allocate space for a new leaf node.
951 */
952 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
953 error = xfs_da_grow_inode(state->args, &blkno);
954 if (error)
955 return(error);
956 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
957 if (error)
958 return(error);
959 newblk->blkno = blkno;
960 newblk->magic = XFS_ATTR_LEAF_MAGIC;
961
962 /*
963 * Rebalance the entries across the two leaves.
964 * NOTE: rebalance() currently depends on the 2nd block being empty.
965 */
966 xfs_attr_leaf_rebalance(state, oldblk, newblk);
967 error = xfs_da_blk_link(state, oldblk, newblk);
968 if (error)
969 return(error);
970
971 /*
972 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
973 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
974 * "new" attrs info. Will need the "old" info to remove it later.
975 *
976 * Insert the "new" entry in the correct block.
977 */
978 if (state->inleaf)
979 error = xfs_attr_leaf_add(oldblk->bp, state->args);
980 else
981 error = xfs_attr_leaf_add(newblk->bp, state->args);
982
983 /*
984 * Update last hashval in each block since we added the name.
985 */
986 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
987 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
988 return(error);
989}
990
991/*
992 * Add a name to the leaf attribute list structure.
993 */
994int
995xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
996{
997 xfs_attr_leafblock_t *leaf;
998 xfs_attr_leaf_hdr_t *hdr;
999 xfs_attr_leaf_map_t *map;
1000 int tablesize, entsize, sum, tmp, i;
1001
1002 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001003 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001005 && (args->index <= be16_to_cpu(leaf->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 hdr = &leaf->hdr;
Nathan Scottaa82daa2005-11-02 10:33:33 +11001007 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1009
1010 /*
1011 * Search through freemap for first-fit on new name length.
1012 * (may need to figure in size of entry struct too)
1013 */
Nathan Scott918ae422006-03-17 17:28:54 +11001014 tablesize = (be16_to_cpu(hdr->count) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 * sizeof(xfs_attr_leaf_entry_t)
1016 + sizeof(xfs_attr_leaf_hdr_t);
1017 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1018 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
Nathan Scott918ae422006-03-17 17:28:54 +11001019 if (tablesize > be16_to_cpu(hdr->firstused)) {
1020 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 continue;
1022 }
1023 if (!map->size)
1024 continue; /* no space in this map */
1025 tmp = entsize;
Nathan Scott918ae422006-03-17 17:28:54 +11001026 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 tmp += sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001028 if (be16_to_cpu(map->size) >= tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 tmp = xfs_attr_leaf_add_work(bp, args, i);
1030 return(tmp);
1031 }
Nathan Scott918ae422006-03-17 17:28:54 +11001032 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034
1035 /*
1036 * If there are no holes in the address space of the block,
1037 * and we don't have enough freespace, then compaction will do us
1038 * no good and we should just give up.
1039 */
1040 if (!hdr->holes && (sum < entsize))
1041 return(XFS_ERROR(ENOSPC));
1042
1043 /*
1044 * Compact the entries to coalesce free space.
1045 * This may change the hdr->count via dropping INCOMPLETE entries.
1046 */
1047 xfs_attr_leaf_compact(args->trans, bp);
1048
1049 /*
1050 * After compaction, the block is guaranteed to have only one
1051 * free region, in freemap[0]. If it is not big enough, give up.
1052 */
Nathan Scott918ae422006-03-17 17:28:54 +11001053 if (be16_to_cpu(hdr->freemap[0].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1055 return(XFS_ERROR(ENOSPC));
1056
1057 return(xfs_attr_leaf_add_work(bp, args, 0));
1058}
1059
1060/*
1061 * Add a name to a leaf attribute list structure.
1062 */
1063STATIC int
1064xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1065{
1066 xfs_attr_leafblock_t *leaf;
1067 xfs_attr_leaf_hdr_t *hdr;
1068 xfs_attr_leaf_entry_t *entry;
1069 xfs_attr_leaf_name_local_t *name_loc;
1070 xfs_attr_leaf_name_remote_t *name_rmt;
1071 xfs_attr_leaf_map_t *map;
1072 xfs_mount_t *mp;
1073 int tmp, i;
1074
1075 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001076 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 hdr = &leaf->hdr;
1078 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
Nathan Scott918ae422006-03-17 17:28:54 +11001079 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /*
1082 * Force open some space in the entry array and fill it in.
1083 */
1084 entry = &leaf->entries[args->index];
Nathan Scott918ae422006-03-17 17:28:54 +11001085 if (args->index < be16_to_cpu(hdr->count)) {
1086 tmp = be16_to_cpu(hdr->count) - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 tmp *= sizeof(xfs_attr_leaf_entry_t);
1088 memmove((char *)(entry+1), (char *)entry, tmp);
1089 xfs_da_log_buf(args->trans, bp,
1090 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1091 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001092 be16_add_cpu(&hdr->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 /*
1095 * Allocate space for the new string (at the end of the run).
1096 */
1097 map = &hdr->freemap[mapindex];
1098 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001099 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1100 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1101 ASSERT(be16_to_cpu(map->size) >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001102 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1103 mp->m_sb.sb_blocksize, NULL));
Nathan Scott918ae422006-03-17 17:28:54 +11001104 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1105 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001106 be16_add_cpu(&map->size,
Nathan Scottaa82daa2005-11-02 10:33:33 +11001107 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1108 mp->m_sb.sb_blocksize, &tmp));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001109 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1110 be16_to_cpu(map->size));
1111 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001113 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001114 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 entry->flags |= XFS_ATTR_INCOMPLETE;
1116 if ((args->blkno2 == args->blkno) &&
1117 (args->index2 <= args->index)) {
1118 args->index2++;
1119 }
1120 }
1121 xfs_da_log_buf(args->trans, bp,
1122 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001123 ASSERT((args->index == 0) ||
1124 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Nathan Scott918ae422006-03-17 17:28:54 +11001125 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001126 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /*
1129 * Copy the attribute name and value into the new space.
1130 *
1131 * For "remote" attribute values, simply note that we need to
1132 * allocate space for the "remote" value. We can't actually
1133 * allocate the extents in this transaction, and we can't decide
1134 * which blocks they should be as we might allocate more blocks
1135 * as part of this transaction (a split operation for example).
1136 */
1137 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001138 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001140 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1142 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001143 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001145 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 name_rmt->namelen = args->namelen;
1147 memcpy((char *)name_rmt->name, args->name, args->namelen);
1148 entry->flags |= XFS_ATTR_INCOMPLETE;
1149 /* just in case */
1150 name_rmt->valuelen = 0;
1151 name_rmt->valueblk = 0;
1152 args->rmtblkno = 1;
1153 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1154 }
1155 xfs_da_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001156 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 xfs_attr_leaf_entsize(leaf, args->index)));
1158
1159 /*
1160 * Update the control info for this leaf node
1161 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001162 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /* both on-disk, don't endian-flip twice */
1164 hdr->firstused = entry->nameidx;
1165 }
Nathan Scott918ae422006-03-17 17:28:54 +11001166 ASSERT(be16_to_cpu(hdr->firstused) >=
1167 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1168 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 + sizeof(xfs_attr_leaf_hdr_t);
1170 map = &hdr->freemap[0];
1171 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001172 if (be16_to_cpu(map->base) == tmp) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001173 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1174 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001175 -((int)sizeof(xfs_attr_leaf_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001178 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 xfs_da_log_buf(args->trans, bp,
1180 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1181 return(0);
1182}
1183
1184/*
1185 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1186 */
1187STATIC void
1188xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1189{
1190 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1191 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1192 xfs_mount_t *mp;
1193 char *tmpbuffer;
1194
1195 mp = trans->t_mountp;
1196 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1197 ASSERT(tmpbuffer != NULL);
1198 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1199 memset(bp->data, 0, XFS_LBSIZE(mp));
1200
1201 /*
1202 * Copy basic information
1203 */
1204 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1205 leaf_d = bp->data;
1206 hdr_s = &leaf_s->hdr;
1207 hdr_d = &leaf_d->hdr;
1208 hdr_d->info = hdr_s->info; /* struct copy */
Nathan Scott918ae422006-03-17 17:28:54 +11001209 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /* handle truncation gracefully */
1211 if (!hdr_d->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001212 hdr_d->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1214 }
1215 hdr_d->usedbytes = 0;
1216 hdr_d->count = 0;
1217 hdr_d->holes = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001218 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1219 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1220 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /*
1223 * Copy all entry's in the same (sorted) order,
1224 * but allocate name/value pairs packed and in sequence.
1225 */
1226 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001227 be16_to_cpu(hdr_s->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1229
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001230 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233/*
1234 * Redistribute the attribute list entries between two leaf nodes,
1235 * taking into account the size of the new entry.
1236 *
1237 * NOTE: if new block is empty, then it will get the upper half of the
1238 * old block. At present, all (one) callers pass in an empty second block.
1239 *
1240 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1241 * to match what it is doing in splitting the attribute leaf block. Those
1242 * values are used in "atomic rename" operations on attributes. Note that
1243 * the "new" and "old" values can end up in different blocks.
1244 */
1245STATIC void
1246xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1247 xfs_da_state_blk_t *blk2)
1248{
1249 xfs_da_args_t *args;
1250 xfs_da_state_blk_t *tmp_blk;
1251 xfs_attr_leafblock_t *leaf1, *leaf2;
1252 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1253 int count, totallen, max, space, swap;
1254
1255 /*
1256 * Set up environment.
1257 */
1258 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1259 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1260 leaf1 = blk1->bp->data;
1261 leaf2 = blk2->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001262 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1263 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 args = state->args;
1265
1266 /*
1267 * Check ordering of blocks, reverse if it makes things simpler.
1268 *
1269 * NOTE: Given that all (current) callers pass in an empty
1270 * second block, this code should never set "swap".
1271 */
1272 swap = 0;
1273 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1274 tmp_blk = blk1;
1275 blk1 = blk2;
1276 blk2 = tmp_blk;
1277 leaf1 = blk1->bp->data;
1278 leaf2 = blk2->bp->data;
1279 swap = 1;
1280 }
1281 hdr1 = &leaf1->hdr;
1282 hdr2 = &leaf2->hdr;
1283
1284 /*
1285 * Examine entries until we reduce the absolute difference in
1286 * byte usage between the two blocks to a minimum. Then get
1287 * the direction to copy and the number of elements to move.
1288 *
1289 * "inleaf" is true if the new entry should be inserted into blk1.
1290 * If "swap" is also true, then reverse the sense of "inleaf".
1291 */
1292 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1293 &count, &totallen);
1294 if (swap)
1295 state->inleaf = !state->inleaf;
1296
1297 /*
1298 * Move any entries required from leaf to leaf:
1299 */
Nathan Scott918ae422006-03-17 17:28:54 +11001300 if (count < be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /*
1302 * Figure the total bytes to be added to the destination leaf.
1303 */
1304 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001305 count = be16_to_cpu(hdr1->count) - count;
1306 space = be16_to_cpu(hdr1->usedbytes) - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 space += count * sizeof(xfs_attr_leaf_entry_t);
1308
1309 /*
1310 * leaf2 is the destination, compact it if it looks tight.
1311 */
Nathan Scott918ae422006-03-17 17:28:54 +11001312 max = be16_to_cpu(hdr2->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001314 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (space > max) {
1316 xfs_attr_leaf_compact(args->trans, blk2->bp);
1317 }
1318
1319 /*
1320 * Move high entries from leaf1 to low end of leaf2.
1321 */
Nathan Scott918ae422006-03-17 17:28:54 +11001322 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 leaf2, 0, count, state->mp);
1324
1325 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1326 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Nathan Scott918ae422006-03-17 17:28:54 +11001327 } else if (count > be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /*
1329 * I assert that since all callers pass in an empty
1330 * second buffer, this code should never execute.
1331 */
1332
1333 /*
1334 * Figure the total bytes to be added to the destination leaf.
1335 */
1336 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001337 count -= be16_to_cpu(hdr1->count);
1338 space = totallen - be16_to_cpu(hdr1->usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 space += count * sizeof(xfs_attr_leaf_entry_t);
1340
1341 /*
1342 * leaf1 is the destination, compact it if it looks tight.
1343 */
Nathan Scott918ae422006-03-17 17:28:54 +11001344 max = be16_to_cpu(hdr1->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001346 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (space > max) {
1348 xfs_attr_leaf_compact(args->trans, blk1->bp);
1349 }
1350
1351 /*
1352 * Move low entries from leaf2 to high end of leaf1.
1353 */
1354 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
Nathan Scott918ae422006-03-17 17:28:54 +11001355 be16_to_cpu(hdr1->count), count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1358 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1359 }
1360
1361 /*
1362 * Copy out last hashval in each block for B-tree code.
1363 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001364 blk1->hashval = be32_to_cpu(
1365 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1366 blk2->hashval = be32_to_cpu(
1367 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /*
1370 * Adjust the expected index for insertion.
1371 * NOTE: this code depends on the (current) situation that the
1372 * second block was originally empty.
1373 *
1374 * If the insertion point moved to the 2nd block, we must adjust
1375 * the index. We must also track the entry just following the
1376 * new entry for use in an "atomic rename" operation, that entry
1377 * is always the "old" entry and the "new" entry is what we are
1378 * inserting. The index/blkno fields refer to the "old" entry,
1379 * while the index2/blkno2 fields refer to the "new" entry.
1380 */
Nathan Scott918ae422006-03-17 17:28:54 +11001381 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ASSERT(state->inleaf == 0);
Nathan Scott918ae422006-03-17 17:28:54 +11001383 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 args->index = args->index2 = blk2->index;
1385 args->blkno = args->blkno2 = blk2->blkno;
Nathan Scott918ae422006-03-17 17:28:54 +11001386 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (state->inleaf) {
1388 args->index = blk1->index;
1389 args->blkno = blk1->blkno;
1390 args->index2 = 0;
1391 args->blkno2 = blk2->blkno;
1392 } else {
1393 blk2->index = blk1->index
Nathan Scott918ae422006-03-17 17:28:54 +11001394 - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 args->index = args->index2 = blk2->index;
1396 args->blkno = args->blkno2 = blk2->blkno;
1397 }
1398 } else {
1399 ASSERT(state->inleaf == 1);
1400 args->index = args->index2 = blk1->index;
1401 args->blkno = args->blkno2 = blk1->blkno;
1402 }
1403}
1404
1405/*
1406 * Examine entries until we reduce the absolute difference in
1407 * byte usage between the two blocks to a minimum.
1408 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1409 * GROT: there will always be enough room in either block for a new entry.
1410 * GROT: Do a double-split for this case?
1411 */
1412STATIC int
1413xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1414 xfs_da_state_blk_t *blk1,
1415 xfs_da_state_blk_t *blk2,
1416 int *countarg, int *usedbytesarg)
1417{
1418 xfs_attr_leafblock_t *leaf1, *leaf2;
1419 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1420 xfs_attr_leaf_entry_t *entry;
1421 int count, max, index, totallen, half;
1422 int lastdelta, foundit, tmp;
1423
1424 /*
1425 * Set up environment.
1426 */
1427 leaf1 = blk1->bp->data;
1428 leaf2 = blk2->bp->data;
1429 hdr1 = &leaf1->hdr;
1430 hdr2 = &leaf2->hdr;
1431 foundit = 0;
1432 totallen = 0;
1433
1434 /*
1435 * Examine entries until we reduce the absolute difference in
1436 * byte usage between the two blocks to a minimum.
1437 */
Nathan Scott918ae422006-03-17 17:28:54 +11001438 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 half = (max+1) * sizeof(*entry);
Nathan Scott918ae422006-03-17 17:28:54 +11001440 half += be16_to_cpu(hdr1->usedbytes) +
1441 be16_to_cpu(hdr2->usedbytes) +
1442 xfs_attr_leaf_newentsize(
1443 state->args->namelen,
1444 state->args->valuelen,
1445 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 half /= 2;
1447 lastdelta = state->blocksize;
1448 entry = &leaf1->entries[0];
1449 for (count = index = 0; count < max; entry++, index++, count++) {
1450
1451#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1452 /*
1453 * The new entry is in the first block, account for it.
1454 */
1455 if (count == blk1->index) {
1456 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001457 xfs_attr_leaf_newentsize(
1458 state->args->namelen,
1459 state->args->valuelen,
1460 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1462 break;
1463 lastdelta = XFS_ATTR_ABS(half - tmp);
1464 totallen = tmp;
1465 foundit = 1;
1466 }
1467
1468 /*
1469 * Wrap around into the second block if necessary.
1470 */
Nathan Scott918ae422006-03-17 17:28:54 +11001471 if (count == be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 leaf1 = leaf2;
1473 entry = &leaf1->entries[0];
1474 index = 0;
1475 }
1476
1477 /*
1478 * Figure out if next leaf entry would be too much.
1479 */
1480 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1481 index);
1482 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1483 break;
1484 lastdelta = XFS_ATTR_ABS(half - tmp);
1485 totallen = tmp;
1486#undef XFS_ATTR_ABS
1487 }
1488
1489 /*
1490 * Calculate the number of usedbytes that will end up in lower block.
1491 * If new entry not in lower block, fix up the count.
1492 */
1493 totallen -= count * sizeof(*entry);
1494 if (foundit) {
1495 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001496 xfs_attr_leaf_newentsize(
1497 state->args->namelen,
1498 state->args->valuelen,
1499 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501
1502 *countarg = count;
1503 *usedbytesarg = totallen;
1504 return(foundit);
1505}
1506
1507/*========================================================================
1508 * Routines used for shrinking the Btree.
1509 *========================================================================*/
1510
1511/*
1512 * Check a leaf block and its neighbors to see if the block should be
1513 * collapsed into one or the other neighbor. Always keep the block
1514 * with the smaller block number.
1515 * If the current block is over 50% full, don't try to join it, return 0.
1516 * If the block is empty, fill in the state structure and return 2.
1517 * If it can be collapsed, fill in the state structure and return 1.
1518 * If nothing can be done, return 0.
1519 *
1520 * GROT: allow for INCOMPLETE entries in calculation.
1521 */
1522int
1523xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1524{
1525 xfs_attr_leafblock_t *leaf;
1526 xfs_da_state_blk_t *blk;
1527 xfs_da_blkinfo_t *info;
1528 int count, bytes, forward, error, retval, i;
1529 xfs_dablk_t blkno;
1530 xfs_dabuf_t *bp;
1531
1532 /*
1533 * Check for the degenerate case of the block being over 50% full.
1534 * If so, it's not worth even looking to see if we might be able
1535 * to coalesce with a sibling.
1536 */
1537 blk = &state->path.blk[ state->path.active-1 ];
1538 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001539 ASSERT(be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001541 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1543 count * sizeof(xfs_attr_leaf_entry_t) +
Nathan Scott918ae422006-03-17 17:28:54 +11001544 be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (bytes > (state->blocksize >> 1)) {
1546 *action = 0; /* blk over 50%, don't try to join */
1547 return(0);
1548 }
1549
1550 /*
1551 * Check for the degenerate case of the block being empty.
1552 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001553 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 * to merge with the forward block unless it is NULL.
1555 */
1556 if (count == 0) {
1557 /*
1558 * Make altpath point to the block we want to keep and
1559 * path point to the block we want to drop (this one).
1560 */
Nathan Scott89da0542006-03-17 17:28:40 +11001561 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 memcpy(&state->altpath, &state->path, sizeof(state->path));
1563 error = xfs_da_path_shift(state, &state->altpath, forward,
1564 0, &retval);
1565 if (error)
1566 return(error);
1567 if (retval) {
1568 *action = 0;
1569 } else {
1570 *action = 2;
1571 }
1572 return(0);
1573 }
1574
1575 /*
1576 * Examine each sibling block to see if we can coalesce with
1577 * at least 25% free space to spare. We need to figure out
1578 * whether to merge with the forward or the backward block.
1579 * We prefer coalescing with the lower numbered sibling so as
1580 * to shrink an attribute list over time.
1581 */
1582 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +11001583 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 for (i = 0; i < 2; forward = !forward, i++) {
1585 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +11001586 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 else
Nathan Scott89da0542006-03-17 17:28:40 +11001588 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (blkno == 0)
1590 continue;
1591 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1592 blkno, -1, &bp, XFS_ATTR_FORK);
1593 if (error)
1594 return(error);
1595 ASSERT(bp != NULL);
1596
1597 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001598 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 bytes = state->blocksize - (state->blocksize>>2);
Nathan Scott918ae422006-03-17 17:28:54 +11001600 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001602 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11001603 count += be16_to_cpu(leaf->hdr.count);
1604 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1606 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1607 xfs_da_brelse(state->args->trans, bp);
1608 if (bytes >= 0)
1609 break; /* fits with at least 25% to spare */
1610 }
1611 if (i >= 2) {
1612 *action = 0;
1613 return(0);
1614 }
1615
1616 /*
1617 * Make altpath point to the block we want to keep (the lower
1618 * numbered block) and path point to the block we want to drop.
1619 */
1620 memcpy(&state->altpath, &state->path, sizeof(state->path));
1621 if (blkno < blk->blkno) {
1622 error = xfs_da_path_shift(state, &state->altpath, forward,
1623 0, &retval);
1624 } else {
1625 error = xfs_da_path_shift(state, &state->path, forward,
1626 0, &retval);
1627 }
1628 if (error)
1629 return(error);
1630 if (retval) {
1631 *action = 0;
1632 } else {
1633 *action = 1;
1634 }
1635 return(0);
1636}
1637
1638/*
1639 * Remove a name from the leaf attribute list structure.
1640 *
1641 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1642 * If two leaves are 37% full, when combined they will leave 25% free.
1643 */
1644int
1645xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1646{
1647 xfs_attr_leafblock_t *leaf;
1648 xfs_attr_leaf_hdr_t *hdr;
1649 xfs_attr_leaf_map_t *map;
1650 xfs_attr_leaf_entry_t *entry;
1651 int before, after, smallest, entsize;
1652 int tablesize, tmp, i;
1653 xfs_mount_t *mp;
1654
1655 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001656 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 hdr = &leaf->hdr;
1658 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001659 ASSERT((be16_to_cpu(hdr->count) > 0)
1660 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001662 && (args->index < be16_to_cpu(hdr->count)));
1663 ASSERT(be16_to_cpu(hdr->firstused) >=
1664 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 entry = &leaf->entries[args->index];
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001666 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1667 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 /*
1670 * Scan through free region table:
1671 * check for adjacency of free'd entry with an existing one,
1672 * find smallest free region in case we need to replace it,
1673 * adjust any map that borders the entry table,
1674 */
Nathan Scott918ae422006-03-17 17:28:54 +11001675 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 + sizeof(xfs_attr_leaf_hdr_t);
1677 map = &hdr->freemap[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001678 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 before = after = -1;
1680 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1681 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1682 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001683 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1684 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1685 if (be16_to_cpu(map->base) == tablesize) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001686 be16_add_cpu(&map->base,
Nathan Scott918ae422006-03-17 17:28:54 +11001687 -((int)sizeof(xfs_attr_leaf_entry_t)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001688 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
Nathan Scott918ae422006-03-17 17:28:54 +11001691 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001692 == be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 before = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001694 } else if (be16_to_cpu(map->base)
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001695 == (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 after = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001697 } else if (be16_to_cpu(map->size) < tmp) {
1698 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 smallest = i;
1700 }
1701 }
1702
1703 /*
1704 * Coalesce adjacent freemap regions,
1705 * or replace the smallest region.
1706 */
1707 if ((before >= 0) || (after >= 0)) {
1708 if ((before >= 0) && (after >= 0)) {
1709 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001710 be16_add_cpu(&map->size, entsize);
1711 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001712 be16_to_cpu(hdr->freemap[after].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 hdr->freemap[after].base = 0;
1714 hdr->freemap[after].size = 0;
1715 } else if (before >= 0) {
1716 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001717 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 } else {
1719 map = &hdr->freemap[after];
1720 /* both on-disk, don't endian flip twice */
1721 map->base = entry->nameidx;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001722 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724 } else {
1725 /*
1726 * Replace smallest region (if it is smaller than free'd entry)
1727 */
1728 map = &hdr->freemap[smallest];
Nathan Scott918ae422006-03-17 17:28:54 +11001729 if (be16_to_cpu(map->size) < entsize) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001730 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
Nathan Scott918ae422006-03-17 17:28:54 +11001731 map->size = cpu_to_be16(entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733 }
1734
1735 /*
1736 * Did we remove the first entry?
1737 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001738 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 smallest = 1;
1740 else
1741 smallest = 0;
1742
1743 /*
1744 * Compress the remaining entries and zero out the removed stuff.
1745 */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001746 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001747 be16_add_cpu(&hdr->usedbytes, -entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 xfs_da_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001749 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 entsize));
1751
Nathan Scott918ae422006-03-17 17:28:54 +11001752 tmp = (be16_to_cpu(hdr->count) - args->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 * sizeof(xfs_attr_leaf_entry_t);
1754 memmove((char *)entry, (char *)(entry+1), tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001755 be16_add_cpu(&hdr->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 xfs_da_log_buf(args->trans, bp,
1757 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
Nathan Scott918ae422006-03-17 17:28:54 +11001758 entry = &leaf->entries[be16_to_cpu(hdr->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1760
1761 /*
1762 * If we removed the first entry, re-find the first used byte
1763 * in the name area. Note that if the entry was the "firstused",
1764 * then we don't have a "hole" in our block resulting from
1765 * removing the name.
1766 */
1767 if (smallest) {
1768 tmp = XFS_LBSIZE(mp);
1769 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001770 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001771 ASSERT(be16_to_cpu(entry->nameidx) >=
1772 be16_to_cpu(hdr->firstused));
1773 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1774
1775 if (be16_to_cpu(entry->nameidx) < tmp)
1776 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
Nathan Scott918ae422006-03-17 17:28:54 +11001778 hdr->firstused = cpu_to_be16(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001780 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1782 }
1783 } else {
1784 hdr->holes = 1; /* mark as needing compaction */
1785 }
1786 xfs_da_log_buf(args->trans, bp,
1787 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1788
1789 /*
1790 * Check if leaf is less than 50% full, caller may want to
1791 * "join" the leaf with a sibling if so.
1792 */
1793 tmp = sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001794 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1795 tmp += be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1797}
1798
1799/*
1800 * Move all the attribute list entries from drop_leaf into save_leaf.
1801 */
1802void
1803xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1804 xfs_da_state_blk_t *save_blk)
1805{
1806 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1807 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1808 xfs_mount_t *mp;
1809 char *tmpbuffer;
1810
1811 /*
1812 * Set up environment.
1813 */
1814 mp = state->mp;
1815 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1816 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1817 drop_leaf = drop_blk->bp->data;
1818 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001819 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1820 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 drop_hdr = &drop_leaf->hdr;
1822 save_hdr = &save_leaf->hdr;
1823
1824 /*
1825 * Save last hashval from dying block for later Btree fixup.
1826 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001827 drop_blk->hashval = be32_to_cpu(
1828 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 /*
1831 * Check if we need a temp buffer, or can we do it in place.
1832 * Note that we don't check "leaf" for holes because we will
1833 * always be dropping it, toosmall() decided that for us already.
1834 */
1835 if (save_hdr->holes == 0) {
1836 /*
1837 * dest leaf has no holes, so we add there. May need
1838 * to make some room in the entry array.
1839 */
1840 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1841 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001842 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } else {
1844 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001845 be16_to_cpu(save_hdr->count),
1846 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848 } else {
1849 /*
1850 * Destination has holes, so we make a temporary copy
1851 * of the leaf and add them both to that.
1852 */
1853 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1854 ASSERT(tmpbuffer != NULL);
1855 memset(tmpbuffer, 0, state->blocksize);
1856 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1857 tmp_hdr = &tmp_leaf->hdr;
1858 tmp_hdr->info = save_hdr->info; /* struct copy */
1859 tmp_hdr->count = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001860 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (!tmp_hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001862 tmp_hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1864 }
1865 tmp_hdr->usedbytes = 0;
1866 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1867 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001868 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001870 be16_to_cpu(tmp_leaf->hdr.count),
1871 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 } else {
1873 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001874 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001876 be16_to_cpu(tmp_leaf->hdr.count),
1877 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001880 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882
1883 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1884 state->blocksize - 1);
1885
1886 /*
1887 * Copy out last hashval in each block for B-tree code.
1888 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001889 save_blk->hashval = be32_to_cpu(
1890 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
1893/*========================================================================
1894 * Routines used for finding things in the Btree.
1895 *========================================================================*/
1896
1897/*
1898 * Look up a name in a leaf attribute list structure.
1899 * This is the internal routine, it uses the caller's buffer.
1900 *
1901 * Note that duplicate keys are allowed, but only check within the
1902 * current leaf node. The Btree code must check in adjacent leaf nodes.
1903 *
1904 * Return in args->index the index into the entry[] array of either
1905 * the found entry, or where the entry should have been (insert before
1906 * that entry).
1907 *
1908 * Don't change the args->value unless we find the attribute.
1909 */
1910int
1911xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1912{
1913 xfs_attr_leafblock_t *leaf;
1914 xfs_attr_leaf_entry_t *entry;
1915 xfs_attr_leaf_name_local_t *name_loc;
1916 xfs_attr_leaf_name_remote_t *name_rmt;
1917 int probe, span;
1918 xfs_dahash_t hashval;
1919
1920 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001921 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11001922 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 < (XFS_LBSIZE(args->dp->i_mount)/8));
1924
1925 /*
1926 * Binary search. (note: small blocks will skip this loop)
1927 */
1928 hashval = args->hashval;
Nathan Scott918ae422006-03-17 17:28:54 +11001929 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 for (entry = &leaf->entries[probe]; span > 4;
1931 entry = &leaf->entries[probe]) {
1932 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001933 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001935 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 probe -= span;
1937 else
1938 break;
1939 }
Tim Shimmin726801b2006-09-28 11:01:37 +10001940 ASSERT((probe >= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 (!leaf->hdr.count
Nathan Scott918ae422006-03-17 17:28:54 +11001942 || (probe < be16_to_cpu(leaf->hdr.count))));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001943 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 /*
1946 * Since we may have duplicate hashval's, find the first matching
1947 * hashval in the leaf.
1948 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001949 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 entry--;
1951 probe--;
1952 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001953 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
1954 (be32_to_cpu(entry->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 entry++;
1956 probe++;
1957 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001958 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
1959 (be32_to_cpu(entry->hashval) != hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 args->index = probe;
1961 return(XFS_ERROR(ENOATTR));
1962 }
1963
1964 /*
1965 * Duplicate keys may be present, so search all of them for a match.
1966 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001967 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
1968 (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 entry++, probe++) {
1970/*
1971 * GROT: Add code to remove incomplete entries.
1972 */
1973 /*
1974 * If we are looking for INCOMPLETE entries, show only those.
1975 * If we are looking for complete entries, show only those.
1976 */
1977 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
1978 (entry->flags & XFS_ATTR_INCOMPLETE)) {
1979 continue;
1980 }
1981 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001982 name_loc = xfs_attr_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (name_loc->namelen != args->namelen)
1984 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10001985 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10001987 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 continue;
1989 args->index = probe;
1990 return(XFS_ERROR(EEXIST));
1991 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001992 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (name_rmt->namelen != args->namelen)
1994 continue;
1995 if (memcmp(args->name, (char *)name_rmt->name,
1996 args->namelen) != 0)
1997 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10001998 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 continue;
2000 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002001 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002003 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return(XFS_ERROR(EEXIST));
2005 }
2006 }
2007 args->index = probe;
2008 return(XFS_ERROR(ENOATTR));
2009}
2010
2011/*
2012 * Get the value associated with an attribute name from a leaf attribute
2013 * list structure.
2014 */
2015int
2016xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2017{
2018 int valuelen;
2019 xfs_attr_leafblock_t *leaf;
2020 xfs_attr_leaf_entry_t *entry;
2021 xfs_attr_leaf_name_local_t *name_loc;
2022 xfs_attr_leaf_name_remote_t *name_rmt;
2023
2024 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002025 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002026 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 < (XFS_LBSIZE(args->dp->i_mount)/8));
Nathan Scott918ae422006-03-17 17:28:54 +11002028 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 entry = &leaf->entries[args->index];
2031 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002032 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 ASSERT(name_loc->namelen == args->namelen);
2034 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002035 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (args->flags & ATTR_KERNOVAL) {
2037 args->valuelen = valuelen;
2038 return(0);
2039 }
2040 if (args->valuelen < valuelen) {
2041 args->valuelen = valuelen;
2042 return(XFS_ERROR(ERANGE));
2043 }
2044 args->valuelen = valuelen;
2045 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2046 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002047 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 ASSERT(name_rmt->namelen == args->namelen);
2049 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002050 valuelen = be32_to_cpu(name_rmt->valuelen);
2051 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2053 if (args->flags & ATTR_KERNOVAL) {
2054 args->valuelen = valuelen;
2055 return(0);
2056 }
2057 if (args->valuelen < valuelen) {
2058 args->valuelen = valuelen;
2059 return(XFS_ERROR(ERANGE));
2060 }
2061 args->valuelen = valuelen;
2062 }
2063 return(0);
2064}
2065
2066/*========================================================================
2067 * Utility routines.
2068 *========================================================================*/
2069
2070/*
2071 * Move the indicated entries from one leaf to another.
2072 * NOTE: this routine modifies both source and destination leaves.
2073 */
2074/*ARGSUSED*/
2075STATIC void
2076xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2077 xfs_attr_leafblock_t *leaf_d, int start_d,
2078 int count, xfs_mount_t *mp)
2079{
2080 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2081 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2082 int desti, tmp, i;
2083
2084 /*
2085 * Check for nothing to do.
2086 */
2087 if (count == 0)
2088 return;
2089
2090 /*
2091 * Set up environment.
2092 */
Nathan Scott89da0542006-03-17 17:28:40 +11002093 ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2094 ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 hdr_s = &leaf_s->hdr;
2096 hdr_d = &leaf_d->hdr;
Nathan Scott918ae422006-03-17 17:28:54 +11002097 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2098 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2099 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2100 ((be16_to_cpu(hdr_s->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 * sizeof(*entry_s))+sizeof(*hdr_s)));
Nathan Scott918ae422006-03-17 17:28:54 +11002102 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2103 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2104 ((be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 * sizeof(*entry_d))+sizeof(*hdr_d)));
2106
Nathan Scott918ae422006-03-17 17:28:54 +11002107 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2108 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2109 ASSERT(count <= be16_to_cpu(hdr_s->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 /*
2112 * Move the entries in the destination leaf up to make a hole?
2113 */
Nathan Scott918ae422006-03-17 17:28:54 +11002114 if (start_d < be16_to_cpu(hdr_d->count)) {
2115 tmp = be16_to_cpu(hdr_d->count) - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 tmp *= sizeof(xfs_attr_leaf_entry_t);
2117 entry_s = &leaf_d->entries[start_d];
2118 entry_d = &leaf_d->entries[start_d + count];
2119 memmove((char *)entry_d, (char *)entry_s, tmp);
2120 }
2121
2122 /*
2123 * Copy all entry's in the same (sorted) order,
2124 * but allocate attribute info packed and in sequence.
2125 */
2126 entry_s = &leaf_s->entries[start_s];
2127 entry_d = &leaf_d->entries[start_d];
2128 desti = start_d;
2129 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002130 ASSERT(be16_to_cpu(entry_s->nameidx)
Nathan Scott918ae422006-03-17 17:28:54 +11002131 >= be16_to_cpu(hdr_s->firstused));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2133#ifdef GROT
2134 /*
2135 * Code to drop INCOMPLETE entries. Difficult to use as we
2136 * may also need to change the insertion index. Code turned
2137 * off for 6.2, should be revisited later.
2138 */
2139 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002140 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002141 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2142 be16_add_cpu(&hdr_s->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 entry_d--; /* to compensate for ++ in loop hdr */
2144 desti--;
2145 if ((start_s + i) < offset)
2146 result++; /* insertion index adjustment */
2147 } else {
2148#endif /* GROT */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002149 be16_add_cpu(&hdr_d->firstused, -tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /* both on-disk, don't endian flip twice */
2151 entry_d->hashval = entry_s->hashval;
2152 /* both on-disk, don't endian flip twice */
2153 entry_d->nameidx = hdr_d->firstused;
2154 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002155 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002157 memmove(xfs_attr_leaf_name(leaf_d, desti),
2158 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002159 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002161 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002162 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2163 be16_add_cpu(&hdr_d->usedbytes, tmp);
2164 be16_add_cpu(&hdr_s->count, -1);
2165 be16_add_cpu(&hdr_d->count, 1);
Nathan Scott918ae422006-03-17 17:28:54 +11002166 tmp = be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 * sizeof(xfs_attr_leaf_entry_t)
2168 + sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002169 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170#ifdef GROT
2171 }
2172#endif /* GROT */
2173 }
2174
2175 /*
2176 * Zero out the entries we just copied.
2177 */
Nathan Scott918ae422006-03-17 17:28:54 +11002178 if (start_s == be16_to_cpu(hdr_s->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2180 entry_s = &leaf_s->entries[start_s];
2181 ASSERT(((char *)entry_s + tmp) <=
2182 ((char *)leaf_s + XFS_LBSIZE(mp)));
2183 memset((char *)entry_s, 0, tmp);
2184 } else {
2185 /*
2186 * Move the remaining entries down to fill the hole,
2187 * then zero the entries at the top.
2188 */
Nathan Scott918ae422006-03-17 17:28:54 +11002189 tmp = be16_to_cpu(hdr_s->count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 tmp *= sizeof(xfs_attr_leaf_entry_t);
2191 entry_s = &leaf_s->entries[start_s + count];
2192 entry_d = &leaf_s->entries[start_s];
2193 memmove((char *)entry_d, (char *)entry_s, tmp);
2194
2195 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002196 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 ASSERT(((char *)entry_s + tmp) <=
2198 ((char *)leaf_s + XFS_LBSIZE(mp)));
2199 memset((char *)entry_s, 0, tmp);
2200 }
2201
2202 /*
2203 * Fill in the freemap information
2204 */
Nathan Scott918ae422006-03-17 17:28:54 +11002205 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002206 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
Nathan Scott918ae422006-03-17 17:28:54 +11002207 sizeof(xfs_attr_leaf_entry_t));
2208 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2209 - be16_to_cpu(hdr_d->freemap[0].base));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 hdr_d->freemap[1].base = 0;
2211 hdr_d->freemap[2].base = 0;
2212 hdr_d->freemap[1].size = 0;
2213 hdr_d->freemap[2].size = 0;
2214 hdr_s->holes = 1; /* leaf may not be compact */
2215}
2216
2217/*
2218 * Compare two leaf blocks "order".
2219 * Return 0 unless leaf2 should go before leaf1.
2220 */
2221int
2222xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2223{
2224 xfs_attr_leafblock_t *leaf1, *leaf2;
2225
2226 leaf1 = leaf1_bp->data;
2227 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002228 ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC) &&
2229 (be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002230 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2231 (be16_to_cpu(leaf2->hdr.count) > 0) &&
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002232 ((be32_to_cpu(leaf2->entries[0].hashval) <
2233 be32_to_cpu(leaf1->entries[0].hashval)) ||
2234 (be32_to_cpu(leaf2->entries[
2235 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2236 be32_to_cpu(leaf1->entries[
2237 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return(1);
2239 }
2240 return(0);
2241}
2242
2243/*
2244 * Pick up the last hashvalue from a leaf block.
2245 */
2246xfs_dahash_t
2247xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2248{
2249 xfs_attr_leafblock_t *leaf;
2250
2251 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002252 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (count)
Nathan Scott918ae422006-03-17 17:28:54 +11002254 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (!leaf->hdr.count)
2256 return(0);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002257 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
2260/*
2261 * Calculate the number of bytes used to store the indicated attribute
2262 * (whether local or remote only calculate bytes in this block).
2263 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002264STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2266{
2267 xfs_attr_leaf_name_local_t *name_loc;
2268 xfs_attr_leaf_name_remote_t *name_rmt;
2269 int size;
2270
Nathan Scott89da0542006-03-17 17:28:40 +11002271 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002273 name_loc = xfs_attr_leaf_name_local(leaf, index);
2274 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002275 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002277 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2278 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
2280 return(size);
2281}
2282
2283/*
2284 * Calculate the number of bytes that would be required to store the new
2285 * attribute (whether local or remote only calculate bytes in this block).
2286 * This routine decides as a side effect whether the attribute will be
2287 * a "local" or a "remote" attribute.
2288 */
2289int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002290xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 int size;
2293
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002294 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2295 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (local) {
2297 *local = 1;
2298 }
2299 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002300 size = xfs_attr_leaf_entsize_remote(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (local) {
2302 *local = 0;
2303 }
2304 }
2305 return(size);
2306}
2307
2308/*
2309 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2310 */
2311int
2312xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2313{
2314 attrlist_cursor_kern_t *cursor;
2315 xfs_attr_leafblock_t *leaf;
2316 xfs_attr_leaf_entry_t *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 int retval, i;
2318
2319 ASSERT(bp != NULL);
2320 leaf = bp->data;
2321 cursor = context->cursor;
2322 cursor->initted = 1;
2323
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002324 trace_xfs_attr_list_leaf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 /*
2327 * Re-find our place in the leaf block if this is a new syscall.
2328 */
2329 if (context->resynch) {
2330 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002331 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002332 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (cursor->offset == context->dupcnt) {
2334 context->dupcnt = 0;
2335 break;
2336 }
2337 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002338 } else if (be32_to_cpu(entry->hashval) >
2339 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 context->dupcnt = 0;
2341 break;
2342 }
2343 }
Nathan Scott918ae422006-03-17 17:28:54 +11002344 if (i == be16_to_cpu(leaf->hdr.count)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002345 trace_xfs_attr_list_notfound(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return(0);
2347 }
2348 } else {
2349 entry = &leaf->entries[0];
2350 i = 0;
2351 }
2352 context->resynch = 0;
2353
2354 /*
2355 * We have found our place, start copying out the new attributes.
2356 */
2357 retval = 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10002358 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002359 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2360 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 cursor->offset = 0;
2362 }
2363
2364 if (entry->flags & XFS_ATTR_INCOMPLETE)
2365 continue; /* skip incomplete entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002368 xfs_attr_leaf_name_local_t *name_loc =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002369 xfs_attr_leaf_name_local(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002370
2371 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002372 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002373 name_loc->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +10002374 (int)name_loc->namelen,
2375 be16_to_cpu(name_loc->valuelen),
Dave Chinnera9273ca2010-01-20 10:47:48 +11002376 &name_loc->nameval[name_loc->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +10002377 if (retval)
2378 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002380 xfs_attr_leaf_name_remote_t *name_rmt =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002381 xfs_attr_leaf_name_remote(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002382
2383 int valuelen = be32_to_cpu(name_rmt->valuelen);
2384
2385 if (context->put_value) {
2386 xfs_da_args_t args;
2387
2388 memset((char *)&args, 0, sizeof(args));
2389 args.dp = context->dp;
2390 args.whichfork = XFS_ATTR_FORK;
2391 args.valuelen = valuelen;
2392 args.value = kmem_alloc(valuelen, KM_SLEEP);
2393 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2394 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2395 retval = xfs_attr_rmtval_get(&args);
2396 if (retval)
2397 return retval;
2398 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002399 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002400 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002401 (int)name_rmt->namelen,
2402 valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002403 args.value);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002404 kmem_free(args.value);
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002405 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002406 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002407 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002408 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002409 (int)name_rmt->namelen,
2410 valuelen,
2411 NULL);
2412 }
2413 if (retval)
2414 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002416 if (context->seen_enough)
2417 break;
2418 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002420 trace_xfs_attr_list_leaf_end(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return(retval);
2422}
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425/*========================================================================
2426 * Manage the INCOMPLETE flag in a leaf entry
2427 *========================================================================*/
2428
2429/*
2430 * Clear the INCOMPLETE flag on an entry in a leaf block.
2431 */
2432int
2433xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2434{
2435 xfs_attr_leafblock_t *leaf;
2436 xfs_attr_leaf_entry_t *entry;
2437 xfs_attr_leaf_name_remote_t *name_rmt;
2438 xfs_dabuf_t *bp;
2439 int error;
2440#ifdef DEBUG
2441 xfs_attr_leaf_name_local_t *name_loc;
2442 int namelen;
2443 char *name;
2444#endif /* DEBUG */
2445
2446 /*
2447 * Set up the operation.
2448 */
2449 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2450 XFS_ATTR_FORK);
2451 if (error) {
2452 return(error);
2453 }
2454 ASSERT(bp != NULL);
2455
2456 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002457 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002458 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 ASSERT(args->index >= 0);
2460 entry = &leaf->entries[ args->index ];
2461 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2462
2463#ifdef DEBUG
2464 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002465 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 namelen = name_loc->namelen;
2467 name = (char *)name_loc->nameval;
2468 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002469 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 namelen = name_rmt->namelen;
2471 name = (char *)name_rmt->name;
2472 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002473 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 ASSERT(namelen == args->namelen);
2475 ASSERT(memcmp(name, args->name, namelen) == 0);
2476#endif /* DEBUG */
2477
2478 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2479 xfs_da_log_buf(args->trans, bp,
2480 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2481
2482 if (args->rmtblkno) {
2483 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002484 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002485 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2486 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 xfs_da_log_buf(args->trans, bp,
2488 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2489 }
2490 xfs_da_buf_done(bp);
2491
2492 /*
2493 * Commit the flag value change and start the next trans in series.
2494 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002495 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
2498/*
2499 * Set the INCOMPLETE flag on an entry in a leaf block.
2500 */
2501int
2502xfs_attr_leaf_setflag(xfs_da_args_t *args)
2503{
2504 xfs_attr_leafblock_t *leaf;
2505 xfs_attr_leaf_entry_t *entry;
2506 xfs_attr_leaf_name_remote_t *name_rmt;
2507 xfs_dabuf_t *bp;
2508 int error;
2509
2510 /*
2511 * Set up the operation.
2512 */
2513 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2514 XFS_ATTR_FORK);
2515 if (error) {
2516 return(error);
2517 }
2518 ASSERT(bp != NULL);
2519
2520 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002521 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002522 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 ASSERT(args->index >= 0);
2524 entry = &leaf->entries[ args->index ];
2525
2526 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2527 entry->flags |= XFS_ATTR_INCOMPLETE;
2528 xfs_da_log_buf(args->trans, bp,
2529 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2530 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002531 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 name_rmt->valueblk = 0;
2533 name_rmt->valuelen = 0;
2534 xfs_da_log_buf(args->trans, bp,
2535 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2536 }
2537 xfs_da_buf_done(bp);
2538
2539 /*
2540 * Commit the flag value change and start the next trans in series.
2541 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002542 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
2545/*
2546 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2547 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2548 * entry given by args->blkno2/index2.
2549 *
2550 * Note that they could be in different blocks, or in the same block.
2551 */
2552int
2553xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2554{
2555 xfs_attr_leafblock_t *leaf1, *leaf2;
2556 xfs_attr_leaf_entry_t *entry1, *entry2;
2557 xfs_attr_leaf_name_remote_t *name_rmt;
2558 xfs_dabuf_t *bp1, *bp2;
2559 int error;
2560#ifdef DEBUG
2561 xfs_attr_leaf_name_local_t *name_loc;
2562 int namelen1, namelen2;
2563 char *name1, *name2;
2564#endif /* DEBUG */
2565
2566 /*
2567 * Read the block containing the "old" attr
2568 */
2569 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2570 XFS_ATTR_FORK);
2571 if (error) {
2572 return(error);
2573 }
2574 ASSERT(bp1 != NULL);
2575
2576 /*
2577 * Read the block containing the "new" attr, if it is different
2578 */
2579 if (args->blkno2 != args->blkno) {
2580 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2581 -1, &bp2, XFS_ATTR_FORK);
2582 if (error) {
2583 return(error);
2584 }
2585 ASSERT(bp2 != NULL);
2586 } else {
2587 bp2 = bp1;
2588 }
2589
2590 leaf1 = bp1->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002591 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002592 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 ASSERT(args->index >= 0);
2594 entry1 = &leaf1->entries[ args->index ];
2595
2596 leaf2 = bp2->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002597 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002598 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 ASSERT(args->index2 >= 0);
2600 entry2 = &leaf2->entries[ args->index2 ];
2601
2602#ifdef DEBUG
2603 if (entry1->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002604 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 namelen1 = name_loc->namelen;
2606 name1 = (char *)name_loc->nameval;
2607 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002608 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 namelen1 = name_rmt->namelen;
2610 name1 = (char *)name_rmt->name;
2611 }
2612 if (entry2->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002613 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 namelen2 = name_loc->namelen;
2615 name2 = (char *)name_loc->nameval;
2616 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002617 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 namelen2 = name_rmt->namelen;
2619 name2 = (char *)name_rmt->name;
2620 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002621 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 ASSERT(namelen1 == namelen2);
2623 ASSERT(memcmp(name1, name2, namelen1) == 0);
2624#endif /* DEBUG */
2625
2626 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2627 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2628
2629 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2630 xfs_da_log_buf(args->trans, bp1,
2631 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2632 if (args->rmtblkno) {
2633 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002634 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002635 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2636 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 xfs_da_log_buf(args->trans, bp1,
2638 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2639 }
2640
2641 entry2->flags |= XFS_ATTR_INCOMPLETE;
2642 xfs_da_log_buf(args->trans, bp2,
2643 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2644 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002645 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 name_rmt->valueblk = 0;
2647 name_rmt->valuelen = 0;
2648 xfs_da_log_buf(args->trans, bp2,
2649 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2650 }
2651 xfs_da_buf_done(bp1);
2652 if (bp1 != bp2)
2653 xfs_da_buf_done(bp2);
2654
2655 /*
2656 * Commit the flag value change and start the next trans in series.
2657 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002658 error = xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 return(error);
2661}
2662
2663/*========================================================================
2664 * Indiscriminately delete the entire attribute fork
2665 *========================================================================*/
2666
2667/*
2668 * Recurse (gasp!) through the attribute nodes until we find leaves.
2669 * We're doing a depth-first traversal in order to invalidate everything.
2670 */
2671int
2672xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2673{
2674 xfs_da_blkinfo_t *info;
2675 xfs_daddr_t blkno;
2676 xfs_dabuf_t *bp;
2677 int error;
2678
2679 /*
2680 * Read block 0 to see what we have to work with.
2681 * We only get here if we have extents, since we remove
2682 * the extents in reverse order the extent containing
2683 * block 0 must still be there.
2684 */
2685 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2686 if (error)
2687 return(error);
2688 blkno = xfs_da_blkno(bp);
2689
2690 /*
2691 * Invalidate the tree, even if the "tree" is only a single leaf block.
2692 * This is a depth-first traversal!
2693 */
2694 info = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002695 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 error = xfs_attr_node_inactive(trans, dp, bp, 1);
Nathan Scott89da0542006-03-17 17:28:40 +11002697 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 error = xfs_attr_leaf_inactive(trans, dp, bp);
2699 } else {
2700 error = XFS_ERROR(EIO);
2701 xfs_da_brelse(*trans, bp);
2702 }
2703 if (error)
2704 return(error);
2705
2706 /*
2707 * Invalidate the incore copy of the root block.
2708 */
2709 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2710 if (error)
2711 return(error);
2712 xfs_da_binval(*trans, bp); /* remove from cache */
2713 /*
2714 * Commit the invalidate and start the next transaction.
2715 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002716 error = xfs_trans_roll(trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 return (error);
2719}
2720
2721/*
2722 * Recurse (gasp!) through the attribute nodes until we find leaves.
2723 * We're doing a depth-first traversal in order to invalidate everything.
2724 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002725STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2727 int level)
2728{
2729 xfs_da_blkinfo_t *info;
2730 xfs_da_intnode_t *node;
2731 xfs_dablk_t child_fsb;
2732 xfs_daddr_t parent_blkno, child_blkno;
2733 int error, count, i;
2734 xfs_dabuf_t *child_bp;
2735
2736 /*
2737 * Since this code is recursive (gasp!) we must protect ourselves.
2738 */
2739 if (level > XFS_DA_NODE_MAXDEPTH) {
2740 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2741 return(XFS_ERROR(EIO));
2742 }
2743
2744 node = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002745 ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
Nathan Scottfac80cc2006-03-17 17:29:56 +11002747 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (!count) {
2749 xfs_da_brelse(*trans, bp);
2750 return(0);
2751 }
Nathan Scott403432d2006-03-17 17:29:46 +11002752 child_fsb = be32_to_cpu(node->btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2754
2755 /*
2756 * If this is the node level just above the leaves, simply loop
2757 * over the leaves removing all of them. If this is higher up
2758 * in the tree, recurse downward.
2759 */
2760 for (i = 0; i < count; i++) {
2761 /*
2762 * Read the subsidiary block to see what we have to work with.
2763 * Don't do this in a transaction. This is a depth-first
2764 * traversal of the tree so we may deal with many blocks
2765 * before we come back to this one.
2766 */
2767 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2768 XFS_ATTR_FORK);
2769 if (error)
2770 return(error);
2771 if (child_bp) {
2772 /* save for re-read later */
2773 child_blkno = xfs_da_blkno(child_bp);
2774
2775 /*
2776 * Invalidate the subtree, however we have to.
2777 */
2778 info = child_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002779 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 error = xfs_attr_node_inactive(trans, dp,
2781 child_bp, level+1);
Nathan Scott89da0542006-03-17 17:28:40 +11002782 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 error = xfs_attr_leaf_inactive(trans, dp,
2784 child_bp);
2785 } else {
2786 error = XFS_ERROR(EIO);
2787 xfs_da_brelse(*trans, child_bp);
2788 }
2789 if (error)
2790 return(error);
2791
2792 /*
2793 * Remove the subsidiary block from the cache
2794 * and from the log.
2795 */
2796 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2797 &child_bp, XFS_ATTR_FORK);
2798 if (error)
2799 return(error);
2800 xfs_da_binval(*trans, child_bp);
2801 }
2802
2803 /*
2804 * If we're not done, re-read the parent to get the next
2805 * child block number.
2806 */
2807 if ((i+1) < count) {
2808 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2809 &bp, XFS_ATTR_FORK);
2810 if (error)
2811 return(error);
Nathan Scott403432d2006-03-17 17:29:46 +11002812 child_fsb = be32_to_cpu(node->btree[i+1].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 xfs_da_brelse(*trans, bp);
2814 }
2815 /*
2816 * Atomically commit the whole invalidate stuff.
2817 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002818 error = xfs_trans_roll(trans, dp);
2819 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return (error);
2821 }
2822
2823 return(0);
2824}
2825
2826/*
2827 * Invalidate all of the "remote" value regions pointed to by a particular
2828 * leaf block.
2829 * Note that we must release the lock on the buffer so that we are not
2830 * caught holding something that the logging code wants to flush to disk.
2831 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002832STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2834{
2835 xfs_attr_leafblock_t *leaf;
2836 xfs_attr_leaf_entry_t *entry;
2837 xfs_attr_leaf_name_remote_t *name_rmt;
2838 xfs_attr_inactive_list_t *list, *lp;
2839 int error, count, size, tmp, i;
2840
2841 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002842 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 /*
2845 * Count the number of "remote" value extents.
2846 */
2847 count = 0;
2848 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002849 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002850 if (be16_to_cpu(entry->nameidx) &&
2851 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002852 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (name_rmt->valueblk)
2854 count++;
2855 }
2856 }
2857
2858 /*
2859 * If there are no "remote" values, we're done.
2860 */
2861 if (count == 0) {
2862 xfs_da_brelse(*trans, bp);
2863 return(0);
2864 }
2865
2866 /*
2867 * Allocate storage for a list of all the "remote" value extents.
2868 */
2869 size = count * sizeof(xfs_attr_inactive_list_t);
2870 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2871
2872 /*
2873 * Identify each of the "remote" value extents.
2874 */
2875 lp = list;
2876 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002877 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002878 if (be16_to_cpu(entry->nameidx) &&
2879 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002880 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11002882 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2883 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2884 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 lp++;
2886 }
2887 }
2888 }
2889 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
2890
2891 /*
2892 * Invalidate each of the "remote" value extents.
2893 */
2894 error = 0;
2895 for (lp = list, i = 0; i < count; i++, lp++) {
2896 tmp = xfs_attr_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11002897 lp->valueblk, lp->valuelen);
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 if (error == 0)
2900 error = tmp; /* save only the 1st errno */
2901 }
2902
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002903 kmem_free((xfs_caddr_t)list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return(error);
2905}
2906
2907/*
2908 * Look at all the extents for this logical region,
2909 * invalidate any buffers that are incore/in transactions.
2910 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002911STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2913 xfs_dablk_t blkno, int blkcnt)
2914{
2915 xfs_bmbt_irec_t map;
2916 xfs_dablk_t tblkno;
2917 int tblkcnt, dblkcnt, nmap, error;
2918 xfs_daddr_t dblkno;
2919 xfs_buf_t *bp;
2920
2921 /*
2922 * Roll through the "value", invalidating the attribute value's
2923 * blocks.
2924 */
2925 tblkno = blkno;
2926 tblkcnt = blkcnt;
2927 while (tblkcnt > 0) {
2928 /*
2929 * Try to remember where we decided to put the value.
2930 */
2931 nmap = 1;
2932 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
2933 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002934 NULL, 0, &map, &nmap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (error) {
2936 return(error);
2937 }
2938 ASSERT(nmap == 1);
2939 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
2940
2941 /*
2942 * If it's a hole, these are already unmapped
2943 * so there's nothing to invalidate.
2944 */
2945 if (map.br_startblock != HOLESTARTBLOCK) {
2946
2947 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
2948 map.br_startblock);
2949 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
2950 map.br_blockcount);
2951 bp = xfs_trans_get_buf(*trans,
2952 dp->i_mount->m_ddev_targp,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002953 dblkno, dblkcnt, XBF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 xfs_trans_binval(*trans, bp);
2955 /*
2956 * Roll to next transaction.
2957 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002958 error = xfs_trans_roll(trans, dp);
2959 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return (error);
2961 }
2962
2963 tblkno += map.br_blockcount;
2964 tblkcnt -= map.br_blockcount;
2965 }
2966
2967 return(0);
2968}