blob: 92cda03d1c016c321342d0525ad24200b72fbce4 [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)
279 xfs_trans_buf_set_type(tp, *bpp, XFS_BLF_ATTR_LEAF_BUF);
280 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(
934 struct xfs_buf *bp,
935 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 xfs_attr_leafblock_t *leaf;
938 xfs_attr_leaf_entry_t *entry;
939 xfs_attr_leaf_name_local_t *name_loc;
940 int bytes, i;
941
Dave Chinner1d9025e2012-06-22 18:50:14 +1000942 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200943 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 entry = &leaf->entries[0];
946 bytes = sizeof(struct xfs_attr_sf_hdr);
Nathan Scott918ae422006-03-17 17:28:54 +1100947 for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (entry->flags & XFS_ATTR_INCOMPLETE)
949 continue; /* don't copy partial entries */
950 if (!(entry->flags & XFS_ATTR_LOCAL))
951 return(0);
Dave Chinner517c2222013-04-24 18:58:55 +1000952 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
954 return(0);
Nathan Scott053b5752006-03-17 17:29:09 +1100955 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return(0);
957 bytes += sizeof(struct xfs_attr_sf_entry)-1
958 + name_loc->namelen
Nathan Scott053b5752006-03-17 17:29:09 +1100959 + be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Nathan Scott13059ff2006-01-11 15:32:01 +1100961 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
Barry Naujoke5889e92007-02-10 18:35:58 +1100962 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scotte0144ca2005-11-25 16:42:22 +1100963 (bytes == sizeof(struct xfs_attr_sf_hdr)))
Nathan Scottd8cc8902005-11-02 10:34:53 +1100964 return(-1);
965 return(xfs_attr_shortform_bytesfit(dp, bytes));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
969 * Convert a leaf attribute list to shortform attribute list
970 */
971int
Dave Chinner517c2222013-04-24 18:58:55 +1000972xfs_attr3_leaf_to_shortform(
973 struct xfs_buf *bp,
974 struct xfs_da_args *args,
975 int forkoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Dave Chinner517c2222013-04-24 18:58:55 +1000977 struct xfs_attr_leafblock *leaf;
978 struct xfs_attr3_icleaf_hdr ichdr;
979 struct xfs_attr_leaf_entry *entry;
980 struct xfs_attr_leaf_name_local *name_loc;
981 struct xfs_da_args nargs;
982 struct xfs_inode *dp = args->dp;
983 char *tmpbuffer;
984 int error;
985 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Dave Chinner5a5881c2012-03-22 05:15:13 +0000987 trace_xfs_attr_leaf_to_sf(args);
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
Dave Chinner517c2222013-04-24 18:58:55 +1000990 if (!tmpbuffer)
991 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Dave Chinner1d9025e2012-06-22 18:50:14 +1000993 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(dp->i_mount));
Dave Chinner517c2222013-04-24 18:58:55 +1000994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
Dave Chinner517c2222013-04-24 18:58:55 +1000996 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
997 entry = xfs_attr3_leaf_entryp(leaf);
998
999 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001000 memset(bp->b_addr, 0, XFS_LBSIZE(dp->i_mount));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /*
1003 * Clean out the prior contents of the attribute list.
1004 */
1005 error = xfs_da_shrink_inode(args, 0, bp);
1006 if (error)
1007 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001008
1009 if (forkoff == -1) {
Nathan Scott13059ff2006-01-11 15:32:01 +11001010 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
Barry Naujoke5889e92007-02-10 18:35:58 +11001011 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
Christoph Hellwige1486de2009-02-04 09:36:00 +01001012 xfs_attr_fork_reset(dp, args->trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto out;
Nathan Scottd8cc8902005-11-02 10:34:53 +11001014 }
1015
1016 xfs_attr_shortform_create(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 /*
1019 * Copy the attributes
1020 */
1021 memset((char *)&nargs, 0, sizeof(nargs));
1022 nargs.dp = dp;
1023 nargs.firstblock = args->firstblock;
1024 nargs.flist = args->flist;
1025 nargs.total = args->total;
1026 nargs.whichfork = XFS_ATTR_FORK;
1027 nargs.trans = args->trans;
Barry Naujok6a178102008-05-21 16:42:05 +10001028 nargs.op_flags = XFS_DA_OP_OKNOENT;
Dave Chinner517c2222013-04-24 18:58:55 +10001029
1030 for (i = 0; i < ichdr.count; entry++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (entry->flags & XFS_ATTR_INCOMPLETE)
1032 continue; /* don't copy partial entries */
1033 if (!entry->nameidx)
1034 continue;
1035 ASSERT(entry->flags & XFS_ATTR_LOCAL);
Dave Chinner517c2222013-04-24 18:58:55 +10001036 name_loc = xfs_attr3_leaf_name_local(leaf, i);
Dave Chinnera9273ca2010-01-20 10:47:48 +11001037 nargs.name = name_loc->nameval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 nargs.namelen = name_loc->namelen;
Dave Chinnera9273ca2010-01-20 10:47:48 +11001039 nargs.value = &name_loc->nameval[nargs.namelen];
Nathan Scott053b5752006-03-17 17:29:09 +11001040 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001041 nargs.hashval = be32_to_cpu(entry->hashval);
Tim Shimmin726801b2006-09-28 11:01:37 +10001042 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
Nathan Scottd8cc8902005-11-02 10:34:53 +11001043 xfs_attr_shortform_add(&nargs, forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 error = 0;
1046
1047out:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001048 kmem_free(tmpbuffer);
Dave Chinner517c2222013-04-24 18:58:55 +10001049 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052/*
1053 * Convert from using a single leaf to a root node and a leaf.
1054 */
1055int
Dave Chinner517c2222013-04-24 18:58:55 +10001056xfs_attr3_leaf_to_node(
1057 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Dave Chinner517c2222013-04-24 18:58:55 +10001059 struct xfs_attr_leafblock *leaf;
1060 struct xfs_attr3_icleaf_hdr icleafhdr;
1061 struct xfs_attr_leaf_entry *entries;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001062 struct xfs_da_node_entry *btree;
Dave Chinner517c2222013-04-24 18:58:55 +10001063 struct xfs_da3_icnode_hdr icnodehdr;
1064 struct xfs_da_intnode *node;
1065 struct xfs_inode *dp = args->dp;
1066 struct xfs_mount *mp = dp->i_mount;
1067 struct xfs_buf *bp1 = NULL;
1068 struct xfs_buf *bp2 = NULL;
1069 xfs_dablk_t blkno;
1070 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Dave Chinner5a5881c2012-03-22 05:15:13 +00001072 trace_xfs_attr_leaf_to_node(args);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 error = xfs_da_grow_inode(args, &blkno);
1075 if (error)
1076 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001077 error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (error)
1079 goto out;
Dave Chinnerad14c332012-11-12 22:54:16 +11001080
Dave Chinner517c2222013-04-24 18:58:55 +10001081 error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (error)
1083 goto out;
Dave Chinner517c2222013-04-24 18:58:55 +10001084
1085 /* copy leaf to new buffer, update identifiers */
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001086 xfs_trans_buf_set_type(args->trans, bp2, XFS_BLF_ATTR_LEAF_BUF);
Dave Chinner1813dd62012-11-14 17:54:40 +11001087 bp2->b_ops = bp1->b_ops;
Dave Chinner517c2222013-04-24 18:58:55 +10001088 memcpy(bp2->b_addr, bp1->b_addr, XFS_LBSIZE(mp));
1089 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1090 struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1091 hdr3->blkno = cpu_to_be64(bp2->b_bn);
1092 }
1093 xfs_trans_log_buf(args->trans, bp2, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 /*
1096 * Set up the new root node.
1097 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001098 error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (error)
1100 goto out;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001101 node = bp1->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001102 xfs_da3_node_hdr_from_disk(&icnodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001103 btree = xfs_da3_node_tree_p(node);
Dave Chinner517c2222013-04-24 18:58:55 +10001104
1105 leaf = bp2->b_addr;
1106 xfs_attr3_leaf_hdr_from_disk(&icleafhdr, leaf);
1107 entries = xfs_attr3_leaf_entryp(leaf);
1108
1109 /* both on-disk, don't endian-flip twice */
1110 btree[0].hashval = entries[icleafhdr.count - 1].hashval;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001111 btree[0].before = cpu_to_be32(blkno);
Dave Chinner517c2222013-04-24 18:58:55 +10001112 icnodehdr.count = 1;
1113 xfs_da3_node_hdr_to_disk(node, &icnodehdr);
1114 xfs_trans_log_buf(args->trans, bp1, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 error = 0;
1116out:
Dave Chinner517c2222013-04-24 18:58:55 +10001117 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120
1121/*========================================================================
1122 * Routines used for growing the Btree.
1123 *========================================================================*/
1124
1125/*
1126 * Create the initial contents of a leaf attribute list
1127 * or a leaf in a node attribute list.
1128 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001129STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001130xfs_attr3_leaf_create(
1131 struct xfs_da_args *args,
1132 xfs_dablk_t blkno,
1133 struct xfs_buf **bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Dave Chinner517c2222013-04-24 18:58:55 +10001135 struct xfs_attr_leafblock *leaf;
1136 struct xfs_attr3_icleaf_hdr ichdr;
1137 struct xfs_inode *dp = args->dp;
1138 struct xfs_mount *mp = dp->i_mount;
1139 struct xfs_buf *bp;
1140 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Dave Chinner5a5881c2012-03-22 05:15:13 +00001142 trace_xfs_attr_leaf_create(args);
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
1145 XFS_ATTR_FORK);
1146 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10001147 return error;
1148 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001149 xfs_trans_buf_set_type(args->trans, bp, XFS_BLF_ATTR_LEAF_BUF);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001150 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001151 memset(leaf, 0, XFS_LBSIZE(mp));
1152
1153 memset(&ichdr, 0, sizeof(ichdr));
1154 ichdr.firstused = XFS_LBSIZE(mp);
1155
1156 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1157 struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1158
1159 ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1160
1161 hdr3->blkno = cpu_to_be64(bp->b_bn);
1162 hdr3->owner = cpu_to_be64(dp->i_ino);
1163 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
1164
1165 ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1166 } else {
1167 ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1168 ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Dave Chinner517c2222013-04-24 18:58:55 +10001170 ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Dave Chinner517c2222013-04-24 18:58:55 +10001172 xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
1173 xfs_trans_log_buf(args->trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 *bpp = bp;
Dave Chinner517c2222013-04-24 18:58:55 +10001176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179/*
1180 * Split the leaf node, rebalance, then add the new entry.
1181 */
1182int
Dave Chinner517c2222013-04-24 18:58:55 +10001183xfs_attr3_leaf_split(
1184 struct xfs_da_state *state,
1185 struct xfs_da_state_blk *oldblk,
1186 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 xfs_dablk_t blkno;
1189 int error;
1190
Dave Chinner5a5881c2012-03-22 05:15:13 +00001191 trace_xfs_attr_leaf_split(state->args);
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /*
1194 * Allocate space for a new leaf node.
1195 */
1196 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1197 error = xfs_da_grow_inode(state->args, &blkno);
1198 if (error)
1199 return(error);
Dave Chinner517c2222013-04-24 18:58:55 +10001200 error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (error)
1202 return(error);
1203 newblk->blkno = blkno;
1204 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1205
1206 /*
1207 * Rebalance the entries across the two leaves.
1208 * NOTE: rebalance() currently depends on the 2nd block being empty.
1209 */
Dave Chinner517c2222013-04-24 18:58:55 +10001210 xfs_attr3_leaf_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001211 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (error)
1213 return(error);
1214
1215 /*
1216 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1217 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1218 * "new" attrs info. Will need the "old" info to remove it later.
1219 *
1220 * Insert the "new" entry in the correct block.
1221 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001222 if (state->inleaf) {
1223 trace_xfs_attr_leaf_add_old(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001224 error = xfs_attr3_leaf_add(oldblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001225 } else {
1226 trace_xfs_attr_leaf_add_new(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +10001227 error = xfs_attr3_leaf_add(newblk->bp, state->args);
Dave Chinner5a5881c2012-03-22 05:15:13 +00001228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /*
1231 * Update last hashval in each block since we added the name.
1232 */
1233 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1234 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1235 return(error);
1236}
1237
1238/*
1239 * Add a name to the leaf attribute list structure.
1240 */
1241int
Dave Chinner517c2222013-04-24 18:58:55 +10001242xfs_attr3_leaf_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001243 struct xfs_buf *bp,
1244 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Dave Chinner517c2222013-04-24 18:58:55 +10001246 struct xfs_attr_leafblock *leaf;
1247 struct xfs_attr3_icleaf_hdr ichdr;
1248 int tablesize;
1249 int entsize;
1250 int sum;
1251 int tmp;
1252 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Dave Chinner5a5881c2012-03-22 05:15:13 +00001254 trace_xfs_attr_leaf_add(args);
1255
Dave Chinner1d9025e2012-06-22 18:50:14 +10001256 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001257 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
1258 ASSERT(args->index >= 0 && args->index <= ichdr.count);
Nathan Scottaa82daa2005-11-02 10:33:33 +11001259 entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 args->trans->t_mountp->m_sb.sb_blocksize, NULL);
1261
1262 /*
1263 * Search through freemap for first-fit on new name length.
1264 * (may need to figure in size of entry struct too)
1265 */
Dave Chinner517c2222013-04-24 18:58:55 +10001266 tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1267 + xfs_attr3_leaf_hdr_size(leaf);
1268 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1269 if (tablesize > ichdr.firstused) {
1270 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 continue;
1272 }
Dave Chinner517c2222013-04-24 18:58:55 +10001273 if (!ichdr.freemap[i].size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 continue; /* no space in this map */
1275 tmp = entsize;
Dave Chinner517c2222013-04-24 18:58:55 +10001276 if (ichdr.freemap[i].base < ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 tmp += sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001278 if (ichdr.freemap[i].size >= tmp) {
1279 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1280 goto out_log_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Dave Chinner517c2222013-04-24 18:58:55 +10001282 sum += ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
1285 /*
1286 * If there are no holes in the address space of the block,
1287 * and we don't have enough freespace, then compaction will do us
1288 * no good and we should just give up.
1289 */
Dave Chinner517c2222013-04-24 18:58:55 +10001290 if (!ichdr.holes && sum < entsize)
1291 return XFS_ERROR(ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 /*
1294 * Compact the entries to coalesce free space.
1295 * This may change the hdr->count via dropping INCOMPLETE entries.
1296 */
Dave Chinner517c2222013-04-24 18:58:55 +10001297 xfs_attr3_leaf_compact(args, &ichdr, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /*
1300 * After compaction, the block is guaranteed to have only one
1301 * free region, in freemap[0]. If it is not big enough, give up.
1302 */
Dave Chinner517c2222013-04-24 18:58:55 +10001303 if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
1304 tmp = ENOSPC;
1305 goto out_log_hdr;
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Dave Chinner517c2222013-04-24 18:58:55 +10001308 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1309
1310out_log_hdr:
1311 xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
1312 xfs_trans_log_buf(args->trans, bp,
1313 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1314 xfs_attr3_leaf_hdr_size(leaf)));
1315 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
1318/*
1319 * Add a name to a leaf attribute list structure.
1320 */
1321STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001322xfs_attr3_leaf_add_work(
1323 struct xfs_buf *bp,
1324 struct xfs_attr3_icleaf_hdr *ichdr,
1325 struct xfs_da_args *args,
1326 int mapindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Dave Chinner517c2222013-04-24 18:58:55 +10001328 struct xfs_attr_leafblock *leaf;
1329 struct xfs_attr_leaf_entry *entry;
1330 struct xfs_attr_leaf_name_local *name_loc;
1331 struct xfs_attr_leaf_name_remote *name_rmt;
1332 struct xfs_mount *mp;
1333 int tmp;
1334 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Dave Chinneree732592012-11-12 22:53:53 +11001336 trace_xfs_attr_leaf_add_work(args);
1337
Dave Chinner1d9025e2012-06-22 18:50:14 +10001338 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001339 ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1340 ASSERT(args->index >= 0 && args->index <= ichdr->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /*
1343 * Force open some space in the entry array and fill it in.
1344 */
Dave Chinner517c2222013-04-24 18:58:55 +10001345 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1346 if (args->index < ichdr->count) {
1347 tmp = ichdr->count - args->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10001349 memmove(entry + 1, entry, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001350 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1352 }
Dave Chinner517c2222013-04-24 18:58:55 +10001353 ichdr->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /*
1356 * Allocate space for the new string (at the end of the run).
1357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 mp = args->trans->t_mountp;
Dave Chinner517c2222013-04-24 18:58:55 +10001359 ASSERT(ichdr->freemap[mapindex].base < XFS_LBSIZE(mp));
1360 ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1361 ASSERT(ichdr->freemap[mapindex].size >=
Nathan Scottaa82daa2005-11-02 10:33:33 +11001362 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1363 mp->m_sb.sb_blocksize, NULL));
Dave Chinner517c2222013-04-24 18:58:55 +10001364 ASSERT(ichdr->freemap[mapindex].size < XFS_LBSIZE(mp));
1365 ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1366
1367 ichdr->freemap[mapindex].size -=
1368 xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
1369 mp->m_sb.sb_blocksize, &tmp);
1370
1371 entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1372 ichdr->freemap[mapindex].size);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001373 entry->hashval = cpu_to_be32(args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
Tim Shimmin726801b2006-09-28 11:01:37 +10001375 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
Barry Naujok6a178102008-05-21 16:42:05 +10001376 if (args->op_flags & XFS_DA_OP_RENAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 entry->flags |= XFS_ATTR_INCOMPLETE;
1378 if ((args->blkno2 == args->blkno) &&
1379 (args->index2 <= args->index)) {
1380 args->index2++;
1381 }
1382 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001383 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001385 ASSERT((args->index == 0) ||
1386 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
Dave Chinner517c2222013-04-24 18:58:55 +10001387 ASSERT((args->index == ichdr->count - 1) ||
Nathan Scott6b19f2d2006-03-17 17:29:02 +11001388 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 * For "remote" attribute values, simply note that we need to
1392 * allocate space for the "remote" value. We can't actually
1393 * allocate the extents in this transaction, and we can't decide
1394 * which blocks they should be as we might allocate more blocks
1395 * as part of this transaction (a split operation for example).
1396 */
1397 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10001398 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 name_loc->namelen = args->namelen;
Nathan Scott053b5752006-03-17 17:29:09 +11001400 name_loc->valuelen = cpu_to_be16(args->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1402 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
Nathan Scott053b5752006-03-17 17:29:09 +11001403 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10001405 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 name_rmt->namelen = args->namelen;
1407 memcpy((char *)name_rmt->name, args->name, args->namelen);
1408 entry->flags |= XFS_ATTR_INCOMPLETE;
1409 /* just in case */
1410 name_rmt->valuelen = 0;
1411 name_rmt->valueblk = 0;
1412 args->rmtblkno = 1;
1413 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1414 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001415 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10001416 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 xfs_attr_leaf_entsize(leaf, args->index)));
1418
1419 /*
1420 * Update the control info for this leaf node
1421 */
Dave Chinner517c2222013-04-24 18:58:55 +10001422 if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1423 ichdr->firstused = be16_to_cpu(entry->nameidx);
1424
1425 ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1426 + xfs_attr3_leaf_hdr_size(leaf));
1427 tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1428 + xfs_attr3_leaf_hdr_size(leaf);
1429
1430 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1431 if (ichdr->freemap[i].base == tmp) {
1432 ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1433 ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 }
Dave Chinner517c2222013-04-24 18:58:55 +10001436 ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
1440/*
1441 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1442 */
1443STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001444xfs_attr3_leaf_compact(
Dave Chinneree732592012-11-12 22:53:53 +11001445 struct xfs_da_args *args,
Dave Chinner517c2222013-04-24 18:58:55 +10001446 struct xfs_attr3_icleaf_hdr *ichdr_d,
Dave Chinneree732592012-11-12 22:53:53 +11001447 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Dave Chinneree732592012-11-12 22:53:53 +11001449 xfs_attr_leafblock_t *leaf_s, *leaf_d;
Dave Chinner517c2222013-04-24 18:58:55 +10001450 struct xfs_attr3_icleaf_hdr ichdr_s;
Dave Chinneree732592012-11-12 22:53:53 +11001451 struct xfs_trans *trans = args->trans;
1452 struct xfs_mount *mp = trans->t_mountp;
1453 char *tmpbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Dave Chinneree732592012-11-12 22:53:53 +11001455 trace_xfs_attr_leaf_compact(args);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1458 ASSERT(tmpbuffer != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001459 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1460 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 /*
1463 * Copy basic information
1464 */
1465 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001466 leaf_d = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001467 ichdr_s = *ichdr_d; /* struct copy */
1468 ichdr_d->firstused = XFS_LBSIZE(mp);
1469 ichdr_d->usedbytes = 0;
1470 ichdr_d->count = 0;
1471 ichdr_d->holes = 0;
1472 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_s);
1473 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 /*
1476 * Copy all entry's in the same (sorted) order,
1477 * but allocate name/value pairs packed and in sequence.
1478 */
Dave Chinner517c2222013-04-24 18:58:55 +10001479 xfs_attr3_leaf_moveents(leaf_s, &ichdr_s, 0, leaf_d, ichdr_d, 0,
1480 ichdr_s.count, mp);
1481 /*
1482 * this logs the entire buffer, but the caller must write the header
1483 * back to the buffer when it is finished modifying it.
1484 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001485 xfs_trans_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001487 kmem_free(tmpbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
1490/*
Dave Chinner517c2222013-04-24 18:58:55 +10001491 * Compare two leaf blocks "order".
1492 * Return 0 unless leaf2 should go before leaf1.
1493 */
1494static int
1495xfs_attr3_leaf_order(
1496 struct xfs_buf *leaf1_bp,
1497 struct xfs_attr3_icleaf_hdr *leaf1hdr,
1498 struct xfs_buf *leaf2_bp,
1499 struct xfs_attr3_icleaf_hdr *leaf2hdr)
1500{
1501 struct xfs_attr_leaf_entry *entries1;
1502 struct xfs_attr_leaf_entry *entries2;
1503
1504 entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1505 entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1506 if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1507 ((be32_to_cpu(entries2[0].hashval) <
1508 be32_to_cpu(entries1[0].hashval)) ||
1509 (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1510 be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1511 return 1;
1512 }
1513 return 0;
1514}
1515
1516int
1517xfs_attr_leaf_order(
1518 struct xfs_buf *leaf1_bp,
1519 struct xfs_buf *leaf2_bp)
1520{
1521 struct xfs_attr3_icleaf_hdr ichdr1;
1522 struct xfs_attr3_icleaf_hdr ichdr2;
1523
1524 xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1_bp->b_addr);
1525 xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2_bp->b_addr);
1526 return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1527}
1528
1529/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 * Redistribute the attribute list entries between two leaf nodes,
1531 * taking into account the size of the new entry.
1532 *
1533 * NOTE: if new block is empty, then it will get the upper half of the
1534 * old block. At present, all (one) callers pass in an empty second block.
1535 *
1536 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1537 * to match what it is doing in splitting the attribute leaf block. Those
1538 * values are used in "atomic rename" operations on attributes. Note that
1539 * the "new" and "old" values can end up in different blocks.
1540 */
1541STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10001542xfs_attr3_leaf_rebalance(
1543 struct xfs_da_state *state,
1544 struct xfs_da_state_blk *blk1,
1545 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Dave Chinner517c2222013-04-24 18:58:55 +10001547 struct xfs_da_args *args;
1548 struct xfs_attr_leafblock *leaf1;
1549 struct xfs_attr_leafblock *leaf2;
1550 struct xfs_attr3_icleaf_hdr ichdr1;
1551 struct xfs_attr3_icleaf_hdr ichdr2;
1552 struct xfs_attr_leaf_entry *entries1;
1553 struct xfs_attr_leaf_entry *entries2;
1554 int count;
1555 int totallen;
1556 int max;
1557 int space;
1558 int swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 /*
1561 * Set up environment.
1562 */
1563 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1564 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001565 leaf1 = blk1->bp->b_addr;
1566 leaf2 = blk2->bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001567 xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1);
1568 xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2);
1569 ASSERT(ichdr2.count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 args = state->args;
1571
Dave Chinner5a5881c2012-03-22 05:15:13 +00001572 trace_xfs_attr_leaf_rebalance(args);
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /*
1575 * Check ordering of blocks, reverse if it makes things simpler.
1576 *
1577 * NOTE: Given that all (current) callers pass in an empty
1578 * second block, this code should never set "swap".
1579 */
1580 swap = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001581 if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1582 struct xfs_da_state_blk *tmp_blk;
1583 struct xfs_attr3_icleaf_hdr tmp_ichdr;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 tmp_blk = blk1;
1586 blk1 = blk2;
1587 blk2 = tmp_blk;
Dave Chinner517c2222013-04-24 18:58:55 +10001588
1589 /* struct copies to swap them rather than reconverting */
1590 tmp_ichdr = ichdr1;
1591 ichdr1 = ichdr2;
1592 ichdr2 = tmp_ichdr;
1593
Dave Chinner1d9025e2012-06-22 18:50:14 +10001594 leaf1 = blk1->bp->b_addr;
1595 leaf2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 swap = 1;
1597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /*
1600 * Examine entries until we reduce the absolute difference in
1601 * byte usage between the two blocks to a minimum. Then get
1602 * the direction to copy and the number of elements to move.
1603 *
1604 * "inleaf" is true if the new entry should be inserted into blk1.
1605 * If "swap" is also true, then reverse the sense of "inleaf".
1606 */
Dave Chinner517c2222013-04-24 18:58:55 +10001607 state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1608 blk2, &ichdr2,
1609 &count, &totallen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (swap)
1611 state->inleaf = !state->inleaf;
1612
1613 /*
1614 * Move any entries required from leaf to leaf:
1615 */
Dave Chinner517c2222013-04-24 18:58:55 +10001616 if (count < ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 /*
1618 * Figure the total bytes to be added to the destination leaf.
1619 */
1620 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001621 count = ichdr1.count - count;
1622 space = ichdr1.usedbytes - totallen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 space += count * sizeof(xfs_attr_leaf_entry_t);
1624
1625 /*
1626 * leaf2 is the destination, compact it if it looks tight.
1627 */
Dave Chinner517c2222013-04-24 18:58:55 +10001628 max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1629 max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001630 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001631 xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /*
1634 * Move high entries from leaf1 to low end of leaf2.
1635 */
Dave Chinner517c2222013-04-24 18:58:55 +10001636 xfs_attr3_leaf_moveents(leaf1, &ichdr1, ichdr1.count - count,
1637 leaf2, &ichdr2, 0, count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Dave Chinner517c2222013-04-24 18:58:55 +10001639 } else if (count > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /*
1641 * I assert that since all callers pass in an empty
1642 * second buffer, this code should never execute.
1643 */
Dave Chinner07428d72012-11-12 22:09:44 +11001644 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /*
1647 * Figure the total bytes to be added to the destination leaf.
1648 */
1649 /* number entries being moved */
Dave Chinner517c2222013-04-24 18:58:55 +10001650 count -= ichdr1.count;
1651 space = totallen - ichdr1.usedbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 space += count * sizeof(xfs_attr_leaf_entry_t);
1653
1654 /*
1655 * leaf1 is the destination, compact it if it looks tight.
1656 */
Dave Chinner517c2222013-04-24 18:58:55 +10001657 max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1658 max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinneree732592012-11-12 22:53:53 +11001659 if (space > max)
Dave Chinner517c2222013-04-24 18:58:55 +10001660 xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 /*
1663 * Move low entries from leaf2 to high end of leaf1.
1664 */
Dave Chinner517c2222013-04-24 18:58:55 +10001665 xfs_attr3_leaf_moveents(leaf2, &ichdr2, 0, leaf1, &ichdr1,
1666 ichdr1.count, count, state->mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
Dave Chinner517c2222013-04-24 18:58:55 +10001669 xfs_attr3_leaf_hdr_to_disk(leaf1, &ichdr1);
1670 xfs_attr3_leaf_hdr_to_disk(leaf2, &ichdr2);
1671 xfs_trans_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
1672 xfs_trans_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * Copy out last hashval in each block for B-tree code.
1676 */
Dave Chinner517c2222013-04-24 18:58:55 +10001677 entries1 = xfs_attr3_leaf_entryp(leaf1);
1678 entries2 = xfs_attr3_leaf_entryp(leaf2);
1679 blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1680 blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 /*
1683 * Adjust the expected index for insertion.
1684 * NOTE: this code depends on the (current) situation that the
1685 * second block was originally empty.
1686 *
1687 * If the insertion point moved to the 2nd block, we must adjust
1688 * the index. We must also track the entry just following the
1689 * new entry for use in an "atomic rename" operation, that entry
1690 * is always the "old" entry and the "new" entry is what we are
1691 * inserting. The index/blkno fields refer to the "old" entry,
1692 * while the index2/blkno2 fields refer to the "new" entry.
1693 */
Dave Chinner517c2222013-04-24 18:58:55 +10001694 if (blk1->index > ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 ASSERT(state->inleaf == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10001696 blk2->index = blk1->index - ichdr1.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 args->index = args->index2 = blk2->index;
1698 args->blkno = args->blkno2 = blk2->blkno;
Dave Chinner517c2222013-04-24 18:58:55 +10001699 } else if (blk1->index == ichdr1.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (state->inleaf) {
1701 args->index = blk1->index;
1702 args->blkno = blk1->blkno;
1703 args->index2 = 0;
1704 args->blkno2 = blk2->blkno;
1705 } else {
Dave Chinner07428d72012-11-12 22:09:44 +11001706 /*
1707 * On a double leaf split, the original attr location
1708 * is already stored in blkno2/index2, so don't
1709 * overwrite it overwise we corrupt the tree.
1710 */
Dave Chinner517c2222013-04-24 18:58:55 +10001711 blk2->index = blk1->index - ichdr1.count;
Dave Chinner07428d72012-11-12 22:09:44 +11001712 args->index = blk2->index;
1713 args->blkno = blk2->blkno;
1714 if (!state->extravalid) {
1715 /*
1716 * set the new attr location to match the old
1717 * one and let the higher level split code
1718 * decide where in the leaf to place it.
1719 */
1720 args->index2 = blk2->index;
1721 args->blkno2 = blk2->blkno;
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724 } else {
1725 ASSERT(state->inleaf == 1);
1726 args->index = args->index2 = blk1->index;
1727 args->blkno = args->blkno2 = blk1->blkno;
1728 }
1729}
1730
1731/*
1732 * Examine entries until we reduce the absolute difference in
1733 * byte usage between the two blocks to a minimum.
1734 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1735 * GROT: there will always be enough room in either block for a new entry.
1736 * GROT: Do a double-split for this case?
1737 */
1738STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10001739xfs_attr3_leaf_figure_balance(
1740 struct xfs_da_state *state,
1741 struct xfs_da_state_blk *blk1,
1742 struct xfs_attr3_icleaf_hdr *ichdr1,
1743 struct xfs_da_state_blk *blk2,
1744 struct xfs_attr3_icleaf_hdr *ichdr2,
1745 int *countarg,
1746 int *usedbytesarg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Dave Chinner517c2222013-04-24 18:58:55 +10001748 struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
1749 struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
1750 struct xfs_attr_leaf_entry *entry;
1751 int count;
1752 int max;
1753 int index;
1754 int totallen = 0;
1755 int half;
1756 int lastdelta;
1757 int foundit = 0;
1758 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 /*
1761 * Examine entries until we reduce the absolute difference in
1762 * byte usage between the two blocks to a minimum.
1763 */
Dave Chinner517c2222013-04-24 18:58:55 +10001764 max = ichdr1->count + ichdr2->count;
1765 half = (max + 1) * sizeof(*entry);
1766 half += ichdr1->usedbytes + ichdr2->usedbytes +
1767 xfs_attr_leaf_newentsize(state->args->namelen,
1768 state->args->valuelen,
1769 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 half /= 2;
1771 lastdelta = state->blocksize;
Dave Chinner517c2222013-04-24 18:58:55 +10001772 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 for (count = index = 0; count < max; entry++, index++, count++) {
1774
1775#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1776 /*
1777 * The new entry is in the first block, account for it.
1778 */
1779 if (count == blk1->index) {
1780 tmp = totallen + sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001781 xfs_attr_leaf_newentsize(
1782 state->args->namelen,
1783 state->args->valuelen,
1784 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1786 break;
1787 lastdelta = XFS_ATTR_ABS(half - tmp);
1788 totallen = tmp;
1789 foundit = 1;
1790 }
1791
1792 /*
1793 * Wrap around into the second block if necessary.
1794 */
Dave Chinner517c2222013-04-24 18:58:55 +10001795 if (count == ichdr1->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 leaf1 = leaf2;
Dave Chinner517c2222013-04-24 18:58:55 +10001797 entry = xfs_attr3_leaf_entryp(leaf1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 index = 0;
1799 }
1800
1801 /*
1802 * Figure out if next leaf entry would be too much.
1803 */
1804 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1805 index);
1806 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1807 break;
1808 lastdelta = XFS_ATTR_ABS(half - tmp);
1809 totallen = tmp;
1810#undef XFS_ATTR_ABS
1811 }
1812
1813 /*
1814 * Calculate the number of usedbytes that will end up in lower block.
1815 * If new entry not in lower block, fix up the count.
1816 */
1817 totallen -= count * sizeof(*entry);
1818 if (foundit) {
1819 totallen -= sizeof(*entry) +
Nathan Scottaa82daa2005-11-02 10:33:33 +11001820 xfs_attr_leaf_newentsize(
1821 state->args->namelen,
1822 state->args->valuelen,
1823 state->blocksize, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825
1826 *countarg = count;
1827 *usedbytesarg = totallen;
Dave Chinner517c2222013-04-24 18:58:55 +10001828 return foundit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
1831/*========================================================================
1832 * Routines used for shrinking the Btree.
1833 *========================================================================*/
1834
1835/*
1836 * Check a leaf block and its neighbors to see if the block should be
1837 * collapsed into one or the other neighbor. Always keep the block
1838 * with the smaller block number.
1839 * If the current block is over 50% full, don't try to join it, return 0.
1840 * If the block is empty, fill in the state structure and return 2.
1841 * If it can be collapsed, fill in the state structure and return 1.
1842 * If nothing can be done, return 0.
1843 *
1844 * GROT: allow for INCOMPLETE entries in calculation.
1845 */
1846int
Dave Chinner517c2222013-04-24 18:58:55 +10001847xfs_attr3_leaf_toosmall(
1848 struct xfs_da_state *state,
1849 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Dave Chinner517c2222013-04-24 18:58:55 +10001851 struct xfs_attr_leafblock *leaf;
1852 struct xfs_da_state_blk *blk;
1853 struct xfs_attr3_icleaf_hdr ichdr;
1854 struct xfs_buf *bp;
1855 xfs_dablk_t blkno;
1856 int bytes;
1857 int forward;
1858 int error;
1859 int retval;
1860 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Dave Chinneree732592012-11-12 22:53:53 +11001862 trace_xfs_attr_leaf_toosmall(state->args);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /*
1865 * Check for the degenerate case of the block being over 50% full.
1866 * If so, it's not worth even looking to see if we might be able
1867 * to coalesce with a sibling.
1868 */
1869 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner517c2222013-04-24 18:58:55 +10001870 leaf = blk->bp->b_addr;
1871 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
1872 bytes = xfs_attr3_leaf_hdr_size(leaf) +
1873 ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
1874 ichdr.usedbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (bytes > (state->blocksize >> 1)) {
1876 *action = 0; /* blk over 50%, don't try to join */
1877 return(0);
1878 }
1879
1880 /*
1881 * Check for the degenerate case of the block being empty.
1882 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001883 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 * to merge with the forward block unless it is NULL.
1885 */
Dave Chinner517c2222013-04-24 18:58:55 +10001886 if (ichdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 /*
1888 * Make altpath point to the block we want to keep and
1889 * path point to the block we want to drop (this one).
1890 */
Dave Chinner517c2222013-04-24 18:58:55 +10001891 forward = (ichdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001893 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 0, &retval);
1895 if (error)
1896 return(error);
1897 if (retval) {
1898 *action = 0;
1899 } else {
1900 *action = 2;
1901 }
Dave Chinner517c2222013-04-24 18:58:55 +10001902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
1905 /*
1906 * Examine each sibling block to see if we can coalesce with
1907 * at least 25% free space to spare. We need to figure out
1908 * whether to merge with the forward or the backward block.
1909 * We prefer coalescing with the lower numbered sibling so as
1910 * to shrink an attribute list over time.
1911 */
1912 /* start with smaller blk num */
Dave Chinner517c2222013-04-24 18:58:55 +10001913 forward = ichdr.forw < ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 for (i = 0; i < 2; forward = !forward, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10001915 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (forward)
Dave Chinner517c2222013-04-24 18:58:55 +10001917 blkno = ichdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 else
Dave Chinner517c2222013-04-24 18:58:55 +10001919 blkno = ichdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (blkno == 0)
1921 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10001922 error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
Dave Chinnerad14c332012-11-12 22:54:16 +11001923 blkno, -1, &bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (error)
1925 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Dave Chinner517c2222013-04-24 18:58:55 +10001927 xfs_attr3_leaf_hdr_from_disk(&ichdr2, bp->b_addr);
1928
1929 bytes = state->blocksize - (state->blocksize >> 2) -
1930 ichdr.usedbytes - ichdr2.usedbytes -
1931 ((ichdr.count + ichdr2.count) *
1932 sizeof(xfs_attr_leaf_entry_t)) -
1933 xfs_attr3_leaf_hdr_size(leaf);
1934
Dave Chinner1d9025e2012-06-22 18:50:14 +10001935 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (bytes >= 0)
1937 break; /* fits with at least 25% to spare */
1938 }
1939 if (i >= 2) {
1940 *action = 0;
1941 return(0);
1942 }
1943
1944 /*
1945 * Make altpath point to the block we want to keep (the lower
1946 * numbered block) and path point to the block we want to drop.
1947 */
1948 memcpy(&state->altpath, &state->path, sizeof(state->path));
1949 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001950 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 0, &retval);
1952 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001953 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 0, &retval);
1955 }
1956 if (error)
1957 return(error);
1958 if (retval) {
1959 *action = 0;
1960 } else {
1961 *action = 1;
1962 }
1963 return(0);
1964}
1965
1966/*
1967 * Remove a name from the leaf attribute list structure.
1968 *
1969 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1970 * If two leaves are 37% full, when combined they will leave 25% free.
1971 */
1972int
Dave Chinner517c2222013-04-24 18:58:55 +10001973xfs_attr3_leaf_remove(
1974 struct xfs_buf *bp,
1975 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Dave Chinner517c2222013-04-24 18:58:55 +10001977 struct xfs_attr_leafblock *leaf;
1978 struct xfs_attr3_icleaf_hdr ichdr;
1979 struct xfs_attr_leaf_entry *entry;
1980 struct xfs_mount *mp = args->trans->t_mountp;
1981 int before;
1982 int after;
1983 int smallest;
1984 int entsize;
1985 int tablesize;
1986 int tmp;
1987 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Dave Chinneree732592012-11-12 22:53:53 +11001989 trace_xfs_attr_leaf_remove(args);
1990
Dave Chinner1d9025e2012-06-22 18:50:14 +10001991 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10001992 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
1993
1994 ASSERT(ichdr.count > 0 && ichdr.count < XFS_LBSIZE(mp) / 8);
1995 ASSERT(args->index >= 0 && args->index < ichdr.count);
1996 ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
1997 xfs_attr3_leaf_hdr_size(leaf));
1998
1999 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2000
2001 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002002 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /*
2005 * Scan through free region table:
2006 * check for adjacency of free'd entry with an existing one,
2007 * find smallest free region in case we need to replace it,
2008 * adjust any map that borders the entry table,
2009 */
Dave Chinner517c2222013-04-24 18:58:55 +10002010 tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
2011 + xfs_attr3_leaf_hdr_size(leaf);
2012 tmp = ichdr.freemap[0].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 before = after = -1;
2014 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2015 entsize = xfs_attr_leaf_entsize(leaf, args->index);
Dave Chinner517c2222013-04-24 18:58:55 +10002016 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
2017 ASSERT(ichdr.freemap[i].base < XFS_LBSIZE(mp));
2018 ASSERT(ichdr.freemap[i].size < XFS_LBSIZE(mp));
2019 if (ichdr.freemap[i].base == tablesize) {
2020 ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2021 ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023
Dave Chinner517c2222013-04-24 18:58:55 +10002024 if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2025 be16_to_cpu(entry->nameidx)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 before = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002027 } else if (ichdr.freemap[i].base ==
2028 (be16_to_cpu(entry->nameidx) + entsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 after = i;
Dave Chinner517c2222013-04-24 18:58:55 +10002030 } else if (ichdr.freemap[i].size < tmp) {
2031 tmp = ichdr.freemap[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 smallest = i;
2033 }
2034 }
2035
2036 /*
2037 * Coalesce adjacent freemap regions,
2038 * or replace the smallest region.
2039 */
2040 if ((before >= 0) || (after >= 0)) {
2041 if ((before >= 0) && (after >= 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10002042 ichdr.freemap[before].size += entsize;
2043 ichdr.freemap[before].size += ichdr.freemap[after].size;
2044 ichdr.freemap[after].base = 0;
2045 ichdr.freemap[after].size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 } else if (before >= 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002047 ichdr.freemap[before].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002049 ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2050 ichdr.freemap[after].size += entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 } else {
2053 /*
2054 * Replace smallest region (if it is smaller than free'd entry)
2055 */
Dave Chinner517c2222013-04-24 18:58:55 +10002056 if (ichdr.freemap[smallest].size < entsize) {
2057 ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2058 ichdr.freemap[smallest].size = entsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060 }
2061
2062 /*
2063 * Did we remove the first entry?
2064 */
Dave Chinner517c2222013-04-24 18:58:55 +10002065 if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 smallest = 1;
2067 else
2068 smallest = 0;
2069
2070 /*
2071 * Compress the remaining entries and zero out the removed stuff.
2072 */
Dave Chinner517c2222013-04-24 18:58:55 +10002073 memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2074 ichdr.usedbytes -= entsize;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002075 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002076 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 entsize));
2078
Dave Chinner517c2222013-04-24 18:58:55 +10002079 tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2080 memmove(entry, entry + 1, tmp);
2081 ichdr.count--;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002082 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002083 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2084
2085 entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2086 memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 /*
2089 * If we removed the first entry, re-find the first used byte
2090 * in the name area. Note that if the entry was the "firstused",
2091 * then we don't have a "hole" in our block resulting from
2092 * removing the name.
2093 */
2094 if (smallest) {
2095 tmp = XFS_LBSIZE(mp);
Dave Chinner517c2222013-04-24 18:58:55 +10002096 entry = xfs_attr3_leaf_entryp(leaf);
2097 for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2098 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002099 ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
2100
2101 if (be16_to_cpu(entry->nameidx) < tmp)
2102 tmp = be16_to_cpu(entry->nameidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
Dave Chinner517c2222013-04-24 18:58:55 +10002104 ichdr.firstused = tmp;
2105 if (!ichdr.firstused)
2106 ichdr.firstused = tmp - XFS_ATTR_LEAF_NAME_ALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002108 ichdr.holes = 1; /* mark as needing compaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Dave Chinner517c2222013-04-24 18:58:55 +10002110 xfs_attr3_leaf_hdr_to_disk(leaf, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002111 xfs_trans_log_buf(args->trans, bp,
Dave Chinner517c2222013-04-24 18:58:55 +10002112 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2113 xfs_attr3_leaf_hdr_size(leaf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 /*
2116 * Check if leaf is less than 50% full, caller may want to
2117 * "join" the leaf with a sibling if so.
2118 */
Dave Chinner517c2222013-04-24 18:58:55 +10002119 tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2120 ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2121
2122 return tmp < mp->m_attr_magicpct; /* leaf is < 37% full */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
2125/*
2126 * Move all the attribute list entries from drop_leaf into save_leaf.
2127 */
2128void
Dave Chinner517c2222013-04-24 18:58:55 +10002129xfs_attr3_leaf_unbalance(
2130 struct xfs_da_state *state,
2131 struct xfs_da_state_blk *drop_blk,
2132 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Dave Chinner517c2222013-04-24 18:58:55 +10002134 struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2135 struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2136 struct xfs_attr3_icleaf_hdr drophdr;
2137 struct xfs_attr3_icleaf_hdr savehdr;
2138 struct xfs_attr_leaf_entry *entry;
2139 struct xfs_mount *mp = state->mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Dave Chinner5a5881c2012-03-22 05:15:13 +00002141 trace_xfs_attr_leaf_unbalance(state->args);
2142
Dave Chinner1d9025e2012-06-22 18:50:14 +10002143 drop_leaf = drop_blk->bp->b_addr;
2144 save_leaf = save_blk->bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002145 xfs_attr3_leaf_hdr_from_disk(&drophdr, drop_leaf);
2146 xfs_attr3_leaf_hdr_from_disk(&savehdr, save_leaf);
2147 entry = xfs_attr3_leaf_entryp(drop_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /*
2150 * Save last hashval from dying block for later Btree fixup.
2151 */
Dave Chinner517c2222013-04-24 18:58:55 +10002152 drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 /*
2155 * Check if we need a temp buffer, or can we do it in place.
2156 * Note that we don't check "leaf" for holes because we will
2157 * always be dropping it, toosmall() decided that for us already.
2158 */
Dave Chinner517c2222013-04-24 18:58:55 +10002159 if (savehdr.holes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /*
2161 * dest leaf has no holes, so we add there. May need
2162 * to make some room in the entry array.
2163 */
Dave Chinner517c2222013-04-24 18:58:55 +10002164 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2165 drop_blk->bp, &drophdr)) {
2166 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2167 save_leaf, &savehdr, 0,
2168 drophdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002170 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2171 save_leaf, &savehdr,
2172 savehdr.count, drophdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174 } else {
2175 /*
2176 * Destination has holes, so we make a temporary copy
2177 * of the leaf and add them both to that.
2178 */
Dave Chinner517c2222013-04-24 18:58:55 +10002179 struct xfs_attr_leafblock *tmp_leaf;
2180 struct xfs_attr3_icleaf_hdr tmphdr;
2181
2182 tmp_leaf = kmem_alloc(state->blocksize, KM_SLEEP);
2183 memset(tmp_leaf, 0, state->blocksize);
2184 memset(&tmphdr, 0, sizeof(tmphdr));
2185
2186 tmphdr.magic = savehdr.magic;
2187 tmphdr.forw = savehdr.forw;
2188 tmphdr.back = savehdr.back;
2189 tmphdr.firstused = state->blocksize;
2190 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2191 drop_blk->bp, &drophdr)) {
2192 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2193 tmp_leaf, &tmphdr, 0,
2194 drophdr.count, mp);
2195 xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0,
2196 tmp_leaf, &tmphdr, tmphdr.count,
2197 savehdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002199 xfs_attr3_leaf_moveents(save_leaf, &savehdr, 0,
2200 tmp_leaf, &tmphdr, 0,
2201 savehdr.count, mp);
2202 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
2203 tmp_leaf, &tmphdr, tmphdr.count,
2204 drophdr.count, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
Dave Chinner517c2222013-04-24 18:58:55 +10002206 memcpy(save_leaf, tmp_leaf, state->blocksize);
2207 savehdr = tmphdr; /* struct copy */
2208 kmem_free(tmp_leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210
Dave Chinner517c2222013-04-24 18:58:55 +10002211 xfs_attr3_leaf_hdr_to_disk(save_leaf, &savehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002212 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 state->blocksize - 1);
2214
2215 /*
2216 * Copy out last hashval in each block for B-tree code.
2217 */
Dave Chinner517c2222013-04-24 18:58:55 +10002218 entry = xfs_attr3_leaf_entryp(save_leaf);
2219 save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
2222/*========================================================================
2223 * Routines used for finding things in the Btree.
2224 *========================================================================*/
2225
2226/*
2227 * Look up a name in a leaf attribute list structure.
2228 * This is the internal routine, it uses the caller's buffer.
2229 *
2230 * Note that duplicate keys are allowed, but only check within the
2231 * current leaf node. The Btree code must check in adjacent leaf nodes.
2232 *
2233 * Return in args->index the index into the entry[] array of either
2234 * the found entry, or where the entry should have been (insert before
2235 * that entry).
2236 *
2237 * Don't change the args->value unless we find the attribute.
2238 */
2239int
Dave Chinner517c2222013-04-24 18:58:55 +10002240xfs_attr3_leaf_lookup_int(
2241 struct xfs_buf *bp,
2242 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Dave Chinner517c2222013-04-24 18:58:55 +10002244 struct xfs_attr_leafblock *leaf;
2245 struct xfs_attr3_icleaf_hdr ichdr;
2246 struct xfs_attr_leaf_entry *entry;
2247 struct xfs_attr_leaf_entry *entries;
2248 struct xfs_attr_leaf_name_local *name_loc;
2249 struct xfs_attr_leaf_name_remote *name_rmt;
2250 xfs_dahash_t hashval;
2251 int probe;
2252 int span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Dave Chinner5a5881c2012-03-22 05:15:13 +00002254 trace_xfs_attr_leaf_lookup(args);
2255
Dave Chinner1d9025e2012-06-22 18:50:14 +10002256 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002257 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2258 entries = xfs_attr3_leaf_entryp(leaf);
2259 ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 /*
2262 * Binary search. (note: small blocks will skip this loop)
2263 */
2264 hashval = args->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002265 probe = span = ichdr.count / 2;
2266 for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 span /= 2;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002268 if (be32_to_cpu(entry->hashval) < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 probe += span;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002270 else if (be32_to_cpu(entry->hashval) > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 probe -= span;
2272 else
2273 break;
2274 }
Dave Chinner517c2222013-04-24 18:58:55 +10002275 ASSERT(probe >= 0 && (!ichdr.count || probe < ichdr.count));
2276 ASSERT(span <= 4 || be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /*
2279 * Since we may have duplicate hashval's, find the first matching
2280 * hashval in the leaf.
2281 */
Dave Chinner517c2222013-04-24 18:58:55 +10002282 while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 entry--;
2284 probe--;
2285 }
Dave Chinner517c2222013-04-24 18:58:55 +10002286 while (probe < ichdr.count &&
2287 be32_to_cpu(entry->hashval) < hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 entry++;
2289 probe++;
2290 }
Dave Chinner517c2222013-04-24 18:58:55 +10002291 if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 args->index = probe;
Dave Chinner517c2222013-04-24 18:58:55 +10002293 return XFS_ERROR(ENOATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295
2296 /*
2297 * Duplicate keys may be present, so search all of them for a match.
2298 */
Dave Chinner517c2222013-04-24 18:58:55 +10002299 for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 entry++, probe++) {
2301/*
2302 * GROT: Add code to remove incomplete entries.
2303 */
2304 /*
2305 * If we are looking for INCOMPLETE entries, show only those.
2306 * If we are looking for complete entries, show only those.
2307 */
2308 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2309 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2310 continue;
2311 }
2312 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002313 name_loc = xfs_attr3_leaf_name_local(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (name_loc->namelen != args->namelen)
2315 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002316 if (memcmp(args->name, name_loc->nameval,
2317 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002319 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 continue;
2321 args->index = probe;
Dave Chinner517c2222013-04-24 18:58:55 +10002322 return XFS_ERROR(EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002324 name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (name_rmt->namelen != args->namelen)
2326 continue;
Dave Chinner517c2222013-04-24 18:58:55 +10002327 if (memcmp(args->name, name_rmt->name,
2328 args->namelen) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 continue;
Tim Shimmin726801b2006-09-28 11:01:37 +10002330 if (!xfs_attr_namesp_match(args->flags, entry->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 continue;
2332 args->index = probe;
Nathan Scottc0f054e2006-03-17 17:29:18 +11002333 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
Nathan Scottc0f054e2006-03-17 17:29:18 +11002335 be32_to_cpu(name_rmt->valuelen));
Dave Chinner517c2222013-04-24 18:58:55 +10002336 return XFS_ERROR(EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338 }
2339 args->index = probe;
Dave Chinner517c2222013-04-24 18:58:55 +10002340 return XFS_ERROR(ENOATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
2343/*
2344 * Get the value associated with an attribute name from a leaf attribute
2345 * list structure.
2346 */
2347int
Dave Chinner517c2222013-04-24 18:58:55 +10002348xfs_attr3_leaf_getvalue(
2349 struct xfs_buf *bp,
2350 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Dave Chinner517c2222013-04-24 18:58:55 +10002352 struct xfs_attr_leafblock *leaf;
2353 struct xfs_attr3_icleaf_hdr ichdr;
2354 struct xfs_attr_leaf_entry *entry;
2355 struct xfs_attr_leaf_name_local *name_loc;
2356 struct xfs_attr_leaf_name_remote *name_rmt;
2357 int valuelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Dave Chinner1d9025e2012-06-22 18:50:14 +10002359 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002360 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2361 ASSERT(ichdr.count < XFS_LBSIZE(args->dp->i_mount) / 8);
2362 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Dave Chinner517c2222013-04-24 18:58:55 +10002364 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002366 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 ASSERT(name_loc->namelen == args->namelen);
2368 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
Nathan Scott053b5752006-03-17 17:29:09 +11002369 valuelen = be16_to_cpu(name_loc->valuelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (args->flags & ATTR_KERNOVAL) {
2371 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374 if (args->valuelen < valuelen) {
2375 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002376 return XFS_ERROR(ERANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
2378 args->valuelen = valuelen;
2379 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2380 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002381 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 ASSERT(name_rmt->namelen == args->namelen);
2383 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002384 valuelen = be32_to_cpu(name_rmt->valuelen);
2385 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
2387 if (args->flags & ATTR_KERNOVAL) {
2388 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
2391 if (args->valuelen < valuelen) {
2392 args->valuelen = valuelen;
Dave Chinner517c2222013-04-24 18:58:55 +10002393 return XFS_ERROR(ERANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
2395 args->valuelen = valuelen;
2396 }
Dave Chinner517c2222013-04-24 18:58:55 +10002397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
2400/*========================================================================
2401 * Utility routines.
2402 *========================================================================*/
2403
2404/*
2405 * Move the indicated entries from one leaf to another.
2406 * NOTE: this routine modifies both source and destination leaves.
2407 */
2408/*ARGSUSED*/
2409STATIC void
Dave Chinner517c2222013-04-24 18:58:55 +10002410xfs_attr3_leaf_moveents(
2411 struct xfs_attr_leafblock *leaf_s,
2412 struct xfs_attr3_icleaf_hdr *ichdr_s,
2413 int start_s,
2414 struct xfs_attr_leafblock *leaf_d,
2415 struct xfs_attr3_icleaf_hdr *ichdr_d,
2416 int start_d,
2417 int count,
2418 struct xfs_mount *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Dave Chinner517c2222013-04-24 18:58:55 +10002420 struct xfs_attr_leaf_entry *entry_s;
2421 struct xfs_attr_leaf_entry *entry_d;
2422 int desti;
2423 int tmp;
2424 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 /*
2427 * Check for nothing to do.
2428 */
2429 if (count == 0)
2430 return;
2431
2432 /*
2433 * Set up environment.
2434 */
Dave Chinner517c2222013-04-24 18:58:55 +10002435 ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2436 ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2437 ASSERT(ichdr_s->magic == ichdr_d->magic);
2438 ASSERT(ichdr_s->count > 0 && ichdr_s->count < XFS_LBSIZE(mp) / 8);
2439 ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2440 + xfs_attr3_leaf_hdr_size(leaf_s));
2441 ASSERT(ichdr_d->count < XFS_LBSIZE(mp) / 8);
2442 ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2443 + xfs_attr3_leaf_hdr_size(leaf_d));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Dave Chinner517c2222013-04-24 18:58:55 +10002445 ASSERT(start_s < ichdr_s->count);
2446 ASSERT(start_d <= ichdr_d->count);
2447 ASSERT(count <= ichdr_s->count);
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 /*
2451 * Move the entries in the destination leaf up to make a hole?
2452 */
Dave Chinner517c2222013-04-24 18:58:55 +10002453 if (start_d < ichdr_d->count) {
2454 tmp = ichdr_d->count - start_d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 tmp *= sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002456 entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2457 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2458 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460
2461 /*
2462 * Copy all entry's in the same (sorted) order,
2463 * but allocate attribute info packed and in sequence.
2464 */
Dave Chinner517c2222013-04-24 18:58:55 +10002465 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2466 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 desti = start_d;
2468 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
Dave Chinner517c2222013-04-24 18:58:55 +10002469 ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2471#ifdef GROT
2472 /*
2473 * Code to drop INCOMPLETE entries. Difficult to use as we
2474 * may also need to change the insertion index. Code turned
2475 * off for 6.2, should be revisited later.
2476 */
2477 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
Dave Chinner517c2222013-04-24 18:58:55 +10002478 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2479 ichdr_s->usedbytes -= tmp;
2480 ichdr_s->count -= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 entry_d--; /* to compensate for ++ in loop hdr */
2482 desti--;
2483 if ((start_s + i) < offset)
2484 result++; /* insertion index adjustment */
2485 } else {
2486#endif /* GROT */
Dave Chinner517c2222013-04-24 18:58:55 +10002487 ichdr_d->firstused -= tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /* both on-disk, don't endian flip twice */
2489 entry_d->hashval = entry_s->hashval;
Dave Chinner517c2222013-04-24 18:58:55 +10002490 entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 entry_d->flags = entry_s->flags;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002492 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 <= XFS_LBSIZE(mp));
Dave Chinner517c2222013-04-24 18:58:55 +10002494 memmove(xfs_attr3_leaf_name(leaf_d, desti),
2495 xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002496 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 <= XFS_LBSIZE(mp));
Dave Chinner517c2222013-04-24 18:58:55 +10002498 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2499 ichdr_s->usedbytes -= tmp;
2500 ichdr_d->usedbytes += tmp;
2501 ichdr_s->count -= 1;
2502 ichdr_d->count += 1;
2503 tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2504 + xfs_attr3_leaf_hdr_size(leaf_d);
2505 ASSERT(ichdr_d->firstused >= tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506#ifdef GROT
2507 }
2508#endif /* GROT */
2509 }
2510
2511 /*
2512 * Zero out the entries we just copied.
2513 */
Dave Chinner517c2222013-04-24 18:58:55 +10002514 if (start_s == ichdr_s->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002516 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 ASSERT(((char *)entry_s + tmp) <=
2518 ((char *)leaf_s + XFS_LBSIZE(mp)));
Dave Chinner517c2222013-04-24 18:58:55 +10002519 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 } else {
2521 /*
2522 * Move the remaining entries down to fill the hole,
2523 * then zero the entries at the top.
2524 */
Dave Chinner517c2222013-04-24 18:58:55 +10002525 tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2526 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2527 entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2528 memmove(entry_d, entry_s, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 tmp = count * sizeof(xfs_attr_leaf_entry_t);
Dave Chinner517c2222013-04-24 18:58:55 +10002531 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 ASSERT(((char *)entry_s + tmp) <=
2533 ((char *)leaf_s + XFS_LBSIZE(mp)));
Dave Chinner517c2222013-04-24 18:58:55 +10002534 memset(entry_s, 0, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536
2537 /*
2538 * Fill in the freemap information
2539 */
Dave Chinner517c2222013-04-24 18:58:55 +10002540 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2541 ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2542 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2543 ichdr_d->freemap[1].base = 0;
2544 ichdr_d->freemap[2].base = 0;
2545 ichdr_d->freemap[1].size = 0;
2546 ichdr_d->freemap[2].size = 0;
2547 ichdr_s->holes = 1; /* leaf may not be compact */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
2550/*
2551 * Pick up the last hashvalue from a leaf block.
2552 */
2553xfs_dahash_t
Dave Chinner1d9025e2012-06-22 18:50:14 +10002554xfs_attr_leaf_lasthash(
2555 struct xfs_buf *bp,
2556 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Dave Chinner517c2222013-04-24 18:58:55 +10002558 struct xfs_attr3_icleaf_hdr ichdr;
2559 struct xfs_attr_leaf_entry *entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Dave Chinner517c2222013-04-24 18:58:55 +10002561 xfs_attr3_leaf_hdr_from_disk(&ichdr, bp->b_addr);
2562 entries = xfs_attr3_leaf_entryp(bp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (count)
Dave Chinner517c2222013-04-24 18:58:55 +10002564 *count = ichdr.count;
2565 if (!ichdr.count)
2566 return 0;
2567 return be32_to_cpu(entries[ichdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570/*
2571 * Calculate the number of bytes used to store the indicated attribute
2572 * (whether local or remote only calculate bytes in this block).
2573 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002574STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2576{
Dave Chinner517c2222013-04-24 18:58:55 +10002577 struct xfs_attr_leaf_entry *entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 xfs_attr_leaf_name_local_t *name_loc;
2579 xfs_attr_leaf_name_remote_t *name_rmt;
2580 int size;
2581
Dave Chinner517c2222013-04-24 18:58:55 +10002582 entries = xfs_attr3_leaf_entryp(leaf);
2583 if (entries[index].flags & XFS_ATTR_LOCAL) {
2584 name_loc = xfs_attr3_leaf_name_local(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002585 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
Nathan Scott053b5752006-03-17 17:29:09 +11002586 be16_to_cpu(name_loc->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002588 name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002589 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
Dave Chinner517c2222013-04-24 18:58:55 +10002591 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
2594/*
2595 * Calculate the number of bytes that would be required to store the new
2596 * attribute (whether local or remote only calculate bytes in this block).
2597 * This routine decides as a side effect whether the attribute will be
2598 * a "local" or a "remote" attribute.
2599 */
2600int
Nathan Scottaa82daa2005-11-02 10:33:33 +11002601xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
2603 int size;
2604
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002605 size = xfs_attr_leaf_entsize_local(namelen, valuelen);
2606 if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (local) {
2608 *local = 1;
2609 }
2610 } else {
Eric Sandeenc9fb86a2009-01-01 16:40:11 -06002611 size = xfs_attr_leaf_entsize_remote(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (local) {
2613 *local = 0;
2614 }
2615 }
Dave Chinner517c2222013-04-24 18:58:55 +10002616 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
2619/*
2620 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2621 */
2622int
Dave Chinner517c2222013-04-24 18:58:55 +10002623xfs_attr3_leaf_list_int(
2624 struct xfs_buf *bp,
2625 struct xfs_attr_list_context *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Dave Chinner517c2222013-04-24 18:58:55 +10002627 struct attrlist_cursor_kern *cursor;
2628 struct xfs_attr_leafblock *leaf;
2629 struct xfs_attr3_icleaf_hdr ichdr;
2630 struct xfs_attr_leaf_entry *entries;
2631 struct xfs_attr_leaf_entry *entry;
2632 int retval;
2633 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002635 trace_xfs_attr_list_leaf(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Dave Chinner517c2222013-04-24 18:58:55 +10002637 leaf = bp->b_addr;
2638 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2639 entries = xfs_attr3_leaf_entryp(leaf);
2640
2641 cursor = context->cursor;
2642 cursor->initted = 1;
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 /*
2645 * Re-find our place in the leaf block if this is a new syscall.
2646 */
2647 if (context->resynch) {
Dave Chinner517c2222013-04-24 18:58:55 +10002648 entry = &entries[0];
2649 for (i = 0; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002650 if (be32_to_cpu(entry->hashval) == cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 if (cursor->offset == context->dupcnt) {
2652 context->dupcnt = 0;
2653 break;
2654 }
2655 context->dupcnt++;
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002656 } else if (be32_to_cpu(entry->hashval) >
2657 cursor->hashval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 context->dupcnt = 0;
2659 break;
2660 }
2661 }
Dave Chinner517c2222013-04-24 18:58:55 +10002662 if (i == ichdr.count) {
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002663 trace_xfs_attr_list_notfound(context);
Dave Chinner517c2222013-04-24 18:58:55 +10002664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002667 entry = &entries[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 i = 0;
2669 }
2670 context->resynch = 0;
2671
2672 /*
2673 * We have found our place, start copying out the new attributes.
2674 */
2675 retval = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10002676 for (; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002677 if (be32_to_cpu(entry->hashval) != cursor->hashval) {
2678 cursor->hashval = be32_to_cpu(entry->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 cursor->offset = 0;
2680 }
2681
2682 if (entry->flags & XFS_ATTR_INCOMPLETE)
2683 continue; /* skip incomplete entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 if (entry->flags & XFS_ATTR_LOCAL) {
Tim Shimmin726801b2006-09-28 11:01:37 +10002686 xfs_attr_leaf_name_local_t *name_loc =
Dave Chinner517c2222013-04-24 18:58:55 +10002687 xfs_attr3_leaf_name_local(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002688
2689 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002690 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002691 name_loc->nameval,
Tim Shimmin726801b2006-09-28 11:01:37 +10002692 (int)name_loc->namelen,
2693 be16_to_cpu(name_loc->valuelen),
Dave Chinnera9273ca2010-01-20 10:47:48 +11002694 &name_loc->nameval[name_loc->namelen]);
Tim Shimmin726801b2006-09-28 11:01:37 +10002695 if (retval)
2696 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002698 xfs_attr_leaf_name_remote_t *name_rmt =
Dave Chinner517c2222013-04-24 18:58:55 +10002699 xfs_attr3_leaf_name_remote(leaf, i);
Tim Shimmin726801b2006-09-28 11:01:37 +10002700
2701 int valuelen = be32_to_cpu(name_rmt->valuelen);
2702
2703 if (context->put_value) {
2704 xfs_da_args_t args;
2705
2706 memset((char *)&args, 0, sizeof(args));
2707 args.dp = context->dp;
2708 args.whichfork = XFS_ATTR_FORK;
2709 args.valuelen = valuelen;
Dave Chinner622d8142010-12-23 11:57:37 +11002710 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
Tim Shimmin726801b2006-09-28 11:01:37 +10002711 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2712 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
2713 retval = xfs_attr_rmtval_get(&args);
2714 if (retval)
2715 return retval;
2716 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002717 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002718 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002719 (int)name_rmt->namelen,
2720 valuelen,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002721 args.value);
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002722 kmem_free(args.value);
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002723 } else {
Tim Shimmin726801b2006-09-28 11:01:37 +10002724 retval = context->put_listent(context,
Christoph Hellwigad9b4632008-06-23 13:23:48 +10002725 entry->flags,
Dave Chinnera9273ca2010-01-20 10:47:48 +11002726 name_rmt->name,
Tim Shimmin726801b2006-09-28 11:01:37 +10002727 (int)name_rmt->namelen,
2728 valuelen,
2729 NULL);
2730 }
2731 if (retval)
2732 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
Tim Shimmin726801b2006-09-28 11:01:37 +10002734 if (context->seen_enough)
2735 break;
2736 cursor->offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002738 trace_xfs_attr_list_leaf_end(context);
Dave Chinner517c2222013-04-24 18:58:55 +10002739 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743/*========================================================================
2744 * Manage the INCOMPLETE flag in a leaf entry
2745 *========================================================================*/
2746
2747/*
2748 * Clear the INCOMPLETE flag on an entry in a leaf block.
2749 */
2750int
Dave Chinner517c2222013-04-24 18:58:55 +10002751xfs_attr3_leaf_clearflag(
2752 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Dave Chinner517c2222013-04-24 18:58:55 +10002754 struct xfs_attr_leafblock *leaf;
2755 struct xfs_attr_leaf_entry *entry;
2756 struct xfs_attr_leaf_name_remote *name_rmt;
2757 struct xfs_buf *bp;
2758 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002760 struct xfs_attr3_icleaf_hdr ichdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 xfs_attr_leaf_name_local_t *name_loc;
2762 int namelen;
2763 char *name;
2764#endif /* DEBUG */
2765
Dave Chinner5a5881c2012-03-22 05:15:13 +00002766 trace_xfs_attr_leaf_clearflag(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 /*
2768 * Set up the operation.
2769 */
Dave Chinner517c2222013-04-24 18:58:55 +10002770 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002771 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Dave Chinner1d9025e2012-06-22 18:50:14 +10002774 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002775 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2777
2778#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002779 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2780 ASSERT(args->index < ichdr.count);
2781 ASSERT(args->index >= 0);
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (entry->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002784 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 namelen = name_loc->namelen;
2786 name = (char *)name_loc->nameval;
2787 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002788 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 namelen = name_rmt->namelen;
2790 name = (char *)name_rmt->name;
2791 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002792 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 ASSERT(namelen == args->namelen);
2794 ASSERT(memcmp(name, args->name, namelen) == 0);
2795#endif /* DEBUG */
2796
2797 entry->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002798 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2800
2801 if (args->rmtblkno) {
2802 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002803 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002804 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2805 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002806 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 /*
2811 * Commit the flag value change and start the next trans in series.
2812 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002813 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
2816/*
2817 * Set the INCOMPLETE flag on an entry in a leaf block.
2818 */
2819int
Dave Chinner517c2222013-04-24 18:58:55 +10002820xfs_attr3_leaf_setflag(
2821 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Dave Chinner517c2222013-04-24 18:58:55 +10002823 struct xfs_attr_leafblock *leaf;
2824 struct xfs_attr_leaf_entry *entry;
2825 struct xfs_attr_leaf_name_remote *name_rmt;
2826 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 int error;
Dave Chinner517c2222013-04-24 18:58:55 +10002828#ifdef DEBUG
2829 struct xfs_attr3_icleaf_hdr ichdr;
2830#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Dave Chinner5a5881c2012-03-22 05:15:13 +00002832 trace_xfs_attr_leaf_setflag(args);
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 /*
2835 * Set up the operation.
2836 */
Dave Chinner517c2222013-04-24 18:58:55 +10002837 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
Dave Chinnerad14c332012-11-12 22:54:16 +11002838 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Dave Chinner1d9025e2012-06-22 18:50:14 +10002841 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002842#ifdef DEBUG
2843 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
2844 ASSERT(args->index < ichdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 ASSERT(args->index >= 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002846#endif
2847 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2850 entry->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002851 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2853 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002854 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 name_rmt->valueblk = 0;
2856 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002857 xfs_trans_log_buf(args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
2861 /*
2862 * Commit the flag value change and start the next trans in series.
2863 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002864 return xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
2867/*
2868 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2869 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2870 * entry given by args->blkno2/index2.
2871 *
2872 * Note that they could be in different blocks, or in the same block.
2873 */
2874int
Dave Chinner517c2222013-04-24 18:58:55 +10002875xfs_attr3_leaf_flipflags(
2876 struct xfs_da_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
Dave Chinner517c2222013-04-24 18:58:55 +10002878 struct xfs_attr_leafblock *leaf1;
2879 struct xfs_attr_leafblock *leaf2;
2880 struct xfs_attr_leaf_entry *entry1;
2881 struct xfs_attr_leaf_entry *entry2;
2882 struct xfs_attr_leaf_name_remote *name_rmt;
2883 struct xfs_buf *bp1;
2884 struct xfs_buf *bp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 int error;
2886#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002887 struct xfs_attr3_icleaf_hdr ichdr1;
2888 struct xfs_attr3_icleaf_hdr ichdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 xfs_attr_leaf_name_local_t *name_loc;
2890 int namelen1, namelen2;
2891 char *name1, *name2;
2892#endif /* DEBUG */
2893
Dave Chinner5a5881c2012-03-22 05:15:13 +00002894 trace_xfs_attr_leaf_flipflags(args);
2895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 /*
2897 * Read the block containing the "old" attr
2898 */
Dave Chinner517c2222013-04-24 18:58:55 +10002899 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
Dave Chinnerad14c332012-11-12 22:54:16 +11002900 if (error)
2901 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 /*
2904 * Read the block containing the "new" attr, if it is different
2905 */
2906 if (args->blkno2 != args->blkno) {
Dave Chinner517c2222013-04-24 18:58:55 +10002907 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
Dave Chinnerad14c332012-11-12 22:54:16 +11002908 -1, &bp2);
2909 if (error)
2910 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 } else {
2912 bp2 = bp1;
2913 }
2914
Dave Chinner1d9025e2012-06-22 18:50:14 +10002915 leaf1 = bp1->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002916 entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Dave Chinner1d9025e2012-06-22 18:50:14 +10002918 leaf2 = bp2->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10002919 entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921#ifdef DEBUG
Dave Chinner517c2222013-04-24 18:58:55 +10002922 xfs_attr3_leaf_hdr_from_disk(&ichdr1, leaf1);
2923 ASSERT(args->index < ichdr1.count);
2924 ASSERT(args->index >= 0);
2925
2926 xfs_attr3_leaf_hdr_from_disk(&ichdr2, leaf2);
2927 ASSERT(args->index2 < ichdr2.count);
2928 ASSERT(args->index2 >= 0);
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (entry1->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002931 name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 namelen1 = name_loc->namelen;
2933 name1 = (char *)name_loc->nameval;
2934 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002935 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 namelen1 = name_rmt->namelen;
2937 name1 = (char *)name_rmt->name;
2938 }
2939 if (entry2->flags & XFS_ATTR_LOCAL) {
Dave Chinner517c2222013-04-24 18:58:55 +10002940 name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 namelen2 = name_loc->namelen;
2942 name2 = (char *)name_loc->nameval;
2943 } else {
Dave Chinner517c2222013-04-24 18:58:55 +10002944 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 namelen2 = name_rmt->namelen;
2946 name2 = (char *)name_rmt->name;
2947 }
Nathan Scott6b19f2d2006-03-17 17:29:02 +11002948 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 ASSERT(namelen1 == namelen2);
2950 ASSERT(memcmp(name1, name2, namelen1) == 0);
2951#endif /* DEBUG */
2952
2953 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2954 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2955
2956 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002957 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2959 if (args->rmtblkno) {
2960 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
Dave Chinner517c2222013-04-24 18:58:55 +10002961 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
Nathan Scottc0f054e2006-03-17 17:29:18 +11002962 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2963 name_rmt->valuelen = cpu_to_be32(args->valuelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002964 xfs_trans_log_buf(args->trans, bp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2966 }
2967
2968 entry2->flags |= XFS_ATTR_INCOMPLETE;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002969 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2971 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
Dave Chinner517c2222013-04-24 18:58:55 +10002972 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 name_rmt->valueblk = 0;
2974 name_rmt->valuelen = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002975 xfs_trans_log_buf(args->trans, bp2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 /*
2980 * Commit the flag value change and start the next trans in series.
2981 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10002982 error = xfs_trans_roll(&args->trans, args->dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Dave Chinner517c2222013-04-24 18:58:55 +10002984 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
2986
2987/*========================================================================
2988 * Indiscriminately delete the entire attribute fork
2989 *========================================================================*/
2990
2991/*
2992 * Recurse (gasp!) through the attribute nodes until we find leaves.
2993 * We're doing a depth-first traversal in order to invalidate everything.
2994 */
2995int
Dave Chinner517c2222013-04-24 18:58:55 +10002996xfs_attr3_root_inactive(
2997 struct xfs_trans **trans,
2998 struct xfs_inode *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Dave Chinner517c2222013-04-24 18:58:55 +10003000 struct xfs_da_blkinfo *info;
3001 struct xfs_buf *bp;
3002 xfs_daddr_t blkno;
3003 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005 /*
3006 * Read block 0 to see what we have to work with.
3007 * We only get here if we have extents, since we remove
3008 * the extents in reverse order the extent containing
3009 * block 0 must still be there.
3010 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003011 error = xfs_da3_node_read(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003013 return error;
3014 blkno = bp->b_bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016 /*
3017 * Invalidate the tree, even if the "tree" is only a single leaf block.
3018 * This is a depth-first traversal!
3019 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10003020 info = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003021 switch (info->magic) {
3022 case cpu_to_be16(XFS_DA_NODE_MAGIC):
3023 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
3024 error = xfs_attr3_node_inactive(trans, dp, bp, 1);
3025 break;
3026 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
3027 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
3028 error = xfs_attr3_leaf_inactive(trans, dp, bp);
3029 break;
3030 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003032 xfs_trans_brelse(*trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
3035 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003036 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 /*
3039 * Invalidate the incore copy of the root block.
3040 */
3041 error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
3042 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003043 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003044 xfs_trans_binval(*trans, bp); /* remove from cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 /*
3046 * Commit the invalidate and start the next transaction.
3047 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003048 error = xfs_trans_roll(trans, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Dave Chinner517c2222013-04-24 18:58:55 +10003050 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
3053/*
3054 * Recurse (gasp!) through the attribute nodes until we find leaves.
3055 * We're doing a depth-first traversal in order to invalidate everything.
3056 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003057STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10003058xfs_attr3_node_inactive(
Dave Chinner1d9025e2012-06-22 18:50:14 +10003059 struct xfs_trans **trans,
3060 struct xfs_inode *dp,
3061 struct xfs_buf *bp,
3062 int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 xfs_da_blkinfo_t *info;
3065 xfs_da_intnode_t *node;
3066 xfs_dablk_t child_fsb;
3067 xfs_daddr_t parent_blkno, child_blkno;
Dave Chinner517c2222013-04-24 18:58:55 +10003068 int error, i;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003069 struct xfs_buf *child_bp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003070 struct xfs_da_node_entry *btree;
Dave Chinner517c2222013-04-24 18:58:55 +10003071 struct xfs_da3_icnode_hdr ichdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 /*
3074 * Since this code is recursive (gasp!) we must protect ourselves.
3075 */
3076 if (level > XFS_DA_NODE_MAXDEPTH) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10003077 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Dave Chinner517c2222013-04-24 18:58:55 +10003078 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
3080
Dave Chinner1d9025e2012-06-22 18:50:14 +10003081 node = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003082 xfs_da3_node_hdr_from_disk(&ichdr, node);
3083 parent_blkno = bp->b_bn;
3084 if (!ichdr.count) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10003085 xfs_trans_brelse(*trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003088 btree = xfs_da3_node_tree_p(node);
3089 child_fsb = be32_to_cpu(btree[0].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003090 xfs_trans_brelse(*trans, bp); /* no locks for later trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
3092 /*
3093 * If this is the node level just above the leaves, simply loop
3094 * over the leaves removing all of them. If this is higher up
3095 * in the tree, recurse downward.
3096 */
Dave Chinner517c2222013-04-24 18:58:55 +10003097 for (i = 0; i < ichdr.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 /*
3099 * Read the subsidiary block to see what we have to work with.
3100 * Don't do this in a transaction. This is a depth-first
3101 * traversal of the tree so we may deal with many blocks
3102 * before we come back to this one.
3103 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003104 error = xfs_da3_node_read(*trans, dp, child_fsb, -2, &child_bp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11003105 XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (error)
3107 return(error);
3108 if (child_bp) {
3109 /* save for re-read later */
Dave Chinner1d9025e2012-06-22 18:50:14 +10003110 child_blkno = XFS_BUF_ADDR(child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 /*
3113 * Invalidate the subtree, however we have to.
3114 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10003115 info = child_bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003116 switch (info->magic) {
3117 case cpu_to_be16(XFS_DA_NODE_MAGIC):
3118 case cpu_to_be16(XFS_DA3_NODE_MAGIC):
3119 error = xfs_attr3_node_inactive(trans, dp,
3120 child_bp, level + 1);
3121 break;
3122 case cpu_to_be16(XFS_ATTR_LEAF_MAGIC):
3123 case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
3124 error = xfs_attr3_leaf_inactive(trans, dp,
3125 child_bp);
3126 break;
3127 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 error = XFS_ERROR(EIO);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003129 xfs_trans_brelse(*trans, child_bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
3132 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003133 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
3135 /*
3136 * Remove the subsidiary block from the cache
3137 * and from the log.
3138 */
3139 error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
3140 &child_bp, XFS_ATTR_FORK);
3141 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003142 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10003143 xfs_trans_binval(*trans, child_bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
3145
3146 /*
3147 * If we're not done, re-read the parent to get the next
3148 * child block number.
3149 */
Dave Chinner517c2222013-04-24 18:58:55 +10003150 if (i + 1 < ichdr.count) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003151 error = xfs_da3_node_read(*trans, dp, 0, parent_blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11003152 &bp, XFS_ATTR_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003154 return error;
3155 child_fsb = be32_to_cpu(btree[i + 1].before);
Dave Chinner1d9025e2012-06-22 18:50:14 +10003156 xfs_trans_brelse(*trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
3158 /*
3159 * Atomically commit the whole invalidate stuff.
3160 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003161 error = xfs_trans_roll(trans, dp);
3162 if (error)
Dave Chinner517c2222013-04-24 18:58:55 +10003163 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 }
3165
Dave Chinner517c2222013-04-24 18:58:55 +10003166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
3169/*
3170 * Invalidate all of the "remote" value regions pointed to by a particular
3171 * leaf block.
3172 * Note that we must release the lock on the buffer so that we are not
3173 * caught holding something that the logging code wants to flush to disk.
3174 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003175STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10003176xfs_attr3_leaf_inactive(
3177 struct xfs_trans **trans,
3178 struct xfs_inode *dp,
3179 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
Dave Chinner517c2222013-04-24 18:58:55 +10003181 struct xfs_attr_leafblock *leaf;
3182 struct xfs_attr3_icleaf_hdr ichdr;
3183 struct xfs_attr_leaf_entry *entry;
3184 struct xfs_attr_leaf_name_remote *name_rmt;
3185 struct xfs_attr_inactive_list *list;
3186 struct xfs_attr_inactive_list *lp;
3187 int error;
3188 int count;
3189 int size;
3190 int tmp;
3191 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Dave Chinner1d9025e2012-06-22 18:50:14 +10003193 leaf = bp->b_addr;
Dave Chinner517c2222013-04-24 18:58:55 +10003194 xfs_attr3_leaf_hdr_from_disk(&ichdr, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196 /*
3197 * Count the number of "remote" value extents.
3198 */
3199 count = 0;
Dave Chinner517c2222013-04-24 18:58:55 +10003200 entry = xfs_attr3_leaf_entryp(leaf);
3201 for (i = 0; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11003202 if (be16_to_cpu(entry->nameidx) &&
3203 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10003204 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (name_rmt->valueblk)
3206 count++;
3207 }
3208 }
3209
3210 /*
3211 * If there are no "remote" values, we're done.
3212 */
3213 if (count == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10003214 xfs_trans_brelse(*trans, bp);
Dave Chinner517c2222013-04-24 18:58:55 +10003215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
3217
3218 /*
3219 * Allocate storage for a list of all the "remote" value extents.
3220 */
3221 size = count * sizeof(xfs_attr_inactive_list_t);
Dave Chinner517c2222013-04-24 18:58:55 +10003222 list = kmem_alloc(size, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 /*
3225 * Identify each of the "remote" value extents.
3226 */
3227 lp = list;
Dave Chinner517c2222013-04-24 18:58:55 +10003228 entry = xfs_attr3_leaf_entryp(leaf);
3229 for (i = 0; i < ichdr.count; entry++, i++) {
Nathan Scott6b19f2d2006-03-17 17:29:02 +11003230 if (be16_to_cpu(entry->nameidx) &&
3231 ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
Dave Chinner517c2222013-04-24 18:58:55 +10003232 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 if (name_rmt->valueblk) {
Nathan Scottd7929ff2006-03-17 17:29:36 +11003234 lp->valueblk = be32_to_cpu(name_rmt->valueblk);
3235 lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
3236 be32_to_cpu(name_rmt->valuelen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 lp++;
3238 }
3239 }
3240 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10003241 xfs_trans_brelse(*trans, bp); /* unlock for trans. in freextent() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 /*
3244 * Invalidate each of the "remote" value extents.
3245 */
3246 error = 0;
3247 for (lp = list, i = 0; i < count; i++, lp++) {
Dave Chinner517c2222013-04-24 18:58:55 +10003248 tmp = xfs_attr3_leaf_freextent(trans, dp,
Nathan Scottd7929ff2006-03-17 17:29:36 +11003249 lp->valueblk, lp->valuelen);
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (error == 0)
3252 error = tmp; /* save only the 1st errno */
3253 }
3254
Dave Chinner517c2222013-04-24 18:58:55 +10003255 kmem_free(list);
3256 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
3258
3259/*
3260 * Look at all the extents for this logical region,
3261 * invalidate any buffers that are incore/in transactions.
3262 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003263STATIC int
Dave Chinner517c2222013-04-24 18:58:55 +10003264xfs_attr3_leaf_freextent(
3265 struct xfs_trans **trans,
3266 struct xfs_inode *dp,
3267 xfs_dablk_t blkno,
3268 int blkcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
Dave Chinner517c2222013-04-24 18:58:55 +10003270 struct xfs_bmbt_irec map;
3271 struct xfs_buf *bp;
3272 xfs_dablk_t tblkno;
3273 xfs_daddr_t dblkno;
3274 int tblkcnt;
3275 int dblkcnt;
3276 int nmap;
3277 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279 /*
3280 * Roll through the "value", invalidating the attribute value's
3281 * blocks.
3282 */
3283 tblkno = blkno;
3284 tblkcnt = blkcnt;
3285 while (tblkcnt > 0) {
3286 /*
3287 * Try to remember where we decided to put the value.
3288 */
3289 nmap = 1;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003290 error = xfs_bmapi_read(dp, (xfs_fileoff_t)tblkno, tblkcnt,
3291 &map, &nmap, XFS_BMAPI_ATTRFORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (error) {
3293 return(error);
3294 }
3295 ASSERT(nmap == 1);
3296 ASSERT(map.br_startblock != DELAYSTARTBLOCK);
3297
3298 /*
3299 * If it's a hole, these are already unmapped
3300 * so there's nothing to invalidate.
3301 */
3302 if (map.br_startblock != HOLESTARTBLOCK) {
3303
3304 dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
3305 map.br_startblock);
3306 dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
3307 map.br_blockcount);
3308 bp = xfs_trans_get_buf(*trans,
3309 dp->i_mount->m_ddev_targp,
Dave Chinnera8acad72012-04-23 15:58:54 +10003310 dblkno, dblkcnt, 0);
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00003311 if (!bp)
3312 return ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 xfs_trans_binval(*trans, bp);
3314 /*
3315 * Roll to next transaction.
3316 */
Niv Sardi322ff6b2008-08-13 16:05:49 +10003317 error = xfs_trans_roll(trans, dp);
3318 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 return (error);
3320 }
3321
3322 tblkno += map.br_blockcount;
3323 tblkcnt -= map.br_blockcount;
3324 }
3325
3326 return(0);
3327}