blob: 31d3cd12926918978f922edb6e0f01c22d29d864 [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{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100274 int err;
275
276 err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinner517c2222013-04-24 18:58:55 +1000277 XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100278 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100279 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100280 return err;
Dave Chinnerad14c332012-11-12 22:54:16 +1100281}
282
Tim Shimmin726801b2006-09-28 11:01:37 +1000283/*========================================================================
284 * Namespace helper routines
285 *========================================================================*/
286
Tim Shimmin726801b2006-09-28 11:01:37 +1000287/*
288 * If namespace bits don't match return 0.
289 * If all match then return 1.
290 */
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000291STATIC int
Tim Shimmin726801b2006-09-28 11:01:37 +1000292xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
293{
294 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/*========================================================================
Nathan Scottd8cc8902005-11-02 10:34:53 +1100299 * External routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 *========================================================================*/
301
302/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100303 * Query whether the requested number of additional bytes of extended
304 * attribute space will be able to fit inline.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000305 *
Nathan Scottd8cc8902005-11-02 10:34:53 +1100306 * Returns zero if not, else the di_forkoff fork offset to be used in the
307 * literal area for attribute data once the new bytes have been added.
308 *
309 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
310 * special case for dev/uuid inodes, they have fixed size data forks.
311 */
312int
313xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
314{
315 int offset;
316 int minforkoff; /* lower limit on valid forkoff locations */
317 int maxforkoff; /* upper limit on valid forkoff locations */
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000318 int dsize;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100319 xfs_mount_t *mp = dp->i_mount;
320
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100321 /* rounded down */
322 offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100323
324 switch (dp->i_d.di_format) {
325 case XFS_DINODE_FMT_DEV:
326 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
327 return (offset >= minforkoff) ? minforkoff : 0;
328 case XFS_DINODE_FMT_UUID:
329 minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
330 return (offset >= minforkoff) ? minforkoff : 0;
331 }
332
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000333 /*
334 * If the requested numbers of bytes is smaller or equal to the
335 * current attribute fork size we can always proceed.
336 *
337 * Note that if_bytes in the data fork might actually be larger than
338 * the current data fork size is due to delalloc extents. In that
339 * case either the extent count will go down when they are converted
340 * to real extents, or the delalloc conversion will take care of the
341 * literal area rebalancing.
342 */
343 if (bytes <= XFS_IFORK_ASIZE(dp))
344 return dp->i_d.di_forkoff;
345
346 /*
347 * For attr2 we can try to move the forkoff if there is space in the
348 * literal area, but for the old format we are done if there is no
349 * space in the fixed attribute fork.
350 */
351 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
Nathan Scottda087ba2005-11-02 15:00:20 +1100352 return 0;
Nathan Scottda087ba2005-11-02 15:00:20 +1100353
Barry Naujoke5889e92007-02-10 18:35:58 +1100354 dsize = dp->i_df.if_bytes;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000355
Barry Naujoke5889e92007-02-10 18:35:58 +1100356 switch (dp->i_d.di_format) {
357 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000358 /*
Barry Naujoke5889e92007-02-10 18:35:58 +1100359 * If there is no attr fork and the data fork is extents,
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000360 * determine if creating the default attr fork will result
361 * in the extents form migrating to btree. If so, the
362 * minimum offset only needs to be the space required for
Barry Naujoke5889e92007-02-10 18:35:58 +1100363 * the btree root.
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000364 */
Christoph Hellwig1a5902c2009-03-29 19:26:46 +0200365 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
366 xfs_default_attroffset(dp))
Barry Naujoke5889e92007-02-10 18:35:58 +1100367 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
368 break;
Barry Naujoke5889e92007-02-10 18:35:58 +1100369 case XFS_DINODE_FMT_BTREE:
370 /*
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000371 * If we have a data btree then keep forkoff if we have one,
372 * otherwise we are adding a new attr, so then we set
373 * minforkoff to where the btree root can finish so we have
Barry Naujoke5889e92007-02-10 18:35:58 +1100374 * plenty of room for attrs
375 */
376 if (dp->i_d.di_forkoff) {
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000377 if (offset < dp->i_d.di_forkoff)
Barry Naujoke5889e92007-02-10 18:35:58 +1100378 return 0;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000379 return dp->i_d.di_forkoff;
380 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500381 dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
Barry Naujoke5889e92007-02-10 18:35:58 +1100382 break;
383 }
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000384
385 /*
386 * A data fork btree root must have space for at least
Barry Naujoke5889e92007-02-10 18:35:58 +1100387 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
388 */
389 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
Nathan Scottd8cc8902005-11-02 10:34:53 +1100390 minforkoff = roundup(minforkoff, 8) >> 3;
391
392 /* attr fork btree root can have at least this many key/ptr pairs */
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100393 maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
394 XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100395 maxforkoff = maxforkoff >> 3; /* rounded down */
396
Nathan Scottd8cc8902005-11-02 10:34:53 +1100397 if (offset >= maxforkoff)
398 return maxforkoff;
Christoph Hellwig4c393a62011-11-19 17:44:30 +0000399 if (offset >= minforkoff)
400 return offset;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100401 return 0;
402}
403
404/*
405 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
406 */
407STATIC void
408xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
409{
Nathan Scott13059ff2006-01-11 15:32:01 +1100410 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
Eric Sandeen62118702008-03-06 13:44:28 +1100411 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000412 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +1100413 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
414 xfs_sb_version_addattr2(&mp->m_sb);
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000415 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100416 xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
417 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +1000418 spin_unlock(&mp->m_sb_lock);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100419 }
420}
421
422/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * Create the initial contents of a shortform attribute list.
424 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100425void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426xfs_attr_shortform_create(xfs_da_args_t *args)
427{
428 xfs_attr_sf_hdr_t *hdr;
429 xfs_inode_t *dp;
430 xfs_ifork_t *ifp;
431
Dave Chinner5a5881c2012-03-22 05:15:13 +0000432 trace_xfs_attr_sf_create(args);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dp = args->dp;
435 ASSERT(dp != NULL);
436 ifp = dp->i_afp;
437 ASSERT(ifp != NULL);
438 ASSERT(ifp->if_bytes == 0);
439 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
440 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
441 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
442 ifp->if_flags |= XFS_IFINLINE;
443 } else {
444 ASSERT(ifp->if_flags & XFS_IFINLINE);
445 }
446 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
447 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
448 hdr->count = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100449 hdr->totsize = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/*
454 * Add a name/value pair to the shortform attribute list.
455 * Overflow from the inode has already been checked for.
456 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100457void
458xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 xfs_attr_shortform_t *sf;
461 xfs_attr_sf_entry_t *sfe;
462 int i, offset, size;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100463 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 xfs_inode_t *dp;
465 xfs_ifork_t *ifp;
466
Dave Chinner5a5881c2012-03-22 05:15:13 +0000467 trace_xfs_attr_sf_add(args);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100470 mp = dp->i_mount;
471 dp->i_d.di_forkoff = forkoff;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 ifp = dp->i_afp;
474 ASSERT(ifp->if_flags & XFS_IFINLINE);
475 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
476 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100477 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
Nathan Scottd8cc8902005-11-02 10:34:53 +1100478#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (sfe->namelen != args->namelen)
480 continue;
481 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
482 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000483 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 continue;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100485 ASSERT(0);
486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
489 offset = (char *)sfe - (char *)sf;
490 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
491 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
492 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
493 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
494
495 sfe->namelen = args->namelen;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100496 sfe->valuelen = args->valuelen;
Tim Shimmin726801b2006-09-28 11:01:37 +1000497 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 memcpy(sfe->nameval, args->name, args->namelen);
499 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100500 sf->hdr.count++;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800501 be16_add_cpu(&sf->hdr.totsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
503
Nathan Scottd8cc8902005-11-02 10:34:53 +1100504 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/*
Christoph Hellwige1486de2009-02-04 09:36:00 +0100508 * After the last attribute is removed revert to original inode format,
509 * making all literal area available to the data fork once more.
510 */
511STATIC void
512xfs_attr_fork_reset(
513 struct xfs_inode *ip,
514 struct xfs_trans *tp)
515{
516 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
517 ip->i_d.di_forkoff = 0;
518 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
519
520 ASSERT(ip->i_d.di_anextents == 0);
521 ASSERT(ip->i_afp == NULL);
522
Christoph Hellwige1486de2009-02-04 09:36:00 +0100523 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
524}
525
526/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100527 * Remove an attribute from the shortform attribute list structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
529int
530xfs_attr_shortform_remove(xfs_da_args_t *args)
531{
532 xfs_attr_shortform_t *sf;
533 xfs_attr_sf_entry_t *sfe;
534 int base, size=0, end, totsize, i;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100535 xfs_mount_t *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 xfs_inode_t *dp;
537
Dave Chinner5a5881c2012-03-22 05:15:13 +0000538 trace_xfs_attr_sf_remove(args);
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 dp = args->dp;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100541 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 base = sizeof(xfs_attr_sf_hdr_t);
543 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
544 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100545 end = sf->hdr.count;
Nathan Scottd8cc8902005-11-02 10:34:53 +1100546 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 base += size, i++) {
548 size = XFS_ATTR_SF_ENTSIZE(sfe);
549 if (sfe->namelen != args->namelen)
550 continue;
551 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
552 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000553 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 continue;
555 break;
556 }
Nathan Scottd8cc8902005-11-02 10:34:53 +1100557 if (i == end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return(XFS_ERROR(ENOATTR));
559
Nathan Scottd8cc8902005-11-02 10:34:53 +1100560 /*
561 * Fix up the attribute fork data, covering the hole
562 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 end = base + size;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100564 totsize = be16_to_cpu(sf->hdr.totsize);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100565 if (end != totsize)
566 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
Nathan Scott3b244aa2006-03-17 17:29:25 +1100567 sf->hdr.count--;
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800568 be16_add_cpu(&sf->hdr.totsize, -size);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100569
570 /*
571 * Fix up the start offset of the attribute fork
572 */
573 totsize -= size;
Barry Naujok6a178102008-05-21 16:42:05 +1000574 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
Christoph Hellwige1486de2009-02-04 09:36:00 +0100575 (mp->m_flags & XFS_MOUNT_ATTR2) &&
576 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
577 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
578 xfs_attr_fork_reset(dp, args->trans);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100579 } else {
580 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
581 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
582 ASSERT(dp->i_d.di_forkoff);
Barry Naujok6a178102008-05-21 16:42:05 +1000583 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
584 (args->op_flags & XFS_DA_OP_ADDNAME) ||
585 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
586 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100587 xfs_trans_log_inode(args->trans, dp,
588 XFS_ILOG_CORE | XFS_ILOG_ADATA);
589 }
590
591 xfs_sbversion_add_attr2(mp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 return(0);
594}
595
596/*
597 * Look up a name in a shortform attribute list structure.
598 */
599/*ARGSUSED*/
600int
601xfs_attr_shortform_lookup(xfs_da_args_t *args)
602{
603 xfs_attr_shortform_t *sf;
604 xfs_attr_sf_entry_t *sfe;
605 int i;
606 xfs_ifork_t *ifp;
607
Dave Chinner5a5881c2012-03-22 05:15:13 +0000608 trace_xfs_attr_sf_lookup(args);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 ifp = args->dp->i_afp;
611 ASSERT(ifp->if_flags & XFS_IFINLINE);
612 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
613 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100614 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
616 if (sfe->namelen != args->namelen)
617 continue;
618 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
619 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000620 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 continue;
622 return(XFS_ERROR(EEXIST));
623 }
624 return(XFS_ERROR(ENOATTR));
625}
626
627/*
628 * Look up a name in a shortform attribute list structure.
629 */
630/*ARGSUSED*/
631int
632xfs_attr_shortform_getvalue(xfs_da_args_t *args)
633{
634 xfs_attr_shortform_t *sf;
635 xfs_attr_sf_entry_t *sfe;
636 int i;
637
638 ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
639 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
640 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100641 for (i = 0; i < sf->hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
643 if (sfe->namelen != args->namelen)
644 continue;
645 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
646 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +1000647 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 continue;
649 if (args->flags & ATTR_KERNOVAL) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100650 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return(XFS_ERROR(EEXIST));
652 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100653 if (args->valuelen < sfe->valuelen) {
654 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return(XFS_ERROR(ERANGE));
656 }
Nathan Scott3b244aa2006-03-17 17:29:25 +1100657 args->valuelen = sfe->valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 memcpy(args->value, &sfe->nameval[args->namelen],
659 args->valuelen);
660 return(XFS_ERROR(EEXIST));
661 }
662 return(XFS_ERROR(ENOATTR));
663}
664
665/*
666 * Convert from using the shortform to the leaf.
667 */
668int
669xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
670{
671 xfs_inode_t *dp;
672 xfs_attr_shortform_t *sf;
673 xfs_attr_sf_entry_t *sfe;
674 xfs_da_args_t nargs;
675 char *tmpbuffer;
676 int error, i, size;
677 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000678 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 xfs_ifork_t *ifp;
680
Dave Chinner5a5881c2012-03-22 05:15:13 +0000681 trace_xfs_attr_sf_to_leaf(args);
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 dp = args->dp;
684 ifp = dp->i_afp;
685 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100686 size = be16_to_cpu(sf->hdr.totsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 tmpbuffer = kmem_alloc(size, KM_SLEEP);
688 ASSERT(tmpbuffer != NULL);
689 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
690 sf = (xfs_attr_shortform_t *)tmpbuffer;
691
692 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
693 bp = NULL;
694 error = xfs_da_grow_inode(args, &blkno);
695 if (error) {
696 /*
697 * If we hit an IO error middle of the transaction inside
698 * grow_inode(), we may have inconsistent data. Bail out.
699 */
700 if (error == EIO)
701 goto out;
702 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
703 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
704 goto out;
705 }
706
707 ASSERT(blkno == 0);
Dave Chinner517c2222013-04-24 18:58:55 +1000708 error = xfs_attr3_leaf_create(args, blkno, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (error) {
710 error = xfs_da_shrink_inode(args, 0, bp);
711 bp = NULL;
712 if (error)
713 goto out;
714 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
715 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
716 goto out;
717 }
718
719 memset((char *)&nargs, 0, sizeof(nargs));
720 nargs.dp = dp;
721 nargs.firstblock = args->firstblock;
722 nargs.flist = args->flist;
723 nargs.total = args->total;
724 nargs.whichfork = XFS_ATTR_FORK;
725 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +1000726 nargs.op_flags = XFS_DA_OP_OKNOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 sfe = &sf->list[0];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100729 for (i = 0; i < sf->hdr.count; i++) {
Dave Chinnera9273ca2010-01-20 10:47:48 +1100730 nargs.name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 nargs.namelen = sfe->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100732 nargs.value = &sfe->nameval[nargs.namelen];
Nathan Scott3b244aa2006-03-17 17:29:25 +1100733 nargs.valuelen = sfe->valuelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100734 nargs.hashval = xfs_da_hashname(sfe->nameval,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 sfe->namelen);
Tim Shimmin726801b2006-09-28 11:01:37 +1000736 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
Dave Chinner517c2222013-04-24 18:58:55 +1000737 error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 ASSERT(error == ENOATTR);
Dave Chinner517c2222013-04-24 18:58:55 +1000739 error = xfs_attr3_leaf_add(bp, &nargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 ASSERT(error != ENOSPC);
741 if (error)
742 goto out;
743 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
744 }
745 error = 0;
746
747out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000748 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return(error);
750}
751
752STATIC int
753xfs_attr_shortform_compare(const void *a, const void *b)
754{
755 xfs_attr_sf_sort_t *sa, *sb;
756
757 sa = (xfs_attr_sf_sort_t *)a;
758 sb = (xfs_attr_sf_sort_t *)b;
Nathan Scott984a0812006-03-17 17:29:31 +1100759 if (sa->hash < sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return(-1);
Nathan Scott984a0812006-03-17 17:29:31 +1100761 } else if (sa->hash > sb->hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return(1);
763 } else {
764 return(sa->entno - sb->entno);
765 }
766}
767
Tim Shimmin726801b2006-09-28 11:01:37 +1000768
769#define XFS_ISRESET_CURSOR(cursor) \
770 (!((cursor)->initted) && !((cursor)->hashval) && \
771 !((cursor)->blkno) && !((cursor)->offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772/*
773 * Copy out entries of shortform attribute lists for attr_list().
Nathan Scottc41564b2006-03-29 08:55:14 +1000774 * Shortform attribute lists are not stored in hashval sorted order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * If the output buffer is not large enough to hold them all, then we
776 * we have to calculate each entries' hashvalue and sort them before
777 * we can begin returning them to the user.
778 */
779/*ARGSUSED*/
780int
781xfs_attr_shortform_list(xfs_attr_list_context_t *context)
782{
783 attrlist_cursor_kern_t *cursor;
784 xfs_attr_sf_sort_t *sbuf, *sbp;
785 xfs_attr_shortform_t *sf;
786 xfs_attr_sf_entry_t *sfe;
787 xfs_inode_t *dp;
788 int sbsize, nsbuf, count, i;
Tim Shimmin726801b2006-09-28 11:01:37 +1000789 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 ASSERT(context != NULL);
792 dp = context->dp;
793 ASSERT(dp != NULL);
794 ASSERT(dp->i_afp != NULL);
795 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
796 ASSERT(sf != NULL);
797 if (!sf->hdr.count)
798 return(0);
799 cursor = context->cursor;
800 ASSERT(cursor != NULL);
801
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000802 trace_xfs_attr_list_sf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /*
Tim Shimmin726801b2006-09-28 11:01:37 +1000805 * If the buffer is large enough and the cursor is at the start,
806 * do not bother with sorting since we will return everything in
807 * one buffer and another call using the cursor won't need to be
808 * made.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * Note the generous fudge factor of 16 overhead bytes per entry.
Tim Shimmin726801b2006-09-28 11:01:37 +1000810 * If bufsize is zero then put_listent must be a search function
811 * and can just scan through what we have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Tim Shimmin726801b2006-09-28 11:01:37 +1000813 if (context->bufsize == 0 ||
814 (XFS_ISRESET_CURSOR(cursor) &&
815 (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
Nathan Scott3b244aa2006-03-17 17:29:25 +1100816 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Tim Shimmin726801b2006-09-28 11:01:37 +1000817 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000818 sfe->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100819 sfe->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +1000820 (int)sfe->namelen,
821 (int)sfe->valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100822 &sfe->nameval[sfe->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +1000823
824 /*
825 * Either search callback finished early or
826 * didn't fit it all in the buffer after all.
827 */
828 if (context->seen_enough)
829 break;
830
831 if (error)
832 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
834 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000835 trace_xfs_attr_list_sf_all(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return(0);
837 }
838
Tim Shimmin726801b2006-09-28 11:01:37 +1000839 /* do no more for a search callback */
840 if (context->bufsize == 0)
841 return 0;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /*
844 * It didn't all fit, so we have to sort everything on hashval.
845 */
Nathan Scott3b244aa2006-03-17 17:29:25 +1100846 sbsize = sf->hdr.count * sizeof(*sbuf);
Dave Chinner622d8142010-12-23 11:57:37 +1100847 sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP | KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /*
850 * Scan the attribute list for the rest of the entries, storing
851 * the relevant info from only those that match into a buffer.
852 */
853 nsbuf = 0;
Nathan Scott3b244aa2006-03-17 17:29:25 +1100854 for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (unlikely(
856 ((char *)sfe < (char *)sf) ||
857 ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
858 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
859 XFS_ERRLEVEL_LOW,
860 context->dp->i_mount, sfe);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000861 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return XFS_ERROR(EFSCORRUPTED);
863 }
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 sbp->entno = i;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100866 sbp->hash = xfs_da_hashname(sfe->nameval, sfe->namelen);
867 sbp->name = sfe->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 sbp->namelen = sfe->namelen;
869 /* These are bytes, and both on-disk, don't endian-flip */
870 sbp->valuelen = sfe->valuelen;
871 sbp->flags = sfe->flags;
872 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
873 sbp++;
874 nsbuf++;
875 }
876
877 /*
878 * Sort the entries on hash then entno.
879 */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100880 xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /*
883 * Re-find our place IN THE SORTED LIST.
884 */
885 count = 0;
886 cursor->initted = 1;
887 cursor->blkno = 0;
888 for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100889 if (sbp->hash == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (cursor->offset == count) {
891 break;
892 }
893 count++;
Nathan Scott984a0812006-03-17 17:29:31 +1100894 } else if (sbp->hash > cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 break;
896 }
897 }
898 if (i == nsbuf) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000899 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return(0);
901 }
902
903 /*
904 * Loop putting entries into the user buffer.
905 */
906 for ( ; i < nsbuf; i++, sbp++) {
Nathan Scott984a0812006-03-17 17:29:31 +1100907 if (cursor->hashval != sbp->hash) {
908 cursor->hashval = sbp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 cursor->offset = 0;
910 }
Tim Shimmin726801b2006-09-28 11:01:37 +1000911 error = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +1000912 sbp->flags,
Tim Shimmin726801b2006-09-28 11:01:37 +1000913 sbp->name,
914 sbp->namelen,
915 sbp->valuelen,
916 &sbp->name[sbp->namelen]);
917 if (error)
918 return error;
919 if (context->seen_enough)
920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 cursor->offset++;
922 }
923
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000924 kmem_free(sbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return(0);
926}
927
928/*
929 * Check a leaf attribute block to see if all the entries would fit into
930 * a shortform attribute list.
931 */
932int
Dave Chinner1d9025e2012-06-22 18:50:14 +1000933xfs_attr_shortform_allfit(
Dave Chinnerb38958d2013-05-20 09:51:14 +1000934 struct xfs_buf *bp,
935 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Dave Chinnerb38958d2013-05-20 09:51:14 +1000937 struct xfs_attr_leafblock *leaf;
938 struct xfs_attr_leaf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 xfs_attr_leaf_name_local_t *name_loc;
Dave Chinnerb38958d2013-05-20 09:51:14 +1000940 struct xfs_attr3_icleaf_hdr leafhdr;
941 int bytes;
942 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Dave Chinner1d9025e2012-06-22 18:50:14 +1000944 leaf = bp->b_addr;
Dave Chinnerb38958d2013-05-20 09:51:14 +1000945 xfs_attr3_leaf_hdr_from_disk(&leafhdr, leaf);
946 entry = xfs_attr3_leaf_entryp(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 bytes = sizeof(struct xfs_attr_sf_hdr);
Dave Chinnerb38958d2013-05-20 09:51:14 +1000949 for (i = 0; i < leafhdr.count; entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (entry->flags & XFS_ATTR_INCOMPLETE)
951 continue; /* don't copy partial entries */
952 if (!(entry->flags & XFS_ATTR_LOCAL))
953 return(0);
Dave Chinner517c2222013-04-24 18:58:55 +1000954 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
956 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100957 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return(0);
Dave Chinnerb38958d2013-05-20 09:51:14 +1000959 bytes += sizeof(struct xfs_attr_sf_entry) - 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100961 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100963 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100964 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100965 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Dave Chinnerb38958d2013-05-20 09:51:14 +1000966 return -1;
967 return xfs_attr_shortform_bytesfit(dp, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970/*
971 * Convert a leaf attribute list to shortform attribute list
972 */
973int
Dave Chinner517c2222013-04-24 18:58:55 +1000974xfs_attr3_leaf_to_shortform(
975 struct xfs_buf *bp,
976 struct xfs_da_args *args,
977 int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Dave Chinner517c2222013-04-24 18:58:55 +1000979 struct xfs_attr_leafblock *leaf;
980 struct xfs_attr3_icleaf_hdr ichdr;
981 struct xfs_attr_leaf_entry *entry;
982 struct xfs_attr_leaf_name_local *name_loc;
983 struct xfs_da_args nargs;
984 struct xfs_inode *dp = args->dp;
985 char *tmpbuffer;
986 int error;
987 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dave Chinner5a5881c2012-03-22 05:15:13 +0000989 trace_xfs_attr_leaf_to_sf(args);
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
Dave Chinner517c2222013-04-24 18:58:55 +1000992 if (!tmpbuffer)
993 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Dave Chinner1d9025e2012-06-22 18:50:14 +1000995 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
Dave Chinner517c2222013-04-24 18:58:55 +1000996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Dave Chinner517c2222013-04-24 18:58:55 +1000998 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
999 entry = xfs_attr3_leaf_entryp(leaf);
1000
1001 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001002 memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /*
1005 * Clean out the prior contents of the attribute list.
1006 */
1007 error = xfs_da_shrink_inode(args, 0, bp);
1008 if (error)
1009 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001010
1011 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +11001012 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +11001013 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Christoph Hellwige1486de2009-02-04 09:36:00 +01001014 xfs_attr_fork_reset(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001016 }
1017
1018 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /*
1021 * Copy the attributes
1022 */
1023 memset((char *)&nargs, 0, sizeof(nargs));
1024 nargs.dp = dp;
1025 nargs.firstblock = args->firstblock;
1026 nargs.flist = args->flist;
1027 nargs.total = args->total;
1028 nargs.whichfork = XFS_ATTR_FORK;
1029 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +10001030 nargs.op_flags = XFS_DA_OP_OKNOENT;
Dave Chinner517c2222013-04-24 18:58:55 +10001031
1032 for (i = 0; i < ichdr.count; entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (entry->flags & XFS_ATTR_INCOMPLETE)
1034 continue; /* don't copy partial entries */
1035 if (!entry->nameidx)
1036 continue;
1037 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Dave Chinner517c2222013-04-24 18:58:55 +10001038 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +11001039 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +11001041 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +11001042 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001043 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +10001044 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +11001045 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047 error = 0;
1048
1049out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001050 kmem_free(tmpbuffer);
Dave Chinner517c2222013-04-24 18:58:55 +10001051 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054/*
1055 * Convert from using a single leaf to a root node and a leaf.
1056 */
1057int
Dave Chinner517c2222013-04-24 18:58:55 +10001058xfs_attr3_leaf_to_node(
1059 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Dave Chinner517c2222013-04-24 18:58:55 +10001061 struct xfs_attr_leafblock *leaf;
1062 struct xfs_attr3_icleaf_hdr icleafhdr;
1063 struct xfs_attr_leaf_entry *entries;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001064 struct xfs_da_node_entry *btree;
Dave Chinner517c2222013-04-24 18:58:55 +10001065 struct xfs_da3_icnode_hdr icnodehdr;
1066 struct xfs_da_intnode *node;
1067 struct xfs_inode *dp = args->dp;
1068 struct xfs_mount *mp = dp->i_mount;
1069 struct xfs_buf *bp1 = NULL;
1070 struct xfs_buf *bp2 = NULL;
1071 xfs_dablk_t blkno;
1072 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Dave Chinner5a5881c2012-03-22 05:15:13 +00001074 trace_xfs_attr_leaf_to_node(args);
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 error = xfs_da_grow_inode(args, &blkno);
1077 if (error)
1078 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001079 error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (error)
1081 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +11001082
Dave Chinner517c2222013-04-24 18:58:55 +10001083 error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (error)
1085 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001086
1087 /* copy leaf to new buffer, update identifiers */
Dave Chinner61fe1352013-04-03 16:11:30 +11001088 xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
Dave Chinner1813dd62012-11-14 17:54:40 +11001089 bp2->b_ops = bp1->b_ops;
Dave Chinner517c2222013-04-24 18:58:55 +10001090 memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(mp));
1091 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1092 struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1093 hdr3->blkno = cpu_to_be64(bp2->b_bn);
1094 }
1095 xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /*
1098 * Set up the new root node.
1099 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001100 error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (error)
1102 goto out;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001103 node = bp1->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001104 xfs_da3_node_hdr_from_disk(&icnodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001105 btree = xfs_da3_node_tree_p(node);
Dave Chinner517c2222013-04-24 18:58:55 +10001106
1107 leaf = bp2->b_addr;
1108 xfs_attr3_leaf_hdr_from_disk(&icleafhdr, leaf);
1109 entries = xfs_attr3_leaf_entryp(leaf);
1110
1111 /* both on-disk, don't endian-flip twice */
1112 btree[0].hashval = entries[icleafhdr.count - 1].hashval;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001113 btree[0].before = cpu_to_be32(blkno);
Dave Chinner517c2222013-04-24 18:58:55 +10001114 icnodehdr.count = 1;
1115 xfs_da3_node_hdr_to_disk(node, &icnodehdr);
1116 xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 error = 0;
1118out:
Dave Chinner517c2222013-04-24 18:58:55 +10001119 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122
1123/*========================================================================
1124 * Routines used for growing the Btree.
1125 *========================================================================*/
1126
1127/*
1128 * Create the initial contents of a leaf attribute list
1129 * or a leaf in a node attribute list.
1130 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001131STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001132xfs_attr3_leaf_create(
1133 struct xfs_da_args *args,
1134 xfs_dablk_t blkno,
1135 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Dave Chinner517c2222013-04-24 18:58:55 +10001137 struct xfs_attr_leafblock *leaf;
1138 struct xfs_attr3_icleaf_hdr ichdr;
1139 struct xfs_inode *dp = args->dp;
1140 struct xfs_mount *mp = dp->i_mount;
1141 struct xfs_buf *bp;
1142 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Dave Chinner5a5881c2012-03-22 05:15:13 +00001144 trace_xfs_attr_leaf_create(args);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
1147 XFS_ATTR_FORK);
1148 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10001149 return error;
1150 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +11001151 xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001152 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001153 memset(leaf, 0, XFS_LBSIZE(mp));
1154
1155 memset(&ichdr, 0, sizeof(ichdr));
1156 ichdr.firstused = XFS_LBSIZE(mp);
1157
1158 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1159 struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1160
1161 ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1162
1163 hdr3->blkno = cpu_to_be64(bp->b_bn);
1164 hdr3->owner = cpu_to_be64(dp->i_ino);
1165 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
1166
1167 ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1168 } else {
1169 ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1170 ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Dave Chinner517c2222013-04-24 18:58:55 +10001172 ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Dave Chinner517c2222013-04-24 18:58:55 +10001174 xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
1175 xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 *bpp = bp;
Dave Chinner517c2222013-04-24 18:58:55 +10001178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181/*
1182 * Split the leaf node, rebalance, then add the new entry.
1183 */
1184int
Dave Chinner517c2222013-04-24 18:58:55 +10001185xfs_attr3_leaf_split(
1186 struct xfs_da_state *state,
1187 struct xfs_da_state_blk *oldblk,
1188 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 xfs_dablk_t blkno;
1191 int error;
1192
Dave Chinner5a5881c2012-03-22 05:15:13 +00001193 trace_xfs_attr_leaf_split(state->args);
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /*
1196 * Allocate space for a new leaf node.
1197 */
1198 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1199 error = xfs_da_grow_inode(state->args, &blkno);
1200 if (error)
1201 return(error);
Dave Chinner517c2222013-04-24 18:58:55 +10001202 error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (error)
1204 return(error);
1205 newblk->blkno = blkno;
1206 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1207
1208 /*
1209 * Rebalance the entries across the two leaves.
1210 * NOTE: rebalance() currently depends on the 2nd block being empty.
1211 */
Dave Chinner517c2222013-04-24 18:58:55 +10001212 xfs_attr3_leaf_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001213 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (error)
1215 return(error);
1216
1217 /*
1218 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1219 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1220 * "new" attrs info. Will need the "old" info to remove it later.
1221 *
1222 * Insert the "new" entry in the correct block.
1223 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001224 if (state->inleaf) {
1225 trace_xfs_attr_leaf_add_old(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001226 error = xfs_attr3_leaf_add(oldblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001227 } else {
1228 trace_xfs_attr_leaf_add_new(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001229 error = xfs_attr3_leaf_add(newblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /*
1233 * Update last hashval in each block since we added the name.
1234 */
1235 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1236 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1237 return(error);
1238}
1239
1240/*
1241 * Add a name to the leaf attribute list structure.
1242 */
1243int
Dave Chinner517c2222013-04-24 18:58:55 +10001244xfs_attr3_leaf_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001245 struct xfs_buf *bp,
1246 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Dave Chinner517c2222013-04-24 18:58:55 +10001248 struct xfs_attr_leafblock *leaf;
1249 struct xfs_attr3_icleaf_hdr ichdr;
1250 int tablesize;
1251 int entsize;
1252 int sum;
1253 int tmp;
1254 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Dave Chinner5a5881c2012-03-22 05:15:13 +00001256 trace_xfs_attr_leaf_add(args);
1257
Dave Chinner1d9025e2012-06-22 18:50:14 +10001258 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001259 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
1260 ASSERT(args->index >= 0 && args->index <= ichdr.count);
Nathan Scottaa82daa2005-11-02 10:33:33 +11001261 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1263
1264 /*
1265 * Search through freemap for first-fit on new name length.
1266 * (may need to figure in size of entry struct too)
1267 */
Dave Chinner517c2222013-04-24 18:58:55 +10001268 tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1269 + xfs_attr3_leaf_hdr_size(leaf);
1270 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1271 if (tablesize > ichdr.firstused) {
1272 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 continue;
1274 }
Dave Chinner517c2222013-04-24 18:58:55 +10001275 if (!ichdr.freemap[i].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 continue; /* no space in this map */
1277 tmp = entsize;
Dave Chinner517c2222013-04-24 18:58:55 +10001278 if (ichdr.freemap[i].base < ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 tmp += sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001280 if (ichdr.freemap[i].size >= tmp) {
1281 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1282 goto out_log_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Dave Chinner517c2222013-04-24 18:58:55 +10001284 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
1287 /*
1288 * If there are no holes in the address space of the block,
1289 * and we don't have enough freespace, then compaction will do us
1290 * no good and we should just give up.
1291 */
Dave Chinner517c2222013-04-24 18:58:55 +10001292 if (!ichdr.holes && sum < entsize)
1293 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 /*
1296 * Compact the entries to coalesce free space.
1297 * This may change the hdr->count via dropping INCOMPLETE entries.
1298 */
Dave Chinner517c2222013-04-24 18:58:55 +10001299 xfs_attr3_leaf_compact(args, &ichdr, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 /*
1302 * After compaction, the block is guaranteed to have only one
1303 * free region, in freemap[0]. If it is not big enough, give up.
1304 */
Dave Chinner517c2222013-04-24 18:58:55 +10001305 if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
1306 tmp = ENOSPC;
1307 goto out_log_hdr;
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Dave Chinner517c2222013-04-24 18:58:55 +10001310 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1311
1312out_log_hdr:
1313 xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
1314 xfs_trans_log_buf(args->trans, bp,
1315 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1316 xfs_attr3_leaf_hdr_size(leaf)));
1317 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
1320/*
1321 * Add a name to a leaf attribute list structure.
1322 */
1323STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001324xfs_attr3_leaf_add_work(
1325 struct xfs_buf *bp,
1326 struct xfs_attr3_icleaf_hdr *ichdr,
1327 struct xfs_da_args *args,
1328 int mapindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Dave Chinner517c2222013-04-24 18:58:55 +10001330 struct xfs_attr_leafblock *leaf;
1331 struct xfs_attr_leaf_entry *entry;
1332 struct xfs_attr_leaf_name_local *name_loc;
1333 struct xfs_attr_leaf_name_remote *name_rmt;
1334 struct xfs_mount *mp;
1335 int tmp;
1336 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Dave Chinneree732592012-11-12 22:53:53 +11001338 trace_xfs_attr_leaf_add_work(args);
1339
Dave Chinner1d9025e2012-06-22 18:50:14 +10001340 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001341 ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1342 ASSERT(args->index >= 0 && args->index <= ichdr->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 /*
1345 * Force open some space in the entry array and fill it in.
1346 */
Dave Chinner517c2222013-04-24 18:58:55 +10001347 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1348 if (args->index < ichdr->count) {
1349 tmp = ichdr->count - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001351 memmove(entry + 1, entry, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001352 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1354 }
Dave Chinner517c2222013-04-24 18:58:55 +10001355 ichdr->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /*
1358 * Allocate space for the new string (at the end of the run).
1359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 mp = args->trans->t_mountp;
Dave Chinner517c2222013-04-24 18:58:55 +10001361 ASSERT(ichdr->freemap[mapindex].base < XFS_LBSIZE(mp));
1362 ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1363 ASSERT(ichdr->freemap[mapindex].size >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001364 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1365 mp->m_sb.sb_blocksize, NULL));
Dave Chinner517c2222013-04-24 18:58:55 +10001366 ASSERT(ichdr->freemap[mapindex].size < XFS_LBSIZE(mp));
1367 ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1368
1369 ichdr->freemap[mapindex].size -=
1370 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1371 mp->m_sb.sb_blocksize, &tmp);
1372
1373 entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1374 ichdr->freemap[mapindex].size);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001375 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001377 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001378 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 entry->flags |= XFS_ATTR_INCOMPLETE;
1380 if ((args->blkno2 == args->blkno) &&
1381 (args->index2 <= args->index)) {
1382 args->index2++;
1383 }
1384 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001385 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001387 ASSERT((args->index == 0) ||
1388 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Dave Chinner517c2222013-04-24 18:58:55 +10001389 ASSERT((args->index == ichdr->count - 1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001390 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * For "remote" attribute values, simply note that we need to
1394 * allocate space for the "remote" value. We can't actually
1395 * allocate the extents in this transaction, and we can't decide
1396 * which blocks they should be as we might allocate more blocks
1397 * as part of this transaction (a split operation for example).
1398 */
1399 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10001400 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001402 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1404 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001405 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10001407 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 name_rmt->namelen = args->namelen;
1409 memcpy((char *)name_rmt->name, args->name, args->namelen);
1410 entry->flags |= XFS_ATTR_INCOMPLETE;
1411 /* just in case */
1412 name_rmt->valuelen = 0;
1413 name_rmt->valueblk = 0;
1414 args->rmtblkno = 1;
Dave Chinnerad1858d2013-05-21 18:02:08 +10001415 args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001417 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10001418 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 xfs_attr_leaf_entsize(leaf, args->index)));
1420
1421 /*
1422 * Update the control info for this leaf node
1423 */
Dave Chinner517c2222013-04-24 18:58:55 +10001424 if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1425 ichdr->firstused = be16_to_cpu(entry->nameidx);
1426
1427 ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1428 + xfs_attr3_leaf_hdr_size(leaf));
1429 tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1430 + xfs_attr3_leaf_hdr_size(leaf);
1431
1432 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1433 if (ichdr->freemap[i].base == tmp) {
1434 ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1435 ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437 }
Dave Chinner517c2222013-04-24 18:58:55 +10001438 ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
1442/*
1443 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1444 */
1445STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001446xfs_attr3_leaf_compact(
Dave Chinneree732592012-11-12 22:53:53 +11001447 struct xfs_da_args *args,
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001448 struct xfs_attr3_icleaf_hdr *ichdr_dst,
Dave Chinneree732592012-11-12 22:53:53 +11001449 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001451 struct xfs_attr_leafblock *leaf_src;
1452 struct xfs_attr_leafblock *leaf_dst;
1453 struct xfs_attr3_icleaf_hdr ichdr_src;
Dave Chinneree732592012-11-12 22:53:53 +11001454 struct xfs_trans *trans = args->trans;
1455 struct xfs_mount *mp = trans->t_mountp;
1456 char *tmpbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Dave Chinneree732592012-11-12 22:53:53 +11001458 trace_xfs_attr_leaf_compact(args);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001461 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1462 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001463 leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1464 leaf_dst = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 /*
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001467 * Copy the on-disk header back into the destination buffer to ensure
1468 * all the information in the header that is not part of the incore
1469 * header structure is preserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001471 memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1472
1473 /* Initialise the incore headers */
1474 ichdr_src = *ichdr_dst; /* struct copy */
1475 ichdr_dst->firstused = XFS_LBSIZE(mp);
1476 ichdr_dst->usedbytes = 0;
1477 ichdr_dst->count = 0;
1478 ichdr_dst->holes = 0;
1479 ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1480 ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1481 ichdr_dst->freemap[0].base;
1482
1483
1484 /* write the header back to initialise the underlying buffer */
1485 xfs_attr3_leaf_hdr_to_disk(leaf_dst, ichdr_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 /*
1488 * Copy all entry's in the same (sorted) order,
1489 * but allocate name/value pairs packed and in sequence.
1490 */
Dave Chinnerd4c712b2013-05-21 18:02:06 +10001491 xfs_attr3_leaf_moveents(leaf_src, &ichdr_src, 0, leaf_dst, ichdr_dst, 0,
1492 ichdr_src.count, mp);
Dave Chinner517c2222013-04-24 18:58:55 +10001493 /*
1494 * this logs the entire buffer, but the caller must write the header
1495 * back to the buffer when it is finished modifying it.
1496 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001497 xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001499 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502/*
Dave Chinner517c2222013-04-24 18:58:55 +10001503 * Compare two leaf blocks "order".
1504 * Return 0 unless leaf2 should go before leaf1.
1505 */
1506static int
1507xfs_attr3_leaf_order(
1508 struct xfs_buf *leaf1_bp,
1509 struct xfs_attr3_icleaf_hdr *leaf1hdr,
1510 struct xfs_buf *leaf2_bp,
1511 struct xfs_attr3_icleaf_hdr *leaf2hdr)
1512{
1513 struct xfs_attr_leaf_entry *entries1;
1514 struct xfs_attr_leaf_entry *entries2;
1515
1516 entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1517 entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1518 if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1519 ((be32_to_cpu(entries2[0].hashval) <
1520 be32_to_cpu(entries1[0].hashval)) ||
1521 (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1522 be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1523 return 1;
1524 }
1525 return 0;
1526}
1527
1528int
1529xfs_attr_leaf_order(
1530 struct xfs_buf *leaf1_bp,
1531 struct xfs_buf *leaf2_bp)
1532{
1533 struct xfs_attr3_icleaf_hdr ichdr1;
1534 struct xfs_attr3_icleaf_hdr ichdr2;
1535
1536 xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1_bp->b_addr);
1537 xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2_bp->b_addr);
1538 return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1539}
1540
1541/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * Redistribute the attribute list entries between two leaf nodes,
1543 * taking into account the size of the new entry.
1544 *
1545 * NOTE: if new block is empty, then it will get the upper half of the
1546 * old block. At present, all (one) callers pass in an empty second block.
1547 *
1548 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1549 * to match what it is doing in splitting the attribute leaf block. Those
1550 * values are used in "atomic rename" operations on attributes. Note that
1551 * the "new" and "old" values can end up in different blocks.
1552 */
1553STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001554xfs_attr3_leaf_rebalance(
1555 struct xfs_da_state *state,
1556 struct xfs_da_state_blk *blk1,
1557 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Dave Chinner517c2222013-04-24 18:58:55 +10001559 struct xfs_da_args *args;
1560 struct xfs_attr_leafblock *leaf1;
1561 struct xfs_attr_leafblock *leaf2;
1562 struct xfs_attr3_icleaf_hdr ichdr1;
1563 struct xfs_attr3_icleaf_hdr ichdr2;
1564 struct xfs_attr_leaf_entry *entries1;
1565 struct xfs_attr_leaf_entry *entries2;
1566 int count;
1567 int totallen;
1568 int max;
1569 int space;
1570 int swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /*
1573 * Set up environment.
1574 */
1575 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1576 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001577 leaf1 = blk1->bp->b_addr;
1578 leaf2 = blk2->bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001579 xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1);
1580 xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2);
1581 ASSERT(ichdr2.count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 args = state->args;
1583
Dave Chinner5a5881c2012-03-22 05:15:13 +00001584 trace_xfs_attr_leaf_rebalance(args);
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /*
1587 * Check ordering of blocks, reverse if it makes things simpler.
1588 *
1589 * NOTE: Given that all (current) callers pass in an empty
1590 * second block, this code should never set "swap".
1591 */
1592 swap = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001593 if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1594 struct xfs_da_state_blk *tmp_blk;
1595 struct xfs_attr3_icleaf_hdr tmp_ichdr;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 tmp_blk = blk1;
1598 blk1 = blk2;
1599 blk2 = tmp_blk;
Dave Chinner517c2222013-04-24 18:58:55 +10001600
1601 /* struct copies to swap them rather than reconverting */
1602 tmp_ichdr = ichdr1;
1603 ichdr1 = ichdr2;
1604 ichdr2 = tmp_ichdr;
1605
Dave Chinner1d9025e2012-06-22 18:50:14 +10001606 leaf1 = blk1->bp->b_addr;
1607 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 swap = 1;
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 /*
1612 * Examine entries until we reduce the absolute difference in
1613 * byte usage between the two blocks to a minimum. Then get
1614 * the direction to copy and the number of elements to move.
1615 *
1616 * "inleaf" is true if the new entry should be inserted into blk1.
1617 * If "swap" is also true, then reverse the sense of "inleaf".
1618 */
Dave Chinner517c2222013-04-24 18:58:55 +10001619 state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1620 blk2, &ichdr2,
1621 &count, &totallen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (swap)
1623 state->inleaf = !state->inleaf;
1624
1625 /*
1626 * Move any entries required from leaf to leaf:
1627 */
Dave Chinner517c2222013-04-24 18:58:55 +10001628 if (count < ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 /*
1630 * Figure the total bytes to be added to the destination leaf.
1631 */
1632 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001633 count = ichdr1.count - count;
1634 space = ichdr1.usedbytes - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 space += count * sizeof(xfs_attr_leaf_entry_t);
1636
1637 /*
1638 * leaf2 is the destination, compact it if it looks tight.
1639 */
Dave Chinner517c2222013-04-24 18:58:55 +10001640 max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1641 max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001642 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001643 xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 /*
1646 * Move high entries from leaf1 to low end of leaf2.
1647 */
Dave Chinner517c2222013-04-24 18:58:55 +10001648 xfs_attr3_leaf_moveents(leaf1, &ichdr1, ichdr1.count - count,
1649 leaf2, &ichdr2, 0, count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Dave Chinner517c2222013-04-24 18:58:55 +10001651 } else if (count > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /*
1653 * I assert that since all callers pass in an empty
1654 * second buffer, this code should never execute.
1655 */
Dave Chinner07428d72012-11-12 22:09:44 +11001656 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /*
1659 * Figure the total bytes to be added to the destination leaf.
1660 */
1661 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001662 count -= ichdr1.count;
1663 space = totallen - ichdr1.usedbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 space += count * sizeof(xfs_attr_leaf_entry_t);
1665
1666 /*
1667 * leaf1 is the destination, compact it if it looks tight.
1668 */
Dave Chinner517c2222013-04-24 18:58:55 +10001669 max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1670 max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001671 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001672 xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /*
1675 * Move low entries from leaf2 to high end of leaf1.
1676 */
Dave Chinner517c2222013-04-24 18:58:55 +10001677 xfs_attr3_leaf_moveents(leaf2, &ichdr2, 0, leaf1, &ichdr1,
1678 ichdr1.count, count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
Dave Chinner517c2222013-04-24 18:58:55 +10001681 xfs_attr3_leaf_hdr_to_disk(leaf1, &ichdr1);
1682 xfs_attr3_leaf_hdr_to_disk(leaf2, &ichdr2);
1683 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1684 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /*
1687 * Copy out last hashval in each block for B-tree code.
1688 */
Dave Chinner517c2222013-04-24 18:58:55 +10001689 entries1 = xfs_attr3_leaf_entryp(leaf1);
1690 entries2 = xfs_attr3_leaf_entryp(leaf2);
1691 blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1692 blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 /*
1695 * Adjust the expected index for insertion.
1696 * NOTE: this code depends on the (current) situation that the
1697 * second block was originally empty.
1698 *
1699 * If the insertion point moved to the 2nd block, we must adjust
1700 * the index. We must also track the entry just following the
1701 * new entry for use in an "atomic rename" operation, that entry
1702 * is always the "old" entry and the "new" entry is what we are
1703 * inserting. The index/blkno fields refer to the "old" entry,
1704 * while the index2/blkno2 fields refer to the "new" entry.
1705 */
Dave Chinner517c2222013-04-24 18:58:55 +10001706 if (blk1->index > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 ASSERT(state->inleaf == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10001708 blk2->index = blk1->index - ichdr1.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 args->index = args->index2 = blk2->index;
1710 args->blkno = args->blkno2 = blk2->blkno;
Dave Chinner517c2222013-04-24 18:58:55 +10001711 } else if (blk1->index == ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (state->inleaf) {
1713 args->index = blk1->index;
1714 args->blkno = blk1->blkno;
1715 args->index2 = 0;
1716 args->blkno2 = blk2->blkno;
1717 } else {
Dave Chinner07428d72012-11-12 22:09:44 +11001718 /*
1719 * On a double leaf split, the original attr location
1720 * is already stored in blkno2/index2, so don't
1721 * overwrite it overwise we corrupt the tree.
1722 */
Dave Chinner517c2222013-04-24 18:58:55 +10001723 blk2->index = blk1->index - ichdr1.count;
Dave Chinner07428d72012-11-12 22:09:44 +11001724 args->index = blk2->index;
1725 args->blkno = blk2->blkno;
1726 if (!state->extravalid) {
1727 /*
1728 * set the new attr location to match the old
1729 * one and let the higher level split code
1730 * decide where in the leaf to place it.
1731 */
1732 args->index2 = blk2->index;
1733 args->blkno2 = blk2->blkno;
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736 } else {
1737 ASSERT(state->inleaf == 1);
1738 args->index = args->index2 = blk1->index;
1739 args->blkno = args->blkno2 = blk1->blkno;
1740 }
1741}
1742
1743/*
1744 * Examine entries until we reduce the absolute difference in
1745 * byte usage between the two blocks to a minimum.
1746 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1747 * GROT: there will always be enough room in either block for a new entry.
1748 * GROT: Do a double-split for this case?
1749 */
1750STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001751xfs_attr3_leaf_figure_balance(
1752 struct xfs_da_state *state,
1753 struct xfs_da_state_blk *blk1,
1754 struct xfs_attr3_icleaf_hdr *ichdr1,
1755 struct xfs_da_state_blk *blk2,
1756 struct xfs_attr3_icleaf_hdr *ichdr2,
1757 int *countarg,
1758 int *usedbytesarg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Dave Chinner517c2222013-04-24 18:58:55 +10001760 struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
1761 struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
1762 struct xfs_attr_leaf_entry *entry;
1763 int count;
1764 int max;
1765 int index;
1766 int totallen = 0;
1767 int half;
1768 int lastdelta;
1769 int foundit = 0;
1770 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /*
1773 * Examine entries until we reduce the absolute difference in
1774 * byte usage between the two blocks to a minimum.
1775 */
Dave Chinner517c2222013-04-24 18:58:55 +10001776 max = ichdr1->count + ichdr2->count;
1777 half = (max + 1) * sizeof(*entry);
1778 half += ichdr1->usedbytes + ichdr2->usedbytes +
1779 xfs_attr_leaf_newentsize(state->args->namelen,
1780 state->args->valuelen,
1781 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 half /= 2;
1783 lastdelta = state->blocksize;
Dave Chinner517c2222013-04-24 18:58:55 +10001784 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 for (count = index = 0; count < max; entry++, index++, count++) {
1786
1787#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1788 /*
1789 * The new entry is in the first block, account for it.
1790 */
1791 if (count == blk1->index) {
1792 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001793 xfs_attr_leaf_newentsize(
1794 state->args->namelen,
1795 state->args->valuelen,
1796 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1798 break;
1799 lastdelta = XFS_ATTR_ABS(half - tmp);
1800 totallen = tmp;
1801 foundit = 1;
1802 }
1803
1804 /*
1805 * Wrap around into the second block if necessary.
1806 */
Dave Chinner517c2222013-04-24 18:58:55 +10001807 if (count == ichdr1->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 leaf1 = leaf2;
Dave Chinner517c2222013-04-24 18:58:55 +10001809 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 index = 0;
1811 }
1812
1813 /*
1814 * Figure out if next leaf entry would be too much.
1815 */
1816 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1817 index);
1818 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1819 break;
1820 lastdelta = XFS_ATTR_ABS(half - tmp);
1821 totallen = tmp;
1822#undef XFS_ATTR_ABS
1823 }
1824
1825 /*
1826 * Calculate the number of usedbytes that will end up in lower block.
1827 * If new entry not in lower block, fix up the count.
1828 */
1829 totallen -= count * sizeof(*entry);
1830 if (foundit) {
1831 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001832 xfs_attr_leaf_newentsize(
1833 state->args->namelen,
1834 state->args->valuelen,
1835 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
1837
1838 *countarg = count;
1839 *usedbytesarg = totallen;
Dave Chinner517c2222013-04-24 18:58:55 +10001840 return foundit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843/*========================================================================
1844 * Routines used for shrinking the Btree.
1845 *========================================================================*/
1846
1847/*
1848 * Check a leaf block and its neighbors to see if the block should be
1849 * collapsed into one or the other neighbor. Always keep the block
1850 * with the smaller block number.
1851 * If the current block is over 50% full, don't try to join it, return 0.
1852 * If the block is empty, fill in the state structure and return 2.
1853 * If it can be collapsed, fill in the state structure and return 1.
1854 * If nothing can be done, return 0.
1855 *
1856 * GROT: allow for INCOMPLETE entries in calculation.
1857 */
1858int
Dave Chinner517c2222013-04-24 18:58:55 +10001859xfs_attr3_leaf_toosmall(
1860 struct xfs_da_state *state,
1861 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Dave Chinner517c2222013-04-24 18:58:55 +10001863 struct xfs_attr_leafblock *leaf;
1864 struct xfs_da_state_blk *blk;
1865 struct xfs_attr3_icleaf_hdr ichdr;
1866 struct xfs_buf *bp;
1867 xfs_dablk_t blkno;
1868 int bytes;
1869 int forward;
1870 int error;
1871 int retval;
1872 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Dave Chinneree732592012-11-12 22:53:53 +11001874 trace_xfs_attr_leaf_toosmall(state->args);
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /*
1877 * Check for the degenerate case of the block being over 50% full.
1878 * If so, it's not worth even looking to see if we might be able
1879 * to coalesce with a sibling.
1880 */
1881 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner517c2222013-04-24 18:58:55 +10001882 leaf = blk->bp->b_addr;
1883 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
1884 bytes = xfs_attr3_leaf_hdr_size(leaf) +
1885 ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
1886 ichdr.usedbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (bytes > (state->blocksize >> 1)) {
1888 *action = 0; /* blk over 50%, don't try to join */
1889 return(0);
1890 }
1891
1892 /*
1893 * Check for the degenerate case of the block being empty.
1894 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001895 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 * to merge with the forward block unless it is NULL.
1897 */
Dave Chinner517c2222013-04-24 18:58:55 +10001898 if (ichdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /*
1900 * Make altpath point to the block we want to keep and
1901 * path point to the block we want to drop (this one).
1902 */
Dave Chinner517c2222013-04-24 18:58:55 +10001903 forward = (ichdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001905 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 0, &retval);
1907 if (error)
1908 return(error);
1909 if (retval) {
1910 *action = 0;
1911 } else {
1912 *action = 2;
1913 }
Dave Chinner517c2222013-04-24 18:58:55 +10001914 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
1917 /*
1918 * Examine each sibling block to see if we can coalesce with
1919 * at least 25% free space to spare. We need to figure out
1920 * whether to merge with the forward or the backward block.
1921 * We prefer coalescing with the lower numbered sibling so as
1922 * to shrink an attribute list over time.
1923 */
1924 /* start with smaller blk num */
Dave Chinner517c2222013-04-24 18:58:55 +10001925 forward = ichdr.forw < ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 for (i = 0; i < 2; forward = !forward, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10001927 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (forward)
Dave Chinner517c2222013-04-24 18:58:55 +10001929 blkno = ichdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 else
Dave Chinner517c2222013-04-24 18:58:55 +10001931 blkno = ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (blkno == 0)
1933 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10001934 error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
Dave Chinnerad14c332012-11-12 22:54:16 +11001935 blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (error)
1937 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Dave Chinner517c2222013-04-24 18:58:55 +10001939 xfs_attr3_leaf_hdr_from_disk(&ichdr2, bp->b_addr);
1940
1941 bytes = state->blocksize - (state->blocksize >> 2) -
1942 ichdr.usedbytes - ichdr2.usedbytes -
1943 ((ichdr.count + ichdr2.count) *
1944 sizeof(xfs_attr_leaf_entry_t)) -
1945 xfs_attr3_leaf_hdr_size(leaf);
1946
Dave Chinner1d9025e2012-06-22 18:50:14 +10001947 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (bytes >= 0)
1949 break; /* fits with at least 25% to spare */
1950 }
1951 if (i >= 2) {
1952 *action = 0;
1953 return(0);
1954 }
1955
1956 /*
1957 * Make altpath point to the block we want to keep (the lower
1958 * numbered block) and path point to the block we want to drop.
1959 */
1960 memcpy(&state->altpath, &state->path, sizeof(state->path));
1961 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001962 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 0, &retval);
1964 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001965 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 0, &retval);
1967 }
1968 if (error)
1969 return(error);
1970 if (retval) {
1971 *action = 0;
1972 } else {
1973 *action = 1;
1974 }
1975 return(0);
1976}
1977
1978/*
1979 * Remove a name from the leaf attribute list structure.
1980 *
1981 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1982 * If two leaves are 37% full, when combined they will leave 25% free.
1983 */
1984int
Dave Chinner517c2222013-04-24 18:58:55 +10001985xfs_attr3_leaf_remove(
1986 struct xfs_buf *bp,
1987 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Dave Chinner517c2222013-04-24 18:58:55 +10001989 struct xfs_attr_leafblock *leaf;
1990 struct xfs_attr3_icleaf_hdr ichdr;
1991 struct xfs_attr_leaf_entry *entry;
1992 struct xfs_mount *mp = args->trans->t_mountp;
1993 int before;
1994 int after;
1995 int smallest;
1996 int entsize;
1997 int tablesize;
1998 int tmp;
1999 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Dave Chinneree732592012-11-12 22:53:53 +11002001 trace_xfs_attr_leaf_remove(args);
2002
Dave Chinner1d9025e2012-06-22 18:50:14 +10002003 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002004 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2005
2006 ASSERT(ichdr.count > 0 && ichdr.count < XFS_LBSIZE(mp) / 8);
2007 ASSERT(args->index >= 0 && args->index < ichdr.count);
2008 ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
2009 xfs_attr3_leaf_hdr_size(leaf));
2010
2011 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2012
2013 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002014 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 /*
2017 * Scan through free region table:
2018 * check for adjacency of free'd entry with an existing one,
2019 * find smallest free region in case we need to replace it,
2020 * adjust any map that borders the entry table,
2021 */
Dave Chinner517c2222013-04-24 18:58:55 +10002022 tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
2023 + xfs_attr3_leaf_hdr_size(leaf);
2024 tmp = ichdr.freemap[0].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 before = after = -1;
2026 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2027 entsize = xfs_attr_leaf_entsize(leaf, args->index);
Dave Chinner517c2222013-04-24 18:58:55 +10002028 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
2029 ASSERT(ichdr.freemap[i].base < XFS_LBSIZE(mp));
2030 ASSERT(ichdr.freemap[i].size < XFS_LBSIZE(mp));
2031 if (ichdr.freemap[i].base == tablesize) {
2032 ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2033 ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
Dave Chinner517c2222013-04-24 18:58:55 +10002036 if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2037 be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 before = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002039 } else if (ichdr.freemap[i].base ==
2040 (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 after = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002042 } else if (ichdr.freemap[i].size < tmp) {
2043 tmp = ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 smallest = i;
2045 }
2046 }
2047
2048 /*
2049 * Coalesce adjacent freemap regions,
2050 * or replace the smallest region.
2051 */
2052 if ((before >= 0) || (after >= 0)) {
2053 if ((before >= 0) && (after >= 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10002054 ichdr.freemap[before].size += entsize;
2055 ichdr.freemap[before].size += ichdr.freemap[after].size;
2056 ichdr.freemap[after].base = 0;
2057 ichdr.freemap[after].size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 } else if (before >= 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002059 ichdr.freemap[before].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002061 ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2062 ichdr.freemap[after].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064 } else {
2065 /*
2066 * Replace smallest region (if it is smaller than free'd entry)
2067 */
Dave Chinner517c2222013-04-24 18:58:55 +10002068 if (ichdr.freemap[smallest].size < entsize) {
2069 ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2070 ichdr.freemap[smallest].size = entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 }
2073
2074 /*
2075 * Did we remove the first entry?
2076 */
Dave Chinner517c2222013-04-24 18:58:55 +10002077 if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 smallest = 1;
2079 else
2080 smallest = 0;
2081
2082 /*
2083 * Compress the remaining entries and zero out the removed stuff.
2084 */
Dave Chinner517c2222013-04-24 18:58:55 +10002085 memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2086 ichdr.usedbytes -= entsize;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002087 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002088 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 entsize));
2090
Dave Chinner517c2222013-04-24 18:58:55 +10002091 tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2092 memmove(entry, entry + 1, tmp);
2093 ichdr.count--;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002094 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002095 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2096
2097 entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2098 memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 /*
2101 * If we removed the first entry, re-find the first used byte
2102 * in the name area. Note that if the entry was the "firstused",
2103 * then we don't have a "hole" in our block resulting from
2104 * removing the name.
2105 */
2106 if (smallest) {
2107 tmp = XFS_LBSIZE(mp);
Dave Chinner517c2222013-04-24 18:58:55 +10002108 entry = xfs_attr3_leaf_entryp(leaf);
2109 for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2110 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002111 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
2112
2113 if (be16_to_cpu(entry->nameidx) < tmp)
2114 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Dave Chinner517c2222013-04-24 18:58:55 +10002116 ichdr.firstused = tmp;
2117 if (!ichdr.firstused)
2118 ichdr.firstused = tmp - XFS_ATTR_LEAF_NAME_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002120 ichdr.holes = 1; /* mark as needing compaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Dave Chinner517c2222013-04-24 18:58:55 +10002122 xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002123 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002124 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2125 xfs_attr3_leaf_hdr_size(leaf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /*
2128 * Check if leaf is less than 50% full, caller may want to
2129 * "join" the leaf with a sibling if so.
2130 */
Dave Chinner517c2222013-04-24 18:58:55 +10002131 tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2132 ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2133
2134 return tmp < mp->m_attr_magicpct; /* leaf is < 37% full */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137/*
2138 * Move all the attribute list entries from drop_leaf into save_leaf.
2139 */
2140void
Dave Chinner517c2222013-04-24 18:58:55 +10002141xfs_attr3_leaf_unbalance(
2142 struct xfs_da_state *state,
2143 struct xfs_da_state_blk *drop_blk,
2144 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Dave Chinner517c2222013-04-24 18:58:55 +10002146 struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2147 struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2148 struct xfs_attr3_icleaf_hdr drophdr;
2149 struct xfs_attr3_icleaf_hdr savehdr;
2150 struct xfs_attr_leaf_entry *entry;
2151 struct xfs_mount *mp = state->mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Dave Chinner5a5881c2012-03-22 05:15:13 +00002153 trace_xfs_attr_leaf_unbalance(state->args);
2154
Dave Chinner1d9025e2012-06-22 18:50:14 +10002155 drop_leaf = drop_blk->bp->b_addr;
2156 save_leaf = save_blk->bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002157 xfs_attr3_leaf_hdr_from_disk(&drophdr, drop_leaf);
2158 xfs_attr3_leaf_hdr_from_disk(&savehdr, save_leaf);
2159 entry = xfs_attr3_leaf_entryp(drop_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 /*
2162 * Save last hashval from dying block for later Btree fixup.
2163 */
Dave Chinner517c2222013-04-24 18:58:55 +10002164 drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 /*
2167 * Check if we need a temp buffer, or can we do it in place.
2168 * Note that we don't check "leaf" for holes because we will
2169 * always be dropping it, toosmall() decided that for us already.
2170 */
Dave Chinner517c2222013-04-24 18:58:55 +10002171 if (savehdr.holes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /*
2173 * dest leaf has no holes, so we add there. May need
2174 * to make some room in the entry array.
2175 */
Dave Chinner517c2222013-04-24 18:58:55 +10002176 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2177 drop_blk->bp, &drophdr)) {
2178 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2179 save_leaf, &savehdr, 0,
2180 drophdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002182 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2183 save_leaf, &savehdr,
2184 savehdr.count, drophdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186 } else {
2187 /*
2188 * Destination has holes, so we make a temporary copy
2189 * of the leaf and add them both to that.
2190 */
Dave Chinner517c2222013-04-24 18:58:55 +10002191 struct xfs_attr_leafblock *tmp_leaf;
2192 struct xfs_attr3_icleaf_hdr tmphdr;
2193
Dave Chinner8517de22013-05-21 18:02:05 +10002194 tmp_leaf = kmem_zalloc(state->blocksize, KM_SLEEP);
Dave Chinner517c2222013-04-24 18:58:55 +10002195
Dave Chinner8517de22013-05-21 18:02:05 +10002196 /*
2197 * Copy the header into the temp leaf so that all the stuff
2198 * not in the incore header is present and gets copied back in
2199 * once we've moved all the entries.
2200 */
2201 memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2202
2203 memset(&tmphdr, 0, sizeof(tmphdr));
Dave Chinner517c2222013-04-24 18:58:55 +10002204 tmphdr.magic = savehdr.magic;
2205 tmphdr.forw = savehdr.forw;
2206 tmphdr.back = savehdr.back;
2207 tmphdr.firstused = state->blocksize;
Dave Chinner8517de22013-05-21 18:02:05 +10002208
2209 /* write the header to the temp buffer to initialise it */
2210 xfs_attr3_leaf_hdr_to_disk(tmp_leaf, &tmphdr);
2211
Dave Chinner517c2222013-04-24 18:58:55 +10002212 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2213 drop_blk->bp, &drophdr)) {
2214 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2215 tmp_leaf, &tmphdr, 0,
2216 drophdr.count, mp);
2217 xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0,
2218 tmp_leaf, &tmphdr, tmphdr.count,
2219 savehdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002221 xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0,
2222 tmp_leaf, &tmphdr, 0,
2223 savehdr.count, mp);
2224 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2225 tmp_leaf, &tmphdr, tmphdr.count,
2226 drophdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
Dave Chinner517c2222013-04-24 18:58:55 +10002228 memcpy(save_leaf, tmp_leaf, state->blocksize);
2229 savehdr = tmphdr; /* struct copy */
2230 kmem_free(tmp_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232
Dave Chinner517c2222013-04-24 18:58:55 +10002233 xfs_attr3_leaf_hdr_to_disk(save_leaf, &savehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002234 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 state->blocksize - 1);
2236
2237 /*
2238 * Copy out last hashval in each block for B-tree code.
2239 */
Dave Chinner517c2222013-04-24 18:58:55 +10002240 entry = xfs_attr3_leaf_entryp(save_leaf);
2241 save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
2244/*========================================================================
2245 * Routines used for finding things in the Btree.
2246 *========================================================================*/
2247
2248/*
2249 * Look up a name in a leaf attribute list structure.
2250 * This is the internal routine, it uses the caller's buffer.
2251 *
2252 * Note that duplicate keys are allowed, but only check within the
2253 * current leaf node. The Btree code must check in adjacent leaf nodes.
2254 *
2255 * Return in args->index the index into the entry[] array of either
2256 * the found entry, or where the entry should have been (insert before
2257 * that entry).
2258 *
2259 * Don't change the args->value unless we find the attribute.
2260 */
2261int
Dave Chinner517c2222013-04-24 18:58:55 +10002262xfs_attr3_leaf_lookup_int(
2263 struct xfs_buf *bp,
2264 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Dave Chinner517c2222013-04-24 18:58:55 +10002266 struct xfs_attr_leafblock *leaf;
2267 struct xfs_attr3_icleaf_hdr ichdr;
2268 struct xfs_attr_leaf_entry *entry;
2269 struct xfs_attr_leaf_entry *entries;
2270 struct xfs_attr_leaf_name_local *name_loc;
2271 struct xfs_attr_leaf_name_remote *name_rmt;
2272 xfs_dahash_t hashval;
2273 int probe;
2274 int span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Dave Chinner5a5881c2012-03-22 05:15:13 +00002276 trace_xfs_attr_leaf_lookup(args);
2277
Dave Chinner1d9025e2012-06-22 18:50:14 +10002278 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002279 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2280 entries = xfs_attr3_leaf_entryp(leaf);
2281 ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 /*
2284 * Binary search. (note: small blocks will skip this loop)
2285 */
2286 hashval = args->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002287 probe = span = ichdr.count / 2;
2288 for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002290 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002292 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 probe -= span;
2294 else
2295 break;
2296 }
Dave Chinner517c2222013-04-24 18:58:55 +10002297 ASSERT(probe >= 0 && (!ichdr.count || probe < ichdr.count));
2298 ASSERT(span <= 4 || be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 /*
2301 * Since we may have duplicate hashval's, find the first matching
2302 * hashval in the leaf.
2303 */
Dave Chinner517c2222013-04-24 18:58:55 +10002304 while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 entry--;
2306 probe--;
2307 }
Dave Chinner517c2222013-04-24 18:58:55 +10002308 while (probe < ichdr.count &&
2309 be32_to_cpu(entry->hashval) < hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 entry++;
2311 probe++;
2312 }
Dave Chinner517c2222013-04-24 18:58:55 +10002313 if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 args->index = probe;
Dave Chinner517c2222013-04-24 18:58:55 +10002315 return XFS_ERROR(ENOATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317
2318 /*
2319 * Duplicate keys may be present, so search all of them for a match.
2320 */
Dave Chinner517c2222013-04-24 18:58:55 +10002321 for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 entry++, probe++) {
2323/*
2324 * GROT: Add code to remove incomplete entries.
2325 */
2326 /*
2327 * If we are looking for INCOMPLETE entries, show only those.
2328 * If we are looking for complete entries, show only those.
2329 */
2330 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2331 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2332 continue;
2333 }
2334 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002335 name_loc = xfs_attr3_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (name_loc->namelen != args->namelen)
2337 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002338 if (memcmp(args->name, name_loc->nameval,
2339 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002341 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 continue;
2343 args->index = probe;
Dave Chinner517c2222013-04-24 18:58:55 +10002344 return XFS_ERROR(EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002346 name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (name_rmt->namelen != args->namelen)
2348 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002349 if (memcmp(args->name, name_rmt->name,
2350 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002352 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 continue;
2354 args->index = probe;
Dave Chinnere461fcb2013-05-20 09:51:16 +10002355 args->valuelen = be32_to_cpu(name_rmt->valuelen);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002356 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Dave Chinnerad1858d2013-05-21 18:02:08 +10002357 args->rmtblkcnt = xfs_attr3_rmt_blocks(
2358 args->dp->i_mount,
2359 args->valuelen);
Dave Chinner517c2222013-04-24 18:58:55 +10002360 return XFS_ERROR(EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362 }
2363 args->index = probe;
Dave Chinner517c2222013-04-24 18:58:55 +10002364 return XFS_ERROR(ENOATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
2367/*
2368 * Get the value associated with an attribute name from a leaf attribute
2369 * list structure.
2370 */
2371int
Dave Chinner517c2222013-04-24 18:58:55 +10002372xfs_attr3_leaf_getvalue(
2373 struct xfs_buf *bp,
2374 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Dave Chinner517c2222013-04-24 18:58:55 +10002376 struct xfs_attr_leafblock *leaf;
2377 struct xfs_attr3_icleaf_hdr ichdr;
2378 struct xfs_attr_leaf_entry *entry;
2379 struct xfs_attr_leaf_name_local *name_loc;
2380 struct xfs_attr_leaf_name_remote *name_rmt;
2381 int valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Dave Chinner1d9025e2012-06-22 18:50:14 +10002383 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002384 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2385 ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8);
2386 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Dave Chinner517c2222013-04-24 18:58:55 +10002388 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002390 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 ASSERT(name_loc->namelen == args->namelen);
2392 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002393 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (args->flags & ATTR_KERNOVAL) {
2395 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 if (args->valuelen < valuelen) {
2399 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002400 return XFS_ERROR(ERANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 args->valuelen = valuelen;
2403 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2404 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002405 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 ASSERT(name_rmt->namelen == args->namelen);
2407 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002408 valuelen = be32_to_cpu(name_rmt->valuelen);
2409 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Dave Chinnerad1858d2013-05-21 18:02:08 +10002410 args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
2411 valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (args->flags & ATTR_KERNOVAL) {
2413 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416 if (args->valuelen < valuelen) {
2417 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002418 return XFS_ERROR(ERANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420 args->valuelen = valuelen;
2421 }
Dave Chinner517c2222013-04-24 18:58:55 +10002422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
2425/*========================================================================
2426 * Utility routines.
2427 *========================================================================*/
2428
2429/*
2430 * Move the indicated entries from one leaf to another.
2431 * NOTE: this routine modifies both source and destination leaves.
2432 */
2433/*ARGSUSED*/
2434STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10002435xfs_attr3_leaf_moveents(
2436 struct xfs_attr_leafblock *leaf_s,
2437 struct xfs_attr3_icleaf_hdr *ichdr_s,
2438 int start_s,
2439 struct xfs_attr_leafblock *leaf_d,
2440 struct xfs_attr3_icleaf_hdr *ichdr_d,
2441 int start_d,
2442 int count,
2443 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Dave Chinner517c2222013-04-24 18:58:55 +10002445 struct xfs_attr_leaf_entry *entry_s;
2446 struct xfs_attr_leaf_entry *entry_d;
2447 int desti;
2448 int tmp;
2449 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 /*
2452 * Check for nothing to do.
2453 */
2454 if (count == 0)
2455 return;
2456
2457 /*
2458 * Set up environment.
2459 */
Dave Chinner517c2222013-04-24 18:58:55 +10002460 ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2461 ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2462 ASSERT(ichdr_s->magic == ichdr_d->magic);
2463 ASSERT(ichdr_s->count > 0 && ichdr_s->count < XFS_LBSIZE(mp) / 8);
2464 ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2465 + xfs_attr3_leaf_hdr_size(leaf_s));
2466 ASSERT(ichdr_d->count < XFS_LBSIZE(mp) / 8);
2467 ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2468 + xfs_attr3_leaf_hdr_size(leaf_d));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Dave Chinner517c2222013-04-24 18:58:55 +10002470 ASSERT(start_s < ichdr_s->count);
2471 ASSERT(start_d <= ichdr_d->count);
2472 ASSERT(count <= ichdr_s->count);
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 /*
2476 * Move the entries in the destination leaf up to make a hole?
2477 */
Dave Chinner517c2222013-04-24 18:58:55 +10002478 if (start_d < ichdr_d->count) {
2479 tmp = ichdr_d->count - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002481 entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2482 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2483 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485
2486 /*
2487 * Copy all entry's in the same (sorted) order,
2488 * but allocate attribute info packed and in sequence.
2489 */
Dave Chinner517c2222013-04-24 18:58:55 +10002490 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2491 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 desti = start_d;
2493 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10002494 ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2496#ifdef GROT
2497 /*
2498 * Code to drop INCOMPLETE entries. Difficult to use as we
2499 * may also need to change the insertion index. Code turned
2500 * off for 6.2, should be revisited later.
2501 */
2502 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Dave Chinner517c2222013-04-24 18:58:55 +10002503 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2504 ichdr_s->usedbytes -= tmp;
2505 ichdr_s->count -= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 entry_d--; /* to compensate for ++ in loop hdr */
2507 desti--;
2508 if ((start_s + i) < offset)
2509 result++; /* insertion index adjustment */
2510 } else {
2511#endif /* GROT */
Dave Chinner517c2222013-04-24 18:58:55 +10002512 ichdr_d->firstused -= tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 /* both on-disk, don't endian flip twice */
2514 entry_d->hashval = entry_s->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002515 entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002517 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 <= XFS_LBSIZE(mp));
Dave Chinner517c2222013-04-24 18:58:55 +10002519 memmove(xfs_attr3_leaf_name(leaf_d, desti),
2520 xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002521 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 <= XFS_LBSIZE(mp));
Dave Chinner517c2222013-04-24 18:58:55 +10002523 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2524 ichdr_s->usedbytes -= tmp;
2525 ichdr_d->usedbytes += tmp;
2526 ichdr_s->count -= 1;
2527 ichdr_d->count += 1;
2528 tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2529 + xfs_attr3_leaf_hdr_size(leaf_d);
2530 ASSERT(ichdr_d->firstused >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531#ifdef GROT
2532 }
2533#endif /* GROT */
2534 }
2535
2536 /*
2537 * Zero out the entries we just copied.
2538 */
Dave Chinner517c2222013-04-24 18:58:55 +10002539 if (start_s == ichdr_s->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002541 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 ASSERT(((char *)entry_s + tmp) <=
2543 ((char *)leaf_s + XFS_LBSIZE(mp)));
Dave Chinner517c2222013-04-24 18:58:55 +10002544 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 } else {
2546 /*
2547 * Move the remaining entries down to fill the hole,
2548 * then zero the entries at the top.
2549 */
Dave Chinner517c2222013-04-24 18:58:55 +10002550 tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2551 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2552 entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2553 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002556 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 ASSERT(((char *)entry_s + tmp) <=
2558 ((char *)leaf_s + XFS_LBSIZE(mp)));
Dave Chinner517c2222013-04-24 18:58:55 +10002559 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561
2562 /*
2563 * Fill in the freemap information
2564 */
Dave Chinner517c2222013-04-24 18:58:55 +10002565 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2566 ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2567 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2568 ichdr_d->freemap[1].base = 0;
2569 ichdr_d->freemap[2].base = 0;
2570 ichdr_d->freemap[1].size = 0;
2571 ichdr_d->freemap[2].size = 0;
2572 ichdr_s->holes = 1; /* leaf may not be compact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
2575/*
2576 * Pick up the last hashvalue from a leaf block.
2577 */
2578xfs_dahash_t
Dave Chinner1d9025e2012-06-22 18:50:14 +10002579xfs_attr_leaf_lasthash(
2580 struct xfs_buf *bp,
2581 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
Dave Chinner517c2222013-04-24 18:58:55 +10002583 struct xfs_attr3_icleaf_hdr ichdr;
2584 struct xfs_attr_leaf_entry *entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Dave Chinner517c2222013-04-24 18:58:55 +10002586 xfs_attr3_leaf_hdr_from_disk(&ichdr, bp->b_addr);
2587 entries = xfs_attr3_leaf_entryp(bp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if (count)
Dave Chinner517c2222013-04-24 18:58:55 +10002589 *count = ichdr.count;
2590 if (!ichdr.count)
2591 return 0;
2592 return be32_to_cpu(entries[ichdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
2595/*
2596 * Calculate the number of bytes used to store the indicated attribute
2597 * (whether local or remote only calculate bytes in this block).
2598 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002599STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2601{
Dave Chinner517c2222013-04-24 18:58:55 +10002602 struct xfs_attr_leaf_entry *entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 xfs_attr_leaf_name_local_t *name_loc;
2604 xfs_attr_leaf_name_remote_t *name_rmt;
2605 int size;
2606
Dave Chinner517c2222013-04-24 18:58:55 +10002607 entries = xfs_attr3_leaf_entryp(leaf);
2608 if (entries[index].flags & XFS_ATTR_LOCAL) {
2609 name_loc = xfs_attr3_leaf_name_local(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002610 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002611 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002613 name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002614 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
Dave Chinner517c2222013-04-24 18:58:55 +10002616 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
2619/*
2620 * Calculate the number of bytes that would be required to store the new
2621 * attribute (whether local or remote only calculate bytes in this block).
2622 * This routine decides as a side effect whether the attribute will be
2623 * a "local" or a "remote" attribute.
2624 */
2625int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002626xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
2628 int size;
2629
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002630 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2631 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (local) {
2633 *local = 1;
2634 }
2635 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002636 size = xfs_attr_leaf_entsize_remote(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (local) {
2638 *local = 0;
2639 }
2640 }
Dave Chinner517c2222013-04-24 18:58:55 +10002641 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
2644/*
2645 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2646 */
2647int
Dave Chinner517c2222013-04-24 18:58:55 +10002648xfs_attr3_leaf_list_int(
2649 struct xfs_buf *bp,
2650 struct xfs_attr_list_context *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
Dave Chinner517c2222013-04-24 18:58:55 +10002652 struct attrlist_cursor_kern *cursor;
2653 struct xfs_attr_leafblock *leaf;
2654 struct xfs_attr3_icleaf_hdr ichdr;
2655 struct xfs_attr_leaf_entry *entries;
2656 struct xfs_attr_leaf_entry *entry;
2657 int retval;
2658 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002660 trace_xfs_attr_list_leaf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Dave Chinner517c2222013-04-24 18:58:55 +10002662 leaf = bp->b_addr;
2663 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2664 entries = xfs_attr3_leaf_entryp(leaf);
2665
2666 cursor = context->cursor;
2667 cursor->initted = 1;
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 /*
2670 * Re-find our place in the leaf block if this is a new syscall.
2671 */
2672 if (context->resynch) {
Dave Chinner517c2222013-04-24 18:58:55 +10002673 entry = &entries[0];
2674 for (i = 0; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002675 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (cursor->offset == context->dupcnt) {
2677 context->dupcnt = 0;
2678 break;
2679 }
2680 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002681 } else if (be32_to_cpu(entry->hashval) >
2682 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 context->dupcnt = 0;
2684 break;
2685 }
2686 }
Dave Chinner517c2222013-04-24 18:58:55 +10002687 if (i == ichdr.count) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002688 trace_xfs_attr_list_notfound(context);
Dave Chinner517c2222013-04-24 18:58:55 +10002689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 }
2691 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002692 entry = &entries[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 i = 0;
2694 }
2695 context->resynch = 0;
2696
2697 /*
2698 * We have found our place, start copying out the new attributes.
2699 */
2700 retval = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10002701 for (; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002702 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2703 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 cursor->offset = 0;
2705 }
2706
2707 if (entry->flags & XFS_ATTR_INCOMPLETE)
2708 continue; /* skip incomplete entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002711 xfs_attr_leaf_name_local_t *name_loc =
Dave Chinner517c2222013-04-24 18:58:55 +10002712 xfs_attr3_leaf_name_local(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002713
2714 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002715 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002716 name_loc->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +10002717 (int)name_loc->namelen,
2718 be16_to_cpu(name_loc->valuelen),
Dave Chinnera9273ca2010-01-20 10:47:48 +11002719 &name_loc->nameval[name_loc->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +10002720 if (retval)
2721 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002723 xfs_attr_leaf_name_remote_t *name_rmt =
Dave Chinner517c2222013-04-24 18:58:55 +10002724 xfs_attr3_leaf_name_remote(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002725
2726 int valuelen = be32_to_cpu(name_rmt->valuelen);
2727
2728 if (context->put_value) {
2729 xfs_da_args_t args;
2730
2731 memset((char *)&args, 0, sizeof(args));
2732 args.dp = context->dp;
2733 args.whichfork = XFS_ATTR_FORK;
2734 args.valuelen = valuelen;
Dave Chinner622d8142010-12-23 11:57:37 +11002735 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
Tim Shimmin726801b2006-09-28 11:01:37 +10002736 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
Dave Chinnerad1858d2013-05-21 18:02:08 +10002737 args.rmtblkcnt = xfs_attr3_rmt_blocks(
2738 args.dp->i_mount, valuelen);
Tim Shimmin726801b2006-09-28 11:01:37 +10002739 retval = xfs_attr_rmtval_get(&args);
2740 if (retval)
2741 return retval;
2742 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002743 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002744 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002745 (int)name_rmt->namelen,
2746 valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002747 args.value);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002748 kmem_free(args.value);
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002749 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002750 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002751 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002752 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002753 (int)name_rmt->namelen,
2754 valuelen,
2755 NULL);
2756 }
2757 if (retval)
2758 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002760 if (context->seen_enough)
2761 break;
2762 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002764 trace_xfs_attr_list_leaf_end(context);
Dave Chinner517c2222013-04-24 18:58:55 +10002765 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
2767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769/*========================================================================
2770 * Manage the INCOMPLETE flag in a leaf entry
2771 *========================================================================*/
2772
2773/*
2774 * Clear the INCOMPLETE flag on an entry in a leaf block.
2775 */
2776int
Dave Chinner517c2222013-04-24 18:58:55 +10002777xfs_attr3_leaf_clearflag(
2778 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Dave Chinner517c2222013-04-24 18:58:55 +10002780 struct xfs_attr_leafblock *leaf;
2781 struct xfs_attr_leaf_entry *entry;
2782 struct xfs_attr_leaf_name_remote *name_rmt;
2783 struct xfs_buf *bp;
2784 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002786 struct xfs_attr3_icleaf_hdr ichdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 xfs_attr_leaf_name_local_t *name_loc;
2788 int namelen;
2789 char *name;
2790#endif /* DEBUG */
2791
Dave Chinner5a5881c2012-03-22 05:15:13 +00002792 trace_xfs_attr_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 /*
2794 * Set up the operation.
2795 */
Dave Chinner517c2222013-04-24 18:58:55 +10002796 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002797 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Dave Chinner1d9025e2012-06-22 18:50:14 +10002800 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002801 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2803
2804#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002805 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2806 ASSERT(args->index < ichdr.count);
2807 ASSERT(args->index >= 0);
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002810 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 namelen = name_loc->namelen;
2812 name = (char *)name_loc->nameval;
2813 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002814 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 namelen = name_rmt->namelen;
2816 name = (char *)name_rmt->name;
2817 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002818 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 ASSERT(namelen == args->namelen);
2820 ASSERT(memcmp(name, args->name, namelen) == 0);
2821#endif /* DEBUG */
2822
2823 entry->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002824 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2826
2827 if (args->rmtblkno) {
2828 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002829 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002830 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2831 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002832 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 /*
2837 * Commit the flag value change and start the next trans in series.
2838 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002839 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
2842/*
2843 * Set the INCOMPLETE flag on an entry in a leaf block.
2844 */
2845int
Dave Chinner517c2222013-04-24 18:58:55 +10002846xfs_attr3_leaf_setflag(
2847 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Dave Chinner517c2222013-04-24 18:58:55 +10002849 struct xfs_attr_leafblock *leaf;
2850 struct xfs_attr_leaf_entry *entry;
2851 struct xfs_attr_leaf_name_remote *name_rmt;
2852 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 int error;
Dave Chinner517c2222013-04-24 18:58:55 +10002854#ifdef DEBUG
2855 struct xfs_attr3_icleaf_hdr ichdr;
2856#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Dave Chinner5a5881c2012-03-22 05:15:13 +00002858 trace_xfs_attr_leaf_setflag(args);
2859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 /*
2861 * Set up the operation.
2862 */
Dave Chinner517c2222013-04-24 18:58:55 +10002863 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002864 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Dave Chinner1d9025e2012-06-22 18:50:14 +10002867 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002868#ifdef DEBUG
2869 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2870 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 ASSERT(args->index >= 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002872#endif
2873 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2876 entry->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002877 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2879 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002880 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 name_rmt->valueblk = 0;
2882 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002883 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
2887 /*
2888 * Commit the flag value change and start the next trans in series.
2889 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002890 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
2893/*
2894 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2895 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2896 * entry given by args->blkno2/index2.
2897 *
2898 * Note that they could be in different blocks, or in the same block.
2899 */
2900int
Dave Chinner517c2222013-04-24 18:58:55 +10002901xfs_attr3_leaf_flipflags(
2902 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Dave Chinner517c2222013-04-24 18:58:55 +10002904 struct xfs_attr_leafblock *leaf1;
2905 struct xfs_attr_leafblock *leaf2;
2906 struct xfs_attr_leaf_entry *entry1;
2907 struct xfs_attr_leaf_entry *entry2;
2908 struct xfs_attr_leaf_name_remote *name_rmt;
2909 struct xfs_buf *bp1;
2910 struct xfs_buf *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 int error;
2912#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002913 struct xfs_attr3_icleaf_hdr ichdr1;
2914 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 xfs_attr_leaf_name_local_t *name_loc;
2916 int namelen1, namelen2;
2917 char *name1, *name2;
2918#endif /* DEBUG */
2919
Dave Chinner5a5881c2012-03-22 05:15:13 +00002920 trace_xfs_attr_leaf_flipflags(args);
2921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 /*
2923 * Read the block containing the "old" attr
2924 */
Dave Chinner517c2222013-04-24 18:58:55 +10002925 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
Dave Chinnerad14c332012-11-12 22:54:16 +11002926 if (error)
2927 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 /*
2930 * Read the block containing the "new" attr, if it is different
2931 */
2932 if (args->blkno2 != args->blkno) {
Dave Chinner517c2222013-04-24 18:58:55 +10002933 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
Dave Chinnerad14c332012-11-12 22:54:16 +11002934 -1, &bp2);
2935 if (error)
2936 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 } else {
2938 bp2 = bp1;
2939 }
2940
Dave Chinner1d9025e2012-06-22 18:50:14 +10002941 leaf1 = bp1->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002942 entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Dave Chinner1d9025e2012-06-22 18:50:14 +10002944 leaf2 = bp2->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002945 entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002948 xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1);
2949 ASSERT(args->index < ichdr1.count);
2950 ASSERT(args->index >= 0);
2951
2952 xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2);
2953 ASSERT(args->index2 < ichdr2.count);
2954 ASSERT(args->index2 >= 0);
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (entry1->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002957 name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 namelen1 = name_loc->namelen;
2959 name1 = (char *)name_loc->nameval;
2960 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002961 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 namelen1 = name_rmt->namelen;
2963 name1 = (char *)name_rmt->name;
2964 }
2965 if (entry2->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002966 name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 namelen2 = name_loc->namelen;
2968 name2 = (char *)name_loc->nameval;
2969 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002970 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 namelen2 = name_rmt->namelen;
2972 name2 = (char *)name_rmt->name;
2973 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002974 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 ASSERT(namelen1 == namelen2);
2976 ASSERT(memcmp(name1, name2, namelen1) == 0);
2977#endif /* DEBUG */
2978
2979 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2980 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2981
2982 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002983 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2985 if (args->rmtblkno) {
2986 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002987 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002988 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2989 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002990 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2992 }
2993
2994 entry2->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002995 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2997 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002998 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 name_rmt->valueblk = 0;
3000 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003001 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
3003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005 /*
3006 * Commit the flag value change and start the next trans in series.
3007 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003008 error = xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Dave Chinner517c2222013-04-24 18:58:55 +10003010 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
3013/*========================================================================
3014 * Indiscriminately delete the entire attribute fork
3015 *========================================================================*/
3016
3017/*
3018 * Recurse (gasp!) through the attribute nodes until we find leaves.
3019 * We're doing a depth-first traversal in order to invalidate everything.
3020 */
3021int
Dave Chinner517c2222013-04-24 18:58:55 +10003022xfs_attr3_root_inactive(
3023 struct xfs_trans **trans,
3024 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Dave Chinner517c2222013-04-24 18:58:55 +10003026 struct xfs_da_blkinfo *info;
3027 struct xfs_buf *bp;
3028 xfs_daddr_t blkno;
3029 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 /*
3032 * Read block 0 to see what we have to work with.
3033 * We only get here if we have extents, since we remove
3034 * the extents in reverse order the extent containing
3035 * block 0 must still be there.
3036 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003037 error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003039 return error;
3040 blkno = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042 /*
3043 * Invalidate the tree, even if the "tree" is only a single leaf block.
3044 * This is a depth-first traversal!
3045 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10003046 info = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003047 switch (info->magic) {
3048 case cpu_to_be16(XFS_DA_NODE_MAGIC):
3049 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
3050 error = xfs_attr3_node_inactive(trans, dp, bp, 1);
3051 break;
3052 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
3053 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
3054 error = xfs_attr3_leaf_inactive(trans, dp, bp);
3055 break;
3056 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003058 xfs_trans_brelse(*trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
3061 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003062 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 /*
3065 * Invalidate the incore copy of the root block.
3066 */
3067 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
3068 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003069 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003070 xfs_trans_binval(*trans, bp); /* remove from cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 /*
3072 * Commit the invalidate and start the next transaction.
3073 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003074 error = xfs_trans_roll(trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Dave Chinner517c2222013-04-24 18:58:55 +10003076 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
3079/*
3080 * Recurse (gasp!) through the attribute nodes until we find leaves.
3081 * We're doing a depth-first traversal in order to invalidate everything.
3082 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003083STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10003084xfs_attr3_node_inactive(
Dave Chinner1d9025e2012-06-22 18:50:14 +10003085 struct xfs_trans **trans,
3086 struct xfs_inode *dp,
3087 struct xfs_buf *bp,
3088 int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
3090 xfs_da_blkinfo_t *info;
3091 xfs_da_intnode_t *node;
3092 xfs_dablk_t child_fsb;
3093 xfs_daddr_t parent_blkno, child_blkno;
Dave Chinner517c2222013-04-24 18:58:55 +10003094 int error, i;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003095 struct xfs_buf *child_bp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003096 struct xfs_da_node_entry *btree;
Dave Chinner517c2222013-04-24 18:58:55 +10003097 struct xfs_da3_icnode_hdr ichdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 /*
3100 * Since this code is recursive (gasp!) we must protect ourselves.
3101 */
3102 if (level > XFS_DA_NODE_MAXDEPTH) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10003103 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Dave Chinner517c2222013-04-24 18:58:55 +10003104 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
3106
Dave Chinner1d9025e2012-06-22 18:50:14 +10003107 node = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003108 xfs_da3_node_hdr_from_disk(&ichdr, node);
3109 parent_blkno = bp->b_bn;
3110 if (!ichdr.count) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10003111 xfs_trans_brelse(*trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003114 btree = xfs_da3_node_tree_p(node);
3115 child_fsb = be32_to_cpu(btree[0].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003116 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 /*
3119 * If this is the node level just above the leaves, simply loop
3120 * over the leaves removing all of them. If this is higher up
3121 * in the tree, recurse downward.
3122 */
Dave Chinner517c2222013-04-24 18:58:55 +10003123 for (i = 0; i < ichdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 /*
3125 * Read the subsidiary block to see what we have to work with.
3126 * Don't do this in a transaction. This is a depth-first
3127 * traversal of the tree so we may deal with many blocks
3128 * before we come back to this one.
3129 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003130 error = xfs_da3_node_read(*trans, dp, child_fsb, -2, &child_bp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11003131 XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (error)
3133 return(error);
3134 if (child_bp) {
3135 /* save for re-read later */
Dave Chinner1d9025e2012-06-22 18:50:14 +10003136 child_blkno = XFS_BUF_ADDR(child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138 /*
3139 * Invalidate the subtree, however we have to.
3140 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10003141 info = child_bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003142 switch (info->magic) {
3143 case cpu_to_be16(XFS_DA_NODE_MAGIC):
3144 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
3145 error = xfs_attr3_node_inactive(trans, dp,
3146 child_bp, level + 1);
3147 break;
3148 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
3149 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
3150 error = xfs_attr3_leaf_inactive(trans, dp,
3151 child_bp);
3152 break;
3153 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003155 xfs_trans_brelse(*trans, child_bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
3158 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003159 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /*
3162 * Remove the subsidiary block from the cache
3163 * and from the log.
3164 */
3165 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
3166 &child_bp, XFS_ATTR_FORK);
3167 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003168 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003169 xfs_trans_binval(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
3171
3172 /*
3173 * If we're not done, re-read the parent to get the next
3174 * child block number.
3175 */
Dave Chinner517c2222013-04-24 18:58:55 +10003176 if (i + 1 < ichdr.count) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003177 error = xfs_da3_node_read(*trans, dp, 0, parent_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11003178 &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003180 return error;
3181 child_fsb = be32_to_cpu(btree[i + 1].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003182 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
3184 /*
3185 * Atomically commit the whole invalidate stuff.
3186 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003187 error = xfs_trans_roll(trans, dp);
3188 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003189 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
3191
Dave Chinner517c2222013-04-24 18:58:55 +10003192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
3195/*
3196 * Invalidate all of the "remote" value regions pointed to by a particular
3197 * leaf block.
3198 * Note that we must release the lock on the buffer so that we are not
3199 * caught holding something that the logging code wants to flush to disk.
3200 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003201STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10003202xfs_attr3_leaf_inactive(
3203 struct xfs_trans **trans,
3204 struct xfs_inode *dp,
3205 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Dave Chinner517c2222013-04-24 18:58:55 +10003207 struct xfs_attr_leafblock *leaf;
3208 struct xfs_attr3_icleaf_hdr ichdr;
3209 struct xfs_attr_leaf_entry *entry;
3210 struct xfs_attr_leaf_name_remote *name_rmt;
3211 struct xfs_attr_inactive_list *list;
3212 struct xfs_attr_inactive_list *lp;
3213 int error;
3214 int count;
3215 int size;
3216 int tmp;
3217 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Dave Chinner1d9025e2012-06-22 18:50:14 +10003219 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003220 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 /*
3223 * Count the number of "remote" value extents.
3224 */
3225 count = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10003226 entry = xfs_attr3_leaf_entryp(leaf);
3227 for (i = 0; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11003228 if (be16_to_cpu(entry->nameidx) &&
3229 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10003230 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 if (name_rmt->valueblk)
3232 count++;
3233 }
3234 }
3235
3236 /*
3237 * If there are no "remote" values, we're done.
3238 */
3239 if (count == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10003240 xfs_trans_brelse(*trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
3243
3244 /*
3245 * Allocate storage for a list of all the "remote" value extents.
3246 */
3247 size = count * sizeof(xfs_attr_inactive_list_t);
Dave Chinner517c2222013-04-24 18:58:55 +10003248 list = kmem_alloc(size, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
3250 /*
3251 * Identify each of the "remote" value extents.
3252 */
3253 lp = list;
Dave Chinner517c2222013-04-24 18:58:55 +10003254 entry = xfs_attr3_leaf_entryp(leaf);
3255 for (i = 0; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11003256 if (be16_to_cpu(entry->nameidx) &&
3257 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10003258 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11003260 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
Dave Chinner59913f12013-06-03 15:28:49 +10003261 lp->valuelen = xfs_attr3_rmt_blocks(dp->i_mount,
Nathan Scottd7929ff2006-03-17 17:29:36 +11003262 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 lp++;
3264 }
3265 }
3266 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10003267 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 /*
3270 * Invalidate each of the "remote" value extents.
3271 */
3272 error = 0;
3273 for (lp = list, i = 0; i < count; i++, lp++) {
Dave Chinner517c2222013-04-24 18:58:55 +10003274 tmp = xfs_attr3_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11003275 lp->valueblk, lp->valuelen);
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (error == 0)
3278 error = tmp; /* save only the 1st errno */
3279 }
3280
Dave Chinner517c2222013-04-24 18:58:55 +10003281 kmem_free(list);
3282 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
3285/*
3286 * Look at all the extents for this logical region,
3287 * invalidate any buffers that are incore/in transactions.
3288 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003289STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10003290xfs_attr3_leaf_freextent(
3291 struct xfs_trans **trans,
3292 struct xfs_inode *dp,
3293 xfs_dablk_t blkno,
3294 int blkcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Dave Chinner517c2222013-04-24 18:58:55 +10003296 struct xfs_bmbt_irec map;
3297 struct xfs_buf *bp;
3298 xfs_dablk_t tblkno;
3299 xfs_daddr_t dblkno;
3300 int tblkcnt;
3301 int dblkcnt;
3302 int nmap;
3303 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
3305 /*
3306 * Roll through the "value", invalidating the attribute value's
3307 * blocks.
3308 */
3309 tblkno = blkno;
3310 tblkcnt = blkcnt;
3311 while (tblkcnt > 0) {
3312 /*
3313 * Try to remember where we decided to put the value.
3314 */
3315 nmap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003316 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
3317 &map, &nmap, XFS_BMAPI_ATTRFORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 if (error) {
3319 return(error);
3320 }
3321 ASSERT(nmap == 1);
3322 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3323
3324 /*
3325 * If it's a hole, these are already unmapped
3326 * so there's nothing to invalidate.
3327 */
3328 if (map.br_startblock != HOLESTARTBLOCK) {
3329
3330 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3331 map.br_startblock);
3332 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3333 map.br_blockcount);
3334 bp = xfs_trans_get_buf(*trans,
3335 dp->i_mount->m_ddev_targp,
Dave Chinnera8acad72012-04-23 15:58:54 +10003336 dblkno, dblkcnt, 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00003337 if (!bp)
3338 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 xfs_trans_binval(*trans, bp);
3340 /*
3341 * Roll to next transaction.
3342 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003343 error = xfs_trans_roll(trans, dp);
3344 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 return (error);
3346 }
3347
3348 tblkno += map.br_blockcount;
3349 tblkcnt -= map.br_blockcount;
3350 }
3351
3352 return(0);
3353}