blob: efe170da288186c8b0fc9cb074fedf735f75284d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_ialloc_btree.h"
31#include "xfs_alloc.h"
32#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_attr.h"
39#include "xfs_attr_leaf.h"
40#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000041#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * xfs_attr_leaf.c
45 *
46 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
47 */
48
49/*========================================================================
50 * Function prototypes for the kernel.
51 *========================================================================*/
52
53/*
54 * Routines used for growing the Btree.
55 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100056STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
Dave Chinner1d9025e2012-06-22 18:50:14 +100057 struct xfs_buf **bpp);
58STATIC int xfs_attr_leaf_add_work(struct xfs_buf *leaf_buffer,
59 xfs_da_args_t *args, int freemap_index);
Dave Chinneree732592012-11-12 22:53:53 +110060STATIC void xfs_attr_leaf_compact(struct xfs_da_args *args,
61 struct xfs_buf *leaf_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
63 xfs_da_state_blk_t *blk1,
64 xfs_da_state_blk_t *blk2);
65STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
66 xfs_da_state_blk_t *leaf_blk_1,
67 xfs_da_state_blk_t *leaf_blk_2,
68 int *number_entries_in_blk1,
69 int *number_usedbytes_in_blk1);
70
71/*
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100072 * Routines used for shrinking the Btree.
73 */
74STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +100075 struct xfs_buf *bp, int level);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100076STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +100077 struct xfs_buf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100078STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
79 xfs_dablk_t blkno, int blkcnt);
80
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Utility routines.
83 */
84STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
85 int src_start,
86 xfs_attr_leafblock_t *dst_leaf,
87 int dst_start, int move_count,
88 xfs_mount_t *mp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100089STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
Tim Shimmin726801b2006-09-28 11:01:37 +100090
Dave Chinnerd9392a42012-11-12 22:54:17 +110091void
Dave Chinnerad14c332012-11-12 22:54:16 +110092xfs_attr_leaf_verify(
93 struct xfs_buf *bp)
94{
95 struct xfs_mount *mp = bp->b_target->bt_mount;
96 struct xfs_attr_leaf_hdr *hdr = bp->b_addr;
97 int block_ok = 0;
98
99 block_ok = hdr->info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
100 if (!block_ok) {
101 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
102 xfs_buf_ioerror(bp, EFSCORRUPTED);
103 }
104
105 bp->b_iodone = NULL;
106 xfs_buf_ioend(bp, 0);
107}
108
109int
110xfs_attr_leaf_read(
111 struct xfs_trans *tp,
112 struct xfs_inode *dp,
113 xfs_dablk_t bno,
114 xfs_daddr_t mappedbno,
115 struct xfs_buf **bpp)
116{
117 return xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
118 XFS_ATTR_FORK, xfs_attr_leaf_verify);
119}
120
Tim Shimmin726801b2006-09-28 11:01:37 +1000121/*========================================================================
122 * Namespace helper routines
123 *========================================================================*/
124
Tim Shimmin726801b2006-09-28 11:01:37 +1000125/*
126 * If namespace bits don't match return 0.
127 * If all match then return 1.
128 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000129STATIC int
Tim Shimmin726801b2006-09-28 11:01:37 +1000130xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
131{
132 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100137 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *========================================================================*/
139
140/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100141 * Query whether the requested number of additional bytes of extended
142 * attribute space will be able to fit inline.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000143 *
Nathan Scottd8cc8902005-11-02 10:34:53 +1100144 * Returns zero if not, else the di_forkoff fork offset to be used in the
145 * literal area for attribute data once the new bytes have been added.
146 *
147 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
148 * special case for dev/uuid inodes, they have fixed size data forks.
149 */
150int
151xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
152{
153 int offset;
154 int minforkoff; /* lower limit on valid forkoff locations */
155 int maxforkoff; /* upper limit on valid forkoff locations */
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000156 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100157 xfs_mount_t *mp = dp->i_mount;
158
Nathan Scottd8cc8902005-11-02 10:34:53 +1100159 offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
160
161 switch (dp->i_d.di_format) {
162 case XFS_DINODE_FMT_DEV:
163 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
164 return (offset >= minforkoff) ? minforkoff : 0;
165 case XFS_DINODE_FMT_UUID:
166 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
167 return (offset >= minforkoff) ? minforkoff : 0;
168 }
169
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000170 /*
171 * If the requested numbers of bytes is smaller or equal to the
172 * current attribute fork size we can always proceed.
173 *
174 * Note that if_bytes in the data fork might actually be larger than
175 * the current data fork size is due to delalloc extents. In that
176 * case either the extent count will go down when they are converted
177 * to real extents, or the delalloc conversion will take care of the
178 * literal area rebalancing.
179 */
180 if (bytes <= XFS_IFORK_ASIZE(dp))
181 return dp->i_d.di_forkoff;
182
183 /*
184 * For attr2 we can try to move the forkoff if there is space in the
185 * literal area, but for the old format we are done if there is no
186 * space in the fixed attribute fork.
187 */
188 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
Nathan Scottda087ba2005-11-02 15:00:20 +1100189 return 0;
Nathan Scottda087ba2005-11-02 15:00:20 +1100190
Barry Naujoke5889e92007-02-10 18:35:58 +1100191 dsize = dp->i_df.if_bytes;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000192
Barry Naujoke5889e92007-02-10 18:35:58 +1100193 switch (dp->i_d.di_format) {
194 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000195 /*
Barry Naujoke5889e92007-02-10 18:35:58 +1100196 * If there is no attr fork and the data fork is extents,
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000197 * determine if creating the default attr fork will result
198 * in the extents form migrating to btree. If so, the
199 * minimum offset only needs to be the space required for
Barry Naujoke5889e92007-02-10 18:35:58 +1100200 * the btree root.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000201 */
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200202 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
203 xfs_default_attroffset(dp))
Barry Naujoke5889e92007-02-10 18:35:58 +1100204 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
205 break;
Barry Naujoke5889e92007-02-10 18:35:58 +1100206 case XFS_DINODE_FMT_BTREE:
207 /*
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000208 * If we have a data btree then keep forkoff if we have one,
209 * otherwise we are adding a new attr, so then we set
210 * minforkoff to where the btree root can finish so we have
Barry Naujoke5889e92007-02-10 18:35:58 +1100211 * plenty of room for attrs
212 */
213 if (dp->i_d.di_forkoff) {
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000214 if (offset < dp->i_d.di_forkoff)
Barry Naujoke5889e92007-02-10 18:35:58 +1100215 return 0;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000216 return dp->i_d.di_forkoff;
217 }
218 dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
Barry Naujoke5889e92007-02-10 18:35:58 +1100219 break;
220 }
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000221
222 /*
223 * A data fork btree root must have space for at least
Barry Naujoke5889e92007-02-10 18:35:58 +1100224 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
225 */
226 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100227 minforkoff = roundup(minforkoff, 8) >> 3;
228
229 /* attr fork btree root can have at least this many key/ptr pairs */
230 maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
231 maxforkoff = maxforkoff >> 3; /* rounded down */
232
Nathan Scottd8cc8902005-11-02 10:34:53 +1100233 if (offset >= maxforkoff)
234 return maxforkoff;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000235 if (offset >= minforkoff)
236 return offset;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100237 return 0;
238}
239
240/*
241 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
242 */
243STATIC void
244xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
245{
Nathan Scott13059ff2006-01-11 15:32:01 +1100246 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100247 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000248 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100249 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
250 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000251 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100252 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
253 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000254 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100255 }
256}
257
258/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * Create the initial contents of a shortform attribute list.
260 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100261void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262xfs_attr_shortform_create(xfs_da_args_t *args)
263{
264 xfs_attr_sf_hdr_t *hdr;
265 xfs_inode_t *dp;
266 xfs_ifork_t *ifp;
267
Dave Chinner5a5881c2012-03-22 05:15:13 +0000268 trace_xfs_attr_sf_create(args);
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 dp = args->dp;
271 ASSERT(dp != NULL);
272 ifp = dp->i_afp;
273 ASSERT(ifp != NULL);
274 ASSERT(ifp->if_bytes == 0);
275 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
276 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
277 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
278 ifp->if_flags |= XFS_IFINLINE;
279 } else {
280 ASSERT(ifp->if_flags & XFS_IFINLINE);
281 }
282 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
283 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
284 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100285 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * Add a name/value pair to the shortform attribute list.
291 * Overflow from the inode has already been checked for.
292 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100293void
294xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 xfs_attr_shortform_t *sf;
297 xfs_attr_sf_entry_t *sfe;
298 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100299 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 xfs_inode_t *dp;
301 xfs_ifork_t *ifp;
302
Dave Chinner5a5881c2012-03-22 05:15:13 +0000303 trace_xfs_attr_sf_add(args);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100306 mp = dp->i_mount;
307 dp->i_d.di_forkoff = forkoff;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 ifp = dp->i_afp;
310 ASSERT(ifp->if_flags & XFS_IFINLINE);
311 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
312 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100313 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100314#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (sfe->namelen != args->namelen)
316 continue;
317 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
318 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000319 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100321 ASSERT(0);
322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
325 offset = (char *)sfe - (char *)sf;
326 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
327 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
328 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
329 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
330
331 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100332 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000333 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 memcpy(sfe->nameval, args->name, args->namelen);
335 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100336 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800337 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
339
Nathan Scottd8cc8902005-11-02 10:34:53 +1100340 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
Christoph Hellwige1486de2009-02-04 09:36:00 +0100344 * After the last attribute is removed revert to original inode format,
345 * making all literal area available to the data fork once more.
346 */
347STATIC void
348xfs_attr_fork_reset(
349 struct xfs_inode *ip,
350 struct xfs_trans *tp)
351{
352 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
353 ip->i_d.di_forkoff = 0;
354 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
355
356 ASSERT(ip->i_d.di_anextents == 0);
357 ASSERT(ip->i_afp == NULL);
358
Christoph Hellwige1486de2009-02-04 09:36:00 +0100359 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
360}
361
362/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100363 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365int
366xfs_attr_shortform_remove(xfs_da_args_t *args)
367{
368 xfs_attr_shortform_t *sf;
369 xfs_attr_sf_entry_t *sfe;
370 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100371 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 xfs_inode_t *dp;
373
Dave Chinner5a5881c2012-03-22 05:15:13 +0000374 trace_xfs_attr_sf_remove(args);
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100377 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 base = sizeof(xfs_attr_sf_hdr_t);
379 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
380 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100381 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100382 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 base += size, i++) {
384 size = XFS_ATTR_SF_ENTSIZE(sfe);
385 if (sfe->namelen != args->namelen)
386 continue;
387 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
388 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000389 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 continue;
391 break;
392 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100393 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return(XFS_ERROR(ENOATTR));
395
Nathan Scottd8cc8902005-11-02 10:34:53 +1100396 /*
397 * Fix up the attribute fork data, covering the hole
398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100400 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100401 if (end != totsize)
402 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100403 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800404 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100405
406 /*
407 * Fix up the start offset of the attribute fork
408 */
409 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000410 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
Christoph Hellwige1486de2009-02-04 09:36:00 +0100411 (mp->m_flags & XFS_MOUNT_ATTR2) &&
412 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
413 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
414 xfs_attr_fork_reset(dp, args->trans);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100415 } else {
416 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
417 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
418 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000419 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
420 (args->op_flags & XFS_DA_OP_ADDNAME) ||
421 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
422 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100423 xfs_trans_log_inode(args->trans, dp,
424 XFS_ILOG_CORE | XFS_ILOG_ADATA);
425 }
426
427 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 return(0);
430}
431
432/*
433 * Look up a name in a shortform attribute list structure.
434 */
435/*ARGSUSED*/
436int
437xfs_attr_shortform_lookup(xfs_da_args_t *args)
438{
439 xfs_attr_shortform_t *sf;
440 xfs_attr_sf_entry_t *sfe;
441 int i;
442 xfs_ifork_t *ifp;
443
Dave Chinner5a5881c2012-03-22 05:15:13 +0000444 trace_xfs_attr_sf_lookup(args);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ifp = args->dp->i_afp;
447 ASSERT(ifp->if_flags & XFS_IFINLINE);
448 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
449 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100450 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
452 if (sfe->namelen != args->namelen)
453 continue;
454 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
455 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000456 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 continue;
458 return(XFS_ERROR(EEXIST));
459 }
460 return(XFS_ERROR(ENOATTR));
461}
462
463/*
464 * Look up a name in a shortform attribute list structure.
465 */
466/*ARGSUSED*/
467int
468xfs_attr_shortform_getvalue(xfs_da_args_t *args)
469{
470 xfs_attr_shortform_t *sf;
471 xfs_attr_sf_entry_t *sfe;
472 int i;
473
474 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
475 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
476 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100477 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
479 if (sfe->namelen != args->namelen)
480 continue;
481 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
482 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000483 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 continue;
485 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100486 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return(XFS_ERROR(EEXIST));
488 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100489 if (args->valuelen < sfe->valuelen) {
490 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return(XFS_ERROR(ERANGE));
492 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100493 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 memcpy(args->value, &sfe->nameval[args->namelen],
495 args->valuelen);
496 return(XFS_ERROR(EEXIST));
497 }
498 return(XFS_ERROR(ENOATTR));
499}
500
501/*
502 * Convert from using the shortform to the leaf.
503 */
504int
505xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
506{
507 xfs_inode_t *dp;
508 xfs_attr_shortform_t *sf;
509 xfs_attr_sf_entry_t *sfe;
510 xfs_da_args_t nargs;
511 char *tmpbuffer;
512 int error, i, size;
513 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000514 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 xfs_ifork_t *ifp;
516
Dave Chinner5a5881c2012-03-22 05:15:13 +0000517 trace_xfs_attr_sf_to_leaf(args);
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 dp = args->dp;
520 ifp = dp->i_afp;
521 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100522 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 tmpbuffer = kmem_alloc(size, KM_SLEEP);
524 ASSERT(tmpbuffer != NULL);
525 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
526 sf = (xfs_attr_shortform_t *)tmpbuffer;
527
528 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
529 bp = NULL;
530 error = xfs_da_grow_inode(args, &blkno);
531 if (error) {
532 /*
533 * If we hit an IO error middle of the transaction inside
534 * grow_inode(), we may have inconsistent data. Bail out.
535 */
536 if (error == EIO)
537 goto out;
538 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
539 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
540 goto out;
541 }
542
543 ASSERT(blkno == 0);
544 error = xfs_attr_leaf_create(args, blkno, &bp);
545 if (error) {
546 error = xfs_da_shrink_inode(args, 0, bp);
547 bp = NULL;
548 if (error)
549 goto out;
550 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
551 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
552 goto out;
553 }
554
555 memset((char *)&nargs, 0, sizeof(nargs));
556 nargs.dp = dp;
557 nargs.firstblock = args->firstblock;
558 nargs.flist = args->flist;
559 nargs.total = args->total;
560 nargs.whichfork = XFS_ATTR_FORK;
561 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000562 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100565 for (i = 0; i < sf->hdr.count; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100566 nargs.name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 nargs.namelen = sfe->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100568 nargs.value = &sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100569 nargs.valuelen = sfe->valuelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100570 nargs.hashval = xfs_da_hashname(sfe->nameval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000572 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
574 ASSERT(error == ENOATTR);
575 error = xfs_attr_leaf_add(bp, &nargs);
576 ASSERT(error != ENOSPC);
577 if (error)
578 goto out;
579 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
580 }
581 error = 0;
582
583out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000584 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return(error);
586}
587
588STATIC int
589xfs_attr_shortform_compare(const void *a, const void *b)
590{
591 xfs_attr_sf_sort_t *sa, *sb;
592
593 sa = (xfs_attr_sf_sort_t *)a;
594 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100595 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100597 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return(1);
599 } else {
600 return(sa->entno - sb->entno);
601 }
602}
603
Tim Shimmin726801b2006-09-28 11:01:37 +1000604
605#define XFS_ISRESET_CURSOR(cursor) \
606 (!((cursor)->initted) && !((cursor)->hashval) && \
607 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
609 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000610 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * If the output buffer is not large enough to hold them all, then we
612 * we have to calculate each entries' hashvalue and sort them before
613 * we can begin returning them to the user.
614 */
615/*ARGSUSED*/
616int
617xfs_attr_shortform_list(xfs_attr_list_context_t *context)
618{
619 attrlist_cursor_kern_t *cursor;
620 xfs_attr_sf_sort_t *sbuf, *sbp;
621 xfs_attr_shortform_t *sf;
622 xfs_attr_sf_entry_t *sfe;
623 xfs_inode_t *dp;
624 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000625 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 ASSERT(context != NULL);
628 dp = context->dp;
629 ASSERT(dp != NULL);
630 ASSERT(dp->i_afp != NULL);
631 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
632 ASSERT(sf != NULL);
633 if (!sf->hdr.count)
634 return(0);
635 cursor = context->cursor;
636 ASSERT(cursor != NULL);
637
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000638 trace_xfs_attr_list_sf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000641 * If the buffer is large enough and the cursor is at the start,
642 * do not bother with sorting since we will return everything in
643 * one buffer and another call using the cursor won't need to be
644 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000646 * If bufsize is zero then put_listent must be a search function
647 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000649 if (context->bufsize == 0 ||
650 (XFS_ISRESET_CURSOR(cursor) &&
651 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100652 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000653 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000654 sfe->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100655 sfe->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +1000656 (int)sfe->namelen,
657 (int)sfe->valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100658 &sfe->nameval[sfe->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000659
660 /*
661 * Either search callback finished early or
662 * didn't fit it all in the buffer after all.
663 */
664 if (context->seen_enough)
665 break;
666
667 if (error)
668 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
670 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000671 trace_xfs_attr_list_sf_all(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return(0);
673 }
674
Tim Shimmin726801b2006-09-28 11:01:37 +1000675 /* do no more for a search callback */
676 if (context->bufsize == 0)
677 return 0;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
680 * It didn't all fit, so we have to sort everything on hashval.
681 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100682 sbsize = sf->hdr.count * sizeof(*sbuf);
Dave Chinner622d8142010-12-23 11:57:37 +1100683 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /*
686 * Scan the attribute list for the rest of the entries, storing
687 * the relevant info from only those that match into a buffer.
688 */
689 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100690 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (unlikely(
692 ((char *)sfe < (char *)sf) ||
693 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
694 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
695 XFS_ERRLEVEL_LOW,
696 context->dp->i_mount, sfe);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000697 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return XFS_ERROR(EFSCORRUPTED);
699 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 sbp->entno = i;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100702 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
703 sbp->name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 sbp->namelen = sfe->namelen;
705 /* These are bytes, and both on-disk, don't endian-flip */
706 sbp->valuelen = sfe->valuelen;
707 sbp->flags = sfe->flags;
708 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
709 sbp++;
710 nsbuf++;
711 }
712
713 /*
714 * Sort the entries on hash then entno.
715 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100716 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /*
719 * Re-find our place IN THE SORTED LIST.
720 */
721 count = 0;
722 cursor->initted = 1;
723 cursor->blkno = 0;
724 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100725 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (cursor->offset == count) {
727 break;
728 }
729 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100730 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
732 }
733 }
734 if (i == nsbuf) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000735 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return(0);
737 }
738
739 /*
740 * Loop putting entries into the user buffer.
741 */
742 for ( ; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100743 if (cursor->hashval != sbp->hash) {
744 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 cursor->offset = 0;
746 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000747 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000748 sbp->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000749 sbp->name,
750 sbp->namelen,
751 sbp->valuelen,
752 &sbp->name[sbp->namelen]);
753 if (error)
754 return error;
755 if (context->seen_enough)
756 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 cursor->offset++;
758 }
759
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000760 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return(0);
762}
763
764/*
765 * Check a leaf attribute block to see if all the entries would fit into
766 * a shortform attribute list.
767 */
768int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000769xfs_attr_shortform_allfit(
770 struct xfs_buf *bp,
771 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 xfs_attr_leafblock_t *leaf;
774 xfs_attr_leaf_entry_t *entry;
775 xfs_attr_leaf_name_local_t *name_loc;
776 int bytes, i;
777
Dave Chinner1d9025e2012-06-22 18:50:14 +1000778 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200779 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 entry = &leaf->entries[0];
782 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100783 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (entry->flags & XFS_ATTR_INCOMPLETE)
785 continue; /* don't copy partial entries */
786 if (!(entry->flags & XFS_ATTR_LOCAL))
787 return(0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600788 name_loc = xfs_attr_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
790 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100791 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return(0);
793 bytes += sizeof(struct xfs_attr_sf_entry)-1
794 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100795 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100797 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100798 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100799 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100800 return(-1);
801 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/*
805 * Convert a leaf attribute list to shortform attribute list
806 */
807int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000808xfs_attr_leaf_to_shortform(
809 struct xfs_buf *bp,
810 xfs_da_args_t *args,
811 int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 xfs_attr_leafblock_t *leaf;
814 xfs_attr_leaf_entry_t *entry;
815 xfs_attr_leaf_name_local_t *name_loc;
816 xfs_da_args_t nargs;
817 xfs_inode_t *dp;
818 char *tmpbuffer;
819 int error, i;
820
Dave Chinner5a5881c2012-03-22 05:15:13 +0000821 trace_xfs_attr_leaf_to_sf(args);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 dp = args->dp;
824 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
825 ASSERT(tmpbuffer != NULL);
826
827 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000828 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200830 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000831 memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /*
834 * Clean out the prior contents of the attribute list.
835 */
836 error = xfs_da_shrink_inode(args, 0, bp);
837 if (error)
838 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100839
840 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +1100841 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +1100842 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Christoph Hellwige1486de2009-02-04 09:36:00 +0100843 xfs_attr_fork_reset(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100845 }
846
847 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /*
850 * Copy the attributes
851 */
852 memset((char *)&nargs, 0, sizeof(nargs));
853 nargs.dp = dp;
854 nargs.firstblock = args->firstblock;
855 nargs.flist = args->flist;
856 nargs.total = args->total;
857 nargs.whichfork = XFS_ATTR_FORK;
858 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000859 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +1100861 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (entry->flags & XFS_ATTR_INCOMPLETE)
863 continue; /* don't copy partial entries */
864 if (!entry->nameidx)
865 continue;
866 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600867 name_loc = xfs_attr_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +1100868 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100870 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +1100871 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100872 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +1000873 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100874 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 error = 0;
877
878out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000879 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return(error);
881}
882
883/*
884 * Convert from using a single leaf to a root node and a leaf.
885 */
886int
887xfs_attr_leaf_to_node(xfs_da_args_t *args)
888{
889 xfs_attr_leafblock_t *leaf;
890 xfs_da_intnode_t *node;
891 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000892 struct xfs_buf *bp1, *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 xfs_dablk_t blkno;
894 int error;
895
Dave Chinner5a5881c2012-03-22 05:15:13 +0000896 trace_xfs_attr_leaf_to_node(args);
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 dp = args->dp;
899 bp1 = bp2 = NULL;
900 error = xfs_da_grow_inode(args, &blkno);
901 if (error)
902 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +1100903 error = xfs_attr_leaf_read(args->trans, args->dp, 0, -1, &bp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (error)
905 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +1100906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 bp2 = NULL;
908 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
909 XFS_ATTR_FORK);
910 if (error)
911 goto out;
912 ASSERT(bp2 != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000913 memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 bp1 = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000915 xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /*
918 * Set up the new root node.
919 */
920 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
921 if (error)
922 goto out;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000923 node = bp1->b_addr;
924 leaf = bp2->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200925 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* both on-disk, don't endian-flip twice */
927 node->btree[0].hashval =
Nathan Scott918ae422006-03-17 17:28:54 +1100928 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
Nathan Scott403432d2006-03-17 17:29:46 +1100929 node->btree[0].before = cpu_to_be32(blkno);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100930 node->hdr.count = cpu_to_be16(1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000931 xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 error = 0;
933out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return(error);
935}
936
937
938/*========================================================================
939 * Routines used for growing the Btree.
940 *========================================================================*/
941
942/*
943 * Create the initial contents of a leaf attribute list
944 * or a leaf in a node attribute list.
945 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000946STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000947xfs_attr_leaf_create(
948 xfs_da_args_t *args,
949 xfs_dablk_t blkno,
950 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 xfs_attr_leafblock_t *leaf;
953 xfs_attr_leaf_hdr_t *hdr;
954 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000955 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int error;
957
Dave Chinner5a5881c2012-03-22 05:15:13 +0000958 trace_xfs_attr_leaf_create(args);
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 dp = args->dp;
961 ASSERT(dp != NULL);
962 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
963 XFS_ATTR_FORK);
964 if (error)
965 return(error);
966 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000967 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
969 hdr = &leaf->hdr;
Nathan Scott89da0542006-03-17 17:28:40 +1100970 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +1100971 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +1100973 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
975 }
976
Nathan Scott918ae422006-03-17 17:28:54 +1100977 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
978 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
979 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Dave Chinner1d9025e2012-06-22 18:50:14 +1000981 xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 *bpp = bp;
984 return(0);
985}
986
987/*
988 * Split the leaf node, rebalance, then add the new entry.
989 */
990int
991xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
992 xfs_da_state_blk_t *newblk)
993{
994 xfs_dablk_t blkno;
995 int error;
996
Dave Chinner5a5881c2012-03-22 05:15:13 +0000997 trace_xfs_attr_leaf_split(state->args);
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /*
1000 * Allocate space for a new leaf node.
1001 */
1002 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1003 error = xfs_da_grow_inode(state->args, &blkno);
1004 if (error)
1005 return(error);
1006 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
1007 if (error)
1008 return(error);
1009 newblk->blkno = blkno;
1010 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1011
1012 /*
1013 * Rebalance the entries across the two leaves.
1014 * NOTE: rebalance() currently depends on the 2nd block being empty.
1015 */
1016 xfs_attr_leaf_rebalance(state, oldblk, newblk);
1017 error = xfs_da_blk_link(state, oldblk, newblk);
1018 if (error)
1019 return(error);
1020
1021 /*
1022 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1023 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1024 * "new" attrs info. Will need the "old" info to remove it later.
1025 *
1026 * Insert the "new" entry in the correct block.
1027 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001028 if (state->inleaf) {
1029 trace_xfs_attr_leaf_add_old(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 error = xfs_attr_leaf_add(oldblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001031 } else {
1032 trace_xfs_attr_leaf_add_new(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 error = xfs_attr_leaf_add(newblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /*
1037 * Update last hashval in each block since we added the name.
1038 */
1039 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1040 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1041 return(error);
1042}
1043
1044/*
1045 * Add a name to the leaf attribute list structure.
1046 */
1047int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001048xfs_attr_leaf_add(
1049 struct xfs_buf *bp,
1050 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 xfs_attr_leafblock_t *leaf;
1053 xfs_attr_leaf_hdr_t *hdr;
1054 xfs_attr_leaf_map_t *map;
1055 int tablesize, entsize, sum, tmp, i;
1056
Dave Chinner5a5881c2012-03-22 05:15:13 +00001057 trace_xfs_attr_leaf_add(args);
1058
Dave Chinner1d9025e2012-06-22 18:50:14 +10001059 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001060 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001062 && (args->index <= be16_to_cpu(leaf->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 hdr = &leaf->hdr;
Nathan Scottaa82daa2005-11-02 10:33:33 +11001064 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1066
1067 /*
1068 * Search through freemap for first-fit on new name length.
1069 * (may need to figure in size of entry struct too)
1070 */
Nathan Scott918ae422006-03-17 17:28:54 +11001071 tablesize = (be16_to_cpu(hdr->count) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 * sizeof(xfs_attr_leaf_entry_t)
1073 + sizeof(xfs_attr_leaf_hdr_t);
1074 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1075 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
Nathan Scott918ae422006-03-17 17:28:54 +11001076 if (tablesize > be16_to_cpu(hdr->firstused)) {
1077 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 continue;
1079 }
1080 if (!map->size)
1081 continue; /* no space in this map */
1082 tmp = entsize;
Nathan Scott918ae422006-03-17 17:28:54 +11001083 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 tmp += sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001085 if (be16_to_cpu(map->size) >= tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 tmp = xfs_attr_leaf_add_work(bp, args, i);
1087 return(tmp);
1088 }
Nathan Scott918ae422006-03-17 17:28:54 +11001089 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
1092 /*
1093 * If there are no holes in the address space of the block,
1094 * and we don't have enough freespace, then compaction will do us
1095 * no good and we should just give up.
1096 */
1097 if (!hdr->holes && (sum < entsize))
1098 return(XFS_ERROR(ENOSPC));
1099
1100 /*
1101 * Compact the entries to coalesce free space.
1102 * This may change the hdr->count via dropping INCOMPLETE entries.
1103 */
Dave Chinneree732592012-11-12 22:53:53 +11001104 xfs_attr_leaf_compact(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 /*
1107 * After compaction, the block is guaranteed to have only one
1108 * free region, in freemap[0]. If it is not big enough, give up.
1109 */
Nathan Scott918ae422006-03-17 17:28:54 +11001110 if (be16_to_cpu(hdr->freemap[0].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1112 return(XFS_ERROR(ENOSPC));
1113
1114 return(xfs_attr_leaf_add_work(bp, args, 0));
1115}
1116
1117/*
1118 * Add a name to a leaf attribute list structure.
1119 */
1120STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001121xfs_attr_leaf_add_work(
1122 struct xfs_buf *bp,
1123 xfs_da_args_t *args,
1124 int mapindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 xfs_attr_leafblock_t *leaf;
1127 xfs_attr_leaf_hdr_t *hdr;
1128 xfs_attr_leaf_entry_t *entry;
1129 xfs_attr_leaf_name_local_t *name_loc;
1130 xfs_attr_leaf_name_remote_t *name_rmt;
1131 xfs_attr_leaf_map_t *map;
1132 xfs_mount_t *mp;
1133 int tmp, i;
1134
Dave Chinneree732592012-11-12 22:53:53 +11001135 trace_xfs_attr_leaf_add_work(args);
1136
Dave Chinner1d9025e2012-06-22 18:50:14 +10001137 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001138 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 hdr = &leaf->hdr;
1140 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
Nathan Scott918ae422006-03-17 17:28:54 +11001141 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 /*
1144 * Force open some space in the entry array and fill it in.
1145 */
1146 entry = &leaf->entries[args->index];
Nathan Scott918ae422006-03-17 17:28:54 +11001147 if (args->index < be16_to_cpu(hdr->count)) {
1148 tmp = be16_to_cpu(hdr->count) - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 tmp *= sizeof(xfs_attr_leaf_entry_t);
1150 memmove((char *)(entry+1), (char *)entry, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001151 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1153 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001154 be16_add_cpu(&hdr->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /*
1157 * Allocate space for the new string (at the end of the run).
1158 */
1159 map = &hdr->freemap[mapindex];
1160 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001161 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1162 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1163 ASSERT(be16_to_cpu(map->size) >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001164 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1165 mp->m_sb.sb_blocksize, NULL));
Nathan Scott918ae422006-03-17 17:28:54 +11001166 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1167 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001168 be16_add_cpu(&map->size,
Nathan Scottaa82daa2005-11-02 10:33:33 +11001169 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1170 mp->m_sb.sb_blocksize, &tmp));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001171 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1172 be16_to_cpu(map->size));
1173 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001175 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001176 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 entry->flags |= XFS_ATTR_INCOMPLETE;
1178 if ((args->blkno2 == args->blkno) &&
1179 (args->index2 <= args->index)) {
1180 args->index2++;
1181 }
1182 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001183 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001185 ASSERT((args->index == 0) ||
1186 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Nathan Scott918ae422006-03-17 17:28:54 +11001187 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001188 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 * For "remote" attribute values, simply note that we need to
1192 * allocate space for the "remote" value. We can't actually
1193 * allocate the extents in this transaction, and we can't decide
1194 * which blocks they should be as we might allocate more blocks
1195 * as part of this transaction (a split operation for example).
1196 */
1197 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001198 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001200 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1202 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001203 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001205 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 name_rmt->namelen = args->namelen;
1207 memcpy((char *)name_rmt->name, args->name, args->namelen);
1208 entry->flags |= XFS_ATTR_INCOMPLETE;
1209 /* just in case */
1210 name_rmt->valuelen = 0;
1211 name_rmt->valueblk = 0;
1212 args->rmtblkno = 1;
1213 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1214 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001215 xfs_trans_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001216 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 xfs_attr_leaf_entsize(leaf, args->index)));
1218
1219 /*
1220 * Update the control info for this leaf node
1221 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001222 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /* both on-disk, don't endian-flip twice */
1224 hdr->firstused = entry->nameidx;
1225 }
Nathan Scott918ae422006-03-17 17:28:54 +11001226 ASSERT(be16_to_cpu(hdr->firstused) >=
1227 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1228 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 + sizeof(xfs_attr_leaf_hdr_t);
1230 map = &hdr->freemap[0];
1231 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001232 if (be16_to_cpu(map->base) == tmp) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001233 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1234 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001235 -((int)sizeof(xfs_attr_leaf_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001238 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001239 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1241 return(0);
1242}
1243
1244/*
1245 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1246 */
1247STATIC void
Dave Chinner1d9025e2012-06-22 18:50:14 +10001248xfs_attr_leaf_compact(
Dave Chinneree732592012-11-12 22:53:53 +11001249 struct xfs_da_args *args,
1250 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Dave Chinneree732592012-11-12 22:53:53 +11001252 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1253 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1254 struct xfs_trans *trans = args->trans;
1255 struct xfs_mount *mp = trans->t_mountp;
1256 char *tmpbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Dave Chinneree732592012-11-12 22:53:53 +11001258 trace_xfs_attr_leaf_compact(args);
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1261 ASSERT(tmpbuffer != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001262 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1263 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /*
1266 * Copy basic information
1267 */
1268 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001269 leaf_d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 hdr_s = &leaf_s->hdr;
1271 hdr_d = &leaf_d->hdr;
1272 hdr_d->info = hdr_s->info; /* struct copy */
Nathan Scott918ae422006-03-17 17:28:54 +11001273 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* handle truncation gracefully */
1275 if (!hdr_d->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001276 hdr_d->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1278 }
1279 hdr_d->usedbytes = 0;
1280 hdr_d->count = 0;
1281 hdr_d->holes = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001282 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1283 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1284 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /*
1287 * Copy all entry's in the same (sorted) order,
1288 * but allocate name/value pairs packed and in sequence.
1289 */
1290 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001291 be16_to_cpu(hdr_s->count), mp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001292 xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001294 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
1297/*
1298 * Redistribute the attribute list entries between two leaf nodes,
1299 * taking into account the size of the new entry.
1300 *
1301 * NOTE: if new block is empty, then it will get the upper half of the
1302 * old block. At present, all (one) callers pass in an empty second block.
1303 *
1304 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1305 * to match what it is doing in splitting the attribute leaf block. Those
1306 * values are used in "atomic rename" operations on attributes. Note that
1307 * the "new" and "old" values can end up in different blocks.
1308 */
1309STATIC void
1310xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1311 xfs_da_state_blk_t *blk2)
1312{
1313 xfs_da_args_t *args;
1314 xfs_da_state_blk_t *tmp_blk;
1315 xfs_attr_leafblock_t *leaf1, *leaf2;
1316 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1317 int count, totallen, max, space, swap;
1318
1319 /*
1320 * Set up environment.
1321 */
1322 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1323 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001324 leaf1 = blk1->bp->b_addr;
1325 leaf2 = blk2->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001326 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1327 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Dave Chinner07428d72012-11-12 22:09:44 +11001328 ASSERT(leaf2->hdr.count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 args = state->args;
1330
Dave Chinner5a5881c2012-03-22 05:15:13 +00001331 trace_xfs_attr_leaf_rebalance(args);
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /*
1334 * Check ordering of blocks, reverse if it makes things simpler.
1335 *
1336 * NOTE: Given that all (current) callers pass in an empty
1337 * second block, this code should never set "swap".
1338 */
1339 swap = 0;
1340 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1341 tmp_blk = blk1;
1342 blk1 = blk2;
1343 blk2 = tmp_blk;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001344 leaf1 = blk1->bp->b_addr;
1345 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 swap = 1;
1347 }
1348 hdr1 = &leaf1->hdr;
1349 hdr2 = &leaf2->hdr;
1350
1351 /*
1352 * Examine entries until we reduce the absolute difference in
1353 * byte usage between the two blocks to a minimum. Then get
1354 * the direction to copy and the number of elements to move.
1355 *
1356 * "inleaf" is true if the new entry should be inserted into blk1.
1357 * If "swap" is also true, then reverse the sense of "inleaf".
1358 */
1359 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1360 &count, &totallen);
1361 if (swap)
1362 state->inleaf = !state->inleaf;
1363
1364 /*
1365 * Move any entries required from leaf to leaf:
1366 */
Nathan Scott918ae422006-03-17 17:28:54 +11001367 if (count < be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /*
1369 * Figure the total bytes to be added to the destination leaf.
1370 */
1371 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001372 count = be16_to_cpu(hdr1->count) - count;
1373 space = be16_to_cpu(hdr1->usedbytes) - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 space += count * sizeof(xfs_attr_leaf_entry_t);
1375
1376 /*
1377 * leaf2 is the destination, compact it if it looks tight.
1378 */
Nathan Scott918ae422006-03-17 17:28:54 +11001379 max = be16_to_cpu(hdr2->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001381 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001382 if (space > max)
1383 xfs_attr_leaf_compact(args, blk2->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /*
1386 * Move high entries from leaf1 to low end of leaf2.
1387 */
Nathan Scott918ae422006-03-17 17:28:54 +11001388 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 leaf2, 0, count, state->mp);
1390
Dave Chinner1d9025e2012-06-22 18:50:14 +10001391 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1392 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Nathan Scott918ae422006-03-17 17:28:54 +11001393 } else if (count > be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /*
1395 * I assert that since all callers pass in an empty
1396 * second buffer, this code should never execute.
1397 */
Dave Chinner07428d72012-11-12 22:09:44 +11001398 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
1401 * Figure the total bytes to be added to the destination leaf.
1402 */
1403 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001404 count -= be16_to_cpu(hdr1->count);
1405 space = totallen - be16_to_cpu(hdr1->usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 space += count * sizeof(xfs_attr_leaf_entry_t);
1407
1408 /*
1409 * leaf1 is the destination, compact it if it looks tight.
1410 */
Nathan Scott918ae422006-03-17 17:28:54 +11001411 max = be16_to_cpu(hdr1->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001413 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001414 if (space > max)
1415 xfs_attr_leaf_compact(args, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /*
1418 * Move low entries from leaf2 to high end of leaf1.
1419 */
1420 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
Nathan Scott918ae422006-03-17 17:28:54 +11001421 be16_to_cpu(hdr1->count), count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Dave Chinner1d9025e2012-06-22 18:50:14 +10001423 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1424 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426
1427 /*
1428 * Copy out last hashval in each block for B-tree code.
1429 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001430 blk1->hashval = be32_to_cpu(
1431 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1432 blk2->hashval = be32_to_cpu(
1433 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 /*
1436 * Adjust the expected index for insertion.
1437 * NOTE: this code depends on the (current) situation that the
1438 * second block was originally empty.
1439 *
1440 * If the insertion point moved to the 2nd block, we must adjust
1441 * the index. We must also track the entry just following the
1442 * new entry for use in an "atomic rename" operation, that entry
1443 * is always the "old" entry and the "new" entry is what we are
1444 * inserting. The index/blkno fields refer to the "old" entry,
1445 * while the index2/blkno2 fields refer to the "new" entry.
1446 */
Nathan Scott918ae422006-03-17 17:28:54 +11001447 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 ASSERT(state->inleaf == 0);
Nathan Scott918ae422006-03-17 17:28:54 +11001449 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 args->index = args->index2 = blk2->index;
1451 args->blkno = args->blkno2 = blk2->blkno;
Nathan Scott918ae422006-03-17 17:28:54 +11001452 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (state->inleaf) {
1454 args->index = blk1->index;
1455 args->blkno = blk1->blkno;
1456 args->index2 = 0;
1457 args->blkno2 = blk2->blkno;
1458 } else {
Dave Chinner07428d72012-11-12 22:09:44 +11001459 /*
1460 * On a double leaf split, the original attr location
1461 * is already stored in blkno2/index2, so don't
1462 * overwrite it overwise we corrupt the tree.
1463 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 blk2->index = blk1->index
Nathan Scott918ae422006-03-17 17:28:54 +11001465 - be16_to_cpu(leaf1->hdr.count);
Dave Chinner07428d72012-11-12 22:09:44 +11001466 args->index = blk2->index;
1467 args->blkno = blk2->blkno;
1468 if (!state->extravalid) {
1469 /*
1470 * set the new attr location to match the old
1471 * one and let the higher level split code
1472 * decide where in the leaf to place it.
1473 */
1474 args->index2 = blk2->index;
1475 args->blkno2 = blk2->blkno;
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478 } else {
1479 ASSERT(state->inleaf == 1);
1480 args->index = args->index2 = blk1->index;
1481 args->blkno = args->blkno2 = blk1->blkno;
1482 }
1483}
1484
1485/*
1486 * Examine entries until we reduce the absolute difference in
1487 * byte usage between the two blocks to a minimum.
1488 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1489 * GROT: there will always be enough room in either block for a new entry.
1490 * GROT: Do a double-split for this case?
1491 */
1492STATIC int
1493xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1494 xfs_da_state_blk_t *blk1,
1495 xfs_da_state_blk_t *blk2,
1496 int *countarg, int *usedbytesarg)
1497{
1498 xfs_attr_leafblock_t *leaf1, *leaf2;
1499 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1500 xfs_attr_leaf_entry_t *entry;
1501 int count, max, index, totallen, half;
1502 int lastdelta, foundit, tmp;
1503
1504 /*
1505 * Set up environment.
1506 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001507 leaf1 = blk1->bp->b_addr;
1508 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 hdr1 = &leaf1->hdr;
1510 hdr2 = &leaf2->hdr;
1511 foundit = 0;
1512 totallen = 0;
1513
1514 /*
1515 * Examine entries until we reduce the absolute difference in
1516 * byte usage between the two blocks to a minimum.
1517 */
Nathan Scott918ae422006-03-17 17:28:54 +11001518 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 half = (max+1) * sizeof(*entry);
Nathan Scott918ae422006-03-17 17:28:54 +11001520 half += be16_to_cpu(hdr1->usedbytes) +
1521 be16_to_cpu(hdr2->usedbytes) +
1522 xfs_attr_leaf_newentsize(
1523 state->args->namelen,
1524 state->args->valuelen,
1525 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 half /= 2;
1527 lastdelta = state->blocksize;
1528 entry = &leaf1->entries[0];
1529 for (count = index = 0; count < max; entry++, index++, count++) {
1530
1531#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1532 /*
1533 * The new entry is in the first block, account for it.
1534 */
1535 if (count == blk1->index) {
1536 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001537 xfs_attr_leaf_newentsize(
1538 state->args->namelen,
1539 state->args->valuelen,
1540 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1542 break;
1543 lastdelta = XFS_ATTR_ABS(half - tmp);
1544 totallen = tmp;
1545 foundit = 1;
1546 }
1547
1548 /*
1549 * Wrap around into the second block if necessary.
1550 */
Nathan Scott918ae422006-03-17 17:28:54 +11001551 if (count == be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 leaf1 = leaf2;
1553 entry = &leaf1->entries[0];
1554 index = 0;
1555 }
1556
1557 /*
1558 * Figure out if next leaf entry would be too much.
1559 */
1560 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1561 index);
1562 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1563 break;
1564 lastdelta = XFS_ATTR_ABS(half - tmp);
1565 totallen = tmp;
1566#undef XFS_ATTR_ABS
1567 }
1568
1569 /*
1570 * Calculate the number of usedbytes that will end up in lower block.
1571 * If new entry not in lower block, fix up the count.
1572 */
1573 totallen -= count * sizeof(*entry);
1574 if (foundit) {
1575 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001576 xfs_attr_leaf_newentsize(
1577 state->args->namelen,
1578 state->args->valuelen,
1579 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581
1582 *countarg = count;
1583 *usedbytesarg = totallen;
1584 return(foundit);
1585}
1586
1587/*========================================================================
1588 * Routines used for shrinking the Btree.
1589 *========================================================================*/
1590
1591/*
1592 * Check a leaf block and its neighbors to see if the block should be
1593 * collapsed into one or the other neighbor. Always keep the block
1594 * with the smaller block number.
1595 * If the current block is over 50% full, don't try to join it, return 0.
1596 * If the block is empty, fill in the state structure and return 2.
1597 * If it can be collapsed, fill in the state structure and return 1.
1598 * If nothing can be done, return 0.
1599 *
1600 * GROT: allow for INCOMPLETE entries in calculation.
1601 */
1602int
1603xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1604{
1605 xfs_attr_leafblock_t *leaf;
1606 xfs_da_state_blk_t *blk;
1607 xfs_da_blkinfo_t *info;
1608 int count, bytes, forward, error, retval, i;
1609 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001610 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Dave Chinneree732592012-11-12 22:53:53 +11001612 trace_xfs_attr_leaf_toosmall(state->args);
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /*
1615 * Check for the degenerate case of the block being over 50% full.
1616 * If so, it's not worth even looking to see if we might be able
1617 * to coalesce with a sibling.
1618 */
1619 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001620 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001621 ASSERT(info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001623 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1625 count * sizeof(xfs_attr_leaf_entry_t) +
Nathan Scott918ae422006-03-17 17:28:54 +11001626 be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (bytes > (state->blocksize >> 1)) {
1628 *action = 0; /* blk over 50%, don't try to join */
1629 return(0);
1630 }
1631
1632 /*
1633 * Check for the degenerate case of the block being empty.
1634 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001635 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * to merge with the forward block unless it is NULL.
1637 */
1638 if (count == 0) {
1639 /*
1640 * Make altpath point to the block we want to keep and
1641 * path point to the block we want to drop (this one).
1642 */
Nathan Scott89da0542006-03-17 17:28:40 +11001643 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 memcpy(&state->altpath, &state->path, sizeof(state->path));
1645 error = xfs_da_path_shift(state, &state->altpath, forward,
1646 0, &retval);
1647 if (error)
1648 return(error);
1649 if (retval) {
1650 *action = 0;
1651 } else {
1652 *action = 2;
1653 }
1654 return(0);
1655 }
1656
1657 /*
1658 * Examine each sibling block to see if we can coalesce with
1659 * at least 25% free space to spare. We need to figure out
1660 * whether to merge with the forward or the backward block.
1661 * We prefer coalescing with the lower numbered sibling so as
1662 * to shrink an attribute list over time.
1663 */
1664 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +11001665 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 for (i = 0; i < 2; forward = !forward, i++) {
1667 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +11001668 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 else
Nathan Scott89da0542006-03-17 17:28:40 +11001670 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (blkno == 0)
1672 continue;
Dave Chinnerad14c332012-11-12 22:54:16 +11001673 error = xfs_attr_leaf_read(state->args->trans, state->args->dp,
1674 blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (error)
1676 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001679 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 bytes = state->blocksize - (state->blocksize>>2);
Nathan Scott918ae422006-03-17 17:28:54 +11001681 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001682 leaf = bp->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11001683 count += be16_to_cpu(leaf->hdr.count);
1684 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1686 bytes -= sizeof(xfs_attr_leaf_hdr_t);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001687 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (bytes >= 0)
1689 break; /* fits with at least 25% to spare */
1690 }
1691 if (i >= 2) {
1692 *action = 0;
1693 return(0);
1694 }
1695
1696 /*
1697 * Make altpath point to the block we want to keep (the lower
1698 * numbered block) and path point to the block we want to drop.
1699 */
1700 memcpy(&state->altpath, &state->path, sizeof(state->path));
1701 if (blkno < blk->blkno) {
1702 error = xfs_da_path_shift(state, &state->altpath, forward,
1703 0, &retval);
1704 } else {
1705 error = xfs_da_path_shift(state, &state->path, forward,
1706 0, &retval);
1707 }
1708 if (error)
1709 return(error);
1710 if (retval) {
1711 *action = 0;
1712 } else {
1713 *action = 1;
1714 }
1715 return(0);
1716}
1717
1718/*
1719 * Remove a name from the leaf attribute list structure.
1720 *
1721 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1722 * If two leaves are 37% full, when combined they will leave 25% free.
1723 */
1724int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001725xfs_attr_leaf_remove(
1726 struct xfs_buf *bp,
1727 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 xfs_attr_leafblock_t *leaf;
1730 xfs_attr_leaf_hdr_t *hdr;
1731 xfs_attr_leaf_map_t *map;
1732 xfs_attr_leaf_entry_t *entry;
1733 int before, after, smallest, entsize;
1734 int tablesize, tmp, i;
1735 xfs_mount_t *mp;
1736
Dave Chinneree732592012-11-12 22:53:53 +11001737 trace_xfs_attr_leaf_remove(args);
1738
Dave Chinner1d9025e2012-06-22 18:50:14 +10001739 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001740 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 hdr = &leaf->hdr;
1742 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001743 ASSERT((be16_to_cpu(hdr->count) > 0)
1744 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001746 && (args->index < be16_to_cpu(hdr->count)));
1747 ASSERT(be16_to_cpu(hdr->firstused) >=
1748 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 entry = &leaf->entries[args->index];
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001750 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1751 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 /*
1754 * Scan through free region table:
1755 * check for adjacency of free'd entry with an existing one,
1756 * find smallest free region in case we need to replace it,
1757 * adjust any map that borders the entry table,
1758 */
Nathan Scott918ae422006-03-17 17:28:54 +11001759 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 + sizeof(xfs_attr_leaf_hdr_t);
1761 map = &hdr->freemap[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001762 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 before = after = -1;
1764 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1765 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1766 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001767 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1768 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1769 if (be16_to_cpu(map->base) == tablesize) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001770 be16_add_cpu(&map->base,
Nathan Scott918ae422006-03-17 17:28:54 +11001771 -((int)sizeof(xfs_attr_leaf_entry_t)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001772 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
Nathan Scott918ae422006-03-17 17:28:54 +11001775 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001776 == be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 before = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001778 } else if (be16_to_cpu(map->base)
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001779 == (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 after = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001781 } else if (be16_to_cpu(map->size) < tmp) {
1782 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 smallest = i;
1784 }
1785 }
1786
1787 /*
1788 * Coalesce adjacent freemap regions,
1789 * or replace the smallest region.
1790 */
1791 if ((before >= 0) || (after >= 0)) {
1792 if ((before >= 0) && (after >= 0)) {
1793 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001794 be16_add_cpu(&map->size, entsize);
1795 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001796 be16_to_cpu(hdr->freemap[after].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 hdr->freemap[after].base = 0;
1798 hdr->freemap[after].size = 0;
1799 } else if (before >= 0) {
1800 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001801 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 } else {
1803 map = &hdr->freemap[after];
1804 /* both on-disk, don't endian flip twice */
1805 map->base = entry->nameidx;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001806 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808 } else {
1809 /*
1810 * Replace smallest region (if it is smaller than free'd entry)
1811 */
1812 map = &hdr->freemap[smallest];
Nathan Scott918ae422006-03-17 17:28:54 +11001813 if (be16_to_cpu(map->size) < entsize) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001814 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
Nathan Scott918ae422006-03-17 17:28:54 +11001815 map->size = cpu_to_be16(entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 }
1818
1819 /*
1820 * Did we remove the first entry?
1821 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001822 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 smallest = 1;
1824 else
1825 smallest = 0;
1826
1827 /*
1828 * Compress the remaining entries and zero out the removed stuff.
1829 */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001830 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001831 be16_add_cpu(&hdr->usedbytes, -entsize);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001832 xfs_trans_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001833 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 entsize));
1835
Nathan Scott918ae422006-03-17 17:28:54 +11001836 tmp = (be16_to_cpu(hdr->count) - args->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 * sizeof(xfs_attr_leaf_entry_t);
1838 memmove((char *)entry, (char *)(entry+1), tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001839 be16_add_cpu(&hdr->count, -1);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001840 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
Nathan Scott918ae422006-03-17 17:28:54 +11001842 entry = &leaf->entries[be16_to_cpu(hdr->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1844
1845 /*
1846 * If we removed the first entry, re-find the first used byte
1847 * in the name area. Note that if the entry was the "firstused",
1848 * then we don't have a "hole" in our block resulting from
1849 * removing the name.
1850 */
1851 if (smallest) {
1852 tmp = XFS_LBSIZE(mp);
1853 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001854 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001855 ASSERT(be16_to_cpu(entry->nameidx) >=
1856 be16_to_cpu(hdr->firstused));
1857 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1858
1859 if (be16_to_cpu(entry->nameidx) < tmp)
1860 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Nathan Scott918ae422006-03-17 17:28:54 +11001862 hdr->firstused = cpu_to_be16(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001864 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1866 }
1867 } else {
1868 hdr->holes = 1; /* mark as needing compaction */
1869 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001870 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1872
1873 /*
1874 * Check if leaf is less than 50% full, caller may want to
1875 * "join" the leaf with a sibling if so.
1876 */
1877 tmp = sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001878 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1879 tmp += be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1881}
1882
1883/*
1884 * Move all the attribute list entries from drop_leaf into save_leaf.
1885 */
1886void
1887xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1888 xfs_da_state_blk_t *save_blk)
1889{
1890 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1891 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1892 xfs_mount_t *mp;
1893 char *tmpbuffer;
1894
Dave Chinner5a5881c2012-03-22 05:15:13 +00001895 trace_xfs_attr_leaf_unbalance(state->args);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * Set up environment.
1899 */
1900 mp = state->mp;
1901 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1902 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001903 drop_leaf = drop_blk->bp->b_addr;
1904 save_leaf = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001905 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1906 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 drop_hdr = &drop_leaf->hdr;
1908 save_hdr = &save_leaf->hdr;
1909
1910 /*
1911 * Save last hashval from dying block for later Btree fixup.
1912 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001913 drop_blk->hashval = be32_to_cpu(
1914 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /*
1917 * Check if we need a temp buffer, or can we do it in place.
1918 * Note that we don't check "leaf" for holes because we will
1919 * always be dropping it, toosmall() decided that for us already.
1920 */
1921 if (save_hdr->holes == 0) {
1922 /*
1923 * dest leaf has no holes, so we add there. May need
1924 * to make some room in the entry array.
1925 */
1926 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1927 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001928 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 } else {
1930 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001931 be16_to_cpu(save_hdr->count),
1932 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934 } else {
1935 /*
1936 * Destination has holes, so we make a temporary copy
1937 * of the leaf and add them both to that.
1938 */
1939 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1940 ASSERT(tmpbuffer != NULL);
1941 memset(tmpbuffer, 0, state->blocksize);
1942 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1943 tmp_hdr = &tmp_leaf->hdr;
1944 tmp_hdr->info = save_hdr->info; /* struct copy */
1945 tmp_hdr->count = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001946 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (!tmp_hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001948 tmp_hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1950 }
1951 tmp_hdr->usedbytes = 0;
1952 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1953 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001954 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001956 be16_to_cpu(tmp_leaf->hdr.count),
1957 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 } else {
1959 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001960 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001962 be16_to_cpu(tmp_leaf->hdr.count),
1963 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001966 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968
Dave Chinner1d9025e2012-06-22 18:50:14 +10001969 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 state->blocksize - 1);
1971
1972 /*
1973 * Copy out last hashval in each block for B-tree code.
1974 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001975 save_blk->hashval = be32_to_cpu(
1976 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
1979/*========================================================================
1980 * Routines used for finding things in the Btree.
1981 *========================================================================*/
1982
1983/*
1984 * Look up a name in a leaf attribute list structure.
1985 * This is the internal routine, it uses the caller's buffer.
1986 *
1987 * Note that duplicate keys are allowed, but only check within the
1988 * current leaf node. The Btree code must check in adjacent leaf nodes.
1989 *
1990 * Return in args->index the index into the entry[] array of either
1991 * the found entry, or where the entry should have been (insert before
1992 * that entry).
1993 *
1994 * Don't change the args->value unless we find the attribute.
1995 */
1996int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001997xfs_attr_leaf_lookup_int(
1998 struct xfs_buf *bp,
1999 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 xfs_attr_leafblock_t *leaf;
2002 xfs_attr_leaf_entry_t *entry;
2003 xfs_attr_leaf_name_local_t *name_loc;
2004 xfs_attr_leaf_name_remote_t *name_rmt;
2005 int probe, span;
2006 xfs_dahash_t hashval;
2007
Dave Chinner5a5881c2012-03-22 05:15:13 +00002008 trace_xfs_attr_leaf_lookup(args);
2009
Dave Chinner1d9025e2012-06-22 18:50:14 +10002010 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002011 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002012 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 < (XFS_LBSIZE(args->dp->i_mount)/8));
2014
2015 /*
2016 * Binary search. (note: small blocks will skip this loop)
2017 */
2018 hashval = args->hashval;
Nathan Scott918ae422006-03-17 17:28:54 +11002019 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 for (entry = &leaf->entries[probe]; span > 4;
2021 entry = &leaf->entries[probe]) {
2022 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002023 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002025 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 probe -= span;
2027 else
2028 break;
2029 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002030 ASSERT((probe >= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 (!leaf->hdr.count
Nathan Scott918ae422006-03-17 17:28:54 +11002032 || (probe < be16_to_cpu(leaf->hdr.count))));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002033 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 /*
2036 * Since we may have duplicate hashval's, find the first matching
2037 * hashval in the leaf.
2038 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002039 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 entry--;
2041 probe--;
2042 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002043 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
2044 (be32_to_cpu(entry->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 entry++;
2046 probe++;
2047 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002048 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
2049 (be32_to_cpu(entry->hashval) != hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 args->index = probe;
2051 return(XFS_ERROR(ENOATTR));
2052 }
2053
2054 /*
2055 * Duplicate keys may be present, so search all of them for a match.
2056 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002057 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
2058 (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 entry++, probe++) {
2060/*
2061 * GROT: Add code to remove incomplete entries.
2062 */
2063 /*
2064 * If we are looking for INCOMPLETE entries, show only those.
2065 * If we are looking for complete entries, show only those.
2066 */
2067 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2068 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2069 continue;
2070 }
2071 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002072 name_loc = xfs_attr_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (name_loc->namelen != args->namelen)
2074 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002075 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002077 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 continue;
2079 args->index = probe;
2080 return(XFS_ERROR(EEXIST));
2081 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002082 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (name_rmt->namelen != args->namelen)
2084 continue;
2085 if (memcmp(args->name, (char *)name_rmt->name,
2086 args->namelen) != 0)
2087 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002088 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 continue;
2090 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002091 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002093 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return(XFS_ERROR(EEXIST));
2095 }
2096 }
2097 args->index = probe;
2098 return(XFS_ERROR(ENOATTR));
2099}
2100
2101/*
2102 * Get the value associated with an attribute name from a leaf attribute
2103 * list structure.
2104 */
2105int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002106xfs_attr_leaf_getvalue(
2107 struct xfs_buf *bp,
2108 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 int valuelen;
2111 xfs_attr_leafblock_t *leaf;
2112 xfs_attr_leaf_entry_t *entry;
2113 xfs_attr_leaf_name_local_t *name_loc;
2114 xfs_attr_leaf_name_remote_t *name_rmt;
2115
Dave Chinner1d9025e2012-06-22 18:50:14 +10002116 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002117 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002118 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 < (XFS_LBSIZE(args->dp->i_mount)/8));
Nathan Scott918ae422006-03-17 17:28:54 +11002120 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 entry = &leaf->entries[args->index];
2123 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002124 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 ASSERT(name_loc->namelen == args->namelen);
2126 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002127 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (args->flags & ATTR_KERNOVAL) {
2129 args->valuelen = valuelen;
2130 return(0);
2131 }
2132 if (args->valuelen < valuelen) {
2133 args->valuelen = valuelen;
2134 return(XFS_ERROR(ERANGE));
2135 }
2136 args->valuelen = valuelen;
2137 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2138 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002139 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 ASSERT(name_rmt->namelen == args->namelen);
2141 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002142 valuelen = be32_to_cpu(name_rmt->valuelen);
2143 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2145 if (args->flags & ATTR_KERNOVAL) {
2146 args->valuelen = valuelen;
2147 return(0);
2148 }
2149 if (args->valuelen < valuelen) {
2150 args->valuelen = valuelen;
2151 return(XFS_ERROR(ERANGE));
2152 }
2153 args->valuelen = valuelen;
2154 }
2155 return(0);
2156}
2157
2158/*========================================================================
2159 * Utility routines.
2160 *========================================================================*/
2161
2162/*
2163 * Move the indicated entries from one leaf to another.
2164 * NOTE: this routine modifies both source and destination leaves.
2165 */
2166/*ARGSUSED*/
2167STATIC void
2168xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2169 xfs_attr_leafblock_t *leaf_d, int start_d,
2170 int count, xfs_mount_t *mp)
2171{
2172 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2173 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2174 int desti, tmp, i;
2175
2176 /*
2177 * Check for nothing to do.
2178 */
2179 if (count == 0)
2180 return;
2181
2182 /*
2183 * Set up environment.
2184 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002185 ASSERT(leaf_s->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2186 ASSERT(leaf_d->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 hdr_s = &leaf_s->hdr;
2188 hdr_d = &leaf_d->hdr;
Nathan Scott918ae422006-03-17 17:28:54 +11002189 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2190 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2191 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2192 ((be16_to_cpu(hdr_s->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 * sizeof(*entry_s))+sizeof(*hdr_s)));
Nathan Scott918ae422006-03-17 17:28:54 +11002194 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2195 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2196 ((be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 * sizeof(*entry_d))+sizeof(*hdr_d)));
2198
Nathan Scott918ae422006-03-17 17:28:54 +11002199 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2200 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2201 ASSERT(count <= be16_to_cpu(hdr_s->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /*
2204 * Move the entries in the destination leaf up to make a hole?
2205 */
Nathan Scott918ae422006-03-17 17:28:54 +11002206 if (start_d < be16_to_cpu(hdr_d->count)) {
2207 tmp = be16_to_cpu(hdr_d->count) - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 tmp *= sizeof(xfs_attr_leaf_entry_t);
2209 entry_s = &leaf_d->entries[start_d];
2210 entry_d = &leaf_d->entries[start_d + count];
2211 memmove((char *)entry_d, (char *)entry_s, tmp);
2212 }
2213
2214 /*
2215 * Copy all entry's in the same (sorted) order,
2216 * but allocate attribute info packed and in sequence.
2217 */
2218 entry_s = &leaf_s->entries[start_s];
2219 entry_d = &leaf_d->entries[start_d];
2220 desti = start_d;
2221 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002222 ASSERT(be16_to_cpu(entry_s->nameidx)
Nathan Scott918ae422006-03-17 17:28:54 +11002223 >= be16_to_cpu(hdr_s->firstused));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2225#ifdef GROT
2226 /*
2227 * Code to drop INCOMPLETE entries. Difficult to use as we
2228 * may also need to change the insertion index. Code turned
2229 * off for 6.2, should be revisited later.
2230 */
2231 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002232 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002233 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2234 be16_add_cpu(&hdr_s->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 entry_d--; /* to compensate for ++ in loop hdr */
2236 desti--;
2237 if ((start_s + i) < offset)
2238 result++; /* insertion index adjustment */
2239 } else {
2240#endif /* GROT */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002241 be16_add_cpu(&hdr_d->firstused, -tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 /* both on-disk, don't endian flip twice */
2243 entry_d->hashval = entry_s->hashval;
2244 /* both on-disk, don't endian flip twice */
2245 entry_d->nameidx = hdr_d->firstused;
2246 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002247 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002249 memmove(xfs_attr_leaf_name(leaf_d, desti),
2250 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002251 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002253 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002254 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2255 be16_add_cpu(&hdr_d->usedbytes, tmp);
2256 be16_add_cpu(&hdr_s->count, -1);
2257 be16_add_cpu(&hdr_d->count, 1);
Nathan Scott918ae422006-03-17 17:28:54 +11002258 tmp = be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 * sizeof(xfs_attr_leaf_entry_t)
2260 + sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002261 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262#ifdef GROT
2263 }
2264#endif /* GROT */
2265 }
2266
2267 /*
2268 * Zero out the entries we just copied.
2269 */
Nathan Scott918ae422006-03-17 17:28:54 +11002270 if (start_s == be16_to_cpu(hdr_s->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2272 entry_s = &leaf_s->entries[start_s];
2273 ASSERT(((char *)entry_s + tmp) <=
2274 ((char *)leaf_s + XFS_LBSIZE(mp)));
2275 memset((char *)entry_s, 0, tmp);
2276 } else {
2277 /*
2278 * Move the remaining entries down to fill the hole,
2279 * then zero the entries at the top.
2280 */
Nathan Scott918ae422006-03-17 17:28:54 +11002281 tmp = be16_to_cpu(hdr_s->count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 tmp *= sizeof(xfs_attr_leaf_entry_t);
2283 entry_s = &leaf_s->entries[start_s + count];
2284 entry_d = &leaf_s->entries[start_s];
2285 memmove((char *)entry_d, (char *)entry_s, tmp);
2286
2287 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002288 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 ASSERT(((char *)entry_s + tmp) <=
2290 ((char *)leaf_s + XFS_LBSIZE(mp)));
2291 memset((char *)entry_s, 0, tmp);
2292 }
2293
2294 /*
2295 * Fill in the freemap information
2296 */
Nathan Scott918ae422006-03-17 17:28:54 +11002297 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002298 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
Nathan Scott918ae422006-03-17 17:28:54 +11002299 sizeof(xfs_attr_leaf_entry_t));
2300 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2301 - be16_to_cpu(hdr_d->freemap[0].base));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 hdr_d->freemap[1].base = 0;
2303 hdr_d->freemap[2].base = 0;
2304 hdr_d->freemap[1].size = 0;
2305 hdr_d->freemap[2].size = 0;
2306 hdr_s->holes = 1; /* leaf may not be compact */
2307}
2308
2309/*
2310 * Compare two leaf blocks "order".
2311 * Return 0 unless leaf2 should go before leaf1.
2312 */
2313int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002314xfs_attr_leaf_order(
2315 struct xfs_buf *leaf1_bp,
2316 struct xfs_buf *leaf2_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
2318 xfs_attr_leafblock_t *leaf1, *leaf2;
2319
Dave Chinner1d9025e2012-06-22 18:50:14 +10002320 leaf1 = leaf1_bp->b_addr;
2321 leaf2 = leaf2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002322 ASSERT((leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) &&
2323 (leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)));
Nathan Scott918ae422006-03-17 17:28:54 +11002324 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2325 (be16_to_cpu(leaf2->hdr.count) > 0) &&
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002326 ((be32_to_cpu(leaf2->entries[0].hashval) <
2327 be32_to_cpu(leaf1->entries[0].hashval)) ||
2328 (be32_to_cpu(leaf2->entries[
2329 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2330 be32_to_cpu(leaf1->entries[
2331 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return(1);
2333 }
2334 return(0);
2335}
2336
2337/*
2338 * Pick up the last hashvalue from a leaf block.
2339 */
2340xfs_dahash_t
Dave Chinner1d9025e2012-06-22 18:50:14 +10002341xfs_attr_leaf_lasthash(
2342 struct xfs_buf *bp,
2343 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 xfs_attr_leafblock_t *leaf;
2346
Dave Chinner1d9025e2012-06-22 18:50:14 +10002347 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002348 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (count)
Nathan Scott918ae422006-03-17 17:28:54 +11002350 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (!leaf->hdr.count)
2352 return(0);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002353 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
2356/*
2357 * Calculate the number of bytes used to store the indicated attribute
2358 * (whether local or remote only calculate bytes in this block).
2359 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002360STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2362{
2363 xfs_attr_leaf_name_local_t *name_loc;
2364 xfs_attr_leaf_name_remote_t *name_rmt;
2365 int size;
2366
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002367 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002369 name_loc = xfs_attr_leaf_name_local(leaf, index);
2370 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002371 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002373 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2374 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376 return(size);
2377}
2378
2379/*
2380 * Calculate the number of bytes that would be required to store the new
2381 * attribute (whether local or remote only calculate bytes in this block).
2382 * This routine decides as a side effect whether the attribute will be
2383 * a "local" or a "remote" attribute.
2384 */
2385int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002386xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388 int size;
2389
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002390 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2391 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (local) {
2393 *local = 1;
2394 }
2395 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002396 size = xfs_attr_leaf_entsize_remote(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (local) {
2398 *local = 0;
2399 }
2400 }
2401 return(size);
2402}
2403
2404/*
2405 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2406 */
2407int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002408xfs_attr_leaf_list_int(
2409 struct xfs_buf *bp,
2410 xfs_attr_list_context_t *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
2412 attrlist_cursor_kern_t *cursor;
2413 xfs_attr_leafblock_t *leaf;
2414 xfs_attr_leaf_entry_t *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 int retval, i;
2416
2417 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002418 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 cursor = context->cursor;
2420 cursor->initted = 1;
2421
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002422 trace_xfs_attr_list_leaf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 /*
2425 * Re-find our place in the leaf block if this is a new syscall.
2426 */
2427 if (context->resynch) {
2428 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002429 for (i = 0; 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (cursor->offset == context->dupcnt) {
2432 context->dupcnt = 0;
2433 break;
2434 }
2435 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002436 } else if (be32_to_cpu(entry->hashval) >
2437 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 context->dupcnt = 0;
2439 break;
2440 }
2441 }
Nathan Scott918ae422006-03-17 17:28:54 +11002442 if (i == be16_to_cpu(leaf->hdr.count)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002443 trace_xfs_attr_list_notfound(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return(0);
2445 }
2446 } else {
2447 entry = &leaf->entries[0];
2448 i = 0;
2449 }
2450 context->resynch = 0;
2451
2452 /*
2453 * We have found our place, start copying out the new attributes.
2454 */
2455 retval = 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10002456 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002457 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2458 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 cursor->offset = 0;
2460 }
2461
2462 if (entry->flags & XFS_ATTR_INCOMPLETE)
2463 continue; /* skip incomplete entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002466 xfs_attr_leaf_name_local_t *name_loc =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002467 xfs_attr_leaf_name_local(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002468
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_loc->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +10002472 (int)name_loc->namelen,
2473 be16_to_cpu(name_loc->valuelen),
Dave Chinnera9273ca2010-01-20 10:47:48 +11002474 &name_loc->nameval[name_loc->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +10002475 if (retval)
2476 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002478 xfs_attr_leaf_name_remote_t *name_rmt =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002479 xfs_attr_leaf_name_remote(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002480
2481 int valuelen = be32_to_cpu(name_rmt->valuelen);
2482
2483 if (context->put_value) {
2484 xfs_da_args_t args;
2485
2486 memset((char *)&args, 0, sizeof(args));
2487 args.dp = context->dp;
2488 args.whichfork = XFS_ATTR_FORK;
2489 args.valuelen = valuelen;
Dave Chinner622d8142010-12-23 11:57:37 +11002490 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
Tim Shimmin726801b2006-09-28 11:01:37 +10002491 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2492 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2493 retval = xfs_attr_rmtval_get(&args);
2494 if (retval)
2495 return retval;
2496 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002497 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002498 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002499 (int)name_rmt->namelen,
2500 valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002501 args.value);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002502 kmem_free(args.value);
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002503 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002504 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002505 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002506 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002507 (int)name_rmt->namelen,
2508 valuelen,
2509 NULL);
2510 }
2511 if (retval)
2512 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002514 if (context->seen_enough)
2515 break;
2516 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002518 trace_xfs_attr_list_leaf_end(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return(retval);
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523/*========================================================================
2524 * Manage the INCOMPLETE flag in a leaf entry
2525 *========================================================================*/
2526
2527/*
2528 * Clear the INCOMPLETE flag on an entry in a leaf block.
2529 */
2530int
2531xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2532{
2533 xfs_attr_leafblock_t *leaf;
2534 xfs_attr_leaf_entry_t *entry;
2535 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002536 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 int error;
2538#ifdef DEBUG
2539 xfs_attr_leaf_name_local_t *name_loc;
2540 int namelen;
2541 char *name;
2542#endif /* DEBUG */
2543
Dave Chinner5a5881c2012-03-22 05:15:13 +00002544 trace_xfs_attr_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /*
2546 * Set up the operation.
2547 */
Dave Chinnerad14c332012-11-12 22:54:16 +11002548 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2549 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Dave Chinner1d9025e2012-06-22 18:50:14 +10002552 leaf = bp->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002553 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 ASSERT(args->index >= 0);
2555 entry = &leaf->entries[ args->index ];
2556 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2557
2558#ifdef DEBUG
2559 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002560 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 namelen = name_loc->namelen;
2562 name = (char *)name_loc->nameval;
2563 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002564 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 namelen = name_rmt->namelen;
2566 name = (char *)name_rmt->name;
2567 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002568 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 ASSERT(namelen == args->namelen);
2570 ASSERT(memcmp(name, args->name, namelen) == 0);
2571#endif /* DEBUG */
2572
2573 entry->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002574 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2576
2577 if (args->rmtblkno) {
2578 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002579 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002580 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2581 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002582 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 /*
2587 * Commit the flag value change and start the next trans in series.
2588 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002589 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
2592/*
2593 * Set the INCOMPLETE flag on an entry in a leaf block.
2594 */
2595int
2596xfs_attr_leaf_setflag(xfs_da_args_t *args)
2597{
2598 xfs_attr_leafblock_t *leaf;
2599 xfs_attr_leaf_entry_t *entry;
2600 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002601 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 int error;
2603
Dave Chinner5a5881c2012-03-22 05:15:13 +00002604 trace_xfs_attr_leaf_setflag(args);
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 /*
2607 * Set up the operation.
2608 */
Dave Chinnerad14c332012-11-12 22:54:16 +11002609 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2610 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Dave Chinner1d9025e2012-06-22 18:50:14 +10002613 leaf = bp->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002614 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 ASSERT(args->index >= 0);
2616 entry = &leaf->entries[ args->index ];
2617
2618 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2619 entry->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002620 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2622 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002623 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 name_rmt->valueblk = 0;
2625 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002626 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 /*
2631 * Commit the flag value change and start the next trans in series.
2632 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002633 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
2636/*
2637 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2638 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2639 * entry given by args->blkno2/index2.
2640 *
2641 * Note that they could be in different blocks, or in the same block.
2642 */
2643int
2644xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2645{
2646 xfs_attr_leafblock_t *leaf1, *leaf2;
2647 xfs_attr_leaf_entry_t *entry1, *entry2;
2648 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002649 struct xfs_buf *bp1, *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 int error;
2651#ifdef DEBUG
2652 xfs_attr_leaf_name_local_t *name_loc;
2653 int namelen1, namelen2;
2654 char *name1, *name2;
2655#endif /* DEBUG */
2656
Dave Chinner5a5881c2012-03-22 05:15:13 +00002657 trace_xfs_attr_leaf_flipflags(args);
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 /*
2660 * Read the block containing the "old" attr
2661 */
Dave Chinnerad14c332012-11-12 22:54:16 +11002662 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
2663 if (error)
2664 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 /*
2667 * Read the block containing the "new" attr, if it is different
2668 */
2669 if (args->blkno2 != args->blkno) {
Dave Chinnerad14c332012-11-12 22:54:16 +11002670 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno2,
2671 -1, &bp2);
2672 if (error)
2673 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 } else {
2675 bp2 = bp1;
2676 }
2677
Dave Chinner1d9025e2012-06-22 18:50:14 +10002678 leaf1 = bp1->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002679 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 ASSERT(args->index >= 0);
2681 entry1 = &leaf1->entries[ args->index ];
2682
Dave Chinner1d9025e2012-06-22 18:50:14 +10002683 leaf2 = bp2->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002684 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 ASSERT(args->index2 >= 0);
2686 entry2 = &leaf2->entries[ args->index2 ];
2687
2688#ifdef DEBUG
2689 if (entry1->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002690 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 namelen1 = name_loc->namelen;
2692 name1 = (char *)name_loc->nameval;
2693 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002694 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 namelen1 = name_rmt->namelen;
2696 name1 = (char *)name_rmt->name;
2697 }
2698 if (entry2->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002699 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 namelen2 = name_loc->namelen;
2701 name2 = (char *)name_loc->nameval;
2702 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002703 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 namelen2 = name_rmt->namelen;
2705 name2 = (char *)name_rmt->name;
2706 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002707 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 ASSERT(namelen1 == namelen2);
2709 ASSERT(memcmp(name1, name2, namelen1) == 0);
2710#endif /* DEBUG */
2711
2712 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2713 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2714
2715 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002716 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2718 if (args->rmtblkno) {
2719 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002720 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002721 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2722 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002723 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2725 }
2726
2727 entry2->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002728 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2730 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002731 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 name_rmt->valueblk = 0;
2733 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002734 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 /*
2739 * Commit the flag value change and start the next trans in series.
2740 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002741 error = xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743 return(error);
2744}
2745
2746/*========================================================================
2747 * Indiscriminately delete the entire attribute fork
2748 *========================================================================*/
2749
2750/*
2751 * Recurse (gasp!) through the attribute nodes until we find leaves.
2752 * We're doing a depth-first traversal in order to invalidate everything.
2753 */
2754int
2755xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2756{
2757 xfs_da_blkinfo_t *info;
2758 xfs_daddr_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002759 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 int error;
2761
2762 /*
2763 * Read block 0 to see what we have to work with.
2764 * We only get here if we have extents, since we remove
2765 * the extents in reverse order the extent containing
2766 * block 0 must still be there.
2767 */
Dave Chinnerd9392a42012-11-12 22:54:17 +11002768 error = xfs_da_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (error)
2770 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002771 blkno = XFS_BUF_ADDR(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 /*
2774 * Invalidate the tree, even if the "tree" is only a single leaf block.
2775 * This is a depth-first traversal!
2776 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002777 info = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002778 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 error = xfs_attr_node_inactive(trans, dp, bp, 1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002780 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 error = xfs_attr_leaf_inactive(trans, dp, bp);
2782 } else {
2783 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002784 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786 if (error)
2787 return(error);
2788
2789 /*
2790 * Invalidate the incore copy of the root block.
2791 */
2792 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2793 if (error)
2794 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002795 xfs_trans_binval(*trans, bp); /* remove from cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 /*
2797 * Commit the invalidate and start the next transaction.
2798 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002799 error = xfs_trans_roll(trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 return (error);
2802}
2803
2804/*
2805 * Recurse (gasp!) through the attribute nodes until we find leaves.
2806 * We're doing a depth-first traversal in order to invalidate everything.
2807 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002808STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002809xfs_attr_node_inactive(
2810 struct xfs_trans **trans,
2811 struct xfs_inode *dp,
2812 struct xfs_buf *bp,
2813 int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
2815 xfs_da_blkinfo_t *info;
2816 xfs_da_intnode_t *node;
2817 xfs_dablk_t child_fsb;
2818 xfs_daddr_t parent_blkno, child_blkno;
2819 int error, count, i;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002820 struct xfs_buf *child_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 /*
2823 * Since this code is recursive (gasp!) we must protect ourselves.
2824 */
2825 if (level > XFS_DA_NODE_MAXDEPTH) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002826 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return(XFS_ERROR(EIO));
2828 }
2829
Dave Chinner1d9025e2012-06-22 18:50:14 +10002830 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002831 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +10002832 parent_blkno = XFS_BUF_ADDR(bp); /* save for re-read later */
Nathan Scottfac80cc2006-03-17 17:29:56 +11002833 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (!count) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002835 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return(0);
2837 }
Nathan Scott403432d2006-03-17 17:29:46 +11002838 child_fsb = be32_to_cpu(node->btree[0].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002839 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 /*
2842 * If this is the node level just above the leaves, simply loop
2843 * over the leaves removing all of them. If this is higher up
2844 * in the tree, recurse downward.
2845 */
2846 for (i = 0; i < count; i++) {
2847 /*
2848 * Read the subsidiary block to see what we have to work with.
2849 * Don't do this in a transaction. This is a depth-first
2850 * traversal of the tree so we may deal with many blocks
2851 * before we come back to this one.
2852 */
Dave Chinnerd9392a42012-11-12 22:54:17 +11002853 error = xfs_da_node_read(*trans, dp, child_fsb, -2, &child_bp,
2854 XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 if (error)
2856 return(error);
2857 if (child_bp) {
2858 /* save for re-read later */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002859 child_blkno = XFS_BUF_ADDR(child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
2861 /*
2862 * Invalidate the subtree, however we have to.
2863 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002864 info = child_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002865 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 error = xfs_attr_node_inactive(trans, dp,
2867 child_bp, level+1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002868 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 error = xfs_attr_leaf_inactive(trans, dp,
2870 child_bp);
2871 } else {
2872 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002873 xfs_trans_brelse(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
2875 if (error)
2876 return(error);
2877
2878 /*
2879 * Remove the subsidiary block from the cache
2880 * and from the log.
2881 */
2882 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2883 &child_bp, XFS_ATTR_FORK);
2884 if (error)
2885 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002886 xfs_trans_binval(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888
2889 /*
2890 * If we're not done, re-read the parent to get the next
2891 * child block number.
2892 */
2893 if ((i+1) < count) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11002894 error = xfs_da_node_read(*trans, dp, 0, parent_blkno,
2895 &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (error)
2897 return(error);
Nathan Scott403432d2006-03-17 17:29:46 +11002898 child_fsb = be32_to_cpu(node->btree[i+1].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002899 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
2901 /*
2902 * Atomically commit the whole invalidate stuff.
2903 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002904 error = xfs_trans_roll(trans, dp);
2905 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return (error);
2907 }
2908
2909 return(0);
2910}
2911
2912/*
2913 * Invalidate all of the "remote" value regions pointed to by a particular
2914 * leaf block.
2915 * Note that we must release the lock on the buffer so that we are not
2916 * caught holding something that the logging code wants to flush to disk.
2917 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002918STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002919xfs_attr_leaf_inactive(
2920 struct xfs_trans **trans,
2921 struct xfs_inode *dp,
2922 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 xfs_attr_leafblock_t *leaf;
2925 xfs_attr_leaf_entry_t *entry;
2926 xfs_attr_leaf_name_remote_t *name_rmt;
2927 xfs_attr_inactive_list_t *list, *lp;
2928 int error, count, size, tmp, i;
2929
Dave Chinner1d9025e2012-06-22 18:50:14 +10002930 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002931 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 /*
2934 * Count the number of "remote" value extents.
2935 */
2936 count = 0;
2937 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002938 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002939 if (be16_to_cpu(entry->nameidx) &&
2940 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002941 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if (name_rmt->valueblk)
2943 count++;
2944 }
2945 }
2946
2947 /*
2948 * If there are no "remote" values, we're done.
2949 */
2950 if (count == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002951 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 return(0);
2953 }
2954
2955 /*
2956 * Allocate storage for a list of all the "remote" value extents.
2957 */
2958 size = count * sizeof(xfs_attr_inactive_list_t);
2959 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2960
2961 /*
2962 * Identify each of the "remote" value extents.
2963 */
2964 lp = list;
2965 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002966 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002967 if (be16_to_cpu(entry->nameidx) &&
2968 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002969 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11002971 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2972 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2973 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 lp++;
2975 }
2976 }
2977 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002978 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980 /*
2981 * Invalidate each of the "remote" value extents.
2982 */
2983 error = 0;
2984 for (lp = list, i = 0; i < count; i++, lp++) {
2985 tmp = xfs_attr_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11002986 lp->valueblk, lp->valuelen);
2987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (error == 0)
2989 error = tmp; /* save only the 1st errno */
2990 }
2991
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002992 kmem_free((xfs_caddr_t)list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 return(error);
2994}
2995
2996/*
2997 * Look at all the extents for this logical region,
2998 * invalidate any buffers that are incore/in transactions.
2999 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003000STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
3002 xfs_dablk_t blkno, int blkcnt)
3003{
3004 xfs_bmbt_irec_t map;
3005 xfs_dablk_t tblkno;
3006 int tblkcnt, dblkcnt, nmap, error;
3007 xfs_daddr_t dblkno;
3008 xfs_buf_t *bp;
3009
3010 /*
3011 * Roll through the "value", invalidating the attribute value's
3012 * blocks.
3013 */
3014 tblkno = blkno;
3015 tblkcnt = blkcnt;
3016 while (tblkcnt > 0) {
3017 /*
3018 * Try to remember where we decided to put the value.
3019 */
3020 nmap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003021 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
3022 &map, &nmap, XFS_BMAPI_ATTRFORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if (error) {
3024 return(error);
3025 }
3026 ASSERT(nmap == 1);
3027 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3028
3029 /*
3030 * If it's a hole, these are already unmapped
3031 * so there's nothing to invalidate.
3032 */
3033 if (map.br_startblock != HOLESTARTBLOCK) {
3034
3035 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3036 map.br_startblock);
3037 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3038 map.br_blockcount);
3039 bp = xfs_trans_get_buf(*trans,
3040 dp->i_mount->m_ddev_targp,
Dave Chinnera8acad72012-04-23 15:58:54 +10003041 dblkno, dblkcnt, 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00003042 if (!bp)
3043 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 xfs_trans_binval(*trans, bp);
3045 /*
3046 * Roll to next transaction.
3047 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003048 error = xfs_trans_roll(trans, dp);
3049 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return (error);
3051 }
3052
3053 tblkno += map.br_blockcount;
3054 tblkcnt -= map.br_blockcount;
3055 }
3056
3057 return(0);
3058}