blob: e4664136843d9675d1daeea68d3ae5c5d31865f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
34#include "xfs_alloc.h"
35#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110040#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_attr.h"
43#include "xfs_attr_leaf.h"
44#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
47 * xfs_attr_leaf.c
48 *
49 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
50 */
51
52/*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
55
56/*
57 * Routines used for growing the Btree.
58 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100059STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
60 xfs_dabuf_t **bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
62 int freemap_index);
63STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
64STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
65 xfs_da_state_blk_t *blk1,
66 xfs_da_state_blk_t *blk2);
67STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
68 xfs_da_state_blk_t *leaf_blk_1,
69 xfs_da_state_blk_t *leaf_blk_2,
70 int *number_entries_in_blk1,
71 int *number_usedbytes_in_blk1);
72
73/*
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100074 * Routines used for shrinking the Btree.
75 */
76STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
77 xfs_dabuf_t *bp, int level);
78STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
79 xfs_dabuf_t *bp);
80STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
81 xfs_dablk_t blkno, int blkcnt);
82
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Utility routines.
85 */
86STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
87 int src_start,
88 xfs_attr_leafblock_t *dst_leaf,
89 int dst_start, int move_count,
90 xfs_mount_t *mp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100091STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
Tim Shimmin726801b2006-09-28 11:01:37 +100092
93/*========================================================================
94 * Namespace helper routines
95 *========================================================================*/
96
David Chinner7989cb82007-02-10 18:34:56 +110097STATIC_INLINE attrnames_t *
Tim Shimmin726801b2006-09-28 11:01:37 +100098xfs_attr_flags_namesp(int flags)
99{
100 return ((flags & XFS_ATTR_SECURE) ? &attr_secure:
101 ((flags & XFS_ATTR_ROOT) ? &attr_trusted : &attr_user));
102}
103
104/*
105 * If namespace bits don't match return 0.
106 * If all match then return 1.
107 */
David Chinner7989cb82007-02-10 18:34:56 +1100108STATIC_INLINE int
Tim Shimmin726801b2006-09-28 11:01:37 +1000109xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
110{
111 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
112}
113
114/*
115 * If namespace bits don't match and we don't have an override for it
116 * then return 0.
117 * If all match or are overridable then return 1.
118 */
David Chinner7989cb82007-02-10 18:34:56 +1100119STATIC_INLINE int
Tim Shimmin726801b2006-09-28 11:01:37 +1000120xfs_attr_namesp_match_overrides(int arg_flags, int ondisk_flags)
121{
122 if (((arg_flags & ATTR_SECURE) == 0) !=
123 ((ondisk_flags & XFS_ATTR_SECURE) == 0) &&
124 !(arg_flags & ATTR_KERNORMALS))
125 return 0;
126 if (((arg_flags & ATTR_ROOT) == 0) !=
127 ((ondisk_flags & XFS_ATTR_ROOT) == 0) &&
128 !(arg_flags & ATTR_KERNROOTLS))
129 return 0;
130 return 1;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133
134/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100135 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *========================================================================*/
137
138/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100139 * Query whether the requested number of additional bytes of extended
140 * attribute space will be able to fit inline.
141 * Returns zero if not, else the di_forkoff fork offset to be used in the
142 * literal area for attribute data once the new bytes have been added.
143 *
144 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
145 * special case for dev/uuid inodes, they have fixed size data forks.
146 */
147int
148xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
149{
150 int offset;
151 int minforkoff; /* lower limit on valid forkoff locations */
152 int maxforkoff; /* upper limit on valid forkoff locations */
Barry Naujoke5889e92007-02-10 18:35:58 +1100153 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100154 xfs_mount_t *mp = dp->i_mount;
155
Nathan Scottd8cc8902005-11-02 10:34:53 +1100156 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
157
158 switch (dp->i_d.di_format) {
159 case XFS_DINODE_FMT_DEV:
160 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
161 return (offset >= minforkoff) ? minforkoff : 0;
162 case XFS_DINODE_FMT_UUID:
163 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
164 return (offset >= minforkoff) ? minforkoff : 0;
165 }
166
Nathan Scott13059ff2006-01-11 15:32:01 +1100167 if (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +1100168 if (bytes <= XFS_IFORK_ASIZE(dp))
169 return mp->m_attroffset >> 3;
170 return 0;
171 }
172
Barry Naujoke5889e92007-02-10 18:35:58 +1100173 dsize = dp->i_df.if_bytes;
174
175 switch (dp->i_d.di_format) {
176 case XFS_DINODE_FMT_EXTENTS:
177 /*
178 * If there is no attr fork and the data fork is extents,
179 * determine if creating the default attr fork will result
180 * in the extents form migrating to btree. If so, the
181 * minimum offset only needs to be the space required for
182 * the btree root.
183 */
184 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes > mp->m_attroffset)
185 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
186 break;
187
188 case XFS_DINODE_FMT_BTREE:
189 /*
190 * If have data btree then keep forkoff if we have one,
191 * otherwise we are adding a new attr, so then we set
192 * minforkoff to where the btree root can finish so we have
193 * plenty of room for attrs
194 */
195 if (dp->i_d.di_forkoff) {
196 if (offset < dp->i_d.di_forkoff)
197 return 0;
198 else
199 return dp->i_d.di_forkoff;
200 } else
201 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
202 break;
203 }
204
205 /*
206 * A data fork btree root must have space for at least
207 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
208 */
209 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100210 minforkoff = roundup(minforkoff, 8) >> 3;
211
212 /* attr fork btree root can have at least this many key/ptr pairs */
213 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
214 maxforkoff = maxforkoff >> 3; /* rounded down */
215
216 if (offset >= minforkoff && offset < maxforkoff)
217 return offset;
218 if (offset >= maxforkoff)
219 return maxforkoff;
220 return 0;
221}
222
223/*
224 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
225 */
226STATIC void
227xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
228{
229 unsigned long s;
230
Nathan Scott13059ff2006-01-11 15:32:01 +1100231 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Nathan Scottd8cc8902005-11-02 10:34:53 +1100232 !(XFS_SB_VERSION_HASATTR2(&mp->m_sb))) {
233 s = XFS_SB_LOCK(mp);
234 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb)) {
235 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
236 XFS_SB_UNLOCK(mp, s);
237 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
238 } else
239 XFS_SB_UNLOCK(mp, s);
240 }
241}
242
243/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * Create the initial contents of a shortform attribute list.
245 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100246void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247xfs_attr_shortform_create(xfs_da_args_t *args)
248{
249 xfs_attr_sf_hdr_t *hdr;
250 xfs_inode_t *dp;
251 xfs_ifork_t *ifp;
252
253 dp = args->dp;
254 ASSERT(dp != NULL);
255 ifp = dp->i_afp;
256 ASSERT(ifp != NULL);
257 ASSERT(ifp->if_bytes == 0);
258 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
259 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
260 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
261 ifp->if_flags |= XFS_IFINLINE;
262 } else {
263 ASSERT(ifp->if_flags & XFS_IFINLINE);
264 }
265 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
266 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
267 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100268 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/*
273 * Add a name/value pair to the shortform attribute list.
274 * Overflow from the inode has already been checked for.
275 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100276void
277xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 xfs_attr_shortform_t *sf;
280 xfs_attr_sf_entry_t *sfe;
281 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100282 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 xfs_inode_t *dp;
284 xfs_ifork_t *ifp;
285
286 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100287 mp = dp->i_mount;
288 dp->i_d.di_forkoff = forkoff;
289 dp->i_df.if_ext_max =
290 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
291 dp->i_afp->if_ext_max =
292 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 ifp = dp->i_afp;
295 ASSERT(ifp->if_flags & XFS_IFINLINE);
296 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
297 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100298 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100299#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (sfe->namelen != args->namelen)
301 continue;
302 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
303 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000304 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100306 ASSERT(0);
307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
310 offset = (char *)sfe - (char *)sf;
311 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
312 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
313 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
314 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
315
316 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100317 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000318 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 memcpy(sfe->nameval, args->name, args->namelen);
320 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100321 sf->hdr.count++;
322 be16_add(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
324
Nathan Scottd8cc8902005-11-02 10:34:53 +1100325 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100329 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
331int
332xfs_attr_shortform_remove(xfs_da_args_t *args)
333{
334 xfs_attr_shortform_t *sf;
335 xfs_attr_sf_entry_t *sfe;
336 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100337 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xfs_inode_t *dp;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100341 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 base = sizeof(xfs_attr_sf_hdr_t);
343 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
344 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100345 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100346 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 base += size, i++) {
348 size = XFS_ATTR_SF_ENTSIZE(sfe);
349 if (sfe->namelen != args->namelen)
350 continue;
351 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
352 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000353 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 continue;
355 break;
356 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100357 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return(XFS_ERROR(ENOATTR));
359
Nathan Scottd8cc8902005-11-02 10:34:53 +1100360 /*
361 * Fix up the attribute fork data, covering the hole
362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100364 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100365 if (end != totsize)
366 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100367 sf->hdr.count--;
368 be16_add(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100369
370 /*
371 * Fix up the start offset of the attribute fork
372 */
373 totsize -= size;
Nathan Scotte0144ca2005-11-25 16:42:22 +1100374 if (totsize == sizeof(xfs_attr_sf_hdr_t) && !args->addname &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100375 (mp->m_flags & XFS_MOUNT_ATTR2) &&
376 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE)) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100377 /*
378 * Last attribute now removed, revert to original
379 * inode format making all literal area available
380 * to the data fork once more.
381 */
382 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
383 dp->i_d.di_forkoff = 0;
384 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
385 ASSERT(dp->i_d.di_anextents == 0);
386 ASSERT(dp->i_afp == NULL);
387 dp->i_df.if_ext_max =
388 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
389 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
390 } else {
391 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
392 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
393 ASSERT(dp->i_d.di_forkoff);
Nathan Scotte0144ca2005-11-25 16:42:22 +1100394 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) || args->addname ||
Nathan Scott13059ff2006-01-11 15:32:01 +1100395 !(mp->m_flags & XFS_MOUNT_ATTR2));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100396 dp->i_afp->if_ext_max =
397 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
398 dp->i_df.if_ext_max =
399 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
400 xfs_trans_log_inode(args->trans, dp,
401 XFS_ILOG_CORE | XFS_ILOG_ADATA);
402 }
403
404 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 return(0);
407}
408
409/*
410 * Look up a name in a shortform attribute list structure.
411 */
412/*ARGSUSED*/
413int
414xfs_attr_shortform_lookup(xfs_da_args_t *args)
415{
416 xfs_attr_shortform_t *sf;
417 xfs_attr_sf_entry_t *sfe;
418 int i;
419 xfs_ifork_t *ifp;
420
421 ifp = args->dp->i_afp;
422 ASSERT(ifp->if_flags & XFS_IFINLINE);
423 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
424 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100425 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
427 if (sfe->namelen != args->namelen)
428 continue;
429 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
430 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000431 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 continue;
433 return(XFS_ERROR(EEXIST));
434 }
435 return(XFS_ERROR(ENOATTR));
436}
437
438/*
439 * Look up a name in a shortform attribute list structure.
440 */
441/*ARGSUSED*/
442int
443xfs_attr_shortform_getvalue(xfs_da_args_t *args)
444{
445 xfs_attr_shortform_t *sf;
446 xfs_attr_sf_entry_t *sfe;
447 int i;
448
449 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
450 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
451 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100452 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
454 if (sfe->namelen != args->namelen)
455 continue;
456 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
457 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000458 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 continue;
460 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100461 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return(XFS_ERROR(EEXIST));
463 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100464 if (args->valuelen < sfe->valuelen) {
465 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return(XFS_ERROR(ERANGE));
467 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100468 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 memcpy(args->value, &sfe->nameval[args->namelen],
470 args->valuelen);
471 return(XFS_ERROR(EEXIST));
472 }
473 return(XFS_ERROR(ENOATTR));
474}
475
476/*
477 * Convert from using the shortform to the leaf.
478 */
479int
480xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
481{
482 xfs_inode_t *dp;
483 xfs_attr_shortform_t *sf;
484 xfs_attr_sf_entry_t *sfe;
485 xfs_da_args_t nargs;
486 char *tmpbuffer;
487 int error, i, size;
488 xfs_dablk_t blkno;
489 xfs_dabuf_t *bp;
490 xfs_ifork_t *ifp;
491
492 dp = args->dp;
493 ifp = dp->i_afp;
494 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100495 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 tmpbuffer = kmem_alloc(size, KM_SLEEP);
497 ASSERT(tmpbuffer != NULL);
498 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
499 sf = (xfs_attr_shortform_t *)tmpbuffer;
500
501 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
502 bp = NULL;
503 error = xfs_da_grow_inode(args, &blkno);
504 if (error) {
505 /*
506 * If we hit an IO error middle of the transaction inside
507 * grow_inode(), we may have inconsistent data. Bail out.
508 */
509 if (error == EIO)
510 goto out;
511 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
512 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
513 goto out;
514 }
515
516 ASSERT(blkno == 0);
517 error = xfs_attr_leaf_create(args, blkno, &bp);
518 if (error) {
519 error = xfs_da_shrink_inode(args, 0, bp);
520 bp = NULL;
521 if (error)
522 goto out;
523 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
524 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
525 goto out;
526 }
527
528 memset((char *)&nargs, 0, sizeof(nargs));
529 nargs.dp = dp;
530 nargs.firstblock = args->firstblock;
531 nargs.flist = args->flist;
532 nargs.total = args->total;
533 nargs.whichfork = XFS_ATTR_FORK;
534 nargs.trans = args->trans;
535 nargs.oknoent = 1;
536
537 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100538 for (i = 0; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 nargs.name = (char *)sfe->nameval;
540 nargs.namelen = sfe->namelen;
541 nargs.value = (char *)&sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100542 nargs.valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
544 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000545 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
547 ASSERT(error == ENOATTR);
548 error = xfs_attr_leaf_add(bp, &nargs);
549 ASSERT(error != ENOSPC);
550 if (error)
551 goto out;
552 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
553 }
554 error = 0;
555
556out:
557 if(bp)
558 xfs_da_buf_done(bp);
559 kmem_free(tmpbuffer, size);
560 return(error);
561}
562
563STATIC int
564xfs_attr_shortform_compare(const void *a, const void *b)
565{
566 xfs_attr_sf_sort_t *sa, *sb;
567
568 sa = (xfs_attr_sf_sort_t *)a;
569 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100570 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100572 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return(1);
574 } else {
575 return(sa->entno - sb->entno);
576 }
577}
578
Tim Shimmin726801b2006-09-28 11:01:37 +1000579
580#define XFS_ISRESET_CURSOR(cursor) \
581 (!((cursor)->initted) && !((cursor)->hashval) && \
582 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/*
584 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000585 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * If the output buffer is not large enough to hold them all, then we
587 * we have to calculate each entries' hashvalue and sort them before
588 * we can begin returning them to the user.
589 */
590/*ARGSUSED*/
591int
592xfs_attr_shortform_list(xfs_attr_list_context_t *context)
593{
594 attrlist_cursor_kern_t *cursor;
595 xfs_attr_sf_sort_t *sbuf, *sbp;
596 xfs_attr_shortform_t *sf;
597 xfs_attr_sf_entry_t *sfe;
598 xfs_inode_t *dp;
599 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000600 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 ASSERT(context != NULL);
603 dp = context->dp;
604 ASSERT(dp != NULL);
605 ASSERT(dp->i_afp != NULL);
606 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
607 ASSERT(sf != NULL);
608 if (!sf->hdr.count)
609 return(0);
610 cursor = context->cursor;
611 ASSERT(cursor != NULL);
612
613 xfs_attr_trace_l_c("sf start", context);
614
615 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000616 * If the buffer is large enough and the cursor is at the start,
617 * do not bother with sorting since we will return everything in
618 * one buffer and another call using the cursor won't need to be
619 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000621 * If bufsize is zero then put_listent must be a search function
622 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000624 if (context->bufsize == 0 ||
625 (XFS_ISRESET_CURSOR(cursor) &&
626 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100627 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 attrnames_t *namesp;
629
Tim Shimmin726801b2006-09-28 11:01:37 +1000630 if (!xfs_attr_namesp_match_overrides(context->flags, sfe->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
632 continue;
633 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000634 namesp = xfs_attr_flags_namesp(sfe->flags);
635 error = context->put_listent(context,
636 namesp,
637 (char *)sfe->nameval,
638 (int)sfe->namelen,
639 (int)sfe->valuelen,
640 (char*)&sfe->nameval[sfe->namelen]);
641
642 /*
643 * Either search callback finished early or
644 * didn't fit it all in the buffer after all.
645 */
646 if (context->seen_enough)
647 break;
648
649 if (error)
650 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
652 }
653 xfs_attr_trace_l_c("sf big-gulp", context);
654 return(0);
655 }
656
Tim Shimmin726801b2006-09-28 11:01:37 +1000657 /* do no more for a search callback */
658 if (context->bufsize == 0)
659 return 0;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * It didn't all fit, so we have to sort everything on hashval.
663 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100664 sbsize = sf->hdr.count * sizeof(*sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
666
667 /*
668 * Scan the attribute list for the rest of the entries, storing
669 * the relevant info from only those that match into a buffer.
670 */
671 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100672 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (unlikely(
674 ((char *)sfe < (char *)sf) ||
675 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
676 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
677 XFS_ERRLEVEL_LOW,
678 context->dp->i_mount, sfe);
679 xfs_attr_trace_l_c("sf corrupted", context);
680 kmem_free(sbuf, sbsize);
681 return XFS_ERROR(EFSCORRUPTED);
682 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000683 if (!xfs_attr_namesp_match_overrides(context->flags, sfe->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
685 continue;
686 }
687 sbp->entno = i;
Nathan Scott984a0812006-03-17 17:29:31 +1100688 sbp->hash = xfs_da_hashname((char *)sfe->nameval, sfe->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 sbp->name = (char *)sfe->nameval;
690 sbp->namelen = sfe->namelen;
691 /* These are bytes, and both on-disk, don't endian-flip */
692 sbp->valuelen = sfe->valuelen;
693 sbp->flags = sfe->flags;
694 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
695 sbp++;
696 nsbuf++;
697 }
698
699 /*
700 * Sort the entries on hash then entno.
701 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100702 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /*
705 * Re-find our place IN THE SORTED LIST.
706 */
707 count = 0;
708 cursor->initted = 1;
709 cursor->blkno = 0;
710 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100711 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (cursor->offset == count) {
713 break;
714 }
715 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100716 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 break;
718 }
719 }
720 if (i == nsbuf) {
721 kmem_free(sbuf, sbsize);
722 xfs_attr_trace_l_c("blk end", context);
723 return(0);
724 }
725
726 /*
727 * Loop putting entries into the user buffer.
728 */
729 for ( ; i < nsbuf; i++, sbp++) {
730 attrnames_t *namesp;
731
Tim Shimmin726801b2006-09-28 11:01:37 +1000732 namesp = xfs_attr_flags_namesp(sbp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Nathan Scott984a0812006-03-17 17:29:31 +1100734 if (cursor->hashval != sbp->hash) {
735 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 cursor->offset = 0;
737 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000738 error = context->put_listent(context,
739 namesp,
740 sbp->name,
741 sbp->namelen,
742 sbp->valuelen,
743 &sbp->name[sbp->namelen]);
744 if (error)
745 return error;
746 if (context->seen_enough)
747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 cursor->offset++;
749 }
750
751 kmem_free(sbuf, sbsize);
752 xfs_attr_trace_l_c("sf E-O-F", context);
753 return(0);
754}
755
756/*
757 * Check a leaf attribute block to see if all the entries would fit into
758 * a shortform attribute list.
759 */
760int
761xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
762{
763 xfs_attr_leafblock_t *leaf;
764 xfs_attr_leaf_entry_t *entry;
765 xfs_attr_leaf_name_local_t *name_loc;
766 int bytes, i;
767
768 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100769 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 entry = &leaf->entries[0];
772 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100773 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (entry->flags & XFS_ATTR_INCOMPLETE)
775 continue; /* don't copy partial entries */
776 if (!(entry->flags & XFS_ATTR_LOCAL))
777 return(0);
778 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
779 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
780 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100781 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return(0);
783 bytes += sizeof(struct xfs_attr_sf_entry)-1
784 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100785 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100787 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100788 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100789 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100790 return(-1);
791 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794/*
795 * Convert a leaf attribute list to shortform attribute list
796 */
797int
Nathan Scottd8cc8902005-11-02 10:34:53 +1100798xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 xfs_attr_leafblock_t *leaf;
801 xfs_attr_leaf_entry_t *entry;
802 xfs_attr_leaf_name_local_t *name_loc;
803 xfs_da_args_t nargs;
804 xfs_inode_t *dp;
805 char *tmpbuffer;
806 int error, i;
807
808 dp = args->dp;
809 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
810 ASSERT(tmpbuffer != NULL);
811
812 ASSERT(bp != NULL);
813 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
814 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Nathan Scott89da0542006-03-17 17:28:40 +1100815 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
817
818 /*
819 * Clean out the prior contents of the attribute list.
820 */
821 error = xfs_da_shrink_inode(args, 0, bp);
822 if (error)
823 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100824
825 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +1100826 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +1100827 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Nathan Scotte0144ca2005-11-25 16:42:22 +1100828
Nathan Scottd8cc8902005-11-02 10:34:53 +1100829 /*
830 * Last attribute was removed, revert to original
831 * inode format making all literal area available
832 * to the data fork once more.
833 */
834 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
835 dp->i_d.di_forkoff = 0;
836 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
837 ASSERT(dp->i_d.di_anextents == 0);
838 ASSERT(dp->i_afp == NULL);
839 dp->i_df.if_ext_max =
840 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
841 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100843 }
844
845 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /*
848 * Copy the attributes
849 */
850 memset((char *)&nargs, 0, sizeof(nargs));
851 nargs.dp = dp;
852 nargs.firstblock = args->firstblock;
853 nargs.flist = args->flist;
854 nargs.total = args->total;
855 nargs.whichfork = XFS_ATTR_FORK;
856 nargs.trans = args->trans;
857 nargs.oknoent = 1;
858 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +1100859 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (entry->flags & XFS_ATTR_INCOMPLETE)
861 continue; /* don't copy partial entries */
862 if (!entry->nameidx)
863 continue;
864 ASSERT(entry->flags & XFS_ATTR_LOCAL);
865 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
866 nargs.name = (char *)name_loc->nameval;
867 nargs.namelen = name_loc->namelen;
868 nargs.value = (char *)&name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +1100869 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100870 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +1000871 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100872 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 error = 0;
875
876out:
877 kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
878 return(error);
879}
880
881/*
882 * Convert from using a single leaf to a root node and a leaf.
883 */
884int
885xfs_attr_leaf_to_node(xfs_da_args_t *args)
886{
887 xfs_attr_leafblock_t *leaf;
888 xfs_da_intnode_t *node;
889 xfs_inode_t *dp;
890 xfs_dabuf_t *bp1, *bp2;
891 xfs_dablk_t blkno;
892 int error;
893
894 dp = args->dp;
895 bp1 = bp2 = NULL;
896 error = xfs_da_grow_inode(args, &blkno);
897 if (error)
898 goto out;
899 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
900 XFS_ATTR_FORK);
901 if (error)
902 goto out;
903 ASSERT(bp1 != NULL);
904 bp2 = NULL;
905 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
906 XFS_ATTR_FORK);
907 if (error)
908 goto out;
909 ASSERT(bp2 != NULL);
910 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
911 xfs_da_buf_done(bp1);
912 bp1 = NULL;
913 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
914
915 /*
916 * Set up the new root node.
917 */
918 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
919 if (error)
920 goto out;
921 node = bp1->data;
922 leaf = bp2->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100923 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /* both on-disk, don't endian-flip twice */
925 node->btree[0].hashval =
Nathan Scott918ae422006-03-17 17:28:54 +1100926 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
Nathan Scott403432d2006-03-17 17:29:46 +1100927 node->btree[0].before = cpu_to_be32(blkno);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100928 node->hdr.count = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
930 error = 0;
931out:
932 if (bp1)
933 xfs_da_buf_done(bp1);
934 if (bp2)
935 xfs_da_buf_done(bp2);
936 return(error);
937}
938
939
940/*========================================================================
941 * Routines used for growing the Btree.
942 *========================================================================*/
943
944/*
945 * Create the initial contents of a leaf attribute list
946 * or a leaf in a node attribute list.
947 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000948STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
950{
951 xfs_attr_leafblock_t *leaf;
952 xfs_attr_leaf_hdr_t *hdr;
953 xfs_inode_t *dp;
954 xfs_dabuf_t *bp;
955 int error;
956
957 dp = args->dp;
958 ASSERT(dp != NULL);
959 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
960 XFS_ATTR_FORK);
961 if (error)
962 return(error);
963 ASSERT(bp != NULL);
964 leaf = bp->data;
965 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
966 hdr = &leaf->hdr;
Nathan Scott89da0542006-03-17 17:28:40 +1100967 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +1100968 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +1100970 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
972 }
973
Nathan Scott918ae422006-03-17 17:28:54 +1100974 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
975 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
976 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
979
980 *bpp = bp;
981 return(0);
982}
983
984/*
985 * Split the leaf node, rebalance, then add the new entry.
986 */
987int
988xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
989 xfs_da_state_blk_t *newblk)
990{
991 xfs_dablk_t blkno;
992 int error;
993
994 /*
995 * Allocate space for a new leaf node.
996 */
997 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
998 error = xfs_da_grow_inode(state->args, &blkno);
999 if (error)
1000 return(error);
1001 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
1002 if (error)
1003 return(error);
1004 newblk->blkno = blkno;
1005 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1006
1007 /*
1008 * Rebalance the entries across the two leaves.
1009 * NOTE: rebalance() currently depends on the 2nd block being empty.
1010 */
1011 xfs_attr_leaf_rebalance(state, oldblk, newblk);
1012 error = xfs_da_blk_link(state, oldblk, newblk);
1013 if (error)
1014 return(error);
1015
1016 /*
1017 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1018 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1019 * "new" attrs info. Will need the "old" info to remove it later.
1020 *
1021 * Insert the "new" entry in the correct block.
1022 */
1023 if (state->inleaf)
1024 error = xfs_attr_leaf_add(oldblk->bp, state->args);
1025 else
1026 error = xfs_attr_leaf_add(newblk->bp, state->args);
1027
1028 /*
1029 * Update last hashval in each block since we added the name.
1030 */
1031 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1032 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1033 return(error);
1034}
1035
1036/*
1037 * Add a name to the leaf attribute list structure.
1038 */
1039int
1040xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
1041{
1042 xfs_attr_leafblock_t *leaf;
1043 xfs_attr_leaf_hdr_t *hdr;
1044 xfs_attr_leaf_map_t *map;
1045 int tablesize, entsize, sum, tmp, i;
1046
1047 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001048 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001050 && (args->index <= be16_to_cpu(leaf->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 hdr = &leaf->hdr;
Nathan Scottaa82daa2005-11-02 10:33:33 +11001052 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1054
1055 /*
1056 * Search through freemap for first-fit on new name length.
1057 * (may need to figure in size of entry struct too)
1058 */
Nathan Scott918ae422006-03-17 17:28:54 +11001059 tablesize = (be16_to_cpu(hdr->count) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 * sizeof(xfs_attr_leaf_entry_t)
1061 + sizeof(xfs_attr_leaf_hdr_t);
1062 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1063 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
Nathan Scott918ae422006-03-17 17:28:54 +11001064 if (tablesize > be16_to_cpu(hdr->firstused)) {
1065 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 continue;
1067 }
1068 if (!map->size)
1069 continue; /* no space in this map */
1070 tmp = entsize;
Nathan Scott918ae422006-03-17 17:28:54 +11001071 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 tmp += sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001073 if (be16_to_cpu(map->size) >= tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 tmp = xfs_attr_leaf_add_work(bp, args, i);
1075 return(tmp);
1076 }
Nathan Scott918ae422006-03-17 17:28:54 +11001077 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
1080 /*
1081 * If there are no holes in the address space of the block,
1082 * and we don't have enough freespace, then compaction will do us
1083 * no good and we should just give up.
1084 */
1085 if (!hdr->holes && (sum < entsize))
1086 return(XFS_ERROR(ENOSPC));
1087
1088 /*
1089 * Compact the entries to coalesce free space.
1090 * This may change the hdr->count via dropping INCOMPLETE entries.
1091 */
1092 xfs_attr_leaf_compact(args->trans, bp);
1093
1094 /*
1095 * After compaction, the block is guaranteed to have only one
1096 * free region, in freemap[0]. If it is not big enough, give up.
1097 */
Nathan Scott918ae422006-03-17 17:28:54 +11001098 if (be16_to_cpu(hdr->freemap[0].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1100 return(XFS_ERROR(ENOSPC));
1101
1102 return(xfs_attr_leaf_add_work(bp, args, 0));
1103}
1104
1105/*
1106 * Add a name to a leaf attribute list structure.
1107 */
1108STATIC int
1109xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1110{
1111 xfs_attr_leafblock_t *leaf;
1112 xfs_attr_leaf_hdr_t *hdr;
1113 xfs_attr_leaf_entry_t *entry;
1114 xfs_attr_leaf_name_local_t *name_loc;
1115 xfs_attr_leaf_name_remote_t *name_rmt;
1116 xfs_attr_leaf_map_t *map;
1117 xfs_mount_t *mp;
1118 int tmp, i;
1119
1120 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001121 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 hdr = &leaf->hdr;
1123 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
Nathan Scott918ae422006-03-17 17:28:54 +11001124 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /*
1127 * Force open some space in the entry array and fill it in.
1128 */
1129 entry = &leaf->entries[args->index];
Nathan Scott918ae422006-03-17 17:28:54 +11001130 if (args->index < be16_to_cpu(hdr->count)) {
1131 tmp = be16_to_cpu(hdr->count) - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 tmp *= sizeof(xfs_attr_leaf_entry_t);
1133 memmove((char *)(entry+1), (char *)entry, tmp);
1134 xfs_da_log_buf(args->trans, bp,
1135 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1136 }
Nathan Scott918ae422006-03-17 17:28:54 +11001137 be16_add(&hdr->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /*
1140 * Allocate space for the new string (at the end of the run).
1141 */
1142 map = &hdr->freemap[mapindex];
1143 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001144 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1145 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1146 ASSERT(be16_to_cpu(map->size) >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001147 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1148 mp->m_sb.sb_blocksize, NULL));
Nathan Scott918ae422006-03-17 17:28:54 +11001149 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1150 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
1151 be16_add(&map->size,
Nathan Scottaa82daa2005-11-02 10:33:33 +11001152 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1153 mp->m_sb.sb_blocksize, &tmp));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001154 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1155 be16_to_cpu(map->size));
1156 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001158 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (args->rename) {
1160 entry->flags |= XFS_ATTR_INCOMPLETE;
1161 if ((args->blkno2 == args->blkno) &&
1162 (args->index2 <= args->index)) {
1163 args->index2++;
1164 }
1165 }
1166 xfs_da_log_buf(args->trans, bp,
1167 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001168 ASSERT((args->index == 0) ||
1169 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Nathan Scott918ae422006-03-17 17:28:54 +11001170 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001171 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /*
1174 * Copy the attribute name and value into the new space.
1175 *
1176 * For "remote" attribute values, simply note that we need to
1177 * allocate space for the "remote" value. We can't actually
1178 * allocate the extents in this transaction, and we can't decide
1179 * which blocks they should be as we might allocate more blocks
1180 * as part of this transaction (a split operation for example).
1181 */
1182 if (entry->flags & XFS_ATTR_LOCAL) {
1183 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
1184 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001185 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1187 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001188 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 } else {
1190 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
1191 name_rmt->namelen = args->namelen;
1192 memcpy((char *)name_rmt->name, args->name, args->namelen);
1193 entry->flags |= XFS_ATTR_INCOMPLETE;
1194 /* just in case */
1195 name_rmt->valuelen = 0;
1196 name_rmt->valueblk = 0;
1197 args->rmtblkno = 1;
1198 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1199 }
1200 xfs_da_log_buf(args->trans, bp,
1201 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1202 xfs_attr_leaf_entsize(leaf, args->index)));
1203
1204 /*
1205 * Update the control info for this leaf node
1206 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001207 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /* both on-disk, don't endian-flip twice */
1209 hdr->firstused = entry->nameidx;
1210 }
Nathan Scott918ae422006-03-17 17:28:54 +11001211 ASSERT(be16_to_cpu(hdr->firstused) >=
1212 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1213 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 + sizeof(xfs_attr_leaf_hdr_t);
1215 map = &hdr->freemap[0];
1216 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001217 if (be16_to_cpu(map->base) == tmp) {
1218 be16_add(&map->base, sizeof(xfs_attr_leaf_entry_t));
1219 be16_add(&map->size,
1220 -((int)sizeof(xfs_attr_leaf_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 }
Nathan Scott918ae422006-03-17 17:28:54 +11001223 be16_add(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 xfs_da_log_buf(args->trans, bp,
1225 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1226 return(0);
1227}
1228
1229/*
1230 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1231 */
1232STATIC void
1233xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1234{
1235 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1236 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1237 xfs_mount_t *mp;
1238 char *tmpbuffer;
1239
1240 mp = trans->t_mountp;
1241 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1242 ASSERT(tmpbuffer != NULL);
1243 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1244 memset(bp->data, 0, XFS_LBSIZE(mp));
1245
1246 /*
1247 * Copy basic information
1248 */
1249 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1250 leaf_d = bp->data;
1251 hdr_s = &leaf_s->hdr;
1252 hdr_d = &leaf_d->hdr;
1253 hdr_d->info = hdr_s->info; /* struct copy */
Nathan Scott918ae422006-03-17 17:28:54 +11001254 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* handle truncation gracefully */
1256 if (!hdr_d->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001257 hdr_d->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1259 }
1260 hdr_d->usedbytes = 0;
1261 hdr_d->count = 0;
1262 hdr_d->holes = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001263 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1264 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1265 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
1268 * Copy all entry's in the same (sorted) order,
1269 * but allocate name/value pairs packed and in sequence.
1270 */
1271 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001272 be16_to_cpu(hdr_s->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1274
1275 kmem_free(tmpbuffer, XFS_LBSIZE(mp));
1276}
1277
1278/*
1279 * Redistribute the attribute list entries between two leaf nodes,
1280 * taking into account the size of the new entry.
1281 *
1282 * NOTE: if new block is empty, then it will get the upper half of the
1283 * old block. At present, all (one) callers pass in an empty second block.
1284 *
1285 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1286 * to match what it is doing in splitting the attribute leaf block. Those
1287 * values are used in "atomic rename" operations on attributes. Note that
1288 * the "new" and "old" values can end up in different blocks.
1289 */
1290STATIC void
1291xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1292 xfs_da_state_blk_t *blk2)
1293{
1294 xfs_da_args_t *args;
1295 xfs_da_state_blk_t *tmp_blk;
1296 xfs_attr_leafblock_t *leaf1, *leaf2;
1297 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1298 int count, totallen, max, space, swap;
1299
1300 /*
1301 * Set up environment.
1302 */
1303 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1304 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1305 leaf1 = blk1->bp->data;
1306 leaf2 = blk2->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001307 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1308 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 args = state->args;
1310
1311 /*
1312 * Check ordering of blocks, reverse if it makes things simpler.
1313 *
1314 * NOTE: Given that all (current) callers pass in an empty
1315 * second block, this code should never set "swap".
1316 */
1317 swap = 0;
1318 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1319 tmp_blk = blk1;
1320 blk1 = blk2;
1321 blk2 = tmp_blk;
1322 leaf1 = blk1->bp->data;
1323 leaf2 = blk2->bp->data;
1324 swap = 1;
1325 }
1326 hdr1 = &leaf1->hdr;
1327 hdr2 = &leaf2->hdr;
1328
1329 /*
1330 * Examine entries until we reduce the absolute difference in
1331 * byte usage between the two blocks to a minimum. Then get
1332 * the direction to copy and the number of elements to move.
1333 *
1334 * "inleaf" is true if the new entry should be inserted into blk1.
1335 * If "swap" is also true, then reverse the sense of "inleaf".
1336 */
1337 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1338 &count, &totallen);
1339 if (swap)
1340 state->inleaf = !state->inleaf;
1341
1342 /*
1343 * Move any entries required from leaf to leaf:
1344 */
Nathan Scott918ae422006-03-17 17:28:54 +11001345 if (count < be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 /*
1347 * Figure the total bytes to be added to the destination leaf.
1348 */
1349 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001350 count = be16_to_cpu(hdr1->count) - count;
1351 space = be16_to_cpu(hdr1->usedbytes) - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 space += count * sizeof(xfs_attr_leaf_entry_t);
1353
1354 /*
1355 * leaf2 is the destination, compact it if it looks tight.
1356 */
Nathan Scott918ae422006-03-17 17:28:54 +11001357 max = be16_to_cpu(hdr2->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001359 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (space > max) {
1361 xfs_attr_leaf_compact(args->trans, blk2->bp);
1362 }
1363
1364 /*
1365 * Move high entries from leaf1 to low end of leaf2.
1366 */
Nathan Scott918ae422006-03-17 17:28:54 +11001367 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 leaf2, 0, count, state->mp);
1369
1370 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1371 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Nathan Scott918ae422006-03-17 17:28:54 +11001372 } else if (count > be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /*
1374 * I assert that since all callers pass in an empty
1375 * second buffer, this code should never execute.
1376 */
1377
1378 /*
1379 * Figure the total bytes to be added to the destination leaf.
1380 */
1381 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001382 count -= be16_to_cpu(hdr1->count);
1383 space = totallen - be16_to_cpu(hdr1->usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 space += count * sizeof(xfs_attr_leaf_entry_t);
1385
1386 /*
1387 * leaf1 is the destination, compact it if it looks tight.
1388 */
Nathan Scott918ae422006-03-17 17:28:54 +11001389 max = be16_to_cpu(hdr1->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001391 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (space > max) {
1393 xfs_attr_leaf_compact(args->trans, blk1->bp);
1394 }
1395
1396 /*
1397 * Move low entries from leaf2 to high end of leaf1.
1398 */
1399 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
Nathan Scott918ae422006-03-17 17:28:54 +11001400 be16_to_cpu(hdr1->count), count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1403 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1404 }
1405
1406 /*
1407 * Copy out last hashval in each block for B-tree code.
1408 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001409 blk1->hashval = be32_to_cpu(
1410 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1411 blk2->hashval = be32_to_cpu(
1412 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 /*
1415 * Adjust the expected index for insertion.
1416 * NOTE: this code depends on the (current) situation that the
1417 * second block was originally empty.
1418 *
1419 * If the insertion point moved to the 2nd block, we must adjust
1420 * the index. We must also track the entry just following the
1421 * new entry for use in an "atomic rename" operation, that entry
1422 * is always the "old" entry and the "new" entry is what we are
1423 * inserting. The index/blkno fields refer to the "old" entry,
1424 * while the index2/blkno2 fields refer to the "new" entry.
1425 */
Nathan Scott918ae422006-03-17 17:28:54 +11001426 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ASSERT(state->inleaf == 0);
Nathan Scott918ae422006-03-17 17:28:54 +11001428 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 args->index = args->index2 = blk2->index;
1430 args->blkno = args->blkno2 = blk2->blkno;
Nathan Scott918ae422006-03-17 17:28:54 +11001431 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (state->inleaf) {
1433 args->index = blk1->index;
1434 args->blkno = blk1->blkno;
1435 args->index2 = 0;
1436 args->blkno2 = blk2->blkno;
1437 } else {
1438 blk2->index = blk1->index
Nathan Scott918ae422006-03-17 17:28:54 +11001439 - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 args->index = args->index2 = blk2->index;
1441 args->blkno = args->blkno2 = blk2->blkno;
1442 }
1443 } else {
1444 ASSERT(state->inleaf == 1);
1445 args->index = args->index2 = blk1->index;
1446 args->blkno = args->blkno2 = blk1->blkno;
1447 }
1448}
1449
1450/*
1451 * Examine entries until we reduce the absolute difference in
1452 * byte usage between the two blocks to a minimum.
1453 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1454 * GROT: there will always be enough room in either block for a new entry.
1455 * GROT: Do a double-split for this case?
1456 */
1457STATIC int
1458xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1459 xfs_da_state_blk_t *blk1,
1460 xfs_da_state_blk_t *blk2,
1461 int *countarg, int *usedbytesarg)
1462{
1463 xfs_attr_leafblock_t *leaf1, *leaf2;
1464 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1465 xfs_attr_leaf_entry_t *entry;
1466 int count, max, index, totallen, half;
1467 int lastdelta, foundit, tmp;
1468
1469 /*
1470 * Set up environment.
1471 */
1472 leaf1 = blk1->bp->data;
1473 leaf2 = blk2->bp->data;
1474 hdr1 = &leaf1->hdr;
1475 hdr2 = &leaf2->hdr;
1476 foundit = 0;
1477 totallen = 0;
1478
1479 /*
1480 * Examine entries until we reduce the absolute difference in
1481 * byte usage between the two blocks to a minimum.
1482 */
Nathan Scott918ae422006-03-17 17:28:54 +11001483 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 half = (max+1) * sizeof(*entry);
Nathan Scott918ae422006-03-17 17:28:54 +11001485 half += be16_to_cpu(hdr1->usedbytes) +
1486 be16_to_cpu(hdr2->usedbytes) +
1487 xfs_attr_leaf_newentsize(
1488 state->args->namelen,
1489 state->args->valuelen,
1490 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 half /= 2;
1492 lastdelta = state->blocksize;
1493 entry = &leaf1->entries[0];
1494 for (count = index = 0; count < max; entry++, index++, count++) {
1495
1496#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1497 /*
1498 * The new entry is in the first block, account for it.
1499 */
1500 if (count == blk1->index) {
1501 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001502 xfs_attr_leaf_newentsize(
1503 state->args->namelen,
1504 state->args->valuelen,
1505 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1507 break;
1508 lastdelta = XFS_ATTR_ABS(half - tmp);
1509 totallen = tmp;
1510 foundit = 1;
1511 }
1512
1513 /*
1514 * Wrap around into the second block if necessary.
1515 */
Nathan Scott918ae422006-03-17 17:28:54 +11001516 if (count == be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 leaf1 = leaf2;
1518 entry = &leaf1->entries[0];
1519 index = 0;
1520 }
1521
1522 /*
1523 * Figure out if next leaf entry would be too much.
1524 */
1525 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1526 index);
1527 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1528 break;
1529 lastdelta = XFS_ATTR_ABS(half - tmp);
1530 totallen = tmp;
1531#undef XFS_ATTR_ABS
1532 }
1533
1534 /*
1535 * Calculate the number of usedbytes that will end up in lower block.
1536 * If new entry not in lower block, fix up the count.
1537 */
1538 totallen -= count * sizeof(*entry);
1539 if (foundit) {
1540 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001541 xfs_attr_leaf_newentsize(
1542 state->args->namelen,
1543 state->args->valuelen,
1544 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
1547 *countarg = count;
1548 *usedbytesarg = totallen;
1549 return(foundit);
1550}
1551
1552/*========================================================================
1553 * Routines used for shrinking the Btree.
1554 *========================================================================*/
1555
1556/*
1557 * Check a leaf block and its neighbors to see if the block should be
1558 * collapsed into one or the other neighbor. Always keep the block
1559 * with the smaller block number.
1560 * If the current block is over 50% full, don't try to join it, return 0.
1561 * If the block is empty, fill in the state structure and return 2.
1562 * If it can be collapsed, fill in the state structure and return 1.
1563 * If nothing can be done, return 0.
1564 *
1565 * GROT: allow for INCOMPLETE entries in calculation.
1566 */
1567int
1568xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1569{
1570 xfs_attr_leafblock_t *leaf;
1571 xfs_da_state_blk_t *blk;
1572 xfs_da_blkinfo_t *info;
1573 int count, bytes, forward, error, retval, i;
1574 xfs_dablk_t blkno;
1575 xfs_dabuf_t *bp;
1576
1577 /*
1578 * Check for the degenerate case of the block being over 50% full.
1579 * If so, it's not worth even looking to see if we might be able
1580 * to coalesce with a sibling.
1581 */
1582 blk = &state->path.blk[ state->path.active-1 ];
1583 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001584 ASSERT(be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001586 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1588 count * sizeof(xfs_attr_leaf_entry_t) +
Nathan Scott918ae422006-03-17 17:28:54 +11001589 be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (bytes > (state->blocksize >> 1)) {
1591 *action = 0; /* blk over 50%, don't try to join */
1592 return(0);
1593 }
1594
1595 /*
1596 * Check for the degenerate case of the block being empty.
1597 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001598 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 * to merge with the forward block unless it is NULL.
1600 */
1601 if (count == 0) {
1602 /*
1603 * Make altpath point to the block we want to keep and
1604 * path point to the block we want to drop (this one).
1605 */
Nathan Scott89da0542006-03-17 17:28:40 +11001606 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 memcpy(&state->altpath, &state->path, sizeof(state->path));
1608 error = xfs_da_path_shift(state, &state->altpath, forward,
1609 0, &retval);
1610 if (error)
1611 return(error);
1612 if (retval) {
1613 *action = 0;
1614 } else {
1615 *action = 2;
1616 }
1617 return(0);
1618 }
1619
1620 /*
1621 * Examine each sibling block to see if we can coalesce with
1622 * at least 25% free space to spare. We need to figure out
1623 * whether to merge with the forward or the backward block.
1624 * We prefer coalescing with the lower numbered sibling so as
1625 * to shrink an attribute list over time.
1626 */
1627 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +11001628 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 for (i = 0; i < 2; forward = !forward, i++) {
1630 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +11001631 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 else
Nathan Scott89da0542006-03-17 17:28:40 +11001633 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (blkno == 0)
1635 continue;
1636 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1637 blkno, -1, &bp, XFS_ATTR_FORK);
1638 if (error)
1639 return(error);
1640 ASSERT(bp != NULL);
1641
1642 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001643 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 bytes = state->blocksize - (state->blocksize>>2);
Nathan Scott918ae422006-03-17 17:28:54 +11001645 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001647 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11001648 count += be16_to_cpu(leaf->hdr.count);
1649 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1651 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1652 xfs_da_brelse(state->args->trans, bp);
1653 if (bytes >= 0)
1654 break; /* fits with at least 25% to spare */
1655 }
1656 if (i >= 2) {
1657 *action = 0;
1658 return(0);
1659 }
1660
1661 /*
1662 * Make altpath point to the block we want to keep (the lower
1663 * numbered block) and path point to the block we want to drop.
1664 */
1665 memcpy(&state->altpath, &state->path, sizeof(state->path));
1666 if (blkno < blk->blkno) {
1667 error = xfs_da_path_shift(state, &state->altpath, forward,
1668 0, &retval);
1669 } else {
1670 error = xfs_da_path_shift(state, &state->path, forward,
1671 0, &retval);
1672 }
1673 if (error)
1674 return(error);
1675 if (retval) {
1676 *action = 0;
1677 } else {
1678 *action = 1;
1679 }
1680 return(0);
1681}
1682
1683/*
1684 * Remove a name from the leaf attribute list structure.
1685 *
1686 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1687 * If two leaves are 37% full, when combined they will leave 25% free.
1688 */
1689int
1690xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1691{
1692 xfs_attr_leafblock_t *leaf;
1693 xfs_attr_leaf_hdr_t *hdr;
1694 xfs_attr_leaf_map_t *map;
1695 xfs_attr_leaf_entry_t *entry;
1696 int before, after, smallest, entsize;
1697 int tablesize, tmp, i;
1698 xfs_mount_t *mp;
1699
1700 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001701 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 hdr = &leaf->hdr;
1703 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001704 ASSERT((be16_to_cpu(hdr->count) > 0)
1705 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001707 && (args->index < be16_to_cpu(hdr->count)));
1708 ASSERT(be16_to_cpu(hdr->firstused) >=
1709 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 entry = &leaf->entries[args->index];
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001711 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1712 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 /*
1715 * Scan through free region table:
1716 * check for adjacency of free'd entry with an existing one,
1717 * find smallest free region in case we need to replace it,
1718 * adjust any map that borders the entry table,
1719 */
Nathan Scott918ae422006-03-17 17:28:54 +11001720 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 + sizeof(xfs_attr_leaf_hdr_t);
1722 map = &hdr->freemap[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001723 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 before = after = -1;
1725 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1726 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1727 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001728 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1729 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1730 if (be16_to_cpu(map->base) == tablesize) {
1731 be16_add(&map->base,
1732 -((int)sizeof(xfs_attr_leaf_entry_t)));
1733 be16_add(&map->size, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735
Nathan Scott918ae422006-03-17 17:28:54 +11001736 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001737 == be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 before = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001739 } else if (be16_to_cpu(map->base)
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001740 == (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 after = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001742 } else if (be16_to_cpu(map->size) < tmp) {
1743 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 smallest = i;
1745 }
1746 }
1747
1748 /*
1749 * Coalesce adjacent freemap regions,
1750 * or replace the smallest region.
1751 */
1752 if ((before >= 0) || (after >= 0)) {
1753 if ((before >= 0) && (after >= 0)) {
1754 map = &hdr->freemap[before];
Nathan Scott918ae422006-03-17 17:28:54 +11001755 be16_add(&map->size, entsize);
1756 be16_add(&map->size,
1757 be16_to_cpu(hdr->freemap[after].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 hdr->freemap[after].base = 0;
1759 hdr->freemap[after].size = 0;
1760 } else if (before >= 0) {
1761 map = &hdr->freemap[before];
Nathan Scott918ae422006-03-17 17:28:54 +11001762 be16_add(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 } else {
1764 map = &hdr->freemap[after];
1765 /* both on-disk, don't endian flip twice */
1766 map->base = entry->nameidx;
Nathan Scott918ae422006-03-17 17:28:54 +11001767 be16_add(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769 } else {
1770 /*
1771 * Replace smallest region (if it is smaller than free'd entry)
1772 */
1773 map = &hdr->freemap[smallest];
Nathan Scott918ae422006-03-17 17:28:54 +11001774 if (be16_to_cpu(map->size) < entsize) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001775 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
Nathan Scott918ae422006-03-17 17:28:54 +11001776 map->size = cpu_to_be16(entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778 }
1779
1780 /*
1781 * Did we remove the first entry?
1782 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001783 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 smallest = 1;
1785 else
1786 smallest = 0;
1787
1788 /*
1789 * Compress the remaining entries and zero out the removed stuff.
1790 */
1791 memset(XFS_ATTR_LEAF_NAME(leaf, args->index), 0, entsize);
Nathan Scott918ae422006-03-17 17:28:54 +11001792 be16_add(&hdr->usedbytes, -entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 xfs_da_log_buf(args->trans, bp,
1794 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1795 entsize));
1796
Nathan Scott918ae422006-03-17 17:28:54 +11001797 tmp = (be16_to_cpu(hdr->count) - args->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 * sizeof(xfs_attr_leaf_entry_t);
1799 memmove((char *)entry, (char *)(entry+1), tmp);
Nathan Scott918ae422006-03-17 17:28:54 +11001800 be16_add(&hdr->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 xfs_da_log_buf(args->trans, bp,
1802 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
Nathan Scott918ae422006-03-17 17:28:54 +11001803 entry = &leaf->entries[be16_to_cpu(hdr->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1805
1806 /*
1807 * If we removed the first entry, re-find the first used byte
1808 * in the name area. Note that if the entry was the "firstused",
1809 * then we don't have a "hole" in our block resulting from
1810 * removing the name.
1811 */
1812 if (smallest) {
1813 tmp = XFS_LBSIZE(mp);
1814 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001815 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001816 ASSERT(be16_to_cpu(entry->nameidx) >=
1817 be16_to_cpu(hdr->firstused));
1818 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1819
1820 if (be16_to_cpu(entry->nameidx) < tmp)
1821 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
Nathan Scott918ae422006-03-17 17:28:54 +11001823 hdr->firstused = cpu_to_be16(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001825 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1827 }
1828 } else {
1829 hdr->holes = 1; /* mark as needing compaction */
1830 }
1831 xfs_da_log_buf(args->trans, bp,
1832 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1833
1834 /*
1835 * Check if leaf is less than 50% full, caller may want to
1836 * "join" the leaf with a sibling if so.
1837 */
1838 tmp = sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001839 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1840 tmp += be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1842}
1843
1844/*
1845 * Move all the attribute list entries from drop_leaf into save_leaf.
1846 */
1847void
1848xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1849 xfs_da_state_blk_t *save_blk)
1850{
1851 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1852 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1853 xfs_mount_t *mp;
1854 char *tmpbuffer;
1855
1856 /*
1857 * Set up environment.
1858 */
1859 mp = state->mp;
1860 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1861 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1862 drop_leaf = drop_blk->bp->data;
1863 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001864 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1865 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 drop_hdr = &drop_leaf->hdr;
1867 save_hdr = &save_leaf->hdr;
1868
1869 /*
1870 * Save last hashval from dying block for later Btree fixup.
1871 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001872 drop_blk->hashval = be32_to_cpu(
1873 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 /*
1876 * Check if we need a temp buffer, or can we do it in place.
1877 * Note that we don't check "leaf" for holes because we will
1878 * always be dropping it, toosmall() decided that for us already.
1879 */
1880 if (save_hdr->holes == 0) {
1881 /*
1882 * dest leaf has no holes, so we add there. May need
1883 * to make some room in the entry array.
1884 */
1885 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1886 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001887 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 } else {
1889 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001890 be16_to_cpu(save_hdr->count),
1891 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893 } else {
1894 /*
1895 * Destination has holes, so we make a temporary copy
1896 * of the leaf and add them both to that.
1897 */
1898 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1899 ASSERT(tmpbuffer != NULL);
1900 memset(tmpbuffer, 0, state->blocksize);
1901 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1902 tmp_hdr = &tmp_leaf->hdr;
1903 tmp_hdr->info = save_hdr->info; /* struct copy */
1904 tmp_hdr->count = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001905 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (!tmp_hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001907 tmp_hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1909 }
1910 tmp_hdr->usedbytes = 0;
1911 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1912 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001913 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001915 be16_to_cpu(tmp_leaf->hdr.count),
1916 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 } else {
1918 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001919 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001921 be16_to_cpu(tmp_leaf->hdr.count),
1922 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1925 kmem_free(tmpbuffer, state->blocksize);
1926 }
1927
1928 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1929 state->blocksize - 1);
1930
1931 /*
1932 * Copy out last hashval in each block for B-tree code.
1933 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001934 save_blk->hashval = be32_to_cpu(
1935 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
1938/*========================================================================
1939 * Routines used for finding things in the Btree.
1940 *========================================================================*/
1941
1942/*
1943 * Look up a name in a leaf attribute list structure.
1944 * This is the internal routine, it uses the caller's buffer.
1945 *
1946 * Note that duplicate keys are allowed, but only check within the
1947 * current leaf node. The Btree code must check in adjacent leaf nodes.
1948 *
1949 * Return in args->index the index into the entry[] array of either
1950 * the found entry, or where the entry should have been (insert before
1951 * that entry).
1952 *
1953 * Don't change the args->value unless we find the attribute.
1954 */
1955int
1956xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1957{
1958 xfs_attr_leafblock_t *leaf;
1959 xfs_attr_leaf_entry_t *entry;
1960 xfs_attr_leaf_name_local_t *name_loc;
1961 xfs_attr_leaf_name_remote_t *name_rmt;
1962 int probe, span;
1963 xfs_dahash_t hashval;
1964
1965 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001966 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11001967 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 < (XFS_LBSIZE(args->dp->i_mount)/8));
1969
1970 /*
1971 * Binary search. (note: small blocks will skip this loop)
1972 */
1973 hashval = args->hashval;
Nathan Scott918ae422006-03-17 17:28:54 +11001974 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 for (entry = &leaf->entries[probe]; span > 4;
1976 entry = &leaf->entries[probe]) {
1977 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001978 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001980 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 probe -= span;
1982 else
1983 break;
1984 }
Tim Shimmin726801b2006-09-28 11:01:37 +10001985 ASSERT((probe >= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 (!leaf->hdr.count
Nathan Scott918ae422006-03-17 17:28:54 +11001987 || (probe < be16_to_cpu(leaf->hdr.count))));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001988 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /*
1991 * Since we may have duplicate hashval's, find the first matching
1992 * hashval in the leaf.
1993 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001994 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 entry--;
1996 probe--;
1997 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001998 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
1999 (be32_to_cpu(entry->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 entry++;
2001 probe++;
2002 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002003 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
2004 (be32_to_cpu(entry->hashval) != hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 args->index = probe;
2006 return(XFS_ERROR(ENOATTR));
2007 }
2008
2009 /*
2010 * Duplicate keys may be present, so search all of them for a match.
2011 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002012 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
2013 (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 entry++, probe++) {
2015/*
2016 * GROT: Add code to remove incomplete entries.
2017 */
2018 /*
2019 * If we are looking for INCOMPLETE entries, show only those.
2020 * If we are looking for complete entries, show only those.
2021 */
2022 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2023 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2024 continue;
2025 }
2026 if (entry->flags & XFS_ATTR_LOCAL) {
2027 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, probe);
2028 if (name_loc->namelen != args->namelen)
2029 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002030 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002032 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 continue;
2034 args->index = probe;
2035 return(XFS_ERROR(EEXIST));
2036 } else {
2037 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, probe);
2038 if (name_rmt->namelen != args->namelen)
2039 continue;
2040 if (memcmp(args->name, (char *)name_rmt->name,
2041 args->namelen) != 0)
2042 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002043 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 continue;
2045 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002046 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002048 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return(XFS_ERROR(EEXIST));
2050 }
2051 }
2052 args->index = probe;
2053 return(XFS_ERROR(ENOATTR));
2054}
2055
2056/*
2057 * Get the value associated with an attribute name from a leaf attribute
2058 * list structure.
2059 */
2060int
2061xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2062{
2063 int valuelen;
2064 xfs_attr_leafblock_t *leaf;
2065 xfs_attr_leaf_entry_t *entry;
2066 xfs_attr_leaf_name_local_t *name_loc;
2067 xfs_attr_leaf_name_remote_t *name_rmt;
2068
2069 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002070 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002071 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 < (XFS_LBSIZE(args->dp->i_mount)/8));
Nathan Scott918ae422006-03-17 17:28:54 +11002073 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 entry = &leaf->entries[args->index];
2076 if (entry->flags & XFS_ATTR_LOCAL) {
2077 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2078 ASSERT(name_loc->namelen == args->namelen);
2079 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002080 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (args->flags & ATTR_KERNOVAL) {
2082 args->valuelen = valuelen;
2083 return(0);
2084 }
2085 if (args->valuelen < valuelen) {
2086 args->valuelen = valuelen;
2087 return(XFS_ERROR(ERANGE));
2088 }
2089 args->valuelen = valuelen;
2090 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2091 } else {
2092 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2093 ASSERT(name_rmt->namelen == args->namelen);
2094 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002095 valuelen = be32_to_cpu(name_rmt->valuelen);
2096 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2098 if (args->flags & ATTR_KERNOVAL) {
2099 args->valuelen = valuelen;
2100 return(0);
2101 }
2102 if (args->valuelen < valuelen) {
2103 args->valuelen = valuelen;
2104 return(XFS_ERROR(ERANGE));
2105 }
2106 args->valuelen = valuelen;
2107 }
2108 return(0);
2109}
2110
2111/*========================================================================
2112 * Utility routines.
2113 *========================================================================*/
2114
2115/*
2116 * Move the indicated entries from one leaf to another.
2117 * NOTE: this routine modifies both source and destination leaves.
2118 */
2119/*ARGSUSED*/
2120STATIC void
2121xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2122 xfs_attr_leafblock_t *leaf_d, int start_d,
2123 int count, xfs_mount_t *mp)
2124{
2125 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2126 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2127 int desti, tmp, i;
2128
2129 /*
2130 * Check for nothing to do.
2131 */
2132 if (count == 0)
2133 return;
2134
2135 /*
2136 * Set up environment.
2137 */
Nathan Scott89da0542006-03-17 17:28:40 +11002138 ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2139 ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 hdr_s = &leaf_s->hdr;
2141 hdr_d = &leaf_d->hdr;
Nathan Scott918ae422006-03-17 17:28:54 +11002142 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2143 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2144 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2145 ((be16_to_cpu(hdr_s->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 * sizeof(*entry_s))+sizeof(*hdr_s)));
Nathan Scott918ae422006-03-17 17:28:54 +11002147 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2148 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2149 ((be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 * sizeof(*entry_d))+sizeof(*hdr_d)));
2151
Nathan Scott918ae422006-03-17 17:28:54 +11002152 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2153 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2154 ASSERT(count <= be16_to_cpu(hdr_s->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /*
2157 * Move the entries in the destination leaf up to make a hole?
2158 */
Nathan Scott918ae422006-03-17 17:28:54 +11002159 if (start_d < be16_to_cpu(hdr_d->count)) {
2160 tmp = be16_to_cpu(hdr_d->count) - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 tmp *= sizeof(xfs_attr_leaf_entry_t);
2162 entry_s = &leaf_d->entries[start_d];
2163 entry_d = &leaf_d->entries[start_d + count];
2164 memmove((char *)entry_d, (char *)entry_s, tmp);
2165 }
2166
2167 /*
2168 * Copy all entry's in the same (sorted) order,
2169 * but allocate attribute info packed and in sequence.
2170 */
2171 entry_s = &leaf_s->entries[start_s];
2172 entry_d = &leaf_d->entries[start_d];
2173 desti = start_d;
2174 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002175 ASSERT(be16_to_cpu(entry_s->nameidx)
Nathan Scott918ae422006-03-17 17:28:54 +11002176 >= be16_to_cpu(hdr_s->firstused));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2178#ifdef GROT
2179 /*
2180 * Code to drop INCOMPLETE entries. Difficult to use as we
2181 * may also need to change the insertion index. Code turned
2182 * off for 6.2, should be revisited later.
2183 */
2184 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2185 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
Nathan Scott918ae422006-03-17 17:28:54 +11002186 be16_add(&hdr_s->usedbytes, -tmp);
2187 be16_add(&hdr_s->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 entry_d--; /* to compensate for ++ in loop hdr */
2189 desti--;
2190 if ((start_s + i) < offset)
2191 result++; /* insertion index adjustment */
2192 } else {
2193#endif /* GROT */
Nathan Scott918ae422006-03-17 17:28:54 +11002194 be16_add(&hdr_d->firstused, -tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /* both on-disk, don't endian flip twice */
2196 entry_d->hashval = entry_s->hashval;
2197 /* both on-disk, don't endian flip twice */
2198 entry_d->nameidx = hdr_d->firstused;
2199 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002200 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 <= XFS_LBSIZE(mp));
2202 memmove(XFS_ATTR_LEAF_NAME(leaf_d, desti),
2203 XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002204 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 <= XFS_LBSIZE(mp));
2206 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
Nathan Scott918ae422006-03-17 17:28:54 +11002207 be16_add(&hdr_s->usedbytes, -tmp);
2208 be16_add(&hdr_d->usedbytes, tmp);
2209 be16_add(&hdr_s->count, -1);
2210 be16_add(&hdr_d->count, 1);
2211 tmp = be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 * sizeof(xfs_attr_leaf_entry_t)
2213 + sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002214 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215#ifdef GROT
2216 }
2217#endif /* GROT */
2218 }
2219
2220 /*
2221 * Zero out the entries we just copied.
2222 */
Nathan Scott918ae422006-03-17 17:28:54 +11002223 if (start_s == be16_to_cpu(hdr_s->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2225 entry_s = &leaf_s->entries[start_s];
2226 ASSERT(((char *)entry_s + tmp) <=
2227 ((char *)leaf_s + XFS_LBSIZE(mp)));
2228 memset((char *)entry_s, 0, tmp);
2229 } else {
2230 /*
2231 * Move the remaining entries down to fill the hole,
2232 * then zero the entries at the top.
2233 */
Nathan Scott918ae422006-03-17 17:28:54 +11002234 tmp = be16_to_cpu(hdr_s->count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 tmp *= sizeof(xfs_attr_leaf_entry_t);
2236 entry_s = &leaf_s->entries[start_s + count];
2237 entry_d = &leaf_s->entries[start_s];
2238 memmove((char *)entry_d, (char *)entry_s, tmp);
2239
2240 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002241 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 ASSERT(((char *)entry_s + tmp) <=
2243 ((char *)leaf_s + XFS_LBSIZE(mp)));
2244 memset((char *)entry_s, 0, tmp);
2245 }
2246
2247 /*
2248 * Fill in the freemap information
2249 */
Nathan Scott918ae422006-03-17 17:28:54 +11002250 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
2251 be16_add(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
2252 sizeof(xfs_attr_leaf_entry_t));
2253 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2254 - be16_to_cpu(hdr_d->freemap[0].base));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 hdr_d->freemap[1].base = 0;
2256 hdr_d->freemap[2].base = 0;
2257 hdr_d->freemap[1].size = 0;
2258 hdr_d->freemap[2].size = 0;
2259 hdr_s->holes = 1; /* leaf may not be compact */
2260}
2261
2262/*
2263 * Compare two leaf blocks "order".
2264 * Return 0 unless leaf2 should go before leaf1.
2265 */
2266int
2267xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2268{
2269 xfs_attr_leafblock_t *leaf1, *leaf2;
2270
2271 leaf1 = leaf1_bp->data;
2272 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002273 ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC) &&
2274 (be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002275 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2276 (be16_to_cpu(leaf2->hdr.count) > 0) &&
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002277 ((be32_to_cpu(leaf2->entries[0].hashval) <
2278 be32_to_cpu(leaf1->entries[0].hashval)) ||
2279 (be32_to_cpu(leaf2->entries[
2280 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2281 be32_to_cpu(leaf1->entries[
2282 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return(1);
2284 }
2285 return(0);
2286}
2287
2288/*
2289 * Pick up the last hashvalue from a leaf block.
2290 */
2291xfs_dahash_t
2292xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2293{
2294 xfs_attr_leafblock_t *leaf;
2295
2296 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002297 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (count)
Nathan Scott918ae422006-03-17 17:28:54 +11002299 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (!leaf->hdr.count)
2301 return(0);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002302 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
2305/*
2306 * Calculate the number of bytes used to store the indicated attribute
2307 * (whether local or remote only calculate bytes in this block).
2308 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002309STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2311{
2312 xfs_attr_leaf_name_local_t *name_loc;
2313 xfs_attr_leaf_name_remote_t *name_rmt;
2314 int size;
2315
Nathan Scott89da0542006-03-17 17:28:40 +11002316 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2318 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, index);
2319 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002320 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 } else {
2322 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, index);
2323 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(name_rmt->namelen);
2324 }
2325 return(size);
2326}
2327
2328/*
2329 * Calculate the number of bytes that would be required to store the new
2330 * attribute (whether local or remote only calculate bytes in this block).
2331 * This routine decides as a side effect whether the attribute will be
2332 * a "local" or a "remote" attribute.
2333 */
2334int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002335xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 int size;
2338
Nathan Scottaa82daa2005-11-02 10:33:33 +11002339 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(namelen, valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (size < XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(blocksize)) {
2341 if (local) {
2342 *local = 1;
2343 }
2344 } else {
Nathan Scottaa82daa2005-11-02 10:33:33 +11002345 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (local) {
2347 *local = 0;
2348 }
2349 }
2350 return(size);
2351}
2352
2353/*
2354 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2355 */
2356int
2357xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2358{
2359 attrlist_cursor_kern_t *cursor;
2360 xfs_attr_leafblock_t *leaf;
2361 xfs_attr_leaf_entry_t *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 int retval, i;
2363
2364 ASSERT(bp != NULL);
2365 leaf = bp->data;
2366 cursor = context->cursor;
2367 cursor->initted = 1;
2368
2369 xfs_attr_trace_l_cl("blk start", context, leaf);
2370
2371 /*
2372 * Re-find our place in the leaf block if this is a new syscall.
2373 */
2374 if (context->resynch) {
2375 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002376 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002377 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (cursor->offset == context->dupcnt) {
2379 context->dupcnt = 0;
2380 break;
2381 }
2382 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002383 } else if (be32_to_cpu(entry->hashval) >
2384 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 context->dupcnt = 0;
2386 break;
2387 }
2388 }
Nathan Scott918ae422006-03-17 17:28:54 +11002389 if (i == be16_to_cpu(leaf->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 xfs_attr_trace_l_c("not found", context);
2391 return(0);
2392 }
2393 } else {
2394 entry = &leaf->entries[0];
2395 i = 0;
2396 }
2397 context->resynch = 0;
2398
2399 /*
2400 * We have found our place, start copying out the new attributes.
2401 */
2402 retval = 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10002403 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
2404 attrnames_t *namesp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002406 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2407 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 cursor->offset = 0;
2409 }
2410
2411 if (entry->flags & XFS_ATTR_INCOMPLETE)
2412 continue; /* skip incomplete entries */
Tim Shimmin726801b2006-09-28 11:01:37 +10002413 if (!xfs_attr_namesp_match_overrides(context->flags, entry->flags))
2414 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Tim Shimmin726801b2006-09-28 11:01:37 +10002416 namesp = xfs_attr_flags_namesp(entry->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002419 xfs_attr_leaf_name_local_t *name_loc =
2420 XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
2421
2422 retval = context->put_listent(context,
2423 namesp,
2424 (char *)name_loc->nameval,
2425 (int)name_loc->namelen,
2426 be16_to_cpu(name_loc->valuelen),
2427 (char *)&name_loc->nameval[name_loc->namelen]);
2428 if (retval)
2429 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002431 xfs_attr_leaf_name_remote_t *name_rmt =
2432 XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2433
2434 int valuelen = be32_to_cpu(name_rmt->valuelen);
2435
2436 if (context->put_value) {
2437 xfs_da_args_t args;
2438
2439 memset((char *)&args, 0, sizeof(args));
2440 args.dp = context->dp;
2441 args.whichfork = XFS_ATTR_FORK;
2442 args.valuelen = valuelen;
2443 args.value = kmem_alloc(valuelen, KM_SLEEP);
2444 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2445 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2446 retval = xfs_attr_rmtval_get(&args);
2447 if (retval)
2448 return retval;
2449 retval = context->put_listent(context,
2450 namesp,
2451 (char *)name_rmt->name,
2452 (int)name_rmt->namelen,
2453 valuelen,
2454 (char*)args.value);
2455 kmem_free(args.value, valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002457 else {
2458 retval = context->put_listent(context,
2459 namesp,
2460 (char *)name_rmt->name,
2461 (int)name_rmt->namelen,
2462 valuelen,
2463 NULL);
2464 }
2465 if (retval)
2466 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002468 if (context->seen_enough)
2469 break;
2470 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472 xfs_attr_trace_l_cl("blk end", context, leaf);
2473 return(retval);
2474}
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477/*========================================================================
2478 * Manage the INCOMPLETE flag in a leaf entry
2479 *========================================================================*/
2480
2481/*
2482 * Clear the INCOMPLETE flag on an entry in a leaf block.
2483 */
2484int
2485xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2486{
2487 xfs_attr_leafblock_t *leaf;
2488 xfs_attr_leaf_entry_t *entry;
2489 xfs_attr_leaf_name_remote_t *name_rmt;
2490 xfs_dabuf_t *bp;
2491 int error;
2492#ifdef DEBUG
2493 xfs_attr_leaf_name_local_t *name_loc;
2494 int namelen;
2495 char *name;
2496#endif /* DEBUG */
2497
2498 /*
2499 * Set up the operation.
2500 */
2501 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2502 XFS_ATTR_FORK);
2503 if (error) {
2504 return(error);
2505 }
2506 ASSERT(bp != NULL);
2507
2508 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002509 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002510 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 ASSERT(args->index >= 0);
2512 entry = &leaf->entries[ args->index ];
2513 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2514
2515#ifdef DEBUG
2516 if (entry->flags & XFS_ATTR_LOCAL) {
2517 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2518 namelen = name_loc->namelen;
2519 name = (char *)name_loc->nameval;
2520 } else {
2521 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2522 namelen = name_rmt->namelen;
2523 name = (char *)name_rmt->name;
2524 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002525 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 ASSERT(namelen == args->namelen);
2527 ASSERT(memcmp(name, args->name, namelen) == 0);
2528#endif /* DEBUG */
2529
2530 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2531 xfs_da_log_buf(args->trans, bp,
2532 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2533
2534 if (args->rmtblkno) {
2535 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2536 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002537 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2538 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 xfs_da_log_buf(args->trans, bp,
2540 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2541 }
2542 xfs_da_buf_done(bp);
2543
2544 /*
2545 * Commit the flag value change and start the next trans in series.
2546 */
2547 error = xfs_attr_rolltrans(&args->trans, args->dp);
2548
2549 return(error);
2550}
2551
2552/*
2553 * Set the INCOMPLETE flag on an entry in a leaf block.
2554 */
2555int
2556xfs_attr_leaf_setflag(xfs_da_args_t *args)
2557{
2558 xfs_attr_leafblock_t *leaf;
2559 xfs_attr_leaf_entry_t *entry;
2560 xfs_attr_leaf_name_remote_t *name_rmt;
2561 xfs_dabuf_t *bp;
2562 int error;
2563
2564 /*
2565 * Set up the operation.
2566 */
2567 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2568 XFS_ATTR_FORK);
2569 if (error) {
2570 return(error);
2571 }
2572 ASSERT(bp != NULL);
2573
2574 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002575 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002576 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 ASSERT(args->index >= 0);
2578 entry = &leaf->entries[ args->index ];
2579
2580 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2581 entry->flags |= XFS_ATTR_INCOMPLETE;
2582 xfs_da_log_buf(args->trans, bp,
2583 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2584 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2585 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2586 name_rmt->valueblk = 0;
2587 name_rmt->valuelen = 0;
2588 xfs_da_log_buf(args->trans, bp,
2589 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2590 }
2591 xfs_da_buf_done(bp);
2592
2593 /*
2594 * Commit the flag value change and start the next trans in series.
2595 */
2596 error = xfs_attr_rolltrans(&args->trans, args->dp);
2597
2598 return(error);
2599}
2600
2601/*
2602 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2603 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2604 * entry given by args->blkno2/index2.
2605 *
2606 * Note that they could be in different blocks, or in the same block.
2607 */
2608int
2609xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2610{
2611 xfs_attr_leafblock_t *leaf1, *leaf2;
2612 xfs_attr_leaf_entry_t *entry1, *entry2;
2613 xfs_attr_leaf_name_remote_t *name_rmt;
2614 xfs_dabuf_t *bp1, *bp2;
2615 int error;
2616#ifdef DEBUG
2617 xfs_attr_leaf_name_local_t *name_loc;
2618 int namelen1, namelen2;
2619 char *name1, *name2;
2620#endif /* DEBUG */
2621
2622 /*
2623 * Read the block containing the "old" attr
2624 */
2625 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2626 XFS_ATTR_FORK);
2627 if (error) {
2628 return(error);
2629 }
2630 ASSERT(bp1 != NULL);
2631
2632 /*
2633 * Read the block containing the "new" attr, if it is different
2634 */
2635 if (args->blkno2 != args->blkno) {
2636 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2637 -1, &bp2, XFS_ATTR_FORK);
2638 if (error) {
2639 return(error);
2640 }
2641 ASSERT(bp2 != NULL);
2642 } else {
2643 bp2 = bp1;
2644 }
2645
2646 leaf1 = bp1->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002647 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002648 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 ASSERT(args->index >= 0);
2650 entry1 = &leaf1->entries[ args->index ];
2651
2652 leaf2 = bp2->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002653 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002654 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 ASSERT(args->index2 >= 0);
2656 entry2 = &leaf2->entries[ args->index2 ];
2657
2658#ifdef DEBUG
2659 if (entry1->flags & XFS_ATTR_LOCAL) {
2660 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf1, args->index);
2661 namelen1 = name_loc->namelen;
2662 name1 = (char *)name_loc->nameval;
2663 } else {
2664 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2665 namelen1 = name_rmt->namelen;
2666 name1 = (char *)name_rmt->name;
2667 }
2668 if (entry2->flags & XFS_ATTR_LOCAL) {
2669 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf2, args->index2);
2670 namelen2 = name_loc->namelen;
2671 name2 = (char *)name_loc->nameval;
2672 } else {
2673 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2674 namelen2 = name_rmt->namelen;
2675 name2 = (char *)name_rmt->name;
2676 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002677 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 ASSERT(namelen1 == namelen2);
2679 ASSERT(memcmp(name1, name2, namelen1) == 0);
2680#endif /* DEBUG */
2681
2682 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2683 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2684
2685 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2686 xfs_da_log_buf(args->trans, bp1,
2687 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2688 if (args->rmtblkno) {
2689 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2690 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002691 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2692 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 xfs_da_log_buf(args->trans, bp1,
2694 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2695 }
2696
2697 entry2->flags |= XFS_ATTR_INCOMPLETE;
2698 xfs_da_log_buf(args->trans, bp2,
2699 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2700 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2701 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2702 name_rmt->valueblk = 0;
2703 name_rmt->valuelen = 0;
2704 xfs_da_log_buf(args->trans, bp2,
2705 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2706 }
2707 xfs_da_buf_done(bp1);
2708 if (bp1 != bp2)
2709 xfs_da_buf_done(bp2);
2710
2711 /*
2712 * Commit the flag value change and start the next trans in series.
2713 */
2714 error = xfs_attr_rolltrans(&args->trans, args->dp);
2715
2716 return(error);
2717}
2718
2719/*========================================================================
2720 * Indiscriminately delete the entire attribute fork
2721 *========================================================================*/
2722
2723/*
2724 * Recurse (gasp!) through the attribute nodes until we find leaves.
2725 * We're doing a depth-first traversal in order to invalidate everything.
2726 */
2727int
2728xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2729{
2730 xfs_da_blkinfo_t *info;
2731 xfs_daddr_t blkno;
2732 xfs_dabuf_t *bp;
2733 int error;
2734
2735 /*
2736 * Read block 0 to see what we have to work with.
2737 * We only get here if we have extents, since we remove
2738 * the extents in reverse order the extent containing
2739 * block 0 must still be there.
2740 */
2741 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2742 if (error)
2743 return(error);
2744 blkno = xfs_da_blkno(bp);
2745
2746 /*
2747 * Invalidate the tree, even if the "tree" is only a single leaf block.
2748 * This is a depth-first traversal!
2749 */
2750 info = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002751 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 error = xfs_attr_node_inactive(trans, dp, bp, 1);
Nathan Scott89da0542006-03-17 17:28:40 +11002753 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 error = xfs_attr_leaf_inactive(trans, dp, bp);
2755 } else {
2756 error = XFS_ERROR(EIO);
2757 xfs_da_brelse(*trans, bp);
2758 }
2759 if (error)
2760 return(error);
2761
2762 /*
2763 * Invalidate the incore copy of the root block.
2764 */
2765 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2766 if (error)
2767 return(error);
2768 xfs_da_binval(*trans, bp); /* remove from cache */
2769 /*
2770 * Commit the invalidate and start the next transaction.
2771 */
2772 error = xfs_attr_rolltrans(trans, dp);
2773
2774 return (error);
2775}
2776
2777/*
2778 * Recurse (gasp!) through the attribute nodes until we find leaves.
2779 * We're doing a depth-first traversal in order to invalidate everything.
2780 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002781STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2783 int level)
2784{
2785 xfs_da_blkinfo_t *info;
2786 xfs_da_intnode_t *node;
2787 xfs_dablk_t child_fsb;
2788 xfs_daddr_t parent_blkno, child_blkno;
2789 int error, count, i;
2790 xfs_dabuf_t *child_bp;
2791
2792 /*
2793 * Since this code is recursive (gasp!) we must protect ourselves.
2794 */
2795 if (level > XFS_DA_NODE_MAXDEPTH) {
2796 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2797 return(XFS_ERROR(EIO));
2798 }
2799
2800 node = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002801 ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
Nathan Scottfac80cc2006-03-17 17:29:56 +11002803 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 if (!count) {
2805 xfs_da_brelse(*trans, bp);
2806 return(0);
2807 }
Nathan Scott403432d2006-03-17 17:29:46 +11002808 child_fsb = be32_to_cpu(node->btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2810
2811 /*
2812 * If this is the node level just above the leaves, simply loop
2813 * over the leaves removing all of them. If this is higher up
2814 * in the tree, recurse downward.
2815 */
2816 for (i = 0; i < count; i++) {
2817 /*
2818 * Read the subsidiary block to see what we have to work with.
2819 * Don't do this in a transaction. This is a depth-first
2820 * traversal of the tree so we may deal with many blocks
2821 * before we come back to this one.
2822 */
2823 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2824 XFS_ATTR_FORK);
2825 if (error)
2826 return(error);
2827 if (child_bp) {
2828 /* save for re-read later */
2829 child_blkno = xfs_da_blkno(child_bp);
2830
2831 /*
2832 * Invalidate the subtree, however we have to.
2833 */
2834 info = child_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002835 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 error = xfs_attr_node_inactive(trans, dp,
2837 child_bp, level+1);
Nathan Scott89da0542006-03-17 17:28:40 +11002838 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 error = xfs_attr_leaf_inactive(trans, dp,
2840 child_bp);
2841 } else {
2842 error = XFS_ERROR(EIO);
2843 xfs_da_brelse(*trans, child_bp);
2844 }
2845 if (error)
2846 return(error);
2847
2848 /*
2849 * Remove the subsidiary block from the cache
2850 * and from the log.
2851 */
2852 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2853 &child_bp, XFS_ATTR_FORK);
2854 if (error)
2855 return(error);
2856 xfs_da_binval(*trans, child_bp);
2857 }
2858
2859 /*
2860 * If we're not done, re-read the parent to get the next
2861 * child block number.
2862 */
2863 if ((i+1) < count) {
2864 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2865 &bp, XFS_ATTR_FORK);
2866 if (error)
2867 return(error);
Nathan Scott403432d2006-03-17 17:29:46 +11002868 child_fsb = be32_to_cpu(node->btree[i+1].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 xfs_da_brelse(*trans, bp);
2870 }
2871 /*
2872 * Atomically commit the whole invalidate stuff.
2873 */
2874 if ((error = xfs_attr_rolltrans(trans, dp)))
2875 return (error);
2876 }
2877
2878 return(0);
2879}
2880
2881/*
2882 * Invalidate all of the "remote" value regions pointed to by a particular
2883 * leaf block.
2884 * Note that we must release the lock on the buffer so that we are not
2885 * caught holding something that the logging code wants to flush to disk.
2886 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002887STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2889{
2890 xfs_attr_leafblock_t *leaf;
2891 xfs_attr_leaf_entry_t *entry;
2892 xfs_attr_leaf_name_remote_t *name_rmt;
2893 xfs_attr_inactive_list_t *list, *lp;
2894 int error, count, size, tmp, i;
2895
2896 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002897 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
2899 /*
2900 * Count the number of "remote" value extents.
2901 */
2902 count = 0;
2903 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002904 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002905 if (be16_to_cpu(entry->nameidx) &&
2906 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2908 if (name_rmt->valueblk)
2909 count++;
2910 }
2911 }
2912
2913 /*
2914 * If there are no "remote" values, we're done.
2915 */
2916 if (count == 0) {
2917 xfs_da_brelse(*trans, bp);
2918 return(0);
2919 }
2920
2921 /*
2922 * Allocate storage for a list of all the "remote" value extents.
2923 */
2924 size = count * sizeof(xfs_attr_inactive_list_t);
2925 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2926
2927 /*
2928 * Identify each of the "remote" value extents.
2929 */
2930 lp = list;
2931 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002932 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002933 if (be16_to_cpu(entry->nameidx) &&
2934 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2936 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11002937 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2938 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2939 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 lp++;
2941 }
2942 }
2943 }
2944 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
2945
2946 /*
2947 * Invalidate each of the "remote" value extents.
2948 */
2949 error = 0;
2950 for (lp = list, i = 0; i < count; i++, lp++) {
2951 tmp = xfs_attr_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11002952 lp->valueblk, lp->valuelen);
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (error == 0)
2955 error = tmp; /* save only the 1st errno */
2956 }
2957
2958 kmem_free((xfs_caddr_t)list, size);
2959 return(error);
2960}
2961
2962/*
2963 * Look at all the extents for this logical region,
2964 * invalidate any buffers that are incore/in transactions.
2965 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002966STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2968 xfs_dablk_t blkno, int blkcnt)
2969{
2970 xfs_bmbt_irec_t map;
2971 xfs_dablk_t tblkno;
2972 int tblkcnt, dblkcnt, nmap, error;
2973 xfs_daddr_t dblkno;
2974 xfs_buf_t *bp;
2975
2976 /*
2977 * Roll through the "value", invalidating the attribute value's
2978 * blocks.
2979 */
2980 tblkno = blkno;
2981 tblkcnt = blkcnt;
2982 while (tblkcnt > 0) {
2983 /*
2984 * Try to remember where we decided to put the value.
2985 */
2986 nmap = 1;
2987 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
2988 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002989 NULL, 0, &map, &nmap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (error) {
2991 return(error);
2992 }
2993 ASSERT(nmap == 1);
2994 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
2995
2996 /*
2997 * If it's a hole, these are already unmapped
2998 * so there's nothing to invalidate.
2999 */
3000 if (map.br_startblock != HOLESTARTBLOCK) {
3001
3002 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3003 map.br_startblock);
3004 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3005 map.br_blockcount);
3006 bp = xfs_trans_get_buf(*trans,
3007 dp->i_mount->m_ddev_targp,
3008 dblkno, dblkcnt, XFS_BUF_LOCK);
3009 xfs_trans_binval(*trans, bp);
3010 /*
3011 * Roll to next transaction.
3012 */
3013 if ((error = xfs_attr_rolltrans(trans, dp)))
3014 return (error);
3015 }
3016
3017 tblkno += map.br_blockcount;
3018 tblkcnt -= map.br_blockcount;
3019 }
3020
3021 return(0);
3022}
3023
3024
3025/*
3026 * Roll from one trans in the sequence of PERMANENT transactions to the next.
3027 */
3028int
3029xfs_attr_rolltrans(xfs_trans_t **transp, xfs_inode_t *dp)
3030{
3031 xfs_trans_t *trans;
3032 unsigned int logres, count;
3033 int error;
3034
3035 /*
3036 * Ensure that the inode is always logged.
3037 */
3038 trans = *transp;
3039 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
3040
3041 /*
3042 * Copy the critical parameters from one trans to the next.
3043 */
3044 logres = trans->t_log_res;
3045 count = trans->t_log_count;
3046 *transp = xfs_trans_dup(trans);
3047
3048 /*
3049 * Commit the current transaction.
3050 * If this commit failed, then it'd just unlock those items that
3051 * are not marked ihold. That also means that a filesystem shutdown
3052 * is in progress. The caller takes the responsibility to cancel
3053 * the duplicate transaction that gets returned.
3054 */
3055 if ((error = xfs_trans_commit(trans, 0, NULL)))
3056 return (error);
3057
3058 trans = *transp;
3059
3060 /*
3061 * Reserve space in the log for th next transaction.
3062 * This also pushes items in the "AIL", the list of logged items,
3063 * out to disk if they are taking up space at the tail of the log
3064 * that we want to use. This requires that either nothing be locked
3065 * across this call, or that anything that is locked be logged in
3066 * the prior and the next transactions.
3067 */
3068 error = xfs_trans_reserve(trans, 0, logres, 0,
3069 XFS_TRANS_PERM_LOG_RES, count);
3070 /*
3071 * Ensure that the inode is in the new transaction and locked.
3072 */
3073 if (!error) {
3074 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
3075 xfs_trans_ihold(trans, dp);
3076 }
3077 return (error);
3078
3079}