blob: ba2b9a2cd2364fc98fab4a513a00501b60b6aea0 [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
91/*========================================================================
92 * Namespace helper routines
93 *========================================================================*/
94
Tim Shimmin726801b2006-09-28 11:01:37 +100095/*
96 * If namespace bits don't match return 0.
97 * If all match then return 1.
98 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +000099STATIC int
Tim Shimmin726801b2006-09-28 11:01:37 +1000100xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
101{
102 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100107 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *========================================================================*/
109
110/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100111 * Query whether the requested number of additional bytes of extended
112 * attribute space will be able to fit inline.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000113 *
Nathan Scottd8cc8902005-11-02 10:34:53 +1100114 * Returns zero if not, else the di_forkoff fork offset to be used in the
115 * literal area for attribute data once the new bytes have been added.
116 *
117 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
118 * special case for dev/uuid inodes, they have fixed size data forks.
119 */
120int
121xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
122{
123 int offset;
124 int minforkoff; /* lower limit on valid forkoff locations */
125 int maxforkoff; /* upper limit on valid forkoff locations */
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000126 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100127 xfs_mount_t *mp = dp->i_mount;
128
Nathan Scottd8cc8902005-11-02 10:34:53 +1100129 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
130
131 switch (dp->i_d.di_format) {
132 case XFS_DINODE_FMT_DEV:
133 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
134 return (offset >= minforkoff) ? minforkoff : 0;
135 case XFS_DINODE_FMT_UUID:
136 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
137 return (offset >= minforkoff) ? minforkoff : 0;
138 }
139
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000140 /*
141 * If the requested numbers of bytes is smaller or equal to the
142 * current attribute fork size we can always proceed.
143 *
144 * Note that if_bytes in the data fork might actually be larger than
145 * the current data fork size is due to delalloc extents. In that
146 * case either the extent count will go down when they are converted
147 * to real extents, or the delalloc conversion will take care of the
148 * literal area rebalancing.
149 */
150 if (bytes <= XFS_IFORK_ASIZE(dp))
151 return dp->i_d.di_forkoff;
152
153 /*
154 * For attr2 we can try to move the forkoff if there is space in the
155 * literal area, but for the old format we are done if there is no
156 * space in the fixed attribute fork.
157 */
158 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
Nathan Scottda087ba2005-11-02 15:00:20 +1100159 return 0;
Nathan Scottda087ba2005-11-02 15:00:20 +1100160
Barry Naujoke5889e92007-02-10 18:35:58 +1100161 dsize = dp->i_df.if_bytes;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000162
Barry Naujoke5889e92007-02-10 18:35:58 +1100163 switch (dp->i_d.di_format) {
164 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000165 /*
Barry Naujoke5889e92007-02-10 18:35:58 +1100166 * If there is no attr fork and the data fork is extents,
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000167 * determine if creating the default attr fork will result
168 * in the extents form migrating to btree. If so, the
169 * minimum offset only needs to be the space required for
Barry Naujoke5889e92007-02-10 18:35:58 +1100170 * the btree root.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000171 */
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200172 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
173 xfs_default_attroffset(dp))
Barry Naujoke5889e92007-02-10 18:35:58 +1100174 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
175 break;
Barry Naujoke5889e92007-02-10 18:35:58 +1100176 case XFS_DINODE_FMT_BTREE:
177 /*
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000178 * If we have a data btree then keep forkoff if we have one,
179 * otherwise we are adding a new attr, so then we set
180 * minforkoff to where the btree root can finish so we have
Barry Naujoke5889e92007-02-10 18:35:58 +1100181 * plenty of room for attrs
182 */
183 if (dp->i_d.di_forkoff) {
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000184 if (offset < dp->i_d.di_forkoff)
Barry Naujoke5889e92007-02-10 18:35:58 +1100185 return 0;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000186 return dp->i_d.di_forkoff;
187 }
188 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
Barry Naujoke5889e92007-02-10 18:35:58 +1100189 break;
190 }
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000191
192 /*
193 * A data fork btree root must have space for at least
Barry Naujoke5889e92007-02-10 18:35:58 +1100194 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
195 */
196 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100197 minforkoff = roundup(minforkoff, 8) >> 3;
198
199 /* attr fork btree root can have at least this many key/ptr pairs */
200 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
201 maxforkoff = maxforkoff >> 3; /* rounded down */
202
Nathan Scottd8cc8902005-11-02 10:34:53 +1100203 if (offset >= maxforkoff)
204 return maxforkoff;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000205 if (offset >= minforkoff)
206 return offset;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100207 return 0;
208}
209
210/*
211 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
212 */
213STATIC void
214xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
215{
Nathan Scott13059ff2006-01-11 15:32:01 +1100216 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100217 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000218 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100219 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
220 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000221 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100222 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
223 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000224 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100225 }
226}
227
228/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * Create the initial contents of a shortform attribute list.
230 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100231void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232xfs_attr_shortform_create(xfs_da_args_t *args)
233{
234 xfs_attr_sf_hdr_t *hdr;
235 xfs_inode_t *dp;
236 xfs_ifork_t *ifp;
237
Dave Chinner5a5881c2012-03-22 05:15:13 +0000238 trace_xfs_attr_sf_create(args);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 dp = args->dp;
241 ASSERT(dp != NULL);
242 ifp = dp->i_afp;
243 ASSERT(ifp != NULL);
244 ASSERT(ifp->if_bytes == 0);
245 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
246 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
247 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
248 ifp->if_flags |= XFS_IFINLINE;
249 } else {
250 ASSERT(ifp->if_flags & XFS_IFINLINE);
251 }
252 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
253 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
254 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100255 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259/*
260 * Add a name/value pair to the shortform attribute list.
261 * Overflow from the inode has already been checked for.
262 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100263void
264xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 xfs_attr_shortform_t *sf;
267 xfs_attr_sf_entry_t *sfe;
268 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100269 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 xfs_inode_t *dp;
271 xfs_ifork_t *ifp;
272
Dave Chinner5a5881c2012-03-22 05:15:13 +0000273 trace_xfs_attr_sf_add(args);
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100276 mp = dp->i_mount;
277 dp->i_d.di_forkoff = forkoff;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ifp = dp->i_afp;
280 ASSERT(ifp->if_flags & XFS_IFINLINE);
281 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
282 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100283 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100284#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (sfe->namelen != args->namelen)
286 continue;
287 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
288 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000289 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100291 ASSERT(0);
292#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
295 offset = (char *)sfe - (char *)sf;
296 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
297 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
298 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
299 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
300
301 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100302 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000303 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 memcpy(sfe->nameval, args->name, args->namelen);
305 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100306 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800307 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
309
Nathan Scottd8cc8902005-11-02 10:34:53 +1100310 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
Christoph Hellwige1486de2009-02-04 09:36:00 +0100314 * After the last attribute is removed revert to original inode format,
315 * making all literal area available to the data fork once more.
316 */
317STATIC void
318xfs_attr_fork_reset(
319 struct xfs_inode *ip,
320 struct xfs_trans *tp)
321{
322 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
323 ip->i_d.di_forkoff = 0;
324 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
325
326 ASSERT(ip->i_d.di_anextents == 0);
327 ASSERT(ip->i_afp == NULL);
328
Christoph Hellwige1486de2009-02-04 09:36:00 +0100329 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
330}
331
332/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100333 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
335int
336xfs_attr_shortform_remove(xfs_da_args_t *args)
337{
338 xfs_attr_shortform_t *sf;
339 xfs_attr_sf_entry_t *sfe;
340 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100341 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 xfs_inode_t *dp;
343
Dave Chinner5a5881c2012-03-22 05:15:13 +0000344 trace_xfs_attr_sf_remove(args);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100347 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 base = sizeof(xfs_attr_sf_hdr_t);
349 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
350 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100351 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100352 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 base += size, i++) {
354 size = XFS_ATTR_SF_ENTSIZE(sfe);
355 if (sfe->namelen != args->namelen)
356 continue;
357 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
358 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000359 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 continue;
361 break;
362 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100363 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return(XFS_ERROR(ENOATTR));
365
Nathan Scottd8cc8902005-11-02 10:34:53 +1100366 /*
367 * Fix up the attribute fork data, covering the hole
368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100370 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100371 if (end != totsize)
372 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100373 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800374 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100375
376 /*
377 * Fix up the start offset of the attribute fork
378 */
379 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000380 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
Christoph Hellwige1486de2009-02-04 09:36:00 +0100381 (mp->m_flags & XFS_MOUNT_ATTR2) &&
382 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
383 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
384 xfs_attr_fork_reset(dp, args->trans);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100385 } else {
386 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
387 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
388 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000389 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
390 (args->op_flags & XFS_DA_OP_ADDNAME) ||
391 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
392 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100393 xfs_trans_log_inode(args->trans, dp,
394 XFS_ILOG_CORE | XFS_ILOG_ADATA);
395 }
396
397 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return(0);
400}
401
402/*
403 * Look up a name in a shortform attribute list structure.
404 */
405/*ARGSUSED*/
406int
407xfs_attr_shortform_lookup(xfs_da_args_t *args)
408{
409 xfs_attr_shortform_t *sf;
410 xfs_attr_sf_entry_t *sfe;
411 int i;
412 xfs_ifork_t *ifp;
413
Dave Chinner5a5881c2012-03-22 05:15:13 +0000414 trace_xfs_attr_sf_lookup(args);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ifp = args->dp->i_afp;
417 ASSERT(ifp->if_flags & XFS_IFINLINE);
418 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
419 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100420 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
422 if (sfe->namelen != args->namelen)
423 continue;
424 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
425 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000426 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 continue;
428 return(XFS_ERROR(EEXIST));
429 }
430 return(XFS_ERROR(ENOATTR));
431}
432
433/*
434 * Look up a name in a shortform attribute list structure.
435 */
436/*ARGSUSED*/
437int
438xfs_attr_shortform_getvalue(xfs_da_args_t *args)
439{
440 xfs_attr_shortform_t *sf;
441 xfs_attr_sf_entry_t *sfe;
442 int i;
443
444 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
445 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
446 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100447 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
449 if (sfe->namelen != args->namelen)
450 continue;
451 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
452 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000453 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 continue;
455 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100456 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return(XFS_ERROR(EEXIST));
458 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100459 if (args->valuelen < sfe->valuelen) {
460 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return(XFS_ERROR(ERANGE));
462 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100463 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 memcpy(args->value, &sfe->nameval[args->namelen],
465 args->valuelen);
466 return(XFS_ERROR(EEXIST));
467 }
468 return(XFS_ERROR(ENOATTR));
469}
470
471/*
472 * Convert from using the shortform to the leaf.
473 */
474int
475xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
476{
477 xfs_inode_t *dp;
478 xfs_attr_shortform_t *sf;
479 xfs_attr_sf_entry_t *sfe;
480 xfs_da_args_t nargs;
481 char *tmpbuffer;
482 int error, i, size;
483 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000484 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 xfs_ifork_t *ifp;
486
Dave Chinner5a5881c2012-03-22 05:15:13 +0000487 trace_xfs_attr_sf_to_leaf(args);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 dp = args->dp;
490 ifp = dp->i_afp;
491 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100492 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 tmpbuffer = kmem_alloc(size, KM_SLEEP);
494 ASSERT(tmpbuffer != NULL);
495 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
496 sf = (xfs_attr_shortform_t *)tmpbuffer;
497
498 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
499 bp = NULL;
500 error = xfs_da_grow_inode(args, &blkno);
501 if (error) {
502 /*
503 * If we hit an IO error middle of the transaction inside
504 * grow_inode(), we may have inconsistent data. Bail out.
505 */
506 if (error == EIO)
507 goto out;
508 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
509 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
510 goto out;
511 }
512
513 ASSERT(blkno == 0);
514 error = xfs_attr_leaf_create(args, blkno, &bp);
515 if (error) {
516 error = xfs_da_shrink_inode(args, 0, bp);
517 bp = NULL;
518 if (error)
519 goto out;
520 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
521 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
522 goto out;
523 }
524
525 memset((char *)&nargs, 0, sizeof(nargs));
526 nargs.dp = dp;
527 nargs.firstblock = args->firstblock;
528 nargs.flist = args->flist;
529 nargs.total = args->total;
530 nargs.whichfork = XFS_ATTR_FORK;
531 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000532 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100535 for (i = 0; i < sf->hdr.count; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100536 nargs.name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 nargs.namelen = sfe->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100538 nargs.value = &sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100539 nargs.valuelen = sfe->valuelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100540 nargs.hashval = xfs_da_hashname(sfe->nameval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000542 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
544 ASSERT(error == ENOATTR);
545 error = xfs_attr_leaf_add(bp, &nargs);
546 ASSERT(error != ENOSPC);
547 if (error)
548 goto out;
549 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
550 }
551 error = 0;
552
553out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000554 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return(error);
556}
557
558STATIC int
559xfs_attr_shortform_compare(const void *a, const void *b)
560{
561 xfs_attr_sf_sort_t *sa, *sb;
562
563 sa = (xfs_attr_sf_sort_t *)a;
564 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100565 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100567 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return(1);
569 } else {
570 return(sa->entno - sb->entno);
571 }
572}
573
Tim Shimmin726801b2006-09-28 11:01:37 +1000574
575#define XFS_ISRESET_CURSOR(cursor) \
576 (!((cursor)->initted) && !((cursor)->hashval) && \
577 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000580 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * If the output buffer is not large enough to hold them all, then we
582 * we have to calculate each entries' hashvalue and sort them before
583 * we can begin returning them to the user.
584 */
585/*ARGSUSED*/
586int
587xfs_attr_shortform_list(xfs_attr_list_context_t *context)
588{
589 attrlist_cursor_kern_t *cursor;
590 xfs_attr_sf_sort_t *sbuf, *sbp;
591 xfs_attr_shortform_t *sf;
592 xfs_attr_sf_entry_t *sfe;
593 xfs_inode_t *dp;
594 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000595 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 ASSERT(context != NULL);
598 dp = context->dp;
599 ASSERT(dp != NULL);
600 ASSERT(dp->i_afp != NULL);
601 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
602 ASSERT(sf != NULL);
603 if (!sf->hdr.count)
604 return(0);
605 cursor = context->cursor;
606 ASSERT(cursor != NULL);
607
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000608 trace_xfs_attr_list_sf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000611 * If the buffer is large enough and the cursor is at the start,
612 * do not bother with sorting since we will return everything in
613 * one buffer and another call using the cursor won't need to be
614 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000616 * If bufsize is zero then put_listent must be a search function
617 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000619 if (context->bufsize == 0 ||
620 (XFS_ISRESET_CURSOR(cursor) &&
621 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100622 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000623 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000624 sfe->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100625 sfe->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +1000626 (int)sfe->namelen,
627 (int)sfe->valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100628 &sfe->nameval[sfe->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000629
630 /*
631 * Either search callback finished early or
632 * didn't fit it all in the buffer after all.
633 */
634 if (context->seen_enough)
635 break;
636
637 if (error)
638 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
640 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000641 trace_xfs_attr_list_sf_all(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return(0);
643 }
644
Tim Shimmin726801b2006-09-28 11:01:37 +1000645 /* do no more for a search callback */
646 if (context->bufsize == 0)
647 return 0;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
650 * It didn't all fit, so we have to sort everything on hashval.
651 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100652 sbsize = sf->hdr.count * sizeof(*sbuf);
Dave Chinner622d8142010-12-23 11:57:37 +1100653 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /*
656 * Scan the attribute list for the rest of the entries, storing
657 * the relevant info from only those that match into a buffer.
658 */
659 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100660 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (unlikely(
662 ((char *)sfe < (char *)sf) ||
663 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
664 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
665 XFS_ERRLEVEL_LOW,
666 context->dp->i_mount, sfe);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000667 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return XFS_ERROR(EFSCORRUPTED);
669 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 sbp->entno = i;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100672 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
673 sbp->name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 sbp->namelen = sfe->namelen;
675 /* These are bytes, and both on-disk, don't endian-flip */
676 sbp->valuelen = sfe->valuelen;
677 sbp->flags = sfe->flags;
678 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
679 sbp++;
680 nsbuf++;
681 }
682
683 /*
684 * Sort the entries on hash then entno.
685 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100686 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /*
689 * Re-find our place IN THE SORTED LIST.
690 */
691 count = 0;
692 cursor->initted = 1;
693 cursor->blkno = 0;
694 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100695 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (cursor->offset == count) {
697 break;
698 }
699 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100700 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 }
703 }
704 if (i == nsbuf) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000705 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return(0);
707 }
708
709 /*
710 * Loop putting entries into the user buffer.
711 */
712 for ( ; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100713 if (cursor->hashval != sbp->hash) {
714 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 cursor->offset = 0;
716 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000717 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000718 sbp->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000719 sbp->name,
720 sbp->namelen,
721 sbp->valuelen,
722 &sbp->name[sbp->namelen]);
723 if (error)
724 return error;
725 if (context->seen_enough)
726 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 cursor->offset++;
728 }
729
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000730 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return(0);
732}
733
734/*
735 * Check a leaf attribute block to see if all the entries would fit into
736 * a shortform attribute list.
737 */
738int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000739xfs_attr_shortform_allfit(
740 struct xfs_buf *bp,
741 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 xfs_attr_leafblock_t *leaf;
744 xfs_attr_leaf_entry_t *entry;
745 xfs_attr_leaf_name_local_t *name_loc;
746 int bytes, i;
747
Dave Chinner1d9025e2012-06-22 18:50:14 +1000748 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200749 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 entry = &leaf->entries[0];
752 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100753 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (entry->flags & XFS_ATTR_INCOMPLETE)
755 continue; /* don't copy partial entries */
756 if (!(entry->flags & XFS_ATTR_LOCAL))
757 return(0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600758 name_loc = xfs_attr_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
760 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100761 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return(0);
763 bytes += sizeof(struct xfs_attr_sf_entry)-1
764 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100765 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100767 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100768 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100769 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100770 return(-1);
771 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/*
775 * Convert a leaf attribute list to shortform attribute list
776 */
777int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000778xfs_attr_leaf_to_shortform(
779 struct xfs_buf *bp,
780 xfs_da_args_t *args,
781 int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 xfs_attr_leafblock_t *leaf;
784 xfs_attr_leaf_entry_t *entry;
785 xfs_attr_leaf_name_local_t *name_loc;
786 xfs_da_args_t nargs;
787 xfs_inode_t *dp;
788 char *tmpbuffer;
789 int error, i;
790
Dave Chinner5a5881c2012-03-22 05:15:13 +0000791 trace_xfs_attr_leaf_to_sf(args);
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 dp = args->dp;
794 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
795 ASSERT(tmpbuffer != NULL);
796
797 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000798 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200800 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000801 memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 /*
804 * Clean out the prior contents of the attribute list.
805 */
806 error = xfs_da_shrink_inode(args, 0, bp);
807 if (error)
808 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100809
810 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +1100811 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +1100812 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Christoph Hellwige1486de2009-02-04 09:36:00 +0100813 xfs_attr_fork_reset(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100815 }
816
817 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /*
820 * Copy the attributes
821 */
822 memset((char *)&nargs, 0, sizeof(nargs));
823 nargs.dp = dp;
824 nargs.firstblock = args->firstblock;
825 nargs.flist = args->flist;
826 nargs.total = args->total;
827 nargs.whichfork = XFS_ATTR_FORK;
828 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000829 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +1100831 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (entry->flags & XFS_ATTR_INCOMPLETE)
833 continue; /* don't copy partial entries */
834 if (!entry->nameidx)
835 continue;
836 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600837 name_loc = xfs_attr_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +1100838 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100840 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +1100841 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100842 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +1000843 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100844 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 error = 0;
847
848out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000849 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return(error);
851}
852
853/*
854 * Convert from using a single leaf to a root node and a leaf.
855 */
856int
857xfs_attr_leaf_to_node(xfs_da_args_t *args)
858{
859 xfs_attr_leafblock_t *leaf;
860 xfs_da_intnode_t *node;
861 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000862 struct xfs_buf *bp1, *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 xfs_dablk_t blkno;
864 int error;
865
Dave Chinner5a5881c2012-03-22 05:15:13 +0000866 trace_xfs_attr_leaf_to_node(args);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 dp = args->dp;
869 bp1 = bp2 = NULL;
870 error = xfs_da_grow_inode(args, &blkno);
871 if (error)
872 goto out;
873 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
Dave Chinner4bb20a82012-11-12 22:54:10 +1100874 XFS_ATTR_FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (error)
876 goto out;
877 ASSERT(bp1 != NULL);
878 bp2 = NULL;
879 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
880 XFS_ATTR_FORK);
881 if (error)
882 goto out;
883 ASSERT(bp2 != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000884 memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 bp1 = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000886 xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /*
889 * Set up the new root node.
890 */
891 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
892 if (error)
893 goto out;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000894 node = bp1->b_addr;
895 leaf = bp2->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200896 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* both on-disk, don't endian-flip twice */
898 node->btree[0].hashval =
Nathan Scott918ae422006-03-17 17:28:54 +1100899 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
Nathan Scott403432d2006-03-17 17:29:46 +1100900 node->btree[0].before = cpu_to_be32(blkno);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100901 node->hdr.count = cpu_to_be16(1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000902 xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 error = 0;
904out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return(error);
906}
907
908
909/*========================================================================
910 * Routines used for growing the Btree.
911 *========================================================================*/
912
913/*
914 * Create the initial contents of a leaf attribute list
915 * or a leaf in a node attribute list.
916 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000917STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000918xfs_attr_leaf_create(
919 xfs_da_args_t *args,
920 xfs_dablk_t blkno,
921 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 xfs_attr_leafblock_t *leaf;
924 xfs_attr_leaf_hdr_t *hdr;
925 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000926 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int error;
928
Dave Chinner5a5881c2012-03-22 05:15:13 +0000929 trace_xfs_attr_leaf_create(args);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 dp = args->dp;
932 ASSERT(dp != NULL);
933 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
934 XFS_ATTR_FORK);
935 if (error)
936 return(error);
937 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000938 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
940 hdr = &leaf->hdr;
Nathan Scott89da0542006-03-17 17:28:40 +1100941 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +1100942 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +1100944 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
946 }
947
Nathan Scott918ae422006-03-17 17:28:54 +1100948 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
949 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
950 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Dave Chinner1d9025e2012-06-22 18:50:14 +1000952 xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 *bpp = bp;
955 return(0);
956}
957
958/*
959 * Split the leaf node, rebalance, then add the new entry.
960 */
961int
962xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
963 xfs_da_state_blk_t *newblk)
964{
965 xfs_dablk_t blkno;
966 int error;
967
Dave Chinner5a5881c2012-03-22 05:15:13 +0000968 trace_xfs_attr_leaf_split(state->args);
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /*
971 * Allocate space for a new leaf node.
972 */
973 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
974 error = xfs_da_grow_inode(state->args, &blkno);
975 if (error)
976 return(error);
977 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
978 if (error)
979 return(error);
980 newblk->blkno = blkno;
981 newblk->magic = XFS_ATTR_LEAF_MAGIC;
982
983 /*
984 * Rebalance the entries across the two leaves.
985 * NOTE: rebalance() currently depends on the 2nd block being empty.
986 */
987 xfs_attr_leaf_rebalance(state, oldblk, newblk);
988 error = xfs_da_blk_link(state, oldblk, newblk);
989 if (error)
990 return(error);
991
992 /*
993 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
994 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
995 * "new" attrs info. Will need the "old" info to remove it later.
996 *
997 * Insert the "new" entry in the correct block.
998 */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000999 if (state->inleaf) {
1000 trace_xfs_attr_leaf_add_old(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 error = xfs_attr_leaf_add(oldblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001002 } else {
1003 trace_xfs_attr_leaf_add_new(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 error = xfs_attr_leaf_add(newblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /*
1008 * Update last hashval in each block since we added the name.
1009 */
1010 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1011 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1012 return(error);
1013}
1014
1015/*
1016 * Add a name to the leaf attribute list structure.
1017 */
1018int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001019xfs_attr_leaf_add(
1020 struct xfs_buf *bp,
1021 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 xfs_attr_leafblock_t *leaf;
1024 xfs_attr_leaf_hdr_t *hdr;
1025 xfs_attr_leaf_map_t *map;
1026 int tablesize, entsize, sum, tmp, i;
1027
Dave Chinner5a5881c2012-03-22 05:15:13 +00001028 trace_xfs_attr_leaf_add(args);
1029
Dave Chinner1d9025e2012-06-22 18:50:14 +10001030 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001031 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001033 && (args->index <= be16_to_cpu(leaf->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 hdr = &leaf->hdr;
Nathan Scottaa82daa2005-11-02 10:33:33 +11001035 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1037
1038 /*
1039 * Search through freemap for first-fit on new name length.
1040 * (may need to figure in size of entry struct too)
1041 */
Nathan Scott918ae422006-03-17 17:28:54 +11001042 tablesize = (be16_to_cpu(hdr->count) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * sizeof(xfs_attr_leaf_entry_t)
1044 + sizeof(xfs_attr_leaf_hdr_t);
1045 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1046 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
Nathan Scott918ae422006-03-17 17:28:54 +11001047 if (tablesize > be16_to_cpu(hdr->firstused)) {
1048 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 continue;
1050 }
1051 if (!map->size)
1052 continue; /* no space in this map */
1053 tmp = entsize;
Nathan Scott918ae422006-03-17 17:28:54 +11001054 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 tmp += sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001056 if (be16_to_cpu(map->size) >= tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 tmp = xfs_attr_leaf_add_work(bp, args, i);
1058 return(tmp);
1059 }
Nathan Scott918ae422006-03-17 17:28:54 +11001060 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
1063 /*
1064 * If there are no holes in the address space of the block,
1065 * and we don't have enough freespace, then compaction will do us
1066 * no good and we should just give up.
1067 */
1068 if (!hdr->holes && (sum < entsize))
1069 return(XFS_ERROR(ENOSPC));
1070
1071 /*
1072 * Compact the entries to coalesce free space.
1073 * This may change the hdr->count via dropping INCOMPLETE entries.
1074 */
Dave Chinneree732592012-11-12 22:53:53 +11001075 xfs_attr_leaf_compact(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /*
1078 * After compaction, the block is guaranteed to have only one
1079 * free region, in freemap[0]. If it is not big enough, give up.
1080 */
Nathan Scott918ae422006-03-17 17:28:54 +11001081 if (be16_to_cpu(hdr->freemap[0].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1083 return(XFS_ERROR(ENOSPC));
1084
1085 return(xfs_attr_leaf_add_work(bp, args, 0));
1086}
1087
1088/*
1089 * Add a name to a leaf attribute list structure.
1090 */
1091STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001092xfs_attr_leaf_add_work(
1093 struct xfs_buf *bp,
1094 xfs_da_args_t *args,
1095 int mapindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 xfs_attr_leafblock_t *leaf;
1098 xfs_attr_leaf_hdr_t *hdr;
1099 xfs_attr_leaf_entry_t *entry;
1100 xfs_attr_leaf_name_local_t *name_loc;
1101 xfs_attr_leaf_name_remote_t *name_rmt;
1102 xfs_attr_leaf_map_t *map;
1103 xfs_mount_t *mp;
1104 int tmp, i;
1105
Dave Chinneree732592012-11-12 22:53:53 +11001106 trace_xfs_attr_leaf_add_work(args);
1107
Dave Chinner1d9025e2012-06-22 18:50:14 +10001108 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001109 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 hdr = &leaf->hdr;
1111 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
Nathan Scott918ae422006-03-17 17:28:54 +11001112 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 /*
1115 * Force open some space in the entry array and fill it in.
1116 */
1117 entry = &leaf->entries[args->index];
Nathan Scott918ae422006-03-17 17:28:54 +11001118 if (args->index < be16_to_cpu(hdr->count)) {
1119 tmp = be16_to_cpu(hdr->count) - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 tmp *= sizeof(xfs_attr_leaf_entry_t);
1121 memmove((char *)(entry+1), (char *)entry, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001122 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1124 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001125 be16_add_cpu(&hdr->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /*
1128 * Allocate space for the new string (at the end of the run).
1129 */
1130 map = &hdr->freemap[mapindex];
1131 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001132 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1133 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1134 ASSERT(be16_to_cpu(map->size) >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001135 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1136 mp->m_sb.sb_blocksize, NULL));
Nathan Scott918ae422006-03-17 17:28:54 +11001137 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1138 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001139 be16_add_cpu(&map->size,
Nathan Scottaa82daa2005-11-02 10:33:33 +11001140 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1141 mp->m_sb.sb_blocksize, &tmp));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001142 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1143 be16_to_cpu(map->size));
1144 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001146 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001147 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 entry->flags |= XFS_ATTR_INCOMPLETE;
1149 if ((args->blkno2 == args->blkno) &&
1150 (args->index2 <= args->index)) {
1151 args->index2++;
1152 }
1153 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001154 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001156 ASSERT((args->index == 0) ||
1157 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Nathan Scott918ae422006-03-17 17:28:54 +11001158 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001159 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 * For "remote" attribute values, simply note that we need to
1163 * allocate space for the "remote" value. We can't actually
1164 * allocate the extents in this transaction, and we can't decide
1165 * which blocks they should be as we might allocate more blocks
1166 * as part of this transaction (a split operation for example).
1167 */
1168 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001169 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001171 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1173 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001174 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001176 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 name_rmt->namelen = args->namelen;
1178 memcpy((char *)name_rmt->name, args->name, args->namelen);
1179 entry->flags |= XFS_ATTR_INCOMPLETE;
1180 /* just in case */
1181 name_rmt->valuelen = 0;
1182 name_rmt->valueblk = 0;
1183 args->rmtblkno = 1;
1184 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1185 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001186 xfs_trans_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001187 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 xfs_attr_leaf_entsize(leaf, args->index)));
1189
1190 /*
1191 * Update the control info for this leaf node
1192 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001193 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* both on-disk, don't endian-flip twice */
1195 hdr->firstused = entry->nameidx;
1196 }
Nathan Scott918ae422006-03-17 17:28:54 +11001197 ASSERT(be16_to_cpu(hdr->firstused) >=
1198 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1199 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 + sizeof(xfs_attr_leaf_hdr_t);
1201 map = &hdr->freemap[0];
1202 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001203 if (be16_to_cpu(map->base) == tmp) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001204 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1205 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001206 -((int)sizeof(xfs_attr_leaf_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001209 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001210 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1212 return(0);
1213}
1214
1215/*
1216 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1217 */
1218STATIC void
Dave Chinner1d9025e2012-06-22 18:50:14 +10001219xfs_attr_leaf_compact(
Dave Chinneree732592012-11-12 22:53:53 +11001220 struct xfs_da_args *args,
1221 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Dave Chinneree732592012-11-12 22:53:53 +11001223 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1224 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1225 struct xfs_trans *trans = args->trans;
1226 struct xfs_mount *mp = trans->t_mountp;
1227 char *tmpbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Dave Chinneree732592012-11-12 22:53:53 +11001229 trace_xfs_attr_leaf_compact(args);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1232 ASSERT(tmpbuffer != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001233 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1234 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /*
1237 * Copy basic information
1238 */
1239 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001240 leaf_d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 hdr_s = &leaf_s->hdr;
1242 hdr_d = &leaf_d->hdr;
1243 hdr_d->info = hdr_s->info; /* struct copy */
Nathan Scott918ae422006-03-17 17:28:54 +11001244 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /* handle truncation gracefully */
1246 if (!hdr_d->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001247 hdr_d->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1249 }
1250 hdr_d->usedbytes = 0;
1251 hdr_d->count = 0;
1252 hdr_d->holes = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001253 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1254 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1255 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 /*
1258 * Copy all entry's in the same (sorted) order,
1259 * but allocate name/value pairs packed and in sequence.
1260 */
1261 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001262 be16_to_cpu(hdr_s->count), mp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001263 xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001265 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268/*
1269 * Redistribute the attribute list entries between two leaf nodes,
1270 * taking into account the size of the new entry.
1271 *
1272 * NOTE: if new block is empty, then it will get the upper half of the
1273 * old block. At present, all (one) callers pass in an empty second block.
1274 *
1275 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1276 * to match what it is doing in splitting the attribute leaf block. Those
1277 * values are used in "atomic rename" operations on attributes. Note that
1278 * the "new" and "old" values can end up in different blocks.
1279 */
1280STATIC void
1281xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1282 xfs_da_state_blk_t *blk2)
1283{
1284 xfs_da_args_t *args;
1285 xfs_da_state_blk_t *tmp_blk;
1286 xfs_attr_leafblock_t *leaf1, *leaf2;
1287 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1288 int count, totallen, max, space, swap;
1289
1290 /*
1291 * Set up environment.
1292 */
1293 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1294 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001295 leaf1 = blk1->bp->b_addr;
1296 leaf2 = blk2->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001297 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1298 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Dave Chinner07428d72012-11-12 22:09:44 +11001299 ASSERT(leaf2->hdr.count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 args = state->args;
1301
Dave Chinner5a5881c2012-03-22 05:15:13 +00001302 trace_xfs_attr_leaf_rebalance(args);
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * Check ordering of blocks, reverse if it makes things simpler.
1306 *
1307 * NOTE: Given that all (current) callers pass in an empty
1308 * second block, this code should never set "swap".
1309 */
1310 swap = 0;
1311 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1312 tmp_blk = blk1;
1313 blk1 = blk2;
1314 blk2 = tmp_blk;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001315 leaf1 = blk1->bp->b_addr;
1316 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 swap = 1;
1318 }
1319 hdr1 = &leaf1->hdr;
1320 hdr2 = &leaf2->hdr;
1321
1322 /*
1323 * Examine entries until we reduce the absolute difference in
1324 * byte usage between the two blocks to a minimum. Then get
1325 * the direction to copy and the number of elements to move.
1326 *
1327 * "inleaf" is true if the new entry should be inserted into blk1.
1328 * If "swap" is also true, then reverse the sense of "inleaf".
1329 */
1330 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1331 &count, &totallen);
1332 if (swap)
1333 state->inleaf = !state->inleaf;
1334
1335 /*
1336 * Move any entries required from leaf to leaf:
1337 */
Nathan Scott918ae422006-03-17 17:28:54 +11001338 if (count < be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /*
1340 * Figure the total bytes to be added to the destination leaf.
1341 */
1342 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001343 count = be16_to_cpu(hdr1->count) - count;
1344 space = be16_to_cpu(hdr1->usedbytes) - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 space += count * sizeof(xfs_attr_leaf_entry_t);
1346
1347 /*
1348 * leaf2 is the destination, compact it if it looks tight.
1349 */
Nathan Scott918ae422006-03-17 17:28:54 +11001350 max = be16_to_cpu(hdr2->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001352 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001353 if (space > max)
1354 xfs_attr_leaf_compact(args, blk2->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /*
1357 * Move high entries from leaf1 to low end of leaf2.
1358 */
Nathan Scott918ae422006-03-17 17:28:54 +11001359 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 leaf2, 0, count, state->mp);
1361
Dave Chinner1d9025e2012-06-22 18:50:14 +10001362 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1363 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Nathan Scott918ae422006-03-17 17:28:54 +11001364 } else if (count > be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /*
1366 * I assert that since all callers pass in an empty
1367 * second buffer, this code should never execute.
1368 */
Dave Chinner07428d72012-11-12 22:09:44 +11001369 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /*
1372 * Figure the total bytes to be added to the destination leaf.
1373 */
1374 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001375 count -= be16_to_cpu(hdr1->count);
1376 space = totallen - be16_to_cpu(hdr1->usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 space += count * sizeof(xfs_attr_leaf_entry_t);
1378
1379 /*
1380 * leaf1 is the destination, compact it if it looks tight.
1381 */
Nathan Scott918ae422006-03-17 17:28:54 +11001382 max = be16_to_cpu(hdr1->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001384 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001385 if (space > max)
1386 xfs_attr_leaf_compact(args, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /*
1389 * Move low entries from leaf2 to high end of leaf1.
1390 */
1391 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
Nathan Scott918ae422006-03-17 17:28:54 +11001392 be16_to_cpu(hdr1->count), count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Dave Chinner1d9025e2012-06-22 18:50:14 +10001394 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1395 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
1398 /*
1399 * Copy out last hashval in each block for B-tree code.
1400 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001401 blk1->hashval = be32_to_cpu(
1402 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1403 blk2->hashval = be32_to_cpu(
1404 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /*
1407 * Adjust the expected index for insertion.
1408 * NOTE: this code depends on the (current) situation that the
1409 * second block was originally empty.
1410 *
1411 * If the insertion point moved to the 2nd block, we must adjust
1412 * the index. We must also track the entry just following the
1413 * new entry for use in an "atomic rename" operation, that entry
1414 * is always the "old" entry and the "new" entry is what we are
1415 * inserting. The index/blkno fields refer to the "old" entry,
1416 * while the index2/blkno2 fields refer to the "new" entry.
1417 */
Nathan Scott918ae422006-03-17 17:28:54 +11001418 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ASSERT(state->inleaf == 0);
Nathan Scott918ae422006-03-17 17:28:54 +11001420 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 args->index = args->index2 = blk2->index;
1422 args->blkno = args->blkno2 = blk2->blkno;
Nathan Scott918ae422006-03-17 17:28:54 +11001423 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (state->inleaf) {
1425 args->index = blk1->index;
1426 args->blkno = blk1->blkno;
1427 args->index2 = 0;
1428 args->blkno2 = blk2->blkno;
1429 } else {
Dave Chinner07428d72012-11-12 22:09:44 +11001430 /*
1431 * On a double leaf split, the original attr location
1432 * is already stored in blkno2/index2, so don't
1433 * overwrite it overwise we corrupt the tree.
1434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 blk2->index = blk1->index
Nathan Scott918ae422006-03-17 17:28:54 +11001436 - be16_to_cpu(leaf1->hdr.count);
Dave Chinner07428d72012-11-12 22:09:44 +11001437 args->index = blk2->index;
1438 args->blkno = blk2->blkno;
1439 if (!state->extravalid) {
1440 /*
1441 * set the new attr location to match the old
1442 * one and let the higher level split code
1443 * decide where in the leaf to place it.
1444 */
1445 args->index2 = blk2->index;
1446 args->blkno2 = blk2->blkno;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 } else {
1450 ASSERT(state->inleaf == 1);
1451 args->index = args->index2 = blk1->index;
1452 args->blkno = args->blkno2 = blk1->blkno;
1453 }
1454}
1455
1456/*
1457 * Examine entries until we reduce the absolute difference in
1458 * byte usage between the two blocks to a minimum.
1459 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1460 * GROT: there will always be enough room in either block for a new entry.
1461 * GROT: Do a double-split for this case?
1462 */
1463STATIC int
1464xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1465 xfs_da_state_blk_t *blk1,
1466 xfs_da_state_blk_t *blk2,
1467 int *countarg, int *usedbytesarg)
1468{
1469 xfs_attr_leafblock_t *leaf1, *leaf2;
1470 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1471 xfs_attr_leaf_entry_t *entry;
1472 int count, max, index, totallen, half;
1473 int lastdelta, foundit, tmp;
1474
1475 /*
1476 * Set up environment.
1477 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001478 leaf1 = blk1->bp->b_addr;
1479 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 hdr1 = &leaf1->hdr;
1481 hdr2 = &leaf2->hdr;
1482 foundit = 0;
1483 totallen = 0;
1484
1485 /*
1486 * Examine entries until we reduce the absolute difference in
1487 * byte usage between the two blocks to a minimum.
1488 */
Nathan Scott918ae422006-03-17 17:28:54 +11001489 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 half = (max+1) * sizeof(*entry);
Nathan Scott918ae422006-03-17 17:28:54 +11001491 half += be16_to_cpu(hdr1->usedbytes) +
1492 be16_to_cpu(hdr2->usedbytes) +
1493 xfs_attr_leaf_newentsize(
1494 state->args->namelen,
1495 state->args->valuelen,
1496 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 half /= 2;
1498 lastdelta = state->blocksize;
1499 entry = &leaf1->entries[0];
1500 for (count = index = 0; count < max; entry++, index++, count++) {
1501
1502#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1503 /*
1504 * The new entry is in the first block, account for it.
1505 */
1506 if (count == blk1->index) {
1507 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001508 xfs_attr_leaf_newentsize(
1509 state->args->namelen,
1510 state->args->valuelen,
1511 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1513 break;
1514 lastdelta = XFS_ATTR_ABS(half - tmp);
1515 totallen = tmp;
1516 foundit = 1;
1517 }
1518
1519 /*
1520 * Wrap around into the second block if necessary.
1521 */
Nathan Scott918ae422006-03-17 17:28:54 +11001522 if (count == be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 leaf1 = leaf2;
1524 entry = &leaf1->entries[0];
1525 index = 0;
1526 }
1527
1528 /*
1529 * Figure out if next leaf entry would be too much.
1530 */
1531 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1532 index);
1533 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1534 break;
1535 lastdelta = XFS_ATTR_ABS(half - tmp);
1536 totallen = tmp;
1537#undef XFS_ATTR_ABS
1538 }
1539
1540 /*
1541 * Calculate the number of usedbytes that will end up in lower block.
1542 * If new entry not in lower block, fix up the count.
1543 */
1544 totallen -= count * sizeof(*entry);
1545 if (foundit) {
1546 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001547 xfs_attr_leaf_newentsize(
1548 state->args->namelen,
1549 state->args->valuelen,
1550 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552
1553 *countarg = count;
1554 *usedbytesarg = totallen;
1555 return(foundit);
1556}
1557
1558/*========================================================================
1559 * Routines used for shrinking the Btree.
1560 *========================================================================*/
1561
1562/*
1563 * Check a leaf block and its neighbors to see if the block should be
1564 * collapsed into one or the other neighbor. Always keep the block
1565 * with the smaller block number.
1566 * If the current block is over 50% full, don't try to join it, return 0.
1567 * If the block is empty, fill in the state structure and return 2.
1568 * If it can be collapsed, fill in the state structure and return 1.
1569 * If nothing can be done, return 0.
1570 *
1571 * GROT: allow for INCOMPLETE entries in calculation.
1572 */
1573int
1574xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1575{
1576 xfs_attr_leafblock_t *leaf;
1577 xfs_da_state_blk_t *blk;
1578 xfs_da_blkinfo_t *info;
1579 int count, bytes, forward, error, retval, i;
1580 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001581 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Dave Chinneree732592012-11-12 22:53:53 +11001583 trace_xfs_attr_leaf_toosmall(state->args);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /*
1586 * Check for the degenerate case of the block being over 50% full.
1587 * If so, it's not worth even looking to see if we might be able
1588 * to coalesce with a sibling.
1589 */
1590 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001591 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001592 ASSERT(info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001594 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1596 count * sizeof(xfs_attr_leaf_entry_t) +
Nathan Scott918ae422006-03-17 17:28:54 +11001597 be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (bytes > (state->blocksize >> 1)) {
1599 *action = 0; /* blk over 50%, don't try to join */
1600 return(0);
1601 }
1602
1603 /*
1604 * Check for the degenerate case of the block being empty.
1605 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001606 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 * to merge with the forward block unless it is NULL.
1608 */
1609 if (count == 0) {
1610 /*
1611 * Make altpath point to the block we want to keep and
1612 * path point to the block we want to drop (this one).
1613 */
Nathan Scott89da0542006-03-17 17:28:40 +11001614 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 memcpy(&state->altpath, &state->path, sizeof(state->path));
1616 error = xfs_da_path_shift(state, &state->altpath, forward,
1617 0, &retval);
1618 if (error)
1619 return(error);
1620 if (retval) {
1621 *action = 0;
1622 } else {
1623 *action = 2;
1624 }
1625 return(0);
1626 }
1627
1628 /*
1629 * Examine each sibling block to see if we can coalesce with
1630 * at least 25% free space to spare. We need to figure out
1631 * whether to merge with the forward or the backward block.
1632 * We prefer coalescing with the lower numbered sibling so as
1633 * to shrink an attribute list over time.
1634 */
1635 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +11001636 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 for (i = 0; i < 2; forward = !forward, i++) {
1638 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +11001639 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 else
Nathan Scott89da0542006-03-17 17:28:40 +11001641 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (blkno == 0)
1643 continue;
1644 error = xfs_da_read_buf(state->args->trans, state->args->dp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11001645 blkno, -1, &bp, XFS_ATTR_FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (error)
1647 return(error);
1648 ASSERT(bp != NULL);
1649
1650 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001651 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 bytes = state->blocksize - (state->blocksize>>2);
Nathan Scott918ae422006-03-17 17:28:54 +11001653 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001654 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001655 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11001656 count += be16_to_cpu(leaf->hdr.count);
1657 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1659 bytes -= sizeof(xfs_attr_leaf_hdr_t);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001660 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (bytes >= 0)
1662 break; /* fits with at least 25% to spare */
1663 }
1664 if (i >= 2) {
1665 *action = 0;
1666 return(0);
1667 }
1668
1669 /*
1670 * Make altpath point to the block we want to keep (the lower
1671 * numbered block) and path point to the block we want to drop.
1672 */
1673 memcpy(&state->altpath, &state->path, sizeof(state->path));
1674 if (blkno < blk->blkno) {
1675 error = xfs_da_path_shift(state, &state->altpath, forward,
1676 0, &retval);
1677 } else {
1678 error = xfs_da_path_shift(state, &state->path, forward,
1679 0, &retval);
1680 }
1681 if (error)
1682 return(error);
1683 if (retval) {
1684 *action = 0;
1685 } else {
1686 *action = 1;
1687 }
1688 return(0);
1689}
1690
1691/*
1692 * Remove a name from the leaf attribute list structure.
1693 *
1694 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1695 * If two leaves are 37% full, when combined they will leave 25% free.
1696 */
1697int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001698xfs_attr_leaf_remove(
1699 struct xfs_buf *bp,
1700 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 xfs_attr_leafblock_t *leaf;
1703 xfs_attr_leaf_hdr_t *hdr;
1704 xfs_attr_leaf_map_t *map;
1705 xfs_attr_leaf_entry_t *entry;
1706 int before, after, smallest, entsize;
1707 int tablesize, tmp, i;
1708 xfs_mount_t *mp;
1709
Dave Chinneree732592012-11-12 22:53:53 +11001710 trace_xfs_attr_leaf_remove(args);
1711
Dave Chinner1d9025e2012-06-22 18:50:14 +10001712 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001713 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 hdr = &leaf->hdr;
1715 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001716 ASSERT((be16_to_cpu(hdr->count) > 0)
1717 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001719 && (args->index < be16_to_cpu(hdr->count)));
1720 ASSERT(be16_to_cpu(hdr->firstused) >=
1721 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 entry = &leaf->entries[args->index];
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001723 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1724 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /*
1727 * Scan through free region table:
1728 * check for adjacency of free'd entry with an existing one,
1729 * find smallest free region in case we need to replace it,
1730 * adjust any map that borders the entry table,
1731 */
Nathan Scott918ae422006-03-17 17:28:54 +11001732 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 + sizeof(xfs_attr_leaf_hdr_t);
1734 map = &hdr->freemap[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001735 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 before = after = -1;
1737 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1738 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1739 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001740 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1741 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1742 if (be16_to_cpu(map->base) == tablesize) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001743 be16_add_cpu(&map->base,
Nathan Scott918ae422006-03-17 17:28:54 +11001744 -((int)sizeof(xfs_attr_leaf_entry_t)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001745 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747
Nathan Scott918ae422006-03-17 17:28:54 +11001748 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001749 == be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 before = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001751 } else if (be16_to_cpu(map->base)
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001752 == (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 after = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001754 } else if (be16_to_cpu(map->size) < tmp) {
1755 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 smallest = i;
1757 }
1758 }
1759
1760 /*
1761 * Coalesce adjacent freemap regions,
1762 * or replace the smallest region.
1763 */
1764 if ((before >= 0) || (after >= 0)) {
1765 if ((before >= 0) && (after >= 0)) {
1766 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001767 be16_add_cpu(&map->size, entsize);
1768 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001769 be16_to_cpu(hdr->freemap[after].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 hdr->freemap[after].base = 0;
1771 hdr->freemap[after].size = 0;
1772 } else if (before >= 0) {
1773 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001774 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 } else {
1776 map = &hdr->freemap[after];
1777 /* both on-disk, don't endian flip twice */
1778 map->base = entry->nameidx;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001779 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 } else {
1782 /*
1783 * Replace smallest region (if it is smaller than free'd entry)
1784 */
1785 map = &hdr->freemap[smallest];
Nathan Scott918ae422006-03-17 17:28:54 +11001786 if (be16_to_cpu(map->size) < entsize) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001787 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
Nathan Scott918ae422006-03-17 17:28:54 +11001788 map->size = cpu_to_be16(entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 }
1791
1792 /*
1793 * Did we remove the first entry?
1794 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001795 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 smallest = 1;
1797 else
1798 smallest = 0;
1799
1800 /*
1801 * Compress the remaining entries and zero out the removed stuff.
1802 */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001803 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001804 be16_add_cpu(&hdr->usedbytes, -entsize);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001805 xfs_trans_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001806 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 entsize));
1808
Nathan Scott918ae422006-03-17 17:28:54 +11001809 tmp = (be16_to_cpu(hdr->count) - args->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 * sizeof(xfs_attr_leaf_entry_t);
1811 memmove((char *)entry, (char *)(entry+1), tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001812 be16_add_cpu(&hdr->count, -1);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001813 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
Nathan Scott918ae422006-03-17 17:28:54 +11001815 entry = &leaf->entries[be16_to_cpu(hdr->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1817
1818 /*
1819 * If we removed the first entry, re-find the first used byte
1820 * in the name area. Note that if the entry was the "firstused",
1821 * then we don't have a "hole" in our block resulting from
1822 * removing the name.
1823 */
1824 if (smallest) {
1825 tmp = XFS_LBSIZE(mp);
1826 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001827 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001828 ASSERT(be16_to_cpu(entry->nameidx) >=
1829 be16_to_cpu(hdr->firstused));
1830 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1831
1832 if (be16_to_cpu(entry->nameidx) < tmp)
1833 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
Nathan Scott918ae422006-03-17 17:28:54 +11001835 hdr->firstused = cpu_to_be16(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001837 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1839 }
1840 } else {
1841 hdr->holes = 1; /* mark as needing compaction */
1842 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001843 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1845
1846 /*
1847 * Check if leaf is less than 50% full, caller may want to
1848 * "join" the leaf with a sibling if so.
1849 */
1850 tmp = sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001851 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1852 tmp += be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1854}
1855
1856/*
1857 * Move all the attribute list entries from drop_leaf into save_leaf.
1858 */
1859void
1860xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1861 xfs_da_state_blk_t *save_blk)
1862{
1863 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1864 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1865 xfs_mount_t *mp;
1866 char *tmpbuffer;
1867
Dave Chinner5a5881c2012-03-22 05:15:13 +00001868 trace_xfs_attr_leaf_unbalance(state->args);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
1871 * Set up environment.
1872 */
1873 mp = state->mp;
1874 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1875 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001876 drop_leaf = drop_blk->bp->b_addr;
1877 save_leaf = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001878 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1879 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 drop_hdr = &drop_leaf->hdr;
1881 save_hdr = &save_leaf->hdr;
1882
1883 /*
1884 * Save last hashval from dying block for later Btree fixup.
1885 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001886 drop_blk->hashval = be32_to_cpu(
1887 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 /*
1890 * Check if we need a temp buffer, or can we do it in place.
1891 * Note that we don't check "leaf" for holes because we will
1892 * always be dropping it, toosmall() decided that for us already.
1893 */
1894 if (save_hdr->holes == 0) {
1895 /*
1896 * dest leaf has no holes, so we add there. May need
1897 * to make some room in the entry array.
1898 */
1899 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1900 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001901 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 } else {
1903 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001904 be16_to_cpu(save_hdr->count),
1905 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907 } else {
1908 /*
1909 * Destination has holes, so we make a temporary copy
1910 * of the leaf and add them both to that.
1911 */
1912 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1913 ASSERT(tmpbuffer != NULL);
1914 memset(tmpbuffer, 0, state->blocksize);
1915 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1916 tmp_hdr = &tmp_leaf->hdr;
1917 tmp_hdr->info = save_hdr->info; /* struct copy */
1918 tmp_hdr->count = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001919 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (!tmp_hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001921 tmp_hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1923 }
1924 tmp_hdr->usedbytes = 0;
1925 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1926 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001927 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001929 be16_to_cpu(tmp_leaf->hdr.count),
1930 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 } else {
1932 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001933 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001935 be16_to_cpu(tmp_leaf->hdr.count),
1936 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001939 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941
Dave Chinner1d9025e2012-06-22 18:50:14 +10001942 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 state->blocksize - 1);
1944
1945 /*
1946 * Copy out last hashval in each block for B-tree code.
1947 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001948 save_blk->hashval = be32_to_cpu(
1949 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
1952/*========================================================================
1953 * Routines used for finding things in the Btree.
1954 *========================================================================*/
1955
1956/*
1957 * Look up a name in a leaf attribute list structure.
1958 * This is the internal routine, it uses the caller's buffer.
1959 *
1960 * Note that duplicate keys are allowed, but only check within the
1961 * current leaf node. The Btree code must check in adjacent leaf nodes.
1962 *
1963 * Return in args->index the index into the entry[] array of either
1964 * the found entry, or where the entry should have been (insert before
1965 * that entry).
1966 *
1967 * Don't change the args->value unless we find the attribute.
1968 */
1969int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001970xfs_attr_leaf_lookup_int(
1971 struct xfs_buf *bp,
1972 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 xfs_attr_leafblock_t *leaf;
1975 xfs_attr_leaf_entry_t *entry;
1976 xfs_attr_leaf_name_local_t *name_loc;
1977 xfs_attr_leaf_name_remote_t *name_rmt;
1978 int probe, span;
1979 xfs_dahash_t hashval;
1980
Dave Chinner5a5881c2012-03-22 05:15:13 +00001981 trace_xfs_attr_leaf_lookup(args);
1982
Dave Chinner1d9025e2012-06-22 18:50:14 +10001983 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001984 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11001985 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 < (XFS_LBSIZE(args->dp->i_mount)/8));
1987
1988 /*
1989 * Binary search. (note: small blocks will skip this loop)
1990 */
1991 hashval = args->hashval;
Nathan Scott918ae422006-03-17 17:28:54 +11001992 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 for (entry = &leaf->entries[probe]; span > 4;
1994 entry = &leaf->entries[probe]) {
1995 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001996 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001998 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 probe -= span;
2000 else
2001 break;
2002 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002003 ASSERT((probe >= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 (!leaf->hdr.count
Nathan Scott918ae422006-03-17 17:28:54 +11002005 || (probe < be16_to_cpu(leaf->hdr.count))));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002006 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 /*
2009 * Since we may have duplicate hashval's, find the first matching
2010 * hashval in the leaf.
2011 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002012 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 entry--;
2014 probe--;
2015 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002016 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
2017 (be32_to_cpu(entry->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 entry++;
2019 probe++;
2020 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002021 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
2022 (be32_to_cpu(entry->hashval) != hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 args->index = probe;
2024 return(XFS_ERROR(ENOATTR));
2025 }
2026
2027 /*
2028 * Duplicate keys may be present, so search all of them for a match.
2029 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002030 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
2031 (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 entry++, probe++) {
2033/*
2034 * GROT: Add code to remove incomplete entries.
2035 */
2036 /*
2037 * If we are looking for INCOMPLETE entries, show only those.
2038 * If we are looking for complete entries, show only those.
2039 */
2040 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2041 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2042 continue;
2043 }
2044 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002045 name_loc = xfs_attr_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (name_loc->namelen != args->namelen)
2047 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002048 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002050 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 continue;
2052 args->index = probe;
2053 return(XFS_ERROR(EEXIST));
2054 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002055 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (name_rmt->namelen != args->namelen)
2057 continue;
2058 if (memcmp(args->name, (char *)name_rmt->name,
2059 args->namelen) != 0)
2060 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002061 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 continue;
2063 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002064 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002066 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return(XFS_ERROR(EEXIST));
2068 }
2069 }
2070 args->index = probe;
2071 return(XFS_ERROR(ENOATTR));
2072}
2073
2074/*
2075 * Get the value associated with an attribute name from a leaf attribute
2076 * list structure.
2077 */
2078int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002079xfs_attr_leaf_getvalue(
2080 struct xfs_buf *bp,
2081 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 int valuelen;
2084 xfs_attr_leafblock_t *leaf;
2085 xfs_attr_leaf_entry_t *entry;
2086 xfs_attr_leaf_name_local_t *name_loc;
2087 xfs_attr_leaf_name_remote_t *name_rmt;
2088
Dave Chinner1d9025e2012-06-22 18:50:14 +10002089 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002090 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002091 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 < (XFS_LBSIZE(args->dp->i_mount)/8));
Nathan Scott918ae422006-03-17 17:28:54 +11002093 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 entry = &leaf->entries[args->index];
2096 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002097 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 ASSERT(name_loc->namelen == args->namelen);
2099 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002100 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (args->flags & ATTR_KERNOVAL) {
2102 args->valuelen = valuelen;
2103 return(0);
2104 }
2105 if (args->valuelen < valuelen) {
2106 args->valuelen = valuelen;
2107 return(XFS_ERROR(ERANGE));
2108 }
2109 args->valuelen = valuelen;
2110 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2111 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002112 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 ASSERT(name_rmt->namelen == args->namelen);
2114 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002115 valuelen = be32_to_cpu(name_rmt->valuelen);
2116 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2118 if (args->flags & ATTR_KERNOVAL) {
2119 args->valuelen = valuelen;
2120 return(0);
2121 }
2122 if (args->valuelen < valuelen) {
2123 args->valuelen = valuelen;
2124 return(XFS_ERROR(ERANGE));
2125 }
2126 args->valuelen = valuelen;
2127 }
2128 return(0);
2129}
2130
2131/*========================================================================
2132 * Utility routines.
2133 *========================================================================*/
2134
2135/*
2136 * Move the indicated entries from one leaf to another.
2137 * NOTE: this routine modifies both source and destination leaves.
2138 */
2139/*ARGSUSED*/
2140STATIC void
2141xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2142 xfs_attr_leafblock_t *leaf_d, int start_d,
2143 int count, xfs_mount_t *mp)
2144{
2145 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2146 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2147 int desti, tmp, i;
2148
2149 /*
2150 * Check for nothing to do.
2151 */
2152 if (count == 0)
2153 return;
2154
2155 /*
2156 * Set up environment.
2157 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002158 ASSERT(leaf_s->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2159 ASSERT(leaf_d->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 hdr_s = &leaf_s->hdr;
2161 hdr_d = &leaf_d->hdr;
Nathan Scott918ae422006-03-17 17:28:54 +11002162 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2163 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2164 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2165 ((be16_to_cpu(hdr_s->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 * sizeof(*entry_s))+sizeof(*hdr_s)));
Nathan Scott918ae422006-03-17 17:28:54 +11002167 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2168 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2169 ((be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 * sizeof(*entry_d))+sizeof(*hdr_d)));
2171
Nathan Scott918ae422006-03-17 17:28:54 +11002172 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2173 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2174 ASSERT(count <= be16_to_cpu(hdr_s->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 /*
2177 * Move the entries in the destination leaf up to make a hole?
2178 */
Nathan Scott918ae422006-03-17 17:28:54 +11002179 if (start_d < be16_to_cpu(hdr_d->count)) {
2180 tmp = be16_to_cpu(hdr_d->count) - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 tmp *= sizeof(xfs_attr_leaf_entry_t);
2182 entry_s = &leaf_d->entries[start_d];
2183 entry_d = &leaf_d->entries[start_d + count];
2184 memmove((char *)entry_d, (char *)entry_s, tmp);
2185 }
2186
2187 /*
2188 * Copy all entry's in the same (sorted) order,
2189 * but allocate attribute info packed and in sequence.
2190 */
2191 entry_s = &leaf_s->entries[start_s];
2192 entry_d = &leaf_d->entries[start_d];
2193 desti = start_d;
2194 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002195 ASSERT(be16_to_cpu(entry_s->nameidx)
Nathan Scott918ae422006-03-17 17:28:54 +11002196 >= be16_to_cpu(hdr_s->firstused));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2198#ifdef GROT
2199 /*
2200 * Code to drop INCOMPLETE entries. Difficult to use as we
2201 * may also need to change the insertion index. Code turned
2202 * off for 6.2, should be revisited later.
2203 */
2204 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002205 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002206 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2207 be16_add_cpu(&hdr_s->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 entry_d--; /* to compensate for ++ in loop hdr */
2209 desti--;
2210 if ((start_s + i) < offset)
2211 result++; /* insertion index adjustment */
2212 } else {
2213#endif /* GROT */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002214 be16_add_cpu(&hdr_d->firstused, -tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* both on-disk, don't endian flip twice */
2216 entry_d->hashval = entry_s->hashval;
2217 /* both on-disk, don't endian flip twice */
2218 entry_d->nameidx = hdr_d->firstused;
2219 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002220 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002222 memmove(xfs_attr_leaf_name(leaf_d, desti),
2223 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002224 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002226 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002227 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2228 be16_add_cpu(&hdr_d->usedbytes, tmp);
2229 be16_add_cpu(&hdr_s->count, -1);
2230 be16_add_cpu(&hdr_d->count, 1);
Nathan Scott918ae422006-03-17 17:28:54 +11002231 tmp = be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 * sizeof(xfs_attr_leaf_entry_t)
2233 + sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002234 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235#ifdef GROT
2236 }
2237#endif /* GROT */
2238 }
2239
2240 /*
2241 * Zero out the entries we just copied.
2242 */
Nathan Scott918ae422006-03-17 17:28:54 +11002243 if (start_s == be16_to_cpu(hdr_s->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2245 entry_s = &leaf_s->entries[start_s];
2246 ASSERT(((char *)entry_s + tmp) <=
2247 ((char *)leaf_s + XFS_LBSIZE(mp)));
2248 memset((char *)entry_s, 0, tmp);
2249 } else {
2250 /*
2251 * Move the remaining entries down to fill the hole,
2252 * then zero the entries at the top.
2253 */
Nathan Scott918ae422006-03-17 17:28:54 +11002254 tmp = be16_to_cpu(hdr_s->count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 tmp *= sizeof(xfs_attr_leaf_entry_t);
2256 entry_s = &leaf_s->entries[start_s + count];
2257 entry_d = &leaf_s->entries[start_s];
2258 memmove((char *)entry_d, (char *)entry_s, tmp);
2259
2260 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002261 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 ASSERT(((char *)entry_s + tmp) <=
2263 ((char *)leaf_s + XFS_LBSIZE(mp)));
2264 memset((char *)entry_s, 0, tmp);
2265 }
2266
2267 /*
2268 * Fill in the freemap information
2269 */
Nathan Scott918ae422006-03-17 17:28:54 +11002270 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002271 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
Nathan Scott918ae422006-03-17 17:28:54 +11002272 sizeof(xfs_attr_leaf_entry_t));
2273 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2274 - be16_to_cpu(hdr_d->freemap[0].base));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 hdr_d->freemap[1].base = 0;
2276 hdr_d->freemap[2].base = 0;
2277 hdr_d->freemap[1].size = 0;
2278 hdr_d->freemap[2].size = 0;
2279 hdr_s->holes = 1; /* leaf may not be compact */
2280}
2281
2282/*
2283 * Compare two leaf blocks "order".
2284 * Return 0 unless leaf2 should go before leaf1.
2285 */
2286int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002287xfs_attr_leaf_order(
2288 struct xfs_buf *leaf1_bp,
2289 struct xfs_buf *leaf2_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 xfs_attr_leafblock_t *leaf1, *leaf2;
2292
Dave Chinner1d9025e2012-06-22 18:50:14 +10002293 leaf1 = leaf1_bp->b_addr;
2294 leaf2 = leaf2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002295 ASSERT((leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) &&
2296 (leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)));
Nathan Scott918ae422006-03-17 17:28:54 +11002297 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2298 (be16_to_cpu(leaf2->hdr.count) > 0) &&
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002299 ((be32_to_cpu(leaf2->entries[0].hashval) <
2300 be32_to_cpu(leaf1->entries[0].hashval)) ||
2301 (be32_to_cpu(leaf2->entries[
2302 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2303 be32_to_cpu(leaf1->entries[
2304 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return(1);
2306 }
2307 return(0);
2308}
2309
2310/*
2311 * Pick up the last hashvalue from a leaf block.
2312 */
2313xfs_dahash_t
Dave Chinner1d9025e2012-06-22 18:50:14 +10002314xfs_attr_leaf_lasthash(
2315 struct xfs_buf *bp,
2316 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 xfs_attr_leafblock_t *leaf;
2319
Dave Chinner1d9025e2012-06-22 18:50:14 +10002320 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002321 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (count)
Nathan Scott918ae422006-03-17 17:28:54 +11002323 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (!leaf->hdr.count)
2325 return(0);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002326 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
2329/*
2330 * Calculate the number of bytes used to store the indicated attribute
2331 * (whether local or remote only calculate bytes in this block).
2332 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002333STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2335{
2336 xfs_attr_leaf_name_local_t *name_loc;
2337 xfs_attr_leaf_name_remote_t *name_rmt;
2338 int size;
2339
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002340 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002342 name_loc = xfs_attr_leaf_name_local(leaf, index);
2343 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002344 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002346 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2347 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349 return(size);
2350}
2351
2352/*
2353 * Calculate the number of bytes that would be required to store the new
2354 * attribute (whether local or remote only calculate bytes in this block).
2355 * This routine decides as a side effect whether the attribute will be
2356 * a "local" or a "remote" attribute.
2357 */
2358int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002359xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 int size;
2362
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002363 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2364 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (local) {
2366 *local = 1;
2367 }
2368 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002369 size = xfs_attr_leaf_entsize_remote(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (local) {
2371 *local = 0;
2372 }
2373 }
2374 return(size);
2375}
2376
2377/*
2378 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2379 */
2380int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002381xfs_attr_leaf_list_int(
2382 struct xfs_buf *bp,
2383 xfs_attr_list_context_t *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
2385 attrlist_cursor_kern_t *cursor;
2386 xfs_attr_leafblock_t *leaf;
2387 xfs_attr_leaf_entry_t *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 int retval, i;
2389
2390 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002391 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 cursor = context->cursor;
2393 cursor->initted = 1;
2394
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002395 trace_xfs_attr_list_leaf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 /*
2398 * Re-find our place in the leaf block if this is a new syscall.
2399 */
2400 if (context->resynch) {
2401 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002402 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002403 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (cursor->offset == context->dupcnt) {
2405 context->dupcnt = 0;
2406 break;
2407 }
2408 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002409 } else if (be32_to_cpu(entry->hashval) >
2410 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 context->dupcnt = 0;
2412 break;
2413 }
2414 }
Nathan Scott918ae422006-03-17 17:28:54 +11002415 if (i == be16_to_cpu(leaf->hdr.count)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002416 trace_xfs_attr_list_notfound(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return(0);
2418 }
2419 } else {
2420 entry = &leaf->entries[0];
2421 i = 0;
2422 }
2423 context->resynch = 0;
2424
2425 /*
2426 * We have found our place, start copying out the new attributes.
2427 */
2428 retval = 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10002429 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002430 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2431 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 cursor->offset = 0;
2433 }
2434
2435 if (entry->flags & XFS_ATTR_INCOMPLETE)
2436 continue; /* skip incomplete entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002439 xfs_attr_leaf_name_local_t *name_loc =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002440 xfs_attr_leaf_name_local(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002441
2442 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002443 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002444 name_loc->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +10002445 (int)name_loc->namelen,
2446 be16_to_cpu(name_loc->valuelen),
Dave Chinnera9273ca2010-01-20 10:47:48 +11002447 &name_loc->nameval[name_loc->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +10002448 if (retval)
2449 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002451 xfs_attr_leaf_name_remote_t *name_rmt =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002452 xfs_attr_leaf_name_remote(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002453
2454 int valuelen = be32_to_cpu(name_rmt->valuelen);
2455
2456 if (context->put_value) {
2457 xfs_da_args_t args;
2458
2459 memset((char *)&args, 0, sizeof(args));
2460 args.dp = context->dp;
2461 args.whichfork = XFS_ATTR_FORK;
2462 args.valuelen = valuelen;
Dave Chinner622d8142010-12-23 11:57:37 +11002463 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
Tim Shimmin726801b2006-09-28 11:01:37 +10002464 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2465 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2466 retval = xfs_attr_rmtval_get(&args);
2467 if (retval)
2468 return retval;
2469 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002470 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002471 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002472 (int)name_rmt->namelen,
2473 valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002474 args.value);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002475 kmem_free(args.value);
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002476 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002477 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002478 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002479 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002480 (int)name_rmt->namelen,
2481 valuelen,
2482 NULL);
2483 }
2484 if (retval)
2485 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002487 if (context->seen_enough)
2488 break;
2489 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002491 trace_xfs_attr_list_leaf_end(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return(retval);
2493}
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496/*========================================================================
2497 * Manage the INCOMPLETE flag in a leaf entry
2498 *========================================================================*/
2499
2500/*
2501 * Clear the INCOMPLETE flag on an entry in a leaf block.
2502 */
2503int
2504xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2505{
2506 xfs_attr_leafblock_t *leaf;
2507 xfs_attr_leaf_entry_t *entry;
2508 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002509 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 int error;
2511#ifdef DEBUG
2512 xfs_attr_leaf_name_local_t *name_loc;
2513 int namelen;
2514 char *name;
2515#endif /* DEBUG */
2516
Dave Chinner5a5881c2012-03-22 05:15:13 +00002517 trace_xfs_attr_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 /*
2519 * Set up the operation.
2520 */
2521 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002522 XFS_ATTR_FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (error) {
2524 return(error);
2525 }
2526 ASSERT(bp != NULL);
2527
Dave Chinner1d9025e2012-06-22 18:50:14 +10002528 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002529 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002530 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 ASSERT(args->index >= 0);
2532 entry = &leaf->entries[ args->index ];
2533 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2534
2535#ifdef DEBUG
2536 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002537 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 namelen = name_loc->namelen;
2539 name = (char *)name_loc->nameval;
2540 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002541 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 namelen = name_rmt->namelen;
2543 name = (char *)name_rmt->name;
2544 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002545 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 ASSERT(namelen == args->namelen);
2547 ASSERT(memcmp(name, args->name, namelen) == 0);
2548#endif /* DEBUG */
2549
2550 entry->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002551 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2553
2554 if (args->rmtblkno) {
2555 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002556 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002557 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2558 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002559 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
2563 /*
2564 * Commit the flag value change and start the next trans in series.
2565 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002566 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
2569/*
2570 * Set the INCOMPLETE flag on an entry in a leaf block.
2571 */
2572int
2573xfs_attr_leaf_setflag(xfs_da_args_t *args)
2574{
2575 xfs_attr_leafblock_t *leaf;
2576 xfs_attr_leaf_entry_t *entry;
2577 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002578 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 int error;
2580
Dave Chinner5a5881c2012-03-22 05:15:13 +00002581 trace_xfs_attr_leaf_setflag(args);
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 /*
2584 * Set up the operation.
2585 */
2586 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002587 XFS_ATTR_FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if (error) {
2589 return(error);
2590 }
2591 ASSERT(bp != NULL);
2592
Dave Chinner1d9025e2012-06-22 18:50:14 +10002593 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002594 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002595 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 ASSERT(args->index >= 0);
2597 entry = &leaf->entries[ args->index ];
2598
2599 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2600 entry->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002601 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2603 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002604 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 name_rmt->valueblk = 0;
2606 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002607 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
2611 /*
2612 * Commit the flag value change and start the next trans in series.
2613 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002614 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
2617/*
2618 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2619 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2620 * entry given by args->blkno2/index2.
2621 *
2622 * Note that they could be in different blocks, or in the same block.
2623 */
2624int
2625xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2626{
2627 xfs_attr_leafblock_t *leaf1, *leaf2;
2628 xfs_attr_leaf_entry_t *entry1, *entry2;
2629 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002630 struct xfs_buf *bp1, *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 int error;
2632#ifdef DEBUG
2633 xfs_attr_leaf_name_local_t *name_loc;
2634 int namelen1, namelen2;
2635 char *name1, *name2;
2636#endif /* DEBUG */
2637
Dave Chinner5a5881c2012-03-22 05:15:13 +00002638 trace_xfs_attr_leaf_flipflags(args);
2639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 /*
2641 * Read the block containing the "old" attr
2642 */
2643 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002644 XFS_ATTR_FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (error) {
2646 return(error);
2647 }
2648 ASSERT(bp1 != NULL);
2649
2650 /*
2651 * Read the block containing the "new" attr, if it is different
2652 */
2653 if (args->blkno2 != args->blkno) {
2654 error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002655 -1, &bp2, XFS_ATTR_FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (error) {
2657 return(error);
2658 }
2659 ASSERT(bp2 != NULL);
2660 } else {
2661 bp2 = bp1;
2662 }
2663
Dave Chinner1d9025e2012-06-22 18:50:14 +10002664 leaf1 = bp1->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002665 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002666 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 ASSERT(args->index >= 0);
2668 entry1 = &leaf1->entries[ args->index ];
2669
Dave Chinner1d9025e2012-06-22 18:50:14 +10002670 leaf2 = bp2->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002671 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002672 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 ASSERT(args->index2 >= 0);
2674 entry2 = &leaf2->entries[ args->index2 ];
2675
2676#ifdef DEBUG
2677 if (entry1->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002678 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 namelen1 = name_loc->namelen;
2680 name1 = (char *)name_loc->nameval;
2681 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002682 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 namelen1 = name_rmt->namelen;
2684 name1 = (char *)name_rmt->name;
2685 }
2686 if (entry2->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002687 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 namelen2 = name_loc->namelen;
2689 name2 = (char *)name_loc->nameval;
2690 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002691 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 namelen2 = name_rmt->namelen;
2693 name2 = (char *)name_rmt->name;
2694 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002695 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 ASSERT(namelen1 == namelen2);
2697 ASSERT(memcmp(name1, name2, namelen1) == 0);
2698#endif /* DEBUG */
2699
2700 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2701 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2702
2703 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002704 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2706 if (args->rmtblkno) {
2707 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002708 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002709 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2710 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002711 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2713 }
2714
2715 entry2->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002716 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2718 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
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 name_rmt->valueblk = 0;
2721 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002722 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 /*
2727 * Commit the flag value change and start the next trans in series.
2728 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002729 error = xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 return(error);
2732}
2733
2734/*========================================================================
2735 * Indiscriminately delete the entire attribute fork
2736 *========================================================================*/
2737
2738/*
2739 * Recurse (gasp!) through the attribute nodes until we find leaves.
2740 * We're doing a depth-first traversal in order to invalidate everything.
2741 */
2742int
2743xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2744{
2745 xfs_da_blkinfo_t *info;
2746 xfs_daddr_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002747 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 int error;
2749
2750 /*
2751 * Read block 0 to see what we have to work with.
2752 * We only get here if we have extents, since we remove
2753 * the extents in reverse order the extent containing
2754 * block 0 must still be there.
2755 */
Dave Chinner4bb20a82012-11-12 22:54:10 +11002756 error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (error)
2758 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002759 blkno = XFS_BUF_ADDR(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 /*
2762 * Invalidate the tree, even if the "tree" is only a single leaf block.
2763 * This is a depth-first traversal!
2764 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002765 info = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002766 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 error = xfs_attr_node_inactive(trans, dp, bp, 1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002768 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 error = xfs_attr_leaf_inactive(trans, dp, bp);
2770 } else {
2771 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002772 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
2774 if (error)
2775 return(error);
2776
2777 /*
2778 * Invalidate the incore copy of the root block.
2779 */
2780 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2781 if (error)
2782 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002783 xfs_trans_binval(*trans, bp); /* remove from cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 /*
2785 * Commit the invalidate and start the next transaction.
2786 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002787 error = xfs_trans_roll(trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 return (error);
2790}
2791
2792/*
2793 * Recurse (gasp!) through the attribute nodes until we find leaves.
2794 * We're doing a depth-first traversal in order to invalidate everything.
2795 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002796STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002797xfs_attr_node_inactive(
2798 struct xfs_trans **trans,
2799 struct xfs_inode *dp,
2800 struct xfs_buf *bp,
2801 int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
2803 xfs_da_blkinfo_t *info;
2804 xfs_da_intnode_t *node;
2805 xfs_dablk_t child_fsb;
2806 xfs_daddr_t parent_blkno, child_blkno;
2807 int error, count, i;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002808 struct xfs_buf *child_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 /*
2811 * Since this code is recursive (gasp!) we must protect ourselves.
2812 */
2813 if (level > XFS_DA_NODE_MAXDEPTH) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002814 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return(XFS_ERROR(EIO));
2816 }
2817
Dave Chinner1d9025e2012-06-22 18:50:14 +10002818 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002819 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +10002820 parent_blkno = XFS_BUF_ADDR(bp); /* save for re-read later */
Nathan Scottfac80cc2006-03-17 17:29:56 +11002821 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (!count) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002823 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return(0);
2825 }
Nathan Scott403432d2006-03-17 17:29:46 +11002826 child_fsb = be32_to_cpu(node->btree[0].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002827 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 /*
2830 * If this is the node level just above the leaves, simply loop
2831 * over the leaves removing all of them. If this is higher up
2832 * in the tree, recurse downward.
2833 */
2834 for (i = 0; i < count; i++) {
2835 /*
2836 * Read the subsidiary block to see what we have to work with.
2837 * Don't do this in a transaction. This is a depth-first
2838 * traversal of the tree so we may deal with many blocks
2839 * before we come back to this one.
2840 */
2841 error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002842 XFS_ATTR_FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (error)
2844 return(error);
2845 if (child_bp) {
2846 /* save for re-read later */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002847 child_blkno = XFS_BUF_ADDR(child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 /*
2850 * Invalidate the subtree, however we have to.
2851 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002852 info = child_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002853 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 error = xfs_attr_node_inactive(trans, dp,
2855 child_bp, level+1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002856 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 error = xfs_attr_leaf_inactive(trans, dp,
2858 child_bp);
2859 } else {
2860 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002861 xfs_trans_brelse(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
2863 if (error)
2864 return(error);
2865
2866 /*
2867 * Remove the subsidiary block from the cache
2868 * and from the log.
2869 */
2870 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2871 &child_bp, XFS_ATTR_FORK);
2872 if (error)
2873 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002874 xfs_trans_binval(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
2876
2877 /*
2878 * If we're not done, re-read the parent to get the next
2879 * child block number.
2880 */
2881 if ((i+1) < count) {
2882 error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002883 &bp, XFS_ATTR_FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (error)
2885 return(error);
Nathan Scott403432d2006-03-17 17:29:46 +11002886 child_fsb = be32_to_cpu(node->btree[i+1].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002887 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
2889 /*
2890 * Atomically commit the whole invalidate stuff.
2891 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002892 error = xfs_trans_roll(trans, dp);
2893 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return (error);
2895 }
2896
2897 return(0);
2898}
2899
2900/*
2901 * Invalidate all of the "remote" value regions pointed to by a particular
2902 * leaf block.
2903 * Note that we must release the lock on the buffer so that we are not
2904 * caught holding something that the logging code wants to flush to disk.
2905 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002906STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002907xfs_attr_leaf_inactive(
2908 struct xfs_trans **trans,
2909 struct xfs_inode *dp,
2910 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 xfs_attr_leafblock_t *leaf;
2913 xfs_attr_leaf_entry_t *entry;
2914 xfs_attr_leaf_name_remote_t *name_rmt;
2915 xfs_attr_inactive_list_t *list, *lp;
2916 int error, count, size, tmp, i;
2917
Dave Chinner1d9025e2012-06-22 18:50:14 +10002918 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002919 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921 /*
2922 * Count the number of "remote" value extents.
2923 */
2924 count = 0;
2925 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002926 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002927 if (be16_to_cpu(entry->nameidx) &&
2928 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002929 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (name_rmt->valueblk)
2931 count++;
2932 }
2933 }
2934
2935 /*
2936 * If there are no "remote" values, we're done.
2937 */
2938 if (count == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002939 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return(0);
2941 }
2942
2943 /*
2944 * Allocate storage for a list of all the "remote" value extents.
2945 */
2946 size = count * sizeof(xfs_attr_inactive_list_t);
2947 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2948
2949 /*
2950 * Identify each of the "remote" value extents.
2951 */
2952 lp = list;
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) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11002959 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2960 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2961 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 lp++;
2963 }
2964 }
2965 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002966 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 /*
2969 * Invalidate each of the "remote" value extents.
2970 */
2971 error = 0;
2972 for (lp = list, i = 0; i < count; i++, lp++) {
2973 tmp = xfs_attr_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11002974 lp->valueblk, lp->valuelen);
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (error == 0)
2977 error = tmp; /* save only the 1st errno */
2978 }
2979
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002980 kmem_free((xfs_caddr_t)list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return(error);
2982}
2983
2984/*
2985 * Look at all the extents for this logical region,
2986 * invalidate any buffers that are incore/in transactions.
2987 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002988STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
2990 xfs_dablk_t blkno, int blkcnt)
2991{
2992 xfs_bmbt_irec_t map;
2993 xfs_dablk_t tblkno;
2994 int tblkcnt, dblkcnt, nmap, error;
2995 xfs_daddr_t dblkno;
2996 xfs_buf_t *bp;
2997
2998 /*
2999 * Roll through the "value", invalidating the attribute value's
3000 * blocks.
3001 */
3002 tblkno = blkno;
3003 tblkcnt = blkcnt;
3004 while (tblkcnt > 0) {
3005 /*
3006 * Try to remember where we decided to put the value.
3007 */
3008 nmap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003009 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
3010 &map, &nmap, XFS_BMAPI_ATTRFORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (error) {
3012 return(error);
3013 }
3014 ASSERT(nmap == 1);
3015 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3016
3017 /*
3018 * If it's a hole, these are already unmapped
3019 * so there's nothing to invalidate.
3020 */
3021 if (map.br_startblock != HOLESTARTBLOCK) {
3022
3023 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3024 map.br_startblock);
3025 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3026 map.br_blockcount);
3027 bp = xfs_trans_get_buf(*trans,
3028 dp->i_mount->m_ddev_targp,
Dave Chinnera8acad72012-04-23 15:58:54 +10003029 dblkno, dblkcnt, 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00003030 if (!bp)
3031 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 xfs_trans_binval(*trans, bp);
3033 /*
3034 * Roll to next transaction.
3035 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003036 error = xfs_trans_roll(trans, dp);
3037 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return (error);
3039 }
3040
3041 tblkno += map.br_blockcount;
3042 tblkcnt -= map.br_blockcount;
3043 }
3044
3045 return(0);
3046}