blob: aa4765f15cbedd4ddbe60dcb431dfd66a63dcb09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_ialloc_btree.h"
31#include "xfs_alloc.h"
32#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_attr.h"
39#include "xfs_attr_leaf.h"
40#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000041#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * xfs_attr_leaf.c
45 *
46 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
47 */
48
49/*========================================================================
50 * Function prototypes for the kernel.
51 *========================================================================*/
52
53/*
54 * Routines used for growing the Btree.
55 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100056STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
Dave Chinner1d9025e2012-06-22 18:50:14 +100057 struct xfs_buf **bpp);
58STATIC int xfs_attr_leaf_add_work(struct xfs_buf *leaf_buffer,
59 xfs_da_args_t *args, int freemap_index);
Dave Chinneree732592012-11-12 22:53:53 +110060STATIC void xfs_attr_leaf_compact(struct xfs_da_args *args,
61 struct xfs_buf *leaf_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
63 xfs_da_state_blk_t *blk1,
64 xfs_da_state_blk_t *blk2);
65STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
66 xfs_da_state_blk_t *leaf_blk_1,
67 xfs_da_state_blk_t *leaf_blk_2,
68 int *number_entries_in_blk1,
69 int *number_usedbytes_in_blk1);
70
71/*
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100072 * Routines used for shrinking the Btree.
73 */
74STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +100075 struct xfs_buf *bp, int level);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100076STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +100077 struct xfs_buf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100078STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
79 xfs_dablk_t blkno, int blkcnt);
80
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Utility routines.
83 */
84STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
85 int src_start,
86 xfs_attr_leafblock_t *dst_leaf,
87 int dst_start, int move_count,
88 xfs_mount_t *mp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100089STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
Tim Shimmin726801b2006-09-28 11:01:37 +100090
Dave Chinner612cfbf2012-11-14 17:52:32 +110091static void
Dave Chinnerad14c332012-11-12 22:54:16 +110092xfs_attr_leaf_verify(
93 struct xfs_buf *bp)
94{
95 struct xfs_mount *mp = bp->b_target->bt_mount;
96 struct xfs_attr_leaf_hdr *hdr = bp->b_addr;
97 int block_ok = 0;
98
99 block_ok = hdr->info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
100 if (!block_ok) {
101 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
102 xfs_buf_ioerror(bp, EFSCORRUPTED);
103 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100104}
Dave Chinnerad14c332012-11-12 22:54:16 +1100105
Dave Chinner612cfbf2012-11-14 17:52:32 +1100106static void
Dave Chinner1813dd62012-11-14 17:54:40 +1100107xfs_attr_leaf_read_verify(
108 struct xfs_buf *bp)
109{
110 xfs_attr_leaf_verify(bp);
111}
112
113static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100114xfs_attr_leaf_write_verify(
115 struct xfs_buf *bp)
116{
117 xfs_attr_leaf_verify(bp);
118}
119
Dave Chinner1813dd62012-11-14 17:54:40 +1100120const struct xfs_buf_ops xfs_attr_leaf_buf_ops = {
121 .verify_read = xfs_attr_leaf_read_verify,
122 .verify_write = xfs_attr_leaf_write_verify,
123};
Dave Chinner612cfbf2012-11-14 17:52:32 +1100124
Dave Chinnerad14c332012-11-12 22:54:16 +1100125int
126xfs_attr_leaf_read(
127 struct xfs_trans *tp,
128 struct xfs_inode *dp,
129 xfs_dablk_t bno,
130 xfs_daddr_t mappedbno,
131 struct xfs_buf **bpp)
132{
133 return xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100134 XFS_ATTR_FORK, &xfs_attr_leaf_buf_ops);
Dave Chinnerad14c332012-11-12 22:54:16 +1100135}
136
Tim Shimmin726801b2006-09-28 11:01:37 +1000137/*========================================================================
138 * Namespace helper routines
139 *========================================================================*/
140
Tim Shimmin726801b2006-09-28 11:01:37 +1000141/*
142 * If namespace bits don't match return 0.
143 * If all match then return 1.
144 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000145STATIC int
Tim Shimmin726801b2006-09-28 11:01:37 +1000146xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
147{
148 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100153 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *========================================================================*/
155
156/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100157 * Query whether the requested number of additional bytes of extended
158 * attribute space will be able to fit inline.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000159 *
Nathan Scottd8cc8902005-11-02 10:34:53 +1100160 * Returns zero if not, else the di_forkoff fork offset to be used in the
161 * literal area for attribute data once the new bytes have been added.
162 *
163 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
164 * special case for dev/uuid inodes, they have fixed size data forks.
165 */
166int
167xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
168{
169 int offset;
170 int minforkoff; /* lower limit on valid forkoff locations */
171 int maxforkoff; /* upper limit on valid forkoff locations */
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000172 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100173 xfs_mount_t *mp = dp->i_mount;
174
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100175 /* rounded down */
176 offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100177
178 switch (dp->i_d.di_format) {
179 case XFS_DINODE_FMT_DEV:
180 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
181 return (offset >= minforkoff) ? minforkoff : 0;
182 case XFS_DINODE_FMT_UUID:
183 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
184 return (offset >= minforkoff) ? minforkoff : 0;
185 }
186
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000187 /*
188 * If the requested numbers of bytes is smaller or equal to the
189 * current attribute fork size we can always proceed.
190 *
191 * Note that if_bytes in the data fork might actually be larger than
192 * the current data fork size is due to delalloc extents. In that
193 * case either the extent count will go down when they are converted
194 * to real extents, or the delalloc conversion will take care of the
195 * literal area rebalancing.
196 */
197 if (bytes <= XFS_IFORK_ASIZE(dp))
198 return dp->i_d.di_forkoff;
199
200 /*
201 * For attr2 we can try to move the forkoff if there is space in the
202 * literal area, but for the old format we are done if there is no
203 * space in the fixed attribute fork.
204 */
205 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
Nathan Scottda087ba2005-11-02 15:00:20 +1100206 return 0;
Nathan Scottda087ba2005-11-02 15:00:20 +1100207
Barry Naujoke5889e92007-02-10 18:35:58 +1100208 dsize = dp->i_df.if_bytes;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000209
Barry Naujoke5889e92007-02-10 18:35:58 +1100210 switch (dp->i_d.di_format) {
211 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000212 /*
Barry Naujoke5889e92007-02-10 18:35:58 +1100213 * If there is no attr fork and the data fork is extents,
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000214 * determine if creating the default attr fork will result
215 * in the extents form migrating to btree. If so, the
216 * minimum offset only needs to be the space required for
Barry Naujoke5889e92007-02-10 18:35:58 +1100217 * the btree root.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000218 */
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200219 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
220 xfs_default_attroffset(dp))
Barry Naujoke5889e92007-02-10 18:35:58 +1100221 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
222 break;
Barry Naujoke5889e92007-02-10 18:35:58 +1100223 case XFS_DINODE_FMT_BTREE:
224 /*
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000225 * If we have a data btree then keep forkoff if we have one,
226 * otherwise we are adding a new attr, so then we set
227 * minforkoff to where the btree root can finish so we have
Barry Naujoke5889e92007-02-10 18:35:58 +1100228 * plenty of room for attrs
229 */
230 if (dp->i_d.di_forkoff) {
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000231 if (offset < dp->i_d.di_forkoff)
Barry Naujoke5889e92007-02-10 18:35:58 +1100232 return 0;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000233 return dp->i_d.di_forkoff;
234 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500235 dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
Barry Naujoke5889e92007-02-10 18:35:58 +1100236 break;
237 }
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000238
239 /*
240 * A data fork btree root must have space for at least
Barry Naujoke5889e92007-02-10 18:35:58 +1100241 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
242 */
243 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100244 minforkoff = roundup(minforkoff, 8) >> 3;
245
246 /* attr fork btree root can have at least this many key/ptr pairs */
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100247 maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
248 XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100249 maxforkoff = maxforkoff >> 3; /* rounded down */
250
Nathan Scottd8cc8902005-11-02 10:34:53 +1100251 if (offset >= maxforkoff)
252 return maxforkoff;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000253 if (offset >= minforkoff)
254 return offset;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100255 return 0;
256}
257
258/*
259 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
260 */
261STATIC void
262xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
263{
Nathan Scott13059ff2006-01-11 15:32:01 +1100264 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100265 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000266 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100267 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
268 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000269 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100270 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
271 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000272 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100273 }
274}
275
276/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * Create the initial contents of a shortform attribute list.
278 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100279void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280xfs_attr_shortform_create(xfs_da_args_t *args)
281{
282 xfs_attr_sf_hdr_t *hdr;
283 xfs_inode_t *dp;
284 xfs_ifork_t *ifp;
285
Dave Chinner5a5881c2012-03-22 05:15:13 +0000286 trace_xfs_attr_sf_create(args);
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 dp = args->dp;
289 ASSERT(dp != NULL);
290 ifp = dp->i_afp;
291 ASSERT(ifp != NULL);
292 ASSERT(ifp->if_bytes == 0);
293 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
294 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
295 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
296 ifp->if_flags |= XFS_IFINLINE;
297 } else {
298 ASSERT(ifp->if_flags & XFS_IFINLINE);
299 }
300 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
301 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
302 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100303 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/*
308 * Add a name/value pair to the shortform attribute list.
309 * Overflow from the inode has already been checked for.
310 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100311void
312xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 xfs_attr_shortform_t *sf;
315 xfs_attr_sf_entry_t *sfe;
316 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100317 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 xfs_inode_t *dp;
319 xfs_ifork_t *ifp;
320
Dave Chinner5a5881c2012-03-22 05:15:13 +0000321 trace_xfs_attr_sf_add(args);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100324 mp = dp->i_mount;
325 dp->i_d.di_forkoff = forkoff;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 ifp = dp->i_afp;
328 ASSERT(ifp->if_flags & XFS_IFINLINE);
329 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
330 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100331 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100332#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (sfe->namelen != args->namelen)
334 continue;
335 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
336 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000337 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100339 ASSERT(0);
340#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 offset = (char *)sfe - (char *)sf;
344 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
345 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
346 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
347 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
348
349 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100350 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000351 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 memcpy(sfe->nameval, args->name, args->namelen);
353 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100354 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800355 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
357
Nathan Scottd8cc8902005-11-02 10:34:53 +1100358 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361/*
Christoph Hellwige1486de2009-02-04 09:36:00 +0100362 * After the last attribute is removed revert to original inode format,
363 * making all literal area available to the data fork once more.
364 */
365STATIC void
366xfs_attr_fork_reset(
367 struct xfs_inode *ip,
368 struct xfs_trans *tp)
369{
370 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
371 ip->i_d.di_forkoff = 0;
372 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
373
374 ASSERT(ip->i_d.di_anextents == 0);
375 ASSERT(ip->i_afp == NULL);
376
Christoph Hellwige1486de2009-02-04 09:36:00 +0100377 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
378}
379
380/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100381 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
383int
384xfs_attr_shortform_remove(xfs_da_args_t *args)
385{
386 xfs_attr_shortform_t *sf;
387 xfs_attr_sf_entry_t *sfe;
388 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100389 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 xfs_inode_t *dp;
391
Dave Chinner5a5881c2012-03-22 05:15:13 +0000392 trace_xfs_attr_sf_remove(args);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100395 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 base = sizeof(xfs_attr_sf_hdr_t);
397 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
398 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100399 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100400 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 base += size, i++) {
402 size = XFS_ATTR_SF_ENTSIZE(sfe);
403 if (sfe->namelen != args->namelen)
404 continue;
405 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
406 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000407 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 continue;
409 break;
410 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100411 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return(XFS_ERROR(ENOATTR));
413
Nathan Scottd8cc8902005-11-02 10:34:53 +1100414 /*
415 * Fix up the attribute fork data, covering the hole
416 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100418 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100419 if (end != totsize)
420 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100421 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800422 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100423
424 /*
425 * Fix up the start offset of the attribute fork
426 */
427 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000428 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
Christoph Hellwige1486de2009-02-04 09:36:00 +0100429 (mp->m_flags & XFS_MOUNT_ATTR2) &&
430 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
431 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
432 xfs_attr_fork_reset(dp, args->trans);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100433 } else {
434 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
435 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
436 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000437 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
438 (args->op_flags & XFS_DA_OP_ADDNAME) ||
439 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
440 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100441 xfs_trans_log_inode(args->trans, dp,
442 XFS_ILOG_CORE | XFS_ILOG_ADATA);
443 }
444
445 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 return(0);
448}
449
450/*
451 * Look up a name in a shortform attribute list structure.
452 */
453/*ARGSUSED*/
454int
455xfs_attr_shortform_lookup(xfs_da_args_t *args)
456{
457 xfs_attr_shortform_t *sf;
458 xfs_attr_sf_entry_t *sfe;
459 int i;
460 xfs_ifork_t *ifp;
461
Dave Chinner5a5881c2012-03-22 05:15:13 +0000462 trace_xfs_attr_sf_lookup(args);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ifp = args->dp->i_afp;
465 ASSERT(ifp->if_flags & XFS_IFINLINE);
466 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
467 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100468 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
470 if (sfe->namelen != args->namelen)
471 continue;
472 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
473 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000474 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 continue;
476 return(XFS_ERROR(EEXIST));
477 }
478 return(XFS_ERROR(ENOATTR));
479}
480
481/*
482 * Look up a name in a shortform attribute list structure.
483 */
484/*ARGSUSED*/
485int
486xfs_attr_shortform_getvalue(xfs_da_args_t *args)
487{
488 xfs_attr_shortform_t *sf;
489 xfs_attr_sf_entry_t *sfe;
490 int i;
491
492 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
493 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
494 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100495 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
497 if (sfe->namelen != args->namelen)
498 continue;
499 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
500 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000501 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 continue;
503 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100504 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return(XFS_ERROR(EEXIST));
506 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100507 if (args->valuelen < sfe->valuelen) {
508 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return(XFS_ERROR(ERANGE));
510 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100511 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 memcpy(args->value, &sfe->nameval[args->namelen],
513 args->valuelen);
514 return(XFS_ERROR(EEXIST));
515 }
516 return(XFS_ERROR(ENOATTR));
517}
518
519/*
520 * Convert from using the shortform to the leaf.
521 */
522int
523xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
524{
525 xfs_inode_t *dp;
526 xfs_attr_shortform_t *sf;
527 xfs_attr_sf_entry_t *sfe;
528 xfs_da_args_t nargs;
529 char *tmpbuffer;
530 int error, i, size;
531 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000532 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 xfs_ifork_t *ifp;
534
Dave Chinner5a5881c2012-03-22 05:15:13 +0000535 trace_xfs_attr_sf_to_leaf(args);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 dp = args->dp;
538 ifp = dp->i_afp;
539 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100540 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 tmpbuffer = kmem_alloc(size, KM_SLEEP);
542 ASSERT(tmpbuffer != NULL);
543 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
544 sf = (xfs_attr_shortform_t *)tmpbuffer;
545
546 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
547 bp = NULL;
548 error = xfs_da_grow_inode(args, &blkno);
549 if (error) {
550 /*
551 * If we hit an IO error middle of the transaction inside
552 * grow_inode(), we may have inconsistent data. Bail out.
553 */
554 if (error == EIO)
555 goto out;
556 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
557 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
558 goto out;
559 }
560
561 ASSERT(blkno == 0);
562 error = xfs_attr_leaf_create(args, blkno, &bp);
563 if (error) {
564 error = xfs_da_shrink_inode(args, 0, bp);
565 bp = NULL;
566 if (error)
567 goto out;
568 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
569 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
570 goto out;
571 }
572
573 memset((char *)&nargs, 0, sizeof(nargs));
574 nargs.dp = dp;
575 nargs.firstblock = args->firstblock;
576 nargs.flist = args->flist;
577 nargs.total = args->total;
578 nargs.whichfork = XFS_ATTR_FORK;
579 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000580 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100583 for (i = 0; i < sf->hdr.count; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100584 nargs.name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 nargs.namelen = sfe->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100586 nargs.value = &sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100587 nargs.valuelen = sfe->valuelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100588 nargs.hashval = xfs_da_hashname(sfe->nameval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000590 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
592 ASSERT(error == ENOATTR);
593 error = xfs_attr_leaf_add(bp, &nargs);
594 ASSERT(error != ENOSPC);
595 if (error)
596 goto out;
597 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
598 }
599 error = 0;
600
601out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000602 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return(error);
604}
605
606STATIC int
607xfs_attr_shortform_compare(const void *a, const void *b)
608{
609 xfs_attr_sf_sort_t *sa, *sb;
610
611 sa = (xfs_attr_sf_sort_t *)a;
612 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100613 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100615 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return(1);
617 } else {
618 return(sa->entno - sb->entno);
619 }
620}
621
Tim Shimmin726801b2006-09-28 11:01:37 +1000622
623#define XFS_ISRESET_CURSOR(cursor) \
624 (!((cursor)->initted) && !((cursor)->hashval) && \
625 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*
627 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000628 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * If the output buffer is not large enough to hold them all, then we
630 * we have to calculate each entries' hashvalue and sort them before
631 * we can begin returning them to the user.
632 */
633/*ARGSUSED*/
634int
635xfs_attr_shortform_list(xfs_attr_list_context_t *context)
636{
637 attrlist_cursor_kern_t *cursor;
638 xfs_attr_sf_sort_t *sbuf, *sbp;
639 xfs_attr_shortform_t *sf;
640 xfs_attr_sf_entry_t *sfe;
641 xfs_inode_t *dp;
642 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000643 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 ASSERT(context != NULL);
646 dp = context->dp;
647 ASSERT(dp != NULL);
648 ASSERT(dp->i_afp != NULL);
649 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
650 ASSERT(sf != NULL);
651 if (!sf->hdr.count)
652 return(0);
653 cursor = context->cursor;
654 ASSERT(cursor != NULL);
655
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000656 trace_xfs_attr_list_sf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000659 * If the buffer is large enough and the cursor is at the start,
660 * do not bother with sorting since we will return everything in
661 * one buffer and another call using the cursor won't need to be
662 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000664 * If bufsize is zero then put_listent must be a search function
665 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000667 if (context->bufsize == 0 ||
668 (XFS_ISRESET_CURSOR(cursor) &&
669 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100670 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000671 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000672 sfe->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100673 sfe->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +1000674 (int)sfe->namelen,
675 (int)sfe->valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100676 &sfe->nameval[sfe->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000677
678 /*
679 * Either search callback finished early or
680 * didn't fit it all in the buffer after all.
681 */
682 if (context->seen_enough)
683 break;
684
685 if (error)
686 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
688 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000689 trace_xfs_attr_list_sf_all(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return(0);
691 }
692
Tim Shimmin726801b2006-09-28 11:01:37 +1000693 /* do no more for a search callback */
694 if (context->bufsize == 0)
695 return 0;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /*
698 * It didn't all fit, so we have to sort everything on hashval.
699 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100700 sbsize = sf->hdr.count * sizeof(*sbuf);
Dave Chinner622d8142010-12-23 11:57:37 +1100701 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /*
704 * Scan the attribute list for the rest of the entries, storing
705 * the relevant info from only those that match into a buffer.
706 */
707 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100708 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (unlikely(
710 ((char *)sfe < (char *)sf) ||
711 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
712 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
713 XFS_ERRLEVEL_LOW,
714 context->dp->i_mount, sfe);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000715 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return XFS_ERROR(EFSCORRUPTED);
717 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 sbp->entno = i;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100720 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
721 sbp->name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 sbp->namelen = sfe->namelen;
723 /* These are bytes, and both on-disk, don't endian-flip */
724 sbp->valuelen = sfe->valuelen;
725 sbp->flags = sfe->flags;
726 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
727 sbp++;
728 nsbuf++;
729 }
730
731 /*
732 * Sort the entries on hash then entno.
733 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100734 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /*
737 * Re-find our place IN THE SORTED LIST.
738 */
739 count = 0;
740 cursor->initted = 1;
741 cursor->blkno = 0;
742 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100743 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (cursor->offset == count) {
745 break;
746 }
747 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100748 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750 }
751 }
752 if (i == nsbuf) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000753 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return(0);
755 }
756
757 /*
758 * Loop putting entries into the user buffer.
759 */
760 for ( ; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100761 if (cursor->hashval != sbp->hash) {
762 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 cursor->offset = 0;
764 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000765 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000766 sbp->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000767 sbp->name,
768 sbp->namelen,
769 sbp->valuelen,
770 &sbp->name[sbp->namelen]);
771 if (error)
772 return error;
773 if (context->seen_enough)
774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 cursor->offset++;
776 }
777
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000778 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return(0);
780}
781
782/*
783 * Check a leaf attribute block to see if all the entries would fit into
784 * a shortform attribute list.
785 */
786int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000787xfs_attr_shortform_allfit(
788 struct xfs_buf *bp,
789 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 xfs_attr_leafblock_t *leaf;
792 xfs_attr_leaf_entry_t *entry;
793 xfs_attr_leaf_name_local_t *name_loc;
794 int bytes, i;
795
Dave Chinner1d9025e2012-06-22 18:50:14 +1000796 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200797 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 entry = &leaf->entries[0];
800 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100801 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (entry->flags & XFS_ATTR_INCOMPLETE)
803 continue; /* don't copy partial entries */
804 if (!(entry->flags & XFS_ATTR_LOCAL))
805 return(0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600806 name_loc = xfs_attr_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
808 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100809 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return(0);
811 bytes += sizeof(struct xfs_attr_sf_entry)-1
812 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100813 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100815 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100816 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100817 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100818 return(-1);
819 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822/*
823 * Convert a leaf attribute list to shortform attribute list
824 */
825int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000826xfs_attr_leaf_to_shortform(
827 struct xfs_buf *bp,
828 xfs_da_args_t *args,
829 int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 xfs_attr_leafblock_t *leaf;
832 xfs_attr_leaf_entry_t *entry;
833 xfs_attr_leaf_name_local_t *name_loc;
834 xfs_da_args_t nargs;
835 xfs_inode_t *dp;
836 char *tmpbuffer;
837 int error, i;
838
Dave Chinner5a5881c2012-03-22 05:15:13 +0000839 trace_xfs_attr_leaf_to_sf(args);
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 dp = args->dp;
842 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
843 ASSERT(tmpbuffer != NULL);
844
845 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000846 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200848 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000849 memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /*
852 * Clean out the prior contents of the attribute list.
853 */
854 error = xfs_da_shrink_inode(args, 0, bp);
855 if (error)
856 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100857
858 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +1100859 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +1100860 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Christoph Hellwige1486de2009-02-04 09:36:00 +0100861 xfs_attr_fork_reset(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100863 }
864
865 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /*
868 * Copy the attributes
869 */
870 memset((char *)&nargs, 0, sizeof(nargs));
871 nargs.dp = dp;
872 nargs.firstblock = args->firstblock;
873 nargs.flist = args->flist;
874 nargs.total = args->total;
875 nargs.whichfork = XFS_ATTR_FORK;
876 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000877 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +1100879 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (entry->flags & XFS_ATTR_INCOMPLETE)
881 continue; /* don't copy partial entries */
882 if (!entry->nameidx)
883 continue;
884 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -0600885 name_loc = xfs_attr_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +1100886 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100888 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +1100889 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100890 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +1000891 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100892 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 error = 0;
895
896out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000897 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return(error);
899}
900
901/*
902 * Convert from using a single leaf to a root node and a leaf.
903 */
904int
905xfs_attr_leaf_to_node(xfs_da_args_t *args)
906{
907 xfs_attr_leafblock_t *leaf;
908 xfs_da_intnode_t *node;
909 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000910 struct xfs_buf *bp1, *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 xfs_dablk_t blkno;
912 int error;
913
Dave Chinner5a5881c2012-03-22 05:15:13 +0000914 trace_xfs_attr_leaf_to_node(args);
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 dp = args->dp;
917 bp1 = bp2 = NULL;
918 error = xfs_da_grow_inode(args, &blkno);
919 if (error)
920 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +1100921 error = xfs_attr_leaf_read(args->trans, args->dp, 0, -1, &bp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (error)
923 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +1100924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 bp2 = NULL;
926 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
927 XFS_ATTR_FORK);
928 if (error)
929 goto out;
Dave Chinner1813dd62012-11-14 17:54:40 +1100930 bp2->b_ops = bp1->b_ops;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000931 memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 bp1 = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000933 xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /*
936 * Set up the new root node.
937 */
938 error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
939 if (error)
940 goto out;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000941 node = bp1->b_addr;
942 leaf = bp2->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200943 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* both on-disk, don't endian-flip twice */
945 node->btree[0].hashval =
Nathan Scott918ae422006-03-17 17:28:54 +1100946 leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
Nathan Scott403432d2006-03-17 17:29:46 +1100947 node->btree[0].before = cpu_to_be32(blkno);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100948 node->hdr.count = cpu_to_be16(1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000949 xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 error = 0;
951out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return(error);
953}
954
955
956/*========================================================================
957 * Routines used for growing the Btree.
958 *========================================================================*/
959
960/*
961 * Create the initial contents of a leaf attribute list
962 * or a leaf in a node attribute list.
963 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000964STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000965xfs_attr_leaf_create(
966 xfs_da_args_t *args,
967 xfs_dablk_t blkno,
968 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 xfs_attr_leafblock_t *leaf;
971 xfs_attr_leaf_hdr_t *hdr;
972 xfs_inode_t *dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000973 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int error;
975
Dave Chinner5a5881c2012-03-22 05:15:13 +0000976 trace_xfs_attr_leaf_create(args);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 dp = args->dp;
979 ASSERT(dp != NULL);
980 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
981 XFS_ATTR_FORK);
982 if (error)
983 return(error);
Dave Chinner1813dd62012-11-14 17:54:40 +1100984 bp->b_ops = &xfs_attr_leaf_buf_ops;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000985 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
987 hdr = &leaf->hdr;
Nathan Scott89da0542006-03-17 17:28:40 +1100988 hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
Nathan Scott918ae422006-03-17 17:28:54 +1100989 hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +1100991 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
993 }
994
Nathan Scott918ae422006-03-17 17:28:54 +1100995 hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
996 hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
997 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Dave Chinner1d9025e2012-06-22 18:50:14 +1000999 xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 *bpp = bp;
1002 return(0);
1003}
1004
1005/*
1006 * Split the leaf node, rebalance, then add the new entry.
1007 */
1008int
1009xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
1010 xfs_da_state_blk_t *newblk)
1011{
1012 xfs_dablk_t blkno;
1013 int error;
1014
Dave Chinner5a5881c2012-03-22 05:15:13 +00001015 trace_xfs_attr_leaf_split(state->args);
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 /*
1018 * Allocate space for a new leaf node.
1019 */
1020 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1021 error = xfs_da_grow_inode(state->args, &blkno);
1022 if (error)
1023 return(error);
1024 error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
1025 if (error)
1026 return(error);
1027 newblk->blkno = blkno;
1028 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1029
1030 /*
1031 * Rebalance the entries across the two leaves.
1032 * NOTE: rebalance() currently depends on the 2nd block being empty.
1033 */
1034 xfs_attr_leaf_rebalance(state, oldblk, newblk);
1035 error = xfs_da_blk_link(state, oldblk, newblk);
1036 if (error)
1037 return(error);
1038
1039 /*
1040 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1041 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1042 * "new" attrs info. Will need the "old" info to remove it later.
1043 *
1044 * Insert the "new" entry in the correct block.
1045 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001046 if (state->inleaf) {
1047 trace_xfs_attr_leaf_add_old(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 error = xfs_attr_leaf_add(oldblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001049 } else {
1050 trace_xfs_attr_leaf_add_new(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 error = xfs_attr_leaf_add(newblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 /*
1055 * Update last hashval in each block since we added the name.
1056 */
1057 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1058 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1059 return(error);
1060}
1061
1062/*
1063 * Add a name to the leaf attribute list structure.
1064 */
1065int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001066xfs_attr_leaf_add(
1067 struct xfs_buf *bp,
1068 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 xfs_attr_leafblock_t *leaf;
1071 xfs_attr_leaf_hdr_t *hdr;
1072 xfs_attr_leaf_map_t *map;
1073 int tablesize, entsize, sum, tmp, i;
1074
Dave Chinner5a5881c2012-03-22 05:15:13 +00001075 trace_xfs_attr_leaf_add(args);
1076
Dave Chinner1d9025e2012-06-22 18:50:14 +10001077 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001078 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001080 && (args->index <= be16_to_cpu(leaf->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 hdr = &leaf->hdr;
Nathan Scottaa82daa2005-11-02 10:33:33 +11001082 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1084
1085 /*
1086 * Search through freemap for first-fit on new name length.
1087 * (may need to figure in size of entry struct too)
1088 */
Nathan Scott918ae422006-03-17 17:28:54 +11001089 tablesize = (be16_to_cpu(hdr->count) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * sizeof(xfs_attr_leaf_entry_t)
1091 + sizeof(xfs_attr_leaf_hdr_t);
1092 map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
1093 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
Nathan Scott918ae422006-03-17 17:28:54 +11001094 if (tablesize > be16_to_cpu(hdr->firstused)) {
1095 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 continue;
1097 }
1098 if (!map->size)
1099 continue; /* no space in this map */
1100 tmp = entsize;
Nathan Scott918ae422006-03-17 17:28:54 +11001101 if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 tmp += sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001103 if (be16_to_cpu(map->size) >= tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 tmp = xfs_attr_leaf_add_work(bp, args, i);
1105 return(tmp);
1106 }
Nathan Scott918ae422006-03-17 17:28:54 +11001107 sum += be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109
1110 /*
1111 * If there are no holes in the address space of the block,
1112 * and we don't have enough freespace, then compaction will do us
1113 * no good and we should just give up.
1114 */
1115 if (!hdr->holes && (sum < entsize))
1116 return(XFS_ERROR(ENOSPC));
1117
1118 /*
1119 * Compact the entries to coalesce free space.
1120 * This may change the hdr->count via dropping INCOMPLETE entries.
1121 */
Dave Chinneree732592012-11-12 22:53:53 +11001122 xfs_attr_leaf_compact(args, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /*
1125 * After compaction, the block is guaranteed to have only one
1126 * free region, in freemap[0]. If it is not big enough, give up.
1127 */
Nathan Scott918ae422006-03-17 17:28:54 +11001128 if (be16_to_cpu(hdr->freemap[0].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 < (entsize + sizeof(xfs_attr_leaf_entry_t)))
1130 return(XFS_ERROR(ENOSPC));
1131
1132 return(xfs_attr_leaf_add_work(bp, args, 0));
1133}
1134
1135/*
1136 * Add a name to a leaf attribute list structure.
1137 */
1138STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001139xfs_attr_leaf_add_work(
1140 struct xfs_buf *bp,
1141 xfs_da_args_t *args,
1142 int mapindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 xfs_attr_leafblock_t *leaf;
1145 xfs_attr_leaf_hdr_t *hdr;
1146 xfs_attr_leaf_entry_t *entry;
1147 xfs_attr_leaf_name_local_t *name_loc;
1148 xfs_attr_leaf_name_remote_t *name_rmt;
1149 xfs_attr_leaf_map_t *map;
1150 xfs_mount_t *mp;
1151 int tmp, i;
1152
Dave Chinneree732592012-11-12 22:53:53 +11001153 trace_xfs_attr_leaf_add_work(args);
1154
Dave Chinner1d9025e2012-06-22 18:50:14 +10001155 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001156 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 hdr = &leaf->hdr;
1158 ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
Nathan Scott918ae422006-03-17 17:28:54 +11001159 ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 /*
1162 * Force open some space in the entry array and fill it in.
1163 */
1164 entry = &leaf->entries[args->index];
Nathan Scott918ae422006-03-17 17:28:54 +11001165 if (args->index < be16_to_cpu(hdr->count)) {
1166 tmp = be16_to_cpu(hdr->count) - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 tmp *= sizeof(xfs_attr_leaf_entry_t);
1168 memmove((char *)(entry+1), (char *)entry, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001169 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1171 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001172 be16_add_cpu(&hdr->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /*
1175 * Allocate space for the new string (at the end of the run).
1176 */
1177 map = &hdr->freemap[mapindex];
1178 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001179 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1180 ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
1181 ASSERT(be16_to_cpu(map->size) >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001182 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1183 mp->m_sb.sb_blocksize, NULL));
Nathan Scott918ae422006-03-17 17:28:54 +11001184 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1185 ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001186 be16_add_cpu(&map->size,
Nathan Scottaa82daa2005-11-02 10:33:33 +11001187 -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1188 mp->m_sb.sb_blocksize, &tmp));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001189 entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
1190 be16_to_cpu(map->size));
1191 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001193 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001194 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 entry->flags |= XFS_ATTR_INCOMPLETE;
1196 if ((args->blkno2 == args->blkno) &&
1197 (args->index2 <= args->index)) {
1198 args->index2++;
1199 }
1200 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001201 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001203 ASSERT((args->index == 0) ||
1204 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Nathan Scott918ae422006-03-17 17:28:54 +11001205 ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001206 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 * For "remote" attribute values, simply note that we need to
1210 * allocate space for the "remote" value. We can't actually
1211 * allocate the extents in this transaction, and we can't decide
1212 * which blocks they should be as we might allocate more blocks
1213 * as part of this transaction (a split operation for example).
1214 */
1215 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001216 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001218 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1220 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001221 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001223 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 name_rmt->namelen = args->namelen;
1225 memcpy((char *)name_rmt->name, args->name, args->namelen);
1226 entry->flags |= XFS_ATTR_INCOMPLETE;
1227 /* just in case */
1228 name_rmt->valuelen = 0;
1229 name_rmt->valueblk = 0;
1230 args->rmtblkno = 1;
1231 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1232 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001233 xfs_trans_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001234 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 xfs_attr_leaf_entsize(leaf, args->index)));
1236
1237 /*
1238 * Update the control info for this leaf node
1239 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001240 if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* both on-disk, don't endian-flip twice */
1242 hdr->firstused = entry->nameidx;
1243 }
Nathan Scott918ae422006-03-17 17:28:54 +11001244 ASSERT(be16_to_cpu(hdr->firstused) >=
1245 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
1246 tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 + sizeof(xfs_attr_leaf_hdr_t);
1248 map = &hdr->freemap[0];
1249 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001250 if (be16_to_cpu(map->base) == tmp) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001251 be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
1252 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001253 -((int)sizeof(xfs_attr_leaf_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001256 be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001257 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1259 return(0);
1260}
1261
1262/*
1263 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1264 */
1265STATIC void
Dave Chinner1d9025e2012-06-22 18:50:14 +10001266xfs_attr_leaf_compact(
Dave Chinneree732592012-11-12 22:53:53 +11001267 struct xfs_da_args *args,
1268 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Dave Chinneree732592012-11-12 22:53:53 +11001270 xfs_attr_leafblock_t *leaf_s, *leaf_d;
1271 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
1272 struct xfs_trans *trans = args->trans;
1273 struct xfs_mount *mp = trans->t_mountp;
1274 char *tmpbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Dave Chinneree732592012-11-12 22:53:53 +11001276 trace_xfs_attr_leaf_compact(args);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1279 ASSERT(tmpbuffer != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001280 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1281 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /*
1284 * Copy basic information
1285 */
1286 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001287 leaf_d = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 hdr_s = &leaf_s->hdr;
1289 hdr_d = &leaf_d->hdr;
1290 hdr_d->info = hdr_s->info; /* struct copy */
Nathan Scott918ae422006-03-17 17:28:54 +11001291 hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /* handle truncation gracefully */
1293 if (!hdr_d->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001294 hdr_d->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
1296 }
1297 hdr_d->usedbytes = 0;
1298 hdr_d->count = 0;
1299 hdr_d->holes = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001300 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
1301 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
1302 sizeof(xfs_attr_leaf_hdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /*
1305 * Copy all entry's in the same (sorted) order,
1306 * but allocate name/value pairs packed and in sequence.
1307 */
1308 xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001309 be16_to_cpu(hdr_s->count), mp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001310 xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001312 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
1315/*
1316 * Redistribute the attribute list entries between two leaf nodes,
1317 * taking into account the size of the new entry.
1318 *
1319 * NOTE: if new block is empty, then it will get the upper half of the
1320 * old block. At present, all (one) callers pass in an empty second block.
1321 *
1322 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1323 * to match what it is doing in splitting the attribute leaf block. Those
1324 * values are used in "atomic rename" operations on attributes. Note that
1325 * the "new" and "old" values can end up in different blocks.
1326 */
1327STATIC void
1328xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1329 xfs_da_state_blk_t *blk2)
1330{
1331 xfs_da_args_t *args;
1332 xfs_da_state_blk_t *tmp_blk;
1333 xfs_attr_leafblock_t *leaf1, *leaf2;
1334 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1335 int count, totallen, max, space, swap;
1336
1337 /*
1338 * Set up environment.
1339 */
1340 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1341 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001342 leaf1 = blk1->bp->b_addr;
1343 leaf2 = blk2->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001344 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1345 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Dave Chinner07428d72012-11-12 22:09:44 +11001346 ASSERT(leaf2->hdr.count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 args = state->args;
1348
Dave Chinner5a5881c2012-03-22 05:15:13 +00001349 trace_xfs_attr_leaf_rebalance(args);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * Check ordering of blocks, reverse if it makes things simpler.
1353 *
1354 * NOTE: Given that all (current) callers pass in an empty
1355 * second block, this code should never set "swap".
1356 */
1357 swap = 0;
1358 if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
1359 tmp_blk = blk1;
1360 blk1 = blk2;
1361 blk2 = tmp_blk;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001362 leaf1 = blk1->bp->b_addr;
1363 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 swap = 1;
1365 }
1366 hdr1 = &leaf1->hdr;
1367 hdr2 = &leaf2->hdr;
1368
1369 /*
1370 * Examine entries until we reduce the absolute difference in
1371 * byte usage between the two blocks to a minimum. Then get
1372 * the direction to copy and the number of elements to move.
1373 *
1374 * "inleaf" is true if the new entry should be inserted into blk1.
1375 * If "swap" is also true, then reverse the sense of "inleaf".
1376 */
1377 state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
1378 &count, &totallen);
1379 if (swap)
1380 state->inleaf = !state->inleaf;
1381
1382 /*
1383 * Move any entries required from leaf to leaf:
1384 */
Nathan Scott918ae422006-03-17 17:28:54 +11001385 if (count < be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * Figure the total bytes to be added to the destination leaf.
1388 */
1389 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001390 count = be16_to_cpu(hdr1->count) - count;
1391 space = be16_to_cpu(hdr1->usedbytes) - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 space += count * sizeof(xfs_attr_leaf_entry_t);
1393
1394 /*
1395 * leaf2 is the destination, compact it if it looks tight.
1396 */
Nathan Scott918ae422006-03-17 17:28:54 +11001397 max = be16_to_cpu(hdr2->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001399 max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001400 if (space > max)
1401 xfs_attr_leaf_compact(args, blk2->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 /*
1404 * Move high entries from leaf1 to low end of leaf2.
1405 */
Nathan Scott918ae422006-03-17 17:28:54 +11001406 xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 leaf2, 0, count, state->mp);
1408
Dave Chinner1d9025e2012-06-22 18:50:14 +10001409 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1410 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Nathan Scott918ae422006-03-17 17:28:54 +11001411 } else if (count > be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /*
1413 * I assert that since all callers pass in an empty
1414 * second buffer, this code should never execute.
1415 */
Dave Chinner07428d72012-11-12 22:09:44 +11001416 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /*
1419 * Figure the total bytes to be added to the destination leaf.
1420 */
1421 /* number entries being moved */
Nathan Scott918ae422006-03-17 17:28:54 +11001422 count -= be16_to_cpu(hdr1->count);
1423 space = totallen - be16_to_cpu(hdr1->usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 space += count * sizeof(xfs_attr_leaf_entry_t);
1425
1426 /*
1427 * leaf1 is the destination, compact it if it looks tight.
1428 */
Nathan Scott918ae422006-03-17 17:28:54 +11001429 max = be16_to_cpu(hdr1->firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 - sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001431 max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001432 if (space > max)
1433 xfs_attr_leaf_compact(args, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 /*
1436 * Move low entries from leaf2 to high end of leaf1.
1437 */
1438 xfs_attr_leaf_moveents(leaf2, 0, leaf1,
Nathan Scott918ae422006-03-17 17:28:54 +11001439 be16_to_cpu(hdr1->count), count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Dave Chinner1d9025e2012-06-22 18:50:14 +10001441 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1442 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
1445 /*
1446 * Copy out last hashval in each block for B-tree code.
1447 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001448 blk1->hashval = be32_to_cpu(
1449 leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
1450 blk2->hashval = be32_to_cpu(
1451 leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 /*
1454 * Adjust the expected index for insertion.
1455 * NOTE: this code depends on the (current) situation that the
1456 * second block was originally empty.
1457 *
1458 * If the insertion point moved to the 2nd block, we must adjust
1459 * the index. We must also track the entry just following the
1460 * new entry for use in an "atomic rename" operation, that entry
1461 * is always the "old" entry and the "new" entry is what we are
1462 * inserting. The index/blkno fields refer to the "old" entry,
1463 * while the index2/blkno2 fields refer to the "new" entry.
1464 */
Nathan Scott918ae422006-03-17 17:28:54 +11001465 if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 ASSERT(state->inleaf == 0);
Nathan Scott918ae422006-03-17 17:28:54 +11001467 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 args->index = args->index2 = blk2->index;
1469 args->blkno = args->blkno2 = blk2->blkno;
Nathan Scott918ae422006-03-17 17:28:54 +11001470 } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (state->inleaf) {
1472 args->index = blk1->index;
1473 args->blkno = blk1->blkno;
1474 args->index2 = 0;
1475 args->blkno2 = blk2->blkno;
1476 } else {
Dave Chinner07428d72012-11-12 22:09:44 +11001477 /*
1478 * On a double leaf split, the original attr location
1479 * is already stored in blkno2/index2, so don't
1480 * overwrite it overwise we corrupt the tree.
1481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 blk2->index = blk1->index
Nathan Scott918ae422006-03-17 17:28:54 +11001483 - be16_to_cpu(leaf1->hdr.count);
Dave Chinner07428d72012-11-12 22:09:44 +11001484 args->index = blk2->index;
1485 args->blkno = blk2->blkno;
1486 if (!state->extravalid) {
1487 /*
1488 * set the new attr location to match the old
1489 * one and let the higher level split code
1490 * decide where in the leaf to place it.
1491 */
1492 args->index2 = blk2->index;
1493 args->blkno2 = blk2->blkno;
1494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496 } else {
1497 ASSERT(state->inleaf == 1);
1498 args->index = args->index2 = blk1->index;
1499 args->blkno = args->blkno2 = blk1->blkno;
1500 }
1501}
1502
1503/*
1504 * Examine entries until we reduce the absolute difference in
1505 * byte usage between the two blocks to a minimum.
1506 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1507 * GROT: there will always be enough room in either block for a new entry.
1508 * GROT: Do a double-split for this case?
1509 */
1510STATIC int
1511xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
1512 xfs_da_state_blk_t *blk1,
1513 xfs_da_state_blk_t *blk2,
1514 int *countarg, int *usedbytesarg)
1515{
1516 xfs_attr_leafblock_t *leaf1, *leaf2;
1517 xfs_attr_leaf_hdr_t *hdr1, *hdr2;
1518 xfs_attr_leaf_entry_t *entry;
1519 int count, max, index, totallen, half;
1520 int lastdelta, foundit, tmp;
1521
1522 /*
1523 * Set up environment.
1524 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001525 leaf1 = blk1->bp->b_addr;
1526 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 hdr1 = &leaf1->hdr;
1528 hdr2 = &leaf2->hdr;
1529 foundit = 0;
1530 totallen = 0;
1531
1532 /*
1533 * Examine entries until we reduce the absolute difference in
1534 * byte usage between the two blocks to a minimum.
1535 */
Nathan Scott918ae422006-03-17 17:28:54 +11001536 max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 half = (max+1) * sizeof(*entry);
Nathan Scott918ae422006-03-17 17:28:54 +11001538 half += be16_to_cpu(hdr1->usedbytes) +
1539 be16_to_cpu(hdr2->usedbytes) +
1540 xfs_attr_leaf_newentsize(
1541 state->args->namelen,
1542 state->args->valuelen,
1543 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 half /= 2;
1545 lastdelta = state->blocksize;
1546 entry = &leaf1->entries[0];
1547 for (count = index = 0; count < max; entry++, index++, count++) {
1548
1549#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1550 /*
1551 * The new entry is in the first block, account for it.
1552 */
1553 if (count == blk1->index) {
1554 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001555 xfs_attr_leaf_newentsize(
1556 state->args->namelen,
1557 state->args->valuelen,
1558 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1560 break;
1561 lastdelta = XFS_ATTR_ABS(half - tmp);
1562 totallen = tmp;
1563 foundit = 1;
1564 }
1565
1566 /*
1567 * Wrap around into the second block if necessary.
1568 */
Nathan Scott918ae422006-03-17 17:28:54 +11001569 if (count == be16_to_cpu(hdr1->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 leaf1 = leaf2;
1571 entry = &leaf1->entries[0];
1572 index = 0;
1573 }
1574
1575 /*
1576 * Figure out if next leaf entry would be too much.
1577 */
1578 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1579 index);
1580 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1581 break;
1582 lastdelta = XFS_ATTR_ABS(half - tmp);
1583 totallen = tmp;
1584#undef XFS_ATTR_ABS
1585 }
1586
1587 /*
1588 * Calculate the number of usedbytes that will end up in lower block.
1589 * If new entry not in lower block, fix up the count.
1590 */
1591 totallen -= count * sizeof(*entry);
1592 if (foundit) {
1593 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001594 xfs_attr_leaf_newentsize(
1595 state->args->namelen,
1596 state->args->valuelen,
1597 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599
1600 *countarg = count;
1601 *usedbytesarg = totallen;
1602 return(foundit);
1603}
1604
1605/*========================================================================
1606 * Routines used for shrinking the Btree.
1607 *========================================================================*/
1608
1609/*
1610 * Check a leaf block and its neighbors to see if the block should be
1611 * collapsed into one or the other neighbor. Always keep the block
1612 * with the smaller block number.
1613 * If the current block is over 50% full, don't try to join it, return 0.
1614 * If the block is empty, fill in the state structure and return 2.
1615 * If it can be collapsed, fill in the state structure and return 1.
1616 * If nothing can be done, return 0.
1617 *
1618 * GROT: allow for INCOMPLETE entries in calculation.
1619 */
1620int
1621xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
1622{
1623 xfs_attr_leafblock_t *leaf;
1624 xfs_da_state_blk_t *blk;
1625 xfs_da_blkinfo_t *info;
1626 int count, bytes, forward, error, retval, i;
1627 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001628 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dave Chinneree732592012-11-12 22:53:53 +11001630 trace_xfs_attr_leaf_toosmall(state->args);
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /*
1633 * Check for the degenerate case of the block being over 50% full.
1634 * If so, it's not worth even looking to see if we might be able
1635 * to coalesce with a sibling.
1636 */
1637 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001638 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001639 ASSERT(info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001641 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 bytes = sizeof(xfs_attr_leaf_hdr_t) +
1643 count * sizeof(xfs_attr_leaf_entry_t) +
Nathan Scott918ae422006-03-17 17:28:54 +11001644 be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (bytes > (state->blocksize >> 1)) {
1646 *action = 0; /* blk over 50%, don't try to join */
1647 return(0);
1648 }
1649
1650 /*
1651 * Check for the degenerate case of the block being empty.
1652 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001653 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 * to merge with the forward block unless it is NULL.
1655 */
1656 if (count == 0) {
1657 /*
1658 * Make altpath point to the block we want to keep and
1659 * path point to the block we want to drop (this one).
1660 */
Nathan Scott89da0542006-03-17 17:28:40 +11001661 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 memcpy(&state->altpath, &state->path, sizeof(state->path));
1663 error = xfs_da_path_shift(state, &state->altpath, forward,
1664 0, &retval);
1665 if (error)
1666 return(error);
1667 if (retval) {
1668 *action = 0;
1669 } else {
1670 *action = 2;
1671 }
1672 return(0);
1673 }
1674
1675 /*
1676 * Examine each sibling block to see if we can coalesce with
1677 * at least 25% free space to spare. We need to figure out
1678 * whether to merge with the forward or the backward block.
1679 * We prefer coalescing with the lower numbered sibling so as
1680 * to shrink an attribute list over time.
1681 */
1682 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +11001683 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 for (i = 0; i < 2; forward = !forward, i++) {
1685 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +11001686 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 else
Nathan Scott89da0542006-03-17 17:28:40 +11001688 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (blkno == 0)
1690 continue;
Dave Chinnerad14c332012-11-12 22:54:16 +11001691 error = xfs_attr_leaf_read(state->args->trans, state->args->dp,
1692 blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (error)
1694 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 leaf = (xfs_attr_leafblock_t *)info;
Nathan Scott918ae422006-03-17 17:28:54 +11001697 count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 bytes = state->blocksize - (state->blocksize>>2);
Nathan Scott918ae422006-03-17 17:28:54 +11001699 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001700 leaf = bp->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11001701 count += be16_to_cpu(leaf->hdr.count);
1702 bytes -= be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 bytes -= count * sizeof(xfs_attr_leaf_entry_t);
1704 bytes -= sizeof(xfs_attr_leaf_hdr_t);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001705 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (bytes >= 0)
1707 break; /* fits with at least 25% to spare */
1708 }
1709 if (i >= 2) {
1710 *action = 0;
1711 return(0);
1712 }
1713
1714 /*
1715 * Make altpath point to the block we want to keep (the lower
1716 * numbered block) and path point to the block we want to drop.
1717 */
1718 memcpy(&state->altpath, &state->path, sizeof(state->path));
1719 if (blkno < blk->blkno) {
1720 error = xfs_da_path_shift(state, &state->altpath, forward,
1721 0, &retval);
1722 } else {
1723 error = xfs_da_path_shift(state, &state->path, forward,
1724 0, &retval);
1725 }
1726 if (error)
1727 return(error);
1728 if (retval) {
1729 *action = 0;
1730 } else {
1731 *action = 1;
1732 }
1733 return(0);
1734}
1735
1736/*
1737 * Remove a name from the leaf attribute list structure.
1738 *
1739 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1740 * If two leaves are 37% full, when combined they will leave 25% free.
1741 */
1742int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001743xfs_attr_leaf_remove(
1744 struct xfs_buf *bp,
1745 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 xfs_attr_leafblock_t *leaf;
1748 xfs_attr_leaf_hdr_t *hdr;
1749 xfs_attr_leaf_map_t *map;
1750 xfs_attr_leaf_entry_t *entry;
1751 int before, after, smallest, entsize;
1752 int tablesize, tmp, i;
1753 xfs_mount_t *mp;
1754
Dave Chinneree732592012-11-12 22:53:53 +11001755 trace_xfs_attr_leaf_remove(args);
1756
Dave Chinner1d9025e2012-06-22 18:50:14 +10001757 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001758 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 hdr = &leaf->hdr;
1760 mp = args->trans->t_mountp;
Nathan Scott918ae422006-03-17 17:28:54 +11001761 ASSERT((be16_to_cpu(hdr->count) > 0)
1762 && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 ASSERT((args->index >= 0)
Nathan Scott918ae422006-03-17 17:28:54 +11001764 && (args->index < be16_to_cpu(hdr->count)));
1765 ASSERT(be16_to_cpu(hdr->firstused) >=
1766 ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 entry = &leaf->entries[args->index];
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001768 ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
1769 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 /*
1772 * Scan through free region table:
1773 * check for adjacency of free'd entry with an existing one,
1774 * find smallest free region in case we need to replace it,
1775 * adjust any map that borders the entry table,
1776 */
Nathan Scott918ae422006-03-17 17:28:54 +11001777 tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 + sizeof(xfs_attr_leaf_hdr_t);
1779 map = &hdr->freemap[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001780 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 before = after = -1;
1782 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
1783 entsize = xfs_attr_leaf_entsize(leaf, args->index);
1784 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
Nathan Scott918ae422006-03-17 17:28:54 +11001785 ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
1786 ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
1787 if (be16_to_cpu(map->base) == tablesize) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001788 be16_add_cpu(&map->base,
Nathan Scott918ae422006-03-17 17:28:54 +11001789 -((int)sizeof(xfs_attr_leaf_entry_t)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001790 be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792
Nathan Scott918ae422006-03-17 17:28:54 +11001793 if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001794 == be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 before = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001796 } else if (be16_to_cpu(map->base)
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001797 == (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 after = i;
Nathan Scott918ae422006-03-17 17:28:54 +11001799 } else if (be16_to_cpu(map->size) < tmp) {
1800 tmp = be16_to_cpu(map->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 smallest = i;
1802 }
1803 }
1804
1805 /*
1806 * Coalesce adjacent freemap regions,
1807 * or replace the smallest region.
1808 */
1809 if ((before >= 0) || (after >= 0)) {
1810 if ((before >= 0) && (after >= 0)) {
1811 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001812 be16_add_cpu(&map->size, entsize);
1813 be16_add_cpu(&map->size,
Nathan Scott918ae422006-03-17 17:28:54 +11001814 be16_to_cpu(hdr->freemap[after].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 hdr->freemap[after].base = 0;
1816 hdr->freemap[after].size = 0;
1817 } else if (before >= 0) {
1818 map = &hdr->freemap[before];
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001819 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 } else {
1821 map = &hdr->freemap[after];
1822 /* both on-disk, don't endian flip twice */
1823 map->base = entry->nameidx;
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001824 be16_add_cpu(&map->size, entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826 } else {
1827 /*
1828 * Replace smallest region (if it is smaller than free'd entry)
1829 */
1830 map = &hdr->freemap[smallest];
Nathan Scott918ae422006-03-17 17:28:54 +11001831 if (be16_to_cpu(map->size) < entsize) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001832 map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
Nathan Scott918ae422006-03-17 17:28:54 +11001833 map->size = cpu_to_be16(entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835 }
1836
1837 /*
1838 * Did we remove the first entry?
1839 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001840 if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 smallest = 1;
1842 else
1843 smallest = 0;
1844
1845 /*
1846 * Compress the remaining entries and zero out the removed stuff.
1847 */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001848 memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001849 be16_add_cpu(&hdr->usedbytes, -entsize);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001850 xfs_trans_log_buf(args->trans, bp,
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06001851 XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 entsize));
1853
Nathan Scott918ae422006-03-17 17:28:54 +11001854 tmp = (be16_to_cpu(hdr->count) - args->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 * sizeof(xfs_attr_leaf_entry_t);
1856 memmove((char *)entry, (char *)(entry+1), tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001857 be16_add_cpu(&hdr->count, -1);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001858 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
Nathan Scott918ae422006-03-17 17:28:54 +11001860 entry = &leaf->entries[be16_to_cpu(hdr->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
1862
1863 /*
1864 * If we removed the first entry, re-find the first used byte
1865 * in the name area. Note that if the entry was the "firstused",
1866 * then we don't have a "hole" in our block resulting from
1867 * removing the name.
1868 */
1869 if (smallest) {
1870 tmp = XFS_LBSIZE(mp);
1871 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11001872 for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001873 ASSERT(be16_to_cpu(entry->nameidx) >=
1874 be16_to_cpu(hdr->firstused));
1875 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
1876
1877 if (be16_to_cpu(entry->nameidx) < tmp)
1878 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Nathan Scott918ae422006-03-17 17:28:54 +11001880 hdr->firstused = cpu_to_be16(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (!hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001882 hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 tmp - XFS_ATTR_LEAF_NAME_ALIGN);
1884 }
1885 } else {
1886 hdr->holes = 1; /* mark as needing compaction */
1887 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001888 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1890
1891 /*
1892 * Check if leaf is less than 50% full, caller may want to
1893 * "join" the leaf with a sibling if so.
1894 */
1895 tmp = sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11001896 tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
1897 tmp += be16_to_cpu(leaf->hdr.usedbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
1899}
1900
1901/*
1902 * Move all the attribute list entries from drop_leaf into save_leaf.
1903 */
1904void
1905xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1906 xfs_da_state_blk_t *save_blk)
1907{
1908 xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1909 xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1910 xfs_mount_t *mp;
1911 char *tmpbuffer;
1912
Dave Chinner5a5881c2012-03-22 05:15:13 +00001913 trace_xfs_attr_leaf_unbalance(state->args);
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /*
1916 * Set up environment.
1917 */
1918 mp = state->mp;
1919 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
1920 ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001921 drop_leaf = drop_blk->bp->b_addr;
1922 save_leaf = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001923 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1924 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 drop_hdr = &drop_leaf->hdr;
1926 save_hdr = &save_leaf->hdr;
1927
1928 /*
1929 * Save last hashval from dying block for later Btree fixup.
1930 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001931 drop_blk->hashval = be32_to_cpu(
1932 drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /*
1935 * Check if we need a temp buffer, or can we do it in place.
1936 * Note that we don't check "leaf" for holes because we will
1937 * always be dropping it, toosmall() decided that for us already.
1938 */
1939 if (save_hdr->holes == 0) {
1940 /*
1941 * dest leaf has no holes, so we add there. May need
1942 * to make some room in the entry array.
1943 */
1944 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1945 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001946 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 } else {
1948 xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001949 be16_to_cpu(save_hdr->count),
1950 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952 } else {
1953 /*
1954 * Destination has holes, so we make a temporary copy
1955 * of the leaf and add them both to that.
1956 */
1957 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1958 ASSERT(tmpbuffer != NULL);
1959 memset(tmpbuffer, 0, state->blocksize);
1960 tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1961 tmp_hdr = &tmp_leaf->hdr;
1962 tmp_hdr->info = save_hdr->info; /* struct copy */
1963 tmp_hdr->count = 0;
Nathan Scott918ae422006-03-17 17:28:54 +11001964 tmp_hdr->firstused = cpu_to_be16(state->blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (!tmp_hdr->firstused) {
Nathan Scott918ae422006-03-17 17:28:54 +11001966 tmp_hdr->firstused = cpu_to_be16(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
1968 }
1969 tmp_hdr->usedbytes = 0;
1970 if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
1971 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001972 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001974 be16_to_cpu(tmp_leaf->hdr.count),
1975 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 } else {
1977 xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
Nathan Scott918ae422006-03-17 17:28:54 +11001978 be16_to_cpu(save_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
Nathan Scott918ae422006-03-17 17:28:54 +11001980 be16_to_cpu(tmp_leaf->hdr.count),
1981 be16_to_cpu(drop_hdr->count), mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983 memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001984 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986
Dave Chinner1d9025e2012-06-22 18:50:14 +10001987 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 state->blocksize - 1);
1989
1990 /*
1991 * Copy out last hashval in each block for B-tree code.
1992 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001993 save_blk->hashval = be32_to_cpu(
1994 save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
1997/*========================================================================
1998 * Routines used for finding things in the Btree.
1999 *========================================================================*/
2000
2001/*
2002 * Look up a name in a leaf attribute list structure.
2003 * This is the internal routine, it uses the caller's buffer.
2004 *
2005 * Note that duplicate keys are allowed, but only check within the
2006 * current leaf node. The Btree code must check in adjacent leaf nodes.
2007 *
2008 * Return in args->index the index into the entry[] array of either
2009 * the found entry, or where the entry should have been (insert before
2010 * that entry).
2011 *
2012 * Don't change the args->value unless we find the attribute.
2013 */
2014int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002015xfs_attr_leaf_lookup_int(
2016 struct xfs_buf *bp,
2017 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 xfs_attr_leafblock_t *leaf;
2020 xfs_attr_leaf_entry_t *entry;
2021 xfs_attr_leaf_name_local_t *name_loc;
2022 xfs_attr_leaf_name_remote_t *name_rmt;
2023 int probe, span;
2024 xfs_dahash_t hashval;
2025
Dave Chinner5a5881c2012-03-22 05:15:13 +00002026 trace_xfs_attr_leaf_lookup(args);
2027
Dave Chinner1d9025e2012-06-22 18:50:14 +10002028 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002029 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002030 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 < (XFS_LBSIZE(args->dp->i_mount)/8));
2032
2033 /*
2034 * Binary search. (note: small blocks will skip this loop)
2035 */
2036 hashval = args->hashval;
Nathan Scott918ae422006-03-17 17:28:54 +11002037 probe = span = be16_to_cpu(leaf->hdr.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 for (entry = &leaf->entries[probe]; span > 4;
2039 entry = &leaf->entries[probe]) {
2040 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002041 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002043 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 probe -= span;
2045 else
2046 break;
2047 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002048 ASSERT((probe >= 0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 (!leaf->hdr.count
Nathan Scott918ae422006-03-17 17:28:54 +11002050 || (probe < be16_to_cpu(leaf->hdr.count))));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002051 ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 /*
2054 * Since we may have duplicate hashval's, find the first matching
2055 * hashval in the leaf.
2056 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002057 while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 entry--;
2059 probe--;
2060 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002061 while ((probe < be16_to_cpu(leaf->hdr.count)) &&
2062 (be32_to_cpu(entry->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 entry++;
2064 probe++;
2065 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002066 if ((probe == be16_to_cpu(leaf->hdr.count)) ||
2067 (be32_to_cpu(entry->hashval) != hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 args->index = probe;
2069 return(XFS_ERROR(ENOATTR));
2070 }
2071
2072 /*
2073 * Duplicate keys may be present, so search all of them for a match.
2074 */
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002075 for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
2076 (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 entry++, probe++) {
2078/*
2079 * GROT: Add code to remove incomplete entries.
2080 */
2081 /*
2082 * If we are looking for INCOMPLETE entries, show only those.
2083 * If we are looking for complete entries, show only those.
2084 */
2085 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2086 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2087 continue;
2088 }
2089 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002090 name_loc = xfs_attr_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (name_loc->namelen != args->namelen)
2092 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002093 if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002095 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 continue;
2097 args->index = probe;
2098 return(XFS_ERROR(EEXIST));
2099 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002100 name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (name_rmt->namelen != args->namelen)
2102 continue;
2103 if (memcmp(args->name, (char *)name_rmt->name,
2104 args->namelen) != 0)
2105 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002106 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 continue;
2108 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002109 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002111 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return(XFS_ERROR(EEXIST));
2113 }
2114 }
2115 args->index = probe;
2116 return(XFS_ERROR(ENOATTR));
2117}
2118
2119/*
2120 * Get the value associated with an attribute name from a leaf attribute
2121 * list structure.
2122 */
2123int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002124xfs_attr_leaf_getvalue(
2125 struct xfs_buf *bp,
2126 xfs_da_args_t *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 int valuelen;
2129 xfs_attr_leafblock_t *leaf;
2130 xfs_attr_leaf_entry_t *entry;
2131 xfs_attr_leaf_name_local_t *name_loc;
2132 xfs_attr_leaf_name_remote_t *name_rmt;
2133
Dave Chinner1d9025e2012-06-22 18:50:14 +10002134 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002135 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott918ae422006-03-17 17:28:54 +11002136 ASSERT(be16_to_cpu(leaf->hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 < (XFS_LBSIZE(args->dp->i_mount)/8));
Nathan Scott918ae422006-03-17 17:28:54 +11002138 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 entry = &leaf->entries[args->index];
2141 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002142 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 ASSERT(name_loc->namelen == args->namelen);
2144 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002145 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (args->flags & ATTR_KERNOVAL) {
2147 args->valuelen = valuelen;
2148 return(0);
2149 }
2150 if (args->valuelen < valuelen) {
2151 args->valuelen = valuelen;
2152 return(XFS_ERROR(ERANGE));
2153 }
2154 args->valuelen = valuelen;
2155 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2156 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002157 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 ASSERT(name_rmt->namelen == args->namelen);
2159 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002160 valuelen = be32_to_cpu(name_rmt->valuelen);
2161 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2163 if (args->flags & ATTR_KERNOVAL) {
2164 args->valuelen = valuelen;
2165 return(0);
2166 }
2167 if (args->valuelen < valuelen) {
2168 args->valuelen = valuelen;
2169 return(XFS_ERROR(ERANGE));
2170 }
2171 args->valuelen = valuelen;
2172 }
2173 return(0);
2174}
2175
2176/*========================================================================
2177 * Utility routines.
2178 *========================================================================*/
2179
2180/*
2181 * Move the indicated entries from one leaf to another.
2182 * NOTE: this routine modifies both source and destination leaves.
2183 */
2184/*ARGSUSED*/
2185STATIC void
2186xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
2187 xfs_attr_leafblock_t *leaf_d, int start_d,
2188 int count, xfs_mount_t *mp)
2189{
2190 xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
2191 xfs_attr_leaf_entry_t *entry_s, *entry_d;
2192 int desti, tmp, i;
2193
2194 /*
2195 * Check for nothing to do.
2196 */
2197 if (count == 0)
2198 return;
2199
2200 /*
2201 * Set up environment.
2202 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002203 ASSERT(leaf_s->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
2204 ASSERT(leaf_d->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 hdr_s = &leaf_s->hdr;
2206 hdr_d = &leaf_d->hdr;
Nathan Scott918ae422006-03-17 17:28:54 +11002207 ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
2208 (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
2209 ASSERT(be16_to_cpu(hdr_s->firstused) >=
2210 ((be16_to_cpu(hdr_s->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 * sizeof(*entry_s))+sizeof(*hdr_s)));
Nathan Scott918ae422006-03-17 17:28:54 +11002212 ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
2213 ASSERT(be16_to_cpu(hdr_d->firstused) >=
2214 ((be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 * sizeof(*entry_d))+sizeof(*hdr_d)));
2216
Nathan Scott918ae422006-03-17 17:28:54 +11002217 ASSERT(start_s < be16_to_cpu(hdr_s->count));
2218 ASSERT(start_d <= be16_to_cpu(hdr_d->count));
2219 ASSERT(count <= be16_to_cpu(hdr_s->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 /*
2222 * Move the entries in the destination leaf up to make a hole?
2223 */
Nathan Scott918ae422006-03-17 17:28:54 +11002224 if (start_d < be16_to_cpu(hdr_d->count)) {
2225 tmp = be16_to_cpu(hdr_d->count) - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 tmp *= sizeof(xfs_attr_leaf_entry_t);
2227 entry_s = &leaf_d->entries[start_d];
2228 entry_d = &leaf_d->entries[start_d + count];
2229 memmove((char *)entry_d, (char *)entry_s, tmp);
2230 }
2231
2232 /*
2233 * Copy all entry's in the same (sorted) order,
2234 * but allocate attribute info packed and in sequence.
2235 */
2236 entry_s = &leaf_s->entries[start_s];
2237 entry_d = &leaf_d->entries[start_d];
2238 desti = start_d;
2239 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002240 ASSERT(be16_to_cpu(entry_s->nameidx)
Nathan Scott918ae422006-03-17 17:28:54 +11002241 >= be16_to_cpu(hdr_s->firstused));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2243#ifdef GROT
2244 /*
2245 * Code to drop INCOMPLETE entries. Difficult to use as we
2246 * may also need to change the insertion index. Code turned
2247 * off for 6.2, should be revisited later.
2248 */
2249 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002250 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002251 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2252 be16_add_cpu(&hdr_s->count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 entry_d--; /* to compensate for ++ in loop hdr */
2254 desti--;
2255 if ((start_s + i) < offset)
2256 result++; /* insertion index adjustment */
2257 } else {
2258#endif /* GROT */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002259 be16_add_cpu(&hdr_d->firstused, -tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 /* both on-disk, don't endian flip twice */
2261 entry_d->hashval = entry_s->hashval;
2262 /* both on-disk, don't endian flip twice */
2263 entry_d->nameidx = hdr_d->firstused;
2264 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002265 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002267 memmove(xfs_attr_leaf_name(leaf_d, desti),
2268 xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002269 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 <= XFS_LBSIZE(mp));
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002271 memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002272 be16_add_cpu(&hdr_s->usedbytes, -tmp);
2273 be16_add_cpu(&hdr_d->usedbytes, tmp);
2274 be16_add_cpu(&hdr_s->count, -1);
2275 be16_add_cpu(&hdr_d->count, 1);
Nathan Scott918ae422006-03-17 17:28:54 +11002276 tmp = be16_to_cpu(hdr_d->count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * sizeof(xfs_attr_leaf_entry_t)
2278 + sizeof(xfs_attr_leaf_hdr_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002279 ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280#ifdef GROT
2281 }
2282#endif /* GROT */
2283 }
2284
2285 /*
2286 * Zero out the entries we just copied.
2287 */
Nathan Scott918ae422006-03-17 17:28:54 +11002288 if (start_s == be16_to_cpu(hdr_s->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2290 entry_s = &leaf_s->entries[start_s];
2291 ASSERT(((char *)entry_s + tmp) <=
2292 ((char *)leaf_s + XFS_LBSIZE(mp)));
2293 memset((char *)entry_s, 0, tmp);
2294 } else {
2295 /*
2296 * Move the remaining entries down to fill the hole,
2297 * then zero the entries at the top.
2298 */
Nathan Scott918ae422006-03-17 17:28:54 +11002299 tmp = be16_to_cpu(hdr_s->count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 tmp *= sizeof(xfs_attr_leaf_entry_t);
2301 entry_s = &leaf_s->entries[start_s + count];
2302 entry_d = &leaf_s->entries[start_s];
2303 memmove((char *)entry_d, (char *)entry_s, tmp);
2304
2305 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Nathan Scott918ae422006-03-17 17:28:54 +11002306 entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 ASSERT(((char *)entry_s + tmp) <=
2308 ((char *)leaf_s + XFS_LBSIZE(mp)));
2309 memset((char *)entry_s, 0, tmp);
2310 }
2311
2312 /*
2313 * Fill in the freemap information
2314 */
Nathan Scott918ae422006-03-17 17:28:54 +11002315 hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002316 be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
Nathan Scott918ae422006-03-17 17:28:54 +11002317 sizeof(xfs_attr_leaf_entry_t));
2318 hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
2319 - be16_to_cpu(hdr_d->freemap[0].base));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 hdr_d->freemap[1].base = 0;
2321 hdr_d->freemap[2].base = 0;
2322 hdr_d->freemap[1].size = 0;
2323 hdr_d->freemap[2].size = 0;
2324 hdr_s->holes = 1; /* leaf may not be compact */
2325}
2326
2327/*
2328 * Compare two leaf blocks "order".
2329 * Return 0 unless leaf2 should go before leaf1.
2330 */
2331int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002332xfs_attr_leaf_order(
2333 struct xfs_buf *leaf1_bp,
2334 struct xfs_buf *leaf2_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 xfs_attr_leafblock_t *leaf1, *leaf2;
2337
Dave Chinner1d9025e2012-06-22 18:50:14 +10002338 leaf1 = leaf1_bp->b_addr;
2339 leaf2 = leaf2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002340 ASSERT((leaf1->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) &&
2341 (leaf2->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)));
Nathan Scott918ae422006-03-17 17:28:54 +11002342 if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
2343 (be16_to_cpu(leaf2->hdr.count) > 0) &&
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002344 ((be32_to_cpu(leaf2->entries[0].hashval) <
2345 be32_to_cpu(leaf1->entries[0].hashval)) ||
2346 (be32_to_cpu(leaf2->entries[
2347 be16_to_cpu(leaf2->hdr.count)-1].hashval) <
2348 be32_to_cpu(leaf1->entries[
2349 be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return(1);
2351 }
2352 return(0);
2353}
2354
2355/*
2356 * Pick up the last hashvalue from a leaf block.
2357 */
2358xfs_dahash_t
Dave Chinner1d9025e2012-06-22 18:50:14 +10002359xfs_attr_leaf_lasthash(
2360 struct xfs_buf *bp,
2361 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
2363 xfs_attr_leafblock_t *leaf;
2364
Dave Chinner1d9025e2012-06-22 18:50:14 +10002365 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002366 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (count)
Nathan Scott918ae422006-03-17 17:28:54 +11002368 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (!leaf->hdr.count)
2370 return(0);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002371 return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
2374/*
2375 * Calculate the number of bytes used to store the indicated attribute
2376 * (whether local or remote only calculate bytes in this block).
2377 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002378STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2380{
2381 xfs_attr_leaf_name_local_t *name_loc;
2382 xfs_attr_leaf_name_remote_t *name_rmt;
2383 int size;
2384
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002385 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002387 name_loc = xfs_attr_leaf_name_local(leaf, index);
2388 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002389 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002391 name_rmt = xfs_attr_leaf_name_remote(leaf, index);
2392 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
2394 return(size);
2395}
2396
2397/*
2398 * Calculate the number of bytes that would be required to store the new
2399 * attribute (whether local or remote only calculate bytes in this block).
2400 * This routine decides as a side effect whether the attribute will be
2401 * a "local" or a "remote" attribute.
2402 */
2403int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002404xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 int size;
2407
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002408 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2409 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (local) {
2411 *local = 1;
2412 }
2413 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002414 size = xfs_attr_leaf_entsize_remote(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (local) {
2416 *local = 0;
2417 }
2418 }
2419 return(size);
2420}
2421
2422/*
2423 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2424 */
2425int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002426xfs_attr_leaf_list_int(
2427 struct xfs_buf *bp,
2428 xfs_attr_list_context_t *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
2430 attrlist_cursor_kern_t *cursor;
2431 xfs_attr_leafblock_t *leaf;
2432 xfs_attr_leaf_entry_t *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 int retval, i;
2434
2435 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002436 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 cursor = context->cursor;
2438 cursor->initted = 1;
2439
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002440 trace_xfs_attr_list_leaf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 /*
2443 * Re-find our place in the leaf block if this is a new syscall.
2444 */
2445 if (context->resynch) {
2446 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002447 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002448 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if (cursor->offset == context->dupcnt) {
2450 context->dupcnt = 0;
2451 break;
2452 }
2453 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002454 } else if (be32_to_cpu(entry->hashval) >
2455 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 context->dupcnt = 0;
2457 break;
2458 }
2459 }
Nathan Scott918ae422006-03-17 17:28:54 +11002460 if (i == be16_to_cpu(leaf->hdr.count)) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002461 trace_xfs_attr_list_notfound(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return(0);
2463 }
2464 } else {
2465 entry = &leaf->entries[0];
2466 i = 0;
2467 }
2468 context->resynch = 0;
2469
2470 /*
2471 * We have found our place, start copying out the new attributes.
2472 */
2473 retval = 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10002474 for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002475 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2476 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 cursor->offset = 0;
2478 }
2479
2480 if (entry->flags & XFS_ATTR_INCOMPLETE)
2481 continue; /* skip incomplete entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002484 xfs_attr_leaf_name_local_t *name_loc =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002485 xfs_attr_leaf_name_local(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002486
2487 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002488 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002489 name_loc->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +10002490 (int)name_loc->namelen,
2491 be16_to_cpu(name_loc->valuelen),
Dave Chinnera9273ca2010-01-20 10:47:48 +11002492 &name_loc->nameval[name_loc->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +10002493 if (retval)
2494 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002496 xfs_attr_leaf_name_remote_t *name_rmt =
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002497 xfs_attr_leaf_name_remote(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002498
2499 int valuelen = be32_to_cpu(name_rmt->valuelen);
2500
2501 if (context->put_value) {
2502 xfs_da_args_t args;
2503
2504 memset((char *)&args, 0, sizeof(args));
2505 args.dp = context->dp;
2506 args.whichfork = XFS_ATTR_FORK;
2507 args.valuelen = valuelen;
Dave Chinner622d8142010-12-23 11:57:37 +11002508 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
Tim Shimmin726801b2006-09-28 11:01:37 +10002509 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2510 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2511 retval = xfs_attr_rmtval_get(&args);
2512 if (retval)
2513 return retval;
2514 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002515 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002516 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002517 (int)name_rmt->namelen,
2518 valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002519 args.value);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002520 kmem_free(args.value);
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002521 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002522 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002523 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002524 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002525 (int)name_rmt->namelen,
2526 valuelen,
2527 NULL);
2528 }
2529 if (retval)
2530 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002532 if (context->seen_enough)
2533 break;
2534 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002536 trace_xfs_attr_list_leaf_end(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return(retval);
2538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541/*========================================================================
2542 * Manage the INCOMPLETE flag in a leaf entry
2543 *========================================================================*/
2544
2545/*
2546 * Clear the INCOMPLETE flag on an entry in a leaf block.
2547 */
2548int
2549xfs_attr_leaf_clearflag(xfs_da_args_t *args)
2550{
2551 xfs_attr_leafblock_t *leaf;
2552 xfs_attr_leaf_entry_t *entry;
2553 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002554 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 int error;
2556#ifdef DEBUG
2557 xfs_attr_leaf_name_local_t *name_loc;
2558 int namelen;
2559 char *name;
2560#endif /* DEBUG */
2561
Dave Chinner5a5881c2012-03-22 05:15:13 +00002562 trace_xfs_attr_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /*
2564 * Set up the operation.
2565 */
Dave Chinnerad14c332012-11-12 22:54:16 +11002566 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2567 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Dave Chinner1d9025e2012-06-22 18:50:14 +10002570 leaf = bp->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002571 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 ASSERT(args->index >= 0);
2573 entry = &leaf->entries[ args->index ];
2574 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2575
2576#ifdef DEBUG
2577 if (entry->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002578 name_loc = xfs_attr_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 namelen = name_loc->namelen;
2580 name = (char *)name_loc->nameval;
2581 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002582 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 namelen = name_rmt->namelen;
2584 name = (char *)name_rmt->name;
2585 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002586 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 ASSERT(namelen == args->namelen);
2588 ASSERT(memcmp(name, args->name, namelen) == 0);
2589#endif /* DEBUG */
2590
2591 entry->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002592 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2594
2595 if (args->rmtblkno) {
2596 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002597 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002598 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2599 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002600 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 /*
2605 * Commit the flag value change and start the next trans in series.
2606 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002607 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
2610/*
2611 * Set the INCOMPLETE flag on an entry in a leaf block.
2612 */
2613int
2614xfs_attr_leaf_setflag(xfs_da_args_t *args)
2615{
2616 xfs_attr_leafblock_t *leaf;
2617 xfs_attr_leaf_entry_t *entry;
2618 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002619 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 int error;
2621
Dave Chinner5a5881c2012-03-22 05:15:13 +00002622 trace_xfs_attr_leaf_setflag(args);
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 /*
2625 * Set up the operation.
2626 */
Dave Chinnerad14c332012-11-12 22:54:16 +11002627 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2628 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Dave Chinner1d9025e2012-06-22 18:50:14 +10002631 leaf = bp->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002632 ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 ASSERT(args->index >= 0);
2634 entry = &leaf->entries[ args->index ];
2635
2636 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2637 entry->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002638 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2640 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002641 name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 name_rmt->valueblk = 0;
2643 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002644 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 /*
2649 * Commit the flag value change and start the next trans in series.
2650 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002651 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
2654/*
2655 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2656 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2657 * entry given by args->blkno2/index2.
2658 *
2659 * Note that they could be in different blocks, or in the same block.
2660 */
2661int
2662xfs_attr_leaf_flipflags(xfs_da_args_t *args)
2663{
2664 xfs_attr_leafblock_t *leaf1, *leaf2;
2665 xfs_attr_leaf_entry_t *entry1, *entry2;
2666 xfs_attr_leaf_name_remote_t *name_rmt;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002667 struct xfs_buf *bp1, *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 int error;
2669#ifdef DEBUG
2670 xfs_attr_leaf_name_local_t *name_loc;
2671 int namelen1, namelen2;
2672 char *name1, *name2;
2673#endif /* DEBUG */
2674
Dave Chinner5a5881c2012-03-22 05:15:13 +00002675 trace_xfs_attr_leaf_flipflags(args);
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 /*
2678 * Read the block containing the "old" attr
2679 */
Dave Chinnerad14c332012-11-12 22:54:16 +11002680 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
2681 if (error)
2682 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 /*
2685 * Read the block containing the "new" attr, if it is different
2686 */
2687 if (args->blkno2 != args->blkno) {
Dave Chinnerad14c332012-11-12 22:54:16 +11002688 error = xfs_attr_leaf_read(args->trans, args->dp, args->blkno2,
2689 -1, &bp2);
2690 if (error)
2691 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 } else {
2693 bp2 = bp1;
2694 }
2695
Dave Chinner1d9025e2012-06-22 18:50:14 +10002696 leaf1 = bp1->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002697 ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 ASSERT(args->index >= 0);
2699 entry1 = &leaf1->entries[ args->index ];
2700
Dave Chinner1d9025e2012-06-22 18:50:14 +10002701 leaf2 = bp2->b_addr;
Nathan Scott918ae422006-03-17 17:28:54 +11002702 ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 ASSERT(args->index2 >= 0);
2704 entry2 = &leaf2->entries[ args->index2 ];
2705
2706#ifdef DEBUG
2707 if (entry1->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002708 name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 namelen1 = name_loc->namelen;
2710 name1 = (char *)name_loc->nameval;
2711 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002712 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 namelen1 = name_rmt->namelen;
2714 name1 = (char *)name_rmt->name;
2715 }
2716 if (entry2->flags & XFS_ATTR_LOCAL) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002717 name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 namelen2 = name_loc->namelen;
2719 name2 = (char *)name_loc->nameval;
2720 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002721 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 namelen2 = name_rmt->namelen;
2723 name2 = (char *)name_rmt->name;
2724 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002725 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 ASSERT(namelen1 == namelen2);
2727 ASSERT(memcmp(name1, name2, namelen1) == 0);
2728#endif /* DEBUG */
2729
2730 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2731 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2732
2733 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002734 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2736 if (args->rmtblkno) {
2737 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002738 name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002739 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2740 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002741 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2743 }
2744
2745 entry2->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002746 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2748 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002749 name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 name_rmt->valueblk = 0;
2751 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002752 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 /*
2757 * Commit the flag value change and start the next trans in series.
2758 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002759 error = xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 return(error);
2762}
2763
2764/*========================================================================
2765 * Indiscriminately delete the entire attribute fork
2766 *========================================================================*/
2767
2768/*
2769 * Recurse (gasp!) through the attribute nodes until we find leaves.
2770 * We're doing a depth-first traversal in order to invalidate everything.
2771 */
2772int
2773xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
2774{
2775 xfs_da_blkinfo_t *info;
2776 xfs_daddr_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002777 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 int error;
2779
2780 /*
2781 * Read block 0 to see what we have to work with.
2782 * We only get here if we have extents, since we remove
2783 * the extents in reverse order the extent containing
2784 * block 0 must still be there.
2785 */
Dave Chinnerd9392a42012-11-12 22:54:17 +11002786 error = xfs_da_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (error)
2788 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002789 blkno = XFS_BUF_ADDR(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 /*
2792 * Invalidate the tree, even if the "tree" is only a single leaf block.
2793 * This is a depth-first traversal!
2794 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002795 info = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002796 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 error = xfs_attr_node_inactive(trans, dp, bp, 1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002798 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 error = xfs_attr_leaf_inactive(trans, dp, bp);
2800 } else {
2801 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002802 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804 if (error)
2805 return(error);
2806
2807 /*
2808 * Invalidate the incore copy of the root block.
2809 */
2810 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
2811 if (error)
2812 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002813 xfs_trans_binval(*trans, bp); /* remove from cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 /*
2815 * Commit the invalidate and start the next transaction.
2816 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002817 error = xfs_trans_roll(trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 return (error);
2820}
2821
2822/*
2823 * Recurse (gasp!) through the attribute nodes until we find leaves.
2824 * We're doing a depth-first traversal in order to invalidate everything.
2825 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002826STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002827xfs_attr_node_inactive(
2828 struct xfs_trans **trans,
2829 struct xfs_inode *dp,
2830 struct xfs_buf *bp,
2831 int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 xfs_da_blkinfo_t *info;
2834 xfs_da_intnode_t *node;
2835 xfs_dablk_t child_fsb;
2836 xfs_daddr_t parent_blkno, child_blkno;
2837 int error, count, i;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002838 struct xfs_buf *child_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 /*
2841 * Since this code is recursive (gasp!) we must protect ourselves.
2842 */
2843 if (level > XFS_DA_NODE_MAXDEPTH) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002844 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return(XFS_ERROR(EIO));
2846 }
2847
Dave Chinner1d9025e2012-06-22 18:50:14 +10002848 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002849 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +10002850 parent_blkno = XFS_BUF_ADDR(bp); /* save for re-read later */
Nathan Scottfac80cc2006-03-17 17:29:56 +11002851 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (!count) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002853 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return(0);
2855 }
Nathan Scott403432d2006-03-17 17:29:46 +11002856 child_fsb = be32_to_cpu(node->btree[0].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002857 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 /*
2860 * If this is the node level just above the leaves, simply loop
2861 * over the leaves removing all of them. If this is higher up
2862 * in the tree, recurse downward.
2863 */
2864 for (i = 0; i < count; i++) {
2865 /*
2866 * Read the subsidiary block to see what we have to work with.
2867 * Don't do this in a transaction. This is a depth-first
2868 * traversal of the tree so we may deal with many blocks
2869 * before we come back to this one.
2870 */
Dave Chinnerd9392a42012-11-12 22:54:17 +11002871 error = xfs_da_node_read(*trans, dp, child_fsb, -2, &child_bp,
2872 XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (error)
2874 return(error);
2875 if (child_bp) {
2876 /* save for re-read later */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002877 child_blkno = XFS_BUF_ADDR(child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879 /*
2880 * Invalidate the subtree, however we have to.
2881 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002882 info = child_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002883 if (info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 error = xfs_attr_node_inactive(trans, dp,
2885 child_bp, level+1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002886 } else if (info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 error = xfs_attr_leaf_inactive(trans, dp,
2888 child_bp);
2889 } else {
2890 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002891 xfs_trans_brelse(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
2893 if (error)
2894 return(error);
2895
2896 /*
2897 * Remove the subsidiary block from the cache
2898 * and from the log.
2899 */
2900 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
2901 &child_bp, XFS_ATTR_FORK);
2902 if (error)
2903 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002904 xfs_trans_binval(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
2906
2907 /*
2908 * If we're not done, re-read the parent to get the next
2909 * child block number.
2910 */
2911 if ((i+1) < count) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11002912 error = xfs_da_node_read(*trans, dp, 0, parent_blkno,
2913 &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (error)
2915 return(error);
Nathan Scott403432d2006-03-17 17:29:46 +11002916 child_fsb = be32_to_cpu(node->btree[i+1].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002917 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
2919 /*
2920 * Atomically commit the whole invalidate stuff.
2921 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002922 error = xfs_trans_roll(trans, dp);
2923 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return (error);
2925 }
2926
2927 return(0);
2928}
2929
2930/*
2931 * Invalidate all of the "remote" value regions pointed to by a particular
2932 * leaf block.
2933 * Note that we must release the lock on the buffer so that we are not
2934 * caught holding something that the logging code wants to flush to disk.
2935 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002936STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002937xfs_attr_leaf_inactive(
2938 struct xfs_trans **trans,
2939 struct xfs_inode *dp,
2940 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
2942 xfs_attr_leafblock_t *leaf;
2943 xfs_attr_leaf_entry_t *entry;
2944 xfs_attr_leaf_name_remote_t *name_rmt;
2945 xfs_attr_inactive_list_t *list, *lp;
2946 int error, count, size, tmp, i;
2947
Dave Chinner1d9025e2012-06-22 18:50:14 +10002948 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002949 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 /*
2952 * Count the number of "remote" value extents.
2953 */
2954 count = 0;
2955 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002956 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002957 if (be16_to_cpu(entry->nameidx) &&
2958 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002959 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (name_rmt->valueblk)
2961 count++;
2962 }
2963 }
2964
2965 /*
2966 * If there are no "remote" values, we're done.
2967 */
2968 if (count == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002969 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return(0);
2971 }
2972
2973 /*
2974 * Allocate storage for a list of all the "remote" value extents.
2975 */
2976 size = count * sizeof(xfs_attr_inactive_list_t);
2977 list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
2978
2979 /*
2980 * Identify each of the "remote" value extents.
2981 */
2982 lp = list;
2983 entry = &leaf->entries[0];
Nathan Scott918ae422006-03-17 17:28:54 +11002984 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002985 if (be16_to_cpu(entry->nameidx) &&
2986 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002987 name_rmt = xfs_attr_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11002989 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
2990 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
2991 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 lp++;
2993 }
2994 }
2995 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002996 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 /*
2999 * Invalidate each of the "remote" value extents.
3000 */
3001 error = 0;
3002 for (lp = list, i = 0; i < count; i++, lp++) {
3003 tmp = xfs_attr_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11003004 lp->valueblk, lp->valuelen);
3005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (error == 0)
3007 error = tmp; /* save only the 1st errno */
3008 }
3009
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003010 kmem_free((xfs_caddr_t)list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return(error);
3012}
3013
3014/*
3015 * Look at all the extents for this logical region,
3016 * invalidate any buffers that are incore/in transactions.
3017 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003018STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
3020 xfs_dablk_t blkno, int blkcnt)
3021{
3022 xfs_bmbt_irec_t map;
3023 xfs_dablk_t tblkno;
3024 int tblkcnt, dblkcnt, nmap, error;
3025 xfs_daddr_t dblkno;
3026 xfs_buf_t *bp;
3027
3028 /*
3029 * Roll through the "value", invalidating the attribute value's
3030 * blocks.
3031 */
3032 tblkno = blkno;
3033 tblkcnt = blkcnt;
3034 while (tblkcnt > 0) {
3035 /*
3036 * Try to remember where we decided to put the value.
3037 */
3038 nmap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003039 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
3040 &map, &nmap, XFS_BMAPI_ATTRFORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 if (error) {
3042 return(error);
3043 }
3044 ASSERT(nmap == 1);
3045 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3046
3047 /*
3048 * If it's a hole, these are already unmapped
3049 * so there's nothing to invalidate.
3050 */
3051 if (map.br_startblock != HOLESTARTBLOCK) {
3052
3053 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3054 map.br_startblock);
3055 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3056 map.br_blockcount);
3057 bp = xfs_trans_get_buf(*trans,
3058 dp->i_mount->m_ddev_targp,
Dave Chinnera8acad72012-04-23 15:58:54 +10003059 dblkno, dblkcnt, 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00003060 if (!bp)
3061 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 xfs_trans_binval(*trans, bp);
3063 /*
3064 * Roll to next transaction.
3065 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003066 error = xfs_trans_roll(trans, dp);
3067 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return (error);
3069 }
3070
3071 tblkno += map.br_blockcount;
3072 tblkcnt -= map.br_blockcount;
3073 }
3074
3075 return(0);
3076}