blob: ee24993c7d121acacb06e9258f3f94655d814457 [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"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_ialloc_btree.h"
31#include "xfs_alloc.h"
32#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_attr.h"
39#include "xfs_attr_leaf.h"
40#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000041#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * xfs_attr_leaf.c
45 *
46 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
47 */
48
49/*========================================================================
50 * Function prototypes for the kernel.
51 *========================================================================*/
52
53/*
54 * Routines used for growing the Btree.
55 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100056STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
Dave Chinner1d9025e2012-06-22 18:50:14 +100057 struct xfs_buf **bpp);
58STATIC int xfs_attr_leaf_add_work(struct xfs_buf *leaf_buffer,
59 xfs_da_args_t *args, int freemap_index);
Dave Chinneree732592012-11-12 22:53:53 +110060STATIC void xfs_attr_leaf_compact(struct xfs_da_args *args,
61 struct xfs_buf *leaf_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
63 xfs_da_state_blk_t *blk1,
64 xfs_da_state_blk_t *blk2);
65STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
66 xfs_da_state_blk_t *leaf_blk_1,
67 xfs_da_state_blk_t *leaf_blk_2,
68 int *number_entries_in_blk1,
69 int *number_usedbytes_in_blk1);
70
71/*
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100072 * Routines used for shrinking the Btree.
73 */
74STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +100075 struct xfs_buf *bp, int level);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100076STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +100077 struct xfs_buf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100078STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
79 xfs_dablk_t blkno, int blkcnt);
80
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Utility routines.
83 */
84STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
85 int src_start,
86 xfs_attr_leafblock_t *dst_leaf,
87 int dst_start, int move_count,
88 xfs_mount_t *mp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100089STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
Tim Shimmin726801b2006-09-28 11:01:37 +100090
Dave Chinner612cfbf2012-11-14 17:52:32 +110091static void
Dave Chinnerad14c332012-11-12 22:54:16 +110092xfs_attr_leaf_verify(
93 struct xfs_buf *bp)
94{
95 struct xfs_mount *mp = bp->b_target->bt_mount;
96 struct xfs_attr_leaf_hdr *hdr = bp->b_addr;
97 int block_ok = 0;
98
99 block_ok = hdr->info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
100 if (!block_ok) {
101 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
102 xfs_buf_ioerror(bp, EFSCORRUPTED);
103 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100104}
Dave Chinnerad14c332012-11-12 22:54:16 +1100105
Dave Chinner612cfbf2012-11-14 17:52:32 +1100106static void
Dave Chinner1813dd62012-11-14 17:54:40 +1100107xfs_attr_leaf_read_verify(
108 struct xfs_buf *bp)
109{
110 xfs_attr_leaf_verify(bp);
111}
112
113static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100114xfs_attr_leaf_write_verify(
115 struct xfs_buf *bp)
116{
117 xfs_attr_leaf_verify(bp);
118}
119
Dave Chinner1813dd62012-11-14 17:54:40 +1100120const struct xfs_buf_ops xfs_attr_leaf_buf_ops = {
121 .verify_read = xfs_attr_leaf_read_verify,
122 .verify_write = xfs_attr_leaf_write_verify,
123};
Dave Chinner612cfbf2012-11-14 17:52:32 +1100124
Dave Chinnerad14c332012-11-12 22:54:16 +1100125int
126xfs_attr_leaf_read(
127 struct xfs_trans *tp,
128 struct xfs_inode *dp,
129 xfs_dablk_t bno,
130 xfs_daddr_t mappedbno,
131 struct xfs_buf **bpp)
132{
133 return xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100134 XFS_ATTR_FORK, &xfs_attr_leaf_buf_ops);
Dave Chinnerad14c332012-11-12 22:54:16 +1100135}
136
Tim Shimmin726801b2006-09-28 11:01:37 +1000137/*========================================================================
138 * Namespace helper routines
139 *========================================================================*/
140
Tim Shimmin726801b2006-09-28 11:01:37 +1000141/*
142 * If namespace bits don't match return 0.
143 * If all match then return 1.
144 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000145STATIC int
Tim Shimmin726801b2006-09-28 11:01:37 +1000146xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
147{
148 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100153 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *========================================================================*/
155
156/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100157 * Query whether the requested number of additional bytes of extended
158 * attribute space will be able to fit inline.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000159 *
Nathan Scottd8cc8902005-11-02 10:34:53 +1100160 * Returns zero if not, else the di_forkoff fork offset to be used in the
161 * literal area for attribute data once the new bytes have been added.
162 *
163 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
164 * special case for dev/uuid inodes, they have fixed size data forks.
165 */
166int
167xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
168{
169 int offset;
170 int minforkoff; /* lower limit on valid forkoff locations */
171 int maxforkoff; /* upper limit on valid forkoff locations */
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000172 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100173 xfs_mount_t *mp = dp->i_mount;
174
Nathan Scottd8cc8902005-11-02 10:34:53 +1100175 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
176
177 switch (dp->i_d.di_format) {
178 case XFS_DINODE_FMT_DEV:
179 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
180 return (offset >= minforkoff) ? minforkoff : 0;
181 case XFS_DINODE_FMT_UUID:
182 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
183 return (offset >= minforkoff) ? minforkoff : 0;
184 }
185
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000186 /*
187 * If the requested numbers of bytes is smaller or equal to the
188 * current attribute fork size we can always proceed.
189 *
190 * Note that if_bytes in the data fork might actually be larger than
191 * the current data fork size is due to delalloc extents. In that
192 * case either the extent count will go down when they are converted
193 * to real extents, or the delalloc conversion will take care of the
194 * literal area rebalancing.
195 */
196 if (bytes <= XFS_IFORK_ASIZE(dp))
197 return dp->i_d.di_forkoff;
198
199 /*
200 * For attr2 we can try to move the forkoff if there is space in the
201 * literal area, but for the old format we are done if there is no
202 * space in the fixed attribute fork.
203 */
204 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
Nathan Scottda087ba2005-11-02 15:00:20 +1100205 return 0;
Nathan Scottda087ba2005-11-02 15:00:20 +1100206
Barry Naujoke5889e92007-02-10 18:35:58 +1100207 dsize = dp->i_df.if_bytes;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000208
Barry Naujoke5889e92007-02-10 18:35:58 +1100209 switch (dp->i_d.di_format) {
210 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000211 /*
Barry Naujoke5889e92007-02-10 18:35:58 +1100212 * If there is no attr fork and the data fork is extents,
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000213 * determine if creating the default attr fork will result
214 * in the extents form migrating to btree. If so, the
215 * minimum offset only needs to be the space required for
Barry Naujoke5889e92007-02-10 18:35:58 +1100216 * the btree root.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000217 */
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200218 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
219 xfs_default_attroffset(dp))
Barry Naujoke5889e92007-02-10 18:35:58 +1100220 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
221 break;
Barry Naujoke5889e92007-02-10 18:35:58 +1100222 case XFS_DINODE_FMT_BTREE:
223 /*
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000224 * If we have a data btree then keep forkoff if we have one,
225 * otherwise we are adding a new attr, so then we set
226 * minforkoff to where the btree root can finish so we have
Barry Naujoke5889e92007-02-10 18:35:58 +1100227 * plenty of room for attrs
228 */
229 if (dp->i_d.di_forkoff) {
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000230 if (offset < dp->i_d.di_forkoff)
Barry Naujoke5889e92007-02-10 18:35:58 +1100231 return 0;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000232 return dp->i_d.di_forkoff;
233 }
234 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
Barry Naujoke5889e92007-02-10 18:35:58 +1100235 break;
236 }
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000237
238 /*
239 * A data fork btree root must have space for at least
Barry Naujoke5889e92007-02-10 18:35:58 +1100240 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
241 */
242 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100243 minforkoff = roundup(minforkoff, 8) >> 3;
244
245 /* attr fork btree root can have at least this many key/ptr pairs */
246 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
247 maxforkoff = maxforkoff >> 3; /* rounded down */
248
Nathan Scottd8cc8902005-11-02 10:34:53 +1100249 if (offset >= maxforkoff)
250 return maxforkoff;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000251 if (offset >= minforkoff)
252 return offset;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100253 return 0;
254}
255
256/*
257 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
258 */
259STATIC void
260xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
261{
Nathan Scott13059ff2006-01-11 15:32:01 +1100262 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100263 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000264 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100265 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
266 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000267 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100268 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
269 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000270 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100271 }
272}
273
274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * Create the initial contents of a shortform attribute list.
276 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100277void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278xfs_attr_shortform_create(xfs_da_args_t *args)
279{
280 xfs_attr_sf_hdr_t *hdr;
281 xfs_inode_t *dp;
282 xfs_ifork_t *ifp;
283
Dave Chinner5a5881c2012-03-22 05:15:13 +0000284 trace_xfs_attr_sf_create(args);
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 dp = args->dp;
287 ASSERT(dp != NULL);
288 ifp = dp->i_afp;
289 ASSERT(ifp != NULL);
290 ASSERT(ifp->if_bytes == 0);
291 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
292 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
293 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
294 ifp->if_flags |= XFS_IFINLINE;
295 } else {
296 ASSERT(ifp->if_flags & XFS_IFINLINE);
297 }
298 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
299 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
300 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100301 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/*
306 * Add a name/value pair to the shortform attribute list.
307 * Overflow from the inode has already been checked for.
308 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100309void
310xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 xfs_attr_shortform_t *sf;
313 xfs_attr_sf_entry_t *sfe;
314 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100315 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 xfs_inode_t *dp;
317 xfs_ifork_t *ifp;
318
Dave Chinner5a5881c2012-03-22 05:15:13 +0000319 trace_xfs_attr_sf_add(args);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100322 mp = dp->i_mount;
323 dp->i_d.di_forkoff = forkoff;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 ifp = dp->i_afp;
326 ASSERT(ifp->if_flags & XFS_IFINLINE);
327 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
328 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100329 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100330#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (sfe->namelen != args->namelen)
332 continue;
333 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
334 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000335 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100337 ASSERT(0);
338#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 offset = (char *)sfe - (char *)sf;
342 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
343 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
344 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
345 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
346
347 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100348 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000349 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 memcpy(sfe->nameval, args->name, args->namelen);
351 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100352 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800353 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
355
Nathan Scottd8cc8902005-11-02 10:34:53 +1100356 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359/*
Christoph Hellwige1486de2009-02-04 09:36:00 +0100360 * After the last attribute is removed revert to original inode format,
361 * making all literal area available to the data fork once more.
362 */
363STATIC void
364xfs_attr_fork_reset(
365 struct xfs_inode *ip,
366 struct xfs_trans *tp)
367{
368 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
369 ip->i_d.di_forkoff = 0;
370 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
371
372 ASSERT(ip->i_d.di_anextents == 0);
373 ASSERT(ip->i_afp == NULL);
374
Christoph Hellwige1486de2009-02-04 09:36:00 +0100375 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
376}
377
378/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100379 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
381int
382xfs_attr_shortform_remove(xfs_da_args_t *args)
383{
384 xfs_attr_shortform_t *sf;
385 xfs_attr_sf_entry_t *sfe;
386 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100387 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 xfs_inode_t *dp;
389
Dave Chinner5a5881c2012-03-22 05:15:13 +0000390 trace_xfs_attr_sf_remove(args);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100393 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 base = sizeof(xfs_attr_sf_hdr_t);
395 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
396 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100397 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100398 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 base += size, i++) {
400 size = XFS_ATTR_SF_ENTSIZE(sfe);
401 if (sfe->namelen != args->namelen)
402 continue;
403 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
404 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000405 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 continue;
407 break;
408 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100409 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return(XFS_ERROR(ENOATTR));
411
Nathan Scottd8cc8902005-11-02 10:34:53 +1100412 /*
413 * Fix up the attribute fork data, covering the hole
414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100416 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100417 if (end != totsize)
418 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100419 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800420 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100421
422 /*
423 * Fix up the start offset of the attribute fork
424 */
425 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000426 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
Christoph Hellwige1486de2009-02-04 09:36:00 +0100427 (mp->m_flags & XFS_MOUNT_ATTR2) &&
428 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
429 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
430 xfs_attr_fork_reset(dp, args->trans);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100431 } else {
432 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
433 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
434 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000435 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
436 (args->op_flags & XFS_DA_OP_ADDNAME) ||
437 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
438 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100439 xfs_trans_log_inode(args->trans, dp,
440 XFS_ILOG_CORE | XFS_ILOG_ADATA);
441 }
442
443 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 return(0);
446}
447
448/*
449 * Look up a name in a shortform attribute list structure.
450 */
451/*ARGSUSED*/
452int
453xfs_attr_shortform_lookup(xfs_da_args_t *args)
454{
455 xfs_attr_shortform_t *sf;
456 xfs_attr_sf_entry_t *sfe;
457 int i;
458 xfs_ifork_t *ifp;
459
Dave Chinner5a5881c2012-03-22 05:15:13 +0000460 trace_xfs_attr_sf_lookup(args);
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 ifp = args->dp->i_afp;
463 ASSERT(ifp->if_flags & XFS_IFINLINE);
464 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
465 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100466 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
468 if (sfe->namelen != args->namelen)
469 continue;
470 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
471 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000472 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 continue;
474 return(XFS_ERROR(EEXIST));
475 }
476 return(XFS_ERROR(ENOATTR));
477}
478
479/*
480 * Look up a name in a shortform attribute list structure.
481 */
482/*ARGSUSED*/
483int
484xfs_attr_shortform_getvalue(xfs_da_args_t *args)
485{
486 xfs_attr_shortform_t *sf;
487 xfs_attr_sf_entry_t *sfe;
488 int i;
489
490 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
491 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
492 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100493 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
495 if (sfe->namelen != args->namelen)
496 continue;
497 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
498 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000499 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 continue;
501 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100502 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return(XFS_ERROR(EEXIST));
504 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100505 if (args->valuelen < sfe->valuelen) {
506 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return(XFS_ERROR(ERANGE));
508 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100509 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 memcpy(args->value, &sfe->nameval[args->namelen],
511 args->valuelen);
512 return(XFS_ERROR(EEXIST));
513 }
514 return(XFS_ERROR(ENOATTR));
515}
516
517/*
518 * Convert from using the shortform to the leaf.
519 */
520int
521xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
522{
523 xfs_inode_t *dp;
524 xfs_attr_shortform_t *sf;
525 xfs_attr_sf_entry_t *sfe;
526 xfs_da_args_t nargs;
527 char *tmpbuffer;
528 int error, i, size;
529 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000530 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 xfs_ifork_t *ifp;
532
Dave Chinner5a5881c2012-03-22 05:15:13 +0000533 trace_xfs_attr_sf_to_leaf(args);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 dp = args->dp;
536 ifp = dp->i_afp;
537 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100538 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 tmpbuffer = kmem_alloc(size, KM_SLEEP);
540 ASSERT(tmpbuffer != NULL);
541 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
542 sf = (xfs_attr_shortform_t *)tmpbuffer;
543
544 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
545 bp = NULL;
546 error = xfs_da_grow_inode(args, &blkno);
547 if (error) {
548 /*
549 * If we hit an IO error middle of the transaction inside
550 * grow_inode(), we may have inconsistent data. Bail out.
551 */
552 if (error == EIO)
553 goto out;
554 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
555 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
556 goto out;
557 }
558
559 ASSERT(blkno == 0);
560 error = xfs_attr_leaf_create(args, blkno, &bp);
561 if (error) {
562 error = xfs_da_shrink_inode(args, 0, bp);
563 bp = NULL;
564 if (error)
565 goto out;
566 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
567 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
568 goto out;
569 }
570
571 memset((char *)&nargs, 0, sizeof(nargs));
572 nargs.dp = dp;
573 nargs.firstblock = args->firstblock;
574 nargs.flist = args->flist;
575 nargs.total = args->total;
576 nargs.whichfork = XFS_ATTR_FORK;
577 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000578 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100581 for (i = 0; i < sf->hdr.count; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100582 nargs.name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 nargs.namelen = sfe->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100584 nargs.value = &sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100585 nargs.valuelen = sfe->valuelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100586 nargs.hashval = xfs_da_hashname(sfe->nameval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000588 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
590 ASSERT(error == ENOATTR);
591 error = xfs_attr_leaf_add(bp, &nargs);
592 ASSERT(error != ENOSPC);
593 if (error)
594 goto out;
595 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
596 }
597 error = 0;
598
599out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000600 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return(error);
602}
603
604STATIC int
605xfs_attr_shortform_compare(const void *a, const void *b)
606{
607 xfs_attr_sf_sort_t *sa, *sb;
608
609 sa = (xfs_attr_sf_sort_t *)a;
610 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100611 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100613 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return(1);
615 } else {
616 return(sa->entno - sb->entno);
617 }
618}
619
Tim Shimmin726801b2006-09-28 11:01:37 +1000620
621#define XFS_ISRESET_CURSOR(cursor) \
622 (!((cursor)->initted) && !((cursor)->hashval) && \
623 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624/*
625 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000626 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * If the output buffer is not large enough to hold them all, then we
628 * we have to calculate each entries' hashvalue and sort them before
629 * we can begin returning them to the user.
630 */
631/*ARGSUSED*/
632int
633xfs_attr_shortform_list(xfs_attr_list_context_t *context)
634{
635 attrlist_cursor_kern_t *cursor;
636 xfs_attr_sf_sort_t *sbuf, *sbp;
637 xfs_attr_shortform_t *sf;
638 xfs_attr_sf_entry_t *sfe;
639 xfs_inode_t *dp;
640 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000641 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 ASSERT(context != NULL);
644 dp = context->dp;
645 ASSERT(dp != NULL);
646 ASSERT(dp->i_afp != NULL);
647 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
648 ASSERT(sf != NULL);
649 if (!sf->hdr.count)
650 return(0);
651 cursor = context->cursor;
652 ASSERT(cursor != NULL);
653
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000654 trace_xfs_attr_list_sf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000657 * If the buffer is large enough and the cursor is at the start,
658 * do not bother with sorting since we will return everything in
659 * one buffer and another call using the cursor won't need to be
660 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000662 * If bufsize is zero then put_listent must be a search function
663 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000665 if (context->bufsize == 0 ||
666 (XFS_ISRESET_CURSOR(cursor) &&
667 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100668 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000669 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000670 sfe->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100671 sfe->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +1000672 (int)sfe->namelen,
673 (int)sfe->valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100674 &sfe->nameval[sfe->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000675
676 /*
677 * Either search callback finished early or
678 * didn't fit it all in the buffer after all.
679 */
680 if (context->seen_enough)
681 break;
682
683 if (error)
684 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
686 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000687 trace_xfs_attr_list_sf_all(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return(0);
689 }
690
Tim Shimmin726801b2006-09-28 11:01:37 +1000691 /* do no more for a search callback */
692 if (context->bufsize == 0)
693 return 0;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * It didn't all fit, so we have to sort everything on hashval.
697 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100698 sbsize = sf->hdr.count * sizeof(*sbuf);
Dave Chinner622d8142010-12-23 11:57:37 +1100699 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /*
702 * Scan the attribute list for the rest of the entries, storing
703 * the relevant info from only those that match into a buffer.
704 */
705 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100706 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (unlikely(
708 ((char *)sfe < (char *)sf) ||
709 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
710 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
711 XFS_ERRLEVEL_LOW,
712 context->dp->i_mount, sfe);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000713 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return XFS_ERROR(EFSCORRUPTED);
715 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 sbp->entno = i;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100718 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
719 sbp->name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 sbp->namelen = sfe->namelen;
721 /* These are bytes, and both on-disk, don't endian-flip */
722 sbp->valuelen = sfe->valuelen;
723 sbp->flags = sfe->flags;
724 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
725 sbp++;
726 nsbuf++;
727 }
728
729 /*
730 * Sort the entries on hash then entno.
731 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100732 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /*
735 * Re-find our place IN THE SORTED LIST.
736 */
737 count = 0;
738 cursor->initted = 1;
739 cursor->blkno = 0;
740 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100741 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (cursor->offset == count) {
743 break;
744 }
745 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100746 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748 }
749 }
750 if (i == nsbuf) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000751 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return(0);
753 }
754
755 /*
756 * Loop putting entries into the user buffer.
757 */
758 for ( ; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100759 if (cursor->hashval != sbp->hash) {
760 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 cursor->offset = 0;
762 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000763 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000764 sbp->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000765 sbp->name,
766 sbp->namelen,
767 sbp->valuelen,
768 &sbp->name[sbp->namelen]);
769 if (error)
770 return error;
771 if (context->seen_enough)
772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 cursor->offset++;
774 }
775
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000776 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return(0);
778}
779
780/*
781 * Check a leaf attribute block to see if all the entries would fit into
782 * a shortform attribute list.
783 */
784int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000785xfs_attr_shortform_allfit(
786 struct xfs_buf *bp,
787 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 xfs_attr_leafblock_t *leaf;
790 xfs_attr_leaf_entry_t *entry;
791 xfs_attr_leaf_name_local_t *name_loc;
792 int bytes, i;
793
Dave Chinner1d9025e2012-06-22 18:50:14 +1000794 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200795 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 entry = &leaf->entries[0];
798 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100799 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (entry->flags & XFS_ATTR_INCOMPLETE)
801 continue; /* don't copy partial entries */
802 if (!(entry->flags & XFS_ATTR_LOCAL))
803 return(0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600804 name_loc = xfs_attr_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
806 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100807 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return(0);
809 bytes += sizeof(struct xfs_attr_sf_entry)-1
810 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100811 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100813 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100814 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100815 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100816 return(-1);
817 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820/*
821 * Convert a leaf attribute list to shortform attribute list
822 */
823int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000824xfs_attr_leaf_to_shortform(
825 struct xfs_buf *bp,
826 xfs_da_args_t *args,
827 int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 xfs_attr_leafblock_t *leaf;
830 xfs_attr_leaf_entry_t *entry;
831 xfs_attr_leaf_name_local_t *name_loc;
832 xfs_da_args_t nargs;
833 xfs_inode_t *dp;
834 char *tmpbuffer;
835 int error, i;
836
Dave Chinner5a5881c2012-03-22 05:15:13 +0000837 trace_xfs_attr_leaf_to_sf(args);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 dp = args->dp;
840 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
841 ASSERT(tmpbuffer != NULL);
842
843 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000844 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200846 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000847 memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /*
850 * Clean out the prior contents of the attribute list.
851 */
852 error = xfs_da_shrink_inode(args, 0, bp);
853 if (error)
854 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100855
856 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +1100857 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +1100858 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Christoph Hellwige1486de2009-02-04 09:36:00 +0100859 xfs_attr_fork_reset(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100861 }
862
863 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /*
866 * Copy the attributes
867 */
868 memset((char *)&nargs, 0, sizeof(nargs));
869 nargs.dp = dp;
870 nargs.firstblock = args->firstblock;
871 nargs.flist = args->flist;
872 nargs.total = args->total;
873 nargs.whichfork = XFS_ATTR_FORK;
874 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000875 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +1100877 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (entry->flags & XFS_ATTR_INCOMPLETE)
879 continue; /* don't copy partial entries */
880 if (!entry->nameidx)
881 continue;
882 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600883 name_loc = xfs_attr_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +1100884 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100886 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +1100887 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100888 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +1000889 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100890 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892 error = 0;
893
894out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000895 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return(error);
897}
898
899/*
900 * Convert from using a single leaf to a root node and a leaf.
901 */
902int
903xfs_attr_leaf_to_node(xfs_da_args_t *args)
904{
905 xfs_attr_leafblock_t *leaf;
906 xfs_da_intnode_t *node;
907 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000908 struct xfs_buf *bp1, *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 xfs_dablk_t blkno;
910 int error;
911
Dave Chinner5a5881c2012-03-22 05:15:13 +0000912 trace_xfs_attr_leaf_to_node(args);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 dp = args->dp;
915 bp1 = bp2 = NULL;
916 error = xfs_da_grow_inode(args, &blkno);
917 if (error)
918 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +1100919 error = xfs_attr_leaf_read(args->trans, args->dp, 0, -1, &bp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (error)
921 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +1100922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 bp2 = NULL;
924 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
925 XFS_ATTR_FORK);
926 if (error)
927 goto out;
Dave Chinner1813dd62012-11-14 17:54:40 +1100928 bp2->b_ops = bp1->b_ops;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000929 memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 bp1 = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000931 xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /*
934 * Set up the new root node.
935 */
936 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
937 if (error)
938 goto out;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000939 node = bp1->b_addr;
940 leaf = bp2->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200941 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* both on-disk, don't endian-flip twice */
943 node->btree[0].hashval =
Nathan Scott918ae422006-03-17 17:28:54 +1100944 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
Nathan Scott403432d2006-03-17 17:29:46 +1100945 node->btree[0].before = cpu_to_be32(blkno);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100946 node->hdr.count = cpu_to_be16(1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000947 xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 error = 0;
949out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return(error);
951}
952
953
954/*========================================================================
955 * Routines used for growing the Btree.
956 *========================================================================*/
957
958/*
959 * Create the initial contents of a leaf attribute list
960 * or a leaf in a node attribute list.
961 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000962STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000963xfs_attr_leaf_create(
964 xfs_da_args_t *args,
965 xfs_dablk_t blkno,
966 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 xfs_attr_leafblock_t *leaf;
969 xfs_attr_leaf_hdr_t *hdr;
970 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000971 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int error;
973
Dave Chinner5a5881c2012-03-22 05:15:13 +0000974 trace_xfs_attr_leaf_create(args);
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 dp = args->dp;
977 ASSERT(dp != NULL);
978 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
979 XFS_ATTR_FORK);
980 if (error)
981 return(error);
Dave Chinner1813dd62012-11-14 17:54:40 +1100982 bp->b_ops = &xfs_attr_leaf_buf_ops;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000983 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
985 hdr = &leaf->hdr;
Nathan Scott89da0542006-03-17 17:28:40 +1100986 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +1100987 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +1100989 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
991 }
992
Nathan Scott918ae422006-03-17 17:28:54 +1100993 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
994 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
995 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Dave Chinner1d9025e2012-06-22 18:50:14 +1000997 xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 *bpp = bp;
1000 return(0);
1001}
1002
1003/*
1004 * Split the leaf node, rebalance, then add the new entry.
1005 */
1006int
1007xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
1008 xfs_da_state_blk_t *newblk)
1009{
1010 xfs_dablk_t blkno;
1011 int error;
1012
Dave Chinner5a5881c2012-03-22 05:15:13 +00001013 trace_xfs_attr_leaf_split(state->args);
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
1016 * Allocate space for a new leaf node.
1017 */
1018 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1019 error = xfs_da_grow_inode(state->args, &blkno);
1020 if (error)
1021 return(error);
1022 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
1023 if (error)
1024 return(error);
1025 newblk->blkno = blkno;
1026 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1027
1028 /*
1029 * Rebalance the entries across the two leaves.
1030 * NOTE: rebalance() currently depends on the 2nd block being empty.
1031 */
1032 xfs_attr_leaf_rebalance(state, oldblk, newblk);
1033 error = xfs_da_blk_link(state, oldblk, newblk);
1034 if (error)
1035 return(error);
1036
1037 /*
1038 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1039 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1040 * "new" attrs info. Will need the "old" info to remove it later.
1041 *
1042 * Insert the "new" entry in the correct block.
1043 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001044 if (state->inleaf) {
1045 trace_xfs_attr_leaf_add_old(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 error = xfs_attr_leaf_add(oldblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001047 } else {
1048 trace_xfs_attr_leaf_add_new(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 error = xfs_attr_leaf_add(newblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /*
1053 * Update last hashval in each block since we added the name.
1054 */
1055 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1056 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1057 return(error);
1058}
1059
1060/*
1061 * Add a name to the leaf attribute list structure.
1062 */
1063int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001064xfs_attr_leaf_add(
1065 struct xfs_buf *bp,
1066 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 xfs_attr_leafblock_t *leaf;
1069 xfs_attr_leaf_hdr_t *hdr;
1070 xfs_attr_leaf_map_t *map;
1071 int tablesize, entsize, sum, tmp, i;
1072
Dave Chinner5a5881c2012-03-22 05:15:13 +00001073 trace_xfs_attr_leaf_add(args);
1074
Dave Chinner1d9025e2012-06-22 18:50:14 +10001075 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001076 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001078 && (args->index <= be16_to_cpu(leaf->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 hdr = &leaf->hdr;
Nathan Scottaa82daa2005-11-02 10:33:33 +11001080 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1082
1083 /*
1084 * Search through freemap for first-fit on new name length.
1085 * (may need to figure in size of entry struct too)
1086 */
Nathan Scott918ae422006-03-17 17:28:54 +11001087 tablesize = (be16_to_cpu(hdr->count) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * sizeof(xfs_attr_leaf_entry_t)
1089 + sizeof(xfs_attr_leaf_hdr_t);
1090 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1091 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
Nathan Scott918ae422006-03-17 17:28:54 +11001092 if (tablesize > be16_to_cpu(hdr->firstused)) {
1093 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 continue;
1095 }
1096 if (!map->size)
1097 continue; /* no space in this map */
1098 tmp = entsize;
Nathan Scott918ae422006-03-17 17:28:54 +11001099 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 tmp += sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001101 if (be16_to_cpu(map->size) >= tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 tmp = xfs_attr_leaf_add_work(bp, args, i);
1103 return(tmp);
1104 }
Nathan Scott918ae422006-03-17 17:28:54 +11001105 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
1108 /*
1109 * If there are no holes in the address space of the block,
1110 * and we don't have enough freespace, then compaction will do us
1111 * no good and we should just give up.
1112 */
1113 if (!hdr->holes && (sum < entsize))
1114 return(XFS_ERROR(ENOSPC));
1115
1116 /*
1117 * Compact the entries to coalesce free space.
1118 * This may change the hdr->count via dropping INCOMPLETE entries.
1119 */
Dave Chinneree732592012-11-12 22:53:53 +11001120 xfs_attr_leaf_compact(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /*
1123 * After compaction, the block is guaranteed to have only one
1124 * free region, in freemap[0]. If it is not big enough, give up.
1125 */
Nathan Scott918ae422006-03-17 17:28:54 +11001126 if (be16_to_cpu(hdr->freemap[0].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1128 return(XFS_ERROR(ENOSPC));
1129
1130 return(xfs_attr_leaf_add_work(bp, args, 0));
1131}
1132
1133/*
1134 * Add a name to a leaf attribute list structure.
1135 */
1136STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001137xfs_attr_leaf_add_work(
1138 struct xfs_buf *bp,
1139 xfs_da_args_t *args,
1140 int mapindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 xfs_attr_leafblock_t *leaf;
1143 xfs_attr_leaf_hdr_t *hdr;
1144 xfs_attr_leaf_entry_t *entry;
1145 xfs_attr_leaf_name_local_t *name_loc;
1146 xfs_attr_leaf_name_remote_t *name_rmt;
1147 xfs_attr_leaf_map_t *map;
1148 xfs_mount_t *mp;
1149 int tmp, i;
1150
Dave Chinneree732592012-11-12 22:53:53 +11001151 trace_xfs_attr_leaf_add_work(args);
1152
Dave Chinner1d9025e2012-06-22 18:50:14 +10001153 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001154 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 hdr = &leaf->hdr;
1156 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
Nathan Scott918ae422006-03-17 17:28:54 +11001157 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /*
1160 * Force open some space in the entry array and fill it in.
1161 */
1162 entry = &leaf->entries[args->index];
Nathan Scott918ae422006-03-17 17:28:54 +11001163 if (args->index < be16_to_cpu(hdr->count)) {
1164 tmp = be16_to_cpu(hdr->count) - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 tmp *= sizeof(xfs_attr_leaf_entry_t);
1166 memmove((char *)(entry+1), (char *)entry, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001167 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1169 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001170 be16_add_cpu(&hdr->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 /*
1173 * Allocate space for the new string (at the end of the run).
1174 */
1175 map = &hdr->freemap[mapindex];
1176 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001177 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1178 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1179 ASSERT(be16_to_cpu(map->size) >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001180 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1181 mp->m_sb.sb_blocksize, NULL));
Nathan Scott918ae422006-03-17 17:28:54 +11001182 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1183 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001184 be16_add_cpu(&map->size,
Nathan Scottaa82daa2005-11-02 10:33:33 +11001185 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1186 mp->m_sb.sb_blocksize, &tmp));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001187 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1188 be16_to_cpu(map->size));
1189 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001191 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001192 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 entry->flags |= XFS_ATTR_INCOMPLETE;
1194 if ((args->blkno2 == args->blkno) &&
1195 (args->index2 <= args->index)) {
1196 args->index2++;
1197 }
1198 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001199 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001201 ASSERT((args->index == 0) ||
1202 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Nathan Scott918ae422006-03-17 17:28:54 +11001203 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001204 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 * For "remote" attribute values, simply note that we need to
1208 * allocate space for the "remote" value. We can't actually
1209 * allocate the extents in this transaction, and we can't decide
1210 * which blocks they should be as we might allocate more blocks
1211 * as part of this transaction (a split operation for example).
1212 */
1213 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001214 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001216 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1218 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001219 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001221 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 name_rmt->namelen = args->namelen;
1223 memcpy((char *)name_rmt->name, args->name, args->namelen);
1224 entry->flags |= XFS_ATTR_INCOMPLETE;
1225 /* just in case */
1226 name_rmt->valuelen = 0;
1227 name_rmt->valueblk = 0;
1228 args->rmtblkno = 1;
1229 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1230 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001231 xfs_trans_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001232 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 xfs_attr_leaf_entsize(leaf, args->index)));
1234
1235 /*
1236 * Update the control info for this leaf node
1237 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001238 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /* both on-disk, don't endian-flip twice */
1240 hdr->firstused = entry->nameidx;
1241 }
Nathan Scott918ae422006-03-17 17:28:54 +11001242 ASSERT(be16_to_cpu(hdr->firstused) >=
1243 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1244 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 + sizeof(xfs_attr_leaf_hdr_t);
1246 map = &hdr->freemap[0];
1247 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001248 if (be16_to_cpu(map->base) == tmp) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001249 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1250 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001251 -((int)sizeof(xfs_attr_leaf_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001254 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001255 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1257 return(0);
1258}
1259
1260/*
1261 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1262 */
1263STATIC void
Dave Chinner1d9025e2012-06-22 18:50:14 +10001264xfs_attr_leaf_compact(
Dave Chinneree732592012-11-12 22:53:53 +11001265 struct xfs_da_args *args,
1266 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Dave Chinneree732592012-11-12 22:53:53 +11001268 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1269 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1270 struct xfs_trans *trans = args->trans;
1271 struct xfs_mount *mp = trans->t_mountp;
1272 char *tmpbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Dave Chinneree732592012-11-12 22:53:53 +11001274 trace_xfs_attr_leaf_compact(args);
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1277 ASSERT(tmpbuffer != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001278 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1279 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /*
1282 * Copy basic information
1283 */
1284 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001285 leaf_d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 hdr_s = &leaf_s->hdr;
1287 hdr_d = &leaf_d->hdr;
1288 hdr_d->info = hdr_s->info; /* struct copy */
Nathan Scott918ae422006-03-17 17:28:54 +11001289 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 /* handle truncation gracefully */
1291 if (!hdr_d->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001292 hdr_d->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1294 }
1295 hdr_d->usedbytes = 0;
1296 hdr_d->count = 0;
1297 hdr_d->holes = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001298 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1299 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1300 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /*
1303 * Copy all entry's in the same (sorted) order,
1304 * but allocate name/value pairs packed and in sequence.
1305 */
1306 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001307 be16_to_cpu(hdr_s->count), mp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001308 xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001310 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313/*
1314 * Redistribute the attribute list entries between two leaf nodes,
1315 * taking into account the size of the new entry.
1316 *
1317 * NOTE: if new block is empty, then it will get the upper half of the
1318 * old block. At present, all (one) callers pass in an empty second block.
1319 *
1320 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1321 * to match what it is doing in splitting the attribute leaf block. Those
1322 * values are used in "atomic rename" operations on attributes. Note that
1323 * the "new" and "old" values can end up in different blocks.
1324 */
1325STATIC void
1326xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1327 xfs_da_state_blk_t *blk2)
1328{
1329 xfs_da_args_t *args;
1330 xfs_da_state_blk_t *tmp_blk;
1331 xfs_attr_leafblock_t *leaf1, *leaf2;
1332 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1333 int count, totallen, max, space, swap;
1334
1335 /*
1336 * Set up environment.
1337 */
1338 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1339 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001340 leaf1 = blk1->bp->b_addr;
1341 leaf2 = blk2->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001342 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1343 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Dave Chinner07428d72012-11-12 22:09:44 +11001344 ASSERT(leaf2->hdr.count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 args = state->args;
1346
Dave Chinner5a5881c2012-03-22 05:15:13 +00001347 trace_xfs_attr_leaf_rebalance(args);
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /*
1350 * Check ordering of blocks, reverse if it makes things simpler.
1351 *
1352 * NOTE: Given that all (current) callers pass in an empty
1353 * second block, this code should never set "swap".
1354 */
1355 swap = 0;
1356 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1357 tmp_blk = blk1;
1358 blk1 = blk2;
1359 blk2 = tmp_blk;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001360 leaf1 = blk1->bp->b_addr;
1361 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 swap = 1;
1363 }
1364 hdr1 = &leaf1->hdr;
1365 hdr2 = &leaf2->hdr;
1366
1367 /*
1368 * Examine entries until we reduce the absolute difference in
1369 * byte usage between the two blocks to a minimum. Then get
1370 * the direction to copy and the number of elements to move.
1371 *
1372 * "inleaf" is true if the new entry should be inserted into blk1.
1373 * If "swap" is also true, then reverse the sense of "inleaf".
1374 */
1375 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1376 &count, &totallen);
1377 if (swap)
1378 state->inleaf = !state->inleaf;
1379
1380 /*
1381 * Move any entries required from leaf to leaf:
1382 */
Nathan Scott918ae422006-03-17 17:28:54 +11001383 if (count < be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /*
1385 * Figure the total bytes to be added to the destination leaf.
1386 */
1387 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001388 count = be16_to_cpu(hdr1->count) - count;
1389 space = be16_to_cpu(hdr1->usedbytes) - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 space += count * sizeof(xfs_attr_leaf_entry_t);
1391
1392 /*
1393 * leaf2 is the destination, compact it if it looks tight.
1394 */
Nathan Scott918ae422006-03-17 17:28:54 +11001395 max = be16_to_cpu(hdr2->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001397 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001398 if (space > max)
1399 xfs_attr_leaf_compact(args, blk2->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /*
1402 * Move high entries from leaf1 to low end of leaf2.
1403 */
Nathan Scott918ae422006-03-17 17:28:54 +11001404 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 leaf2, 0, count, state->mp);
1406
Dave Chinner1d9025e2012-06-22 18:50:14 +10001407 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1408 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Nathan Scott918ae422006-03-17 17:28:54 +11001409 } else if (count > be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /*
1411 * I assert that since all callers pass in an empty
1412 * second buffer, this code should never execute.
1413 */
Dave Chinner07428d72012-11-12 22:09:44 +11001414 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /*
1417 * Figure the total bytes to be added to the destination leaf.
1418 */
1419 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001420 count -= be16_to_cpu(hdr1->count);
1421 space = totallen - be16_to_cpu(hdr1->usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 space += count * sizeof(xfs_attr_leaf_entry_t);
1423
1424 /*
1425 * leaf1 is the destination, compact it if it looks tight.
1426 */
Nathan Scott918ae422006-03-17 17:28:54 +11001427 max = be16_to_cpu(hdr1->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001429 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001430 if (space > max)
1431 xfs_attr_leaf_compact(args, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /*
1434 * Move low entries from leaf2 to high end of leaf1.
1435 */
1436 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
Nathan Scott918ae422006-03-17 17:28:54 +11001437 be16_to_cpu(hdr1->count), count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Dave Chinner1d9025e2012-06-22 18:50:14 +10001439 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1440 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442
1443 /*
1444 * Copy out last hashval in each block for B-tree code.
1445 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001446 blk1->hashval = be32_to_cpu(
1447 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1448 blk2->hashval = be32_to_cpu(
1449 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 /*
1452 * Adjust the expected index for insertion.
1453 * NOTE: this code depends on the (current) situation that the
1454 * second block was originally empty.
1455 *
1456 * If the insertion point moved to the 2nd block, we must adjust
1457 * the index. We must also track the entry just following the
1458 * new entry for use in an "atomic rename" operation, that entry
1459 * is always the "old" entry and the "new" entry is what we are
1460 * inserting. The index/blkno fields refer to the "old" entry,
1461 * while the index2/blkno2 fields refer to the "new" entry.
1462 */
Nathan Scott918ae422006-03-17 17:28:54 +11001463 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 ASSERT(state->inleaf == 0);
Nathan Scott918ae422006-03-17 17:28:54 +11001465 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 args->index = args->index2 = blk2->index;
1467 args->blkno = args->blkno2 = blk2->blkno;
Nathan Scott918ae422006-03-17 17:28:54 +11001468 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (state->inleaf) {
1470 args->index = blk1->index;
1471 args->blkno = blk1->blkno;
1472 args->index2 = 0;
1473 args->blkno2 = blk2->blkno;
1474 } else {
Dave Chinner07428d72012-11-12 22:09:44 +11001475 /*
1476 * On a double leaf split, the original attr location
1477 * is already stored in blkno2/index2, so don't
1478 * overwrite it overwise we corrupt the tree.
1479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 blk2->index = blk1->index
Nathan Scott918ae422006-03-17 17:28:54 +11001481 - be16_to_cpu(leaf1->hdr.count);
Dave Chinner07428d72012-11-12 22:09:44 +11001482 args->index = blk2->index;
1483 args->blkno = blk2->blkno;
1484 if (!state->extravalid) {
1485 /*
1486 * set the new attr location to match the old
1487 * one and let the higher level split code
1488 * decide where in the leaf to place it.
1489 */
1490 args->index2 = blk2->index;
1491 args->blkno2 = blk2->blkno;
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494 } else {
1495 ASSERT(state->inleaf == 1);
1496 args->index = args->index2 = blk1->index;
1497 args->blkno = args->blkno2 = blk1->blkno;
1498 }
1499}
1500
1501/*
1502 * Examine entries until we reduce the absolute difference in
1503 * byte usage between the two blocks to a minimum.
1504 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1505 * GROT: there will always be enough room in either block for a new entry.
1506 * GROT: Do a double-split for this case?
1507 */
1508STATIC int
1509xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1510 xfs_da_state_blk_t *blk1,
1511 xfs_da_state_blk_t *blk2,
1512 int *countarg, int *usedbytesarg)
1513{
1514 xfs_attr_leafblock_t *leaf1, *leaf2;
1515 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1516 xfs_attr_leaf_entry_t *entry;
1517 int count, max, index, totallen, half;
1518 int lastdelta, foundit, tmp;
1519
1520 /*
1521 * Set up environment.
1522 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001523 leaf1 = blk1->bp->b_addr;
1524 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 hdr1 = &leaf1->hdr;
1526 hdr2 = &leaf2->hdr;
1527 foundit = 0;
1528 totallen = 0;
1529
1530 /*
1531 * Examine entries until we reduce the absolute difference in
1532 * byte usage between the two blocks to a minimum.
1533 */
Nathan Scott918ae422006-03-17 17:28:54 +11001534 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 half = (max+1) * sizeof(*entry);
Nathan Scott918ae422006-03-17 17:28:54 +11001536 half += be16_to_cpu(hdr1->usedbytes) +
1537 be16_to_cpu(hdr2->usedbytes) +
1538 xfs_attr_leaf_newentsize(
1539 state->args->namelen,
1540 state->args->valuelen,
1541 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 half /= 2;
1543 lastdelta = state->blocksize;
1544 entry = &leaf1->entries[0];
1545 for (count = index = 0; count < max; entry++, index++, count++) {
1546
1547#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1548 /*
1549 * The new entry is in the first block, account for it.
1550 */
1551 if (count == blk1->index) {
1552 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001553 xfs_attr_leaf_newentsize(
1554 state->args->namelen,
1555 state->args->valuelen,
1556 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1558 break;
1559 lastdelta = XFS_ATTR_ABS(half - tmp);
1560 totallen = tmp;
1561 foundit = 1;
1562 }
1563
1564 /*
1565 * Wrap around into the second block if necessary.
1566 */
Nathan Scott918ae422006-03-17 17:28:54 +11001567 if (count == be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 leaf1 = leaf2;
1569 entry = &leaf1->entries[0];
1570 index = 0;
1571 }
1572
1573 /*
1574 * Figure out if next leaf entry would be too much.
1575 */
1576 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1577 index);
1578 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1579 break;
1580 lastdelta = XFS_ATTR_ABS(half - tmp);
1581 totallen = tmp;
1582#undef XFS_ATTR_ABS
1583 }
1584
1585 /*
1586 * Calculate the number of usedbytes that will end up in lower block.
1587 * If new entry not in lower block, fix up the count.
1588 */
1589 totallen -= count * sizeof(*entry);
1590 if (foundit) {
1591 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001592 xfs_attr_leaf_newentsize(
1593 state->args->namelen,
1594 state->args->valuelen,
1595 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
1597
1598 *countarg = count;
1599 *usedbytesarg = totallen;
1600 return(foundit);
1601}
1602
1603/*========================================================================
1604 * Routines used for shrinking the Btree.
1605 *========================================================================*/
1606
1607/*
1608 * Check a leaf block and its neighbors to see if the block should be
1609 * collapsed into one or the other neighbor. Always keep the block
1610 * with the smaller block number.
1611 * If the current block is over 50% full, don't try to join it, return 0.
1612 * If the block is empty, fill in the state structure and return 2.
1613 * If it can be collapsed, fill in the state structure and return 1.
1614 * If nothing can be done, return 0.
1615 *
1616 * GROT: allow for INCOMPLETE entries in calculation.
1617 */
1618int
1619xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1620{
1621 xfs_attr_leafblock_t *leaf;
1622 xfs_da_state_blk_t *blk;
1623 xfs_da_blkinfo_t *info;
1624 int count, bytes, forward, error, retval, i;
1625 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001626 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Dave Chinneree732592012-11-12 22:53:53 +11001628 trace_xfs_attr_leaf_toosmall(state->args);
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /*
1631 * Check for the degenerate case of the block being over 50% full.
1632 * If so, it's not worth even looking to see if we might be able
1633 * to coalesce with a sibling.
1634 */
1635 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001636 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001637 ASSERT(info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001639 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1641 count * sizeof(xfs_attr_leaf_entry_t) +
Nathan Scott918ae422006-03-17 17:28:54 +11001642 be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (bytes > (state->blocksize >> 1)) {
1644 *action = 0; /* blk over 50%, don't try to join */
1645 return(0);
1646 }
1647
1648 /*
1649 * Check for the degenerate case of the block being empty.
1650 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001651 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * to merge with the forward block unless it is NULL.
1653 */
1654 if (count == 0) {
1655 /*
1656 * Make altpath point to the block we want to keep and
1657 * path point to the block we want to drop (this one).
1658 */
Nathan Scott89da0542006-03-17 17:28:40 +11001659 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 memcpy(&state->altpath, &state->path, sizeof(state->path));
1661 error = xfs_da_path_shift(state, &state->altpath, forward,
1662 0, &retval);
1663 if (error)
1664 return(error);
1665 if (retval) {
1666 *action = 0;
1667 } else {
1668 *action = 2;
1669 }
1670 return(0);
1671 }
1672
1673 /*
1674 * Examine each sibling block to see if we can coalesce with
1675 * at least 25% free space to spare. We need to figure out
1676 * whether to merge with the forward or the backward block.
1677 * We prefer coalescing with the lower numbered sibling so as
1678 * to shrink an attribute list over time.
1679 */
1680 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +11001681 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 for (i = 0; i < 2; forward = !forward, i++) {
1683 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +11001684 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 else
Nathan Scott89da0542006-03-17 17:28:40 +11001686 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (blkno == 0)
1688 continue;
Dave Chinnerad14c332012-11-12 22:54:16 +11001689 error = xfs_attr_leaf_read(state->args->trans, state->args->dp,
1690 blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (error)
1692 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001695 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 bytes = state->blocksize - (state->blocksize>>2);
Nathan Scott918ae422006-03-17 17:28:54 +11001697 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001698 leaf = bp->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11001699 count += be16_to_cpu(leaf->hdr.count);
1700 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1702 bytes -= sizeof(xfs_attr_leaf_hdr_t);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001703 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (bytes >= 0)
1705 break; /* fits with at least 25% to spare */
1706 }
1707 if (i >= 2) {
1708 *action = 0;
1709 return(0);
1710 }
1711
1712 /*
1713 * Make altpath point to the block we want to keep (the lower
1714 * numbered block) and path point to the block we want to drop.
1715 */
1716 memcpy(&state->altpath, &state->path, sizeof(state->path));
1717 if (blkno < blk->blkno) {
1718 error = xfs_da_path_shift(state, &state->altpath, forward,
1719 0, &retval);
1720 } else {
1721 error = xfs_da_path_shift(state, &state->path, forward,
1722 0, &retval);
1723 }
1724 if (error)
1725 return(error);
1726 if (retval) {
1727 *action = 0;
1728 } else {
1729 *action = 1;
1730 }
1731 return(0);
1732}
1733
1734/*
1735 * Remove a name from the leaf attribute list structure.
1736 *
1737 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1738 * If two leaves are 37% full, when combined they will leave 25% free.
1739 */
1740int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001741xfs_attr_leaf_remove(
1742 struct xfs_buf *bp,
1743 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 xfs_attr_leafblock_t *leaf;
1746 xfs_attr_leaf_hdr_t *hdr;
1747 xfs_attr_leaf_map_t *map;
1748 xfs_attr_leaf_entry_t *entry;
1749 int before, after, smallest, entsize;
1750 int tablesize, tmp, i;
1751 xfs_mount_t *mp;
1752
Dave Chinneree732592012-11-12 22:53:53 +11001753 trace_xfs_attr_leaf_remove(args);
1754
Dave Chinner1d9025e2012-06-22 18:50:14 +10001755 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001756 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 hdr = &leaf->hdr;
1758 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001759 ASSERT((be16_to_cpu(hdr->count) > 0)
1760 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001762 && (args->index < be16_to_cpu(hdr->count)));
1763 ASSERT(be16_to_cpu(hdr->firstused) >=
1764 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 entry = &leaf->entries[args->index];
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001766 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1767 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 /*
1770 * Scan through free region table:
1771 * check for adjacency of free'd entry with an existing one,
1772 * find smallest free region in case we need to replace it,
1773 * adjust any map that borders the entry table,
1774 */
Nathan Scott918ae422006-03-17 17:28:54 +11001775 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 + sizeof(xfs_attr_leaf_hdr_t);
1777 map = &hdr->freemap[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001778 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 before = after = -1;
1780 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1781 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1782 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001783 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1784 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1785 if (be16_to_cpu(map->base) == tablesize) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001786 be16_add_cpu(&map->base,
Nathan Scott918ae422006-03-17 17:28:54 +11001787 -((int)sizeof(xfs_attr_leaf_entry_t)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001788 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790
Nathan Scott918ae422006-03-17 17:28:54 +11001791 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001792 == be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 before = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001794 } else if (be16_to_cpu(map->base)
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001795 == (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 after = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001797 } else if (be16_to_cpu(map->size) < tmp) {
1798 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 smallest = i;
1800 }
1801 }
1802
1803 /*
1804 * Coalesce adjacent freemap regions,
1805 * or replace the smallest region.
1806 */
1807 if ((before >= 0) || (after >= 0)) {
1808 if ((before >= 0) && (after >= 0)) {
1809 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001810 be16_add_cpu(&map->size, entsize);
1811 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001812 be16_to_cpu(hdr->freemap[after].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 hdr->freemap[after].base = 0;
1814 hdr->freemap[after].size = 0;
1815 } else if (before >= 0) {
1816 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001817 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 } else {
1819 map = &hdr->freemap[after];
1820 /* both on-disk, don't endian flip twice */
1821 map->base = entry->nameidx;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001822 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 } else {
1825 /*
1826 * Replace smallest region (if it is smaller than free'd entry)
1827 */
1828 map = &hdr->freemap[smallest];
Nathan Scott918ae422006-03-17 17:28:54 +11001829 if (be16_to_cpu(map->size) < entsize) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001830 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
Nathan Scott918ae422006-03-17 17:28:54 +11001831 map->size = cpu_to_be16(entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833 }
1834
1835 /*
1836 * Did we remove the first entry?
1837 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001838 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 smallest = 1;
1840 else
1841 smallest = 0;
1842
1843 /*
1844 * Compress the remaining entries and zero out the removed stuff.
1845 */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001846 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001847 be16_add_cpu(&hdr->usedbytes, -entsize);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001848 xfs_trans_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001849 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 entsize));
1851
Nathan Scott918ae422006-03-17 17:28:54 +11001852 tmp = (be16_to_cpu(hdr->count) - args->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 * sizeof(xfs_attr_leaf_entry_t);
1854 memmove((char *)entry, (char *)(entry+1), tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001855 be16_add_cpu(&hdr->count, -1);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001856 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
Nathan Scott918ae422006-03-17 17:28:54 +11001858 entry = &leaf->entries[be16_to_cpu(hdr->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1860
1861 /*
1862 * If we removed the first entry, re-find the first used byte
1863 * in the name area. Note that if the entry was the "firstused",
1864 * then we don't have a "hole" in our block resulting from
1865 * removing the name.
1866 */
1867 if (smallest) {
1868 tmp = XFS_LBSIZE(mp);
1869 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001870 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001871 ASSERT(be16_to_cpu(entry->nameidx) >=
1872 be16_to_cpu(hdr->firstused));
1873 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1874
1875 if (be16_to_cpu(entry->nameidx) < tmp)
1876 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Nathan Scott918ae422006-03-17 17:28:54 +11001878 hdr->firstused = cpu_to_be16(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001880 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1882 }
1883 } else {
1884 hdr->holes = 1; /* mark as needing compaction */
1885 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001886 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1888
1889 /*
1890 * Check if leaf is less than 50% full, caller may want to
1891 * "join" the leaf with a sibling if so.
1892 */
1893 tmp = sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001894 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1895 tmp += be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1897}
1898
1899/*
1900 * Move all the attribute list entries from drop_leaf into save_leaf.
1901 */
1902void
1903xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1904 xfs_da_state_blk_t *save_blk)
1905{
1906 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1907 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1908 xfs_mount_t *mp;
1909 char *tmpbuffer;
1910
Dave Chinner5a5881c2012-03-22 05:15:13 +00001911 trace_xfs_attr_leaf_unbalance(state->args);
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /*
1914 * Set up environment.
1915 */
1916 mp = state->mp;
1917 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1918 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001919 drop_leaf = drop_blk->bp->b_addr;
1920 save_leaf = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001921 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1922 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 drop_hdr = &drop_leaf->hdr;
1924 save_hdr = &save_leaf->hdr;
1925
1926 /*
1927 * Save last hashval from dying block for later Btree fixup.
1928 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001929 drop_blk->hashval = be32_to_cpu(
1930 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /*
1933 * Check if we need a temp buffer, or can we do it in place.
1934 * Note that we don't check "leaf" for holes because we will
1935 * always be dropping it, toosmall() decided that for us already.
1936 */
1937 if (save_hdr->holes == 0) {
1938 /*
1939 * dest leaf has no holes, so we add there. May need
1940 * to make some room in the entry array.
1941 */
1942 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1943 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001944 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 } else {
1946 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001947 be16_to_cpu(save_hdr->count),
1948 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 } else {
1951 /*
1952 * Destination has holes, so we make a temporary copy
1953 * of the leaf and add them both to that.
1954 */
1955 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1956 ASSERT(tmpbuffer != NULL);
1957 memset(tmpbuffer, 0, state->blocksize);
1958 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1959 tmp_hdr = &tmp_leaf->hdr;
1960 tmp_hdr->info = save_hdr->info; /* struct copy */
1961 tmp_hdr->count = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001962 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (!tmp_hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001964 tmp_hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1966 }
1967 tmp_hdr->usedbytes = 0;
1968 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1969 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001970 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001972 be16_to_cpu(tmp_leaf->hdr.count),
1973 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 } else {
1975 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001976 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001978 be16_to_cpu(tmp_leaf->hdr.count),
1979 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001982 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984
Dave Chinner1d9025e2012-06-22 18:50:14 +10001985 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 state->blocksize - 1);
1987
1988 /*
1989 * Copy out last hashval in each block for B-tree code.
1990 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001991 save_blk->hashval = be32_to_cpu(
1992 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995/*========================================================================
1996 * Routines used for finding things in the Btree.
1997 *========================================================================*/
1998
1999/*
2000 * Look up a name in a leaf attribute list structure.
2001 * This is the internal routine, it uses the caller's buffer.
2002 *
2003 * Note that duplicate keys are allowed, but only check within the
2004 * current leaf node. The Btree code must check in adjacent leaf nodes.
2005 *
2006 * Return in args->index the index into the entry[] array of either
2007 * the found entry, or where the entry should have been (insert before
2008 * that entry).
2009 *
2010 * Don't change the args->value unless we find the attribute.
2011 */
2012int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002013xfs_attr_leaf_lookup_int(
2014 struct xfs_buf *bp,
2015 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 xfs_attr_leafblock_t *leaf;
2018 xfs_attr_leaf_entry_t *entry;
2019 xfs_attr_leaf_name_local_t *name_loc;
2020 xfs_attr_leaf_name_remote_t *name_rmt;
2021 int probe, span;
2022 xfs_dahash_t hashval;
2023
Dave Chinner5a5881c2012-03-22 05:15:13 +00002024 trace_xfs_attr_leaf_lookup(args);
2025
Dave Chinner1d9025e2012-06-22 18:50:14 +10002026 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002027 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002028 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 < (XFS_LBSIZE(args->dp->i_mount)/8));
2030
2031 /*
2032 * Binary search. (note: small blocks will skip this loop)
2033 */
2034 hashval = args->hashval;
Nathan Scott918ae422006-03-17 17:28:54 +11002035 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 for (entry = &leaf->entries[probe]; span > 4;
2037 entry = &leaf->entries[probe]) {
2038 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002039 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002041 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 probe -= span;
2043 else
2044 break;
2045 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002046 ASSERT((probe >= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 (!leaf->hdr.count
Nathan Scott918ae422006-03-17 17:28:54 +11002048 || (probe < be16_to_cpu(leaf->hdr.count))));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002049 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 /*
2052 * Since we may have duplicate hashval's, find the first matching
2053 * hashval in the leaf.
2054 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002055 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 entry--;
2057 probe--;
2058 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002059 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
2060 (be32_to_cpu(entry->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 entry++;
2062 probe++;
2063 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002064 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
2065 (be32_to_cpu(entry->hashval) != hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 args->index = probe;
2067 return(XFS_ERROR(ENOATTR));
2068 }
2069
2070 /*
2071 * Duplicate keys may be present, so search all of them for a match.
2072 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002073 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
2074 (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 entry++, probe++) {
2076/*
2077 * GROT: Add code to remove incomplete entries.
2078 */
2079 /*
2080 * If we are looking for INCOMPLETE entries, show only those.
2081 * If we are looking for complete entries, show only those.
2082 */
2083 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2084 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2085 continue;
2086 }
2087 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002088 name_loc = xfs_attr_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (name_loc->namelen != args->namelen)
2090 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002091 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002093 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 continue;
2095 args->index = probe;
2096 return(XFS_ERROR(EEXIST));
2097 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002098 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (name_rmt->namelen != args->namelen)
2100 continue;
2101 if (memcmp(args->name, (char *)name_rmt->name,
2102 args->namelen) != 0)
2103 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002104 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 continue;
2106 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002107 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002109 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return(XFS_ERROR(EEXIST));
2111 }
2112 }
2113 args->index = probe;
2114 return(XFS_ERROR(ENOATTR));
2115}
2116
2117/*
2118 * Get the value associated with an attribute name from a leaf attribute
2119 * list structure.
2120 */
2121int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002122xfs_attr_leaf_getvalue(
2123 struct xfs_buf *bp,
2124 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 int valuelen;
2127 xfs_attr_leafblock_t *leaf;
2128 xfs_attr_leaf_entry_t *entry;
2129 xfs_attr_leaf_name_local_t *name_loc;
2130 xfs_attr_leaf_name_remote_t *name_rmt;
2131
Dave Chinner1d9025e2012-06-22 18:50:14 +10002132 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002133 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002134 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 < (XFS_LBSIZE(args->dp->i_mount)/8));
Nathan Scott918ae422006-03-17 17:28:54 +11002136 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 entry = &leaf->entries[args->index];
2139 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002140 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 ASSERT(name_loc->namelen == args->namelen);
2142 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002143 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (args->flags & ATTR_KERNOVAL) {
2145 args->valuelen = valuelen;
2146 return(0);
2147 }
2148 if (args->valuelen < valuelen) {
2149 args->valuelen = valuelen;
2150 return(XFS_ERROR(ERANGE));
2151 }
2152 args->valuelen = valuelen;
2153 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2154 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002155 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 ASSERT(name_rmt->namelen == args->namelen);
2157 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002158 valuelen = be32_to_cpu(name_rmt->valuelen);
2159 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2161 if (args->flags & ATTR_KERNOVAL) {
2162 args->valuelen = valuelen;
2163 return(0);
2164 }
2165 if (args->valuelen < valuelen) {
2166 args->valuelen = valuelen;
2167 return(XFS_ERROR(ERANGE));
2168 }
2169 args->valuelen = valuelen;
2170 }
2171 return(0);
2172}
2173
2174/*========================================================================
2175 * Utility routines.
2176 *========================================================================*/
2177
2178/*
2179 * Move the indicated entries from one leaf to another.
2180 * NOTE: this routine modifies both source and destination leaves.
2181 */
2182/*ARGSUSED*/
2183STATIC void
2184xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2185 xfs_attr_leafblock_t *leaf_d, int start_d,
2186 int count, xfs_mount_t *mp)
2187{
2188 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2189 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2190 int desti, tmp, i;
2191
2192 /*
2193 * Check for nothing to do.
2194 */
2195 if (count == 0)
2196 return;
2197
2198 /*
2199 * Set up environment.
2200 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002201 ASSERT(leaf_s->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2202 ASSERT(leaf_d->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 hdr_s = &leaf_s->hdr;
2204 hdr_d = &leaf_d->hdr;
Nathan Scott918ae422006-03-17 17:28:54 +11002205 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2206 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2207 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2208 ((be16_to_cpu(hdr_s->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * sizeof(*entry_s))+sizeof(*hdr_s)));
Nathan Scott918ae422006-03-17 17:28:54 +11002210 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2211 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2212 ((be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 * sizeof(*entry_d))+sizeof(*hdr_d)));
2214
Nathan Scott918ae422006-03-17 17:28:54 +11002215 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2216 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2217 ASSERT(count <= be16_to_cpu(hdr_s->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 /*
2220 * Move the entries in the destination leaf up to make a hole?
2221 */
Nathan Scott918ae422006-03-17 17:28:54 +11002222 if (start_d < be16_to_cpu(hdr_d->count)) {
2223 tmp = be16_to_cpu(hdr_d->count) - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 tmp *= sizeof(xfs_attr_leaf_entry_t);
2225 entry_s = &leaf_d->entries[start_d];
2226 entry_d = &leaf_d->entries[start_d + count];
2227 memmove((char *)entry_d, (char *)entry_s, tmp);
2228 }
2229
2230 /*
2231 * Copy all entry's in the same (sorted) order,
2232 * but allocate attribute info packed and in sequence.
2233 */
2234 entry_s = &leaf_s->entries[start_s];
2235 entry_d = &leaf_d->entries[start_d];
2236 desti = start_d;
2237 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002238 ASSERT(be16_to_cpu(entry_s->nameidx)
Nathan Scott918ae422006-03-17 17:28:54 +11002239 >= be16_to_cpu(hdr_s->firstused));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2241#ifdef GROT
2242 /*
2243 * Code to drop INCOMPLETE entries. Difficult to use as we
2244 * may also need to change the insertion index. Code turned
2245 * off for 6.2, should be revisited later.
2246 */
2247 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002248 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002249 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2250 be16_add_cpu(&hdr_s->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 entry_d--; /* to compensate for ++ in loop hdr */
2252 desti--;
2253 if ((start_s + i) < offset)
2254 result++; /* insertion index adjustment */
2255 } else {
2256#endif /* GROT */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002257 be16_add_cpu(&hdr_d->firstused, -tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /* both on-disk, don't endian flip twice */
2259 entry_d->hashval = entry_s->hashval;
2260 /* both on-disk, don't endian flip twice */
2261 entry_d->nameidx = hdr_d->firstused;
2262 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002263 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002265 memmove(xfs_attr_leaf_name(leaf_d, desti),
2266 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002267 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002269 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002270 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2271 be16_add_cpu(&hdr_d->usedbytes, tmp);
2272 be16_add_cpu(&hdr_s->count, -1);
2273 be16_add_cpu(&hdr_d->count, 1);
Nathan Scott918ae422006-03-17 17:28:54 +11002274 tmp = be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * sizeof(xfs_attr_leaf_entry_t)
2276 + sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002277 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278#ifdef GROT
2279 }
2280#endif /* GROT */
2281 }
2282
2283 /*
2284 * Zero out the entries we just copied.
2285 */
Nathan Scott918ae422006-03-17 17:28:54 +11002286 if (start_s == be16_to_cpu(hdr_s->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2288 entry_s = &leaf_s->entries[start_s];
2289 ASSERT(((char *)entry_s + tmp) <=
2290 ((char *)leaf_s + XFS_LBSIZE(mp)));
2291 memset((char *)entry_s, 0, tmp);
2292 } else {
2293 /*
2294 * Move the remaining entries down to fill the hole,
2295 * then zero the entries at the top.
2296 */
Nathan Scott918ae422006-03-17 17:28:54 +11002297 tmp = be16_to_cpu(hdr_s->count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 tmp *= sizeof(xfs_attr_leaf_entry_t);
2299 entry_s = &leaf_s->entries[start_s + count];
2300 entry_d = &leaf_s->entries[start_s];
2301 memmove((char *)entry_d, (char *)entry_s, tmp);
2302
2303 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002304 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 ASSERT(((char *)entry_s + tmp) <=
2306 ((char *)leaf_s + XFS_LBSIZE(mp)));
2307 memset((char *)entry_s, 0, tmp);
2308 }
2309
2310 /*
2311 * Fill in the freemap information
2312 */
Nathan Scott918ae422006-03-17 17:28:54 +11002313 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002314 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
Nathan Scott918ae422006-03-17 17:28:54 +11002315 sizeof(xfs_attr_leaf_entry_t));
2316 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2317 - be16_to_cpu(hdr_d->freemap[0].base));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 hdr_d->freemap[1].base = 0;
2319 hdr_d->freemap[2].base = 0;
2320 hdr_d->freemap[1].size = 0;
2321 hdr_d->freemap[2].size = 0;
2322 hdr_s->holes = 1; /* leaf may not be compact */
2323}
2324
2325/*
2326 * Compare two leaf blocks "order".
2327 * Return 0 unless leaf2 should go before leaf1.
2328 */
2329int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002330xfs_attr_leaf_order(
2331 struct xfs_buf *leaf1_bp,
2332 struct xfs_buf *leaf2_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 xfs_attr_leafblock_t *leaf1, *leaf2;
2335
Dave Chinner1d9025e2012-06-22 18:50:14 +10002336 leaf1 = leaf1_bp->b_addr;
2337 leaf2 = leaf2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002338 ASSERT((leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) &&
2339 (leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)));
Nathan Scott918ae422006-03-17 17:28:54 +11002340 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2341 (be16_to_cpu(leaf2->hdr.count) > 0) &&
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002342 ((be32_to_cpu(leaf2->entries[0].hashval) <
2343 be32_to_cpu(leaf1->entries[0].hashval)) ||
2344 (be32_to_cpu(leaf2->entries[
2345 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2346 be32_to_cpu(leaf1->entries[
2347 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return(1);
2349 }
2350 return(0);
2351}
2352
2353/*
2354 * Pick up the last hashvalue from a leaf block.
2355 */
2356xfs_dahash_t
Dave Chinner1d9025e2012-06-22 18:50:14 +10002357xfs_attr_leaf_lasthash(
2358 struct xfs_buf *bp,
2359 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 xfs_attr_leafblock_t *leaf;
2362
Dave Chinner1d9025e2012-06-22 18:50:14 +10002363 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002364 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (count)
Nathan Scott918ae422006-03-17 17:28:54 +11002366 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (!leaf->hdr.count)
2368 return(0);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002369 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
2372/*
2373 * Calculate the number of bytes used to store the indicated attribute
2374 * (whether local or remote only calculate bytes in this block).
2375 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002376STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2378{
2379 xfs_attr_leaf_name_local_t *name_loc;
2380 xfs_attr_leaf_name_remote_t *name_rmt;
2381 int size;
2382
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002383 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002385 name_loc = xfs_attr_leaf_name_local(leaf, index);
2386 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002387 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002389 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2390 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392 return(size);
2393}
2394
2395/*
2396 * Calculate the number of bytes that would be required to store the new
2397 * attribute (whether local or remote only calculate bytes in this block).
2398 * This routine decides as a side effect whether the attribute will be
2399 * a "local" or a "remote" attribute.
2400 */
2401int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002402xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
2404 int size;
2405
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002406 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2407 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (local) {
2409 *local = 1;
2410 }
2411 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002412 size = xfs_attr_leaf_entsize_remote(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (local) {
2414 *local = 0;
2415 }
2416 }
2417 return(size);
2418}
2419
2420/*
2421 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2422 */
2423int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002424xfs_attr_leaf_list_int(
2425 struct xfs_buf *bp,
2426 xfs_attr_list_context_t *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 attrlist_cursor_kern_t *cursor;
2429 xfs_attr_leafblock_t *leaf;
2430 xfs_attr_leaf_entry_t *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 int retval, i;
2432
2433 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002434 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 cursor = context->cursor;
2436 cursor->initted = 1;
2437
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002438 trace_xfs_attr_list_leaf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 /*
2441 * Re-find our place in the leaf block if this is a new syscall.
2442 */
2443 if (context->resynch) {
2444 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002445 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002446 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (cursor->offset == context->dupcnt) {
2448 context->dupcnt = 0;
2449 break;
2450 }
2451 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002452 } else if (be32_to_cpu(entry->hashval) >
2453 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 context->dupcnt = 0;
2455 break;
2456 }
2457 }
Nathan Scott918ae422006-03-17 17:28:54 +11002458 if (i == be16_to_cpu(leaf->hdr.count)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002459 trace_xfs_attr_list_notfound(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return(0);
2461 }
2462 } else {
2463 entry = &leaf->entries[0];
2464 i = 0;
2465 }
2466 context->resynch = 0;
2467
2468 /*
2469 * We have found our place, start copying out the new attributes.
2470 */
2471 retval = 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10002472 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002473 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2474 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 cursor->offset = 0;
2476 }
2477
2478 if (entry->flags & XFS_ATTR_INCOMPLETE)
2479 continue; /* skip incomplete entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002482 xfs_attr_leaf_name_local_t *name_loc =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002483 xfs_attr_leaf_name_local(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002484
2485 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002486 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002487 name_loc->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +10002488 (int)name_loc->namelen,
2489 be16_to_cpu(name_loc->valuelen),
Dave Chinnera9273ca2010-01-20 10:47:48 +11002490 &name_loc->nameval[name_loc->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +10002491 if (retval)
2492 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002494 xfs_attr_leaf_name_remote_t *name_rmt =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002495 xfs_attr_leaf_name_remote(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002496
2497 int valuelen = be32_to_cpu(name_rmt->valuelen);
2498
2499 if (context->put_value) {
2500 xfs_da_args_t args;
2501
2502 memset((char *)&args, 0, sizeof(args));
2503 args.dp = context->dp;
2504 args.whichfork = XFS_ATTR_FORK;
2505 args.valuelen = valuelen;
Dave Chinner622d8142010-12-23 11:57:37 +11002506 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
Tim Shimmin726801b2006-09-28 11:01:37 +10002507 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2508 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2509 retval = xfs_attr_rmtval_get(&args);
2510 if (retval)
2511 return retval;
2512 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002513 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002514 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002515 (int)name_rmt->namelen,
2516 valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002517 args.value);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002518 kmem_free(args.value);
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002519 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002520 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002521 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002522 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002523 (int)name_rmt->namelen,
2524 valuelen,
2525 NULL);
2526 }
2527 if (retval)
2528 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002530 if (context->seen_enough)
2531 break;
2532 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002534 trace_xfs_attr_list_leaf_end(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 return(retval);
2536}
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539/*========================================================================
2540 * Manage the INCOMPLETE flag in a leaf entry
2541 *========================================================================*/
2542
2543/*
2544 * Clear the INCOMPLETE flag on an entry in a leaf block.
2545 */
2546int
2547xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2548{
2549 xfs_attr_leafblock_t *leaf;
2550 xfs_attr_leaf_entry_t *entry;
2551 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002552 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 int error;
2554#ifdef DEBUG
2555 xfs_attr_leaf_name_local_t *name_loc;
2556 int namelen;
2557 char *name;
2558#endif /* DEBUG */
2559
Dave Chinner5a5881c2012-03-22 05:15:13 +00002560 trace_xfs_attr_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /*
2562 * Set up the operation.
2563 */
Dave Chinnerad14c332012-11-12 22:54:16 +11002564 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2565 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Dave Chinner1d9025e2012-06-22 18:50:14 +10002568 leaf = bp->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002569 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 ASSERT(args->index >= 0);
2571 entry = &leaf->entries[ args->index ];
2572 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2573
2574#ifdef DEBUG
2575 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002576 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 namelen = name_loc->namelen;
2578 name = (char *)name_loc->nameval;
2579 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002580 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 namelen = name_rmt->namelen;
2582 name = (char *)name_rmt->name;
2583 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002584 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 ASSERT(namelen == args->namelen);
2586 ASSERT(memcmp(name, args->name, namelen) == 0);
2587#endif /* DEBUG */
2588
2589 entry->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002590 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2592
2593 if (args->rmtblkno) {
2594 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002595 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002596 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2597 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002598 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 /*
2603 * Commit the flag value change and start the next trans in series.
2604 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002605 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
2608/*
2609 * Set the INCOMPLETE flag on an entry in a leaf block.
2610 */
2611int
2612xfs_attr_leaf_setflag(xfs_da_args_t *args)
2613{
2614 xfs_attr_leafblock_t *leaf;
2615 xfs_attr_leaf_entry_t *entry;
2616 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002617 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 int error;
2619
Dave Chinner5a5881c2012-03-22 05:15:13 +00002620 trace_xfs_attr_leaf_setflag(args);
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 /*
2623 * Set up the operation.
2624 */
Dave Chinnerad14c332012-11-12 22:54:16 +11002625 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2626 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Dave Chinner1d9025e2012-06-22 18:50:14 +10002629 leaf = bp->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002630 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 ASSERT(args->index >= 0);
2632 entry = &leaf->entries[ args->index ];
2633
2634 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2635 entry->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002636 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2638 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002639 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 name_rmt->valueblk = 0;
2641 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002642 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 /*
2647 * Commit the flag value change and start the next trans in series.
2648 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002649 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
2652/*
2653 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2654 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2655 * entry given by args->blkno2/index2.
2656 *
2657 * Note that they could be in different blocks, or in the same block.
2658 */
2659int
2660xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2661{
2662 xfs_attr_leafblock_t *leaf1, *leaf2;
2663 xfs_attr_leaf_entry_t *entry1, *entry2;
2664 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002665 struct xfs_buf *bp1, *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 int error;
2667#ifdef DEBUG
2668 xfs_attr_leaf_name_local_t *name_loc;
2669 int namelen1, namelen2;
2670 char *name1, *name2;
2671#endif /* DEBUG */
2672
Dave Chinner5a5881c2012-03-22 05:15:13 +00002673 trace_xfs_attr_leaf_flipflags(args);
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 /*
2676 * Read the block containing the "old" attr
2677 */
Dave Chinnerad14c332012-11-12 22:54:16 +11002678 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
2679 if (error)
2680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 /*
2683 * Read the block containing the "new" attr, if it is different
2684 */
2685 if (args->blkno2 != args->blkno) {
Dave Chinnerad14c332012-11-12 22:54:16 +11002686 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno2,
2687 -1, &bp2);
2688 if (error)
2689 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 } else {
2691 bp2 = bp1;
2692 }
2693
Dave Chinner1d9025e2012-06-22 18:50:14 +10002694 leaf1 = bp1->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002695 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 ASSERT(args->index >= 0);
2697 entry1 = &leaf1->entries[ args->index ];
2698
Dave Chinner1d9025e2012-06-22 18:50:14 +10002699 leaf2 = bp2->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002700 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 ASSERT(args->index2 >= 0);
2702 entry2 = &leaf2->entries[ args->index2 ];
2703
2704#ifdef DEBUG
2705 if (entry1->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002706 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 namelen1 = name_loc->namelen;
2708 name1 = (char *)name_loc->nameval;
2709 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002710 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 namelen1 = name_rmt->namelen;
2712 name1 = (char *)name_rmt->name;
2713 }
2714 if (entry2->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002715 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 namelen2 = name_loc->namelen;
2717 name2 = (char *)name_loc->nameval;
2718 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002719 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 namelen2 = name_rmt->namelen;
2721 name2 = (char *)name_rmt->name;
2722 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002723 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 ASSERT(namelen1 == namelen2);
2725 ASSERT(memcmp(name1, name2, namelen1) == 0);
2726#endif /* DEBUG */
2727
2728 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2729 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2730
2731 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002732 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2734 if (args->rmtblkno) {
2735 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002736 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002737 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2738 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002739 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2741 }
2742
2743 entry2->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002744 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2746 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002747 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 name_rmt->valueblk = 0;
2749 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002750 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /*
2755 * Commit the flag value change and start the next trans in series.
2756 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002757 error = xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 return(error);
2760}
2761
2762/*========================================================================
2763 * Indiscriminately delete the entire attribute fork
2764 *========================================================================*/
2765
2766/*
2767 * Recurse (gasp!) through the attribute nodes until we find leaves.
2768 * We're doing a depth-first traversal in order to invalidate everything.
2769 */
2770int
2771xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2772{
2773 xfs_da_blkinfo_t *info;
2774 xfs_daddr_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002775 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 int error;
2777
2778 /*
2779 * Read block 0 to see what we have to work with.
2780 * We only get here if we have extents, since we remove
2781 * the extents in reverse order the extent containing
2782 * block 0 must still be there.
2783 */
Dave Chinnerd9392a42012-11-12 22:54:17 +11002784 error = xfs_da_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (error)
2786 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002787 blkno = XFS_BUF_ADDR(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 /*
2790 * Invalidate the tree, even if the "tree" is only a single leaf block.
2791 * This is a depth-first traversal!
2792 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002793 info = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002794 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 error = xfs_attr_node_inactive(trans, dp, bp, 1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002796 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 error = xfs_attr_leaf_inactive(trans, dp, bp);
2798 } else {
2799 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002800 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802 if (error)
2803 return(error);
2804
2805 /*
2806 * Invalidate the incore copy of the root block.
2807 */
2808 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2809 if (error)
2810 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002811 xfs_trans_binval(*trans, bp); /* remove from cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 /*
2813 * Commit the invalidate and start the next transaction.
2814 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002815 error = xfs_trans_roll(trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817 return (error);
2818}
2819
2820/*
2821 * Recurse (gasp!) through the attribute nodes until we find leaves.
2822 * We're doing a depth-first traversal in order to invalidate everything.
2823 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002824STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002825xfs_attr_node_inactive(
2826 struct xfs_trans **trans,
2827 struct xfs_inode *dp,
2828 struct xfs_buf *bp,
2829 int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
2831 xfs_da_blkinfo_t *info;
2832 xfs_da_intnode_t *node;
2833 xfs_dablk_t child_fsb;
2834 xfs_daddr_t parent_blkno, child_blkno;
2835 int error, count, i;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002836 struct xfs_buf *child_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 /*
2839 * Since this code is recursive (gasp!) we must protect ourselves.
2840 */
2841 if (level > XFS_DA_NODE_MAXDEPTH) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002842 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return(XFS_ERROR(EIO));
2844 }
2845
Dave Chinner1d9025e2012-06-22 18:50:14 +10002846 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002847 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +10002848 parent_blkno = XFS_BUF_ADDR(bp); /* save for re-read later */
Nathan Scottfac80cc2006-03-17 17:29:56 +11002849 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 if (!count) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002851 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return(0);
2853 }
Nathan Scott403432d2006-03-17 17:29:46 +11002854 child_fsb = be32_to_cpu(node->btree[0].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002855 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857 /*
2858 * If this is the node level just above the leaves, simply loop
2859 * over the leaves removing all of them. If this is higher up
2860 * in the tree, recurse downward.
2861 */
2862 for (i = 0; i < count; i++) {
2863 /*
2864 * Read the subsidiary block to see what we have to work with.
2865 * Don't do this in a transaction. This is a depth-first
2866 * traversal of the tree so we may deal with many blocks
2867 * before we come back to this one.
2868 */
Dave Chinnerd9392a42012-11-12 22:54:17 +11002869 error = xfs_da_node_read(*trans, dp, child_fsb, -2, &child_bp,
2870 XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (error)
2872 return(error);
2873 if (child_bp) {
2874 /* save for re-read later */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002875 child_blkno = XFS_BUF_ADDR(child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 /*
2878 * Invalidate the subtree, however we have to.
2879 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002880 info = child_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002881 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 error = xfs_attr_node_inactive(trans, dp,
2883 child_bp, level+1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002884 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 error = xfs_attr_leaf_inactive(trans, dp,
2886 child_bp);
2887 } else {
2888 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002889 xfs_trans_brelse(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891 if (error)
2892 return(error);
2893
2894 /*
2895 * Remove the subsidiary block from the cache
2896 * and from the log.
2897 */
2898 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2899 &child_bp, XFS_ATTR_FORK);
2900 if (error)
2901 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002902 xfs_trans_binval(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
2904
2905 /*
2906 * If we're not done, re-read the parent to get the next
2907 * child block number.
2908 */
2909 if ((i+1) < count) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11002910 error = xfs_da_node_read(*trans, dp, 0, parent_blkno,
2911 &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (error)
2913 return(error);
Nathan Scott403432d2006-03-17 17:29:46 +11002914 child_fsb = be32_to_cpu(node->btree[i+1].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002915 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
2917 /*
2918 * Atomically commit the whole invalidate stuff.
2919 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002920 error = xfs_trans_roll(trans, dp);
2921 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return (error);
2923 }
2924
2925 return(0);
2926}
2927
2928/*
2929 * Invalidate all of the "remote" value regions pointed to by a particular
2930 * leaf block.
2931 * Note that we must release the lock on the buffer so that we are not
2932 * caught holding something that the logging code wants to flush to disk.
2933 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002934STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002935xfs_attr_leaf_inactive(
2936 struct xfs_trans **trans,
2937 struct xfs_inode *dp,
2938 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
2940 xfs_attr_leafblock_t *leaf;
2941 xfs_attr_leaf_entry_t *entry;
2942 xfs_attr_leaf_name_remote_t *name_rmt;
2943 xfs_attr_inactive_list_t *list, *lp;
2944 int error, count, size, tmp, i;
2945
Dave Chinner1d9025e2012-06-22 18:50:14 +10002946 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002947 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 /*
2950 * Count the number of "remote" value extents.
2951 */
2952 count = 0;
2953 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002954 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002955 if (be16_to_cpu(entry->nameidx) &&
2956 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002957 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (name_rmt->valueblk)
2959 count++;
2960 }
2961 }
2962
2963 /*
2964 * If there are no "remote" values, we're done.
2965 */
2966 if (count == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002967 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return(0);
2969 }
2970
2971 /*
2972 * Allocate storage for a list of all the "remote" value extents.
2973 */
2974 size = count * sizeof(xfs_attr_inactive_list_t);
2975 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2976
2977 /*
2978 * Identify each of the "remote" value extents.
2979 */
2980 lp = list;
2981 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002982 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002983 if (be16_to_cpu(entry->nameidx) &&
2984 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002985 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11002987 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2988 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2989 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 lp++;
2991 }
2992 }
2993 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002994 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 /*
2997 * Invalidate each of the "remote" value extents.
2998 */
2999 error = 0;
3000 for (lp = list, i = 0; i < count; i++, lp++) {
3001 tmp = xfs_attr_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11003002 lp->valueblk, lp->valuelen);
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (error == 0)
3005 error = tmp; /* save only the 1st errno */
3006 }
3007
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003008 kmem_free((xfs_caddr_t)list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return(error);
3010}
3011
3012/*
3013 * Look at all the extents for this logical region,
3014 * invalidate any buffers that are incore/in transactions.
3015 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003016STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
3018 xfs_dablk_t blkno, int blkcnt)
3019{
3020 xfs_bmbt_irec_t map;
3021 xfs_dablk_t tblkno;
3022 int tblkcnt, dblkcnt, nmap, error;
3023 xfs_daddr_t dblkno;
3024 xfs_buf_t *bp;
3025
3026 /*
3027 * Roll through the "value", invalidating the attribute value's
3028 * blocks.
3029 */
3030 tblkno = blkno;
3031 tblkcnt = blkcnt;
3032 while (tblkcnt > 0) {
3033 /*
3034 * Try to remember where we decided to put the value.
3035 */
3036 nmap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003037 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
3038 &map, &nmap, XFS_BMAPI_ATTRFORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (error) {
3040 return(error);
3041 }
3042 ASSERT(nmap == 1);
3043 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3044
3045 /*
3046 * If it's a hole, these are already unmapped
3047 * so there's nothing to invalidate.
3048 */
3049 if (map.br_startblock != HOLESTARTBLOCK) {
3050
3051 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3052 map.br_startblock);
3053 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3054 map.br_blockcount);
3055 bp = xfs_trans_get_buf(*trans,
3056 dp->i_mount->m_ddev_targp,
Dave Chinnera8acad72012-04-23 15:58:54 +10003057 dblkno, dblkcnt, 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00003058 if (!bp)
3059 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 xfs_trans_binval(*trans, bp);
3061 /*
3062 * Roll to next transaction.
3063 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003064 error = xfs_trans_roll(trans, dp);
3065 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return (error);
3067 }
3068
3069 tblkno += map.br_blockcount;
3070 tblkcnt -= map.br_blockcount;
3071 }
3072
3073 return(0);
3074}