blob: 9719bbef122ce355c20894a51d424e4b264be933 [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
97STATIC inline attrnames_t *
98xfs_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 */
108STATIC inline int
109xfs_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 */
119STATIC inline int
120xfs_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 */
153 xfs_mount_t *mp = dp->i_mount;
154
Nathan Scottd8cc8902005-11-02 10:34:53 +1100155 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
156
157 switch (dp->i_d.di_format) {
158 case XFS_DINODE_FMT_DEV:
159 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
160 return (offset >= minforkoff) ? minforkoff : 0;
161 case XFS_DINODE_FMT_UUID:
162 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
163 return (offset >= minforkoff) ? minforkoff : 0;
164 }
165
Nathan Scott13059ff2006-01-11 15:32:01 +1100166 if (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +1100167 if (bytes <= XFS_IFORK_ASIZE(dp))
168 return mp->m_attroffset >> 3;
169 return 0;
170 }
171
Nathan Scottd8cc8902005-11-02 10:34:53 +1100172 /* data fork btree root can have at least this many key/ptr pairs */
173 minforkoff = MAX(dp->i_df.if_bytes, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
174 minforkoff = roundup(minforkoff, 8) >> 3;
175
176 /* attr fork btree root can have at least this many key/ptr pairs */
177 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
178 maxforkoff = maxforkoff >> 3; /* rounded down */
179
180 if (offset >= minforkoff && offset < maxforkoff)
181 return offset;
182 if (offset >= maxforkoff)
183 return maxforkoff;
184 return 0;
185}
186
187/*
188 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
189 */
190STATIC void
191xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
192{
193 unsigned long s;
194
Nathan Scott13059ff2006-01-11 15:32:01 +1100195 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Nathan Scottd8cc8902005-11-02 10:34:53 +1100196 !(XFS_SB_VERSION_HASATTR2(&mp->m_sb))) {
197 s = XFS_SB_LOCK(mp);
198 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb)) {
199 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
200 XFS_SB_UNLOCK(mp, s);
201 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
202 } else
203 XFS_SB_UNLOCK(mp, s);
204 }
205}
206
207/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * Create the initial contents of a shortform attribute list.
209 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100210void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211xfs_attr_shortform_create(xfs_da_args_t *args)
212{
213 xfs_attr_sf_hdr_t *hdr;
214 xfs_inode_t *dp;
215 xfs_ifork_t *ifp;
216
217 dp = args->dp;
218 ASSERT(dp != NULL);
219 ifp = dp->i_afp;
220 ASSERT(ifp != NULL);
221 ASSERT(ifp->if_bytes == 0);
222 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
223 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
224 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
225 ifp->if_flags |= XFS_IFINLINE;
226 } else {
227 ASSERT(ifp->if_flags & XFS_IFINLINE);
228 }
229 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
230 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
231 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100232 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/*
237 * Add a name/value pair to the shortform attribute list.
238 * Overflow from the inode has already been checked for.
239 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100240void
241xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 xfs_attr_shortform_t *sf;
244 xfs_attr_sf_entry_t *sfe;
245 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100246 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 xfs_inode_t *dp;
248 xfs_ifork_t *ifp;
249
250 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100251 mp = dp->i_mount;
252 dp->i_d.di_forkoff = forkoff;
253 dp->i_df.if_ext_max =
254 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
255 dp->i_afp->if_ext_max =
256 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 ifp = dp->i_afp;
259 ASSERT(ifp->if_flags & XFS_IFINLINE);
260 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
261 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100262 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100263#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (sfe->namelen != args->namelen)
265 continue;
266 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
267 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000268 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100270 ASSERT(0);
271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 offset = (char *)sfe - (char *)sf;
275 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
276 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
277 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
278 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
279
280 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100281 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000282 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 memcpy(sfe->nameval, args->name, args->namelen);
284 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100285 sf->hdr.count++;
286 be16_add(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
288
Nathan Scottd8cc8902005-11-02 10:34:53 +1100289 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100293 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
295int
296xfs_attr_shortform_remove(xfs_da_args_t *args)
297{
298 xfs_attr_shortform_t *sf;
299 xfs_attr_sf_entry_t *sfe;
300 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100301 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfs_inode_t *dp;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100305 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 base = sizeof(xfs_attr_sf_hdr_t);
307 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
308 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100309 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100310 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 base += size, i++) {
312 size = XFS_ATTR_SF_ENTSIZE(sfe);
313 if (sfe->namelen != args->namelen)
314 continue;
315 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
316 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000317 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 continue;
319 break;
320 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100321 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return(XFS_ERROR(ENOATTR));
323
Nathan Scottd8cc8902005-11-02 10:34:53 +1100324 /*
325 * Fix up the attribute fork data, covering the hole
326 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100328 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100329 if (end != totsize)
330 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100331 sf->hdr.count--;
332 be16_add(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100333
334 /*
335 * Fix up the start offset of the attribute fork
336 */
337 totsize -= size;
Nathan Scotte0144ca2005-11-25 16:42:22 +1100338 if (totsize == sizeof(xfs_attr_sf_hdr_t) && !args->addname &&
Nathan Scott13059ff2006-01-11 15:32:01 +1100339 (mp->m_flags & XFS_MOUNT_ATTR2)) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100340 /*
341 * Last attribute now removed, revert to original
342 * inode format making all literal area available
343 * to the data fork once more.
344 */
345 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
346 dp->i_d.di_forkoff = 0;
347 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
348 ASSERT(dp->i_d.di_anextents == 0);
349 ASSERT(dp->i_afp == NULL);
350 dp->i_df.if_ext_max =
351 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
352 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
353 } else {
354 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
355 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
356 ASSERT(dp->i_d.di_forkoff);
Nathan Scotte0144ca2005-11-25 16:42:22 +1100357 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) || args->addname ||
Nathan Scott13059ff2006-01-11 15:32:01 +1100358 !(mp->m_flags & XFS_MOUNT_ATTR2));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100359 dp->i_afp->if_ext_max =
360 XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
361 dp->i_df.if_ext_max =
362 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
363 xfs_trans_log_inode(args->trans, dp,
364 XFS_ILOG_CORE | XFS_ILOG_ADATA);
365 }
366
367 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 return(0);
370}
371
372/*
373 * Look up a name in a shortform attribute list structure.
374 */
375/*ARGSUSED*/
376int
377xfs_attr_shortform_lookup(xfs_da_args_t *args)
378{
379 xfs_attr_shortform_t *sf;
380 xfs_attr_sf_entry_t *sfe;
381 int i;
382 xfs_ifork_t *ifp;
383
384 ifp = args->dp->i_afp;
385 ASSERT(ifp->if_flags & XFS_IFINLINE);
386 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
387 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100388 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
390 if (sfe->namelen != args->namelen)
391 continue;
392 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
393 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000394 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 continue;
396 return(XFS_ERROR(EEXIST));
397 }
398 return(XFS_ERROR(ENOATTR));
399}
400
401/*
402 * Look up a name in a shortform attribute list structure.
403 */
404/*ARGSUSED*/
405int
406xfs_attr_shortform_getvalue(xfs_da_args_t *args)
407{
408 xfs_attr_shortform_t *sf;
409 xfs_attr_sf_entry_t *sfe;
410 int i;
411
412 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
413 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
414 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100415 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
417 if (sfe->namelen != args->namelen)
418 continue;
419 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
420 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000421 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 continue;
423 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100424 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return(XFS_ERROR(EEXIST));
426 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100427 if (args->valuelen < sfe->valuelen) {
428 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return(XFS_ERROR(ERANGE));
430 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100431 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 memcpy(args->value, &sfe->nameval[args->namelen],
433 args->valuelen);
434 return(XFS_ERROR(EEXIST));
435 }
436 return(XFS_ERROR(ENOATTR));
437}
438
439/*
440 * Convert from using the shortform to the leaf.
441 */
442int
443xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
444{
445 xfs_inode_t *dp;
446 xfs_attr_shortform_t *sf;
447 xfs_attr_sf_entry_t *sfe;
448 xfs_da_args_t nargs;
449 char *tmpbuffer;
450 int error, i, size;
451 xfs_dablk_t blkno;
452 xfs_dabuf_t *bp;
453 xfs_ifork_t *ifp;
454
455 dp = args->dp;
456 ifp = dp->i_afp;
457 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100458 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 tmpbuffer = kmem_alloc(size, KM_SLEEP);
460 ASSERT(tmpbuffer != NULL);
461 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
462 sf = (xfs_attr_shortform_t *)tmpbuffer;
463
464 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
465 bp = NULL;
466 error = xfs_da_grow_inode(args, &blkno);
467 if (error) {
468 /*
469 * If we hit an IO error middle of the transaction inside
470 * grow_inode(), we may have inconsistent data. Bail out.
471 */
472 if (error == EIO)
473 goto out;
474 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
475 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
476 goto out;
477 }
478
479 ASSERT(blkno == 0);
480 error = xfs_attr_leaf_create(args, blkno, &bp);
481 if (error) {
482 error = xfs_da_shrink_inode(args, 0, bp);
483 bp = NULL;
484 if (error)
485 goto out;
486 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
487 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
488 goto out;
489 }
490
491 memset((char *)&nargs, 0, sizeof(nargs));
492 nargs.dp = dp;
493 nargs.firstblock = args->firstblock;
494 nargs.flist = args->flist;
495 nargs.total = args->total;
496 nargs.whichfork = XFS_ATTR_FORK;
497 nargs.trans = args->trans;
498 nargs.oknoent = 1;
499
500 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100501 for (i = 0; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 nargs.name = (char *)sfe->nameval;
503 nargs.namelen = sfe->namelen;
504 nargs.value = (char *)&sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100505 nargs.valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
507 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000508 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
510 ASSERT(error == ENOATTR);
511 error = xfs_attr_leaf_add(bp, &nargs);
512 ASSERT(error != ENOSPC);
513 if (error)
514 goto out;
515 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
516 }
517 error = 0;
518
519out:
520 if(bp)
521 xfs_da_buf_done(bp);
522 kmem_free(tmpbuffer, size);
523 return(error);
524}
525
526STATIC int
527xfs_attr_shortform_compare(const void *a, const void *b)
528{
529 xfs_attr_sf_sort_t *sa, *sb;
530
531 sa = (xfs_attr_sf_sort_t *)a;
532 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100533 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100535 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return(1);
537 } else {
538 return(sa->entno - sb->entno);
539 }
540}
541
Tim Shimmin726801b2006-09-28 11:01:37 +1000542
543#define XFS_ISRESET_CURSOR(cursor) \
544 (!((cursor)->initted) && !((cursor)->hashval) && \
545 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
547 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000548 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * If the output buffer is not large enough to hold them all, then we
550 * we have to calculate each entries' hashvalue and sort them before
551 * we can begin returning them to the user.
552 */
553/*ARGSUSED*/
554int
555xfs_attr_shortform_list(xfs_attr_list_context_t *context)
556{
557 attrlist_cursor_kern_t *cursor;
558 xfs_attr_sf_sort_t *sbuf, *sbp;
559 xfs_attr_shortform_t *sf;
560 xfs_attr_sf_entry_t *sfe;
561 xfs_inode_t *dp;
562 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000563 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 ASSERT(context != NULL);
566 dp = context->dp;
567 ASSERT(dp != NULL);
568 ASSERT(dp->i_afp != NULL);
569 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
570 ASSERT(sf != NULL);
571 if (!sf->hdr.count)
572 return(0);
573 cursor = context->cursor;
574 ASSERT(cursor != NULL);
575
576 xfs_attr_trace_l_c("sf start", context);
577
578 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000579 * If the buffer is large enough and the cursor is at the start,
580 * do not bother with sorting since we will return everything in
581 * one buffer and another call using the cursor won't need to be
582 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000584 * If bufsize is zero then put_listent must be a search function
585 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000587 if (context->bufsize == 0 ||
588 (XFS_ISRESET_CURSOR(cursor) &&
589 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100590 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 attrnames_t *namesp;
592
Tim Shimmin726801b2006-09-28 11:01:37 +1000593 if (!xfs_attr_namesp_match_overrides(context->flags, sfe->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
595 continue;
596 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000597 namesp = xfs_attr_flags_namesp(sfe->flags);
598 error = context->put_listent(context,
599 namesp,
600 (char *)sfe->nameval,
601 (int)sfe->namelen,
602 (int)sfe->valuelen,
603 (char*)&sfe->nameval[sfe->namelen]);
604
605 /*
606 * Either search callback finished early or
607 * didn't fit it all in the buffer after all.
608 */
609 if (context->seen_enough)
610 break;
611
612 if (error)
613 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
615 }
616 xfs_attr_trace_l_c("sf big-gulp", context);
617 return(0);
618 }
619
Tim Shimmin726801b2006-09-28 11:01:37 +1000620 /* do no more for a search callback */
621 if (context->bufsize == 0)
622 return 0;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
625 * It didn't all fit, so we have to sort everything on hashval.
626 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100627 sbsize = sf->hdr.count * sizeof(*sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
629
630 /*
631 * Scan the attribute list for the rest of the entries, storing
632 * the relevant info from only those that match into a buffer.
633 */
634 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100635 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (unlikely(
637 ((char *)sfe < (char *)sf) ||
638 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
639 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
640 XFS_ERRLEVEL_LOW,
641 context->dp->i_mount, sfe);
642 xfs_attr_trace_l_c("sf corrupted", context);
643 kmem_free(sbuf, sbsize);
644 return XFS_ERROR(EFSCORRUPTED);
645 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000646 if (!xfs_attr_namesp_match_overrides(context->flags, sfe->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
648 continue;
649 }
650 sbp->entno = i;
Nathan Scott984a0812006-03-17 17:29:31 +1100651 sbp->hash = xfs_da_hashname((char *)sfe->nameval, sfe->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 sbp->name = (char *)sfe->nameval;
653 sbp->namelen = sfe->namelen;
654 /* These are bytes, and both on-disk, don't endian-flip */
655 sbp->valuelen = sfe->valuelen;
656 sbp->flags = sfe->flags;
657 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
658 sbp++;
659 nsbuf++;
660 }
661
662 /*
663 * Sort the entries on hash then entno.
664 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100665 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /*
668 * Re-find our place IN THE SORTED LIST.
669 */
670 count = 0;
671 cursor->initted = 1;
672 cursor->blkno = 0;
673 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100674 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (cursor->offset == count) {
676 break;
677 }
678 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100679 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 break;
681 }
682 }
683 if (i == nsbuf) {
684 kmem_free(sbuf, sbsize);
685 xfs_attr_trace_l_c("blk end", context);
686 return(0);
687 }
688
689 /*
690 * Loop putting entries into the user buffer.
691 */
692 for ( ; i < nsbuf; i++, sbp++) {
693 attrnames_t *namesp;
694
Tim Shimmin726801b2006-09-28 11:01:37 +1000695 namesp = xfs_attr_flags_namesp(sbp->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Nathan Scott984a0812006-03-17 17:29:31 +1100697 if (cursor->hashval != sbp->hash) {
698 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 cursor->offset = 0;
700 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000701 error = context->put_listent(context,
702 namesp,
703 sbp->name,
704 sbp->namelen,
705 sbp->valuelen,
706 &sbp->name[sbp->namelen]);
707 if (error)
708 return error;
709 if (context->seen_enough)
710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 cursor->offset++;
712 }
713
714 kmem_free(sbuf, sbsize);
715 xfs_attr_trace_l_c("sf E-O-F", context);
716 return(0);
717}
718
719/*
720 * Check a leaf attribute block to see if all the entries would fit into
721 * a shortform attribute list.
722 */
723int
724xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
725{
726 xfs_attr_leafblock_t *leaf;
727 xfs_attr_leaf_entry_t *entry;
728 xfs_attr_leaf_name_local_t *name_loc;
729 int bytes, i;
730
731 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100732 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 entry = &leaf->entries[0];
735 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100736 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (entry->flags & XFS_ATTR_INCOMPLETE)
738 continue; /* don't copy partial entries */
739 if (!(entry->flags & XFS_ATTR_LOCAL))
740 return(0);
741 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
742 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
743 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100744 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return(0);
746 bytes += sizeof(struct xfs_attr_sf_entry)-1
747 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100748 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100750 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100751 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100752 return(-1);
753 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*
757 * Convert a leaf attribute list to shortform attribute list
758 */
759int
Nathan Scottd8cc8902005-11-02 10:34:53 +1100760xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 xfs_attr_leafblock_t *leaf;
763 xfs_attr_leaf_entry_t *entry;
764 xfs_attr_leaf_name_local_t *name_loc;
765 xfs_da_args_t nargs;
766 xfs_inode_t *dp;
767 char *tmpbuffer;
768 int error, i;
769
770 dp = args->dp;
771 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
772 ASSERT(tmpbuffer != NULL);
773
774 ASSERT(bp != NULL);
775 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
776 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Nathan Scott89da0542006-03-17 17:28:40 +1100777 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
779
780 /*
781 * Clean out the prior contents of the attribute list.
782 */
783 error = xfs_da_shrink_inode(args, 0, bp);
784 if (error)
785 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100786
787 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +1100788 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Nathan Scotte0144ca2005-11-25 16:42:22 +1100789
Nathan Scottd8cc8902005-11-02 10:34:53 +1100790 /*
791 * Last attribute was removed, revert to original
792 * inode format making all literal area available
793 * to the data fork once more.
794 */
795 xfs_idestroy_fork(dp, XFS_ATTR_FORK);
796 dp->i_d.di_forkoff = 0;
797 dp->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
798 ASSERT(dp->i_d.di_anextents == 0);
799 ASSERT(dp->i_afp == NULL);
800 dp->i_df.if_ext_max =
801 XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
802 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100804 }
805
806 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /*
809 * Copy the attributes
810 */
811 memset((char *)&nargs, 0, sizeof(nargs));
812 nargs.dp = dp;
813 nargs.firstblock = args->firstblock;
814 nargs.flist = args->flist;
815 nargs.total = args->total;
816 nargs.whichfork = XFS_ATTR_FORK;
817 nargs.trans = args->trans;
818 nargs.oknoent = 1;
819 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +1100820 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (entry->flags & XFS_ATTR_INCOMPLETE)
822 continue; /* don't copy partial entries */
823 if (!entry->nameidx)
824 continue;
825 ASSERT(entry->flags & XFS_ATTR_LOCAL);
826 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
827 nargs.name = (char *)name_loc->nameval;
828 nargs.namelen = name_loc->namelen;
829 nargs.value = (char *)&name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +1100830 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100831 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +1000832 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100833 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 error = 0;
836
837out:
838 kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
839 return(error);
840}
841
842/*
843 * Convert from using a single leaf to a root node and a leaf.
844 */
845int
846xfs_attr_leaf_to_node(xfs_da_args_t *args)
847{
848 xfs_attr_leafblock_t *leaf;
849 xfs_da_intnode_t *node;
850 xfs_inode_t *dp;
851 xfs_dabuf_t *bp1, *bp2;
852 xfs_dablk_t blkno;
853 int error;
854
855 dp = args->dp;
856 bp1 = bp2 = NULL;
857 error = xfs_da_grow_inode(args, &blkno);
858 if (error)
859 goto out;
860 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
861 XFS_ATTR_FORK);
862 if (error)
863 goto out;
864 ASSERT(bp1 != NULL);
865 bp2 = NULL;
866 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
867 XFS_ATTR_FORK);
868 if (error)
869 goto out;
870 ASSERT(bp2 != NULL);
871 memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
872 xfs_da_buf_done(bp1);
873 bp1 = NULL;
874 xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
875
876 /*
877 * Set up the new root node.
878 */
879 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
880 if (error)
881 goto out;
882 node = bp1->data;
883 leaf = bp2->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100884 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /* both on-disk, don't endian-flip twice */
886 node->btree[0].hashval =
Nathan Scott918ae422006-03-17 17:28:54 +1100887 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
Nathan Scott403432d2006-03-17 17:29:46 +1100888 node->btree[0].before = cpu_to_be32(blkno);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100889 node->hdr.count = cpu_to_be16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
891 error = 0;
892out:
893 if (bp1)
894 xfs_da_buf_done(bp1);
895 if (bp2)
896 xfs_da_buf_done(bp2);
897 return(error);
898}
899
900
901/*========================================================================
902 * Routines used for growing the Btree.
903 *========================================================================*/
904
905/*
906 * Create the initial contents of a leaf attribute list
907 * or a leaf in a node attribute list.
908 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000909STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
911{
912 xfs_attr_leafblock_t *leaf;
913 xfs_attr_leaf_hdr_t *hdr;
914 xfs_inode_t *dp;
915 xfs_dabuf_t *bp;
916 int error;
917
918 dp = args->dp;
919 ASSERT(dp != NULL);
920 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
921 XFS_ATTR_FORK);
922 if (error)
923 return(error);
924 ASSERT(bp != NULL);
925 leaf = bp->data;
926 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
927 hdr = &leaf->hdr;
Nathan Scott89da0542006-03-17 17:28:40 +1100928 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +1100929 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +1100931 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
933 }
934
Nathan Scott918ae422006-03-17 17:28:54 +1100935 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
936 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
937 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
940
941 *bpp = bp;
942 return(0);
943}
944
945/*
946 * Split the leaf node, rebalance, then add the new entry.
947 */
948int
949xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
950 xfs_da_state_blk_t *newblk)
951{
952 xfs_dablk_t blkno;
953 int error;
954
955 /*
956 * Allocate space for a new leaf node.
957 */
958 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
959 error = xfs_da_grow_inode(state->args, &blkno);
960 if (error)
961 return(error);
962 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
963 if (error)
964 return(error);
965 newblk->blkno = blkno;
966 newblk->magic = XFS_ATTR_LEAF_MAGIC;
967
968 /*
969 * Rebalance the entries across the two leaves.
970 * NOTE: rebalance() currently depends on the 2nd block being empty.
971 */
972 xfs_attr_leaf_rebalance(state, oldblk, newblk);
973 error = xfs_da_blk_link(state, oldblk, newblk);
974 if (error)
975 return(error);
976
977 /*
978 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
979 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
980 * "new" attrs info. Will need the "old" info to remove it later.
981 *
982 * Insert the "new" entry in the correct block.
983 */
984 if (state->inleaf)
985 error = xfs_attr_leaf_add(oldblk->bp, state->args);
986 else
987 error = xfs_attr_leaf_add(newblk->bp, state->args);
988
989 /*
990 * Update last hashval in each block since we added the name.
991 */
992 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
993 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
994 return(error);
995}
996
997/*
998 * Add a name to the leaf attribute list structure.
999 */
1000int
1001xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
1002{
1003 xfs_attr_leafblock_t *leaf;
1004 xfs_attr_leaf_hdr_t *hdr;
1005 xfs_attr_leaf_map_t *map;
1006 int tablesize, entsize, sum, tmp, i;
1007
1008 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001009 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001011 && (args->index <= be16_to_cpu(leaf->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 hdr = &leaf->hdr;
Nathan Scottaa82daa2005-11-02 10:33:33 +11001013 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1015
1016 /*
1017 * Search through freemap for first-fit on new name length.
1018 * (may need to figure in size of entry struct too)
1019 */
Nathan Scott918ae422006-03-17 17:28:54 +11001020 tablesize = (be16_to_cpu(hdr->count) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 * sizeof(xfs_attr_leaf_entry_t)
1022 + sizeof(xfs_attr_leaf_hdr_t);
1023 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1024 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
Nathan Scott918ae422006-03-17 17:28:54 +11001025 if (tablesize > be16_to_cpu(hdr->firstused)) {
1026 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 continue;
1028 }
1029 if (!map->size)
1030 continue; /* no space in this map */
1031 tmp = entsize;
Nathan Scott918ae422006-03-17 17:28:54 +11001032 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 tmp += sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001034 if (be16_to_cpu(map->size) >= tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 tmp = xfs_attr_leaf_add_work(bp, args, i);
1036 return(tmp);
1037 }
Nathan Scott918ae422006-03-17 17:28:54 +11001038 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040
1041 /*
1042 * If there are no holes in the address space of the block,
1043 * and we don't have enough freespace, then compaction will do us
1044 * no good and we should just give up.
1045 */
1046 if (!hdr->holes && (sum < entsize))
1047 return(XFS_ERROR(ENOSPC));
1048
1049 /*
1050 * Compact the entries to coalesce free space.
1051 * This may change the hdr->count via dropping INCOMPLETE entries.
1052 */
1053 xfs_attr_leaf_compact(args->trans, bp);
1054
1055 /*
1056 * After compaction, the block is guaranteed to have only one
1057 * free region, in freemap[0]. If it is not big enough, give up.
1058 */
Nathan Scott918ae422006-03-17 17:28:54 +11001059 if (be16_to_cpu(hdr->freemap[0].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1061 return(XFS_ERROR(ENOSPC));
1062
1063 return(xfs_attr_leaf_add_work(bp, args, 0));
1064}
1065
1066/*
1067 * Add a name to a leaf attribute list structure.
1068 */
1069STATIC int
1070xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
1071{
1072 xfs_attr_leafblock_t *leaf;
1073 xfs_attr_leaf_hdr_t *hdr;
1074 xfs_attr_leaf_entry_t *entry;
1075 xfs_attr_leaf_name_local_t *name_loc;
1076 xfs_attr_leaf_name_remote_t *name_rmt;
1077 xfs_attr_leaf_map_t *map;
1078 xfs_mount_t *mp;
1079 int tmp, i;
1080
1081 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001082 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 hdr = &leaf->hdr;
1084 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
Nathan Scott918ae422006-03-17 17:28:54 +11001085 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /*
1088 * Force open some space in the entry array and fill it in.
1089 */
1090 entry = &leaf->entries[args->index];
Nathan Scott918ae422006-03-17 17:28:54 +11001091 if (args->index < be16_to_cpu(hdr->count)) {
1092 tmp = be16_to_cpu(hdr->count) - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 tmp *= sizeof(xfs_attr_leaf_entry_t);
1094 memmove((char *)(entry+1), (char *)entry, tmp);
1095 xfs_da_log_buf(args->trans, bp,
1096 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1097 }
Nathan Scott918ae422006-03-17 17:28:54 +11001098 be16_add(&hdr->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /*
1101 * Allocate space for the new string (at the end of the run).
1102 */
1103 map = &hdr->freemap[mapindex];
1104 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001105 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1106 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1107 ASSERT(be16_to_cpu(map->size) >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001108 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1109 mp->m_sb.sb_blocksize, NULL));
Nathan Scott918ae422006-03-17 17:28:54 +11001110 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1111 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
1112 be16_add(&map->size,
Nathan Scottaa82daa2005-11-02 10:33:33 +11001113 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1114 mp->m_sb.sb_blocksize, &tmp));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001115 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1116 be16_to_cpu(map->size));
1117 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001119 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (args->rename) {
1121 entry->flags |= XFS_ATTR_INCOMPLETE;
1122 if ((args->blkno2 == args->blkno) &&
1123 (args->index2 <= args->index)) {
1124 args->index2++;
1125 }
1126 }
1127 xfs_da_log_buf(args->trans, bp,
1128 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001129 ASSERT((args->index == 0) ||
1130 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Nathan Scott918ae422006-03-17 17:28:54 +11001131 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001132 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /*
1135 * Copy the attribute name and value into the new space.
1136 *
1137 * For "remote" attribute values, simply note that we need to
1138 * allocate space for the "remote" value. We can't actually
1139 * allocate the extents in this transaction, and we can't decide
1140 * which blocks they should be as we might allocate more blocks
1141 * as part of this transaction (a split operation for example).
1142 */
1143 if (entry->flags & XFS_ATTR_LOCAL) {
1144 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
1145 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001146 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1148 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001149 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 } else {
1151 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
1152 name_rmt->namelen = args->namelen;
1153 memcpy((char *)name_rmt->name, args->name, args->namelen);
1154 entry->flags |= XFS_ATTR_INCOMPLETE;
1155 /* just in case */
1156 name_rmt->valuelen = 0;
1157 name_rmt->valueblk = 0;
1158 args->rmtblkno = 1;
1159 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1160 }
1161 xfs_da_log_buf(args->trans, bp,
1162 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1163 xfs_attr_leaf_entsize(leaf, args->index)));
1164
1165 /*
1166 * Update the control info for this leaf node
1167 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001168 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* both on-disk, don't endian-flip twice */
1170 hdr->firstused = entry->nameidx;
1171 }
Nathan Scott918ae422006-03-17 17:28:54 +11001172 ASSERT(be16_to_cpu(hdr->firstused) >=
1173 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1174 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 + sizeof(xfs_attr_leaf_hdr_t);
1176 map = &hdr->freemap[0];
1177 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001178 if (be16_to_cpu(map->base) == tmp) {
1179 be16_add(&map->base, sizeof(xfs_attr_leaf_entry_t));
1180 be16_add(&map->size,
1181 -((int)sizeof(xfs_attr_leaf_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 }
Nathan Scott918ae422006-03-17 17:28:54 +11001184 be16_add(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 xfs_da_log_buf(args->trans, bp,
1186 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1187 return(0);
1188}
1189
1190/*
1191 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1192 */
1193STATIC void
1194xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
1195{
1196 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1197 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1198 xfs_mount_t *mp;
1199 char *tmpbuffer;
1200
1201 mp = trans->t_mountp;
1202 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1203 ASSERT(tmpbuffer != NULL);
1204 memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
1205 memset(bp->data, 0, XFS_LBSIZE(mp));
1206
1207 /*
1208 * Copy basic information
1209 */
1210 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
1211 leaf_d = bp->data;
1212 hdr_s = &leaf_s->hdr;
1213 hdr_d = &leaf_d->hdr;
1214 hdr_d->info = hdr_s->info; /* struct copy */
Nathan Scott918ae422006-03-17 17:28:54 +11001215 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /* handle truncation gracefully */
1217 if (!hdr_d->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001218 hdr_d->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1220 }
1221 hdr_d->usedbytes = 0;
1222 hdr_d->count = 0;
1223 hdr_d->holes = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001224 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1225 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1226 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /*
1229 * Copy all entry's in the same (sorted) order,
1230 * but allocate name/value pairs packed and in sequence.
1231 */
1232 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001233 be16_to_cpu(hdr_s->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
1235
1236 kmem_free(tmpbuffer, XFS_LBSIZE(mp));
1237}
1238
1239/*
1240 * Redistribute the attribute list entries between two leaf nodes,
1241 * taking into account the size of the new entry.
1242 *
1243 * NOTE: if new block is empty, then it will get the upper half of the
1244 * old block. At present, all (one) callers pass in an empty second block.
1245 *
1246 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1247 * to match what it is doing in splitting the attribute leaf block. Those
1248 * values are used in "atomic rename" operations on attributes. Note that
1249 * the "new" and "old" values can end up in different blocks.
1250 */
1251STATIC void
1252xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1253 xfs_da_state_blk_t *blk2)
1254{
1255 xfs_da_args_t *args;
1256 xfs_da_state_blk_t *tmp_blk;
1257 xfs_attr_leafblock_t *leaf1, *leaf2;
1258 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1259 int count, totallen, max, space, swap;
1260
1261 /*
1262 * Set up environment.
1263 */
1264 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1265 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1266 leaf1 = blk1->bp->data;
1267 leaf2 = blk2->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001268 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1269 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 args = state->args;
1271
1272 /*
1273 * Check ordering of blocks, reverse if it makes things simpler.
1274 *
1275 * NOTE: Given that all (current) callers pass in an empty
1276 * second block, this code should never set "swap".
1277 */
1278 swap = 0;
1279 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1280 tmp_blk = blk1;
1281 blk1 = blk2;
1282 blk2 = tmp_blk;
1283 leaf1 = blk1->bp->data;
1284 leaf2 = blk2->bp->data;
1285 swap = 1;
1286 }
1287 hdr1 = &leaf1->hdr;
1288 hdr2 = &leaf2->hdr;
1289
1290 /*
1291 * Examine entries until we reduce the absolute difference in
1292 * byte usage between the two blocks to a minimum. Then get
1293 * the direction to copy and the number of elements to move.
1294 *
1295 * "inleaf" is true if the new entry should be inserted into blk1.
1296 * If "swap" is also true, then reverse the sense of "inleaf".
1297 */
1298 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1299 &count, &totallen);
1300 if (swap)
1301 state->inleaf = !state->inleaf;
1302
1303 /*
1304 * Move any entries required from leaf to leaf:
1305 */
Nathan Scott918ae422006-03-17 17:28:54 +11001306 if (count < be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /*
1308 * Figure the total bytes to be added to the destination leaf.
1309 */
1310 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001311 count = be16_to_cpu(hdr1->count) - count;
1312 space = be16_to_cpu(hdr1->usedbytes) - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 space += count * sizeof(xfs_attr_leaf_entry_t);
1314
1315 /*
1316 * leaf2 is the destination, compact it if it looks tight.
1317 */
Nathan Scott918ae422006-03-17 17:28:54 +11001318 max = be16_to_cpu(hdr2->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001320 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (space > max) {
1322 xfs_attr_leaf_compact(args->trans, blk2->bp);
1323 }
1324
1325 /*
1326 * Move high entries from leaf1 to low end of leaf2.
1327 */
Nathan Scott918ae422006-03-17 17:28:54 +11001328 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 leaf2, 0, count, state->mp);
1330
1331 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1332 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Nathan Scott918ae422006-03-17 17:28:54 +11001333 } else if (count > be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
1335 * I assert that since all callers pass in an empty
1336 * second buffer, this code should never execute.
1337 */
1338
1339 /*
1340 * Figure the total bytes to be added to the destination leaf.
1341 */
1342 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001343 count -= be16_to_cpu(hdr1->count);
1344 space = totallen - be16_to_cpu(hdr1->usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 space += count * sizeof(xfs_attr_leaf_entry_t);
1346
1347 /*
1348 * leaf1 is the destination, compact it if it looks tight.
1349 */
Nathan Scott918ae422006-03-17 17:28:54 +11001350 max = be16_to_cpu(hdr1->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001352 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (space > max) {
1354 xfs_attr_leaf_compact(args->trans, blk1->bp);
1355 }
1356
1357 /*
1358 * Move low entries from leaf2 to high end of leaf1.
1359 */
1360 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
Nathan Scott918ae422006-03-17 17:28:54 +11001361 be16_to_cpu(hdr1->count), count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1364 xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1365 }
1366
1367 /*
1368 * Copy out last hashval in each block for B-tree code.
1369 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001370 blk1->hashval = be32_to_cpu(
1371 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1372 blk2->hashval = be32_to_cpu(
1373 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /*
1376 * Adjust the expected index for insertion.
1377 * NOTE: this code depends on the (current) situation that the
1378 * second block was originally empty.
1379 *
1380 * If the insertion point moved to the 2nd block, we must adjust
1381 * the index. We must also track the entry just following the
1382 * new entry for use in an "atomic rename" operation, that entry
1383 * is always the "old" entry and the "new" entry is what we are
1384 * inserting. The index/blkno fields refer to the "old" entry,
1385 * while the index2/blkno2 fields refer to the "new" entry.
1386 */
Nathan Scott918ae422006-03-17 17:28:54 +11001387 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ASSERT(state->inleaf == 0);
Nathan Scott918ae422006-03-17 17:28:54 +11001389 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 args->index = args->index2 = blk2->index;
1391 args->blkno = args->blkno2 = blk2->blkno;
Nathan Scott918ae422006-03-17 17:28:54 +11001392 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 if (state->inleaf) {
1394 args->index = blk1->index;
1395 args->blkno = blk1->blkno;
1396 args->index2 = 0;
1397 args->blkno2 = blk2->blkno;
1398 } else {
1399 blk2->index = blk1->index
Nathan Scott918ae422006-03-17 17:28:54 +11001400 - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 args->index = args->index2 = blk2->index;
1402 args->blkno = args->blkno2 = blk2->blkno;
1403 }
1404 } else {
1405 ASSERT(state->inleaf == 1);
1406 args->index = args->index2 = blk1->index;
1407 args->blkno = args->blkno2 = blk1->blkno;
1408 }
1409}
1410
1411/*
1412 * Examine entries until we reduce the absolute difference in
1413 * byte usage between the two blocks to a minimum.
1414 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1415 * GROT: there will always be enough room in either block for a new entry.
1416 * GROT: Do a double-split for this case?
1417 */
1418STATIC int
1419xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1420 xfs_da_state_blk_t *blk1,
1421 xfs_da_state_blk_t *blk2,
1422 int *countarg, int *usedbytesarg)
1423{
1424 xfs_attr_leafblock_t *leaf1, *leaf2;
1425 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1426 xfs_attr_leaf_entry_t *entry;
1427 int count, max, index, totallen, half;
1428 int lastdelta, foundit, tmp;
1429
1430 /*
1431 * Set up environment.
1432 */
1433 leaf1 = blk1->bp->data;
1434 leaf2 = blk2->bp->data;
1435 hdr1 = &leaf1->hdr;
1436 hdr2 = &leaf2->hdr;
1437 foundit = 0;
1438 totallen = 0;
1439
1440 /*
1441 * Examine entries until we reduce the absolute difference in
1442 * byte usage between the two blocks to a minimum.
1443 */
Nathan Scott918ae422006-03-17 17:28:54 +11001444 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 half = (max+1) * sizeof(*entry);
Nathan Scott918ae422006-03-17 17:28:54 +11001446 half += be16_to_cpu(hdr1->usedbytes) +
1447 be16_to_cpu(hdr2->usedbytes) +
1448 xfs_attr_leaf_newentsize(
1449 state->args->namelen,
1450 state->args->valuelen,
1451 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 half /= 2;
1453 lastdelta = state->blocksize;
1454 entry = &leaf1->entries[0];
1455 for (count = index = 0; count < max; entry++, index++, count++) {
1456
1457#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1458 /*
1459 * The new entry is in the first block, account for it.
1460 */
1461 if (count == blk1->index) {
1462 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001463 xfs_attr_leaf_newentsize(
1464 state->args->namelen,
1465 state->args->valuelen,
1466 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1468 break;
1469 lastdelta = XFS_ATTR_ABS(half - tmp);
1470 totallen = tmp;
1471 foundit = 1;
1472 }
1473
1474 /*
1475 * Wrap around into the second block if necessary.
1476 */
Nathan Scott918ae422006-03-17 17:28:54 +11001477 if (count == be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 leaf1 = leaf2;
1479 entry = &leaf1->entries[0];
1480 index = 0;
1481 }
1482
1483 /*
1484 * Figure out if next leaf entry would be too much.
1485 */
1486 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1487 index);
1488 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1489 break;
1490 lastdelta = XFS_ATTR_ABS(half - tmp);
1491 totallen = tmp;
1492#undef XFS_ATTR_ABS
1493 }
1494
1495 /*
1496 * Calculate the number of usedbytes that will end up in lower block.
1497 * If new entry not in lower block, fix up the count.
1498 */
1499 totallen -= count * sizeof(*entry);
1500 if (foundit) {
1501 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001502 xfs_attr_leaf_newentsize(
1503 state->args->namelen,
1504 state->args->valuelen,
1505 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507
1508 *countarg = count;
1509 *usedbytesarg = totallen;
1510 return(foundit);
1511}
1512
1513/*========================================================================
1514 * Routines used for shrinking the Btree.
1515 *========================================================================*/
1516
1517/*
1518 * Check a leaf block and its neighbors to see if the block should be
1519 * collapsed into one or the other neighbor. Always keep the block
1520 * with the smaller block number.
1521 * If the current block is over 50% full, don't try to join it, return 0.
1522 * If the block is empty, fill in the state structure and return 2.
1523 * If it can be collapsed, fill in the state structure and return 1.
1524 * If nothing can be done, return 0.
1525 *
1526 * GROT: allow for INCOMPLETE entries in calculation.
1527 */
1528int
1529xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1530{
1531 xfs_attr_leafblock_t *leaf;
1532 xfs_da_state_blk_t *blk;
1533 xfs_da_blkinfo_t *info;
1534 int count, bytes, forward, error, retval, i;
1535 xfs_dablk_t blkno;
1536 xfs_dabuf_t *bp;
1537
1538 /*
1539 * Check for the degenerate case of the block being over 50% full.
1540 * If so, it's not worth even looking to see if we might be able
1541 * to coalesce with a sibling.
1542 */
1543 blk = &state->path.blk[ state->path.active-1 ];
1544 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001545 ASSERT(be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001547 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1549 count * sizeof(xfs_attr_leaf_entry_t) +
Nathan Scott918ae422006-03-17 17:28:54 +11001550 be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (bytes > (state->blocksize >> 1)) {
1552 *action = 0; /* blk over 50%, don't try to join */
1553 return(0);
1554 }
1555
1556 /*
1557 * Check for the degenerate case of the block being empty.
1558 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001559 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 * to merge with the forward block unless it is NULL.
1561 */
1562 if (count == 0) {
1563 /*
1564 * Make altpath point to the block we want to keep and
1565 * path point to the block we want to drop (this one).
1566 */
Nathan Scott89da0542006-03-17 17:28:40 +11001567 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 memcpy(&state->altpath, &state->path, sizeof(state->path));
1569 error = xfs_da_path_shift(state, &state->altpath, forward,
1570 0, &retval);
1571 if (error)
1572 return(error);
1573 if (retval) {
1574 *action = 0;
1575 } else {
1576 *action = 2;
1577 }
1578 return(0);
1579 }
1580
1581 /*
1582 * Examine each sibling block to see if we can coalesce with
1583 * at least 25% free space to spare. We need to figure out
1584 * whether to merge with the forward or the backward block.
1585 * We prefer coalescing with the lower numbered sibling so as
1586 * to shrink an attribute list over time.
1587 */
1588 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +11001589 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 for (i = 0; i < 2; forward = !forward, i++) {
1591 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +11001592 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 else
Nathan Scott89da0542006-03-17 17:28:40 +11001594 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (blkno == 0)
1596 continue;
1597 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1598 blkno, -1, &bp, XFS_ATTR_FORK);
1599 if (error)
1600 return(error);
1601 ASSERT(bp != NULL);
1602
1603 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001604 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 bytes = state->blocksize - (state->blocksize>>2);
Nathan Scott918ae422006-03-17 17:28:54 +11001606 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001608 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11001609 count += be16_to_cpu(leaf->hdr.count);
1610 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1612 bytes -= sizeof(xfs_attr_leaf_hdr_t);
1613 xfs_da_brelse(state->args->trans, bp);
1614 if (bytes >= 0)
1615 break; /* fits with at least 25% to spare */
1616 }
1617 if (i >= 2) {
1618 *action = 0;
1619 return(0);
1620 }
1621
1622 /*
1623 * Make altpath point to the block we want to keep (the lower
1624 * numbered block) and path point to the block we want to drop.
1625 */
1626 memcpy(&state->altpath, &state->path, sizeof(state->path));
1627 if (blkno < blk->blkno) {
1628 error = xfs_da_path_shift(state, &state->altpath, forward,
1629 0, &retval);
1630 } else {
1631 error = xfs_da_path_shift(state, &state->path, forward,
1632 0, &retval);
1633 }
1634 if (error)
1635 return(error);
1636 if (retval) {
1637 *action = 0;
1638 } else {
1639 *action = 1;
1640 }
1641 return(0);
1642}
1643
1644/*
1645 * Remove a name from the leaf attribute list structure.
1646 *
1647 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1648 * If two leaves are 37% full, when combined they will leave 25% free.
1649 */
1650int
1651xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
1652{
1653 xfs_attr_leafblock_t *leaf;
1654 xfs_attr_leaf_hdr_t *hdr;
1655 xfs_attr_leaf_map_t *map;
1656 xfs_attr_leaf_entry_t *entry;
1657 int before, after, smallest, entsize;
1658 int tablesize, tmp, i;
1659 xfs_mount_t *mp;
1660
1661 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001662 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 hdr = &leaf->hdr;
1664 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001665 ASSERT((be16_to_cpu(hdr->count) > 0)
1666 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001668 && (args->index < be16_to_cpu(hdr->count)));
1669 ASSERT(be16_to_cpu(hdr->firstused) >=
1670 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 entry = &leaf->entries[args->index];
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001672 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1673 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /*
1676 * Scan through free region table:
1677 * check for adjacency of free'd entry with an existing one,
1678 * find smallest free region in case we need to replace it,
1679 * adjust any map that borders the entry table,
1680 */
Nathan Scott918ae422006-03-17 17:28:54 +11001681 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 + sizeof(xfs_attr_leaf_hdr_t);
1683 map = &hdr->freemap[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001684 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 before = after = -1;
1686 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1687 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1688 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001689 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1690 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1691 if (be16_to_cpu(map->base) == tablesize) {
1692 be16_add(&map->base,
1693 -((int)sizeof(xfs_attr_leaf_entry_t)));
1694 be16_add(&map->size, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
Nathan Scott918ae422006-03-17 17:28:54 +11001697 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001698 == be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 before = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001700 } else if (be16_to_cpu(map->base)
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001701 == (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 after = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001703 } else if (be16_to_cpu(map->size) < tmp) {
1704 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 smallest = i;
1706 }
1707 }
1708
1709 /*
1710 * Coalesce adjacent freemap regions,
1711 * or replace the smallest region.
1712 */
1713 if ((before >= 0) || (after >= 0)) {
1714 if ((before >= 0) && (after >= 0)) {
1715 map = &hdr->freemap[before];
Nathan Scott918ae422006-03-17 17:28:54 +11001716 be16_add(&map->size, entsize);
1717 be16_add(&map->size,
1718 be16_to_cpu(hdr->freemap[after].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 hdr->freemap[after].base = 0;
1720 hdr->freemap[after].size = 0;
1721 } else if (before >= 0) {
1722 map = &hdr->freemap[before];
Nathan Scott918ae422006-03-17 17:28:54 +11001723 be16_add(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 } else {
1725 map = &hdr->freemap[after];
1726 /* both on-disk, don't endian flip twice */
1727 map->base = entry->nameidx;
Nathan Scott918ae422006-03-17 17:28:54 +11001728 be16_add(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730 } else {
1731 /*
1732 * Replace smallest region (if it is smaller than free'd entry)
1733 */
1734 map = &hdr->freemap[smallest];
Nathan Scott918ae422006-03-17 17:28:54 +11001735 if (be16_to_cpu(map->size) < entsize) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001736 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
Nathan Scott918ae422006-03-17 17:28:54 +11001737 map->size = cpu_to_be16(entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739 }
1740
1741 /*
1742 * Did we remove the first entry?
1743 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001744 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 smallest = 1;
1746 else
1747 smallest = 0;
1748
1749 /*
1750 * Compress the remaining entries and zero out the removed stuff.
1751 */
1752 memset(XFS_ATTR_LEAF_NAME(leaf, args->index), 0, entsize);
Nathan Scott918ae422006-03-17 17:28:54 +11001753 be16_add(&hdr->usedbytes, -entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 xfs_da_log_buf(args->trans, bp,
1755 XFS_DA_LOGRANGE(leaf, XFS_ATTR_LEAF_NAME(leaf, args->index),
1756 entsize));
1757
Nathan Scott918ae422006-03-17 17:28:54 +11001758 tmp = (be16_to_cpu(hdr->count) - args->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 * sizeof(xfs_attr_leaf_entry_t);
1760 memmove((char *)entry, (char *)(entry+1), tmp);
Nathan Scott918ae422006-03-17 17:28:54 +11001761 be16_add(&hdr->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 xfs_da_log_buf(args->trans, bp,
1763 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
Nathan Scott918ae422006-03-17 17:28:54 +11001764 entry = &leaf->entries[be16_to_cpu(hdr->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1766
1767 /*
1768 * If we removed the first entry, re-find the first used byte
1769 * in the name area. Note that if the entry was the "firstused",
1770 * then we don't have a "hole" in our block resulting from
1771 * removing the name.
1772 */
1773 if (smallest) {
1774 tmp = XFS_LBSIZE(mp);
1775 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001776 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001777 ASSERT(be16_to_cpu(entry->nameidx) >=
1778 be16_to_cpu(hdr->firstused));
1779 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1780
1781 if (be16_to_cpu(entry->nameidx) < tmp)
1782 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
Nathan Scott918ae422006-03-17 17:28:54 +11001784 hdr->firstused = cpu_to_be16(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001786 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1788 }
1789 } else {
1790 hdr->holes = 1; /* mark as needing compaction */
1791 }
1792 xfs_da_log_buf(args->trans, bp,
1793 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1794
1795 /*
1796 * Check if leaf is less than 50% full, caller may want to
1797 * "join" the leaf with a sibling if so.
1798 */
1799 tmp = sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001800 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1801 tmp += be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1803}
1804
1805/*
1806 * Move all the attribute list entries from drop_leaf into save_leaf.
1807 */
1808void
1809xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1810 xfs_da_state_blk_t *save_blk)
1811{
1812 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1813 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1814 xfs_mount_t *mp;
1815 char *tmpbuffer;
1816
1817 /*
1818 * Set up environment.
1819 */
1820 mp = state->mp;
1821 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1822 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1823 drop_leaf = drop_blk->bp->data;
1824 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001825 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
1826 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 drop_hdr = &drop_leaf->hdr;
1828 save_hdr = &save_leaf->hdr;
1829
1830 /*
1831 * Save last hashval from dying block for later Btree fixup.
1832 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001833 drop_blk->hashval = be32_to_cpu(
1834 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 /*
1837 * Check if we need a temp buffer, or can we do it in place.
1838 * Note that we don't check "leaf" for holes because we will
1839 * always be dropping it, toosmall() decided that for us already.
1840 */
1841 if (save_hdr->holes == 0) {
1842 /*
1843 * dest leaf has no holes, so we add there. May need
1844 * to make some room in the entry array.
1845 */
1846 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1847 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001848 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 } else {
1850 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001851 be16_to_cpu(save_hdr->count),
1852 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
1854 } else {
1855 /*
1856 * Destination has holes, so we make a temporary copy
1857 * of the leaf and add them both to that.
1858 */
1859 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1860 ASSERT(tmpbuffer != NULL);
1861 memset(tmpbuffer, 0, state->blocksize);
1862 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1863 tmp_hdr = &tmp_leaf->hdr;
1864 tmp_hdr->info = save_hdr->info; /* struct copy */
1865 tmp_hdr->count = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001866 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (!tmp_hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001868 tmp_hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1870 }
1871 tmp_hdr->usedbytes = 0;
1872 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1873 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001874 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001876 be16_to_cpu(tmp_leaf->hdr.count),
1877 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 } else {
1879 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001880 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001882 be16_to_cpu(tmp_leaf->hdr.count),
1883 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
1886 kmem_free(tmpbuffer, state->blocksize);
1887 }
1888
1889 xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1890 state->blocksize - 1);
1891
1892 /*
1893 * Copy out last hashval in each block for B-tree code.
1894 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001895 save_blk->hashval = be32_to_cpu(
1896 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
1899/*========================================================================
1900 * Routines used for finding things in the Btree.
1901 *========================================================================*/
1902
1903/*
1904 * Look up a name in a leaf attribute list structure.
1905 * This is the internal routine, it uses the caller's buffer.
1906 *
1907 * Note that duplicate keys are allowed, but only check within the
1908 * current leaf node. The Btree code must check in adjacent leaf nodes.
1909 *
1910 * Return in args->index the index into the entry[] array of either
1911 * the found entry, or where the entry should have been (insert before
1912 * that entry).
1913 *
1914 * Don't change the args->value unless we find the attribute.
1915 */
1916int
1917xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
1918{
1919 xfs_attr_leafblock_t *leaf;
1920 xfs_attr_leaf_entry_t *entry;
1921 xfs_attr_leaf_name_local_t *name_loc;
1922 xfs_attr_leaf_name_remote_t *name_rmt;
1923 int probe, span;
1924 xfs_dahash_t hashval;
1925
1926 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001927 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11001928 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 < (XFS_LBSIZE(args->dp->i_mount)/8));
1930
1931 /*
1932 * Binary search. (note: small blocks will skip this loop)
1933 */
1934 hashval = args->hashval;
Nathan Scott918ae422006-03-17 17:28:54 +11001935 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 for (entry = &leaf->entries[probe]; span > 4;
1937 entry = &leaf->entries[probe]) {
1938 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001939 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001941 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 probe -= span;
1943 else
1944 break;
1945 }
Tim Shimmin726801b2006-09-28 11:01:37 +10001946 ASSERT((probe >= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 (!leaf->hdr.count
Nathan Scott918ae422006-03-17 17:28:54 +11001948 || (probe < be16_to_cpu(leaf->hdr.count))));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001949 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /*
1952 * Since we may have duplicate hashval's, find the first matching
1953 * hashval in the leaf.
1954 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001955 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 entry--;
1957 probe--;
1958 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001959 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
1960 (be32_to_cpu(entry->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 entry++;
1962 probe++;
1963 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001964 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
1965 (be32_to_cpu(entry->hashval) != hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 args->index = probe;
1967 return(XFS_ERROR(ENOATTR));
1968 }
1969
1970 /*
1971 * Duplicate keys may be present, so search all of them for a match.
1972 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001973 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
1974 (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 entry++, probe++) {
1976/*
1977 * GROT: Add code to remove incomplete entries.
1978 */
1979 /*
1980 * If we are looking for INCOMPLETE entries, show only those.
1981 * If we are looking for complete entries, show only those.
1982 */
1983 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
1984 (entry->flags & XFS_ATTR_INCOMPLETE)) {
1985 continue;
1986 }
1987 if (entry->flags & XFS_ATTR_LOCAL) {
1988 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, probe);
1989 if (name_loc->namelen != args->namelen)
1990 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10001991 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10001993 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 continue;
1995 args->index = probe;
1996 return(XFS_ERROR(EEXIST));
1997 } else {
1998 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, probe);
1999 if (name_rmt->namelen != args->namelen)
2000 continue;
2001 if (memcmp(args->name, (char *)name_rmt->name,
2002 args->namelen) != 0)
2003 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002004 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 continue;
2006 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002007 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002009 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return(XFS_ERROR(EEXIST));
2011 }
2012 }
2013 args->index = probe;
2014 return(XFS_ERROR(ENOATTR));
2015}
2016
2017/*
2018 * Get the value associated with an attribute name from a leaf attribute
2019 * list structure.
2020 */
2021int
2022xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
2023{
2024 int valuelen;
2025 xfs_attr_leafblock_t *leaf;
2026 xfs_attr_leaf_entry_t *entry;
2027 xfs_attr_leaf_name_local_t *name_loc;
2028 xfs_attr_leaf_name_remote_t *name_rmt;
2029
2030 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002031 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002032 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 < (XFS_LBSIZE(args->dp->i_mount)/8));
Nathan Scott918ae422006-03-17 17:28:54 +11002034 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 entry = &leaf->entries[args->index];
2037 if (entry->flags & XFS_ATTR_LOCAL) {
2038 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2039 ASSERT(name_loc->namelen == args->namelen);
2040 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002041 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (args->flags & ATTR_KERNOVAL) {
2043 args->valuelen = valuelen;
2044 return(0);
2045 }
2046 if (args->valuelen < valuelen) {
2047 args->valuelen = valuelen;
2048 return(XFS_ERROR(ERANGE));
2049 }
2050 args->valuelen = valuelen;
2051 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2052 } else {
2053 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2054 ASSERT(name_rmt->namelen == args->namelen);
2055 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002056 valuelen = be32_to_cpu(name_rmt->valuelen);
2057 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2059 if (args->flags & ATTR_KERNOVAL) {
2060 args->valuelen = valuelen;
2061 return(0);
2062 }
2063 if (args->valuelen < valuelen) {
2064 args->valuelen = valuelen;
2065 return(XFS_ERROR(ERANGE));
2066 }
2067 args->valuelen = valuelen;
2068 }
2069 return(0);
2070}
2071
2072/*========================================================================
2073 * Utility routines.
2074 *========================================================================*/
2075
2076/*
2077 * Move the indicated entries from one leaf to another.
2078 * NOTE: this routine modifies both source and destination leaves.
2079 */
2080/*ARGSUSED*/
2081STATIC void
2082xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2083 xfs_attr_leafblock_t *leaf_d, int start_d,
2084 int count, xfs_mount_t *mp)
2085{
2086 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2087 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2088 int desti, tmp, i;
2089
2090 /*
2091 * Check for nothing to do.
2092 */
2093 if (count == 0)
2094 return;
2095
2096 /*
2097 * Set up environment.
2098 */
Nathan Scott89da0542006-03-17 17:28:40 +11002099 ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
2100 ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 hdr_s = &leaf_s->hdr;
2102 hdr_d = &leaf_d->hdr;
Nathan Scott918ae422006-03-17 17:28:54 +11002103 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2104 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2105 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2106 ((be16_to_cpu(hdr_s->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 * sizeof(*entry_s))+sizeof(*hdr_s)));
Nathan Scott918ae422006-03-17 17:28:54 +11002108 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2109 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2110 ((be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 * sizeof(*entry_d))+sizeof(*hdr_d)));
2112
Nathan Scott918ae422006-03-17 17:28:54 +11002113 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2114 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2115 ASSERT(count <= be16_to_cpu(hdr_s->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 /*
2118 * Move the entries in the destination leaf up to make a hole?
2119 */
Nathan Scott918ae422006-03-17 17:28:54 +11002120 if (start_d < be16_to_cpu(hdr_d->count)) {
2121 tmp = be16_to_cpu(hdr_d->count) - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 tmp *= sizeof(xfs_attr_leaf_entry_t);
2123 entry_s = &leaf_d->entries[start_d];
2124 entry_d = &leaf_d->entries[start_d + count];
2125 memmove((char *)entry_d, (char *)entry_s, tmp);
2126 }
2127
2128 /*
2129 * Copy all entry's in the same (sorted) order,
2130 * but allocate attribute info packed and in sequence.
2131 */
2132 entry_s = &leaf_s->entries[start_s];
2133 entry_d = &leaf_d->entries[start_d];
2134 desti = start_d;
2135 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002136 ASSERT(be16_to_cpu(entry_s->nameidx)
Nathan Scott918ae422006-03-17 17:28:54 +11002137 >= be16_to_cpu(hdr_s->firstused));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2139#ifdef GROT
2140 /*
2141 * Code to drop INCOMPLETE entries. Difficult to use as we
2142 * may also need to change the insertion index. Code turned
2143 * off for 6.2, should be revisited later.
2144 */
2145 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2146 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
Nathan Scott918ae422006-03-17 17:28:54 +11002147 be16_add(&hdr_s->usedbytes, -tmp);
2148 be16_add(&hdr_s->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 entry_d--; /* to compensate for ++ in loop hdr */
2150 desti--;
2151 if ((start_s + i) < offset)
2152 result++; /* insertion index adjustment */
2153 } else {
2154#endif /* GROT */
Nathan Scott918ae422006-03-17 17:28:54 +11002155 be16_add(&hdr_d->firstused, -tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 /* both on-disk, don't endian flip twice */
2157 entry_d->hashval = entry_s->hashval;
2158 /* both on-disk, don't endian flip twice */
2159 entry_d->nameidx = hdr_d->firstused;
2160 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002161 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 <= XFS_LBSIZE(mp));
2163 memmove(XFS_ATTR_LEAF_NAME(leaf_d, desti),
2164 XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002165 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 <= XFS_LBSIZE(mp));
2167 memset(XFS_ATTR_LEAF_NAME(leaf_s, start_s + i), 0, tmp);
Nathan Scott918ae422006-03-17 17:28:54 +11002168 be16_add(&hdr_s->usedbytes, -tmp);
2169 be16_add(&hdr_d->usedbytes, tmp);
2170 be16_add(&hdr_s->count, -1);
2171 be16_add(&hdr_d->count, 1);
2172 tmp = be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 * sizeof(xfs_attr_leaf_entry_t)
2174 + sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002175 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#ifdef GROT
2177 }
2178#endif /* GROT */
2179 }
2180
2181 /*
2182 * Zero out the entries we just copied.
2183 */
Nathan Scott918ae422006-03-17 17:28:54 +11002184 if (start_s == be16_to_cpu(hdr_s->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2186 entry_s = &leaf_s->entries[start_s];
2187 ASSERT(((char *)entry_s + tmp) <=
2188 ((char *)leaf_s + XFS_LBSIZE(mp)));
2189 memset((char *)entry_s, 0, tmp);
2190 } else {
2191 /*
2192 * Move the remaining entries down to fill the hole,
2193 * then zero the entries at the top.
2194 */
Nathan Scott918ae422006-03-17 17:28:54 +11002195 tmp = be16_to_cpu(hdr_s->count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 tmp *= sizeof(xfs_attr_leaf_entry_t);
2197 entry_s = &leaf_s->entries[start_s + count];
2198 entry_d = &leaf_s->entries[start_s];
2199 memmove((char *)entry_d, (char *)entry_s, tmp);
2200
2201 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002202 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 ASSERT(((char *)entry_s + tmp) <=
2204 ((char *)leaf_s + XFS_LBSIZE(mp)));
2205 memset((char *)entry_s, 0, tmp);
2206 }
2207
2208 /*
2209 * Fill in the freemap information
2210 */
Nathan Scott918ae422006-03-17 17:28:54 +11002211 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
2212 be16_add(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
2213 sizeof(xfs_attr_leaf_entry_t));
2214 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2215 - be16_to_cpu(hdr_d->freemap[0].base));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 hdr_d->freemap[1].base = 0;
2217 hdr_d->freemap[2].base = 0;
2218 hdr_d->freemap[1].size = 0;
2219 hdr_d->freemap[2].size = 0;
2220 hdr_s->holes = 1; /* leaf may not be compact */
2221}
2222
2223/*
2224 * Compare two leaf blocks "order".
2225 * Return 0 unless leaf2 should go before leaf1.
2226 */
2227int
2228xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
2229{
2230 xfs_attr_leafblock_t *leaf1, *leaf2;
2231
2232 leaf1 = leaf1_bp->data;
2233 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002234 ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC) &&
2235 (be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002236 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2237 (be16_to_cpu(leaf2->hdr.count) > 0) &&
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002238 ((be32_to_cpu(leaf2->entries[0].hashval) <
2239 be32_to_cpu(leaf1->entries[0].hashval)) ||
2240 (be32_to_cpu(leaf2->entries[
2241 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2242 be32_to_cpu(leaf1->entries[
2243 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return(1);
2245 }
2246 return(0);
2247}
2248
2249/*
2250 * Pick up the last hashvalue from a leaf block.
2251 */
2252xfs_dahash_t
2253xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
2254{
2255 xfs_attr_leafblock_t *leaf;
2256
2257 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002258 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (count)
Nathan Scott918ae422006-03-17 17:28:54 +11002260 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (!leaf->hdr.count)
2262 return(0);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002263 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
2266/*
2267 * Calculate the number of bytes used to store the indicated attribute
2268 * (whether local or remote only calculate bytes in this block).
2269 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002270STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2272{
2273 xfs_attr_leaf_name_local_t *name_loc;
2274 xfs_attr_leaf_name_remote_t *name_rmt;
2275 int size;
2276
Nathan Scott89da0542006-03-17 17:28:40 +11002277 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
2279 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, index);
2280 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002281 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 } else {
2283 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, index);
2284 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(name_rmt->namelen);
2285 }
2286 return(size);
2287}
2288
2289/*
2290 * Calculate the number of bytes that would be required to store the new
2291 * attribute (whether local or remote only calculate bytes in this block).
2292 * This routine decides as a side effect whether the attribute will be
2293 * a "local" or a "remote" attribute.
2294 */
2295int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002296xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 int size;
2299
Nathan Scottaa82daa2005-11-02 10:33:33 +11002300 size = XFS_ATTR_LEAF_ENTSIZE_LOCAL(namelen, valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (size < XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(blocksize)) {
2302 if (local) {
2303 *local = 1;
2304 }
2305 } else {
Nathan Scottaa82daa2005-11-02 10:33:33 +11002306 size = XFS_ATTR_LEAF_ENTSIZE_REMOTE(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (local) {
2308 *local = 0;
2309 }
2310 }
2311 return(size);
2312}
2313
2314/*
2315 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2316 */
2317int
2318xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
2319{
2320 attrlist_cursor_kern_t *cursor;
2321 xfs_attr_leafblock_t *leaf;
2322 xfs_attr_leaf_entry_t *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 int retval, i;
2324
2325 ASSERT(bp != NULL);
2326 leaf = bp->data;
2327 cursor = context->cursor;
2328 cursor->initted = 1;
2329
2330 xfs_attr_trace_l_cl("blk start", context, leaf);
2331
2332 /*
2333 * Re-find our place in the leaf block if this is a new syscall.
2334 */
2335 if (context->resynch) {
2336 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002337 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002338 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (cursor->offset == context->dupcnt) {
2340 context->dupcnt = 0;
2341 break;
2342 }
2343 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002344 } else if (be32_to_cpu(entry->hashval) >
2345 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 context->dupcnt = 0;
2347 break;
2348 }
2349 }
Nathan Scott918ae422006-03-17 17:28:54 +11002350 if (i == be16_to_cpu(leaf->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 xfs_attr_trace_l_c("not found", context);
2352 return(0);
2353 }
2354 } else {
2355 entry = &leaf->entries[0];
2356 i = 0;
2357 }
2358 context->resynch = 0;
2359
2360 /*
2361 * We have found our place, start copying out the new attributes.
2362 */
2363 retval = 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10002364 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
2365 attrnames_t *namesp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002367 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2368 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 cursor->offset = 0;
2370 }
2371
2372 if (entry->flags & XFS_ATTR_INCOMPLETE)
2373 continue; /* skip incomplete entries */
Tim Shimmin726801b2006-09-28 11:01:37 +10002374 if (!xfs_attr_namesp_match_overrides(context->flags, entry->flags))
2375 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Tim Shimmin726801b2006-09-28 11:01:37 +10002377 namesp = xfs_attr_flags_namesp(entry->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002380 xfs_attr_leaf_name_local_t *name_loc =
2381 XFS_ATTR_LEAF_NAME_LOCAL(leaf, i);
2382
2383 retval = context->put_listent(context,
2384 namesp,
2385 (char *)name_loc->nameval,
2386 (int)name_loc->namelen,
2387 be16_to_cpu(name_loc->valuelen),
2388 (char *)&name_loc->nameval[name_loc->namelen]);
2389 if (retval)
2390 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002392 xfs_attr_leaf_name_remote_t *name_rmt =
2393 XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2394
2395 int valuelen = be32_to_cpu(name_rmt->valuelen);
2396
2397 if (context->put_value) {
2398 xfs_da_args_t args;
2399
2400 memset((char *)&args, 0, sizeof(args));
2401 args.dp = context->dp;
2402 args.whichfork = XFS_ATTR_FORK;
2403 args.valuelen = valuelen;
2404 args.value = kmem_alloc(valuelen, KM_SLEEP);
2405 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2406 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2407 retval = xfs_attr_rmtval_get(&args);
2408 if (retval)
2409 return retval;
2410 retval = context->put_listent(context,
2411 namesp,
2412 (char *)name_rmt->name,
2413 (int)name_rmt->namelen,
2414 valuelen,
2415 (char*)args.value);
2416 kmem_free(args.value, valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002418 else {
2419 retval = context->put_listent(context,
2420 namesp,
2421 (char *)name_rmt->name,
2422 (int)name_rmt->namelen,
2423 valuelen,
2424 NULL);
2425 }
2426 if (retval)
2427 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002429 if (context->seen_enough)
2430 break;
2431 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 }
2433 xfs_attr_trace_l_cl("blk end", context, leaf);
2434 return(retval);
2435}
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438/*========================================================================
2439 * Manage the INCOMPLETE flag in a leaf entry
2440 *========================================================================*/
2441
2442/*
2443 * Clear the INCOMPLETE flag on an entry in a leaf block.
2444 */
2445int
2446xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2447{
2448 xfs_attr_leafblock_t *leaf;
2449 xfs_attr_leaf_entry_t *entry;
2450 xfs_attr_leaf_name_remote_t *name_rmt;
2451 xfs_dabuf_t *bp;
2452 int error;
2453#ifdef DEBUG
2454 xfs_attr_leaf_name_local_t *name_loc;
2455 int namelen;
2456 char *name;
2457#endif /* DEBUG */
2458
2459 /*
2460 * Set up the operation.
2461 */
2462 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2463 XFS_ATTR_FORK);
2464 if (error) {
2465 return(error);
2466 }
2467 ASSERT(bp != NULL);
2468
2469 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002470 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002471 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 ASSERT(args->index >= 0);
2473 entry = &leaf->entries[ args->index ];
2474 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2475
2476#ifdef DEBUG
2477 if (entry->flags & XFS_ATTR_LOCAL) {
2478 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf, args->index);
2479 namelen = name_loc->namelen;
2480 name = (char *)name_loc->nameval;
2481 } else {
2482 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2483 namelen = name_rmt->namelen;
2484 name = (char *)name_rmt->name;
2485 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002486 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 ASSERT(namelen == args->namelen);
2488 ASSERT(memcmp(name, args->name, namelen) == 0);
2489#endif /* DEBUG */
2490
2491 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2492 xfs_da_log_buf(args->trans, bp,
2493 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2494
2495 if (args->rmtblkno) {
2496 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2497 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002498 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2499 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 xfs_da_log_buf(args->trans, bp,
2501 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2502 }
2503 xfs_da_buf_done(bp);
2504
2505 /*
2506 * Commit the flag value change and start the next trans in series.
2507 */
2508 error = xfs_attr_rolltrans(&args->trans, args->dp);
2509
2510 return(error);
2511}
2512
2513/*
2514 * Set the INCOMPLETE flag on an entry in a leaf block.
2515 */
2516int
2517xfs_attr_leaf_setflag(xfs_da_args_t *args)
2518{
2519 xfs_attr_leafblock_t *leaf;
2520 xfs_attr_leaf_entry_t *entry;
2521 xfs_attr_leaf_name_remote_t *name_rmt;
2522 xfs_dabuf_t *bp;
2523 int error;
2524
2525 /*
2526 * Set up the operation.
2527 */
2528 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
2529 XFS_ATTR_FORK);
2530 if (error) {
2531 return(error);
2532 }
2533 ASSERT(bp != NULL);
2534
2535 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002536 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002537 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 ASSERT(args->index >= 0);
2539 entry = &leaf->entries[ args->index ];
2540
2541 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2542 entry->flags |= XFS_ATTR_INCOMPLETE;
2543 xfs_da_log_buf(args->trans, bp,
2544 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2545 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2546 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, args->index);
2547 name_rmt->valueblk = 0;
2548 name_rmt->valuelen = 0;
2549 xfs_da_log_buf(args->trans, bp,
2550 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2551 }
2552 xfs_da_buf_done(bp);
2553
2554 /*
2555 * Commit the flag value change and start the next trans in series.
2556 */
2557 error = xfs_attr_rolltrans(&args->trans, args->dp);
2558
2559 return(error);
2560}
2561
2562/*
2563 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2564 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2565 * entry given by args->blkno2/index2.
2566 *
2567 * Note that they could be in different blocks, or in the same block.
2568 */
2569int
2570xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2571{
2572 xfs_attr_leafblock_t *leaf1, *leaf2;
2573 xfs_attr_leaf_entry_t *entry1, *entry2;
2574 xfs_attr_leaf_name_remote_t *name_rmt;
2575 xfs_dabuf_t *bp1, *bp2;
2576 int error;
2577#ifdef DEBUG
2578 xfs_attr_leaf_name_local_t *name_loc;
2579 int namelen1, namelen2;
2580 char *name1, *name2;
2581#endif /* DEBUG */
2582
2583 /*
2584 * Read the block containing the "old" attr
2585 */
2586 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
2587 XFS_ATTR_FORK);
2588 if (error) {
2589 return(error);
2590 }
2591 ASSERT(bp1 != NULL);
2592
2593 /*
2594 * Read the block containing the "new" attr, if it is different
2595 */
2596 if (args->blkno2 != args->blkno) {
2597 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
2598 -1, &bp2, XFS_ATTR_FORK);
2599 if (error) {
2600 return(error);
2601 }
2602 ASSERT(bp2 != NULL);
2603 } else {
2604 bp2 = bp1;
2605 }
2606
2607 leaf1 = bp1->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002608 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002609 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 ASSERT(args->index >= 0);
2611 entry1 = &leaf1->entries[ args->index ];
2612
2613 leaf2 = bp2->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002614 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +11002615 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 ASSERT(args->index2 >= 0);
2617 entry2 = &leaf2->entries[ args->index2 ];
2618
2619#ifdef DEBUG
2620 if (entry1->flags & XFS_ATTR_LOCAL) {
2621 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf1, args->index);
2622 namelen1 = name_loc->namelen;
2623 name1 = (char *)name_loc->nameval;
2624 } else {
2625 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
2626 namelen1 = name_rmt->namelen;
2627 name1 = (char *)name_rmt->name;
2628 }
2629 if (entry2->flags & XFS_ATTR_LOCAL) {
2630 name_loc = XFS_ATTR_LEAF_NAME_LOCAL(leaf2, args->index2);
2631 namelen2 = name_loc->namelen;
2632 name2 = (char *)name_loc->nameval;
2633 } else {
2634 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2635 namelen2 = name_rmt->namelen;
2636 name2 = (char *)name_rmt->name;
2637 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002638 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 ASSERT(namelen1 == namelen2);
2640 ASSERT(memcmp(name1, name2, namelen1) == 0);
2641#endif /* DEBUG */
2642
2643 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2644 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2645
2646 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2647 xfs_da_log_buf(args->trans, bp1,
2648 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2649 if (args->rmtblkno) {
2650 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2651 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002652 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2653 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 xfs_da_log_buf(args->trans, bp1,
2655 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2656 }
2657
2658 entry2->flags |= XFS_ATTR_INCOMPLETE;
2659 xfs_da_log_buf(args->trans, bp2,
2660 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2661 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2662 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf2, args->index2);
2663 name_rmt->valueblk = 0;
2664 name_rmt->valuelen = 0;
2665 xfs_da_log_buf(args->trans, bp2,
2666 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2667 }
2668 xfs_da_buf_done(bp1);
2669 if (bp1 != bp2)
2670 xfs_da_buf_done(bp2);
2671
2672 /*
2673 * Commit the flag value change and start the next trans in series.
2674 */
2675 error = xfs_attr_rolltrans(&args->trans, args->dp);
2676
2677 return(error);
2678}
2679
2680/*========================================================================
2681 * Indiscriminately delete the entire attribute fork
2682 *========================================================================*/
2683
2684/*
2685 * Recurse (gasp!) through the attribute nodes until we find leaves.
2686 * We're doing a depth-first traversal in order to invalidate everything.
2687 */
2688int
2689xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2690{
2691 xfs_da_blkinfo_t *info;
2692 xfs_daddr_t blkno;
2693 xfs_dabuf_t *bp;
2694 int error;
2695
2696 /*
2697 * Read block 0 to see what we have to work with.
2698 * We only get here if we have extents, since we remove
2699 * the extents in reverse order the extent containing
2700 * block 0 must still be there.
2701 */
2702 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
2703 if (error)
2704 return(error);
2705 blkno = xfs_da_blkno(bp);
2706
2707 /*
2708 * Invalidate the tree, even if the "tree" is only a single leaf block.
2709 * This is a depth-first traversal!
2710 */
2711 info = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002712 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 error = xfs_attr_node_inactive(trans, dp, bp, 1);
Nathan Scott89da0542006-03-17 17:28:40 +11002714 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 error = xfs_attr_leaf_inactive(trans, dp, bp);
2716 } else {
2717 error = XFS_ERROR(EIO);
2718 xfs_da_brelse(*trans, bp);
2719 }
2720 if (error)
2721 return(error);
2722
2723 /*
2724 * Invalidate the incore copy of the root block.
2725 */
2726 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2727 if (error)
2728 return(error);
2729 xfs_da_binval(*trans, bp); /* remove from cache */
2730 /*
2731 * Commit the invalidate and start the next transaction.
2732 */
2733 error = xfs_attr_rolltrans(trans, dp);
2734
2735 return (error);
2736}
2737
2738/*
2739 * Recurse (gasp!) through the attribute nodes until we find leaves.
2740 * We're doing a depth-first traversal in order to invalidate everything.
2741 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002742STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
2744 int level)
2745{
2746 xfs_da_blkinfo_t *info;
2747 xfs_da_intnode_t *node;
2748 xfs_dablk_t child_fsb;
2749 xfs_daddr_t parent_blkno, child_blkno;
2750 int error, count, i;
2751 xfs_dabuf_t *child_bp;
2752
2753 /*
2754 * Since this code is recursive (gasp!) we must protect ourselves.
2755 */
2756 if (level > XFS_DA_NODE_MAXDEPTH) {
2757 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2758 return(XFS_ERROR(EIO));
2759 }
2760
2761 node = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002762 ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
Nathan Scottfac80cc2006-03-17 17:29:56 +11002764 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (!count) {
2766 xfs_da_brelse(*trans, bp);
2767 return(0);
2768 }
Nathan Scott403432d2006-03-17 17:29:46 +11002769 child_fsb = be32_to_cpu(node->btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 xfs_da_brelse(*trans, bp); /* no locks for later trans */
2771
2772 /*
2773 * If this is the node level just above the leaves, simply loop
2774 * over the leaves removing all of them. If this is higher up
2775 * in the tree, recurse downward.
2776 */
2777 for (i = 0; i < count; i++) {
2778 /*
2779 * Read the subsidiary block to see what we have to work with.
2780 * Don't do this in a transaction. This is a depth-first
2781 * traversal of the tree so we may deal with many blocks
2782 * before we come back to this one.
2783 */
2784 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
2785 XFS_ATTR_FORK);
2786 if (error)
2787 return(error);
2788 if (child_bp) {
2789 /* save for re-read later */
2790 child_blkno = xfs_da_blkno(child_bp);
2791
2792 /*
2793 * Invalidate the subtree, however we have to.
2794 */
2795 info = child_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002796 if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 error = xfs_attr_node_inactive(trans, dp,
2798 child_bp, level+1);
Nathan Scott89da0542006-03-17 17:28:40 +11002799 } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 error = xfs_attr_leaf_inactive(trans, dp,
2801 child_bp);
2802 } else {
2803 error = XFS_ERROR(EIO);
2804 xfs_da_brelse(*trans, child_bp);
2805 }
2806 if (error)
2807 return(error);
2808
2809 /*
2810 * Remove the subsidiary block from the cache
2811 * and from the log.
2812 */
2813 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2814 &child_bp, XFS_ATTR_FORK);
2815 if (error)
2816 return(error);
2817 xfs_da_binval(*trans, child_bp);
2818 }
2819
2820 /*
2821 * If we're not done, re-read the parent to get the next
2822 * child block number.
2823 */
2824 if ((i+1) < count) {
2825 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
2826 &bp, XFS_ATTR_FORK);
2827 if (error)
2828 return(error);
Nathan Scott403432d2006-03-17 17:29:46 +11002829 child_fsb = be32_to_cpu(node->btree[i+1].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 xfs_da_brelse(*trans, bp);
2831 }
2832 /*
2833 * Atomically commit the whole invalidate stuff.
2834 */
2835 if ((error = xfs_attr_rolltrans(trans, dp)))
2836 return (error);
2837 }
2838
2839 return(0);
2840}
2841
2842/*
2843 * Invalidate all of the "remote" value regions pointed to by a particular
2844 * leaf block.
2845 * Note that we must release the lock on the buffer so that we are not
2846 * caught holding something that the logging code wants to flush to disk.
2847 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002848STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
2850{
2851 xfs_attr_leafblock_t *leaf;
2852 xfs_attr_leaf_entry_t *entry;
2853 xfs_attr_leaf_name_remote_t *name_rmt;
2854 xfs_attr_inactive_list_t *list, *lp;
2855 int error, count, size, tmp, i;
2856
2857 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11002858 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 /*
2861 * Count the number of "remote" value extents.
2862 */
2863 count = 0;
2864 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002865 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002866 if (be16_to_cpu(entry->nameidx) &&
2867 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2869 if (name_rmt->valueblk)
2870 count++;
2871 }
2872 }
2873
2874 /*
2875 * If there are no "remote" values, we're done.
2876 */
2877 if (count == 0) {
2878 xfs_da_brelse(*trans, bp);
2879 return(0);
2880 }
2881
2882 /*
2883 * Allocate storage for a list of all the "remote" value extents.
2884 */
2885 size = count * sizeof(xfs_attr_inactive_list_t);
2886 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2887
2888 /*
2889 * Identify each of the "remote" value extents.
2890 */
2891 lp = list;
2892 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002893 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002894 if (be16_to_cpu(entry->nameidx) &&
2895 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 name_rmt = XFS_ATTR_LEAF_NAME_REMOTE(leaf, i);
2897 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11002898 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2899 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2900 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 lp++;
2902 }
2903 }
2904 }
2905 xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
2906
2907 /*
2908 * Invalidate each of the "remote" value extents.
2909 */
2910 error = 0;
2911 for (lp = list, i = 0; i < count; i++, lp++) {
2912 tmp = xfs_attr_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11002913 lp->valueblk, lp->valuelen);
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (error == 0)
2916 error = tmp; /* save only the 1st errno */
2917 }
2918
2919 kmem_free((xfs_caddr_t)list, size);
2920 return(error);
2921}
2922
2923/*
2924 * Look at all the extents for this logical region,
2925 * invalidate any buffers that are incore/in transactions.
2926 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002927STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2929 xfs_dablk_t blkno, int blkcnt)
2930{
2931 xfs_bmbt_irec_t map;
2932 xfs_dablk_t tblkno;
2933 int tblkcnt, dblkcnt, nmap, error;
2934 xfs_daddr_t dblkno;
2935 xfs_buf_t *bp;
2936
2937 /*
2938 * Roll through the "value", invalidating the attribute value's
2939 * blocks.
2940 */
2941 tblkno = blkno;
2942 tblkcnt = blkcnt;
2943 while (tblkcnt > 0) {
2944 /*
2945 * Try to remember where we decided to put the value.
2946 */
2947 nmap = 1;
2948 error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
2949 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002950 NULL, 0, &map, &nmap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (error) {
2952 return(error);
2953 }
2954 ASSERT(nmap == 1);
2955 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
2956
2957 /*
2958 * If it's a hole, these are already unmapped
2959 * so there's nothing to invalidate.
2960 */
2961 if (map.br_startblock != HOLESTARTBLOCK) {
2962
2963 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
2964 map.br_startblock);
2965 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
2966 map.br_blockcount);
2967 bp = xfs_trans_get_buf(*trans,
2968 dp->i_mount->m_ddev_targp,
2969 dblkno, dblkcnt, XFS_BUF_LOCK);
2970 xfs_trans_binval(*trans, bp);
2971 /*
2972 * Roll to next transaction.
2973 */
2974 if ((error = xfs_attr_rolltrans(trans, dp)))
2975 return (error);
2976 }
2977
2978 tblkno += map.br_blockcount;
2979 tblkcnt -= map.br_blockcount;
2980 }
2981
2982 return(0);
2983}
2984
2985
2986/*
2987 * Roll from one trans in the sequence of PERMANENT transactions to the next.
2988 */
2989int
2990xfs_attr_rolltrans(xfs_trans_t **transp, xfs_inode_t *dp)
2991{
2992 xfs_trans_t *trans;
2993 unsigned int logres, count;
2994 int error;
2995
2996 /*
2997 * Ensure that the inode is always logged.
2998 */
2999 trans = *transp;
3000 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
3001
3002 /*
3003 * Copy the critical parameters from one trans to the next.
3004 */
3005 logres = trans->t_log_res;
3006 count = trans->t_log_count;
3007 *transp = xfs_trans_dup(trans);
3008
3009 /*
3010 * Commit the current transaction.
3011 * If this commit failed, then it'd just unlock those items that
3012 * are not marked ihold. That also means that a filesystem shutdown
3013 * is in progress. The caller takes the responsibility to cancel
3014 * the duplicate transaction that gets returned.
3015 */
3016 if ((error = xfs_trans_commit(trans, 0, NULL)))
3017 return (error);
3018
3019 trans = *transp;
3020
3021 /*
3022 * Reserve space in the log for th next transaction.
3023 * This also pushes items in the "AIL", the list of logged items,
3024 * out to disk if they are taking up space at the tail of the log
3025 * that we want to use. This requires that either nothing be locked
3026 * across this call, or that anything that is locked be logged in
3027 * the prior and the next transactions.
3028 */
3029 error = xfs_trans_reserve(trans, 0, logres, 0,
3030 XFS_TRANS_PERM_LOG_RES, count);
3031 /*
3032 * Ensure that the inode is in the new transaction and locked.
3033 */
3034 if (!error) {
3035 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
3036 xfs_trans_ihold(trans, dp);
3037 }
3038 return (error);
3039
3040}