blob: 287c8089b7055d8a113e3d89bc9a39c148d53535 [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.
Dave Chinner517c2222013-04-24 18:58:55 +10003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
32#include "xfs_alloc.h"
33#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110035#include "xfs_attr_remote.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_attr.h"
41#include "xfs_attr_leaf.h"
42#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Dave Chinner517c2222013-04-24 18:58:55 +100044#include "xfs_buf_item.h"
45#include "xfs_cksum.h"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * xfs_attr_leaf.c
50 *
51 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
52 */
53
54/*========================================================================
55 * Function prototypes for the kernel.
56 *========================================================================*/
57
58/*
59 * Routines used for growing the Btree.
60 */
Dave Chinner517c2222013-04-24 18:58:55 +100061STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args,
62 xfs_dablk_t which_block, struct xfs_buf **bpp);
63STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer,
64 struct xfs_attr3_icleaf_hdr *ichdr,
65 struct xfs_da_args *args, int freemap_index);
66STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args,
67 struct xfs_attr3_icleaf_hdr *ichdr,
68 struct xfs_buf *leaf_buffer);
69STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 xfs_da_state_blk_t *blk1,
71 xfs_da_state_blk_t *blk2);
Dave Chinner517c2222013-04-24 18:58:55 +100072STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
73 xfs_da_state_blk_t *leaf_blk_1,
74 struct xfs_attr3_icleaf_hdr *ichdr1,
75 xfs_da_state_blk_t *leaf_blk_2,
76 struct xfs_attr3_icleaf_hdr *ichdr2,
77 int *number_entries_in_blk1,
78 int *number_usedbytes_in_blk1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/*
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100081 * Routines used for shrinking the Btree.
82 */
Dave Chinner517c2222013-04-24 18:58:55 +100083STATIC int xfs_attr3_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +100084 struct xfs_buf *bp, int level);
Dave Chinner517c2222013-04-24 18:58:55 +100085STATIC int xfs_attr3_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +100086 struct xfs_buf *bp);
Dave Chinner517c2222013-04-24 18:58:55 +100087STATIC int xfs_attr3_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100088 xfs_dablk_t blkno, int blkcnt);
89
90/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * Utility routines.
92 */
Dave Chinner517c2222013-04-24 18:58:55 +100093STATIC void xfs_attr3_leaf_moveents(struct xfs_attr_leafblock *src_leaf,
94 struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
95 struct xfs_attr_leafblock *dst_leaf,
96 struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
97 int move_count, struct xfs_mount *mp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100098STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
Tim Shimmin726801b2006-09-28 11:01:37 +100099
Dave Chinner517c2222013-04-24 18:58:55 +1000100void
101xfs_attr3_leaf_hdr_from_disk(
102 struct xfs_attr3_icleaf_hdr *to,
103 struct xfs_attr_leafblock *from)
104{
105 int i;
106
107 ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
108 from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
109
110 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
111 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from;
112
113 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
114 to->back = be32_to_cpu(hdr3->info.hdr.back);
115 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
116 to->count = be16_to_cpu(hdr3->count);
117 to->usedbytes = be16_to_cpu(hdr3->usedbytes);
118 to->firstused = be16_to_cpu(hdr3->firstused);
119 to->holes = hdr3->holes;
120
121 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
122 to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base);
123 to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size);
124 }
125 return;
126 }
127 to->forw = be32_to_cpu(from->hdr.info.forw);
128 to->back = be32_to_cpu(from->hdr.info.back);
129 to->magic = be16_to_cpu(from->hdr.info.magic);
130 to->count = be16_to_cpu(from->hdr.count);
131 to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
132 to->firstused = be16_to_cpu(from->hdr.firstused);
133 to->holes = from->hdr.holes;
134
135 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
136 to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base);
137 to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size);
138 }
139}
140
141void
142xfs_attr3_leaf_hdr_to_disk(
143 struct xfs_attr_leafblock *to,
144 struct xfs_attr3_icleaf_hdr *from)
145{
146 int i;
147
148 ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
149 from->magic == XFS_ATTR3_LEAF_MAGIC);
150
151 if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
152 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to;
153
154 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
155 hdr3->info.hdr.back = cpu_to_be32(from->back);
156 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
157 hdr3->count = cpu_to_be16(from->count);
158 hdr3->usedbytes = cpu_to_be16(from->usedbytes);
159 hdr3->firstused = cpu_to_be16(from->firstused);
160 hdr3->holes = from->holes;
161 hdr3->pad1 = 0;
162
163 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
164 hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base);
165 hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size);
166 }
167 return;
168 }
169 to->hdr.info.forw = cpu_to_be32(from->forw);
170 to->hdr.info.back = cpu_to_be32(from->back);
171 to->hdr.info.magic = cpu_to_be16(from->magic);
172 to->hdr.count = cpu_to_be16(from->count);
173 to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
174 to->hdr.firstused = cpu_to_be16(from->firstused);
175 to->hdr.holes = from->holes;
176 to->hdr.pad1 = 0;
177
178 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
179 to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base);
180 to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size);
181 }
182}
183
184static bool
185xfs_attr3_leaf_verify(
Dave Chinnerad14c332012-11-12 22:54:16 +1100186 struct xfs_buf *bp)
187{
188 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner517c2222013-04-24 18:58:55 +1000189 struct xfs_attr_leafblock *leaf = bp->b_addr;
190 struct xfs_attr3_icleaf_hdr ichdr;
Dave Chinnerad14c332012-11-12 22:54:16 +1100191
Dave Chinner517c2222013-04-24 18:58:55 +1000192 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
193
194 if (xfs_sb_version_hascrc(&mp->m_sb)) {
195 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
196
197 if (ichdr.magic != XFS_ATTR3_LEAF_MAGIC)
198 return false;
199
200 if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_uuid))
201 return false;
202 if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
203 return false;
204 } else {
205 if (ichdr.magic != XFS_ATTR_LEAF_MAGIC)
206 return false;
207 }
208 if (ichdr.count == 0)
209 return false;
210
211 /* XXX: need to range check rest of attr header values */
212 /* XXX: hash order check? */
213
214 return true;
215}
216
217static void
218xfs_attr3_leaf_write_verify(
219 struct xfs_buf *bp)
220{
221 struct xfs_mount *mp = bp->b_target->bt_mount;
222 struct xfs_buf_log_item *bip = bp->b_fspriv;
223 struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
224
225 if (!xfs_attr3_leaf_verify(bp)) {
226 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
227 xfs_buf_ioerror(bp, EFSCORRUPTED);
228 return;
229 }
230
231 if (!xfs_sb_version_hascrc(&mp->m_sb))
232 return;
233
234 if (bip)
235 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
236
237 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_ATTR3_LEAF_CRC_OFF);
238}
239
240/*
241 * leaf/node format detection on trees is sketchy, so a node read can be done on
242 * leaf level blocks when detection identifies the tree as a node format tree
243 * incorrectly. In this case, we need to swap the verifier to match the correct
244 * format of the block being read.
245 */
246static void
247xfs_attr3_leaf_read_verify(
248 struct xfs_buf *bp)
249{
250 struct xfs_mount *mp = bp->b_target->bt_mount;
251
252 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
253 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
254 XFS_ATTR3_LEAF_CRC_OFF)) ||
255 !xfs_attr3_leaf_verify(bp)) {
256 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
Dave Chinnerad14c332012-11-12 22:54:16 +1100257 xfs_buf_ioerror(bp, EFSCORRUPTED);
258 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100259}
Dave Chinnerad14c332012-11-12 22:54:16 +1100260
Dave Chinner517c2222013-04-24 18:58:55 +1000261const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
262 .verify_read = xfs_attr3_leaf_read_verify,
263 .verify_write = xfs_attr3_leaf_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100264};
Dave Chinner612cfbf2012-11-14 17:52:32 +1100265
Dave Chinnerad14c332012-11-12 22:54:16 +1100266int
Dave Chinner517c2222013-04-24 18:58:55 +1000267xfs_attr3_leaf_read(
Dave Chinnerad14c332012-11-12 22:54:16 +1100268 struct xfs_trans *tp,
269 struct xfs_inode *dp,
270 xfs_dablk_t bno,
271 xfs_daddr_t mappedbno,
272 struct xfs_buf **bpp)
273{
274 return xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinner517c2222013-04-24 18:58:55 +1000275 XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops);
Dave Chinnerad14c332012-11-12 22:54:16 +1100276}
277
Tim Shimmin726801b2006-09-28 11:01:37 +1000278/*========================================================================
279 * Namespace helper routines
280 *========================================================================*/
281
Tim Shimmin726801b2006-09-28 11:01:37 +1000282/*
283 * If namespace bits don't match return 0.
284 * If all match then return 1.
285 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000286STATIC int
Tim Shimmin726801b2006-09-28 11:01:37 +1000287xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
288{
289 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100294 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *========================================================================*/
296
297/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100298 * Query whether the requested number of additional bytes of extended
299 * attribute space will be able to fit inline.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000300 *
Nathan Scottd8cc8902005-11-02 10:34:53 +1100301 * Returns zero if not, else the di_forkoff fork offset to be used in the
302 * literal area for attribute data once the new bytes have been added.
303 *
304 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
305 * special case for dev/uuid inodes, they have fixed size data forks.
306 */
307int
308xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
309{
310 int offset;
311 int minforkoff; /* lower limit on valid forkoff locations */
312 int maxforkoff; /* upper limit on valid forkoff locations */
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000313 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100314 xfs_mount_t *mp = dp->i_mount;
315
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100316 /* rounded down */
317 offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100318
319 switch (dp->i_d.di_format) {
320 case XFS_DINODE_FMT_DEV:
321 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
322 return (offset >= minforkoff) ? minforkoff : 0;
323 case XFS_DINODE_FMT_UUID:
324 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
325 return (offset >= minforkoff) ? minforkoff : 0;
326 }
327
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000328 /*
329 * If the requested numbers of bytes is smaller or equal to the
330 * current attribute fork size we can always proceed.
331 *
332 * Note that if_bytes in the data fork might actually be larger than
333 * the current data fork size is due to delalloc extents. In that
334 * case either the extent count will go down when they are converted
335 * to real extents, or the delalloc conversion will take care of the
336 * literal area rebalancing.
337 */
338 if (bytes <= XFS_IFORK_ASIZE(dp))
339 return dp->i_d.di_forkoff;
340
341 /*
342 * For attr2 we can try to move the forkoff if there is space in the
343 * literal area, but for the old format we are done if there is no
344 * space in the fixed attribute fork.
345 */
346 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
Nathan Scottda087ba2005-11-02 15:00:20 +1100347 return 0;
Nathan Scottda087ba2005-11-02 15:00:20 +1100348
Barry Naujoke5889e92007-02-10 18:35:58 +1100349 dsize = dp->i_df.if_bytes;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000350
Barry Naujoke5889e92007-02-10 18:35:58 +1100351 switch (dp->i_d.di_format) {
352 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000353 /*
Barry Naujoke5889e92007-02-10 18:35:58 +1100354 * If there is no attr fork and the data fork is extents,
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000355 * determine if creating the default attr fork will result
356 * in the extents form migrating to btree. If so, the
357 * minimum offset only needs to be the space required for
Barry Naujoke5889e92007-02-10 18:35:58 +1100358 * the btree root.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000359 */
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200360 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
361 xfs_default_attroffset(dp))
Barry Naujoke5889e92007-02-10 18:35:58 +1100362 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
363 break;
Barry Naujoke5889e92007-02-10 18:35:58 +1100364 case XFS_DINODE_FMT_BTREE:
365 /*
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000366 * If we have a data btree then keep forkoff if we have one,
367 * otherwise we are adding a new attr, so then we set
368 * minforkoff to where the btree root can finish so we have
Barry Naujoke5889e92007-02-10 18:35:58 +1100369 * plenty of room for attrs
370 */
371 if (dp->i_d.di_forkoff) {
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000372 if (offset < dp->i_d.di_forkoff)
Barry Naujoke5889e92007-02-10 18:35:58 +1100373 return 0;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000374 return dp->i_d.di_forkoff;
375 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500376 dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
Barry Naujoke5889e92007-02-10 18:35:58 +1100377 break;
378 }
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000379
380 /*
381 * A data fork btree root must have space for at least
Barry Naujoke5889e92007-02-10 18:35:58 +1100382 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
383 */
384 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100385 minforkoff = roundup(minforkoff, 8) >> 3;
386
387 /* attr fork btree root can have at least this many key/ptr pairs */
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100388 maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
389 XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100390 maxforkoff = maxforkoff >> 3; /* rounded down */
391
Nathan Scottd8cc8902005-11-02 10:34:53 +1100392 if (offset >= maxforkoff)
393 return maxforkoff;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000394 if (offset >= minforkoff)
395 return offset;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100396 return 0;
397}
398
399/*
400 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
401 */
402STATIC void
403xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
404{
Nathan Scott13059ff2006-01-11 15:32:01 +1100405 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100406 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000407 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100408 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
409 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000410 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100411 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
412 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000413 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100414 }
415}
416
417/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * Create the initial contents of a shortform attribute list.
419 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100420void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421xfs_attr_shortform_create(xfs_da_args_t *args)
422{
423 xfs_attr_sf_hdr_t *hdr;
424 xfs_inode_t *dp;
425 xfs_ifork_t *ifp;
426
Dave Chinner5a5881c2012-03-22 05:15:13 +0000427 trace_xfs_attr_sf_create(args);
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dp = args->dp;
430 ASSERT(dp != NULL);
431 ifp = dp->i_afp;
432 ASSERT(ifp != NULL);
433 ASSERT(ifp->if_bytes == 0);
434 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
435 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
436 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
437 ifp->if_flags |= XFS_IFINLINE;
438 } else {
439 ASSERT(ifp->if_flags & XFS_IFINLINE);
440 }
441 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
442 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
443 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100444 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
449 * Add a name/value pair to the shortform attribute list.
450 * Overflow from the inode has already been checked for.
451 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100452void
453xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 xfs_attr_shortform_t *sf;
456 xfs_attr_sf_entry_t *sfe;
457 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100458 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 xfs_inode_t *dp;
460 xfs_ifork_t *ifp;
461
Dave Chinner5a5881c2012-03-22 05:15:13 +0000462 trace_xfs_attr_sf_add(args);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100465 mp = dp->i_mount;
466 dp->i_d.di_forkoff = forkoff;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 ifp = dp->i_afp;
469 ASSERT(ifp->if_flags & XFS_IFINLINE);
470 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
471 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100472 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100473#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (sfe->namelen != args->namelen)
475 continue;
476 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
477 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000478 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100480 ASSERT(0);
481#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
484 offset = (char *)sfe - (char *)sf;
485 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
486 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
487 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
488 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
489
490 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100491 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000492 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 memcpy(sfe->nameval, args->name, args->namelen);
494 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100495 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800496 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
498
Nathan Scottd8cc8902005-11-02 10:34:53 +1100499 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502/*
Christoph Hellwige1486de2009-02-04 09:36:00 +0100503 * After the last attribute is removed revert to original inode format,
504 * making all literal area available to the data fork once more.
505 */
506STATIC void
507xfs_attr_fork_reset(
508 struct xfs_inode *ip,
509 struct xfs_trans *tp)
510{
511 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
512 ip->i_d.di_forkoff = 0;
513 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
514
515 ASSERT(ip->i_d.di_anextents == 0);
516 ASSERT(ip->i_afp == NULL);
517
Christoph Hellwige1486de2009-02-04 09:36:00 +0100518 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
519}
520
521/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100522 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
524int
525xfs_attr_shortform_remove(xfs_da_args_t *args)
526{
527 xfs_attr_shortform_t *sf;
528 xfs_attr_sf_entry_t *sfe;
529 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100530 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 xfs_inode_t *dp;
532
Dave Chinner5a5881c2012-03-22 05:15:13 +0000533 trace_xfs_attr_sf_remove(args);
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100536 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 base = sizeof(xfs_attr_sf_hdr_t);
538 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
539 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100540 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100541 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 base += size, i++) {
543 size = XFS_ATTR_SF_ENTSIZE(sfe);
544 if (sfe->namelen != args->namelen)
545 continue;
546 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
547 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000548 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 continue;
550 break;
551 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100552 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return(XFS_ERROR(ENOATTR));
554
Nathan Scottd8cc8902005-11-02 10:34:53 +1100555 /*
556 * Fix up the attribute fork data, covering the hole
557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100559 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100560 if (end != totsize)
561 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100562 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800563 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100564
565 /*
566 * Fix up the start offset of the attribute fork
567 */
568 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000569 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
Christoph Hellwige1486de2009-02-04 09:36:00 +0100570 (mp->m_flags & XFS_MOUNT_ATTR2) &&
571 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
572 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
573 xfs_attr_fork_reset(dp, args->trans);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100574 } else {
575 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
576 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
577 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000578 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
579 (args->op_flags & XFS_DA_OP_ADDNAME) ||
580 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
581 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100582 xfs_trans_log_inode(args->trans, dp,
583 XFS_ILOG_CORE | XFS_ILOG_ADATA);
584 }
585
586 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 return(0);
589}
590
591/*
592 * Look up a name in a shortform attribute list structure.
593 */
594/*ARGSUSED*/
595int
596xfs_attr_shortform_lookup(xfs_da_args_t *args)
597{
598 xfs_attr_shortform_t *sf;
599 xfs_attr_sf_entry_t *sfe;
600 int i;
601 xfs_ifork_t *ifp;
602
Dave Chinner5a5881c2012-03-22 05:15:13 +0000603 trace_xfs_attr_sf_lookup(args);
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ifp = args->dp->i_afp;
606 ASSERT(ifp->if_flags & XFS_IFINLINE);
607 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
608 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100609 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
611 if (sfe->namelen != args->namelen)
612 continue;
613 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
614 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000615 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 continue;
617 return(XFS_ERROR(EEXIST));
618 }
619 return(XFS_ERROR(ENOATTR));
620}
621
622/*
623 * Look up a name in a shortform attribute list structure.
624 */
625/*ARGSUSED*/
626int
627xfs_attr_shortform_getvalue(xfs_da_args_t *args)
628{
629 xfs_attr_shortform_t *sf;
630 xfs_attr_sf_entry_t *sfe;
631 int i;
632
633 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
634 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
635 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100636 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
638 if (sfe->namelen != args->namelen)
639 continue;
640 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
641 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000642 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 continue;
644 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100645 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return(XFS_ERROR(EEXIST));
647 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100648 if (args->valuelen < sfe->valuelen) {
649 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return(XFS_ERROR(ERANGE));
651 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100652 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 memcpy(args->value, &sfe->nameval[args->namelen],
654 args->valuelen);
655 return(XFS_ERROR(EEXIST));
656 }
657 return(XFS_ERROR(ENOATTR));
658}
659
660/*
661 * Convert from using the shortform to the leaf.
662 */
663int
664xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
665{
666 xfs_inode_t *dp;
667 xfs_attr_shortform_t *sf;
668 xfs_attr_sf_entry_t *sfe;
669 xfs_da_args_t nargs;
670 char *tmpbuffer;
671 int error, i, size;
672 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000673 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 xfs_ifork_t *ifp;
675
Dave Chinner5a5881c2012-03-22 05:15:13 +0000676 trace_xfs_attr_sf_to_leaf(args);
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 dp = args->dp;
679 ifp = dp->i_afp;
680 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100681 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 tmpbuffer = kmem_alloc(size, KM_SLEEP);
683 ASSERT(tmpbuffer != NULL);
684 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
685 sf = (xfs_attr_shortform_t *)tmpbuffer;
686
687 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
688 bp = NULL;
689 error = xfs_da_grow_inode(args, &blkno);
690 if (error) {
691 /*
692 * If we hit an IO error middle of the transaction inside
693 * grow_inode(), we may have inconsistent data. Bail out.
694 */
695 if (error == EIO)
696 goto out;
697 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
698 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
699 goto out;
700 }
701
702 ASSERT(blkno == 0);
Dave Chinner517c2222013-04-24 18:58:55 +1000703 error = xfs_attr3_leaf_create(args, blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (error) {
705 error = xfs_da_shrink_inode(args, 0, bp);
706 bp = NULL;
707 if (error)
708 goto out;
709 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
710 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
711 goto out;
712 }
713
714 memset((char *)&nargs, 0, sizeof(nargs));
715 nargs.dp = dp;
716 nargs.firstblock = args->firstblock;
717 nargs.flist = args->flist;
718 nargs.total = args->total;
719 nargs.whichfork = XFS_ATTR_FORK;
720 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000721 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100724 for (i = 0; i < sf->hdr.count; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100725 nargs.name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 nargs.namelen = sfe->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100727 nargs.value = &sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100728 nargs.valuelen = sfe->valuelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100729 nargs.hashval = xfs_da_hashname(sfe->nameval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000731 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Dave Chinner517c2222013-04-24 18:58:55 +1000732 error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 ASSERT(error == ENOATTR);
Dave Chinner517c2222013-04-24 18:58:55 +1000734 error = xfs_attr3_leaf_add(bp, &nargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 ASSERT(error != ENOSPC);
736 if (error)
737 goto out;
738 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
739 }
740 error = 0;
741
742out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000743 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return(error);
745}
746
747STATIC int
748xfs_attr_shortform_compare(const void *a, const void *b)
749{
750 xfs_attr_sf_sort_t *sa, *sb;
751
752 sa = (xfs_attr_sf_sort_t *)a;
753 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100754 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100756 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return(1);
758 } else {
759 return(sa->entno - sb->entno);
760 }
761}
762
Tim Shimmin726801b2006-09-28 11:01:37 +1000763
764#define XFS_ISRESET_CURSOR(cursor) \
765 (!((cursor)->initted) && !((cursor)->hashval) && \
766 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/*
768 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000769 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * If the output buffer is not large enough to hold them all, then we
771 * we have to calculate each entries' hashvalue and sort them before
772 * we can begin returning them to the user.
773 */
774/*ARGSUSED*/
775int
776xfs_attr_shortform_list(xfs_attr_list_context_t *context)
777{
778 attrlist_cursor_kern_t *cursor;
779 xfs_attr_sf_sort_t *sbuf, *sbp;
780 xfs_attr_shortform_t *sf;
781 xfs_attr_sf_entry_t *sfe;
782 xfs_inode_t *dp;
783 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000784 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 ASSERT(context != NULL);
787 dp = context->dp;
788 ASSERT(dp != NULL);
789 ASSERT(dp->i_afp != NULL);
790 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
791 ASSERT(sf != NULL);
792 if (!sf->hdr.count)
793 return(0);
794 cursor = context->cursor;
795 ASSERT(cursor != NULL);
796
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000797 trace_xfs_attr_list_sf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000800 * If the buffer is large enough and the cursor is at the start,
801 * do not bother with sorting since we will return everything in
802 * one buffer and another call using the cursor won't need to be
803 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000805 * If bufsize is zero then put_listent must be a search function
806 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000808 if (context->bufsize == 0 ||
809 (XFS_ISRESET_CURSOR(cursor) &&
810 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100811 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000812 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000813 sfe->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100814 sfe->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +1000815 (int)sfe->namelen,
816 (int)sfe->valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100817 &sfe->nameval[sfe->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000818
819 /*
820 * Either search callback finished early or
821 * didn't fit it all in the buffer after all.
822 */
823 if (context->seen_enough)
824 break;
825
826 if (error)
827 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
829 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000830 trace_xfs_attr_list_sf_all(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return(0);
832 }
833
Tim Shimmin726801b2006-09-28 11:01:37 +1000834 /* do no more for a search callback */
835 if (context->bufsize == 0)
836 return 0;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
839 * It didn't all fit, so we have to sort everything on hashval.
840 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100841 sbsize = sf->hdr.count * sizeof(*sbuf);
Dave Chinner622d8142010-12-23 11:57:37 +1100842 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /*
845 * Scan the attribute list for the rest of the entries, storing
846 * the relevant info from only those that match into a buffer.
847 */
848 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100849 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (unlikely(
851 ((char *)sfe < (char *)sf) ||
852 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
853 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
854 XFS_ERRLEVEL_LOW,
855 context->dp->i_mount, sfe);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000856 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return XFS_ERROR(EFSCORRUPTED);
858 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 sbp->entno = i;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100861 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
862 sbp->name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 sbp->namelen = sfe->namelen;
864 /* These are bytes, and both on-disk, don't endian-flip */
865 sbp->valuelen = sfe->valuelen;
866 sbp->flags = sfe->flags;
867 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
868 sbp++;
869 nsbuf++;
870 }
871
872 /*
873 * Sort the entries on hash then entno.
874 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100875 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 /*
878 * Re-find our place IN THE SORTED LIST.
879 */
880 count = 0;
881 cursor->initted = 1;
882 cursor->blkno = 0;
883 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100884 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (cursor->offset == count) {
886 break;
887 }
888 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100889 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 break;
891 }
892 }
893 if (i == nsbuf) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000894 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return(0);
896 }
897
898 /*
899 * Loop putting entries into the user buffer.
900 */
901 for ( ; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100902 if (cursor->hashval != sbp->hash) {
903 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 cursor->offset = 0;
905 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000906 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000907 sbp->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000908 sbp->name,
909 sbp->namelen,
910 sbp->valuelen,
911 &sbp->name[sbp->namelen]);
912 if (error)
913 return error;
914 if (context->seen_enough)
915 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 cursor->offset++;
917 }
918
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000919 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return(0);
921}
922
923/*
924 * Check a leaf attribute block to see if all the entries would fit into
925 * a shortform attribute list.
926 */
927int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000928xfs_attr_shortform_allfit(
929 struct xfs_buf *bp,
930 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 xfs_attr_leafblock_t *leaf;
933 xfs_attr_leaf_entry_t *entry;
934 xfs_attr_leaf_name_local_t *name_loc;
935 int bytes, i;
936
Dave Chinner1d9025e2012-06-22 18:50:14 +1000937 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200938 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 entry = &leaf->entries[0];
941 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100942 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (entry->flags & XFS_ATTR_INCOMPLETE)
944 continue; /* don't copy partial entries */
945 if (!(entry->flags & XFS_ATTR_LOCAL))
946 return(0);
Dave Chinner517c2222013-04-24 18:58:55 +1000947 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
949 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100950 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return(0);
952 bytes += sizeof(struct xfs_attr_sf_entry)-1
953 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100954 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100956 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100957 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100958 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100959 return(-1);
960 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963/*
964 * Convert a leaf attribute list to shortform attribute list
965 */
966int
Dave Chinner517c2222013-04-24 18:58:55 +1000967xfs_attr3_leaf_to_shortform(
968 struct xfs_buf *bp,
969 struct xfs_da_args *args,
970 int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Dave Chinner517c2222013-04-24 18:58:55 +1000972 struct xfs_attr_leafblock *leaf;
973 struct xfs_attr3_icleaf_hdr ichdr;
974 struct xfs_attr_leaf_entry *entry;
975 struct xfs_attr_leaf_name_local *name_loc;
976 struct xfs_da_args nargs;
977 struct xfs_inode *dp = args->dp;
978 char *tmpbuffer;
979 int error;
980 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Dave Chinner5a5881c2012-03-22 05:15:13 +0000982 trace_xfs_attr_leaf_to_sf(args);
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
Dave Chinner517c2222013-04-24 18:58:55 +1000985 if (!tmpbuffer)
986 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Dave Chinner1d9025e2012-06-22 18:50:14 +1000988 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
Dave Chinner517c2222013-04-24 18:58:55 +1000989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Dave Chinner517c2222013-04-24 18:58:55 +1000991 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
992 entry = xfs_attr3_leaf_entryp(leaf);
993
994 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000995 memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /*
998 * Clean out the prior contents of the attribute list.
999 */
1000 error = xfs_da_shrink_inode(args, 0, bp);
1001 if (error)
1002 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001003
1004 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +11001005 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +11001006 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Christoph Hellwige1486de2009-02-04 09:36:00 +01001007 xfs_attr_fork_reset(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001009 }
1010
1011 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 /*
1014 * Copy the attributes
1015 */
1016 memset((char *)&nargs, 0, sizeof(nargs));
1017 nargs.dp = dp;
1018 nargs.firstblock = args->firstblock;
1019 nargs.flist = args->flist;
1020 nargs.total = args->total;
1021 nargs.whichfork = XFS_ATTR_FORK;
1022 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +10001023 nargs.op_flags = XFS_DA_OP_OKNOENT;
Dave Chinner517c2222013-04-24 18:58:55 +10001024
1025 for (i = 0; i < ichdr.count; entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (entry->flags & XFS_ATTR_INCOMPLETE)
1027 continue; /* don't copy partial entries */
1028 if (!entry->nameidx)
1029 continue;
1030 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Dave Chinner517c2222013-04-24 18:58:55 +10001031 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +11001032 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +11001034 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +11001035 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001036 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +10001037 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +11001038 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040 error = 0;
1041
1042out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001043 kmem_free(tmpbuffer);
Dave Chinner517c2222013-04-24 18:58:55 +10001044 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047/*
1048 * Convert from using a single leaf to a root node and a leaf.
1049 */
1050int
Dave Chinner517c2222013-04-24 18:58:55 +10001051xfs_attr3_leaf_to_node(
1052 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Dave Chinner517c2222013-04-24 18:58:55 +10001054 struct xfs_attr_leafblock *leaf;
1055 struct xfs_attr3_icleaf_hdr icleafhdr;
1056 struct xfs_attr_leaf_entry *entries;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001057 struct xfs_da_node_entry *btree;
Dave Chinner517c2222013-04-24 18:58:55 +10001058 struct xfs_da3_icnode_hdr icnodehdr;
1059 struct xfs_da_intnode *node;
1060 struct xfs_inode *dp = args->dp;
1061 struct xfs_mount *mp = dp->i_mount;
1062 struct xfs_buf *bp1 = NULL;
1063 struct xfs_buf *bp2 = NULL;
1064 xfs_dablk_t blkno;
1065 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Dave Chinner5a5881c2012-03-22 05:15:13 +00001067 trace_xfs_attr_leaf_to_node(args);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 error = xfs_da_grow_inode(args, &blkno);
1070 if (error)
1071 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001072 error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (error)
1074 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +11001075
Dave Chinner517c2222013-04-24 18:58:55 +10001076 error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (error)
1078 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001079
1080 /* copy leaf to new buffer, update identifiers */
Dave Chinner1813dd62012-11-14 17:54:40 +11001081 bp2->b_ops = bp1->b_ops;
Dave Chinner517c2222013-04-24 18:58:55 +10001082 memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(mp));
1083 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1084 struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1085 hdr3->blkno = cpu_to_be64(bp2->b_bn);
1086 }
1087 xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /*
1090 * Set up the new root node.
1091 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001092 error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (error)
1094 goto out;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001095 node = bp1->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001096 xfs_da3_node_hdr_from_disk(&icnodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001097 btree = xfs_da3_node_tree_p(node);
Dave Chinner517c2222013-04-24 18:58:55 +10001098
1099 leaf = bp2->b_addr;
1100 xfs_attr3_leaf_hdr_from_disk(&icleafhdr, leaf);
1101 entries = xfs_attr3_leaf_entryp(leaf);
1102
1103 /* both on-disk, don't endian-flip twice */
1104 btree[0].hashval = entries[icleafhdr.count - 1].hashval;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001105 btree[0].before = cpu_to_be32(blkno);
Dave Chinner517c2222013-04-24 18:58:55 +10001106 icnodehdr.count = 1;
1107 xfs_da3_node_hdr_to_disk(node, &icnodehdr);
1108 xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 error = 0;
1110out:
Dave Chinner517c2222013-04-24 18:58:55 +10001111 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114
1115/*========================================================================
1116 * Routines used for growing the Btree.
1117 *========================================================================*/
1118
1119/*
1120 * Create the initial contents of a leaf attribute list
1121 * or a leaf in a node attribute list.
1122 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001123STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001124xfs_attr3_leaf_create(
1125 struct xfs_da_args *args,
1126 xfs_dablk_t blkno,
1127 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Dave Chinner517c2222013-04-24 18:58:55 +10001129 struct xfs_attr_leafblock *leaf;
1130 struct xfs_attr3_icleaf_hdr ichdr;
1131 struct xfs_inode *dp = args->dp;
1132 struct xfs_mount *mp = dp->i_mount;
1133 struct xfs_buf *bp;
1134 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Dave Chinner5a5881c2012-03-22 05:15:13 +00001136 trace_xfs_attr_leaf_create(args);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
1139 XFS_ATTR_FORK);
1140 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10001141 return error;
1142 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001143 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001144 memset(leaf, 0, XFS_LBSIZE(mp));
1145
1146 memset(&ichdr, 0, sizeof(ichdr));
1147 ichdr.firstused = XFS_LBSIZE(mp);
1148
1149 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1150 struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1151
1152 ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1153
1154 hdr3->blkno = cpu_to_be64(bp->b_bn);
1155 hdr3->owner = cpu_to_be64(dp->i_ino);
1156 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
1157
1158 ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1159 } else {
1160 ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1161 ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Dave Chinner517c2222013-04-24 18:58:55 +10001163 ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Dave Chinner517c2222013-04-24 18:58:55 +10001165 xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
1166 xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 *bpp = bp;
Dave Chinner517c2222013-04-24 18:58:55 +10001169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172/*
1173 * Split the leaf node, rebalance, then add the new entry.
1174 */
1175int
Dave Chinner517c2222013-04-24 18:58:55 +10001176xfs_attr3_leaf_split(
1177 struct xfs_da_state *state,
1178 struct xfs_da_state_blk *oldblk,
1179 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 xfs_dablk_t blkno;
1182 int error;
1183
Dave Chinner5a5881c2012-03-22 05:15:13 +00001184 trace_xfs_attr_leaf_split(state->args);
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /*
1187 * Allocate space for a new leaf node.
1188 */
1189 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1190 error = xfs_da_grow_inode(state->args, &blkno);
1191 if (error)
1192 return(error);
Dave Chinner517c2222013-04-24 18:58:55 +10001193 error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (error)
1195 return(error);
1196 newblk->blkno = blkno;
1197 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1198
1199 /*
1200 * Rebalance the entries across the two leaves.
1201 * NOTE: rebalance() currently depends on the 2nd block being empty.
1202 */
Dave Chinner517c2222013-04-24 18:58:55 +10001203 xfs_attr3_leaf_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001204 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (error)
1206 return(error);
1207
1208 /*
1209 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1210 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1211 * "new" attrs info. Will need the "old" info to remove it later.
1212 *
1213 * Insert the "new" entry in the correct block.
1214 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001215 if (state->inleaf) {
1216 trace_xfs_attr_leaf_add_old(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001217 error = xfs_attr3_leaf_add(oldblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001218 } else {
1219 trace_xfs_attr_leaf_add_new(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001220 error = xfs_attr3_leaf_add(newblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 /*
1224 * Update last hashval in each block since we added the name.
1225 */
1226 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1227 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1228 return(error);
1229}
1230
1231/*
1232 * Add a name to the leaf attribute list structure.
1233 */
1234int
Dave Chinner517c2222013-04-24 18:58:55 +10001235xfs_attr3_leaf_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001236 struct xfs_buf *bp,
1237 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Dave Chinner517c2222013-04-24 18:58:55 +10001239 struct xfs_attr_leafblock *leaf;
1240 struct xfs_attr3_icleaf_hdr ichdr;
1241 int tablesize;
1242 int entsize;
1243 int sum;
1244 int tmp;
1245 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Dave Chinner5a5881c2012-03-22 05:15:13 +00001247 trace_xfs_attr_leaf_add(args);
1248
Dave Chinner1d9025e2012-06-22 18:50:14 +10001249 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001250 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
1251 ASSERT(args->index >= 0 && args->index <= ichdr.count);
Nathan Scottaa82daa2005-11-02 10:33:33 +11001252 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1254
1255 /*
1256 * Search through freemap for first-fit on new name length.
1257 * (may need to figure in size of entry struct too)
1258 */
Dave Chinner517c2222013-04-24 18:58:55 +10001259 tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1260 + xfs_attr3_leaf_hdr_size(leaf);
1261 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1262 if (tablesize > ichdr.firstused) {
1263 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 continue;
1265 }
Dave Chinner517c2222013-04-24 18:58:55 +10001266 if (!ichdr.freemap[i].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 continue; /* no space in this map */
1268 tmp = entsize;
Dave Chinner517c2222013-04-24 18:58:55 +10001269 if (ichdr.freemap[i].base < ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 tmp += sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001271 if (ichdr.freemap[i].size >= tmp) {
1272 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1273 goto out_log_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Dave Chinner517c2222013-04-24 18:58:55 +10001275 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277
1278 /*
1279 * If there are no holes in the address space of the block,
1280 * and we don't have enough freespace, then compaction will do us
1281 * no good and we should just give up.
1282 */
Dave Chinner517c2222013-04-24 18:58:55 +10001283 if (!ichdr.holes && sum < entsize)
1284 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /*
1287 * Compact the entries to coalesce free space.
1288 * This may change the hdr->count via dropping INCOMPLETE entries.
1289 */
Dave Chinner517c2222013-04-24 18:58:55 +10001290 xfs_attr3_leaf_compact(args, &ichdr, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /*
1293 * After compaction, the block is guaranteed to have only one
1294 * free region, in freemap[0]. If it is not big enough, give up.
1295 */
Dave Chinner517c2222013-04-24 18:58:55 +10001296 if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
1297 tmp = ENOSPC;
1298 goto out_log_hdr;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Dave Chinner517c2222013-04-24 18:58:55 +10001301 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1302
1303out_log_hdr:
1304 xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
1305 xfs_trans_log_buf(args->trans, bp,
1306 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1307 xfs_attr3_leaf_hdr_size(leaf)));
1308 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311/*
1312 * Add a name to a leaf attribute list structure.
1313 */
1314STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001315xfs_attr3_leaf_add_work(
1316 struct xfs_buf *bp,
1317 struct xfs_attr3_icleaf_hdr *ichdr,
1318 struct xfs_da_args *args,
1319 int mapindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Dave Chinner517c2222013-04-24 18:58:55 +10001321 struct xfs_attr_leafblock *leaf;
1322 struct xfs_attr_leaf_entry *entry;
1323 struct xfs_attr_leaf_name_local *name_loc;
1324 struct xfs_attr_leaf_name_remote *name_rmt;
1325 struct xfs_mount *mp;
1326 int tmp;
1327 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Dave Chinneree732592012-11-12 22:53:53 +11001329 trace_xfs_attr_leaf_add_work(args);
1330
Dave Chinner1d9025e2012-06-22 18:50:14 +10001331 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001332 ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1333 ASSERT(args->index >= 0 && args->index <= ichdr->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /*
1336 * Force open some space in the entry array and fill it in.
1337 */
Dave Chinner517c2222013-04-24 18:58:55 +10001338 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1339 if (args->index < ichdr->count) {
1340 tmp = ichdr->count - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001342 memmove(entry + 1, entry, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001343 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1345 }
Dave Chinner517c2222013-04-24 18:58:55 +10001346 ichdr->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 /*
1349 * Allocate space for the new string (at the end of the run).
1350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 mp = args->trans->t_mountp;
Dave Chinner517c2222013-04-24 18:58:55 +10001352 ASSERT(ichdr->freemap[mapindex].base < XFS_LBSIZE(mp));
1353 ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1354 ASSERT(ichdr->freemap[mapindex].size >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001355 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1356 mp->m_sb.sb_blocksize, NULL));
Dave Chinner517c2222013-04-24 18:58:55 +10001357 ASSERT(ichdr->freemap[mapindex].size < XFS_LBSIZE(mp));
1358 ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1359
1360 ichdr->freemap[mapindex].size -=
1361 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1362 mp->m_sb.sb_blocksize, &tmp);
1363
1364 entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1365 ichdr->freemap[mapindex].size);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001366 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001368 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001369 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 entry->flags |= XFS_ATTR_INCOMPLETE;
1371 if ((args->blkno2 == args->blkno) &&
1372 (args->index2 <= args->index)) {
1373 args->index2++;
1374 }
1375 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001376 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001378 ASSERT((args->index == 0) ||
1379 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Dave Chinner517c2222013-04-24 18:58:55 +10001380 ASSERT((args->index == ichdr->count - 1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001381 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * For "remote" attribute values, simply note that we need to
1385 * allocate space for the "remote" value. We can't actually
1386 * allocate the extents in this transaction, and we can't decide
1387 * which blocks they should be as we might allocate more blocks
1388 * as part of this transaction (a split operation for example).
1389 */
1390 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10001391 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001393 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1395 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001396 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10001398 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 name_rmt->namelen = args->namelen;
1400 memcpy((char *)name_rmt->name, args->name, args->namelen);
1401 entry->flags |= XFS_ATTR_INCOMPLETE;
1402 /* just in case */
1403 name_rmt->valuelen = 0;
1404 name_rmt->valueblk = 0;
1405 args->rmtblkno = 1;
1406 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1407 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001408 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10001409 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 xfs_attr_leaf_entsize(leaf, args->index)));
1411
1412 /*
1413 * Update the control info for this leaf node
1414 */
Dave Chinner517c2222013-04-24 18:58:55 +10001415 if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1416 ichdr->firstused = be16_to_cpu(entry->nameidx);
1417
1418 ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1419 + xfs_attr3_leaf_hdr_size(leaf));
1420 tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1421 + xfs_attr3_leaf_hdr_size(leaf);
1422
1423 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1424 if (ichdr->freemap[i].base == tmp) {
1425 ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1426 ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 }
Dave Chinner517c2222013-04-24 18:58:55 +10001429 ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433/*
1434 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1435 */
1436STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001437xfs_attr3_leaf_compact(
Dave Chinneree732592012-11-12 22:53:53 +11001438 struct xfs_da_args *args,
Dave Chinner517c2222013-04-24 18:58:55 +10001439 struct xfs_attr3_icleaf_hdr *ichdr_d,
Dave Chinneree732592012-11-12 22:53:53 +11001440 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Dave Chinneree732592012-11-12 22:53:53 +11001442 xfs_attr_leafblock_t *leaf_s, *leaf_d;
Dave Chinner517c2222013-04-24 18:58:55 +10001443 struct xfs_attr3_icleaf_hdr ichdr_s;
Dave Chinneree732592012-11-12 22:53:53 +11001444 struct xfs_trans *trans = args->trans;
1445 struct xfs_mount *mp = trans->t_mountp;
1446 char *tmpbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Dave Chinneree732592012-11-12 22:53:53 +11001448 trace_xfs_attr_leaf_compact(args);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1451 ASSERT(tmpbuffer != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001452 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1453 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /*
1456 * Copy basic information
1457 */
1458 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001459 leaf_d = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001460 ichdr_s = *ichdr_d; /* struct copy */
1461 ichdr_d->firstused = XFS_LBSIZE(mp);
1462 ichdr_d->usedbytes = 0;
1463 ichdr_d->count = 0;
1464 ichdr_d->holes = 0;
1465 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_s);
1466 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 /*
1469 * Copy all entry's in the same (sorted) order,
1470 * but allocate name/value pairs packed and in sequence.
1471 */
Dave Chinner517c2222013-04-24 18:58:55 +10001472 xfs_attr3_leaf_moveents(leaf_s, &ichdr_s, 0, leaf_d, ichdr_d, 0,
1473 ichdr_s.count, mp);
1474 /*
1475 * this logs the entire buffer, but the caller must write the header
1476 * back to the buffer when it is finished modifying it.
1477 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001478 xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001480 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483/*
Dave Chinner517c2222013-04-24 18:58:55 +10001484 * Compare two leaf blocks "order".
1485 * Return 0 unless leaf2 should go before leaf1.
1486 */
1487static int
1488xfs_attr3_leaf_order(
1489 struct xfs_buf *leaf1_bp,
1490 struct xfs_attr3_icleaf_hdr *leaf1hdr,
1491 struct xfs_buf *leaf2_bp,
1492 struct xfs_attr3_icleaf_hdr *leaf2hdr)
1493{
1494 struct xfs_attr_leaf_entry *entries1;
1495 struct xfs_attr_leaf_entry *entries2;
1496
1497 entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1498 entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1499 if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1500 ((be32_to_cpu(entries2[0].hashval) <
1501 be32_to_cpu(entries1[0].hashval)) ||
1502 (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1503 be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1504 return 1;
1505 }
1506 return 0;
1507}
1508
1509int
1510xfs_attr_leaf_order(
1511 struct xfs_buf *leaf1_bp,
1512 struct xfs_buf *leaf2_bp)
1513{
1514 struct xfs_attr3_icleaf_hdr ichdr1;
1515 struct xfs_attr3_icleaf_hdr ichdr2;
1516
1517 xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1_bp->b_addr);
1518 xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2_bp->b_addr);
1519 return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1520}
1521
1522/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * Redistribute the attribute list entries between two leaf nodes,
1524 * taking into account the size of the new entry.
1525 *
1526 * NOTE: if new block is empty, then it will get the upper half of the
1527 * old block. At present, all (one) callers pass in an empty second block.
1528 *
1529 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1530 * to match what it is doing in splitting the attribute leaf block. Those
1531 * values are used in "atomic rename" operations on attributes. Note that
1532 * the "new" and "old" values can end up in different blocks.
1533 */
1534STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001535xfs_attr3_leaf_rebalance(
1536 struct xfs_da_state *state,
1537 struct xfs_da_state_blk *blk1,
1538 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Dave Chinner517c2222013-04-24 18:58:55 +10001540 struct xfs_da_args *args;
1541 struct xfs_attr_leafblock *leaf1;
1542 struct xfs_attr_leafblock *leaf2;
1543 struct xfs_attr3_icleaf_hdr ichdr1;
1544 struct xfs_attr3_icleaf_hdr ichdr2;
1545 struct xfs_attr_leaf_entry *entries1;
1546 struct xfs_attr_leaf_entry *entries2;
1547 int count;
1548 int totallen;
1549 int max;
1550 int space;
1551 int swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 /*
1554 * Set up environment.
1555 */
1556 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1557 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001558 leaf1 = blk1->bp->b_addr;
1559 leaf2 = blk2->bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001560 xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1);
1561 xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2);
1562 ASSERT(ichdr2.count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 args = state->args;
1564
Dave Chinner5a5881c2012-03-22 05:15:13 +00001565 trace_xfs_attr_leaf_rebalance(args);
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /*
1568 * Check ordering of blocks, reverse if it makes things simpler.
1569 *
1570 * NOTE: Given that all (current) callers pass in an empty
1571 * second block, this code should never set "swap".
1572 */
1573 swap = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001574 if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1575 struct xfs_da_state_blk *tmp_blk;
1576 struct xfs_attr3_icleaf_hdr tmp_ichdr;
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 tmp_blk = blk1;
1579 blk1 = blk2;
1580 blk2 = tmp_blk;
Dave Chinner517c2222013-04-24 18:58:55 +10001581
1582 /* struct copies to swap them rather than reconverting */
1583 tmp_ichdr = ichdr1;
1584 ichdr1 = ichdr2;
1585 ichdr2 = tmp_ichdr;
1586
Dave Chinner1d9025e2012-06-22 18:50:14 +10001587 leaf1 = blk1->bp->b_addr;
1588 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 swap = 1;
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /*
1593 * Examine entries until we reduce the absolute difference in
1594 * byte usage between the two blocks to a minimum. Then get
1595 * the direction to copy and the number of elements to move.
1596 *
1597 * "inleaf" is true if the new entry should be inserted into blk1.
1598 * If "swap" is also true, then reverse the sense of "inleaf".
1599 */
Dave Chinner517c2222013-04-24 18:58:55 +10001600 state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1601 blk2, &ichdr2,
1602 &count, &totallen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (swap)
1604 state->inleaf = !state->inleaf;
1605
1606 /*
1607 * Move any entries required from leaf to leaf:
1608 */
Dave Chinner517c2222013-04-24 18:58:55 +10001609 if (count < ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /*
1611 * Figure the total bytes to be added to the destination leaf.
1612 */
1613 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001614 count = ichdr1.count - count;
1615 space = ichdr1.usedbytes - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 space += count * sizeof(xfs_attr_leaf_entry_t);
1617
1618 /*
1619 * leaf2 is the destination, compact it if it looks tight.
1620 */
Dave Chinner517c2222013-04-24 18:58:55 +10001621 max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1622 max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001623 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001624 xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /*
1627 * Move high entries from leaf1 to low end of leaf2.
1628 */
Dave Chinner517c2222013-04-24 18:58:55 +10001629 xfs_attr3_leaf_moveents(leaf1, &ichdr1, ichdr1.count - count,
1630 leaf2, &ichdr2, 0, count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Dave Chinner517c2222013-04-24 18:58:55 +10001632 } else if (count > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /*
1634 * I assert that since all callers pass in an empty
1635 * second buffer, this code should never execute.
1636 */
Dave Chinner07428d72012-11-12 22:09:44 +11001637 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 /*
1640 * Figure the total bytes to be added to the destination leaf.
1641 */
1642 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001643 count -= ichdr1.count;
1644 space = totallen - ichdr1.usedbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 space += count * sizeof(xfs_attr_leaf_entry_t);
1646
1647 /*
1648 * leaf1 is the destination, compact it if it looks tight.
1649 */
Dave Chinner517c2222013-04-24 18:58:55 +10001650 max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1651 max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001652 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001653 xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 /*
1656 * Move low entries from leaf2 to high end of leaf1.
1657 */
Dave Chinner517c2222013-04-24 18:58:55 +10001658 xfs_attr3_leaf_moveents(leaf2, &ichdr2, 0, leaf1, &ichdr1,
1659 ichdr1.count, count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661
Dave Chinner517c2222013-04-24 18:58:55 +10001662 xfs_attr3_leaf_hdr_to_disk(leaf1, &ichdr1);
1663 xfs_attr3_leaf_hdr_to_disk(leaf2, &ichdr2);
1664 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1665 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /*
1668 * Copy out last hashval in each block for B-tree code.
1669 */
Dave Chinner517c2222013-04-24 18:58:55 +10001670 entries1 = xfs_attr3_leaf_entryp(leaf1);
1671 entries2 = xfs_attr3_leaf_entryp(leaf2);
1672 blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1673 blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /*
1676 * Adjust the expected index for insertion.
1677 * NOTE: this code depends on the (current) situation that the
1678 * second block was originally empty.
1679 *
1680 * If the insertion point moved to the 2nd block, we must adjust
1681 * the index. We must also track the entry just following the
1682 * new entry for use in an "atomic rename" operation, that entry
1683 * is always the "old" entry and the "new" entry is what we are
1684 * inserting. The index/blkno fields refer to the "old" entry,
1685 * while the index2/blkno2 fields refer to the "new" entry.
1686 */
Dave Chinner517c2222013-04-24 18:58:55 +10001687 if (blk1->index > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 ASSERT(state->inleaf == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10001689 blk2->index = blk1->index - ichdr1.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 args->index = args->index2 = blk2->index;
1691 args->blkno = args->blkno2 = blk2->blkno;
Dave Chinner517c2222013-04-24 18:58:55 +10001692 } else if (blk1->index == ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (state->inleaf) {
1694 args->index = blk1->index;
1695 args->blkno = blk1->blkno;
1696 args->index2 = 0;
1697 args->blkno2 = blk2->blkno;
1698 } else {
Dave Chinner07428d72012-11-12 22:09:44 +11001699 /*
1700 * On a double leaf split, the original attr location
1701 * is already stored in blkno2/index2, so don't
1702 * overwrite it overwise we corrupt the tree.
1703 */
Dave Chinner517c2222013-04-24 18:58:55 +10001704 blk2->index = blk1->index - ichdr1.count;
Dave Chinner07428d72012-11-12 22:09:44 +11001705 args->index = blk2->index;
1706 args->blkno = blk2->blkno;
1707 if (!state->extravalid) {
1708 /*
1709 * set the new attr location to match the old
1710 * one and let the higher level split code
1711 * decide where in the leaf to place it.
1712 */
1713 args->index2 = blk2->index;
1714 args->blkno2 = blk2->blkno;
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 } else {
1718 ASSERT(state->inleaf == 1);
1719 args->index = args->index2 = blk1->index;
1720 args->blkno = args->blkno2 = blk1->blkno;
1721 }
1722}
1723
1724/*
1725 * Examine entries until we reduce the absolute difference in
1726 * byte usage between the two blocks to a minimum.
1727 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1728 * GROT: there will always be enough room in either block for a new entry.
1729 * GROT: Do a double-split for this case?
1730 */
1731STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001732xfs_attr3_leaf_figure_balance(
1733 struct xfs_da_state *state,
1734 struct xfs_da_state_blk *blk1,
1735 struct xfs_attr3_icleaf_hdr *ichdr1,
1736 struct xfs_da_state_blk *blk2,
1737 struct xfs_attr3_icleaf_hdr *ichdr2,
1738 int *countarg,
1739 int *usedbytesarg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Dave Chinner517c2222013-04-24 18:58:55 +10001741 struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
1742 struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
1743 struct xfs_attr_leaf_entry *entry;
1744 int count;
1745 int max;
1746 int index;
1747 int totallen = 0;
1748 int half;
1749 int lastdelta;
1750 int foundit = 0;
1751 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 /*
1754 * Examine entries until we reduce the absolute difference in
1755 * byte usage between the two blocks to a minimum.
1756 */
Dave Chinner517c2222013-04-24 18:58:55 +10001757 max = ichdr1->count + ichdr2->count;
1758 half = (max + 1) * sizeof(*entry);
1759 half += ichdr1->usedbytes + ichdr2->usedbytes +
1760 xfs_attr_leaf_newentsize(state->args->namelen,
1761 state->args->valuelen,
1762 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 half /= 2;
1764 lastdelta = state->blocksize;
Dave Chinner517c2222013-04-24 18:58:55 +10001765 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 for (count = index = 0; count < max; entry++, index++, count++) {
1767
1768#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1769 /*
1770 * The new entry is in the first block, account for it.
1771 */
1772 if (count == blk1->index) {
1773 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001774 xfs_attr_leaf_newentsize(
1775 state->args->namelen,
1776 state->args->valuelen,
1777 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1779 break;
1780 lastdelta = XFS_ATTR_ABS(half - tmp);
1781 totallen = tmp;
1782 foundit = 1;
1783 }
1784
1785 /*
1786 * Wrap around into the second block if necessary.
1787 */
Dave Chinner517c2222013-04-24 18:58:55 +10001788 if (count == ichdr1->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 leaf1 = leaf2;
Dave Chinner517c2222013-04-24 18:58:55 +10001790 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 index = 0;
1792 }
1793
1794 /*
1795 * Figure out if next leaf entry would be too much.
1796 */
1797 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1798 index);
1799 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1800 break;
1801 lastdelta = XFS_ATTR_ABS(half - tmp);
1802 totallen = tmp;
1803#undef XFS_ATTR_ABS
1804 }
1805
1806 /*
1807 * Calculate the number of usedbytes that will end up in lower block.
1808 * If new entry not in lower block, fix up the count.
1809 */
1810 totallen -= count * sizeof(*entry);
1811 if (foundit) {
1812 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001813 xfs_attr_leaf_newentsize(
1814 state->args->namelen,
1815 state->args->valuelen,
1816 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818
1819 *countarg = count;
1820 *usedbytesarg = totallen;
Dave Chinner517c2222013-04-24 18:58:55 +10001821 return foundit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
1824/*========================================================================
1825 * Routines used for shrinking the Btree.
1826 *========================================================================*/
1827
1828/*
1829 * Check a leaf block and its neighbors to see if the block should be
1830 * collapsed into one or the other neighbor. Always keep the block
1831 * with the smaller block number.
1832 * If the current block is over 50% full, don't try to join it, return 0.
1833 * If the block is empty, fill in the state structure and return 2.
1834 * If it can be collapsed, fill in the state structure and return 1.
1835 * If nothing can be done, return 0.
1836 *
1837 * GROT: allow for INCOMPLETE entries in calculation.
1838 */
1839int
Dave Chinner517c2222013-04-24 18:58:55 +10001840xfs_attr3_leaf_toosmall(
1841 struct xfs_da_state *state,
1842 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Dave Chinner517c2222013-04-24 18:58:55 +10001844 struct xfs_attr_leafblock *leaf;
1845 struct xfs_da_state_blk *blk;
1846 struct xfs_attr3_icleaf_hdr ichdr;
1847 struct xfs_buf *bp;
1848 xfs_dablk_t blkno;
1849 int bytes;
1850 int forward;
1851 int error;
1852 int retval;
1853 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Dave Chinneree732592012-11-12 22:53:53 +11001855 trace_xfs_attr_leaf_toosmall(state->args);
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
1858 * Check for the degenerate case of the block being over 50% full.
1859 * If so, it's not worth even looking to see if we might be able
1860 * to coalesce with a sibling.
1861 */
1862 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner517c2222013-04-24 18:58:55 +10001863 leaf = blk->bp->b_addr;
1864 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
1865 bytes = xfs_attr3_leaf_hdr_size(leaf) +
1866 ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
1867 ichdr.usedbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (bytes > (state->blocksize >> 1)) {
1869 *action = 0; /* blk over 50%, don't try to join */
1870 return(0);
1871 }
1872
1873 /*
1874 * Check for the degenerate case of the block being empty.
1875 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001876 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * to merge with the forward block unless it is NULL.
1878 */
Dave Chinner517c2222013-04-24 18:58:55 +10001879 if (ichdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /*
1881 * Make altpath point to the block we want to keep and
1882 * path point to the block we want to drop (this one).
1883 */
Dave Chinner517c2222013-04-24 18:58:55 +10001884 forward = (ichdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001886 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 0, &retval);
1888 if (error)
1889 return(error);
1890 if (retval) {
1891 *action = 0;
1892 } else {
1893 *action = 2;
1894 }
Dave Chinner517c2222013-04-24 18:58:55 +10001895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897
1898 /*
1899 * Examine each sibling block to see if we can coalesce with
1900 * at least 25% free space to spare. We need to figure out
1901 * whether to merge with the forward or the backward block.
1902 * We prefer coalescing with the lower numbered sibling so as
1903 * to shrink an attribute list over time.
1904 */
1905 /* start with smaller blk num */
Dave Chinner517c2222013-04-24 18:58:55 +10001906 forward = ichdr.forw < ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 for (i = 0; i < 2; forward = !forward, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10001908 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (forward)
Dave Chinner517c2222013-04-24 18:58:55 +10001910 blkno = ichdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 else
Dave Chinner517c2222013-04-24 18:58:55 +10001912 blkno = ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (blkno == 0)
1914 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10001915 error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
Dave Chinnerad14c332012-11-12 22:54:16 +11001916 blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (error)
1918 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Dave Chinner517c2222013-04-24 18:58:55 +10001920 xfs_attr3_leaf_hdr_from_disk(&ichdr2, bp->b_addr);
1921
1922 bytes = state->blocksize - (state->blocksize >> 2) -
1923 ichdr.usedbytes - ichdr2.usedbytes -
1924 ((ichdr.count + ichdr2.count) *
1925 sizeof(xfs_attr_leaf_entry_t)) -
1926 xfs_attr3_leaf_hdr_size(leaf);
1927
Dave Chinner1d9025e2012-06-22 18:50:14 +10001928 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (bytes >= 0)
1930 break; /* fits with at least 25% to spare */
1931 }
1932 if (i >= 2) {
1933 *action = 0;
1934 return(0);
1935 }
1936
1937 /*
1938 * Make altpath point to the block we want to keep (the lower
1939 * numbered block) and path point to the block we want to drop.
1940 */
1941 memcpy(&state->altpath, &state->path, sizeof(state->path));
1942 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001943 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 0, &retval);
1945 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001946 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 0, &retval);
1948 }
1949 if (error)
1950 return(error);
1951 if (retval) {
1952 *action = 0;
1953 } else {
1954 *action = 1;
1955 }
1956 return(0);
1957}
1958
1959/*
1960 * Remove a name from the leaf attribute list structure.
1961 *
1962 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1963 * If two leaves are 37% full, when combined they will leave 25% free.
1964 */
1965int
Dave Chinner517c2222013-04-24 18:58:55 +10001966xfs_attr3_leaf_remove(
1967 struct xfs_buf *bp,
1968 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Dave Chinner517c2222013-04-24 18:58:55 +10001970 struct xfs_attr_leafblock *leaf;
1971 struct xfs_attr3_icleaf_hdr ichdr;
1972 struct xfs_attr_leaf_entry *entry;
1973 struct xfs_mount *mp = args->trans->t_mountp;
1974 int before;
1975 int after;
1976 int smallest;
1977 int entsize;
1978 int tablesize;
1979 int tmp;
1980 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Dave Chinneree732592012-11-12 22:53:53 +11001982 trace_xfs_attr_leaf_remove(args);
1983
Dave Chinner1d9025e2012-06-22 18:50:14 +10001984 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001985 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
1986
1987 ASSERT(ichdr.count > 0 && ichdr.count < XFS_LBSIZE(mp) / 8);
1988 ASSERT(args->index >= 0 && args->index < ichdr.count);
1989 ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
1990 xfs_attr3_leaf_hdr_size(leaf));
1991
1992 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1993
1994 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001995 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /*
1998 * Scan through free region table:
1999 * check for adjacency of free'd entry with an existing one,
2000 * find smallest free region in case we need to replace it,
2001 * adjust any map that borders the entry table,
2002 */
Dave Chinner517c2222013-04-24 18:58:55 +10002003 tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
2004 + xfs_attr3_leaf_hdr_size(leaf);
2005 tmp = ichdr.freemap[0].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 before = after = -1;
2007 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2008 entsize = xfs_attr_leaf_entsize(leaf, args->index);
Dave Chinner517c2222013-04-24 18:58:55 +10002009 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
2010 ASSERT(ichdr.freemap[i].base < XFS_LBSIZE(mp));
2011 ASSERT(ichdr.freemap[i].size < XFS_LBSIZE(mp));
2012 if (ichdr.freemap[i].base == tablesize) {
2013 ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2014 ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016
Dave Chinner517c2222013-04-24 18:58:55 +10002017 if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2018 be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 before = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002020 } else if (ichdr.freemap[i].base ==
2021 (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 after = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002023 } else if (ichdr.freemap[i].size < tmp) {
2024 tmp = ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 smallest = i;
2026 }
2027 }
2028
2029 /*
2030 * Coalesce adjacent freemap regions,
2031 * or replace the smallest region.
2032 */
2033 if ((before >= 0) || (after >= 0)) {
2034 if ((before >= 0) && (after >= 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10002035 ichdr.freemap[before].size += entsize;
2036 ichdr.freemap[before].size += ichdr.freemap[after].size;
2037 ichdr.freemap[after].base = 0;
2038 ichdr.freemap[after].size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 } else if (before >= 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002040 ichdr.freemap[before].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002042 ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2043 ichdr.freemap[after].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045 } else {
2046 /*
2047 * Replace smallest region (if it is smaller than free'd entry)
2048 */
Dave Chinner517c2222013-04-24 18:58:55 +10002049 if (ichdr.freemap[smallest].size < entsize) {
2050 ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2051 ichdr.freemap[smallest].size = entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053 }
2054
2055 /*
2056 * Did we remove the first entry?
2057 */
Dave Chinner517c2222013-04-24 18:58:55 +10002058 if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 smallest = 1;
2060 else
2061 smallest = 0;
2062
2063 /*
2064 * Compress the remaining entries and zero out the removed stuff.
2065 */
Dave Chinner517c2222013-04-24 18:58:55 +10002066 memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2067 ichdr.usedbytes -= entsize;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002068 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002069 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 entsize));
2071
Dave Chinner517c2222013-04-24 18:58:55 +10002072 tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2073 memmove(entry, entry + 1, tmp);
2074 ichdr.count--;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002075 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002076 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2077
2078 entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2079 memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 /*
2082 * If we removed the first entry, re-find the first used byte
2083 * in the name area. Note that if the entry was the "firstused",
2084 * then we don't have a "hole" in our block resulting from
2085 * removing the name.
2086 */
2087 if (smallest) {
2088 tmp = XFS_LBSIZE(mp);
Dave Chinner517c2222013-04-24 18:58:55 +10002089 entry = xfs_attr3_leaf_entryp(leaf);
2090 for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2091 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002092 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
2093
2094 if (be16_to_cpu(entry->nameidx) < tmp)
2095 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Dave Chinner517c2222013-04-24 18:58:55 +10002097 ichdr.firstused = tmp;
2098 if (!ichdr.firstused)
2099 ichdr.firstused = tmp - XFS_ATTR_LEAF_NAME_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002101 ichdr.holes = 1; /* mark as needing compaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Dave Chinner517c2222013-04-24 18:58:55 +10002103 xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002104 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002105 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2106 xfs_attr3_leaf_hdr_size(leaf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 /*
2109 * Check if leaf is less than 50% full, caller may want to
2110 * "join" the leaf with a sibling if so.
2111 */
Dave Chinner517c2222013-04-24 18:58:55 +10002112 tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2113 ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2114
2115 return tmp < mp->m_attr_magicpct; /* leaf is < 37% full */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118/*
2119 * Move all the attribute list entries from drop_leaf into save_leaf.
2120 */
2121void
Dave Chinner517c2222013-04-24 18:58:55 +10002122xfs_attr3_leaf_unbalance(
2123 struct xfs_da_state *state,
2124 struct xfs_da_state_blk *drop_blk,
2125 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Dave Chinner517c2222013-04-24 18:58:55 +10002127 struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2128 struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2129 struct xfs_attr3_icleaf_hdr drophdr;
2130 struct xfs_attr3_icleaf_hdr savehdr;
2131 struct xfs_attr_leaf_entry *entry;
2132 struct xfs_mount *mp = state->mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Dave Chinner5a5881c2012-03-22 05:15:13 +00002134 trace_xfs_attr_leaf_unbalance(state->args);
2135
Dave Chinner1d9025e2012-06-22 18:50:14 +10002136 drop_leaf = drop_blk->bp->b_addr;
2137 save_leaf = save_blk->bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002138 xfs_attr3_leaf_hdr_from_disk(&drophdr, drop_leaf);
2139 xfs_attr3_leaf_hdr_from_disk(&savehdr, save_leaf);
2140 entry = xfs_attr3_leaf_entryp(drop_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 /*
2143 * Save last hashval from dying block for later Btree fixup.
2144 */
Dave Chinner517c2222013-04-24 18:58:55 +10002145 drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 /*
2148 * Check if we need a temp buffer, or can we do it in place.
2149 * Note that we don't check "leaf" for holes because we will
2150 * always be dropping it, toosmall() decided that for us already.
2151 */
Dave Chinner517c2222013-04-24 18:58:55 +10002152 if (savehdr.holes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /*
2154 * dest leaf has no holes, so we add there. May need
2155 * to make some room in the entry array.
2156 */
Dave Chinner517c2222013-04-24 18:58:55 +10002157 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2158 drop_blk->bp, &drophdr)) {
2159 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2160 save_leaf, &savehdr, 0,
2161 drophdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002163 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2164 save_leaf, &savehdr,
2165 savehdr.count, drophdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167 } else {
2168 /*
2169 * Destination has holes, so we make a temporary copy
2170 * of the leaf and add them both to that.
2171 */
Dave Chinner517c2222013-04-24 18:58:55 +10002172 struct xfs_attr_leafblock *tmp_leaf;
2173 struct xfs_attr3_icleaf_hdr tmphdr;
2174
2175 tmp_leaf = kmem_alloc(state->blocksize, KM_SLEEP);
2176 memset(tmp_leaf, 0, state->blocksize);
2177 memset(&tmphdr, 0, sizeof(tmphdr));
2178
2179 tmphdr.magic = savehdr.magic;
2180 tmphdr.forw = savehdr.forw;
2181 tmphdr.back = savehdr.back;
2182 tmphdr.firstused = state->blocksize;
2183 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2184 drop_blk->bp, &drophdr)) {
2185 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2186 tmp_leaf, &tmphdr, 0,
2187 drophdr.count, mp);
2188 xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0,
2189 tmp_leaf, &tmphdr, tmphdr.count,
2190 savehdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002192 xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0,
2193 tmp_leaf, &tmphdr, 0,
2194 savehdr.count, mp);
2195 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2196 tmp_leaf, &tmphdr, tmphdr.count,
2197 drophdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Dave Chinner517c2222013-04-24 18:58:55 +10002199 memcpy(save_leaf, tmp_leaf, state->blocksize);
2200 savehdr = tmphdr; /* struct copy */
2201 kmem_free(tmp_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203
Dave Chinner517c2222013-04-24 18:58:55 +10002204 xfs_attr3_leaf_hdr_to_disk(save_leaf, &savehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002205 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 state->blocksize - 1);
2207
2208 /*
2209 * Copy out last hashval in each block for B-tree code.
2210 */
Dave Chinner517c2222013-04-24 18:58:55 +10002211 entry = xfs_attr3_leaf_entryp(save_leaf);
2212 save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
2215/*========================================================================
2216 * Routines used for finding things in the Btree.
2217 *========================================================================*/
2218
2219/*
2220 * Look up a name in a leaf attribute list structure.
2221 * This is the internal routine, it uses the caller's buffer.
2222 *
2223 * Note that duplicate keys are allowed, but only check within the
2224 * current leaf node. The Btree code must check in adjacent leaf nodes.
2225 *
2226 * Return in args->index the index into the entry[] array of either
2227 * the found entry, or where the entry should have been (insert before
2228 * that entry).
2229 *
2230 * Don't change the args->value unless we find the attribute.
2231 */
2232int
Dave Chinner517c2222013-04-24 18:58:55 +10002233xfs_attr3_leaf_lookup_int(
2234 struct xfs_buf *bp,
2235 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Dave Chinner517c2222013-04-24 18:58:55 +10002237 struct xfs_attr_leafblock *leaf;
2238 struct xfs_attr3_icleaf_hdr ichdr;
2239 struct xfs_attr_leaf_entry *entry;
2240 struct xfs_attr_leaf_entry *entries;
2241 struct xfs_attr_leaf_name_local *name_loc;
2242 struct xfs_attr_leaf_name_remote *name_rmt;
2243 xfs_dahash_t hashval;
2244 int probe;
2245 int span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Dave Chinner5a5881c2012-03-22 05:15:13 +00002247 trace_xfs_attr_leaf_lookup(args);
2248
Dave Chinner1d9025e2012-06-22 18:50:14 +10002249 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002250 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2251 entries = xfs_attr3_leaf_entryp(leaf);
2252 ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 /*
2255 * Binary search. (note: small blocks will skip this loop)
2256 */
2257 hashval = args->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002258 probe = span = ichdr.count / 2;
2259 for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002261 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002263 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 probe -= span;
2265 else
2266 break;
2267 }
Dave Chinner517c2222013-04-24 18:58:55 +10002268 ASSERT(probe >= 0 && (!ichdr.count || probe < ichdr.count));
2269 ASSERT(span <= 4 || be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 /*
2272 * Since we may have duplicate hashval's, find the first matching
2273 * hashval in the leaf.
2274 */
Dave Chinner517c2222013-04-24 18:58:55 +10002275 while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 entry--;
2277 probe--;
2278 }
Dave Chinner517c2222013-04-24 18:58:55 +10002279 while (probe < ichdr.count &&
2280 be32_to_cpu(entry->hashval) < hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 entry++;
2282 probe++;
2283 }
Dave Chinner517c2222013-04-24 18:58:55 +10002284 if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 args->index = probe;
Dave Chinner517c2222013-04-24 18:58:55 +10002286 return XFS_ERROR(ENOATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288
2289 /*
2290 * Duplicate keys may be present, so search all of them for a match.
2291 */
Dave Chinner517c2222013-04-24 18:58:55 +10002292 for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 entry++, probe++) {
2294/*
2295 * GROT: Add code to remove incomplete entries.
2296 */
2297 /*
2298 * If we are looking for INCOMPLETE entries, show only those.
2299 * If we are looking for complete entries, show only those.
2300 */
2301 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2302 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2303 continue;
2304 }
2305 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002306 name_loc = xfs_attr3_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (name_loc->namelen != args->namelen)
2308 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002309 if (memcmp(args->name, name_loc->nameval,
2310 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002312 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 continue;
2314 args->index = probe;
Dave Chinner517c2222013-04-24 18:58:55 +10002315 return XFS_ERROR(EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002317 name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (name_rmt->namelen != args->namelen)
2319 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002320 if (memcmp(args->name, name_rmt->name,
2321 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002323 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 continue;
2325 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002326 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002328 be32_to_cpu(name_rmt->valuelen));
Dave Chinner517c2222013-04-24 18:58:55 +10002329 return XFS_ERROR(EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331 }
2332 args->index = probe;
Dave Chinner517c2222013-04-24 18:58:55 +10002333 return XFS_ERROR(ENOATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
2336/*
2337 * Get the value associated with an attribute name from a leaf attribute
2338 * list structure.
2339 */
2340int
Dave Chinner517c2222013-04-24 18:58:55 +10002341xfs_attr3_leaf_getvalue(
2342 struct xfs_buf *bp,
2343 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Dave Chinner517c2222013-04-24 18:58:55 +10002345 struct xfs_attr_leafblock *leaf;
2346 struct xfs_attr3_icleaf_hdr ichdr;
2347 struct xfs_attr_leaf_entry *entry;
2348 struct xfs_attr_leaf_name_local *name_loc;
2349 struct xfs_attr_leaf_name_remote *name_rmt;
2350 int valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Dave Chinner1d9025e2012-06-22 18:50:14 +10002352 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002353 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2354 ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8);
2355 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Dave Chinner517c2222013-04-24 18:58:55 +10002357 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002359 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 ASSERT(name_loc->namelen == args->namelen);
2361 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002362 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (args->flags & ATTR_KERNOVAL) {
2364 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367 if (args->valuelen < valuelen) {
2368 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002369 return XFS_ERROR(ERANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 args->valuelen = valuelen;
2372 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2373 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002374 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 ASSERT(name_rmt->namelen == args->namelen);
2376 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002377 valuelen = be32_to_cpu(name_rmt->valuelen);
2378 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2380 if (args->flags & ATTR_KERNOVAL) {
2381 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384 if (args->valuelen < valuelen) {
2385 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002386 return XFS_ERROR(ERANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388 args->valuelen = valuelen;
2389 }
Dave Chinner517c2222013-04-24 18:58:55 +10002390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
2393/*========================================================================
2394 * Utility routines.
2395 *========================================================================*/
2396
2397/*
2398 * Move the indicated entries from one leaf to another.
2399 * NOTE: this routine modifies both source and destination leaves.
2400 */
2401/*ARGSUSED*/
2402STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10002403xfs_attr3_leaf_moveents(
2404 struct xfs_attr_leafblock *leaf_s,
2405 struct xfs_attr3_icleaf_hdr *ichdr_s,
2406 int start_s,
2407 struct xfs_attr_leafblock *leaf_d,
2408 struct xfs_attr3_icleaf_hdr *ichdr_d,
2409 int start_d,
2410 int count,
2411 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
Dave Chinner517c2222013-04-24 18:58:55 +10002413 struct xfs_attr_leaf_entry *entry_s;
2414 struct xfs_attr_leaf_entry *entry_d;
2415 int desti;
2416 int tmp;
2417 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 /*
2420 * Check for nothing to do.
2421 */
2422 if (count == 0)
2423 return;
2424
2425 /*
2426 * Set up environment.
2427 */
Dave Chinner517c2222013-04-24 18:58:55 +10002428 ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2429 ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2430 ASSERT(ichdr_s->magic == ichdr_d->magic);
2431 ASSERT(ichdr_s->count > 0 && ichdr_s->count < XFS_LBSIZE(mp) / 8);
2432 ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2433 + xfs_attr3_leaf_hdr_size(leaf_s));
2434 ASSERT(ichdr_d->count < XFS_LBSIZE(mp) / 8);
2435 ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2436 + xfs_attr3_leaf_hdr_size(leaf_d));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Dave Chinner517c2222013-04-24 18:58:55 +10002438 ASSERT(start_s < ichdr_s->count);
2439 ASSERT(start_d <= ichdr_d->count);
2440 ASSERT(count <= ichdr_s->count);
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 /*
2444 * Move the entries in the destination leaf up to make a hole?
2445 */
Dave Chinner517c2222013-04-24 18:58:55 +10002446 if (start_d < ichdr_d->count) {
2447 tmp = ichdr_d->count - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002449 entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2450 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2451 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 }
2453
2454 /*
2455 * Copy all entry's in the same (sorted) order,
2456 * but allocate attribute info packed and in sequence.
2457 */
Dave Chinner517c2222013-04-24 18:58:55 +10002458 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2459 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 desti = start_d;
2461 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10002462 ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2464#ifdef GROT
2465 /*
2466 * Code to drop INCOMPLETE entries. Difficult to use as we
2467 * may also need to change the insertion index. Code turned
2468 * off for 6.2, should be revisited later.
2469 */
2470 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Dave Chinner517c2222013-04-24 18:58:55 +10002471 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2472 ichdr_s->usedbytes -= tmp;
2473 ichdr_s->count -= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 entry_d--; /* to compensate for ++ in loop hdr */
2475 desti--;
2476 if ((start_s + i) < offset)
2477 result++; /* insertion index adjustment */
2478 } else {
2479#endif /* GROT */
Dave Chinner517c2222013-04-24 18:58:55 +10002480 ichdr_d->firstused -= tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 /* both on-disk, don't endian flip twice */
2482 entry_d->hashval = entry_s->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002483 entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002485 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 <= XFS_LBSIZE(mp));
Dave Chinner517c2222013-04-24 18:58:55 +10002487 memmove(xfs_attr3_leaf_name(leaf_d, desti),
2488 xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002489 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 <= XFS_LBSIZE(mp));
Dave Chinner517c2222013-04-24 18:58:55 +10002491 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2492 ichdr_s->usedbytes -= tmp;
2493 ichdr_d->usedbytes += tmp;
2494 ichdr_s->count -= 1;
2495 ichdr_d->count += 1;
2496 tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2497 + xfs_attr3_leaf_hdr_size(leaf_d);
2498 ASSERT(ichdr_d->firstused >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499#ifdef GROT
2500 }
2501#endif /* GROT */
2502 }
2503
2504 /*
2505 * Zero out the entries we just copied.
2506 */
Dave Chinner517c2222013-04-24 18:58:55 +10002507 if (start_s == ichdr_s->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002509 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 ASSERT(((char *)entry_s + tmp) <=
2511 ((char *)leaf_s + XFS_LBSIZE(mp)));
Dave Chinner517c2222013-04-24 18:58:55 +10002512 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 } else {
2514 /*
2515 * Move the remaining entries down to fill the hole,
2516 * then zero the entries at the top.
2517 */
Dave Chinner517c2222013-04-24 18:58:55 +10002518 tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2519 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2520 entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2521 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002524 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 ASSERT(((char *)entry_s + tmp) <=
2526 ((char *)leaf_s + XFS_LBSIZE(mp)));
Dave Chinner517c2222013-04-24 18:58:55 +10002527 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529
2530 /*
2531 * Fill in the freemap information
2532 */
Dave Chinner517c2222013-04-24 18:58:55 +10002533 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2534 ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2535 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2536 ichdr_d->freemap[1].base = 0;
2537 ichdr_d->freemap[2].base = 0;
2538 ichdr_d->freemap[1].size = 0;
2539 ichdr_d->freemap[2].size = 0;
2540 ichdr_s->holes = 1; /* leaf may not be compact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
2543/*
2544 * Pick up the last hashvalue from a leaf block.
2545 */
2546xfs_dahash_t
Dave Chinner1d9025e2012-06-22 18:50:14 +10002547xfs_attr_leaf_lasthash(
2548 struct xfs_buf *bp,
2549 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Dave Chinner517c2222013-04-24 18:58:55 +10002551 struct xfs_attr3_icleaf_hdr ichdr;
2552 struct xfs_attr_leaf_entry *entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Dave Chinner517c2222013-04-24 18:58:55 +10002554 xfs_attr3_leaf_hdr_from_disk(&ichdr, bp->b_addr);
2555 entries = xfs_attr3_leaf_entryp(bp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (count)
Dave Chinner517c2222013-04-24 18:58:55 +10002557 *count = ichdr.count;
2558 if (!ichdr.count)
2559 return 0;
2560 return be32_to_cpu(entries[ichdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
2563/*
2564 * Calculate the number of bytes used to store the indicated attribute
2565 * (whether local or remote only calculate bytes in this block).
2566 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002567STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2569{
Dave Chinner517c2222013-04-24 18:58:55 +10002570 struct xfs_attr_leaf_entry *entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 xfs_attr_leaf_name_local_t *name_loc;
2572 xfs_attr_leaf_name_remote_t *name_rmt;
2573 int size;
2574
Dave Chinner517c2222013-04-24 18:58:55 +10002575 entries = xfs_attr3_leaf_entryp(leaf);
2576 if (entries[index].flags & XFS_ATTR_LOCAL) {
2577 name_loc = xfs_attr3_leaf_name_local(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002578 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002579 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002581 name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002582 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
Dave Chinner517c2222013-04-24 18:58:55 +10002584 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
2587/*
2588 * Calculate the number of bytes that would be required to store the new
2589 * attribute (whether local or remote only calculate bytes in this block).
2590 * This routine decides as a side effect whether the attribute will be
2591 * a "local" or a "remote" attribute.
2592 */
2593int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002594xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
2596 int size;
2597
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002598 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2599 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (local) {
2601 *local = 1;
2602 }
2603 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002604 size = xfs_attr_leaf_entsize_remote(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 if (local) {
2606 *local = 0;
2607 }
2608 }
Dave Chinner517c2222013-04-24 18:58:55 +10002609 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
2612/*
2613 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2614 */
2615int
Dave Chinner517c2222013-04-24 18:58:55 +10002616xfs_attr3_leaf_list_int(
2617 struct xfs_buf *bp,
2618 struct xfs_attr_list_context *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
Dave Chinner517c2222013-04-24 18:58:55 +10002620 struct attrlist_cursor_kern *cursor;
2621 struct xfs_attr_leafblock *leaf;
2622 struct xfs_attr3_icleaf_hdr ichdr;
2623 struct xfs_attr_leaf_entry *entries;
2624 struct xfs_attr_leaf_entry *entry;
2625 int retval;
2626 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002628 trace_xfs_attr_list_leaf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Dave Chinner517c2222013-04-24 18:58:55 +10002630 leaf = bp->b_addr;
2631 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2632 entries = xfs_attr3_leaf_entryp(leaf);
2633
2634 cursor = context->cursor;
2635 cursor->initted = 1;
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 /*
2638 * Re-find our place in the leaf block if this is a new syscall.
2639 */
2640 if (context->resynch) {
Dave Chinner517c2222013-04-24 18:58:55 +10002641 entry = &entries[0];
2642 for (i = 0; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002643 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (cursor->offset == context->dupcnt) {
2645 context->dupcnt = 0;
2646 break;
2647 }
2648 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002649 } else if (be32_to_cpu(entry->hashval) >
2650 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 context->dupcnt = 0;
2652 break;
2653 }
2654 }
Dave Chinner517c2222013-04-24 18:58:55 +10002655 if (i == ichdr.count) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002656 trace_xfs_attr_list_notfound(context);
Dave Chinner517c2222013-04-24 18:58:55 +10002657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
2659 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002660 entry = &entries[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 i = 0;
2662 }
2663 context->resynch = 0;
2664
2665 /*
2666 * We have found our place, start copying out the new attributes.
2667 */
2668 retval = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10002669 for (; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002670 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2671 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 cursor->offset = 0;
2673 }
2674
2675 if (entry->flags & XFS_ATTR_INCOMPLETE)
2676 continue; /* skip incomplete entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002679 xfs_attr_leaf_name_local_t *name_loc =
Dave Chinner517c2222013-04-24 18:58:55 +10002680 xfs_attr3_leaf_name_local(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002681
2682 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002683 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002684 name_loc->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +10002685 (int)name_loc->namelen,
2686 be16_to_cpu(name_loc->valuelen),
Dave Chinnera9273ca2010-01-20 10:47:48 +11002687 &name_loc->nameval[name_loc->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +10002688 if (retval)
2689 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002691 xfs_attr_leaf_name_remote_t *name_rmt =
Dave Chinner517c2222013-04-24 18:58:55 +10002692 xfs_attr3_leaf_name_remote(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002693
2694 int valuelen = be32_to_cpu(name_rmt->valuelen);
2695
2696 if (context->put_value) {
2697 xfs_da_args_t args;
2698
2699 memset((char *)&args, 0, sizeof(args));
2700 args.dp = context->dp;
2701 args.whichfork = XFS_ATTR_FORK;
2702 args.valuelen = valuelen;
Dave Chinner622d8142010-12-23 11:57:37 +11002703 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
Tim Shimmin726801b2006-09-28 11:01:37 +10002704 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2705 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2706 retval = xfs_attr_rmtval_get(&args);
2707 if (retval)
2708 return retval;
2709 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002710 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002711 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002712 (int)name_rmt->namelen,
2713 valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002714 args.value);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002715 kmem_free(args.value);
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002716 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002717 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002718 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002719 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002720 (int)name_rmt->namelen,
2721 valuelen,
2722 NULL);
2723 }
2724 if (retval)
2725 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002727 if (context->seen_enough)
2728 break;
2729 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002731 trace_xfs_attr_list_leaf_end(context);
Dave Chinner517c2222013-04-24 18:58:55 +10002732 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736/*========================================================================
2737 * Manage the INCOMPLETE flag in a leaf entry
2738 *========================================================================*/
2739
2740/*
2741 * Clear the INCOMPLETE flag on an entry in a leaf block.
2742 */
2743int
Dave Chinner517c2222013-04-24 18:58:55 +10002744xfs_attr3_leaf_clearflag(
2745 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
Dave Chinner517c2222013-04-24 18:58:55 +10002747 struct xfs_attr_leafblock *leaf;
2748 struct xfs_attr_leaf_entry *entry;
2749 struct xfs_attr_leaf_name_remote *name_rmt;
2750 struct xfs_buf *bp;
2751 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002753 struct xfs_attr3_icleaf_hdr ichdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 xfs_attr_leaf_name_local_t *name_loc;
2755 int namelen;
2756 char *name;
2757#endif /* DEBUG */
2758
Dave Chinner5a5881c2012-03-22 05:15:13 +00002759 trace_xfs_attr_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 /*
2761 * Set up the operation.
2762 */
Dave Chinner517c2222013-04-24 18:58:55 +10002763 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002764 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Dave Chinner1d9025e2012-06-22 18:50:14 +10002767 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002768 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2770
2771#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002772 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2773 ASSERT(args->index < ichdr.count);
2774 ASSERT(args->index >= 0);
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002777 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 namelen = name_loc->namelen;
2779 name = (char *)name_loc->nameval;
2780 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002781 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 namelen = name_rmt->namelen;
2783 name = (char *)name_rmt->name;
2784 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002785 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 ASSERT(namelen == args->namelen);
2787 ASSERT(memcmp(name, args->name, namelen) == 0);
2788#endif /* DEBUG */
2789
2790 entry->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002791 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2793
2794 if (args->rmtblkno) {
2795 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002796 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002797 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2798 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002799 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 /*
2804 * Commit the flag value change and start the next trans in series.
2805 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002806 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807}
2808
2809/*
2810 * Set the INCOMPLETE flag on an entry in a leaf block.
2811 */
2812int
Dave Chinner517c2222013-04-24 18:58:55 +10002813xfs_attr3_leaf_setflag(
2814 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Dave Chinner517c2222013-04-24 18:58:55 +10002816 struct xfs_attr_leafblock *leaf;
2817 struct xfs_attr_leaf_entry *entry;
2818 struct xfs_attr_leaf_name_remote *name_rmt;
2819 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 int error;
Dave Chinner517c2222013-04-24 18:58:55 +10002821#ifdef DEBUG
2822 struct xfs_attr3_icleaf_hdr ichdr;
2823#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Dave Chinner5a5881c2012-03-22 05:15:13 +00002825 trace_xfs_attr_leaf_setflag(args);
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 /*
2828 * Set up the operation.
2829 */
Dave Chinner517c2222013-04-24 18:58:55 +10002830 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002831 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Dave Chinner1d9025e2012-06-22 18:50:14 +10002834 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002835#ifdef DEBUG
2836 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2837 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 ASSERT(args->index >= 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002839#endif
2840 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2843 entry->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002844 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2846 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002847 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 name_rmt->valueblk = 0;
2849 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002850 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 /*
2855 * Commit the flag value change and start the next trans in series.
2856 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002857 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858}
2859
2860/*
2861 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2862 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2863 * entry given by args->blkno2/index2.
2864 *
2865 * Note that they could be in different blocks, or in the same block.
2866 */
2867int
Dave Chinner517c2222013-04-24 18:58:55 +10002868xfs_attr3_leaf_flipflags(
2869 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
Dave Chinner517c2222013-04-24 18:58:55 +10002871 struct xfs_attr_leafblock *leaf1;
2872 struct xfs_attr_leafblock *leaf2;
2873 struct xfs_attr_leaf_entry *entry1;
2874 struct xfs_attr_leaf_entry *entry2;
2875 struct xfs_attr_leaf_name_remote *name_rmt;
2876 struct xfs_buf *bp1;
2877 struct xfs_buf *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 int error;
2879#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002880 struct xfs_attr3_icleaf_hdr ichdr1;
2881 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 xfs_attr_leaf_name_local_t *name_loc;
2883 int namelen1, namelen2;
2884 char *name1, *name2;
2885#endif /* DEBUG */
2886
Dave Chinner5a5881c2012-03-22 05:15:13 +00002887 trace_xfs_attr_leaf_flipflags(args);
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 /*
2890 * Read the block containing the "old" attr
2891 */
Dave Chinner517c2222013-04-24 18:58:55 +10002892 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
Dave Chinnerad14c332012-11-12 22:54:16 +11002893 if (error)
2894 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 /*
2897 * Read the block containing the "new" attr, if it is different
2898 */
2899 if (args->blkno2 != args->blkno) {
Dave Chinner517c2222013-04-24 18:58:55 +10002900 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
Dave Chinnerad14c332012-11-12 22:54:16 +11002901 -1, &bp2);
2902 if (error)
2903 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 } else {
2905 bp2 = bp1;
2906 }
2907
Dave Chinner1d9025e2012-06-22 18:50:14 +10002908 leaf1 = bp1->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002909 entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Dave Chinner1d9025e2012-06-22 18:50:14 +10002911 leaf2 = bp2->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002912 entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002915 xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1);
2916 ASSERT(args->index < ichdr1.count);
2917 ASSERT(args->index >= 0);
2918
2919 xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2);
2920 ASSERT(args->index2 < ichdr2.count);
2921 ASSERT(args->index2 >= 0);
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (entry1->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002924 name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 namelen1 = name_loc->namelen;
2926 name1 = (char *)name_loc->nameval;
2927 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002928 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 namelen1 = name_rmt->namelen;
2930 name1 = (char *)name_rmt->name;
2931 }
2932 if (entry2->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002933 name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 namelen2 = name_loc->namelen;
2935 name2 = (char *)name_loc->nameval;
2936 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002937 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 namelen2 = name_rmt->namelen;
2939 name2 = (char *)name_rmt->name;
2940 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002941 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 ASSERT(namelen1 == namelen2);
2943 ASSERT(memcmp(name1, name2, namelen1) == 0);
2944#endif /* DEBUG */
2945
2946 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2947 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2948
2949 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002950 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2952 if (args->rmtblkno) {
2953 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002954 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002955 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2956 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002957 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2959 }
2960
2961 entry2->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002962 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2964 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002965 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 name_rmt->valueblk = 0;
2967 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002968 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 /*
2973 * Commit the flag value change and start the next trans in series.
2974 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002975 error = xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Dave Chinner517c2222013-04-24 18:58:55 +10002977 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980/*========================================================================
2981 * Indiscriminately delete the entire attribute fork
2982 *========================================================================*/
2983
2984/*
2985 * Recurse (gasp!) through the attribute nodes until we find leaves.
2986 * We're doing a depth-first traversal in order to invalidate everything.
2987 */
2988int
Dave Chinner517c2222013-04-24 18:58:55 +10002989xfs_attr3_root_inactive(
2990 struct xfs_trans **trans,
2991 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Dave Chinner517c2222013-04-24 18:58:55 +10002993 struct xfs_da_blkinfo *info;
2994 struct xfs_buf *bp;
2995 xfs_daddr_t blkno;
2996 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 /*
2999 * Read block 0 to see what we have to work with.
3000 * We only get here if we have extents, since we remove
3001 * the extents in reverse order the extent containing
3002 * block 0 must still be there.
3003 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003004 error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003006 return error;
3007 blkno = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 /*
3010 * Invalidate the tree, even if the "tree" is only a single leaf block.
3011 * This is a depth-first traversal!
3012 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10003013 info = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003014 switch (info->magic) {
3015 case cpu_to_be16(XFS_DA_NODE_MAGIC):
3016 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
3017 error = xfs_attr3_node_inactive(trans, dp, bp, 1);
3018 break;
3019 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
3020 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
3021 error = xfs_attr3_leaf_inactive(trans, dp, bp);
3022 break;
3023 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003025 xfs_trans_brelse(*trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003026 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003029 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 /*
3032 * Invalidate the incore copy of the root block.
3033 */
3034 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
3035 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003036 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003037 xfs_trans_binval(*trans, bp); /* remove from cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 /*
3039 * Commit the invalidate and start the next transaction.
3040 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003041 error = xfs_trans_roll(trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Dave Chinner517c2222013-04-24 18:58:55 +10003043 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
3046/*
3047 * Recurse (gasp!) through the attribute nodes until we find leaves.
3048 * We're doing a depth-first traversal in order to invalidate everything.
3049 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003050STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10003051xfs_attr3_node_inactive(
Dave Chinner1d9025e2012-06-22 18:50:14 +10003052 struct xfs_trans **trans,
3053 struct xfs_inode *dp,
3054 struct xfs_buf *bp,
3055 int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 xfs_da_blkinfo_t *info;
3058 xfs_da_intnode_t *node;
3059 xfs_dablk_t child_fsb;
3060 xfs_daddr_t parent_blkno, child_blkno;
Dave Chinner517c2222013-04-24 18:58:55 +10003061 int error, i;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003062 struct xfs_buf *child_bp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003063 struct xfs_da_node_entry *btree;
Dave Chinner517c2222013-04-24 18:58:55 +10003064 struct xfs_da3_icnode_hdr ichdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 /*
3067 * Since this code is recursive (gasp!) we must protect ourselves.
3068 */
3069 if (level > XFS_DA_NODE_MAXDEPTH) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10003070 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Dave Chinner517c2222013-04-24 18:58:55 +10003071 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
3073
Dave Chinner1d9025e2012-06-22 18:50:14 +10003074 node = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003075 xfs_da3_node_hdr_from_disk(&ichdr, node);
3076 parent_blkno = bp->b_bn;
3077 if (!ichdr.count) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10003078 xfs_trans_brelse(*trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003081 btree = xfs_da3_node_tree_p(node);
3082 child_fsb = be32_to_cpu(btree[0].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003083 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 /*
3086 * If this is the node level just above the leaves, simply loop
3087 * over the leaves removing all of them. If this is higher up
3088 * in the tree, recurse downward.
3089 */
Dave Chinner517c2222013-04-24 18:58:55 +10003090 for (i = 0; i < ichdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 /*
3092 * Read the subsidiary block to see what we have to work with.
3093 * Don't do this in a transaction. This is a depth-first
3094 * traversal of the tree so we may deal with many blocks
3095 * before we come back to this one.
3096 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003097 error = xfs_da3_node_read(*trans, dp, child_fsb, -2, &child_bp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11003098 XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 if (error)
3100 return(error);
3101 if (child_bp) {
3102 /* save for re-read later */
Dave Chinner1d9025e2012-06-22 18:50:14 +10003103 child_blkno = XFS_BUF_ADDR(child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 /*
3106 * Invalidate the subtree, however we have to.
3107 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10003108 info = child_bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003109 switch (info->magic) {
3110 case cpu_to_be16(XFS_DA_NODE_MAGIC):
3111 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
3112 error = xfs_attr3_node_inactive(trans, dp,
3113 child_bp, level + 1);
3114 break;
3115 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
3116 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
3117 error = xfs_attr3_leaf_inactive(trans, dp,
3118 child_bp);
3119 break;
3120 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003122 xfs_trans_brelse(*trans, child_bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 }
3125 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003126 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 /*
3129 * Remove the subsidiary block from the cache
3130 * and from the log.
3131 */
3132 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
3133 &child_bp, XFS_ATTR_FORK);
3134 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003135 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003136 xfs_trans_binval(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
3138
3139 /*
3140 * If we're not done, re-read the parent to get the next
3141 * child block number.
3142 */
Dave Chinner517c2222013-04-24 18:58:55 +10003143 if (i + 1 < ichdr.count) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003144 error = xfs_da3_node_read(*trans, dp, 0, parent_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11003145 &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003147 return error;
3148 child_fsb = be32_to_cpu(btree[i + 1].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003149 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 }
3151 /*
3152 * Atomically commit the whole invalidate stuff.
3153 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003154 error = xfs_trans_roll(trans, dp);
3155 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003156 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
3158
Dave Chinner517c2222013-04-24 18:58:55 +10003159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
3161
3162/*
3163 * Invalidate all of the "remote" value regions pointed to by a particular
3164 * leaf block.
3165 * Note that we must release the lock on the buffer so that we are not
3166 * caught holding something that the logging code wants to flush to disk.
3167 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003168STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10003169xfs_attr3_leaf_inactive(
3170 struct xfs_trans **trans,
3171 struct xfs_inode *dp,
3172 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Dave Chinner517c2222013-04-24 18:58:55 +10003174 struct xfs_attr_leafblock *leaf;
3175 struct xfs_attr3_icleaf_hdr ichdr;
3176 struct xfs_attr_leaf_entry *entry;
3177 struct xfs_attr_leaf_name_remote *name_rmt;
3178 struct xfs_attr_inactive_list *list;
3179 struct xfs_attr_inactive_list *lp;
3180 int error;
3181 int count;
3182 int size;
3183 int tmp;
3184 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Dave Chinner1d9025e2012-06-22 18:50:14 +10003186 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003187 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
3189 /*
3190 * Count the number of "remote" value extents.
3191 */
3192 count = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10003193 entry = xfs_attr3_leaf_entryp(leaf);
3194 for (i = 0; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11003195 if (be16_to_cpu(entry->nameidx) &&
3196 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10003197 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (name_rmt->valueblk)
3199 count++;
3200 }
3201 }
3202
3203 /*
3204 * If there are no "remote" values, we're done.
3205 */
3206 if (count == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10003207 xfs_trans_brelse(*trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210
3211 /*
3212 * Allocate storage for a list of all the "remote" value extents.
3213 */
3214 size = count * sizeof(xfs_attr_inactive_list_t);
Dave Chinner517c2222013-04-24 18:58:55 +10003215 list = kmem_alloc(size, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 /*
3218 * Identify each of the "remote" value extents.
3219 */
3220 lp = list;
Dave Chinner517c2222013-04-24 18:58:55 +10003221 entry = xfs_attr3_leaf_entryp(leaf);
3222 for (i = 0; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11003223 if (be16_to_cpu(entry->nameidx) &&
3224 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10003225 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11003227 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
3228 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
3229 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 lp++;
3231 }
3232 }
3233 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10003234 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 /*
3237 * Invalidate each of the "remote" value extents.
3238 */
3239 error = 0;
3240 for (lp = list, i = 0; i < count; i++, lp++) {
Dave Chinner517c2222013-04-24 18:58:55 +10003241 tmp = xfs_attr3_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11003242 lp->valueblk, lp->valuelen);
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (error == 0)
3245 error = tmp; /* save only the 1st errno */
3246 }
3247
Dave Chinner517c2222013-04-24 18:58:55 +10003248 kmem_free(list);
3249 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
3252/*
3253 * Look at all the extents for this logical region,
3254 * invalidate any buffers that are incore/in transactions.
3255 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003256STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10003257xfs_attr3_leaf_freextent(
3258 struct xfs_trans **trans,
3259 struct xfs_inode *dp,
3260 xfs_dablk_t blkno,
3261 int blkcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Dave Chinner517c2222013-04-24 18:58:55 +10003263 struct xfs_bmbt_irec map;
3264 struct xfs_buf *bp;
3265 xfs_dablk_t tblkno;
3266 xfs_daddr_t dblkno;
3267 int tblkcnt;
3268 int dblkcnt;
3269 int nmap;
3270 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
3272 /*
3273 * Roll through the "value", invalidating the attribute value's
3274 * blocks.
3275 */
3276 tblkno = blkno;
3277 tblkcnt = blkcnt;
3278 while (tblkcnt > 0) {
3279 /*
3280 * Try to remember where we decided to put the value.
3281 */
3282 nmap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003283 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
3284 &map, &nmap, XFS_BMAPI_ATTRFORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (error) {
3286 return(error);
3287 }
3288 ASSERT(nmap == 1);
3289 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3290
3291 /*
3292 * If it's a hole, these are already unmapped
3293 * so there's nothing to invalidate.
3294 */
3295 if (map.br_startblock != HOLESTARTBLOCK) {
3296
3297 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3298 map.br_startblock);
3299 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3300 map.br_blockcount);
3301 bp = xfs_trans_get_buf(*trans,
3302 dp->i_mount->m_ddev_targp,
Dave Chinnera8acad72012-04-23 15:58:54 +10003303 dblkno, dblkcnt, 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00003304 if (!bp)
3305 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 xfs_trans_binval(*trans, bp);
3307 /*
3308 * Roll to next transaction.
3309 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003310 error = xfs_trans_roll(trans, dp);
3311 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 return (error);
3313 }
3314
3315 tblkno += map.br_blockcount;
3316 tblkcnt -= map.br_blockcount;
3317 }
3318
3319 return(0);
3320}